Skip to content

fix(cypher): x AS x projects x instead of binding it to itself - #2096

Merged
aaj3f merged 2 commits into
mainfrom
fix/cypher-self-alias
Oct 14, 2026
Merged

aaj3f merged 2 commits into
mainfrom
fix/cypher-self-alias

Conversation

@aaj3f

@aaj3f aaj3f commented Oct 12, 2026 •

Copy link
Copy Markdown
Contributor

A Cypher projection item x AS x lowered to BIND(?x AS ?x), like any other alias. A bind onto a variable that's already bound keeps the row only when the two bindings compare equal, and on an indexed ledger an encoded binding from the index never equals its decoded value. So every row dropped:

MATCH (a:Person) WITH a AS a RETURN a.name                    -- indexed: 0 rows (novelty: 4)
MATCH (a:Person) CALL (a) { WITH a AS a RETURN a.name AS z } RETURN z   -- indexed: 0 rows

The top-level form was already wrong on main. The CALL form, though, is a regression from #2006. Its CALL-correlation fix now hands the CALL's import to the subquery that a WITH in the body lowers to, so the self-bind meets an already-bound a. Before #2006 the body ran with a unbound and the bind passed, which is the only reason this shape came out right then.

x AS x is just x, so it now lowers as a plain projection of x, with no bind at all.

Tests

cypher_with_aliasing_a_variable_to_itself_keeps_every_row (it_query_cypher) runs every case twice: on a novelty ledger and on an indexed one (rebuild_and_publish_index). The cases are:

Rows on four people, novelty / indexed:

CALL (a) { … } body main this branch
WITH a.name AS n RETURN n AS z 4 / 4 4 / 4
WITH a.name AS n, 1 AS k RETURN n AS z 4 / 4 4 / 4
WITH a AS a RETURN a.name AS z 4 / 0 4 / 4
WITH a, a.name AS n RETURN n AS z 4 / 4 4 / 4
RETURN a.name AS z 4 / 4 4 / 4

With the fix reverted, exactly the six indexed self-alias cases of that test fail, and none of its other cases do:

  • WITH a AS a and WITH a AS a, a.name AS n in a CALL, each with both RETURNs;
  • the two top-level forms.

