Skip to content

Commit

Permalink
fix: put boolean in paren (#226)
Browse files Browse the repository at this point in the history
  • Loading branch information
eakmanrq authored Dec 23, 2024
1 parent ca1836d commit 0b3ab88
Show file tree
Hide file tree
Showing 3 changed files with 7 additions and 6 deletions.
5 changes: 3 additions & 2 deletions sqlframe/base/column.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import sqlglot
from sqlglot import Dialect
from sqlglot import expressions as exp
from sqlglot.expressions import paren
from sqlglot.helper import flatten, is_iterable
from sqlglot.optimizer.normalize_identifiers import normalize_identifiers

Expand Down Expand Up @@ -63,10 +64,10 @@ def __le__(self, other: ColumnOrLiteral) -> Column:
return self.binary_op(exp.LTE, other)

def __and__(self, other: ColumnOrLiteral) -> Column:
return self.binary_op(exp.And, other)
return self.binary_op(exp.And, other, paren=True)

def __or__(self, other: ColumnOrLiteral) -> Column:
return self.binary_op(exp.Or, other)
return self.binary_op(exp.Or, other, paren=True)

def __mod__(self, other: ColumnOrLiteral) -> Column:
return self.binary_op(exp.Mod, other, paren=True)
Expand Down
6 changes: 3 additions & 3 deletions tests/unit/standalone/test_column.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,13 +31,13 @@ def test_ge():
def test_and():
assert (
(F.col("cola") == F.col("colb")) & (F.col("colc") == F.col("cold"))
).sql() == "cola = colb AND colc = cold"
).sql() == "(cola = colb AND colc = cold)"


def test_or():
assert (
(F.col("cola") == F.col("colb")) | (F.col("colc") == F.col("cold"))
).sql() == "cola = colb OR colc = cold"
).sql() == "(cola = colb OR colc = cold)"


def test_mod():
Expand Down Expand Up @@ -89,7 +89,7 @@ def test_invert():


def test_invert_conjuction():
assert (~(F.col("cola") | F.col("colb"))).sql() == "NOT (cola OR colb)"
assert (~(F.col("cola") | F.col("colb"))).sql() == "NOT ((cola OR colb))"


def test_paren():
Expand Down
2 changes: 1 addition & 1 deletion tests/unit/standalone/test_functions.py
Original file line number Diff line number Diff line change
Expand Up @@ -2611,7 +2611,7 @@ def test_sort_array(expression, expected):
SF.length(y) - SF.length(x)
),
),
"ARRAY_SORT(cola, (x, y) -> CASE WHEN x IS NULL OR y IS NULL THEN 0 ELSE (LENGTH(y) - LENGTH(x)) END)",
"ARRAY_SORT(cola, (x, y) -> CASE WHEN (x IS NULL OR y IS NULL) THEN 0 ELSE (LENGTH(y) - LENGTH(x)) END)",
),
],
)
Expand Down

0 comments on commit 0b3ab88

Please sign in to comment.