Skip to content

fix(binder): keep a variable's name when an arithmetic identity returns it - #1288

Open
zahariash wants to merge 1 commit into
LadybugDB:mainfrom
zahariash:fix/arith-identity-alias
Open

zahariash wants to merge 1 commit into
LadybugDB:mainfrom
zahariash:fix/arith-identity-alias

Conversation

@zahariash

Copy link
Copy Markdown
Contributor

This work was produced with the help of language models.

Fixes #1247.

UNWIND [1, 2] AS x WITH x + 0 AS y, x RETURN y, x returned two columns named x, as did RETURN x, x + 0. A projection names each item by setting the alias on its expression, and a variable reference binds to the scope's own object, so when x + 0, x * 1, x - 0 or x / 1 simplified to that object, the alias renamed the variable.

Fix. An arithmetic identity over a scope expression stays as written, so the alias lands on +(x,0). A scope expression is also never simplified again, and NOT NOT does not unwrap a NOT taken from the scope; either would return a part of the kept expression that is no longer in scope: CASE WHEN true THEN m END after WITH n + 0 AS m then ran the aggregate again, and NOT z after WITH NOT b AS z fails on main today with "Cannot evaluate expression with type VARIABLE". Boolean, CASE and cast simplifications still return the operand and still rename it, as plain WITH x AS y, x does; that is unchanged and tracked in #1287. Property references are copies, so n.id + 0 = 1 still becomes an index lookup.

Tests. projection/simplified_variable_alias.test: ArithmeticIdentityOverVariableKeepsAlias checks the column names of the issue's three queries and of RETURN x, x + 0; on main they are x, x, m, m, x, x. SimplifiedScopeExpressionReferencedLater and SimplifiedNodeStaysPattern reference a kept x + 0, b AND true, NOT b or node from a later CASE, AND, NOT, ORDER BY or MATCH; the NOT z query fails on main, the rest pin the two rules. Full suite: no failures.

…ns it

A projection names each item by setting the alias on its expression, and a
variable reference binds to the scope's own object. When x + 0, x * 1,
x - 0 or x / 1 simplified to that object, the item's alias renamed the
variable: WITH x + 0 AS y, x RETURN y, x and RETURN x, x + 0 each returned
two columns named x. An arithmetic identity over a scope expression now
stays as written, so the alias lands on it.

A scope expression is also never simplified again, and NOT NOT no longer
unwraps a NOT from the scope. Otherwise a nested reference to a kept
expression, e.g. CASE WHEN true THEN m END after WITH n + 0 AS m, was
simplified into an operand that is no longer in scope, which ran an
aggregate again or failed to evaluate.

Boolean and CASE simplifications still return the operand and still
rename it, as WITH x AS y, x does; that is unchanged. Property references
are copies, so n.id + 0 = 1 still becomes an index lookup.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

This branch has not been deployed

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

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Bug: a WITH alias on a folded identity such as x + 0 AS y is named after the operand

1 participant