From 568e48595e69a22b97ac87cc5ff463f33b7510e0 Mon Sep 17 00:00:00 2001 From: Zhongxian Liang Date: Tue, 31 Oct 2023 18:16:57 +0800 Subject: [PATCH] fix #500, handle BETWEEN inside CASE expression --- src/parser/grammar.ne | 17 +++++++++++++++-- test/features/case.ts | 13 +++++++++++++ 2 files changed, 28 insertions(+), 2 deletions(-) diff --git a/src/parser/grammar.ne b/src/parser/grammar.ne index 87168ffa2d..dcb917dd57 100644 --- a/src/parser/grammar.ne +++ b/src/parser/grammar.ne @@ -175,7 +175,6 @@ free_form_sql -> ( asteriskless_free_form_sql | asterisk ) {% unwrap %} asteriskless_free_form_sql -> ( asteriskless_andless_expression | logic_operator - | between_predicate | comma | comment | other_keyword ) {% unwrap %} @@ -186,6 +185,7 @@ andless_expression -> ( asteriskless_andless_expression | asterisk ) {% unwrap % asteriskless_andless_expression -> ( array_subscript + | between_predicate | case_expression | function_call | property_access @@ -248,7 +248,7 @@ square_brackets -> "[" free_form_sql:* "]" {% }) %} -property_access -> expression _ %DOT _ (identifier | array_subscript | all_columns_asterisk) {% +property_access -> property_access_prefix _ %DOT _ (identifier | array_subscript | all_columns_asterisk) {% // Allowing property to be is currently a hack. // A better way would be to allow on the left side of array_subscript, // but we currently can't do that because of another hack that requires @@ -262,6 +262,19 @@ property_access -> expression _ %DOT _ (identifier | array_subscript | all_colum } %} +property_access_prefix -> + ( array_subscript + | function_call + | property_access + | parenthesis + | curly_braces + | square_brackets + | operator + | identifier + | parameter + | literal + | keyword ) {% unwrap %} + between_predicate -> %BETWEEN _ andless_expression_chain _ %AND _ andless_expression {% ([betweenToken, _1, expr1, _2, andToken, _3, expr2]) => ({ type: NodeType.between_predicate, diff --git a/test/features/case.ts b/test/features/case.ts index 5d77009617..defb1b4bcf 100644 --- a/test/features/case.ts +++ b/test/features/case.ts @@ -197,4 +197,17 @@ export default function supportsCase(format: FormatFn) { tbl; `); }); + + it('formats between inside case expression', () => { + const result = format(` + SELECT CASE WHEN x1 BETWEEN 1 AND 12 THEN '' END c1; + `); + + expect(result).toBe(dedent` + SELECT + CASE + WHEN x1 BETWEEN 1 AND 12 THEN '' + END c1; + `); + }); }