Repository navigation
fix(cypher): x AS x projects x instead of binding it to itself - #2096
Conversation
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.
bplatz
left a comment
There was a problem hiding this comment.
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() { |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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.
| // `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. |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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.
|
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 Both inline notes are addressed in f4bc23b. |
…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.
A Cypher projection item
x AS xlowered toBIND(?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:The top-level form was already wrong on main. The
CALLform, though, is a regression from #2006. Its CALL-correlation fix now hands the CALL's import to the subquery that aWITHin the body lowers to, so the self-bind meets an already-bounda. Before #2006 the body ran withaunbound and the bind passed, which is the only reason this shape came out right then.x AS xis justx, so it now lowers as a plain projection ofx, 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:CALL (a) { … }loses its correlation when aWITHinside the body drops the imported variable — returns a cross product #1894's fiveCALL (a) { … }shapes, each withRETURN zand withRETURN a.name AS an, z(every person gets their own name, once);CALL (a) { WITH a AS a, a.name AS n … };CALL (a) { WITH a AS b … }, which already worked;WITH a AS aforms.Rows on four people, novelty / indexed:
CALL (a) { … }bodyWITH a.name AS n RETURN n AS zWITH a.name AS n, 1 AS k RETURN n AS zWITH a AS a RETURN a.name AS zWITH a, a.name AS n RETURN n AS zRETURN a.name AS zWith the fix reverted, exactly the six indexed self-alias cases of that test fail, and none of its other cases do:
WITH a AS aandWITH a AS a, a.name AS nin a CALL, each with both RETURNs;with_self_alias_projects_without_bind(fluree-db-cypher'sit_lower) pins the same thing at lowering, where it doesn't depend on how two bindings compare:WITH a AS aandWITH a AS a, a.name AS nemit noBindontoain the WITH's subquery, andais 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 xnames a variable the row already has, so it shouldn't lower to a bind in the first place.Fixes #1894