Skip to content

Commit

Permalink
Postgres, Redshift: Support SIMILAR TO pattern matching expressions (#…
Browse files Browse the repository at this point in the history
…2732)

* Added first LIKE segment

* See if this fixes recursive references

* Added more segments

* Committing PatternMatchingExpressionSegment so far

* Added first LIKE segment

* See if this fixes recursive references

* Added more segments

* Committing PatternMatchingExpressionSegment so far

* Rolled back dev changes for the ANSI dialect

* Copied Expression_A_Grammar to postgres

* Added first LIKE segment

* See if this fixes recursive references

* Added more segments

* Committing PatternMatchingExpressionSegment so far

* Added first LIKE segment

* See if this fixes recursive references

* Added more segments

* Committing PatternMatchingExpressionSegment so far

* Rolled back dev changes for the ANSI dialect

* Copied Expression_A_Grammar to postgres

* Added Like operators

* Updated postgres dialect

* Updated implementation

* Implemented SIMILAR TO

* Added SIMILAR TO tests

* Added POSIX Expression and Operator

* Simplified implementation + added redshift tests

* Linted

* Added TODO

* Added links to issues addressing TODOs

* Simplified to LikeGrammar overwrite

Co-authored-by: Barry Pollard <barry_pollard@hotmail.com>

* Deleted bloat not needed due to LikeGrammar overwrite

Co-authored-by: Barry Pollard <barry_pollard@hotmail.com>

* Reverted LikeOperator changes in Postgres

* Deleted unnecessary TODO

* Updated yml hashes

* Linted

* Update src/sqlfluff/dialects/dialect_ansi.py

Co-authored-by: Barry Pollard <barry_pollard@hotmail.com>
  • Loading branch information
PLBMR and tunetheweb committed Feb 28, 2022
1 parent 413cd9e commit a1571ac
Show file tree
Hide file tree
Showing 5 changed files with 1,027 additions and 0 deletions.
1 change: 1 addition & 0 deletions src/sqlfluff/dialects/dialect_postgres.py
Expand Up @@ -202,6 +202,7 @@
)

postgres_dialect.replace(
LikeGrammar=OneOf("LIKE", "ILIKE", Sequence("SIMILAR", "TO")),
ComparisonOperatorGrammar=OneOf(
Ref("EqualsSegment"),
Ref("GreaterThanSegment"),
Expand Down
@@ -0,0 +1,72 @@
-- postgres_pattern_match_expressions.sql
/* examples of pattern match expressions
( https://www.postgresql.org/docs/14/functions-matching.html ) that are
supported in postgres. */

-- LIKE/ILIKE expressions supported
SELECT *
FROM animals
WHERE family LIKE '%ursidae%';

SELECT *
FROM animals
WHERE family NOT LIKE '%ursidae%';

SELECT *
FROM animals
WHERE genus ILIKE '%ursus%';

SELECT *
FROM animals
WHERE genus NOT ILIKE '%ursus%';

SELECT *
FROM animals
WHERE family LIKE '%ursidae%' ESCAPE '\\';

SELECT *
FROM animals
WHERE genus NOT ILIKE '%ursus%' ESCAPE '\\';

SELECT COALESCE(family LIKE '%ursidae%' ESCAPE '\\', FALSE) AS is_bear
FROM animals;

-- SIMILAR TO expressions supported
SELECT *
FROM animals
WHERE family SIMILAR TO '%ursidae%';

SELECT *
FROM animals
WHERE family NOT SIMILAR TO '%ursidae%';

SELECT *
FROM animals
WHERE genus SIMILAR TO '%ursus%';

SELECT *
FROM animals
WHERE genus NOT SIMILAR TO '%ursus%';

SELECT *
FROM animals
WHERE family SIMILAR TO '%ursidae%' ESCAPE '\\';

SELECT *
FROM animals
WHERE genus NOT SIMILAR TO '%ursus%' ESCAPE '\\';

SELECT COALESCE(family SIMILAR TO '%ursidae%' ESCAPE '\\', FALSE) AS is_bear
FROM animals;

-- From https://github.com/sqlfluff/sqlfluff/issues/2722
WITH cleaned_bear_financial_branch AS (
SELECT
branch_id,
TO_NUMBER(CASE WHEN honey_numerical_code SIMILAR TO '[0-9]{0,7}.?[0-9]{0,2}' THEN honey_numerical_code ELSE NULL END, '24601') AS honey_numerical_code
FROM bear_financial_branch
)

SELECT branch_id
FROM cleaned_bear_financial_branch
LIMIT 10;

0 comments on commit a1571ac

Please sign in to comment.