Skip to content

Commit

Permalink
Resolve #47: Support quantitive like operators
Browse files Browse the repository at this point in the history
  • Loading branch information
takegue committed Aug 13, 2023
1 parent a13f120 commit 2dabe45
Show file tree
Hide file tree
Showing 3 changed files with 87 additions and 8 deletions.
32 changes: 27 additions & 5 deletions grammar.js
Original file line number Diff line number Diff line change
Expand Up @@ -1649,15 +1649,14 @@ module.exports = grammar({
['binary_and', $._keyword_and],
['binary_or', $._keyword_or],
['operator_compare', seq(optional($._keyword_not), $._keyword_in)],
['operator_compare', seq(optional($._keyword_not), $._keyword_like)],
// ['operator_compare', seq(optional($._keyword_not), $._keyword_like)],
[
'operator_compare',
seq($._keyword_is, optional($._keyword_not), kw('DISTINCT FROM')),
],
];

return choice(
...table.map(([precedence, operator]) =>
const exps = table.map(([precedence, operator]) =>
prec.left(
precedence,
seq(
Expand All @@ -1666,9 +1665,32 @@ module.exports = grammar({
field('right', $._expression),
),
)
),
);
)

const specials = [
'operator_compare',
prec.left(seq(
field('left', $._expression),
choice(
seq(
field('operator', $.like_operator),
field('right', alias($.string, $.like_pattern)),
),
seq(
field('operator', $.quantitve_like_operator),
field('right', $.quantitve_like_patterns),
)
)
))
]

return choice(...exps, ...specials);
},

like_operator: ($) => seq(optional($._keyword_not), $._keyword_like),
quantitve_like_operator: ($) => seq(optional($._keyword_not), $._keyword_like, choice(kw('ANY'), kw('SOME'), kw('ALL'))),
quantitve_like_patterns : ($) => seq('(', commaSep1(alias($.string, $.like_pattern)), ')'),

between_operator: ($) =>
prec.left(
'operator_compare',
Expand Down
54 changes: 54 additions & 0 deletions test/corpus/operators.txt
Original file line number Diff line number Diff line change
Expand Up @@ -208,3 +208,57 @@ SELECT
(identifier)
(argument
(identifier)))))))))

================================================================================
OPERATORS: Quantitive LIKE operator
================================================================================

WITH Words AS
(SELECT 'Intend with clarity.' as value UNION ALL
SELECT 'Secure with intention.' UNION ALL
SELECT 'Clarity and security.')
SELECT * FROM Words WHERE value LIKE ANY ('Intend%', '%intention%');

--------------------------------------------------------------------------------

(source_file
(query_statement
(query_expr
(cte_clause
(cte
(identifier)
(query_expr
(set_operation
(query_expr
(select
(select_list
(select_expression
(string)
(as_alias
(identifier))))))
(query_expr
(set_operation
(query_expr
(select
(select_list
(select_expression
(string)))))
(query_expr
(select
(select_list
(select_expression
(string)))))))))))
(select
(select_list
(select_all
(asterisk_expression)))
(from_clause
(from_item
(identifier)))
(where_clause
(binary_expression
(identifier)
(quantitve_like_operator)
(quantitve_like_patterns
(like_pattern)
(like_pattern))))))))
9 changes: 6 additions & 3 deletions test/corpus/statement_dml.txt
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,8 @@ WHERE DetailedInventory.product = NewArrivals.product AND
(where_clause
(binary_expression
(identifier)
(string))))
(like_operator)
(like_pattern))))
(update_statement
(identifier)
(set_clause
Expand Down Expand Up @@ -268,7 +269,8 @@ WHEN NOT MATCHED BY SOURCE THEN
(search_condition
(binary_expression
(identifier)
(string)))
(like_operator)
(like_pattern)))
(merge_insert_clause
(insert_columns
(identifier)
Expand All @@ -281,7 +283,8 @@ WHEN NOT MATCHED BY SOURCE THEN
(search_condition
(binary_expression
(identifier)
(string)))
(like_operator)
(like_pattern)))
(merge_delete_clause)))
(merge_statement
(identifier)
Expand Down

0 comments on commit 2dabe45

Please sign in to comment.