Skip to content

Commit b4153e0

Browse files
authored
Merge PR: handle BETWEEN inside CASE expression (#654)
2 parents 777d419 + 568e485 commit b4153e0

File tree

2 files changed

+28
-2
lines changed

2 files changed

+28
-2
lines changed

src/parser/grammar.ne

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -175,7 +175,6 @@ free_form_sql -> ( asteriskless_free_form_sql | asterisk ) {% unwrap %}
175175
asteriskless_free_form_sql ->
176176
( asteriskless_andless_expression
177177
| logic_operator
178-
| between_predicate
179178
| comma
180179
| comment
181180
| other_keyword ) {% unwrap %}
@@ -186,6 +185,7 @@ andless_expression -> ( asteriskless_andless_expression | asterisk ) {% unwrap %
186185

187186
asteriskless_andless_expression ->
188187
( array_subscript
188+
| between_predicate
189189
| case_expression
190190
| function_call
191191
| property_access
@@ -248,7 +248,7 @@ square_brackets -> "[" free_form_sql:* "]" {%
248248
})
249249
%}
250250

251-
property_access -> expression _ %DOT _ (identifier | array_subscript | all_columns_asterisk) {%
251+
property_access -> property_access_prefix _ %DOT _ (identifier | array_subscript | all_columns_asterisk) {%
252252
// Allowing property to be <array_subscript> is currently a hack.
253253
// A better way would be to allow <property_access> on the left side of array_subscript,
254254
// 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
262262
}
263263
%}
264264

265+
property_access_prefix ->
266+
( array_subscript
267+
| function_call
268+
| property_access
269+
| parenthesis
270+
| curly_braces
271+
| square_brackets
272+
| operator
273+
| identifier
274+
| parameter
275+
| literal
276+
| keyword ) {% unwrap %}
277+
265278
between_predicate -> %BETWEEN _ andless_expression_chain _ %AND _ andless_expression {%
266279
([betweenToken, _1, expr1, _2, andToken, _3, expr2]) => ({
267280
type: NodeType.between_predicate,

test/features/case.ts

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -197,4 +197,17 @@ export default function supportsCase(format: FormatFn) {
197197
tbl;
198198
`);
199199
});
200+
201+
it('formats between inside case expression', () => {
202+
const result = format(`
203+
SELECT CASE WHEN x1 BETWEEN 1 AND 12 THEN '' END c1;
204+
`);
205+
206+
expect(result).toBe(dedent`
207+
SELECT
208+
CASE
209+
WHEN x1 BETWEEN 1 AND 12 THEN ''
210+
END c1;
211+
`);
212+
});
200213
}

0 commit comments

Comments
 (0)