Skip to content

feat(cypher): support bounded undirected relationship alternatives - #194

Open
MuddyBootsCode wants to merge 2 commits into
hyperdimensionalcomputingai:mainfrom
MuddyBootsCode:codex/undirected-relationship-alternation
Open

MuddyBootsCode wants to merge 2 commits into
hyperdimensionalcomputingai:mainfrom
MuddyBootsCode:codex/undirected-relationship-alternation

Conversation

@MuddyBootsCode

@MuddyBootsCode MuddyBootsCode commented Nov 21, 2026 •

Copy link
Copy Markdown
Contributor

Summary

Fixes #186. A standalone bounded undirected match can follow multiple relationship types and return endpoints without naming the path. Each hop chooses its own direction. On the issue fixture, the unnamed query now returns IDs 2, 3, 4 and 5 with CSR enabled or disabled.

Bound start label review follow-up

Commit ca7214e fixes the overlapping-ID case raised in review. On the original PR, binding s to X(1) and then writing (s:Y) returned IDs 2 and 3 from Y’s edges. The planner now rejects that label change for both named and unnamed paths. Binding s to Y(1) still returns 2 and 3.

The revised commit passed 738 Rust tests (11 ignored), 1,765 Python tests (one skipped), and all nine upstream CI checks. The physical EXPLAIN for the valid review example matched the original PR with CSR enabled and disabled, for both path forms. The targeted timing difference varied between repeats. Regression logs, execution plans and timing samples

We reran both official SF1 suites on release wheels against the PR base, twice per build in each storage mode. The 30-query suite’s geometric mean latency was 1.8% higher in memory and 1.2% higher on lazy Lance. The 14-query complex suite measured 5.2% lower and 4.3% lower, respectively. Eight cases initially measured more than 20% slower. On two additional alternating runs per build, their differences ranged from 5.2% faster to 6.6% slower. The only standard-plan difference from the original PR was the order of two ANDed predicates in IC3. Full comparison, raw runs and outlier repeats

We also rotated the base, original PR, and revised wheels through three run orders for five queries. The initial IC13 slowdown reversed: the revised wheel averaged 2.9% faster than base and 1.1% slower than the original PR. Q22 was 10.9% slower than the original PR and 2.3% slower than base. Its fixed-hop CSR plan matched across revisions. The cause of that timing difference remains open. Counterbalanced runs and raw timings

The original submission’s measurements below were taken at c3e3a29.

Problem and why it matters

A chain-of-title lookup needs to walk both ways through succession, estate and distribution records. Adding an unused p = previously made an otherwise unsupported query work. On high-fan-out SF1 data, the working named workaround also builds a full Arrow path for each trail even when only endpoint IDs are returned.

Change

Reuse the existing edge inventory and trail walker. The unnamed form emits boundary columns and skips constructing the Arrow path value. It still reads the participating node and relationship tables and tracks visited edge occurrences, so repeated or parallel edges retain their Cypher semantics. A query with many eligible trails can still do substantial work before DISTINCT. Enumerating every possible combination of direction and type would grow the plan with the hop bound.

The new case requires one anonymous, explicitly bounded undirected segment with multiple listed types and resolved boundary labels. It inherits the inventory’s signed integer or uint8/16/32 key support and 20-hop maximum. Named paths still emit their path value. Queries using the new endpoint-only inventory bypass the plan-template cache and are planned with the supplied parameter values on each execution.

Execution plan and performance

For the eight-way chain-of-title tree query using the pattern from #186, the baseline unnamed form raises NotImplementedError. The working baseline and the new form each use one traversal with the same inputs. Excerpts from the release builds:

main, named:     PathMatchExec: p edge inventory
branch, unnamed: PathMatchExec: endpoints edge inventory

