Skip to content

Generate document literals, compound group ids, $cond and $concatArrays - #4597

Open
evanchooly wants to merge 1 commit into
audits-find-projectionsfrom
audits-document-literals
Open

evanchooly wants to merge 1 commit into
audits-find-projectionsfrom
audits-document-literals

Conversation

@evanchooly

Copy link
Copy Markdown
Member

Stacked on #4596. Self-reviewed only.

What this solves

The generator refused every document literal ("a document literal has no binding") and every compound $group _id ("a compound _id has no binding"), and it had no binding for $cond or $concatArrays. That blocked examples Morphia can write. For example, { $group: { _id: { fiscal_year: "$fiscal_year", dept: "$dept" }, … } } in the two seeded $merge examples with paired reads, which #4592 couldn't generate. Another was the seven ranked-accumulator examples that set n from the group key with $cond.

10 cases now generate (487 → 497):

  • the seven "set n by the group key" examples ($bottomN, $firstN, $lastN, $maxN, $minN, $topN);
  • $map's $$IDX;
  • a nested $project inclusion;
  • the two $merge examples, which now run their documented read after the write.

None of the committed cases are removed. Integration: 509 tests under Critter, then reflection, 0 failures. Audits suite: 445 tests, 0 failures, 4 skipped.

How

  • Document literals: a {} catalog entry binds Expressions.document() as a FIELDS expression, so a document of expressions is document().field(k, v)…. A compound _id goes through it: group(id(document().field("y", "$year"))). So does a nested $project document, which the server reads as a nested projection. Literals are emitted only where the parameter is Object or Expression.
  • $cond: binds condition(if, then, else). Its {if, then, else} form becomes the array Morphia writes, under the existing COND_ARRAY rule.
  • $concatArrays: binds concatArrays(Object, Object...).

Gaps, each mirroring what Morphia or Java does

  • Document literals:
    • a literal with a null field, which DocumentExpressionCodec omits;
    • a literal with an operator key.
  • Array literals:
    • an array literal holding null, which ArrayLiteralCodec throws on;
    • a document anywhere inside a document literal's array, which ArrayLiteralCodec can't write there (BsonInvalidOperationException).
  • Arguments:
    • a null $cond operand (condition builds a List.of);
    • a lone null passed as a varargs argument (Java reads it as the array);
    • $concatArrays given one bare argument in its accumulator form (concatArrays always writes an array).
  • Projections and seed data:
    • a $project mixing exclusions with inclusions or values, which Projection.validateProjections rejects;
    • an example that prints results from a collection nothing seeds. $reduce's "Computing a Single Reduction" seeds arrayconcat elsewhere on its page, so both runs returned nothing.

Decisions for review

  • A possible core bug: Codex reproduced that an array holding a document, nested inside document().field(…), fails to encode, while the same array works elsewhere. I've only made it a gap here. It may deserve a core issue.
  • Special cases in the generator: the $cond and $concatArrays checks name the operator in CaseGenerator, as the earlier $text and $mod ones do. A general rule would wrongly reject the 20 existing accumulator cases (such as sum("$runtime")) that Morphia writes bare.
  • One more page error is a reviewed exclusion: $regexFindAll's "Use Captured Groupings to Parse User Name" prints the $addFields output from before the pipeline's $set, like the email examples in Generate query regex, $type, $text, $expr, $mod and regex expression cases #4595.

Review

Codex ran three local rounds, finding four, two and one issues. Every one is now a gap with a regression case in GeneratorTest. The design doc and the progress log are updated.

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant