Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -581,8 +581,16 @@ def _and(conditions: tuple[sge.Expression, ...]) -> typing.Optional[sge.Expressi
if not conditions:
return None

def check_and_parenthesize(expr: sge.Expression) -> sge.Expression:
if isinstance(expr, sge.Or):
return sge.paren(expr)
return expr
Comment on lines +584 to +587

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

Using copy=False when parenthesizing an expression can lead to AST corruption in SQLGlot. In SQLGlot, AST nodes maintain a reference to their parent. If expr is already part of another expression tree, wrapping it with copy=False will mutate its parent pointer to point to the new Paren node, while its original parent still retains a reference to it. This can cause unexpected side effects or bugs during AST traversal or formatting. It is safer to copy the expression (which is the default behavior of sge.paren) to prevent mutating shared nodes.

Suggested change
def check_and_parenthesize(expr: sge.Expression) -> sge.Expression:
if isinstance(expr, sge.Or):
return sge.paren(expr, copy=False)
return expr
def check_and_parenthesize(expr: sge.Expression) -> sge.Expression:
if isinstance(expr, sge.Or):
return sge.paren(expr)
return expr


return functools.reduce(
lambda left, right: sge.And(this=left, expression=right), conditions
lambda left, right: sge.And(
this=check_and_parenthesize(left), expression=check_and_parenthesize(right)
),
conditions,
)


Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
SELECT
`rowindex`,
`rowindex` AS `rowindex_1`,
`int64_col`,
`float64_col`,
`string_col`
FROM `bigframes-dev`.`sqlglot_test`.`scalar_types` AS `bft_0`
WHERE
(
(
`rowindex` = 1
) OR (
`int64_col` = 2
)
)
AND (
IF((
`rowindex` = 1
) OR (
`int64_col` = 2
), `float64_col` > 0, NULL)
OR (
`string_col` = 'a'
)
)
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
SELECT
`rowindex`,
`rowindex` AS `rowindex_1`,
`int64_col`,
`string_col`
FROM `bigframes-dev`.`sqlglot_test`.`scalar_types` AS `bft_0`
WHERE
(
(
`rowindex` = 1
) OR (
`int64_col` = 2
)
)
AND STARTS_WITH(`string_col`, 'H')
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
SELECT
`rowindex`,
`rowindex` AS `rowindex_1`,
`int64_col`,
`string_col`
FROM `bigframes-dev`.`sqlglot_test`.`scalar_types` AS `bft_0`
WHERE
STARTS_WITH(`string_col`, 'H')
AND (
(
`rowindex` = 1
) OR (
`int64_col` = 2
)
)
Original file line number Diff line number Diff line change
Expand Up @@ -22,4 +22,29 @@
def test_compile_filter(scalar_types_df: bpd.DataFrame, snapshot):
bf_df = scalar_types_df[["rowindex", "int64_col"]]
bf_filter = bf_df[bf_df["rowindex"] >= 1]

snapshot.assert_match(bf_filter.sql, "out.sql")


def test_compile_filter_w_or_first(scalar_types_df: bpd.DataFrame, snapshot):
bf_df = scalar_types_df[["rowindex", "int64_col", "string_col"]]
filtered = bf_df[(bf_df["rowindex"] == 1) | (bf_df["int64_col"] == 2)]
filtered = filtered[filtered["string_col"].str.startswith("H")]

snapshot.assert_match(filtered.sql, "out.sql")


def test_compile_filter_w_or_second(scalar_types_df: bpd.DataFrame, snapshot):
bf_df = scalar_types_df[["rowindex", "int64_col", "string_col"]]
filtered = bf_df[bf_df["string_col"].str.startswith("H")]
filtered = filtered[(filtered["rowindex"] == 1) | (filtered["int64_col"] == 2)]

snapshot.assert_match(filtered.sql, "out.sql")


def test_compile_filter_w_multiple_or(scalar_types_df: bpd.DataFrame, snapshot):
bf_df = scalar_types_df[["rowindex", "int64_col", "float64_col", "string_col"]]
filtered = bf_df[(bf_df["rowindex"] == 1) | (bf_df["int64_col"] == 2)]
filtered = filtered[(filtered["float64_col"] > 0) | (bf_df["string_col"] == "a")]

snapshot.assert_match(filtered.sql, "out.sql")
Loading