On an eight-way, four-level tree with 4,681 nodes, all 584 expected endpoints were returned through depth three. With CSR enabled, the upstream named and branch unnamed forms both took about 2.1 ms in memory. On SF1, starting at Tagclass 182 and following hasType or isSubclassOf up to three hops returned the same 9,901 distinct tags. In memory the upstream named medians were 8.06 and 8.11 ms versus 6.79 and 6.84 ms on branch unnamed. On disk the corresponding medians were 9.30 and 9.12 ms versus 7.95 and 7.74 ms. The timings are specific to these workloads.

On the same branch wheel, omitting the path name reduced median latency on the high-degree, three-hop SF1 query by about 16% in memory and 14% on disk. Both runs showed the difference. The low-degree and eight-way tree samples did not show a consistent gain beyond timing variation. The high-degree traversal produced 9,901 trails and 9,901 distinct endpoints. These timings use a bound starting node. Queries with only the far endpoint filtered may enumerate trails from many more starting nodes.

The unnamed multi-type form uses the named form’s trail semantics. It can return to a node via a different parallel relationship, and it cannot reuse the same stored relationship later in the trail. The existing anonymous single-type traversal skips immediate backtracking.

A separate case discovered in review reproduces on both upstream main and the branch. A named path inside an EXISTS predicate with a parameter in its endpoint property map can fail with an unbound placeholder. The new unnamed case is planned for each execution with the supplied parameter values. This patch leaves the existing named case for separate consideration.

main, named, physical EXPLAIN
SortPreservingMergeExec: [id@0 ASC NULLS LAST]
  SortExec: expr=[id@0 ASC NULLS LAST], preserve_partitioning=[true]
    AggregateExec: mode=FinalPartitioned, gby=[id@0 as id], aggr=[]
      RepartitionExec: partitioning=Hash([id@0], 18), input_partitions=18
        AggregateExec: mode=Partial, gby=[id@0 as id], aggr=[]
          ProjectionExec: expr=[q__id@0 as id]
            HashJoinExec: mode=CollectLeft, join_type=Left, on=[(s__id@0, __optional__s__id@0)], projection=[q__id@2], NullsEqual: true
              ProjectionExec: expr=[id@0 as s__id]
                FilterExec: id@0 = 0
                  KeyStatisticsExec: distinct=[id=4681]
                    DataSourceExec: partitions=1, partition_sizes=[1]
              ProjectionExec: expr=[s__id@0 as __optional__s__id, q__id@2 as q__id]
                PathMatchExec: p edge inventory
                  AggregateExec: mode=FinalPartitioned, gby=[s__id@0 as s__id, s__name@1 as s__name], aggr=[], ordering_mode=PartiallySorted([0])
                    RepartitionExec: partitioning=Hash([s__id@0, s__name@1], 18), input_partitions=18
                      AggregateExec: mode=Partial, gby=[s__id@0 as s__id, s__name@1 as s__name], aggr=[], ordering_mode=PartiallySorted([0])
                        RepartitionExec: partitioning=RoundRobinBatch(18), input_partitions=1
                          ProjectionExec: expr=[id@0 as s__id, name@1 as s__name]
                            FilterExec: id@0 = 0
                              KeyStatisticsExec: distinct=[id=4681]
                                DataSourceExec: partitions=1, partition_sizes=[1]
                  KeyStatisticsExec: distinct=[id=4681]
                    DataSourceExec: partitions=1, partition_sizes=[1]
                  KeyStatisticsExec: distinct=[dst=1089, src=520]
                    DataSourceExec: partitions=1, partition_sizes=[1]
                  KeyStatisticsExec: distinct=[dst=1601, src=520]
                    DataSourceExec: partitions=1, partition_sizes=[1]
                  KeyStatisticsExec: distinct=[dst=1601, src=520]
                    DataSourceExec: partitions=1, partition_sizes=[1]
branch, unnamed, physical EXPLAIN
SortPreservingMergeExec: [id@0 ASC NULLS LAST]
  SortExec: expr=[id@0 ASC NULLS LAST], preserve_partitioning=[true]
    AggregateExec: mode=FinalPartitioned, gby=[id@0 as id], aggr=[]
      RepartitionExec: partitioning=Hash([id@0], 18), input_partitions=18
        AggregateExec: mode=Partial, gby=[id@0 as id], aggr=[]
          ProjectionExec: expr=[q__id@0 as id]
            HashJoinExec: mode=CollectLeft, join_type=Left, on=[(s__id@0, __optional__s__id@0)], projection=[q__id@2], NullsEqual: true
              ProjectionExec: expr=[id@0 as s__id]
                FilterExec: id@0 = 0
                  KeyStatisticsExec: distinct=[id=4681]
                    DataSourceExec: partitions=1, partition_sizes=[1]
              ProjectionExec: expr=[s__id@0 as __optional__s__id, q__id@2 as q__id]
                PathMatchExec: endpoints edge inventory
                  AggregateExec: mode=FinalPartitioned, gby=[s__id@0 as s__id, s__name@1 as s__name], aggr=[], ordering_mode=PartiallySorted([0])
                    RepartitionExec: partitioning=Hash([s__id@0, s__name@1], 18), input_partitions=18
                      AggregateExec: mode=Partial, gby=[s__id@0 as s__id, s__name@1 as s__name], aggr=[], ordering_mode=PartiallySorted([0])
                        RepartitionExec: partitioning=RoundRobinBatch(18), input_partitions=1
                          ProjectionExec: expr=[id@0 as s__id, name@1 as s__name]
                            FilterExec: id@0 = 0
                              KeyStatisticsExec: distinct=[id=4681]
                                DataSourceExec: partitions=1, partition_sizes=[1]
                  KeyStatisticsExec: distinct=[id=4681]
                    DataSourceExec: partitions=1, partition_sizes=[1]
                  KeyStatisticsExec: distinct=[dst=1089, src=520]
                    DataSourceExec: partitions=1, partition_sizes=[1]
                  KeyStatisticsExec: distinct=[dst=1601, src=520]
                    DataSourceExec: partitions=1, partition_sizes=[1]
                  KeyStatisticsExec: distinct=[dst=1601, src=520]
                    DataSourceExec: partitions=1, partition_sizes=[1]

A reproducible SF1 query using the complex runner’s graph:

MATCH (s:Tagclass {id: 182})
OPTIONAL MATCH (s)-[:hasType|isSubclassOf*1..3]-(q:Tag)
RETURN DISTINCT q.id AS id ORDER BY id

For the main baseline, add p = immediately after OPTIONAL MATCH. Both builds were also checked with the named spelling. An independent trail enumeration checked the endpoint IDs on SF1.

Validation

The original submission commit has the same binary diff as the tested and benchmarked working tree (SHA-256 b521f96a426de834af817367672c36047d36735f00905e9db39ad89bcc031601). The verification archive includes the test and reproduction logs, all 16 benchmark runs with per-round timings, physical plans for both builds, and focused measurements and reproduction instructions.

  • Rust workspace, 738 passed and 11 ignored. Python tests and doctests, 1,763 passed and one skipped.
  • Rust format and Clippy, Ruff, Pyright, strict documentation build, spelling, and all 18 executable Cypher guide blocks passed.
  • Coverage includes mixed directions, parallel relationships, self-loops, zero hops, optional null rows, UNWIND and DISTINCT, property values, and different parameter bindings in nested predicates and comprehensions.
  • Both official SF1 suites passed all expected-answer checks in memory and lazy modes. Each release wheel ran twice per mode, alternating main and branch. The native module loaded by each runner was verified against the intended wheel. Only this benchmark used the shared validation slots at the start of each run.
Suite In memory Lazy Lance
30 custom queries +0.4% −2.0%
14 complex queries +0.2% −1.2%

Positive values mean higher latency. These are geometric means of per-query branch/main ratios. Physical EXPLAIN output matched in 86 of 88 comparisons after display padding was removed. In the other two, IC3 printed its two equivalent country filters in a different order. Both orders appeared in repeated EXPLAINs on unchanged main, with no change in traversal or operator structure. Given those plans and the variation between baseline repeats, the suite-level timing deltas do not establish a speedup or a regression.

Main was c9cd09659e7deb2516b88d8c6f65ead5f2b3a9b6. Both builds used Rust 1.96.0 and the macOS 26.2 Command Line Tools SDK on an Apple M5 Max with 128 GiB, macOS 27.0.1, on AC power. Both suites used the repository runner settings. The tables give means in milliseconds, branch/main changes and the repeat spread on main.

30-query SF1 suite

Runner commit 199852076ba8dceb345e18eeb2f8ac04c73f3ea1.

memory

Query Main 1 ms Main 2 ms Branch 1 ms Branch 2 ms Mean change Baseline repeat change
test_benchmark_query1 0.706 0.703 0.728 0.697 +1.1% -0.4%
test_benchmark_query2 0.588 0.602 0.698 0.571 +6.6% +2.3%
test_benchmark_query3 0.680 0.917 0.680 0.672 -15.3% +34.9%
test_benchmark_query4 0.541 0.549 0.532 0.655 +8.9% +1.4%
test_benchmark_query5 1.022 1.033 1.015 1.032 -0.4% +1.0%
test_benchmark_query6 0.471 0.467 0.461 0.477 -0.1% -0.7%
test_benchmark_query7 1.251 1.246 1.204 1.238 -2.2% -0.4%
test_benchmark_query8 1.021 0.790 0.783 0.789 -13.2% -22.7%
test_benchmark_query9 0.924 0.867 0.888 0.898 -0.3% -6.1%
test_benchmark_query10 0.884 0.879 1.010 0.885 +7.4% -0.6%
test_benchmark_query11 1.647 2.079 1.854 1.667 -5.5% +26.3%
test_benchmark_query12 0.764 0.800 0.776 0.949 +10.3% +4.7%
test_benchmark_query13 0.839 0.818 0.853 0.850 +2.8% -2.6%
test_benchmark_query14 0.563 0.571 0.567 0.586 +1.7% +1.5%
test_benchmark_query15 0.601 0.609 0.614 0.621 +2.1% +1.4%
test_benchmark_query16 0.716 0.704 0.721 0.763 +4.5% -1.7%
test_benchmark_query17 1.318 1.061 1.122 1.059 -8.3% -19.5%
test_benchmark_query18 0.732 0.731 0.737 0.732 +0.4% -0.2%
test_benchmark_query19 1.097 1.147 1.411 1.094 +11.6% +4.5%
test_benchmark_query20 1.627 1.736 1.493 1.700 -5.0% +6.7%
test_benchmark_query21 0.892 0.791 0.782 0.849 -3.1% -11.4%
test_benchmark_query22 0.774 0.761 0.771 0.763 -0.0% -1.7%
test_benchmark_query23 0.788 0.792 0.813 0.808 +2.6% +0.6%
test_benchmark_query24 0.737 0.731 0.760 0.738 +2.1% -0.8%
test_benchmark_query25 0.873 0.866 0.860 0.850 -1.7% -0.8%
test_benchmark_query26 0.955 0.796 0.839 0.796 -6.6% -16.6%
test_benchmark_query27 0.906 0.896 1.111 0.949 +14.4% -1.0%
test_benchmark_query28 1.408 1.670 1.458 1.493 -4.1% +18.7%
test_benchmark_query29 0.794 0.777 0.770 0.885 +5.4% -2.1%
test_benchmark_query30 9.512 9.568 10.128 9.625 +3.5% +0.6%

Geometric mean of per-query branch/main ratios: 1.0043.

lazy

