fix(parser): support qualified names and nested functions in SQL#99
Open
cpsievert wants to merge 1 commit intoposit-dev:mainfrom
Open
fix(parser): support qualified names and nested functions in SQL#99cpsievert wants to merge 1 commit intoposit-dev:mainfrom
cpsievert wants to merge 1 commit intoposit-dev:mainfrom
Conversation
The tree-sitter grammar's `positional_arg` rule was too restrictive, only accepting simple identifiers, numbers, strings, and wildcards. This caused parse failures for common SQL patterns: 1. Table alias prefixes: `SELECT p.name, SUM(s.quantity)` 2. Cross-table arithmetic: `SUM(quantity * price)` 3. Nested function calls: `ROUND(AVG(price), 2)` 4. Full table qualifiers: `products.product_name` The fix extends `positional_arg` to also accept: - `qualified_name` for table.column references - `function_call` for nested functions - Binary arithmetic/comparison expressions - Parenthesized expressions Adds 6 regression tests covering all reported limitations. Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
georgestagg
approved these changes
Feb 5, 2026
Collaborator
georgestagg
left a comment
There was a problem hiding this comment.
Thanks, nice spot!
Would you mind tweaking where the tests live before merging?
Comment on lines
+3137
to
+3142
|
|
||
| // ======================================== | ||
| // Parser Limitation Tests (from ggsql-parser-limitations.md) | ||
| // Tests to verify and fix reported parsing issues | ||
| // ======================================== | ||
|
|
Collaborator
There was a problem hiding this comment.
I don't think this comment adds anything.
I don't mind comments naming groups of specific types of tests, but I'd like to avoid littering the code with comments referencing specific planning files (ggsql-parser-limitations.md), or general statements like "reported parsing issues".
Comment on lines
+3143
to
+3236
| #[test] | ||
| fn test_table_alias_prefixes_in_select() { | ||
| // Issue 1: Table alias prefixes in SELECT clause | ||
| // Query like `SELECT p.product_name FROM products p` should parse | ||
| let query = r#" | ||
| SELECT p.product_name, SUM(s.quantity) as total | ||
| FROM sales s JOIN products p ON s.product_id = p.product_id | ||
| GROUP BY p.product_name | ||
| VISUALISE | ||
| DRAW bar MAPPING product_name AS x, total AS y | ||
| "#; | ||
|
|
||
| let result = parse_test_query(query); | ||
| assert!(result.is_ok(), "Parse failed: {:?}", result); | ||
| } | ||
|
|
||
| #[test] | ||
| fn test_cross_table_arithmetic() { | ||
| // Issue 2: Cross-table arithmetic expressions | ||
| // `quantity * price` across joined tables should work | ||
| let query = r#" | ||
| WITH t AS ( | ||
| SELECT region, SUM(quantity * price) as revenue | ||
| FROM sales JOIN products ON sales.product_id = products.product_id | ||
| GROUP BY region | ||
| ) | ||
| VISUALISE FROM t | ||
| DRAW bar MAPPING region AS x, revenue AS y | ||
| "#; | ||
|
|
||
| let result = parse_test_query(query); | ||
| assert!(result.is_ok(), "Parse failed: {:?}", result); | ||
| } | ||
|
|
||
| #[test] | ||
| fn test_nested_function_calls() { | ||
| // Issue 3: Nested function calls | ||
| // `ROUND(AVG(price), 2)` should parse | ||
| let query = r#" | ||
| SELECT category, ROUND(AVG(price), 2) as avg_price | ||
| FROM products | ||
| GROUP BY category | ||
| VISUALISE | ||
| DRAW bar MAPPING category AS x, avg_price AS y | ||
| "#; | ||
|
|
||
| let result = parse_test_query(query); | ||
| assert!(result.is_ok(), "Parse failed: {:?}", result); | ||
| } | ||
|
|
||
| #[test] | ||
| fn test_full_table_name_qualifiers() { | ||
| // Issue 4: Full table name qualifiers | ||
| // `products.product_name` (full table name, not alias) should work | ||
| let query = r#" | ||
| SELECT products.product_name, SUM(sales.quantity) as total | ||
| FROM sales | ||
| JOIN products ON sales.product_id = products.product_id | ||
| GROUP BY products.product_name | ||
| VISUALISE | ||
| DRAW bar MAPPING product_name AS x, total AS y | ||
| "#; | ||
|
|
||
| let result = parse_test_query(query); | ||
| assert!(result.is_ok(), "Parse failed: {:?}", result); | ||
| } | ||
|
|
||
| #[test] | ||
| fn test_simple_cross_table_multiplication() { | ||
| // Simplified version of cross-table arithmetic | ||
| let query = r#" | ||
| SELECT a.x * b.y as result | ||
| FROM a JOIN b ON a.id = b.id | ||
| VISUALISE | ||
| DRAW point MAPPING result AS x | ||
| "#; | ||
|
|
||
| let result = parse_test_query(query); | ||
| assert!(result.is_ok(), "Parse failed: {:?}", result); | ||
| } | ||
|
|
||
| #[test] | ||
| fn test_deeply_nested_functions() { | ||
| // Multiple levels of nesting | ||
| let query = r#" | ||
| SELECT COALESCE(NULLIF(TRIM(name), ''), 'Unknown') as clean_name | ||
| FROM data | ||
| VISUALISE | ||
| DRAW bar MAPPING clean_name AS x | ||
| "#; | ||
|
|
||
| let result = parse_test_query(query); | ||
| assert!(result.is_ok(), "Parse failed: {:?}", result); | ||
| } |
Collaborator
There was a problem hiding this comment.
I like these tests! But I think they should live in the parser.
Can we move them into the tree-sitter-ggsql/test/corpus/basic.txt file, converted into the format required there?
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
Fixes parsing limitations that caused common SQL patterns to fail. The tree-sitter grammar's
positional_argrule was too restrictive, preventing:SELECT p.name, SUM(s.quantity) FROM sales s JOIN products pSUM(quantity * price)across joined tablesROUND(AVG(price), 2),COALESCE(NULLIF(TRIM(x), ''), 'default')products.product_nameinstead of justproduct_nameRoot Cause
The
positional_argrule ingrammar.jsonly accepted:This meant function arguments like
SUM(s.quantity)failed becauses.quantityis aqualified_name, not a simpleidentifier. Similarly,ROUND(AVG(x), 2)failed becauseAVG(x)is afunction_call, not one of the accepted types.Investigation
Using
tree-sitter parse, I traced the exact failure points:Solution
Extended
positional_argto support complex expressions:Test plan
New tests added:
test_table_alias_prefixes_in_selectSELECT p.name, SUM(s.quantity)test_cross_table_arithmeticSUM(quantity * price)test_nested_function_callsROUND(AVG(price), 2)test_full_table_name_qualifiersproducts.product_nametest_simple_cross_table_multiplicationa.x * b.ytest_deeply_nested_functionsCOALESCE(NULLIF(TRIM(name), ''), 'Unknown')🤖 Generated with Claude Code