Skip to content

Commit

Permalink
fix: add support for more operators, closes #105
Browse files Browse the repository at this point in the history
  • Loading branch information
tconbeer committed Feb 2, 2022
1 parent 2cc8825 commit 8a5c5b5
Show file tree
Hide file tree
Showing 5 changed files with 90 additions and 11 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ All notable changes to this project will be documented in this file.
### Fixes

- adds "cross join" to list of supported join types. No longer merges the "cross" keyword with the previous statement ([#110](https://github.com/tconbeer/sqlfmt/issues/110) - thank you [@rdeese](https://github.com/rdeese)!)
- add support for every valid operator in postgresql, even the weird ones, like `@>`, `||/`, `?-|` ([#105](https://github.com/tconbeer/sqlfmt/issues/105))

## [0.4.3] - 2022-01-31

Expand Down
38 changes: 32 additions & 6 deletions src/sqlfmt/dialect.py
Original file line number Diff line number Diff line change
Expand Up @@ -220,12 +220,33 @@ def __init__(self) -> None:
name="operator",
priority=910,
pattern=group(
r"\|\|?\/", # square or cube root ||/
r"~=", # geo compare
r"!?~\*?", # posix like/not like
r"\?(=|!|<=|<!)", # regex lookahead/behind
r"\?(-\||\|\||-|\|)", # regex lookahead/behind
r"@-@", # length operator
r"@@@?", # center point operator; also text match
r"##", # closest point
r"<->", # distance operator
r"@>", # contains
r"<@", # contained by
r"<>",
r"!=",
r"\|?>>=?",
r"<<(=|\|)?",
r"=>",
r"(-|#)>>?", # json extraction
r"&&",
r"&<\|?", # not extends
r"\|?&>", # not extends
r"<\^", # below
r">\^", # above
r"\?#", # intersect
r"\|\|",
r"[+\-*/%&@|^=<>:]=?",
r"~",
r"-\|-",
r"[*+?]?\?", # regex greedy/non-greedy, also ?
r"!!", # negate text match
r"[+\-*/%&@|^=<>:#!]=?", # singles
),
action=partial(
actions.add_node_to_buffer, token_type=TokenType.OPERATOR
Expand All @@ -235,16 +256,21 @@ def __init__(self) -> None:
name="word_operator",
priority=920,
pattern=group(
r"all",
r"any",
r"between",
r"exists",
r"ilike",
r"in",
r"is",
r"isnull",
r"like",
r"not",
r"like(\s+any)?",
r"notnull",
r"not",
r"over",
r"similar",
r"rlike",
r"some",
r"similar\s+to",
)
+ group(r"\W", r"$"),
action=partial(
Expand Down
4 changes: 1 addition & 3 deletions tests/data/errors/900_bad_token.sql
Original file line number Diff line number Diff line change
@@ -1,3 +1 @@
select ?
from my_table
where no_question_mark is true
select $
2 changes: 0 additions & 2 deletions tests/unit_tests/test_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@

import pytest

from sqlfmt.analyzer import SqlfmtParsingError
from sqlfmt.api import (
_format_many,
_generate_matched_paths,
Expand Down Expand Up @@ -51,7 +50,6 @@ def test_format_empty_string(all_output_modes: Mode) -> None:
@pytest.mark.parametrize(
"source,exception",
[
("?\n", SqlfmtParsingError),
("select )\n", SqlfmtBracketError),
("{{\n", SqlfmtBracketError),
],
Expand Down
56 changes: 56 additions & 0 deletions tests/unit_tests/test_dialect.py
Original file line number Diff line number Diff line change
Expand Up @@ -82,11 +82,67 @@ def test_rule_props_are_unique(self, polyglot: Polyglot) -> None:
("main", "double_colon", "::"),
("main", "colon", ":"),
("main", "semicolon", ";"),
("main", "operator", "+"),
("main", "operator", "-"),
("main", "operator", "/"),
("main", "operator", "<>"),
("main", "operator", "||"),
("main", "operator", "=>"),
("main", "operator", "||/"),
("main", "operator", "|/"),
("main", "operator", "#"),
("main", "operator", ">>"),
("main", "operator", "<<"),
("main", "operator", "!"),
("main", "operator", "!="),
# posix like/ not like
("main", "operator", "~"),
("main", "operator", "!~"),
("main", "operator", "~*"),
("main", "operator", "!~*"),
# postgresql geo operators
# see: https://www.postgresql.org/docs/current/functions-geometry.html
("main", "operator", "@-@"),
("main", "operator", "@@"),
("main", "operator", "##"),
("main", "operator", "<->"),
("main", "operator", "<@"),
("main", "operator", "@>"),
("main", "operator", "&&"),
("main", "operator", "&<"),
("main", "operator", "&>"),
("main", "operator", "<<|"),
("main", "operator", "|>>"),
("main", "operator", "&<|"),
("main", "operator", "|&>"),
("main", "operator", "<^"),
("main", "operator", ">^"),
("main", "operator", "?#"),
("main", "operator", "?-"),
("main", "operator", "?|"),
("main", "operator", "?-|"),
("main", "operator", "?||"),
("main", "operator", "~="),
# network operators
# see https://www.postgresql.org/docs/current/functions-net.html
("main", "operator", "<<="),
("main", "operator", ">>="),
# json operators
# see https://www.postgresql.org/docs/current/functions-json.html
("main", "operator", "->"),
("main", "operator", "->>"),
("main", "operator", "#>"),
("main", "operator", "#>>"),
("main", "operator", "-|-"), # range adjacency
("main", "word_operator", "is"),
("main", "word_operator", "in"),
("main", "word_operator", "like"),
("main", "word_operator", "ilike"),
("main", "word_operator", "like any"),
("main", "word_operator", "any"),
("main", "word_operator", "some"),
("main", "word_operator", "exists"),
("main", "word_operator", "all"),
("main", "as", "as"),
("main", "on", "on"),
("main", "boolean_operator", "AND"),
Expand Down

0 comments on commit 8a5c5b5

Please sign in to comment.