Query Main 1 ms Main 2 ms Branch 1 ms Branch 2 ms Mean change Baseline repeat change
test_benchmark_query1 1.220 1.190 1.207 1.183 -0.8% -2.4%
test_benchmark_query2 1.388 1.392 1.373 1.366 -1.5% +0.3%
test_benchmark_query3 0.821 0.793 0.791 0.810 -0.8% -3.5%
test_benchmark_query4 0.945 0.926 0.904 0.924 -2.3% -2.0%
test_benchmark_query5 2.471 1.983 1.960 1.960 -12.0% -19.8%
test_benchmark_query6 0.797 0.829 0.855 0.801 +1.8% +3.9%
test_benchmark_query7 1.265 1.354 1.373 1.420 +6.7% +7.0%
test_benchmark_query8 1.559 1.534 1.538 1.545 -0.3% -1.6%
test_benchmark_query9 1.286 1.249 1.272 1.340 +3.0% -2.9%
test_benchmark_query10 1.130 1.104 1.108 1.117 -0.4% -2.3%
test_benchmark_query11 2.041 2.031 2.008 2.085 +0.5% -0.5%
test_benchmark_query12 4.511 4.430 4.398 4.453 -1.0% -1.8%
test_benchmark_query13 1.302 1.179 1.210 1.199 -2.9% -9.5%
test_benchmark_query14 0.888 0.762 0.770 0.761 -7.2% -14.2%
test_benchmark_query15 0.856 0.988 0.978 0.947 +4.4% +15.4%
test_benchmark_query16 1.008 1.030 1.033 1.008 +0.2% +2.2%
test_benchmark_query17 1.725 1.811 1.683 1.673 -5.1% +5.0%
test_benchmark_query18 1.415 1.409 1.370 1.367 -3.1% -0.4%
test_benchmark_query19 1.615 1.539 1.556 1.541 -1.8% -4.7%
test_benchmark_query20 39.998 39.700 39.775 39.437 -0.6% -0.7%
test_benchmark_query21 0.860 0.837 0.838 0.807 -3.1% -2.7%
test_benchmark_query22 1.307 1.161 1.212 1.147 -4.4% -11.2%
test_benchmark_query23 1.327 1.521 1.294 1.371 -6.4% +14.6%
test_benchmark_query24 1.035 1.031 1.000 1.032 -1.6% -0.4%
test_benchmark_query25 1.322 1.295 1.271 1.273 -2.8% -2.1%
test_benchmark_query26 1.081 1.040 1.064 1.102 +2.1% -3.8%
test_benchmark_query27 1.447 1.465 1.409 1.394 -3.8% +1.2%
test_benchmark_query28 1.954 2.051 1.945 1.937 -3.1% +5.0%
test_benchmark_query29 1.201 1.154 1.133 1.078 -6.1% -3.9%
test_benchmark_query30 12.674 13.316 12.409 12.359 -4.7% +5.1%

Geometric mean of per-query branch/main ratios: 0.9803.

14-query complex SF1 suite

Runner commit a332324f2d4660b03d1d429d8ef99767af3981e5.

memory

Query Main 1 ms Main 2 ms Branch 1 ms Branch 2 ms Mean change Baseline repeat change
test_benchmark_query1 23.430 23.834 22.957 22.977 -2.8% +1.7%
test_benchmark_query2 2.523 2.586 2.550 2.581 +0.4% +2.5%
test_benchmark_query3 21.858 20.893 21.269 21.197 -0.7% -4.4%
test_benchmark_query4 6.158 6.308 6.639 6.404 +4.6% +2.4%
test_benchmark_query5 38.757 39.340 40.512 40.629 +3.9% +1.5%
test_benchmark_query6 20.702 20.601 22.545 20.829 +5.0% -0.5%
test_benchmark_query7 9.542 9.780 9.477 9.347 -2.6% +2.5%
test_benchmark_query8 2.405 2.444 2.415 2.372 -1.3% +1.6%
test_benchmark_query9 35.762 35.538 35.834 35.730 +0.4% -0.6%
test_benchmark_query10 14.997 14.992 14.723 14.547 -2.4% -0.0%
test_benchmark_query11 3.059 3.134 3.107 3.349 +4.2% +2.5%
test_benchmark_query12 15.104 15.643 14.842 14.798 -3.6% +3.6%
test_benchmark_query13 1.158 1.143 1.135 1.131 -1.5% -1.3%
test_benchmark_query14 241.467 248.242 245.194 243.433 -0.2% +2.8%

