Skip to content

Commit

Permalink
Merge PR: handle BETWEEN inside CASE expression (#654)
Browse files Browse the repository at this point in the history
  • Loading branch information
nene committed Oct 31, 2023
2 parents 777d419 + 568e485 commit b4153e0
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 2 deletions.
17 changes: 15 additions & 2 deletions src/parser/grammar.ne
Original file line number Diff line number Diff line change
Expand Up @@ -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 %}
Expand All @@ -186,6 +185,7 @@ andless_expression -> ( asteriskless_andless_expression | asterisk ) {% unwrap %

asteriskless_andless_expression ->
( array_subscript
| between_predicate
| case_expression
| function_call
| property_access
Expand Down Expand Up @@ -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 <array_subscript> is currently a hack.
// A better way would be to allow <property_access> on the left side of array_subscript,
// but we currently can't do that because of another hack that requires
Expand All @@ -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,
Expand Down
13 changes: 13 additions & 0 deletions test/features/case.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
`);
});
}

0 comments on commit b4153e0

Please sign in to comment.