Repository navigation
feat!: make the CEL grammars configurable and split them into packages - #26
Conversation
There was a problem hiding this comment.
🟡 Changes recommended
Unresolved critical issues can bypass decorator-enforced field restrictions, alter user-written filter paths, and confuse row types with JSON struct types.
9 open issues
Apply decorators during name and member resolution · New Strip only synthetic roots from presence filters · New Apply decorators to restrictions built by filter operations · New Cache translated arguments per CheckedCall · New Reject unknown identifiers with a positioned client error · New Validate has() operands before extracting path segments · New Use Locale.ROOT when lowercasing type names · New Report reserved names separately from unknown fields · New Use Locale.ROOT when lowercasing operation names · New
What changed in this PR
Replaces aquery’s fixed CEL compiler and filter grammar with configurable APIs, while separating expression, filter, and internal implementation packages.
Changes:
- Adds extensible operation chains, macros, decorators, and function overloads.
- Introduces
CelEnvironment, named rows, conditional expressions, and nullable-field comparisons. - Reorganizes CEL packages and replaces storage flags with typed scope fields.
Paths below use cel/ for aquery-grammar-cel/src/main/java/dev/horizon/aquery/cel/ and tests/ for aquery/src/test/java/dev/horizon/aquery/cel/.
| File | Description |
|---|---|
| aquery/src/test/java/dev/horizon/aquery/cel/filter/CelFilterTest.java | Updated as part of this pull request. |
| aquery/src/test/java/dev/horizon/aquery/cel/filter/CelFilterGrammarTest.java | Updated as part of this pull request. |
| aquery/src/test/java/dev/horizon/aquery/cel/CelExpressionCompilerTest.java | Updated as part of this pull request. |
| aquery-grammar-cel/src/main/java/module-info.java | Updated as part of this pull request. |
| aquery-grammar-cel/src/main/java/dev/horizon/aquery/cel/Type.java | Updated as part of this pull request. |
| aquery-grammar-cel/src/main/java/dev/horizon/aquery/cel/Syntax.java | Updated as part of this pull request. |
| aquery-grammar-cel/src/main/java/dev/horizon/aquery/cel/Scope.java | Updated as part of this pull request. |
| aquery-grammar-cel/src/main/java/dev/horizon/aquery/cel/RefusedOperation.java | Updated as part of this pull request. |
| aquery-grammar-cel/src/main/java/dev/horizon/aquery/cel/Refusal.java | Updated as part of this pull request. |
| aquery-grammar-cel/src/main/java/dev/horizon/aquery/cel/MacroCall.java | Updated as part of this pull request. |
| aquery-grammar-cel/src/main/java/dev/horizon/aquery/cel/Macro.java | Updated as part of this pull request. |
| aquery-grammar-cel/src/main/java/dev/horizon/aquery/cel/internal/MacroDeclaration.java | Updated as part of this pull request. |
| aquery-grammar-cel/src/main/java/dev/horizon/aquery/cel/internal/FactoryMacroCall.java | Updated as part of this pull request. |
| aquery-grammar-cel/src/main/java/dev/horizon/aquery/cel/internal/DurationText.java | Updated as part of this pull request. |
| aquery-grammar-cel/src/main/java/dev/horizon/aquery/cel/internal/CelTypes.java | Updated as part of this pull request. |
| aquery-grammar-cel/src/main/java/dev/horizon/aquery/cel/internal/CelTree.java | Updated as part of this pull request. |
| aquery-grammar-cel/src/main/java/dev/horizon/aquery/cel/internal/CelSyntax.java | Updated as part of this pull request. |
| aquery-grammar-cel/src/main/java/dev/horizon/aquery/cel/internal/CelSourceText.java | Updated as part of this pull request. |
| aquery-grammar-cel/src/main/java/dev/horizon/aquery/cel/Has.java | Updated as part of this pull request. |
| aquery-grammar-cel/src/main/java/dev/horizon/aquery/cel/Function.java | Updated as part of this pull request. |
| aquery-grammar-cel/src/main/java/dev/horizon/aquery/cel/FilterTranslation.java | Updated as part of this pull request. |
| aquery-grammar-cel/src/main/java/dev/horizon/aquery/cel/FilterStringMatch.java | Updated as part of this pull request. |
| aquery-grammar-cel/src/main/java/dev/horizon/aquery/cel/FilterOperation.java | Updated as part of this pull request. |
| aquery-grammar-cel/src/main/java/dev/horizon/aquery/cel/FilterNegation.java | Updated as part of this pull request. |
| aquery-grammar-cel/src/main/java/dev/horizon/aquery/cel/FilterMembership.java | Updated as part of this pull request. |
| aquery-grammar-cel/src/main/java/dev/horizon/aquery/cel/FilterJunction.java | Updated as part of this pull request. |
| aquery-grammar-cel/src/main/java/dev/horizon/aquery/cel/FilterComparison.java | Updated as part of this pull request. |
| aquery-grammar-cel/src/main/java/dev/horizon/aquery/cel/filter/ValueNode.java | Updated as part of this pull request. |
| aquery-grammar-cel/src/main/java/dev/horizon/aquery/cel/filter/Translation.java | Updated as part of this pull request. |
| aquery-grammar-cel/src/main/java/dev/horizon/aquery/cel/filter/Site.java | Updated as part of this pull request. |
| aquery-grammar-cel/src/main/java/dev/horizon/aquery/cel/filter/ParsedOperand.java | Updated as part of this pull request. |
| aquery-grammar-cel/src/main/java/dev/horizon/aquery/cel/filter/ParsedCall.java | Updated as part of this pull request. |
| aquery-grammar-cel/src/main/java/dev/horizon/aquery/cel/filter/operation/StringMatch.java | Updated as part of this pull request. |
| aquery-grammar-cel/src/main/java/dev/horizon/aquery/cel/filter/operation/Refused.java | Updated as part of this pull request. |
| aquery-grammar-cel/src/main/java/dev/horizon/aquery/cel/filter/operation/Negation.java | Updated as part of this pull request. |
| aquery-grammar-cel/src/main/java/dev/horizon/aquery/cel/filter/operation/Membership.java | Updated as part of this pull request. |
| aquery-grammar-cel/src/main/java/dev/horizon/aquery/cel/filter/operation/Disjunction.java | Updated as part of this pull request. |
| aquery-grammar-cel/src/main/java/dev/horizon/aquery/cel/filter/operation/Conjunction.java | Updated as part of this pull request. |
| aquery-grammar-cel/src/main/java/dev/horizon/aquery/cel/filter/operation/Comparison.java | Updated as part of this pull request. |
| aquery-grammar-cel/src/main/java/dev/horizon/aquery/cel/filter/Operation.java | Updated as part of this pull request. |
| aquery-grammar-cel/src/main/java/dev/horizon/aquery/cel/filter/Operand.java | Updated as part of this pull request. |
| aquery-grammar-cel/src/main/java/dev/horizon/aquery/cel/filter/Next.java | Updated as part of this pull request. |
| aquery-grammar-cel/src/main/java/dev/horizon/aquery/cel/filter/FieldPathNode.java | Updated as part of this pull request. |
| aquery-grammar-cel/src/main/java/dev/horizon/aquery/cel/filter/Decorator.java | Updated as part of this pull request. |
| aquery-grammar-cel/src/main/java/dev/horizon/aquery/cel/filter/Chain.java | Updated as part of this pull request. |
| aquery-grammar-cel/src/main/java/dev/horizon/aquery/cel/filter/CelSyntaxParser.java | Updated as part of this pull request. |
| aquery-grammar-cel/src/main/java/dev/horizon/aquery/cel/filter/CelFilterTranslator.java | Updated as part of this pull request. |
| aquery-grammar-cel/src/main/java/dev/horizon/aquery/cel/filter/CelFilterParser.java | Updated as part of this pull request. |
| aquery-grammar-cel/src/main/java/dev/horizon/aquery/cel/filter/CelFilterGrammar.java | Updated as part of this pull request. |
| aquery-grammar-cel/src/main/java/dev/horizon/aquery/cel/filter/Call.java | Updated as part of this pull request. |
| aquery-grammar-cel/src/main/java/dev/horizon/aquery/cel/ExpressionTranslation.java | Updated as part of this pull request. |
| aquery-grammar-cel/src/main/java/dev/horizon/aquery/cel/ExpressionStringMatch.java | Updated as part of this pull request. |
| aquery-grammar-cel/src/main/java/dev/horizon/aquery/cel/ExpressionOperation.java | Updated as part of this pull request. |
| aquery-grammar-cel/src/main/java/dev/horizon/aquery/cel/ExpressionNegative.java | Updated as part of this pull request. |
| aquery-grammar-cel/src/main/java/dev/horizon/aquery/cel/ExpressionNegation.java | Updated as part of this pull request. |
| aquery-grammar-cel/src/main/java/dev/horizon/aquery/cel/ExpressionMembership.java | Updated as part of this pull request. |
| aquery-grammar-cel/src/main/java/dev/horizon/aquery/cel/ExpressionLogic.java | Updated as part of this pull request. |
| aquery-grammar-cel/src/main/java/dev/horizon/aquery/cel/ExpressionIndexing.java | Updated as part of this pull request. |
| aquery-grammar-cel/src/main/java/dev/horizon/aquery/cel/ExpressionConversion.java | Updated as part of this pull request. |
| aquery-grammar-cel/src/main/java/dev/horizon/aquery/cel/ExpressionComparison.java | Updated as part of this pull request. |
| aquery-grammar-cel/src/main/java/dev/horizon/aquery/cel/ExpressionArithmetic.java | Updated as part of this pull request. |
| aquery-grammar-cel/src/main/java/dev/horizon/aquery/cel/expression/Translation.java | Updated as part of this pull request. |
| aquery-grammar-cel/src/main/java/dev/horizon/aquery/cel/expression/Site.java | Updated as part of this pull request. |
| aquery-grammar-cel/src/main/java/dev/horizon/aquery/cel/expression/Quantification.java | Updated as part of this pull request. |
| aquery-grammar-cel/src/main/java/dev/horizon/aquery/cel/expression/ParameterValues.java | Updated as part of this pull request. |
| aquery-grammar-cel/src/main/java/dev/horizon/aquery/cel/expression/Parameters.java | Updated as part of this pull request. |
| aquery-grammar-cel/src/main/java/dev/horizon/aquery/cel/expression/Overload.java | Updated as part of this pull request. |
| aquery-grammar-cel/src/main/java/dev/horizon/aquery/cel/expression/operation/Timestamp.java | Updated as part of this pull request. |
| aquery-grammar-cel/src/main/java/dev/horizon/aquery/cel/expression/operation/StringMatch.java | Updated as part of this pull request. |
| aquery-grammar-cel/src/main/java/dev/horizon/aquery/cel/expression/operation/Refused.java | Updated as part of this pull request. |
| aquery-grammar-cel/src/main/java/dev/horizon/aquery/cel/expression/operation/NullOperand.java | Updated as part of this pull request. |
| aquery-grammar-cel/src/main/java/dev/horizon/aquery/cel/expression/operation/NullInequality.java | Updated as part of this pull request. |
| aquery-grammar-cel/src/main/java/dev/horizon/aquery/cel/expression/operation/NullEquality.java | Updated as part of this pull request. |
| aquery-grammar-cel/src/main/java/dev/horizon/aquery/cel/expression/operation/Negative.java | Updated as part of this pull request. |
| aquery-grammar-cel/src/main/java/dev/horizon/aquery/cel/expression/operation/Negation.java | Updated as part of this pull request. |
| aquery-grammar-cel/src/main/java/dev/horizon/aquery/cel/expression/operation/Membership.java | Updated as part of this pull request. |
| aquery-grammar-cel/src/main/java/dev/horizon/aquery/cel/expression/operation/Logic.java | Updated as part of this pull request. |
| aquery-grammar-cel/src/main/java/dev/horizon/aquery/cel/expression/operation/Indexing.java | Updated as part of this pull request. |
| aquery-grammar-cel/src/main/java/dev/horizon/aquery/cel/expression/operation/Duration.java | Updated as part of this pull request. |
| aquery-grammar-cel/src/main/java/dev/horizon/aquery/cel/expression/operation/Division.java | Updated as part of this pull request. |
| aquery-grammar-cel/src/main/java/dev/horizon/aquery/cel/expression/operation/Conditional.java | Updated as part of this pull request. |
| aquery-grammar-cel/src/main/java/dev/horizon/aquery/cel/expression/operation/Comparison.java | Updated as part of this pull request. |
| aquery-grammar-cel/src/main/java/dev/horizon/aquery/cel/expression/operation/Arithmetic.java | Updated as part of this pull request. |
| aquery-grammar-cel/src/main/java/dev/horizon/aquery/cel/expression/Operation.java | Updated as part of this pull request. |
| aquery-grammar-cel/src/main/java/dev/horizon/aquery/cel/expression/Next.java | Updated as part of this pull request. |
| aquery-grammar-cel/src/main/java/dev/horizon/aquery/cel/expression/Namespace.java | Updated as part of this pull request. |
| aquery-grammar-cel/src/main/java/dev/horizon/aquery/cel/expression/Name.java | Updated as part of this pull request. |
| aquery-grammar-cel/src/main/java/dev/horizon/aquery/cel/expression/InvalidExpressionException.java | Updated as part of this pull request. |
| aquery-grammar-cel/src/main/java/dev/horizon/aquery/cel/expression/FunctionOperation.java | Updated as part of this pull request. |
| aquery-grammar-cel/src/main/java/dev/horizon/aquery/cel/expression/Function.java | Updated as part of this pull request. |
| aquery-grammar-cel/src/main/java/dev/horizon/aquery/cel/expression/Decorator.java | Updated as part of this pull request. |
| aquery-grammar-cel/src/main/java/dev/horizon/aquery/cel/expression/Constant.java | Updated as part of this pull request. |
| aquery-grammar-cel/src/main/java/dev/horizon/aquery/cel/expression/CompiledExpression.java | Updated as part of this pull request. |
| aquery-grammar-cel/src/main/java/dev/horizon/aquery/cel/expression/Checker.java | Updated as part of this pull request. |
| aquery-grammar-cel/src/main/java/dev/horizon/aquery/cel/expression/CheckedCall.java | Updated as part of this pull request. |
| aquery-grammar-cel/src/main/java/dev/horizon/aquery/cel/expression/Chain.java | Updated as part of this pull request. |
| aquery-grammar-cel/src/main/java/dev/horizon/aquery/cel/expression/CelGrammar.java | Updated as part of this pull request. |
| aquery-grammar-cel/src/main/java/dev/horizon/aquery/cel/expression/CelEnvironment.java | Updated as part of this pull request. |
| aquery-grammar-cel/src/main/java/dev/horizon/aquery/cel/expression/Call.java | Updated as part of this pull request. |
| aquery-grammar-cel/src/main/java/dev/horizon/aquery/cel/Expression.java | Updated as part of this pull request. |
| aquery-grammar-cel/src/main/java/dev/horizon/aquery/cel/CelTree.java | Updated as part of this pull request. |
| aquery-grammar-cel/src/main/java/dev/horizon/aquery/cel/CelFilterTranslator.java | Updated as part of this pull request. |
| aquery-grammar-cel/src/main/java/dev/horizon/aquery/cel/CelExpressionCompiler.java | Updated as part of this pull request. |
Review effort: Max
| case IDENT -> names.find(node.ident().name(), site); | ||
| case SELECT -> member(node, site); | ||
| default -> new Name.ValueName(expression(node)); |
There was a problem hiding this comment.
Fixed in 9048151. A node is now decorated once, when it becomes the child of another node: Translation.operand decorates the operand of a member access, a presence test and a quantifier, and Name decorates the fields it builds inside has() and behind an edge. labels.tier, has(note) and labels['tier'] now pass the same FieldReference through the decorator. CelGrammarTest.decoratesEveryField covers each form.
| if (!node.ident().name().equals(CelTree.RECORD)) { | ||
| segments.add(node.ident().name()); | ||
| } |
There was a problem hiding this comment.
Fixed in 9048151. FieldPathNode keeps every identifier, and only presence drops aquery__record, when it is the direct operand of the select that has() generates. aquery__record.count == 1 now keeps its root, as a row of CelFilterTest.refusesAtCompile shows.
| FieldPath field = element.path(); | ||
| List<Condition> equalities = values.stream() | ||
| .<Condition> map(value -> new Restriction(field, Operator.EQUALS, value.value())).toList(); | ||
| return equalities.size() == 1 ? equalities.getFirst() : new Or(equalities); |
There was a problem hiding this comment.
Fixed in 9048151. Call has decorate(Condition), and Membership passes each equality of field in [a, b] through it before it builds the Or. The grammar still decorates the condition that an operation returns, so nothing is decorated twice. count in [1, 2] is a row of CelFilterGrammarTest.rewritesWithADecorator.
| @Override | ||
| public Expression argument(int index) { | ||
| return translation.expression(node(index)); | ||
| } | ||
|
|
||
| @Override | ||
| public List<Expression> arguments() { | ||
| return site.node().call().args().stream().map(translation::expression).toList(); | ||
| } |
There was a problem hiding this comment.
Fixed in 9048151. CheckedCall keeps each translated argument, translates it on its first read, and arguments() reads through the same cache.
| public Name member(String field, Site site) { | ||
| return scope.field(field).<Name> map(found -> new ValueName(new FieldReference(found, row, site.position()))) | ||
| .orElseGet(() -> new EdgeName(scope.edge(field).orElseThrow(), row, List.of(), prefix() + field)); | ||
| } |
There was a problem hiding this comment.
Fixed in 9048151. Every lookup in Name that could throw NoSuchElementException now refuses through site.refuse: a root field or edge ('int' is not a field), an edge member, a parameter and a struct member. int == int is a row of the refusal table in CelGrammarTest.
| if (node.getKind() == CelExpr.ExprKind.Kind.SELECT && node.select().testOnly()) { | ||
| return new Restriction(new FieldPath(path.segments()), Operator.HAS, new Text("*")); | ||
| } |
There was a problem hiding this comment.
Fixed in 9048151. The operand of has() must be a field path, so has(foo().bar) is refused with "has() takes a field, as in has(note) or has(labels.tier)". It is a row of CelFilterTest.refusesAtParse.
|
|
||
| @Override | ||
| public String describe() { | ||
| return name().toLowerCase(); |
| if (scope.field(name).isPresent() || scope.edge(name).isPresent() || name.equals(Checker.PARAMS) | ||
| || name.equals(CelTree.RECORD)) { | ||
| throw new IllegalArgumentException("the row " + name + " has the name of a field or an edge of " + scope.name()); | ||
| } |
There was a problem hiding this comment.
Fixed in 9048151. A reserved name is refused with "the row params has a name that aquery reserves", and a name of a field or an edge keeps its own message.
| } | ||
|
|
||
| String function() { | ||
| return kind().name().toLowerCase(); |
CelGrammar compiles expressions and CelFilterGrammar parses filters. Each grammar is a chain of operations, macros and a decorator that a caller can extend: with() adds a function, an operation, a macro or a decorator, and without() takes away the operations of a class. An operation translates a call or passes it to the next operation of its function. CelEnvironment replaces the scope, rows and parameters arguments of the compiler. Expressions gain conditionals, null comparisons, named rows and has() on a field of the row, in filters too. The module now has the packages cel (types, scopes and macros), cel.expression, cel.expression.operation, cel.filter and cel.filter.operation, and an internal package that it does not export. BREAKING CHANGE: CelExpressionCompiler is removed in favour of CelGrammar.standard().compile(text, environment, type), and the types of the CEL module moved to sub-packages, e.g. CelFilterParser is now dev.horizon.aquery.cel.filter.CelFilterParser. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…t read. - decorate each node when it becomes the child of another, so a decorator sees the field of labels.tier, has(note) and an edge as it sees labels['tier']. - pass the equalities of field in [a, b] through the decorators with call.decorate. - strip only the synthetic row of has() from a filter path, and refuse has() of anything other than a field. - refuse an identifier, an edge member, a parameter or a struct member that the scope does not have with a positioned error. - translate each argument of a call once. - lowercase type and quantifier names with Locale.ROOT, and give reserved row names their own error. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
9048151 to
a966716
Compare



Summary
CelGrammarcompiles expressions andCelFilterGrammarparses filters. Each one is built from operations, macros and a decorator.with()adds a function, an operation, a macro or a decorator.without(Class)removes the operations of a class.CelEnvironmentreplaces the separate scope, rows and parameters arguments of the old compiler.CelEnvironment.of(scope).with("new", scope).with(parameters)builds one.? :;== nulland!= nullon fields that can be unset;new.total > old.total;has(field)on a field of the row, which filters now accept as well.cel:Type,Scope,Macro,MacroCall,Syntax,Has.cel.expressionandcel.expression.operation: the expression API and its built-in operations.cel.filterandcel.filter.operation: the filter API and its built-in operations.cel.internal: shared cel-java code, not exported.null.null(for exampleexpire_time < null) is refused.Breaking changes
CelExpressionCompileris removed. UseCelGrammar.standard().compile(text, environment, type)instead.CelFilterParseris nowdev.horizon.aquery.cel.filter.CelFilterParser.new CelFilterParser()still uses the standard grammar.Scope.column(String, Type)is replaced bycolumn(String, Type.Scalar, String),nullable(...),uuid(...)andjson(...).Test plan
mvn install: theaquerymodule's tests (508) pass, including the newCelGrammarTestandCelFilterGrammarTest.aquery-it(filters on PostgreSQL, MySQL, SQL Server, SQLite and H2): 585 tests, 52 skipped.mvn verifyagainst this branch (its twoCelFilterParserimports need updating).🤖 Generated with Claude Code