Skip to content
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

GODRIVER-3412 Add index hint support for distinct command. #1888

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -523,6 +523,8 @@ func executeDistinct(ctx context.Context, operation *operation) (*operationResul
val := elem.Value()

switch key {
case "hint":
opts.SetHint(val)
case "collation":
collation, err := createCollation(val.Document())
if err != nil {
Expand Down
7 changes: 7 additions & 0 deletions mongo/collection.go
Original file line number Diff line number Diff line change
Expand Up @@ -1281,6 +1281,13 @@ func (coll *Collection) Distinct(
}
op.Comment(comment)
}
if args.Hint != nil {
hint, err := marshalValue(args.Hint, coll.bsonOpts, coll.registry)
if err != nil {
return &DistinctResult{err: err}
}
op.Hint(hint)
}
retry := driver.RetryNone
if coll.client.retryReads {
retry = driver.RetryOncePerCommand
Expand Down
15 changes: 15 additions & 0 deletions mongo/options/distinctoptions.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ package options
type DistinctOptions struct {
Collation *Collation
Comment interface{}
Hint interface{}
}

// DistinctOptionsBuilder contains options to configure distinct operations. Each
Expand Down Expand Up @@ -60,3 +61,17 @@ func (do *DistinctOptionsBuilder) SetComment(comment interface{}) *DistinctOptio

return do
}

// SetHint sets the value for the Comment field. Specifies a string or document that
// will be included in server logs, profiling logs, and currentOp queries to help trace
// the operation. The default value is nil, which means that no comment will be included
// in the logs.
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We need to update the comments.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good catch! Will fix that.

func (do *DistinctOptionsBuilder) SetHint(hint interface{}) *DistinctOptionsBuilder {
do.Opts = append(do.Opts, func(opts *DistinctOptions) error {
opts.Hint = hint

return nil
})

return do
}
139 changes: 139 additions & 0 deletions testdata/crud/unified/distinct-hint.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,139 @@
{
"description": "distinct-hint",
"schemaVersion": "1.0",
"runOnRequirements": [
{
"minServerVersion": "7.1.0"
}
],
"createEntities": [
{
"client": {
"id": "client0",
"observeEvents": [
"commandStartedEvent"
]
}
},
{
"database": {
"id": "database0",
"client": "client0",
"databaseName": "distinct-hint-tests"
}
},
{
"collection": {
"id": "collection0",
"database": "database0",
"collectionName": "coll0"
}
}
],
"initialData": [
{
"collectionName": "coll0",
"databaseName": "distinct-hint-tests",
"documents": [
{
"_id": 1,
"x": 11
},
{
"_id": 2,
"x": 22
},
{
"_id": 3,
"x": 33
}
]
}
],
"tests": [
{
"description": "distinct with hint string",
"operations": [
{
"name": "distinct",
"object": "collection0",
"arguments": {
"fieldName": "x",
"filter": {
"_id": 1
},
"hint": "_id_"
},
"expectResult": [
11
]
}
],
"expectEvents": [
{
"client": "client0",
"events": [
{
"commandStartedEvent": {
"command": {
"distinct": "coll0",
"key": "x",
"query": {
"_id": 1
},
"hint": "_id_"
},
"commandName": "distinct",
"databaseName": "distinct-hint-tests"
}
}
]
}
]
},
{
"description": "distinct with hint document",
"operations": [
{
"name": "distinct",
"object": "collection0",
"arguments": {
"fieldName": "x",
"filter": {
"_id": 1
},
"hint": {
"_id": 1
}
},
"expectResult": [
11
]
}
],
"expectEvents": [
{
"client": "client0",
"events": [
{
"commandStartedEvent": {
"command": {
"distinct": "coll0",
"key": "x",
"query": {
"_id": 1
},
"hint": {
"_id": 1
}
},
"commandName": "distinct",
"databaseName": "distinct-hint-tests"
}
}
]
}
]
}
]
}
73 changes: 73 additions & 0 deletions testdata/crud/unified/distinct-hint.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
description: "distinct-hint"

schemaVersion: "1.0"
runOnRequirements:
# https://jira.mongodb.org/browse/SERVER-14227
# Server supports distinct with hint starting from 7.1.0.
- minServerVersion: "7.1.0"

createEntities:
- client:
id: &client0 client0
observeEvents: [ commandStartedEvent ]
- database:
id: &database0 database0
client: *client0
databaseName: &database0Name distinct-hint-tests
- collection:
id: &collection0 collection0
database: *database0
collectionName: &collection0Name coll0

initialData:
- collectionName: *collection0Name
databaseName: *database0Name
documents:
- { _id: 1, x: 11 }
- { _id: 2, x: 22 }
- { _id: 3, x: 33 }

tests:
- description: "distinct with hint string"
operations:
- name: distinct
object: *collection0
arguments:
fieldName: &fieldName x
filter: &filter { _id: 1 }
hint: _id_
expectResult: [ 11 ]
expectEvents:
- client: *client0
events:
- commandStartedEvent:
command:
distinct: *collection0Name
key: *fieldName
query: *filter
hint: _id_
commandName: distinct
databaseName: *database0Name

- description: "distinct with hint document"
operations:
- name: distinct
object: *collection0
arguments:
fieldName: *fieldName
filter: *filter
hint:
_id: 1
expectResult: [ 11 ]
expectEvents:
- client: *client0
events:
- commandStartedEvent:
command:
distinct: *collection0Name
key: *fieldName
query: *filter
hint:
_id: 1
commandName: distinct
databaseName: *database0Name
14 changes: 14 additions & 0 deletions x/mongo/driver/operation/distinct.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ type Distinct struct {
clock *session.ClusterClock
collection string
comment bsoncore.Value
hint bsoncore.Value
monitor *event.CommandMonitor
crypt driver.Crypt
database string
Expand Down Expand Up @@ -120,6 +121,9 @@ func (d *Distinct) command(dst []byte, desc description.SelectedServer) ([]byte,
if d.comment.Type != bsoncore.Type(0) {
dst = bsoncore.AppendValueElement(dst, "comment", d.comment)
}
if d.hint.Type != bsoncore.Type(0) {
dst = bsoncore.AppendValueElement(dst, "hint", d.hint)
}
if d.key != nil {
dst = bsoncore.AppendStringElement(dst, "key", *d.key)
}
Expand Down Expand Up @@ -199,6 +203,16 @@ func (d *Distinct) Comment(comment bsoncore.Value) *Distinct {
return d
}

// Hint sets a value to help trace an operation.
func (d *Distinct) Hint(hint bsoncore.Value) *Distinct {
if d == nil {
d = new(Distinct)
}

d.hint = hint
return d
}

// CommandMonitor sets the monitor to use for APM events.
func (d *Distinct) CommandMonitor(monitor *event.CommandMonitor) *Distinct {
if d == nil {
Expand Down
Loading