tests/spec: record the expression evaluator's spec from mongod

27 cases, recorded before a line of the evaluator is written, which is the
whole point of having built the recorder first: the expectations come from
mongod 8.3.7 rather than from what the implementation is about to do.

The corpus reads 1 pass / 26 fail, and the one pass is the unknown-operator
refusal M2 already answers correctly. Expressions are exercised through
`$group` because `_id` and the accumulator arguments are the only expression
positions that exist until `$addFields` and `$project`'s computed fields land;
testing them anywhere else would be testing a stage that is not there.

Ten things the recording settled that guessing would have got wrong:

    $add over a missing field or null   null -- not an error, and not 0
    $add over a string                  error 7157723
    $divide by zero                     error 4848401
    $mod of -5 by 4                     -1, the dividend's sign
    $lt of a number and a string        true, canonical type order
    $and over -5                        truthy
    $not of a missing field             true
    $switch, no branch and no default   error 40069
    two operators in one expression     error 15983, *not* $group's 40238
    $subtract with one operand          error 16020

The six error codes are in `ErrorCode` already, so the evaluator's refusals and
its runtime failures have somewhere measured to land. The evaluator itself is
the next commit and is not started.

191/191 unit tests, crud corpus unchanged at 201/90/196.
This commit is contained in:
A.Shakhmatov
2026-08-09 22:12:48 +03:00
parent 76afa75efe
commit 1c72aa8938
3 changed files with 1814 additions and 5 deletions

View File

