feat(cosmosdb): improve SQL parser compatibility for all 13 query feature areas#62
Merged
feat(cosmosdb): improve SQL parser compatibility for all 13 query feature areas#62
Conversation
…w clauses Add missing lexer tokens: !=, IN, BETWEEN, TOP, VALUE, ORDER, BY, GROUP, OFFSET, LIMIT, ASC, DESC, EXISTS, LIKE, HAVING, JOIN. Fix IDENTIFIER to allow leading underscore (for _ts, _etag, etc.). Merge scalar_expression and scalar_expression_in_where into a single unified scalar_expression rule. Add TOP, VALUE, ORDER BY, GROUP BY, OFFSET LIMIT, HAVING, JOIN, IN, BETWEEN, LIKE, EXISTS, NOT, and subquery support. Fix object_constant_field_pair to use COLON_SYMBOL. Resolves all 13 failing query feature areas from BYT-9043. Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
Covers: SELECT TOP, WHERE operators (!=, IN, BETWEEN, _ts fields), functions in SELECT, ORDER BY, aggregation, GROUP BY, string/math/ type-check functions, DISTINCT VALUE, VALUE keyword, OFFSET LIMIT, geospatial with JSON objects, and NOT EQUAL. Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
There was a problem hiding this comment.
Pull request overview
This PR expands the Cosmos DB SQL ANTLR grammar and adds example queries to validate parsing of additional query constructs.
Changes:
- Extended
SELECTgrammar to supportTOP,VALUE,GROUP BY/HAVING,ORDER BY,OFFSET ... LIMIT ..., andJOIN. - Unified/expanded
scalar_expressionto cover more operators and expression forms (e.g.,IN,BETWEEN,LIKE,EXISTS, object/array creation). - Added a suite of example
.sqlfiles that are exercised byparser_test.go.
Reviewed changes
Copilot reviewed 25 out of 26 changed files in this pull request and generated 3 comments.
Show a summary per file
| File | Description |
|---|---|
| cosmosdb/CosmosDBParser.g4 | Adds/updates core parser rules for new clauses and expressions. |
| cosmosdb/CosmosDBLexer.g4 | Adds new keyword/operator tokens and broadens identifier start chars (e.g., _). |
| cosmosdb/cosmosdb_lexer.go | Regenerated lexer output reflecting lexer grammar changes. |
| cosmosdb/cosmosdbparser_visitor.go | Regenerated visitor interface for new/removed parser rules. |
| cosmosdb/cosmosdbparser_listener.go | Regenerated listener interface for new/removed parser rules. |
| cosmosdb/cosmosdbparser_base_visitor.go | Regenerated base visitor implementations. |
| cosmosdb/cosmosdbparser_base_listener.go | Regenerated base listener implementations. |
| cosmosdb/examples/aggregation.sql | Example for aggregation COUNT. |
| cosmosdb/examples/between_expression.sql | Example for BETWEEN. |
| cosmosdb/examples/distinct_value.sql | Example for DISTINCT VALUE. |
| cosmosdb/examples/geospatial.sql | Example for geospatial function + object/array literals. |
| cosmosdb/examples/group_by.sql | Example for GROUP BY with aggregation. |
| cosmosdb/examples/in_expression.sql | Example for IN (...). |
| cosmosdb/examples/math_functions.sql | Example for numeric + conversion functions. |
| cosmosdb/examples/not_equal.sql | Example for !=. |
| cosmosdb/examples/offset_limit.sql | Example for ORDER BY ... OFFSET ... LIMIT .... |
| cosmosdb/examples/order_by.sql | Example for ORDER BY. |
| cosmosdb/examples/select_functions.sql | Example for scalar functions and arithmetic in projection. |
| cosmosdb/examples/select_top.sql | Example for TOP. |
| cosmosdb/examples/string_functions.sql | Example for string functions. |
| cosmosdb/examples/type_check_functions.sql | Example for type-check functions. |
| cosmosdb/examples/underscore_fields.sql | Example for system properties like _ts, _etag, _rid. |
| cosmosdb/examples/value_count.sql | Example for SELECT VALUE COUNT(1). |
| cosmosdb/examples/value_keyword.sql | Example for SELECT VALUE projection. |
| cosmosdb/examples/where_operators.sql | Example for WHERE with != and _ts filtering. |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
Split binary_operator into precedence-tiered rules (multiplicative, additive, shift, comparison) and reorder scalar_expression alternatives so AND/OR have lower precedence than comparison operators. Previously AND/OR bound tighter than =, >, < which caused incorrect parse trees. Also fix STRINGTONUMBER to StringToNumber for consistency. Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
rebelice
approved these changes
Mar 18, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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
scalar_expression/scalar_expression_in_whereinto a single unifiedscalar_expressionruleGrammar Changes
Lexer (
CosmosDBLexer.g4):IN,BETWEEN,TOP,VALUE,ORDER,BY,GROUP,OFFSET,LIMIT,ASC,DESC,EXISTS,LIKE,HAVING,JOIN!=operator (NOT_EQUAL_OPERATOR)IDENTIFIERrule to allow leading underscore (for system properties like_ts,_etag,_rid)Parser (
CosmosDBParser.g4):scalar_expressionandscalar_expression_in_whereinto singlescalar_expression(resolves the TODO in the grammar)SELECT TOP N,SELECT VALUE,SELECT DISTINCT VALUEsupportORDER BYclause withASC/DESCGROUP BYandHAVINGclausesOFFSET ... LIMIT ...paginationIN,BETWEEN,LIKE,NOTexpression supportEXISTSsubquery and scalar subquery supportJOINclause for intra-document joinsobject_constant_field_pairto useCOLON_SYMBOLinstead ofCOMMA_SYMBOLidentifierrule allowing keywords as property names/aliasesCOUNT(*)13 Feature Areas Now Supported
SELECT TOP N!=,IN,BETWEEN, underscore fieldsORDER BYCOUNT,SUM,AVG,MIN,MAX)GROUP BYUPPER,LOWER,LENGTH)ROUND)IS_STRING,IS_NUMBER,IS_DEFINED)SELECT DISTINCT VALUESELECT VALUEOFFSET ... LIMIT ...paginationTest plan
make buildgenerates parser successfullymake testpasses🤖 Generated with Claude Code