- 
                Notifications
    
You must be signed in to change notification settings  - Fork 15
 
REP-6465 Fix handling of dotted shard keys #127
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
          
     Merged
      
      
            FGasper
  merged 33 commits into
  mongodb-labs:main
from
FGasper:REP-6465-fix-dotted-shard-key
  
      
      
   
  Aug 20, 2025 
      
    
  
     Merged
                    Changes from 26 commits
      Commits
    
    
            Show all changes
          
          
            33 commits
          
        
        Select commit
          Hold shift + click to select a range
      
      e3107ba
              
                add dockey
              
              
                FGasper 0ac82bc
              
                no base64
              
              
                FGasper 828f072
              
                refactor a bit
              
              
                FGasper bb0ac8b
              
                fix agg
              
              
                FGasper b965033
              
                bring in mongosync
              
              
                FGasper 2d6ca57
              
                add error check
              
              
                FGasper b137e99
              
                precreate
              
              
                FGasper 1f25562
              
                try older versions
              
              
                FGasper fc6b8c6
              
                back-compat
              
              
                FGasper c3121d4
              
                maybe fix 5
              
              
                FGasper 32563c9
              
                avoid pre-v6 wonkiness
              
              
                FGasper 8b6d91b
              
                comment
              
              
                FGasper 5460ac8
              
                rename DB … ??
              
              
                FGasper e23adab
              
                move test
              
              
                FGasper c9b78f5
              
                more
              
              
                FGasper 34c63cf
              
                fix test
              
              
                FGasper 9d90009
              
                Use the simpler logic that routing actually uses.
              
              
                FGasper a7c0de9
              
                rollback
              
              
                FGasper 0ed6ddd
              
                revert
              
              
                FGasper 578310c
              
                pare back
              
              
                FGasper f28c72f
              
                “true” doc key
              
              
                FGasper 0300b8b
              
                fix DB name
              
              
                FGasper 7c12d88
              
                fix test
              
              
                FGasper ac2b99a
              
                compat with pre-v6
              
              
                FGasper 7bcddaa
              
                only sharded for now
              
              
                FGasper 9f01a5f
              
                all CI
              
              
                FGasper 54d347a
              
                Update internal/verifier/dockey_agg_test.go
              
              
                FGasper 877a9cc
              
                refactor & add more tests
              
              
                FGasper c0ee5c4
              
                Merge branch 'REP-6465-fix-dotted-shard-key' of github.com:FGasper/mi…
              
              
                FGasper 5c01288
              
                comment
              
              
                FGasper 035fd76
              
                check reverse
              
              
                FGasper ed0aa0d
              
                panic & assert in tests
              
              
                FGasper b86061e
              
                no error check
              
              
                FGasper File filter
Filter by extension
Conversations
          Failed to load comments.   
        
        
          
      Loading
        
  Jump to
        
          Jump to file
        
      
      
          Failed to load files.   
        
        
          
      Loading
        
  Diff view
Diff view
There are no files selected for viewing
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
              | Original file line number | Diff line number | Diff line change | 
|---|---|---|
| @@ -0,0 +1,87 @@ | ||
| // Package dockey contains logic related to document key determination. | ||
| // Its tests use a cluster and thus are stored in internal/verifier. | ||
| 
     | 
||
| package dockey | ||
| 
     | 
||
| import ( | ||
| "maps" | ||
| "slices" | ||
| "strconv" | ||
| 
     | 
||
| "github.com/samber/lo" | ||
| "go.mongodb.org/mongo-driver/bson" | ||
| ) | ||
| 
     | 
||
| // ExtractTrueDocKeyAgg returns an aggregation expression that extracts the | ||
| // document key from the document to which the `docExpr` refers. | ||
| // | ||
| // NB: This avoids the problem documented in SERVER-109340; as a result, | ||
| // the returned key may not always match the change stream’s `documentKey` | ||
| // (because the server misreports its own sharding logic). | ||
| func ExtractTrueDocKeyAgg(fieldNames []string, docExpr string) bson.D { | ||
| var docKeyNumKeys bson.D | ||
| numToKeyLookup := map[string]string{} | ||
| 
     | 
||
| for n, name := range fieldNames { | ||
| var valExpr = docExpr + "." + name | ||
| 
     | 
||
| // Aggregation forbids direct creation of an object with dotted keys. | ||
| // So here we create an object with numeric keys, then below we’ll | ||
| // map the numeric keys back to the real ones. | ||
| 
     | 
||
| nStr := strconv.Itoa(n) | ||
| docKeyNumKeys = append(docKeyNumKeys, bson.E{nStr, valExpr}) | ||
| numToKeyLookup[nStr] = name | ||
| } | ||
| 
     | 
||
| // Now convert the numeric keys back to the real ones. | ||
| return mapObjectKeysAgg(docKeyNumKeys, numToKeyLookup) | ||
| } | ||
| 
     | 
||
| // Potentially reusable: | ||
| func mapObjectKeysAgg(expr any, mapping map[string]string) bson.D { | ||
| // We would ideally pass mapping into the aggregation and $getField | ||
| // to get the mapped key, but pre-v8 server versions required $getField’s | ||
| // field parameter to be a constant. (And pre-v5 didn’t have $getField | ||
| // at all.) So we use a $switch instead. | ||
| mapAgg := bson.D{ | ||
| {"$switch", bson.D{ | ||
| {"branches", lo.Map( | ||
| slices.Collect(maps.Keys(mapping)), | ||
| func(key string, _ int) bson.D { | ||
| return bson.D{ | ||
| {"case", bson.D{ | ||
| {"$eq", bson.A{ | ||
| key, | ||
| "$$numericKey", | ||
| }}, | ||
| }}, | ||
| {"then", mapping[key]}, | ||
| } | ||
| }, | ||
| )}, | ||
| }}, | ||
| } | ||
| 
     | 
||
| return bson.D{ | ||
| {"$arrayToObject", bson.D{ | ||
| {"$map", bson.D{ | ||
| {"input", bson.D{ | ||
| {"$objectToArray", expr}, | ||
| }}, | ||
| {"in", bson.D{ | ||
| {"$let", bson.D{ | ||
| {"vars", bson.D{ | ||
| {"numericKey", "$$this.k"}, | ||
| {"value", "$$this.v"}, | ||
| }}, | ||
| {"in", bson.D{ | ||
| {"k", mapAgg}, | ||
| {"v", "$$value"}, | ||
| }}, | ||
| }}, | ||
| }}, | ||
| }}, | ||
| }}, | ||
| } | ||
| } | 
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
              
      
      Oops, something went wrong.
        
    
  
  Add this suggestion to a batch that can be applied as a single commit.
  This suggestion is invalid because no changes were made to the code.
  Suggestions cannot be applied while the pull request is closed.
  Suggestions cannot be applied while viewing a subset of changes.
  Only one suggestion per line can be applied in a batch.
  Add this suggestion to a batch that can be applied as a single commit.
  Applying suggestions on deleted lines is not supported.
  You must change the existing code in this line in order to create a valid suggestion.
  Outdated suggestions cannot be applied.
  This suggestion has been applied or marked resolved.
  Suggestions cannot be applied from pending reviews.
  Suggestions cannot be applied on multi-line comments.
  Suggestions cannot be applied while the pull request is queued to merge.
  Suggestion cannot be applied right now. Please check back later.
  
    
  
    
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You probably also want to handle
bsoncore.ErrInvalidDepthTraversalErrorhere, which is the kind of error you get if you have a shard key likea.band theafield in the target document is an array.