Skip to content

Generate query regex, $type, $text, $expr, $mod and regex expression cases - #4595

Open
evanchooly wants to merge 1 commit into
audits-write-generationfrom
audits-query-bindings
Open

evanchooly wants to merge 1 commit into
audits-write-generationfrom
audits-query-bindings

Conversation

@evanchooly

Copy link
Copy Markdown
Member

Stacked on #4592. Self-reviewed only.

What this solves

The generator had no bindings for $regex, $type, $text, $expr and $mod in queries, or for $regexFind, $regexFindAll and $regexMatch in expressions. That made about 60 documented examples gaps. For example, db.products.find( { sku: { $regex: /^ABC/i } } ) was reported as "query operator $regex has no binding", and six sample-dataset $group examples were blocked only by a { $type: "double" } in their $match.

32 cases now generate (445 → 477):

  • 6 $regex;
  • 4 $text;
  • 16 regex expressions;
  • 6 sample-dataset group examples that $type unblocked.

Integration: 489 tests under Critter, then reflection, 0 failures. Audits suite: 444 tests, 0 failures, 4 skipped.

How

Regex filters. A query FIELD binding with options takes a regular expression, and each flag becomes an option call: /^ABC/i is regex("sku", "^ABC").caseInsensitive(). {$regex: "p", $options: "o"} and {$regex: /p/, $options: "o"} become the one regular expression Morphia writes, under the existing REGEX_FORM rule.

$type. A varargs FIELD binding takes one value, or an array of two or more. A new constants field maps MQL aliases to Morphia's Type, where the camel-to-snake conversion is wrong: "int" is INTEGER_32_BIT.

$mod. A query FIELD binding with more than two parameters spreads its array over them: [d, r].

Top-level operators. In a query, a FILTER binding of another form is a top-level operator, read as that form reads its value: $expr is positional and $text is named. $text joins the named-argument operators, and both rule ports now apply that rule to top-level query operators.

Regex expressions. RegexExpressionCodec writes regex: /p/, options: "o", while pages write regex: /p/o or regex: "p". The new REGEX_OPTIONS rule equates those spellings, implemented in both FormEquivalence and core's FormRules, with golden vectors. It leaves a $ string alone, since that's a field path the server evaluates.

A generator bug fixed

When one test class called two factories with the same name (Filters.eq and ComparisonExpressions.eq), the generator imported both classes but left the calls unqualified, so the test wouldn't compile. No committed case had hit it yet; $expr with $eq would have. Static calls are now marked when emitted and resolved per class, and a shared name is called through its class.

Cases that are gaps, with their reasons

  • java.util.regex: Morphia compiles query patterns with it. A pattern it rejects ((*UCP)) is a gap, and so is one that compiles only under its flags (regex(f, p) compiles it alone first). So is one whose inline modifiers Java folds into the written flags: (?i)abc would be sent as /(?i)abc/i.
  • $type: a number (2), a one-item array, or an alias Type has no constant for.
  • $text: a flag written false, which TextSearchFilterCodec omits.
  • $mod: none of the manual's examples generate. Three demonstrate server errors (one, none or four elements). The others pass a double (4.5, 4.99, -0), and the double overload would write a Double remainder where the page has an Int32. Long operands are also gaps, because the codec writes small longs as Int32.
  • Combined forms: $options or a string $regex beside another operator, a repeated flag, a computed regex-expression pattern or options, and $expr given an array.

Decisions for review

  • $type: "number": dev.morphia.query.Type has no NUMBER constant, so $type: "number" can't be written from Morphia. Several manual examples use it. Should a NUMBER constant be added in core?
  • Four page errors are reviewed exclusions. Serialization and behavior pass; only the printed results are wrong:
    • $regex "Match Whole Words Only" leaves out _id: 103, which /\bline\b/ matches.
    • The two "Parse Email from String" examples print the $addFields output from before the pipeline's $set.
    • $regexFindAll "s Option" prints a match with one space where the data has five.
  • Special cases in the generator: the $text false-flag gap and the $mod Long gap mirror codec behavior. Rather than add catalog fields for one operator each, they're named directly in CaseGenerator.

Review

Codex ran three local rounds. Round 1 found eight spellings that would have produced wrong or failing cases, and round 2 found four more. Each is now a gap or covered by a rule, with a regression case in GeneratorTest. Round 3 was clean.

The design docs (operator-audits-design.md, the rule table in operator-audits-form-migration.md, 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