Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix #500, handle BETWEEN inside CASE expression #654

Merged
merged 1 commit into from
Oct 31, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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;
`);
});
}
Loading