Skip to content

Commit 31744b2

Browse files
Feat(prql): handle NULL (#3331)
* handle null in prql * lint * update * Simplify * elif -> if * Duplicate test --------- Co-authored-by: Jo <[email protected]>
1 parent 61f5b12 commit 31744b2

File tree

2 files changed

+22
-0
lines changed

2 files changed

+22
-0
lines changed

sqlglot/dialects/prql.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,20 @@ class Parser(parser.Parser):
5555
"SORT": lambda self, query: self._parse_order_by(query),
5656
}
5757

58+
def _parse_equality(self) -> t.Optional[exp.Expression]:
59+
eq = self._parse_tokens(self._parse_comparison, self.EQUALITY)
60+
if not isinstance(eq, (exp.EQ, exp.NEQ)):
61+
return eq
62+
63+
# https://prql-lang.org/book/reference/spec/null.html
64+
if isinstance(eq.expression, exp.Null):
65+
is_exp = exp.Is(this=eq.this, expression=eq.expression)
66+
return is_exp if isinstance(eq, exp.EQ) else exp.Not(this=is_exp)
67+
if isinstance(eq.this, exp.Null):
68+
is_exp = exp.Is(this=eq.expression, expression=eq.this)
69+
return is_exp if isinstance(eq, exp.EQ) else exp.Not(this=is_exp)
70+
return eq
71+
5872
def _parse_statement(self) -> t.Optional[exp.Expression]:
5973
expression = self._parse_expression()
6074
expression = expression if expression else self._parse_query()

tests/dialects/test_prql.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,3 +58,11 @@ def test_prql(self):
5858
self.validate_identity(
5959
"from x intersect y", "SELECT * FROM x INTERSECT ALL SELECT * FROM y"
6060
)
61+
self.validate_identity(
62+
"from x filter a == null filter null != b",
63+
"SELECT * FROM x WHERE a IS NULL AND NOT b IS NULL",
64+
)
65+
self.validate_identity(
66+
"from x filter (a > 1 || null != b || c != null)",
67+
"SELECT * FROM x WHERE (a > 1 OR NOT b IS NULL OR NOT c IS NULL)",
68+
)

0 commit comments

Comments
 (0)