Skip to content

count{} subquery miscounts with WHERE IS NULL OR predicate #5165

Closed

Description

@shulei5831sl

ArcadeDB version

arcadedata/arcadedb:latest
ArcadeDB version: 26.7.2-SNAPSHOT

Environment

  • Host OS: Linux
  • Deployment: Docker
  • Query interface: HTTP /api/v1/command/test
  • Query language: Cypher
  • Differential comparison: Neo4j 2026.05.0

Description

A correlated count { ... } expression subquery returns incorrect counts when the subquery contains a WHERE predicate combining IS NULL with another predicate using OR.

Simple predicates are counted correctly, and IS NULL alone is also counted correctly. The wrong result appears when the predicates are combined as:

WHERE f.active IS NULL OR f.active = true

Steps to reproduce

Run from a clean database.

MATCH (n) DETACH DELETE n;

CREATE
  (u1:User {id:1, active:true}),
  (u2:User {id:2, active:true}),
  (u3:User {id:3, active:true}),
  (u4:User {id:4, active:false}),
  (u5:User {id:5, active:false}),
  (u6:User {id:6, active:false}),
  (u1)-[:FRIEND]->(u4),
  (u1)-[:FRIEND]->(u5),
  (u2)-[:FRIEND]->(u4),
  (u2)-[:FRIEND]->(u1),
  (u3)-[:FRIEND]->(u5),
  (u3)-[:FRIEND]->(u6);

Data summary:

u1 -> u4(false), u5(false)
u2 -> u4(false), u1(true)
u3 -> u5(false), u6(false)

No matched friend has active IS NULL. Only u2 has a friend with active = true.

Failing query

MATCH (u:User {active:true})
RETURN u.id,
  count {
    MATCH (u)-[:FRIEND]->(f:User)
    WHERE f.active IS NULL OR f.active = true
  } AS cnt
ORDER BY u.id;

Expected behavior

Only u2 should have one matching friend.

u.id | cnt
-----+----
   1 |  0
   2 |  1
   3 |  0

Neo4j 2026.05.0 returns:

1 | 0
2 | 1
3 | 0

Actual behavior

ArcadeDB returns:

u.id | cnt
-----+----
   1 |  1
   2 |  1
   3 |  1

Rows u1 and u3 are false positives: both only have friends with active:false, so the count should be 0.

Control queries

The same count {} expression works correctly without OR.

MATCH (u:User {active:true})
RETURN u.id,
  count {
    MATCH (u)-[:FRIEND]->(f:User)
    WHERE f.active = true
  } AS cnt
ORDER BY u.id;

Expected and observed on ArcadeDB:

1 | 0
2 | 1
3 | 0

IS NULL alone is also correct:

MATCH (u:User {active:true})
RETURN u.id,
  count {
    MATCH (u)-[:FRIEND]->(f:User)
    WHERE f.active IS NULL
  } AS cnt
ORDER BY u.id;

Expected and observed on ArcadeDB:

1 | 0
2 | 0
3 | 0

A basic correlated count {} without WHERE is also correct:

MATCH (u:User {active:true})
RETURN u.id,
  count {
    MATCH (u)-[:FRIEND]->(:User)
  } AS cnt
ORDER BY u.id;

Expected and observed on ArcadeDB:

1 | 2
2 | 2
3 | 2

Additional control: equivalent aggregation outside count{}

The equivalent aggregation outside the count {} expression should return the expected result:

MATCH (u:User {active:true})
MATCH (u)-[:FRIEND]->(f:User)
WITH u.id AS id,
     sum(CASE WHEN f.active IS NULL OR f.active = true THEN 1 ELSE 0 END) AS cnt
RETURN id, cnt
ORDER BY id;

Expected:

1 | 0
2 | 1
3 | 0

Cross-engine comparison

Failing query:

MATCH (u:User {active:true})
RETURN u.id,
  count {
    MATCH (u)-[:FRIEND]->(f:User)
    WHERE f.active IS NULL OR f.active = true
  } AS cnt
ORDER BY u.id;
Engine u1 u2 u3
Neo4j 2026.05.0 0 1 0
ArcadeDB 26.7.2-SNAPSHOT 1 1 1

Summary

The correlated count {} expression appears to mis-handle a WHERE predicate containing IS NULL OR .... The result is a false-positive count of 1 for outer rows where the subquery should count zero matching rows.

Activity

  1. added this to the 26.8.1 milestone on Jul 9, 2026
  2. self-assigned this
    on Jul 9, 2026
  3. added theissue type on Jul 9, 2026
  4. added 2 commits that reference this issue on Jul 9, 2026
  5. lvca commented on Jul 9, 2026

    @lvca
    Member

    Confirmed and fixed in main (will ship in the next release).

    What happened: in a correlated count { ... } subquery, ArcadeDB injects a hidden filter to tie the inner match to the current outer row. When your inner WHERE used OR (e.g. f.active IS NULL OR f.active = true), that hidden filter was combined with a bare AND, and because AND binds tighter than OR the condition effectively became (row-link AND f.active IS NULL) OR f.active = true. The OR branch then matched globally instead of per row, which is why every user got a count of 1.

    Fix: the inner WHERE is now wrapped in parentheses before the row-link is added, so OR keeps its intended meaning. Your query now returns 0 / 1 / 0 as expected, matching Neo4j.

    Thanks for the very clear report and the ready-made reproduction, it made this quick to pin down.

  6. added 2 commits that reference this issue on Aug 1, 2026
  7. added 2 commits that reference this issue on Aug 14, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

No labels
No labels

Type

Projects

No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions