Skip to content

fix(#5463, #5462): pin count(*) against a relationship inline WHERE predicate - #5485

Merged
robfrank merged 2 commits into
mainfrom
fix/5463-count-star-rel-inline-where
Jul 28, 2026
Merged

robfrank merged 2 commits into
mainfrom
fix/5463-count-star-rel-inline-where

Conversation

@robfrank

Copy link
Copy Markdown
Collaborator

Closes #5463
Closes #5462

Two issues, one defect, one reproducer, so they are fixed and closed together.

Summary

With two (:A {v:1})-[:E]->(:A {v:2}) edges tagged ok and bad, MATCH (a:A {v:1})-[r:E WHERE r.tag = 'ok']->(b:A) RETURN count(*) answered 0 while RETURN count(r) on the identical MATCH answered 1. The root cause is that the Cypher variable-usage analysis, which decides which pattern bindings the plan must materialize, did not look inside inline pattern predicates. When nothing outside the pattern referenced r, the edge binding was elided from the plan, the inline predicate r.tag = 'ok' was then evaluated against an unbound variable, and every row was rejected. Any downstream mention of r kept the binding alive and masked the defect, which is why count(r), r.tag, and even a collect(r.tag) sitting next to the count(*) (issue #5462's sharpest control) all looked correct, and why a literal WHERE 1=1 was fine: it references no variable at all.

The behavioral fix is already on main. It landed in 475582108 ("fix(cypher) #5464: honor inline WHERE predicates in node and relationship patterns"), whose third listed defect is precisely this one, tracked there as #5466: "the variable-usage analysis now looks inside inline pattern predicates." Both reporters ran build 1134185d5494f5ab0583da4a508d846449f8e04c, which predates that commit, so #5463 and #5462 were filed against a build that could not yet contain the fix. The pattern-comprehension leg of the same defect class was closed separately by 043309f61 (PR #5471, #5460).

This was verified, not assumed. The new test was run at 475582108^ (116278160, the reporters' era) and at current main:

Commit Result
116278160 (pre-fix) 8 of 10 methods FAIL, count(*) answers 0 exactly as reported
043309f61 (current main) 10 of 10 PASS

So this PR carries regression coverage only and changes no production code, because none needs changing. What was genuinely missing is a test: neither reported shape had anything pinning it, so the behavior could silently regress again.

Issue5463CountStarRelInlineWhereTest (10 methods) covers count(*) agreeing with count(r) and with the materialized r.tag rows; the predicate honored across every tag value including the empty and match-all cases; #5462's collect(r.tag) control; the literal-predicate and clause-level-WHERE controls from both reports; anonymous source, anonymous target and both-ends-anonymous shapes; right-to-left, undirected and untyped variants; count(b), count(DISTINCT b), sum(b.v) and count(*) through WITH; count(*) under a grouping key; OPTIONAL MATCH hit and miss; and both pattern-parsing paths that accept a relationship inline predicate, the MATCH path and the pattern-comprehension / COUNT {} / EXISTS {} path.

Scope note: adjacent gap found, deliberately not fixed here

While probing the surrounding defect class on current main, the relationship inline WHERE is still ignored outright inside a variable-length pattern:

MATCH (a:A {v:1})-[r:E*1..1 WHERE r.tag = 'ok']->(b:A)  RETURN count(*) AS c;  -- 2
MATCH (a:A {v:1})-[r:E*1..1 WHERE r.tag = 'zzz']->(b:A) RETURN count(*) AS c;  -- 2

The predicate changes nothing, so it is dropped rather than misevaluated, which is a different failure mode from the 0 these two issues report on a single-hop pattern. It is left alone on purpose: it is out of scope here, its shortestPath half is already tracked by #5481 and the node-pattern half by #5480 (both being worked separately), and the plain variable-length case has no open issue yet and deserves its own triage.

Test plan

  • Issue5463CountStarRelInlineWhereTest fails 8 of 10 methods at 475582108^, reproducing count(*) = 0 as reported in both issues
  • Issue5463CountStarRelInlineWhereTest passes 10 of 10 on current main
  • No production code touched, so no behavior change risk
  • Surrounding Cypher suites green: Issue5463CountStarRelInlineWhereTest, CypherInlinePatternWhereTest, Issue5464ExistsRelInlineWhereTest, OpenCypherPatternComprehensionInlineWhereTest, CypherInlinePropertyFilterTest, CypherInlinePropertyNormalizationTest, OpenCypherPatternPredicateTest, CypherPatternPredicateTest, CypherExistsTest, CypherCountSubqueryTest, CypherCountSubqueryCorrelatedTest, CypherCollectSubqueryTest, CountEdgesOptimizationTest, CypherCountNonExistingLabelTest, GAVEligibilityTest - Tests run: 170, Failures: 0, Errors: 0, Skipped: 0
  • Reviewer: confirm both issues against a released build rather than latest, since both reporters hit a snapshot image predating 475582108

A relationship inline WHERE predicate that reads an edge property returned
count(*) = 0 while count(r) returned 1 for the same MATCH: the variable-usage
analysis did not look inside inline pattern predicates, so the edge binding was
elided whenever nothing outside the pattern referenced r, and the predicate was
then evaluated against an unbound variable and rejected every row.

The behavioral fix is already on main - it landed in 4755821 as the third
defect of #5464 (tracked there as #5466), after the build both reporters used.
What was missing is coverage: neither reported shape had a test pinning it.

Adds Issue5463CountStarRelInlineWhereTest, verified to fail 8 of 10 methods at
4755821^ and to pass in full on main. It covers count(*) versus count(r),
#5462's collect(r.tag) control, the literal-predicate and clause-level-WHERE
controls, anonymous endpoints, direction and typing variants, other aggregates,
grouping, OPTIONAL MATCH, and both pattern-parsing paths that accept a
relationship inline predicate.
@mergify

mergify Bot commented Jul 28, 2026

Copy link
Copy Markdown
Contributor

Tick the box to add this pull request to the merge queue (same as @mergifyio queue).

  • Queue this pull request

@codacy-production

codacy-production Bot commented Jul 28, 2026 •

Copy link
Copy Markdown

Up to standards ✅

🟢 Issues 0 issues

Results:
0 new issues

View in Codacy

🟢 Coverage ∅ diff coverage · -6.34% coverage variation

Metric Results
Coverage variation ✅ -6.34% coverage variation
Diff coverage ✅ ∅ diff coverage

View coverage diff in Codacy

Coverage variation details
Coverable lines Covered lines Coverage
Common ancestor commit (043309f) 145777 107842 73.98%
Head commit (725adcb) 177789 (+32012) 120253 (+12411) 67.64% (-6.34%)

Coverage variation is the difference between the coverage for the head and common ancestor commits of the pull request branch: <coverage of head commit> - <coverage of common ancestor commit>

Diff coverage details
Coverable lines Covered lines Diff coverage
Pull request (#5485) 0 0 ∅ (not applicable)

Diff coverage is the percentage of lines that are covered by tests out of the coverable lines that the pull request added or modified: <covered lines added or modified>/<coverable lines added or modified> * 100%

NEW Get contextual insights on your PRs based on Codacy's metrics, along with PR and Jira context, without leaving GitHub. Enable AI reviewer
TIP This summary will be updated as you push new changes.

@claude

claude Bot commented Jul 28, 2026

Copy link
Copy Markdown

Review: fix(#5463, #5462) - pin count(*) against a relationship inline WHERE

Verdict: LGTM. Clean, well-scoped, regression-coverage-only PR. No production code is touched, so behavior-change risk is nil, and the write-up honestly frames this as pinning a fix that already landed in 475582108 rather than introducing one. That's the right call: both reporters hit a snapshot predating the fix, but neither shape had a test guarding it, so it could silently regress.

What I checked

  • Conventions - both flagged-looking items are actually repo-consistent: the @author Luca Garulli header matches the sibling tests (CypherInlinePatternWhereTest, Issue5464ExistsRelInlineWhereTest), and the docs/5463-*.md companion follows the existing docs/NNNN-*.md pattern (42 such files already present). No CLAUDE.md violation (no Claude authorship added).
  • Setup - new DatabaseFactory("./target/databases/...").create() + @AfterEach drop mirrors the established pattern in this package; DB path is test-unique, so no cross-test collision.
  • Semantics - the assertions rest on correct Cypher behavior: ungrouped count(*)/count(b) returns a single 0 row on empty input (exercised by the noMatch, optionalMiss, patternComprehensionNoMatch, existsSubqueryNoMatch cases), and the count() helper's "exactly one aggregate row" guard is appropriate for every ungrouped case it's used on.

Coverage assessment (strong)

The suite is genuinely broad for the defect class: count(*) vs count(r) vs materialized r.tag agreement; #5462's collect(r.tag) control; literal-inline and clause-level WHERE controls; anonymous endpoints; direction/undirected/untyped variants; WITH routing; grouping key; OPTIONAL MATCH hit+miss; and both parse paths (MATCH and pattern-comprehension / COUNT {} / EXISTS {}). This meaningfully hardens the surface beyond the two reported shapes.

Minor / optional (non-blocking)

No blocking concerns. 👍

@claude

claude Bot commented Jul 28, 2026

Copy link
Copy Markdown

Review: PR #5485 — regression coverage for count(*) vs relationship inline WHERE

TL;DR: LGTM. This is a clean, test-only change (a new test class + a tracking doc, +339 −0, no production code). It follows repo conventions faithfully and the coverage is genuinely strong. A few non-blocking observations below.

What's good

Non-blocking observations

  1. Overlap with existing coverage. CypherInlinePatternWhereTest already pins the core shape (line ~128: MATCH (:P {name:'Alice'})-[r:KNOWS WHERE r.since < 2000]->(m) RETURN count(*) with r unreferenced downstream). The new class is still worth having — it adds count(*) returns 0 for relationship inline WHERE while count(r) returns the correct result #5462/count(*) returns 0 while count(r) returns 1 for a relationship inline WHERE predicate #5463's exact reproducer and a much wider variant matrix — but the central defect was arguably already guarded. Worth a one-line pointer between the two classes so a future reader doesn't assume they're redundant and delete one.
  2. create() without a pre-drop. Like its siblings, setUp() calls new DatabaseFactory(path).create(); a crashed prior run leaves the ./target/databases/issue-5463-... dir and the next run throws "already exists". This matches existing convention so it's fine here, but a TestHelper-style drop-if-exists would make the suite more robust to dirty workspaces.
  3. @author Luca Garulli on a test authored by @robfrank — copied boilerplate and consistent with the rest of the tree, so not actionable, just noting the attribution is nominal.
  4. Couldn't execute Maven in the review sandbox (approval-gated), so I verified statically: the 10 @Test methods match the "10 methods" claim, and each expected count checks out against the two-edge fixture (ok/bad). I'm trusting the PR's stated Tests run: 170, Failures: 0 run for the actual green result.

No blocking concerns. Nice, well-evidenced regression PR.

@robfrank
robfrank merged commit f439716 into main Jul 28, 2026
23 of 26 checks passed
@codecov

codecov Bot commented Jul 28, 2026

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 65.49%. Comparing base (043309f) to head (725adcb).
⚠️ Report is 2 commits behind head on main.

Additional details and impacted files
@@             Coverage Diff              @@
##               main    #5485      +/-   ##
============================================
+ Coverage     65.11%   65.49%   +0.37%     
+ Complexity     1118     1115       -3     
============================================
  Files          1742     1742              
  Lines        145777   145790      +13     
  Branches      31197    31200       +3     
============================================
+ Hits          94923    95482     +559     
+ Misses        37978    37436     -542     
+ Partials      12876    12872       -4     

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

robfrank added a commit that referenced this pull request Aug 14, 2026
@robfrank
robfrank deleted the fix/5463-count-star-rel-inline-where branch August 26, 2026 09:13
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

1 participant