Geometric mean of per-query branch/main ratios: 1.0021.

lazy

Query Main 1 ms Main 2 ms Branch 1 ms Branch 2 ms Mean change Baseline repeat change
test_benchmark_query1 36.235 35.691 34.300 35.179 -3.4% -1.5%
test_benchmark_query2 25.584 24.728 25.075 25.578 +0.7% -3.3%
test_benchmark_query3 156.647 157.030 159.983 157.395 +1.2% +0.2%
test_benchmark_query4 13.536 14.517 13.986 14.213 +0.5% +7.2%
test_benchmark_query5 69.120 69.096 69.888 71.437 +2.2% -0.0%
test_benchmark_query6 25.009 22.299 23.642 23.204 -1.0% -10.8%
test_benchmark_query7 14.260 14.112 13.931 13.750 -2.4% -1.0%
test_benchmark_query8 3.391 3.481 3.533 3.396 +0.8% +2.6%
test_benchmark_query9 207.231 227.554 207.964 204.019 -5.2% +9.8%
test_benchmark_query10 23.430 24.082 23.665 23.260 -1.2% +2.8%
test_benchmark_query11 5.860 5.740 5.687 6.192 +2.4% -2.1%
test_benchmark_query12 19.406 19.460 19.419 17.884 -4.0% +0.3%
test_benchmark_query13 1.752 1.430 1.443 1.414 -10.2% -18.4%
test_benchmark_query14 251.289 250.865 268.708 249.240 +3.1% -0.2%

Geometric mean of per-query branch/main ratios: 0.9876.

User impact

Endpoint queries can use ordinary MATCH or OPTIONAL MATCH without declaring an unused path. The Cypher guide documents the supported shape, and the operator guide explains its inventory and output cost.

@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Nov 21, 2026 •

Copy link
Copy Markdown
Codex Review Summary: ✅ Completed

This comment shows the latest Codex review activity on this pull request.

Review Status Commit Review trigger
📝 Code Review ✅ Completed 2026-11-21T23:11:55.855052Z c3e3a29 PR opened
ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review" or "@codex security review".

Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings.

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: c3e3a294d7

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

Comment on lines +913 to +915
if match_clause.is_bounded_undirected_alternation() {
if let [GraphPattern::Path(path)] = inferred.patterns.as_slice() {
return self.plan_inventory_path(base, path);

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

P2 Badge Validate the label of an already-bound start node

When two labels share an ID, MATCH (s:X {id: 1}) MATCH (s:Y)-[:A|B*1..2]-(q:Y) RETURN q.id (with A and B configured between Y nodes) follows paths from Y(1), although s was bound to X(1). This new shortcut calls plan_inventory_path, which skips validate_variable_label for an already-bound start. analyze_operator then overwrites the starting variable's label with Y, and Pending::bound looks up its existing numeric ID under that label, silently matching a different node. Validate the starting label before taking this route, as plan_path does, so conflicting bindings are rejected rather than producing unrelated endpoints.

Useful? React with 👍 / 👎.

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.

Fixed in ca7214e. I reproduced the overlapping-ID example on the original PR: it returned IDs 2 and 3 by following Y’s edges from a variable bound to X. The planner now reports Variable 's' already has label 'X', cannot redefine as 'Y' before traversal. The matching-label control still returns 2 and 3.

The regression covers both named and unnamed paths. Their valid physical plans matched before and after with CSR on and off. All nine CI checks passed, and I added the refreshed SF1 results to the PR description. Tests, plans and benchmark evidence

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.

Support unnamed bounded undirected paths with multiple relationship types

1 participant