[GEODE-10508] Remedation of ANTLR nondeterminism warnings in OQL grammar #7942
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 resolves four nondeterminism warnings generated by ANTLR during the OQL (Object Query Language) grammar compilation process. These warnings indicated parser ambiguity that could lead to unpredictable parsing behavior.
Issue
Fixes GEODE-10508
Problem Description
During the
generateGrammarSourcetask, ANTLR produced the following warnings:Root Cause
Lines 574 & 578 (projection rule):
aggregateExprandexpralternatives when encountering aggregate function keywords (sum,avg,min,max,count)sum(field)sumas a field nameLines 961 & 979 (aggregateExpr rule):
distinctkeyword created ambiguity in aggregate function parsingdistinctkeyword or skip it and proceed directly to the expressionSolution
1. Added Syntactic Predicates (Lines 574 & 578)
Added lookahead predicates to the
projectionrule:Reasoning:
The predicate
(("sum"|"avg"|"min"|"max"|"count") TOK_LPAREN)=>instructs the parser to look ahead and check if an aggregate keyword is followed by a left parenthesis. If true, it choosesaggregateExpr; otherwise, it choosesexpr. This provides explicit lookahead logic to resolve the ambiguity.2. Added Greedy Option (Lines 961 & 979)
Added
greedyoption for optionaldistinctkeywords:Reasoning:
The
greedyoption tells the parser to greedily match thedistinctkeyword whenever it appears, rather than being ambiguous about whether to match or skip. This establishes clear matching priority and eliminates nondeterminism.3. Updated Test to Use Token Constants
Modified
AbstractCompiledValueTestJUnitTest.java:Reasoning:
Adding syntactic predicates changes ANTLR's token numbering in the generated lexer (
LITERAL_orshifted from 89 to 94). Using the constant ensures test correctness regardless of future grammar changes. This is a best practice for maintaining test stability.Changes Made
geode-core/src/main/antlr/org/apache/geode/cache/query/internal/parse/oql.gprojectionrule (2 locations)greedyoption toaggregateExprrule (2 locations)geode-core/src/test/java/org/apache/geode/cache/query/internal/AbstractCompiledValueTestJUnitTest.javaOQLLexerTokenTypesTesting
Verification Steps
AbstractCompiledValueTestJUnitTestpassesTest Commands
Impact Assessment
Benefits
Technical Details
=>) are a standard ANTLR 2 feature for lookaheadChecklist
For all changes, please confirm:
develop)?gradlew buildrun cleanly?