Skip to content

Commit 640ce77

Browse files
authored
Merge pull request #99 from cpsievert/fix/parser-function-args
fix(parser): support qualified names and nested functions in SQL
2 parents 6c76b8b + f4f1ce2 commit 640ce77

2 files changed

Lines changed: 172 additions & 6 deletions

File tree

tree-sitter-ggsql/grammar.js

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -276,12 +276,25 @@ module.exports = grammar({
276276
field('value', $.positional_arg)
277277
),
278278

279-
positional_arg: $ => choice(
280-
$.identifier,
279+
// Positional argument: supports complex expressions including:
280+
// - Simple values: identifier, number, string, *
281+
// - Qualified names: table.column
282+
// - Nested function calls: ROUND(AVG(x), 2)
283+
// - Arithmetic expressions: quantity * price
284+
// - Type casts: value::type
285+
positional_arg: $ => prec.left(choice(
286+
// Simple values
287+
$.qualified_name, // Handles both simple identifiers and table.column
281288
$.number,
282289
$.string,
283-
'*'
284-
),
290+
'*',
291+
// Nested function call
292+
$.function_call,
293+
// Arithmetic/comparison expression (binary operators)
294+
seq($.positional_arg, choice('+', '-', '*', '/', '%', '||', '::', '<', '>', '<=', '>=', '=', '!=', '<>'), $.positional_arg),
295+
// Parenthesized expression
296+
seq('(', $.positional_arg, ')')
297+
)),
285298

286299
// Namespaced identifier: matches "namespace:name" pattern
287300
// Examples: ggsql:penguins, ggsql:airquality

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

Lines changed: 155 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1123,8 +1123,9 @@ DRAW line MAPPING x AS x, total AS y
11231123
(function_args
11241124
(function_arg
11251125
(positional_arg
1126-
(identifier
1127-
(bare_identifier)))))
1126+
(qualified_name
1127+
(identifier
1128+
(bare_identifier))))))
11281129
(window_specification
11291130
(window_order_clause
11301131
(order_item
@@ -2101,3 +2102,155 @@ SELECT * FROM "data.csv"
21012102
(qualified_name
21022103
(identifier
21032104
(quoted_identifier))))))))))
2105+
2106+
================================================================================
2107+
Qualified name in function argument
2108+
================================================================================
2109+
2110+
SELECT SUM(s.quantity) FROM sales s VISUALISE DRAW bar MAPPING x AS x
2111+
2112+
--------------------------------------------------------------------------------
2113+
2114+
(query
2115+
(sql_portion
2116+
(sql_statement
2117+
(select_statement
2118+
(select_body
2119+
(function_call
2120+
(identifier
2121+
(bare_identifier))
2122+
(function_args
2123+
(function_arg
2124+
(positional_arg
2125+
(qualified_name
2126+
(identifier
2127+
(bare_identifier))
2128+
(identifier
2129+
(bare_identifier)))))))
2130+
(from_clause
2131+
(table_ref
2132+
table: (qualified_name
2133+
(identifier
2134+
(bare_identifier)))
2135+
alias: (identifier
2136+
(bare_identifier))))))))
2137+
(visualise_statement
2138+
(visualise_keyword)
2139+
(viz_clause
2140+
(draw_clause
2141+
(geom_type)
2142+
(mapping_clause
2143+
(mapping_list
2144+
(mapping_element
2145+
(explicit_mapping
2146+
value: (mapping_value
2147+
(column_reference
2148+
(identifier
2149+
(bare_identifier))))
2150+
aesthetic: (aesthetic_name)))))))))
2151+
2152+
================================================================================
2153+
Nested function calls
2154+
================================================================================
2155+
2156+
SELECT ROUND(AVG(price), 2) as avg FROM data VISUALISE DRAW bar MAPPING x AS x
2157+
2158+
--------------------------------------------------------------------------------
2159+
2160+
(query
2161+
(sql_portion
2162+
(sql_statement
2163+
(select_statement
2164+
(select_body
2165+
(function_call
2166+
(identifier
2167+
(bare_identifier))
2168+
(function_args
2169+
(function_arg
2170+
(positional_arg
2171+
(function_call
2172+
(identifier
2173+
(bare_identifier))
2174+
(function_args
2175+
(function_arg
2176+
(positional_arg
2177+
(qualified_name
2178+
(identifier
2179+
(bare_identifier)))))))))
2180+
(function_arg
2181+
(positional_arg
2182+
(number)))))
2183+
(identifier
2184+
(bare_identifier))
2185+
(identifier
2186+
(bare_identifier))
2187+
(from_clause
2188+
(table_ref
2189+
table: (qualified_name
2190+
(identifier
2191+
(bare_identifier)))))))))
2192+
(visualise_statement
2193+
(visualise_keyword)
2194+
(viz_clause
2195+
(draw_clause
2196+
(geom_type)
2197+
(mapping_clause
2198+
(mapping_list
2199+
(mapping_element
2200+
(explicit_mapping
2201+
value: (mapping_value
2202+
(column_reference
2203+
(identifier
2204+
(bare_identifier))))
2205+
aesthetic: (aesthetic_name)))))))))
2206+
2207+
================================================================================
2208+
Arithmetic in function argument
2209+
================================================================================
2210+
2211+
SELECT SUM(quantity * price) as total FROM data VISUALISE DRAW bar MAPPING x AS x
2212+
2213+
--------------------------------------------------------------------------------
2214+
2215+
(query
2216+
(sql_portion
2217+
(sql_statement
2218+
(select_statement
2219+
(select_body
2220+
(function_call
2221+
(identifier
2222+
(bare_identifier))
2223+
(function_args
2224+
(function_arg
2225+
(positional_arg
2226+
(positional_arg
2227+
(qualified_name
2228+
(identifier
2229+
(bare_identifier))))
2230+
(positional_arg
2231+
(qualified_name
2232+
(identifier
2233+
(bare_identifier))))))))
2234+
(identifier
2235+
(bare_identifier))
2236+
(identifier
2237+
(bare_identifier))
2238+
(from_clause
2239+
(table_ref
2240+
table: (qualified_name
2241+
(identifier
2242+
(bare_identifier)))))))))
2243+
(visualise_statement
2244+
(visualise_keyword)
2245+
(viz_clause
2246+
(draw_clause
2247+
(geom_type)
2248+
(mapping_clause
2249+
(mapping_list
2250+
(mapping_element
2251+
(explicit_mapping
2252+
value: (mapping_value
2253+
(column_reference
2254+
(identifier
2255+
(bare_identifier))))
2256+
aesthetic: (aesthetic_name)))))))))

0 commit comments

Comments
 (0)