Skip to content

Commit

Permalink
fix: support pg LIKE operators, ~~ etc (#577)
Browse files Browse the repository at this point in the history
  • Loading branch information
tconbeer authored Apr 25, 2024
1 parent b336923 commit df686ce
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 1 deletion.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,10 @@ All notable changes to this project will be documented in this file.

## [Unreleased]

### Bug Fixes

- The Postgres operators for `(NOT) (I)LIKE`, `~~`, `~~*`, `!~~`, `!~~*`, are now supported (these use two tildes where the posix version of these operators use a single tilde) ([#576](https://github.com/tconbeer/sqlfmt/issues/576) - thank you [@tuckerrc](https://github.com/tuckerrc)!).

## [0.21.2] - 2024-01-22

### Bug Fixes
Expand Down
4 changes: 4 additions & 0 deletions src/sqlfmt/operator_precedence.py
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,10 @@ def _from_operator(node: Node) -> "OperatorPrecedence":
"~*": OperatorPrecedence.MEMBERSHIP,
"!~": OperatorPrecedence.MEMBERSHIP,
"!~*": OperatorPrecedence.MEMBERSHIP,
"~~": OperatorPrecedence.MEMBERSHIP,
"~~*": OperatorPrecedence.MEMBERSHIP,
"!~~": OperatorPrecedence.MEMBERSHIP,
"!~~*": OperatorPrecedence.MEMBERSHIP,
}
return value_mapping.get(node.value, OperatorPrecedence.OTHER)

Expand Down
2 changes: 1 addition & 1 deletion src/sqlfmt/rules/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -163,7 +163,7 @@
pattern=group(
r"\|\|?\/", # square or cube root ||/
r"~=", # geo compare
r"!?~\*?", # posix like/not like
r"!?~{1,2}\*?", # posix like/not like
r"\?(=|!|<=|<!)", # regex lookahead/behind
r"\?(-\||\|\||-|\|)", # regex lookahead/behind
r"@-@", # length operator
Expand Down
7 changes: 7 additions & 0 deletions tests/unit_tests/test_rule.py
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,13 @@ def get_rule(ruleset: List[Rule], rule_name: str) -> Rule:
(CORE, "operator", "!~"),
(CORE, "operator", "~*"),
(CORE, "operator", "!~*"),
# postgres (not) like/ilike operators
# like posix but doubles the tilde
# https://github.com/tconbeer/sqlfmt/issues/576
(CORE, "operator", "~~"),
(CORE, "operator", "!~~"),
(CORE, "operator", "~~*"),
(CORE, "operator", "!~~*"),
# postgresql geo operators
# see: https://www.postgresql.org/docs/current/functions-geometry.html
(CORE, "operator", "@-@"),
Expand Down

0 comments on commit df686ce

Please sign in to comment.