@@ -0,0 +1,649 @@
{
"_comment": [
"Inputs only. Expectations are measured -- see record.js.",
"Expressions are exercised through $group, because that is where this",
"server can reach them: $group's _id and its accumulator arguments are",
"the only expression positions until $addFields and $project's computed",
"fields land. A source that tested them anywhere else would be testing a",
"stage that does not exist yet."
],
"documents": [
{
"_id": 1,
"a": 10,
"b": 3,
"s": "x",
"n": null,
"f": false
},
{
"_id": 2,
"a": 20,
"b": 0,
"s": "y",
"f": true
},
{
"_id": 3,
"a": -5,
"b": 4,
"s": "x",
"n": 7,
"f": false
}
],
"cases": [
{
"description": "a compound _id groups on every field",
"pipeline": [
{
"$group": {
"_id": {
"s": "$s",
"f": "$f"
},
"n": {
"$sum": 1
}
}
},
{
"$sort": {
"_id.s": 1,
"_id.f": 1
}
}
]
},
{
"description": "a nested compound _id",
"pipeline": [
{
"$group": {
"_id": {
"outer": {
"inner": "$s"
}
},
"n": {
"$sum": 1
}
}
},
{
"$sort": {
"_id.outer.inner": 1
}
}
]
},
{
"description": "$literal keeps a $-string from being a path",
"pipeline": [
{
"$group": {
"_id": {
"$literal": "$s"
},
"n": {
"$sum": 1
}
}
}
]
},
{
"description": "$literal of a document",
"pipeline": [
{
"$group": {
"_id": {
"$literal": {
"$sum": 1
}
},
"n": {
"$sum": 1
}
}
}
]
},
{
"description": "$add of a path and a constant",
"pipeline": [
{
"$group": {
"_id": {
"$add": [
"$a",
1
]
},
"n": {
"$sum": 1
}
}
},
{
"$sort": {
"_id": 1
}
}
]
},
{
"description": "$subtract and $multiply",
"pipeline": [
{
"$group": {
"_id": "$s",
"d": {
"$sum": {
"$subtract": [
"$a",
"$b"
]
}
},
"m": {
"$sum": {
"$multiply": [
"$a",
2
]
}
}
}
},
{
"$sort": {
"_id": 1
}
}
]
},
{
"description": "$divide by zero is an error",
"pipeline": [
{
"$group": {
"_id": null,
"v": {
"$sum": {
"$divide": [
"$a",
"$b"
]
}
}
}
}
]
},
{
"description": "$divide and $mod",
"pipeline": [
{
"$match": {
"b": {
"$gt": 0
}
}
},
{
"$group": {
"_id": "$s",
"d": {
"$push": {
"$divide": [
"$a",
"$b"
]
}
},
"m": {
"$push": {
"$mod": [
"$a",
"$b"
]
}
}
}
},
{
"$sort": {
"_id": 1
}
}
]
},
{
"description": "arithmetic over a missing field",
"pipeline": [
{
"$group": {
"_id": {
"$add": [
"$missing",
1
]
},
"n": {
"$sum": 1
}
}
}
]
},
{
"description": "arithmetic over null",
"pipeline": [
{
"$group": {
"_id": "$s",
"v": {
"$push": {
"$add": [
"$n",
1
]
}
}
}
},
{
"$sort": {
"_id": 1
}
}
]
},
{
"description": "$eq and $ne",
"pipeline": [
{
"$group": {
"_id": {
"$eq": [
"$s",
"x"
]
},
"n": {
"$sum": 1
}
}
},
{
"$sort": {
"_id": 1
}
}
]
},
{
"description": "comparison across types follows canonical order",
"pipeline": [
{
"$group": {
"_id": {
"$lt": [
"$a",
"$s"
]
},
"n": {
"$sum": 1
}
}
},
{
"$sort": {
"_id": 1
}
}
]
},
{
"description": "$cmp returns the ordering",
"pipeline": [
{
"$group": {
"_id": "$s",
"v": {
"$push": {
"$cmp": [
"$a",
"$b"
]
}
}
}
},
{
"$sort": {
"_id": 1
}
}
]
},
{
"description": "$and and $or are truthy over non-booleans",
"pipeline": [
{
"$group": {
"_id": {
"$and": [
"$a",
"$f"
]
},
"n": {
"$sum": 1
}
}
},
{
"$sort": {
"_id": 1
}
}
]
},
{
"description": "$not of a missing field",
"pipeline": [
{
"$group": {
"_id": {
"$not": [
"$missing"
]
},
"n": {
"$sum": 1
}
}
}
]
},
{
"description": "$cond in its array form",
"pipeline": [
{
"$group": {
"_id": {
"$cond": [
{
"$gt": [
"$a",
0
]
},
"positive",
"negative"
]
},
"n": {
"$sum": 1
}
}
},
{
"$sort": {
"_id": 1
}
}
]
},
{
"description": "$cond in its document form",
"pipeline": [
{
"$group": {
"_id": {
"$cond": {
"if": "$f",
"then": "yes",
"else": "no"
}
},
"n": {
"$sum": 1
}
}
},
{
"$sort": {
"_id": 1
}
}
]
},
{
"description": "$ifNull falls through on missing and on null",
"pipeline": [
{
"$group": {
"_id": {
"$ifNull": [
"$n",
"fallback"
]
},
"count": {
"$sum": 1
}
}
},
{
"$sort": {
"_id": 1
}
}
]
},
{
"description": "$switch picks the first matching branch",
"pipeline": [
{
"$group": {
"_id": {
"$switch": {
"branches": [
{
"case": {
"$lt": [
"$a",
0
]
},
"then": "neg"
},
{
"case": {
"$lt": [
"$a",
15
]
},
"then": "small"
}
],
"default": "big"
}
},
"n": {
"$sum": 1
}
}
},
{
"$sort": {
"_id": 1
}
}
]
},
{
"description": "$switch with no branch matching and no default is an error",
"pipeline": [
{
"$group": {
"_id": {
"$switch": {
"branches": [
{
"case": false,
"then": "never"
}
]
}
},
"n": {
"$sum": 1
}
}
}
]
},
{
"description": "an unknown expression operator is refused",
"pipeline": [
{
"$group": {
"_id": {
"$bogusExpr": "$a"
},
"n": {
"$sum": 1
}
}
}
]
},
{
"description": "two operators in one expression document are refused",
"pipeline": [
{
"$group": {
"_id": {
"$add": [
"$a",
1
],
"$literal": 2
},
"n": {
"$sum": 1
}
}
}
]
},
{
"description": "a nested expression inside an accumulator argument",
"pipeline": [
{
"$group": {
"_id": null,
"v": {
"$max": {
"$add": [
{
"$multiply": [
"$a",
2
]
},
"$b"
]
}
}
}
}
]
},
{
"description": "arithmetic over a non-numeric value is an error",
"pipeline": [
{
"$group": {
"_id": {
"$add": [
"$s",
1
]
},
"n": {
"$sum": 1
}
}
}
]
},
{
"description": "$not takes a bare argument as well as an array",
"pipeline": [
{
"$group": {
"_id": {
"$not": "$f"
},
"n": {
"$sum": 1
}
}
},
{
"$sort": {
"_id": 1
}
}
]
},
{
"description": "$add of several operands",
"pipeline": [
{
"$group": {
"_id": {
"$add": [
"$a",
"$b",
100
]
},
"n": {
"$sum": 1
}
}
},
{
"$sort": {
"_id": 1
}
}
]
},
{
"description": "$subtract needs exactly two operands",
"pipeline": [
{
"$group": {
"_id": {
"$subtract": [
"$a"
]
},
"n": {
"$sum": 1
}
}
}
]
}
]
}