Skip to content

fix(bigframes): parenthesize OR expressions for AND chaining in SQLGlot - #18113

Draft
sycai wants to merge 1 commit into
mainfrom
sycai_fix_and_chaining
Draft

fix(bigframes): parenthesize OR expressions for AND chaining in SQLGlot#18113
sycai wants to merge 1 commit into
mainfrom
sycai_fix_and_chaining

Conversation

@sycai

@sycai sycai commented Aug 13, 2026

Copy link
Copy Markdown
Contributor

No description provided.

@sycai sycai self-assigned this Aug 13, 2026
@sycai sycai added the do not merge Indicates a pull request not ready for merge, due to either quality or timing. label Aug 13, 2026

@gemini-code-assist gemini-code-assist Bot left a comment

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.

Code Review

This pull request ensures that OR expressions are correctly parenthesized when combined with AND operators in the SQLGlot compiler, and adds unit tests to verify the generated SQL. The reviewer pointed out that using copy=False when parenthesizing expressions can lead to AST corruption in SQLGlot due to parent pointer mutation, and recommended copying the expression instead.

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

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

do not merge Indicates a pull request not ready for merge, due to either quality or timing.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant