commands: distinct

A whole command that did not exist: five corpus cases answered "no such
command". Two things about it were measured against mongod 8.3.7 rather
than recalled, and the first is not what anyone would guess.

  - The answer is **sorted in canonical BSON order**, not in the order the
    values were met. `{s: "b"}, {s: "a"}, {s: null}` answers
    `[null, "a", "b"]`. Insertion order is the obvious implementation, it
    passes every test anybody would think to write by hand against
    `[11, 22, 33]`, and it is wrong.
  - Deduping is the same comparator, so an int32 `1` and a double `1.0`
    collapse while `null` and `"1"` survive.

Both fall out of `bson.compare`, which `$sort` and `$min` already use --
and that is not luck: mongod accumulates into a `BSONElementSet` ordered by
the same `woCompare`. The rest reuses the shared read path: byte-walked
`collect_values_bytes` for the key, so the traversal, the multikey descent
and the numeric path segments are the ones the matcher and the index
already agree on.

Also measured: a terminal array contributes its elements exactly one level
deep (`[[7, 8], 9]` gives `[7, 8]` and `9`, never 7 and 8); a missing field
contributes nothing where an explicit null contributes null; an absent
collection, an absent database and an empty key are each `ok: 1` with an
empty array rather than an error; `query` absent and `query: null` are both
an empty filter; a missing `key` is IDLFailedToParse (40414) while a
wrong-typed one is TypeMismatch (14).

Running the identical probe against both servers now agrees on every
semantic row. Three divergences remain, all outside this command and
recorded in PLAN §6: an unknown query operator matches nothing instead of
erroring (shared with find/count/aggregate, and the same class as M2's six
silent wrong answers), a non-string collection name is refused by dispatch
as BadValue where mongod says InvalidNamespace, and an unknown top-level
field is tolerated -- deliberately, since `comment` and `rawData` arrive
through that door and the corpus requires both be ignored.

crud scorecard: 201 pass / 90 fail -> 204 / 87. distinct.json 0/2 -> 2/0,
distinct-rawdata 0/1 -> 1/0. distinct-comment nets zero: its "no such
command" is replaced by the pre-4.4.14 document-comment case, which
`estimatedDocumentCount` already carries as a standing failure -- and
emulating a bug fixed in 4.4.14 for one command would make the two
disagree. distinct-collation still needs M8, but now fails with the honest
"expected 1 elements, got 2".

198/198 unit (7 new) in ReleaseFast and ReleaseSafe, 83/83 fuzz, aggregation
corpus 70/0, full e2e matrix and crash-fuzz green.
This commit is contained in:
A.Shakhmatov
2026-08-09 23:16:54 +03:00
parent fc611a2c64
commit 1f141ef619
2 changed files with 393 additions and 8 deletions

View File

@@ -18,7 +18,7 @@
# hasServerConnectionId, and maxTimeMS in an expected command (CSOT rewrites
# it -- the only assertion this runner declines to make).
total 201 pass 90 fail 196 skip 175 files 0 errored
total 204 pass 87 fail 196 skip 175 files 0 errored
# per-file: name pass fail skip
aggregate-allowdiskuse.json 3 0 0
@@ -110,8 +110,8 @@ deleteOne.json 3 0 0
distinct-collation.json 0 1 0
distinct-comment.json 1 1 1
distinct-hint.json 0 0 2
distinct-rawdata.json 0 1 1
distinct.json 0 2 0
distinct-rawdata.json 1 0 1
distinct.json 2 0 0
estimatedDocumentCount-comment.json 1 1 1
estimatedDocumentCount-rawdata.json 1 0 1
estimatedDocumentCount.json 2 1 3
@@ -318,14 +318,11 @@ deleteOne-hint-unacknowledged.json SKIP Unacknowledged deleteOne with hint docum
deleteOne-let.json SKIP deleteOne with let option needs server >= 5.0
deleteOne-let.json FAIL deleteOne with let option unsupported (server-side error) deleteOne: expected an error, the operation succeeded
deleteOne-rawdata.json SKIP deleteOne with rawData option needs server >= 8.2.0
distinct-collation.json FAIL Distinct with a collation MongoServerError: no such command: 'distinct'
distinct-collation.json FAIL Distinct with a collation distinct: expected 1 elements, got 2
distinct-comment.json SKIP distinct with document comment needs server >= 4.4.14
distinct-comment.json FAIL distinct with string comment MongoServerError: no such command: 'distinct'
distinct-comment.json FAIL distinct with document comment - pre 4.4, server error distinct: expected an error, the operation succeeded
distinct-hint.json SKIP * needs server >= 7.1.0
distinct-rawdata.json SKIP distinct with rawData option needs server >= 8.2.0
distinct-rawdata.json FAIL distinct with rawData option on less than 8.2.0 - ignore argument MongoServerError: no such command: 'distinct'
distinct.json FAIL Distinct without a filter MongoServerError: no such command: 'distinct'
distinct.json FAIL Distinct with a filter MongoServerError: no such command: 'distinct'
estimatedDocumentCount-comment.json SKIP estimatedDocumentCount with document comment needs server >= 4.4.14
estimatedDocumentCount-comment.json FAIL estimatedDocumentCount with document comment - pre 4.4.14, server error estimatedDocumentCount: expected an error, the operation succeeded
estimatedDocumentCount-rawdata.json SKIP Estimated document count with rawData option needs server >= 8.2.0