Skip to content

Commit ec946f7

Browse files
committed
Fixup grammar: subquery as function argument
1 parent adf591c commit ec946f7

2 files changed

Lines changed: 82 additions & 0 deletions

File tree

tree-sitter-ggsql/grammar.js

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -173,6 +173,17 @@ module.exports = grammar({
173173
')'
174174
)),
175175

176+
// Scalar subquery for use inside expressions (e.g. function arguments)
177+
// Matches (SELECT ...) or (WITH ... SELECT ...),
178+
scalar_subquery: $ => prec(2, seq(
179+
'(',
180+
choice(
181+
$.with_statement,
182+
$.select_statement,
183+
),
184+
')'
185+
)),
186+
176187
// Token-by-token fallback for any other subquery content
177188
subquery_body: $ => repeat1(choice(
178189
$.window_function,
@@ -311,6 +322,8 @@ module.exports = grammar({
311322
$.cast_expression,
312323
// Nested function call
313324
$.function_call,
325+
// Scalar subquery: (SELECT ...) or (WITH ... SELECT ...)
326+
$.scalar_subquery,
314327
// Arithmetic/comparison expression (binary operators)
315328
seq($.positional_arg, choice('+', '-', '*', '/', '%', '||', '::', '<', '>', '<=', '>=', '=', '!=', '<>'), $.positional_arg),
316329
// Parenthesized expression

tree-sitter-ggsql/test/corpus/basic.txt

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2485,6 +2485,75 @@ SELECT SUM(TRY_CAST(price AS INTEGER)) as total FROM data VISUALISE DRAW bar MAP
24852485
(bare_identifier))))
24862486
name: (aesthetic_name)))))))))
24872487

2488+
================================================================================
2489+
Scalar subquery as function argument
2490+
================================================================================
2491+
2492+
SELECT LEAST(SQRT(x), (SELECT MAX(y) FROM t)) FROM data
2493+
VISUALISE
2494+
DRAW point MAPPING x AS x
2495+
2496+
--------------------------------------------------------------------------------
2497+
2498+
(query
2499+
(sql_portion
2500+
(sql_statement
2501+
(select_statement
2502+
(select_body
2503+
(function_call
2504+
(identifier
2505+
(bare_identifier))
2506+
(function_args
2507+
(function_arg
2508+
(positional_arg
2509+
(function_call
2510+
(identifier
2511+
(bare_identifier))
2512+
(function_args
2513+
(function_arg
2514+
(positional_arg
2515+
(qualified_name
2516+
(identifier
2517+
(bare_identifier)))))))))
2518+
(function_arg
2519+
(positional_arg
2520+
(scalar_subquery
2521+
(select_statement
2522+
(select_body
2523+
(function_call
2524+
(identifier
2525+
(bare_identifier))
2526+
(function_args
2527+
(function_arg
2528+
(positional_arg
2529+
(qualified_name
2530+
(identifier
2531+
(bare_identifier)))))))
2532+
(from_clause
2533+
(table_ref
2534+
table: (qualified_name
2535+
(identifier
2536+
(bare_identifier))))))))))))
2537+
(from_clause
2538+
(table_ref
2539+
table: (qualified_name
2540+
(identifier
2541+
(bare_identifier)))))))))
2542+
(visualise_statement
2543+
(visualise_keyword)
2544+
(viz_clause
2545+
(draw_clause
2546+
(geom_type)
2547+
(mapping_clause
2548+
(mapping_list
2549+
(mapping_element
2550+
(explicit_mapping
2551+
value: (mapping_value
2552+
(column_reference
2553+
(identifier
2554+
(bare_identifier))))
2555+
name: (aesthetic_name)))))))))
2556+
24882557
================================================================================
24892558
FACET single variable (wrap layout)
24902559
================================================================================

0 commit comments

Comments
 (0)