with_self_alias_projects_without_bind (fluree-db-cypher's it_lower) pins the same thing at lowering, where it doesn't depend on how two bindings compare: WITH a AS a and WITH a AS a, a.name AS n emit no Bind onto a in the WITH's subquery, and a is still projected. With the fix reverted, each shape fails it.

Related

#2010 fixes the compare itself: a bind onto a bound variable compares the two terms, whatever form each one is in. This change stands on its own either way, though. x AS x names a variable the row already has, so it shouldn't lower to a bind in the first place.

Fixes #1894

A projection item `x AS x` lowered to BIND(?x AS ?x) like any alias. A
bind onto a variable that is already bound keeps the row only when the
two bindings compare equal, and on an indexed ledger an encoded binding
never equals its decoded value. So `MATCH (a:Person) WITH a AS a RETURN
a.name` returned no rows on an indexed ledger. Inside `CALL (a) { … }`
the body now receives the import (the WITH carry), so `WITH a AS a`
there dropped every row too, where it used to return them.

`x AS x` is `x`, so it now projects x unchanged. Test: #1894's five CALL
shapes, plus `WITH a AS a, a.name AS n`, `WITH a AS b` and the top-level
forms, on novelty and on an indexed ledger.
@aaj3f aaj3f added bug Something isn't working as expected area:query Query execution, planning, fast paths, overlay, result formatting labels Oct 12, 2026
@aaj3f
aaj3f requested a review from bplatz October 12, 2026 12:46

@bplatz bplatz 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.

Approving. Lowering x AS x as a plain projection is the right fix. I confirmed the mutation claim locally: reverting the one line fails exactly the six indexed self-alias cases. I also tried a few self-alias shapes the test doesn't cover (aggregation, DISTINCT, ORDER BY with LIMIT, LIMIT followed by WHERE, relationship, path and UNWIND variables, and a self-aliased RETURN). All of them come back right on both novelty and indexed.

Two small things inline, neither blocking.

On #1894: as I read it, #2006 fixed the cross product, and this PR fixes the one shape that #2006 regressed and pins all five. So Fixes #1894 looks right to me. Please take a quick look and confirm you consider #1894 fully resolved, since merging will close it.

/// did `CALL (a) { WITH a AS a … }` once the CALL's import reached the body.
/// The five CALL shapes are #1894's: each person gets its own name, once.
#[tokio::test]
async fn cypher_with_aliasing_a_variable_to_itself_keeps_every_row() {

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.

This test only catches the self-bind because an encoded binding doesn't equal its decoded value. Once #2010 lands, that compare passes, so reverting this fix would leave the test green. Could you add a lowering-level pin in fluree-db-cypher/tests/it_lower.rs asserting that WITH a AS a emits no Bind in the WITH's subquery? That one stays meaningful after #2010.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Good call. This is the part of the test that goes quiet once #2010 lands. In f4bc23b I added with_self_alias_projects_without_bind to fluree-db-cypher/tests/it_lower.rs. For WITH a AS a and WITH a AS a, a.name AS n, it takes the WITH's subquery and checks three things: there's no Bind onto a, there's no Bind at all in the first shape, and a is still in the subquery's select. With the one-line fix in lower/stmt.rs reverted, each shape fails on its own (`a AS a` emitted a Bind). So it holds whatever the compare does after #2010.

Comment thread fluree-db-cypher/src/lower/stmt.rs Outdated
Comment on lines +608 to +611
// `x AS x` is `x`. As a `Bind` it would rebind a variable that is
// already bound, which keeps the row only when the two bindings
// compare equal, and an encoded binding from an index does not
// equal its decoded value.

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.

Nit: the second half of this stops being true once #2010 lands, since encoded and decoded bindings will compare equal. The lasting reason is enough on its own, something like: x AS x is x; a Bind onto an already-bound variable is an equality check, not a projection. The test's doc comment could drop its history narration too.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Agreed, the encoded/decoded half is history rather than a reason. The comment now reads just: x AS x is x; a Bind onto an already-bound variable is an equality check, not a projection. I also dropped the history from the test's doc comment in it_query_cypher.rs. Both are in f4bc23b.

The api test catches a self-bind only because an encoded binding does
not equal its decoded value. A lowering test holds regardless: `WITH a
AS a` and `WITH a AS a, a.name AS n` emit no Bind onto `a` in the WITH's
subquery, and `a` is still projected.

The comment at the fix and the api test's doc comment keep the lasting
reason only.
@aaj3f

aaj3f commented Oct 14, 2026

Copy link
Copy Markdown
Contributor Author

Thanks for trying the extra shapes. That's more than I'd covered.

On #1894: yes, I consider it fully resolved. #2006 fixed the cross product, i.e. the two shapes where a WITH narrows a away. This PR fixes WITH a AS a, the one shape #2006 regressed on indexed ledgers. I re-ran cypher_with_aliasing_a_variable_to_itself_keeps_every_row at f4bc23b. All five of #1894's shapes return one row per person, each with their own name, on novelty and on an indexed ledger. So Fixes #1894 should stay.

Both inline notes are addressed in f4bc23b.

@aaj3f
aaj3f merged commit 7a11ea7 into main Oct 14, 2026
19 checks passed
@aaj3f
aaj3f deleted the fix/cypher-self-alias branch October 14, 2026 13:14
bplatz added a commit that referenced this pull request Nov 17, 2026
…ision

Drops the JSON-LD half: fluree-db-query's lower.rs, the grouping tests and
jsonld-query.md take main's versions. That guard walked every JSON-LD
query's where clause, and `next` treats a JSON-LD rebind as an equality
check.

The alias guard now shares #2096's identity rule. It also reads a CALL
body's imports after a WITH: the CALL seeds them into every row of the
body, so `CALL (a) { MATCH (x) WITH x WITH x.name AS a … }` filtered to
zero rows past the old guard. The same holds for UNWIND.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

area:query Query execution, planning, fast paths, overlay, result formatting bug Something isn't working as expected

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Cypher CALL (a) { … } loses its correlation when a WITH inside the body drops the imported variable — returns a cross product

2 participants