From b92c3e9115d5d68d1cc53077b49c01f7d0109e0f Mon Sep 17 00:00:00 2001 From: Matthias Queitsch Date: Fri, 15 Nov 2024 23:14:46 +0100 Subject: [PATCH] feat: add `cross join unnest with ordinality` --- grammar.js | 24 ++++++++++++--- queries/highlights.scm | 1 + test/corpus/select.txt | 66 ++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 87 insertions(+), 4 deletions(-) diff --git a/grammar.js b/grammar.js index cb3d2ff..2978b44 100644 --- a/grammar.js +++ b/grammar.js @@ -294,6 +294,7 @@ module.exports = grammar({ keyword_out: _ => make_keyword("out"), keyword_inout: _ => make_keyword("inout"), keyword_variadic: _ => make_keyword("variadic"), + keyword_ordinality: _ => make_keyword("ordinality"), keyword_session: _ => make_keyword("session"), keyword_isolation: _ => make_keyword("isolation"), @@ -3114,10 +3115,25 @@ module.exports = grammar({ ) ), - cross_join: $ => seq( - $.keyword_cross, - $.keyword_join, - $.relation, + cross_join: $ => prec.right( + seq( + $.keyword_cross, + $.keyword_join, + $.relation, + optional( + seq( + $.keyword_with, + $.keyword_ordinality, + optional( + seq( + $.keyword_as, + field("alias", $.identifier), + paren_list($.identifier), + ) + ) + ) + ) + ) ), lateral_join: $ => seq( diff --git a/queries/highlights.scm b/queries/highlights.scm index 385caff..8eb675b 100644 --- a/queries/highlights.scm +++ b/queries/highlights.scm @@ -267,6 +267,7 @@ (keyword_out) (keyword_inout) (keyword_variadic) + (keyword_ordinality) (keyword_session) (keyword_isolation) (keyword_level) diff --git a/test/corpus/select.txt b/test/corpus/select.txt index a003d4b..4c55ae2 100644 --- a/test/corpus/select.txt +++ b/test/corpus/select.txt @@ -2970,3 +2970,69 @@ FROM ( (object_reference (identifier)))))) (identifier))))) + +================================================================================ +SELECT FROM UNNESTED ARRAY WITH ORDINALITY +================================================================================ + +SELECT numbers, n, a +FROM ( + VALUES + (ARRAY[2, 5]), + (ARRAY[7, 8, 9]) +) AS x (numbers) +CROSS JOIN UNNEST(numbers) WITH ORDINALITY AS t (n, a); + +-------------------------------------------------------------------------------- + +(program + (statement + (select + (keyword_select) + (select_expression + (term + (field + (identifier))) + (term + (field + (identifier))) + (term + (field + (identifier))))) + (from + (keyword_from) + (relation + (values + (keyword_values) + (list + (array + (keyword_array) + (literal) + (literal))) + (list + (array + (keyword_array) + (literal) + (literal) + (literal)))) + (keyword_as) + (identifier) + (list + (column + (identifier)))) + (cross_join + (keyword_cross) + (keyword_join) + (relation + (invocation + (object_reference + (identifier)) + (term + (field + (identifier))))) + (keyword_with) + (keyword_ordinality) + (keyword_as) + (identifier) + (identifier) + (identifier)))))