fix(planner): distinct column names for unaliased complex expressions - #350
Open
temporaryfix wants to merge 2 commits into
Open
fix(planner): distinct column names for unaliased complex expressions#350temporaryfix wants to merge 2 commits into
temporaryfix wants to merge 2 commits into
Conversation
`expression_to_string` is the source of column names when a Return/Project item has no explicit alias (via `output_column_name`'s fallback). It used to collapse every Binary/Unary/Case/Labels/Type/Id/Slice/List/Map/subquery shape to the literal string "expr" via a catch-all `_` arm. Two such items in one clause silently produced two columns sharing a name — result-by-name lookups would shadow, and ORDER BY's pre-Return-alias prelude in `plan_sort` would resolve to whichever index was inserted last. This change covers every `LogicalExpression` variant explicitly with a Cypher-like rendering: - Binary: `(left <op> right)` via a new `binary_op_symbol` helper. - Unary: `(<op> operand)` (NOT, -, IS NULL, IS NOT NULL). - FunctionCall: `name(arg, arg, ...)` instead of `name(...)`. - Parameter, Labels, Type, Id: matching Cypher syntax. - SliceAccess, List, Map, MapProjection: Cypher-like literals. - Heavy expressions (Case, subqueries, comprehensions, Reduce): short generic labels (`case`, `exists`, `count`, `subquery`, `reduce`, `list_comprehension`, `pattern_comprehension`, predicate kind). Two unaliased instances of the same kind in one RETURN still collide; the right answer there is to alias them, per the doc comment. Drops the `_` catch-all so future `LogicalExpression`/`BinaryOp`/`UnaryOp` variants force an explicit handler at compile time (matches the codebase's existing `convert_binary_op` convention — "for forward compatibility" means no catch-all, not a `_` arm). Removes the now-unused `expression_to_string` re-export in `lpg/project.rs` (the helper is reached through `output_column_name`). Regression tests in `expression_and_projection.rs`: - `return_two_unaliased_binary_expressions_have_distinct_column_names` — verified fails pre-fix with `["expr", "expr"]`. - `return_two_unaliased_id_calls_have_distinct_column_names` — verified fails pre-fix with `["id(...)", "id(...)"]`. - `return_mixed_scalar_intrinsics_have_distinct_column_names` — already passed pre-fix because Cypher emits these as FunctionCall; retained as forward-regression guard.
The expression_to_string fix is a user-visible change for callers matching unaliased column names by string. Document under Unreleased so the next release notes pick it up.
5 tasks
Codecov Report❌ Patch coverage is
📢 Thoughts on this report? Let us know! |
Contributor
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
expression_to_string— the source of column names when aRETURN/WITH/Projectitem has no explicit alias — collapsedBinary/Unary/Case/Labels/Type/Id/Slice/List/Map/subquery shapes to the literal string"expr"via a catch-all_arm. Two such items in one clause silently produced two columns sharing a name, so result-by-name lookups in external clients (Python bindings, MCP, anything usingresult.columnsas a dict key) would shadow.The internal planner is mostly insulated: ORDER BY/GROUP BY/DISTINCT resolution goes through
resolved_column_namerather than the user-visible string, so this isn't a correctness bug in query execution. It is, however, a real bug at the client API boundary: result-set tooling and dict-style accessors over the column list see collisions for any unaliased complex expression.What changes
Every
LogicalExpressionvariant now produces a distinct, Cypher-style rendering. Examples:n.a + n.b"expr""(n.a + n.b)"NOT n.active"expr""(NOT n.active)"id(a)"id(...)""id(a)"count(*)"count(...)""count(*)"labels(n)"labels(...)""labels(n)"text_score(s.body, $q)"text_score(...)""text_score(s.body, $q)"CASE n.tier ... END"expr""case"EXISTS { ... }"expr""exists"Heavy expressions (Case, subqueries, comprehensions, Reduce) collapse to short generic labels (
"case","exists","count","subquery","reduce","list_comprehension","pattern_comprehension", predicate kind). Two unaliased instances of the same kind in one RETURN still collide; the right answer there is to alias them — documented in the function's doc comment.The
_catch-all arm is dropped from the three matches (LogicalExpression,BinaryOp,UnaryOp) so future variants force an explicit handler at compile time. Matches the codebase's existingconvert_binary_opconvention ("for forward compatibility" = exhaustive match, not a_arm).A new
binary_op_symbolhelper sits next toexpression_to_stringand mapsBinaryOpvariants to their Cypher symbols (+,<>,STARTS WITH, etc.).Breaking change
This is a user-visible change. Callers matching column names by literal string against the old
"expr"/"name(...)"outputs need to either update the checks or alias the expressions explicitly in the query.The version bump implication is for the maintainer to decide — CHANGELOG note added under
[Unreleased]§ Changed.Test plan
cargo test -p grafeo-engine --no-default-features --features "lpg gql ai parallel" --test expression_and_projection— 68 tests pass (65 existing + 3 new).return_two_unaliased_binary_expressions_have_distinct_column_names— verified fails pre-fix with["expr", "expr"].return_two_unaliased_id_calls_have_distinct_column_names— verified fails pre-fix with["id(...)", "id(...)"].return_mixed_scalar_intrinsics_have_distinct_column_names— already passed pre-fix because Cypher emits these asFunctionCall; retained as forward-regression guard for theLabels/Type/IdLogicalExpressionvariants."expr"/"id(...)"/"labels(...)"/"count(...)"literal strings — zero internal hits beyond the new tests.Notes for the reviewer
expression_to_stringand its immediate neighbor inquery/planner/common.rs; fix(planner): remove probe state-pollution in heap top-K rewrite (#347, #335) #349 touchesresolve_expression_to_columnand addsoutput_column_name/resolved_column_namehelpers in different parts of the same file. Whichever lands first, the other rebases cleanly.