feat: Strengthen comprehension pattern matching validation (fixes #44) #89
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Summary
This PR significantly strengthens the CEL comprehension pattern matching implementation to address the brittleness identified in issue #44. While theoretically possible for custom hand-written accumulator expressions to be misidentified, the improvements make this significantly less likely and provide better observability when edge cases occur.
Problem Statement
From issue #44: The
analyzeComprehensionPattern()function uses fragile AST pattern matching to identify CEL comprehension macros. Since CEL expands comprehensions (all,exists,exists_one,filter,map) into accumulator-based expressions at parse time with no metadata, we must infer the original macro from structural patterns. This could theoretically misidentify custom accumulator expressions.Changes Made
1. Result Expression Validation ✅
Priority: High Impact
All comprehension types now validate their result expressions:
all(),exists(),map(),filter(): Result must beaccu(identity)exists_one(): Result must beaccu == 1isIdentityResult()helper functionisEqualsOneResult()to validate exact structureExample:
2. Strengthened Conditional Validation ✅
Priority: High Impact
Made conditional pattern validation much more specific:
isConditionalCountStep()(forexists_one):operators.Conditional && len(args) == 3accu + 1(notaccu + 2or other)accuisConditionalAppendStep()(formapwith filter):[iterVar](that's a filter, not map)[expression]isConditionalFilterStep()(forfilter):[iterVar]accu3. Comprehensive Documentation ✅
Priority: High Value
Added detailed comments explaining CEL's macro expansion:
Makes implicit knowledge explicit for future maintainers.
4. Enhanced Edge Case Tests ✅
Priority: Medium Impact
New file:
comprehensions_edge_cases_test.gowith comprehensive coverage:TestComprehensionPatternDetectionOrder (5 tests)
map(x, x)vsfilter(x, true)disambiguationTestComprehensionWithComplexNestedExpressions (3 tests)
TestComprehensionEdgeCasesWithEmptyLists (4 tests)
TestComprehensionWithChainedOperations (4 tests)
filter().map()!exists(),!all()TestComprehensionWithVariableNameEdgeCases (3 tests)
TestComprehensionWithMapFilter (2 tests)
map()fromfilter()Verification
All Tests Pass ✅
$ make test PASS coverage: 90.8% of statements ok github.com/spandigital/cel2sql/v3 22.916s PASS coverage: 92.1% of statements ok github.com/spandigital/cel2sql/v3/pg 13.509sLinting Passes ✅
Code Formatted ✅
$ make fmt go fmt ./... goimports -w .Impact Assessment
Likelihood of Misidentification:
Very Low→ Extremely Low (multi-layer validation)Low-Medium→ Low (result validation catches anomalies)Benefits:
Potential Issues:
Related Issues
Fixes #44 - Comprehension Pattern Matching is Brittle
Checklist
make fmtpasses)make lintwith 0 issues)make test)🤖 Generated with Claude Code
Co-Authored-By: Claude noreply@anthropic.com