Skip to content

openCypher: the COUNT push-downs for LSQB Q2 and Q3 count a parallel or a reciprocal KNOWS edge once, where the row pipeline matches one row per relationship (Q2 only while a Graph Analytical View over every type is READY) #9298

Closed

Description

@tae898

Summary

With two KNOWS edges between the same two persons, the LSQB Q2 text MATCH (person1:Person)-[:KNOWS]-(person2:Person), (person1)<-[:HAS_CREATOR]-(comment:Comment)-[:REPLY_OF]->(post:Post)-[:HAS_CREATOR]->(person2) RETURN count(*) answers 1 while a Graph Analytical View over every type is READY, and 2 without a view and with a view over Person and KNOWS only; the same pattern behind a WITH (the row pipeline) answers 2, and 2 is the Cypher answer because each of the two relationships is a match of its own. The same holds when the two edges are p1->p2 and p2->p1. EXPLAIN shows COUNT PAIR JOIN (build-probe hash join) (PairHashJoinOp) in all three situations, so it is the same operator with the right and the wrong answer.

The LSQB Q3 text (the three persons of a country-partitioned triangle) with a second KNOWS edge on one side of the triangle answers 10 without a view and with the narrow view, and 8 with the view over every type, where the row pipeline answers 12 (each of the six orderings of the triangle can use either of the two parallel edges). With KNOWS in both directions on one side it is the same 10, 10, and 8 against 12. EXPLAIN shows COUNT TRIANGLES (CSR sorted intersection, country-partitioned) (PartitionedTriangleOp) in all three situations. The control, the same triangle with one edge per side, is right everywhere (6).

LSQB data has one edge per pair of persons, which is why the benchmark does not show it. The answer is the same on the 26.10.1 release jars (d36b4ca3ae4c170abc73598e0dffa2bb58e06621) and on main cbf701d66ed1f8633111233d9d9db3d9d7464b98, on Temurin 25.0.4 with -XX:+UseCompactObjectHeaders and on Temurin 21.0.12. A self loop on one person next to the triangle also answers 20, 20, and 18 against 6, which is the known double count of an undirected self loop (#9283) and not what this is about.

Where (at cbf701d66ed1f8633111233d9d9db3d9d7464b98)

PairHashJoinOp.buildAndProbeInline tests the probe edge with Arrays.binarySearch(probeNbrs, pStart, pEnd, ep2) >= 0 and then adds 1, in its fused two-hop path and in its general path, so a probe neighbour that occurs twice in the merged KNOWS range (a parallel edge, or the two directions of a reciprocal pair, both listed in the BOTH view) counts as one.

PartitionedTriangleOp.countRange (the view over every type) and countTrianglesPerNode intersect the two sorted neighbour ranges with a merge that advances both cursors on a match (iu++; iv++), so a value that occurs m times in one range and n times in the other is counted min(m, n) times where the matches are m * n. The two paths differ in how much of the multiplicity they lose (8 and 10 above), which I did not trace further.

Repro

Deterministic, three persons, java MultigraphCounts <an empty directory> (the program creates one database per case in <dir>-C0 to <dir>-C8):

import com.arcadedb.database.Database;
import com.arcadedb.database.DatabaseFactory;
import com.arcadedb.database.RID;
import com.arcadedb.query.sql.executor.ResultSet;

/**
 * Minimal deterministic cases for what LsqbDiff found on graphs with a parallel or a reciprocal KNOWS edge (no self loop, so not #9283): the LSQB Q2 and Q3 shapes answer a smaller
 * number than the same pattern behind a WITH (the row pipeline), the Q2 one only while a Graph Analytical View over every type is READY. The expected number is the Cypher one,
 * a match per relationship: every parallel or reciprocal edge is a match of its own.
 *
 *   java MultigraphCounts <empty directory>
 *
 * Cases (Person p1, p2, p3 and, for Q2, a Comment c created by p1 that replies to a Post created by p2):
 *   Q2 shape, one KNOWS p1->p2 (control), the same with a second KNOWS p1->p2 (parallel), the same with KNOWS p2->p1 in addition (reciprocal);
 *   Q3 shape (a KNOWS triangle p1, p2, p3), the same with a second KNOWS p1->p2 (parallel).
 * Each case runs with no view, with a view over Person and KNOWS only, and with a view over every type.
 */
public class MultigraphCounts {
  static Database db;

  public static void main(final String[] args) throws Exception {
    System.out.println("engine " + com.arcadedb.Constants.getRawVersion() + " build " + com.arcadedb.Constants.getBuildNumber() + ", java " + System.getProperty("java.version"));
    final String q2 = "MATCH (person1:Person)-[:KNOWS]-(person2:Person), (person1)<-[:HAS_CREATOR]-(comment:Comment)-[:REPLY_OF]->(post:Post)-[:HAS_CREATOR]->(person2)";
    final String q3 = "MATCH (person1:Person)-[:KNOWS]-(person2:Person)-[:KNOWS]-(person3:Person)-[:KNOWS]-(person1)";
    final String q3l = "MATCH (country:Country) MATCH (person1:Person)-[:IS_LOCATED_IN]->(city1:City)-[:IS_PART_OF]->(country) MATCH (person2:Person)-[:IS_LOCATED_IN]->(city2:City)-[:IS_PART_OF]->(country) "
        + "MATCH (person3:Person)-[:IS_LOCATED_IN]->(city3:City)-[:IS_PART_OF]->(country) MATCH (person1)-[:KNOWS]-(person2)-[:KNOWS]-(person3)-[:KNOWS]-(person1)";
    final String WITH3L = "country, person1, person2, person3, city1, city2, city3";
    final String[] views = { "none", "narrow", "wide" };
    // a case: knows edges as {from, to} over persons 0..2, whether the Q2 arms exist, the query, its WITH variables, the expected count
    final Object[][] cases = { { "Q2 one KNOWS p1->p2 (control)", new int[][] { { 0, 1 } }, true, q2, "person1, person2, comment, post", 1L },
        { "Q2 two parallel KNOWS p1->p2", new int[][] { { 0, 1 }, { 0, 1 } }, true, q2, "person1, person2, comment, post", 2L },
        { "Q2 KNOWS p1->p2 and p2->p1", new int[][] { { 0, 1 }, { 1, 0 } }, true, q2, "person1, person2, comment, post", 2L },
        { "Q3 triangle p1-p2-p3 (control)", new int[][] { { 0, 1 }, { 1, 2 }, { 2, 0 } }, false, q3, "person1, person2, person3", 6L },
        { "Q3 triangle with a second KNOWS p1->p2", new int[][] { { 0, 1 }, { 0, 1 }, { 1, 2 }, { 2, 0 } }, false, q3, "person1, person2, person3", 12L },
        { "Q3 as the benchmark writes it (one country, one city), triangle (control)", new int[][] { { 0, 1 }, { 1, 2 }, { 2, 0 } }, false, q3l, WITH3L, 6L },
        { "Q3 as the benchmark writes it, a second KNOWS p1->p2", new int[][] { { 0, 1 }, { 0, 1 }, { 1, 2 }, { 2, 0 } }, false, q3l, WITH3L, 12L },
        { "Q3 as the benchmark writes it, KNOWS p1->p2 and p2->p1", new int[][] { { 0, 1 }, { 1, 0 }, { 1, 2 }, { 2, 0 } }, false, q3l, WITH3L, 12L },
        { "Q3 as the benchmark writes it, a self loop on p1 next to the triangle", new int[][] { { 0, 1 }, { 1, 2 }, { 2, 0 }, { 0, 0 } }, false, q3l, WITH3L, 6L } };
    int caseNo = 0;
    for (final Object[] c : cases) {
      final String type = "C" + caseNo++;
      // each case in its own database copy: the data differ, so build the graph fresh in a directory of its own
      final DatabaseFactory cf = new DatabaseFactory(args[0] + "-" + type);
      if (cf.exists())
        cf.open().drop();
      final Database cdb = cf.create();
      for (final String t : new String[] { "Person", "Comment", "Post", "Country", "City" })
        cdb.command("sql", "CREATE VERTEX TYPE " + t);
      for (final String e : new String[] { "KNOWS", "HAS_CREATOR", "REPLY_OF", "IS_LOCATED_IN", "IS_PART_OF" })
        cdb.command("sql", "CREATE EDGE TYPE " + e);
      db = cdb;
      final RID[] p = new RID[3];
      final RID[] other = new RID[2];
      cdb.transaction(() -> {
        for (int i = 0; i < 3; i++)
          p[i] = cdb.newVertex("Person").set("id", i).save().getIdentity();
        final RID country = cdb.newVertex("Country").set("id", 1).save().getIdentity(), city = cdb.newVertex("City").set("id", 1).save().getIdentity();
        city.asVertex().newEdge("IS_PART_OF", country).save();
        for (int i = 0; i < 3; i++)
          p[i].asVertex().newEdge("IS_LOCATED_IN", city).save();
        other[0] = cdb.newVertex("Comment").set("id", 10).save().getIdentity();
        other[1] = cdb.newVertex("Post").set("id", 20).save().getIdentity();
        for (final int[] e : (int[][]) c[1])
          p[e[0]].asVertex().newEdge("KNOWS", p[e[1]]).save();
        if ((Boolean) c[2]) {
          other[0].asVertex().newEdge("HAS_CREATOR", p[0]).save();
          other[0].asVertex().newEdge("REPLY_OF", other[1]).save();
          other[1].asVertex().newEdge("HAS_CREATOR", p[1]).save();
        }
      });
      final String written = c[3] + " RETURN count(*) AS n", rows = c[3] + " WITH " + c[4] + " RETURN count(*) AS n";
      final StringBuilder sb = new StringBuilder();
      for (final String v : views) {
        if (v.equals("narrow"))
          view(cdb, "narrow", "VERTEX TYPES (Person) EDGE TYPES (KNOWS) PROPERTIES (id) UPDATE MODE OFF");
        if (v.equals("wide")) {
          cdb.command("sql", "DROP GRAPH ANALYTICAL VIEW narrow");
          view(cdb, "wide", "VERTEX TYPES (Person, Comment, Post, Country, City) EDGE TYPES (KNOWS, HAS_CREATOR, REPLY_OF, IS_LOCATED_IN, IS_PART_OF) PROPERTIES (id) UPDATE MODE OFF");
        }
        sb.append(" | ").append(v).append(" view ").append(count(written));
      }
      System.out.println("CASE " + c[0] + " | expected " + c[5] + " | row pipeline " + count(rows) + sb);
      cdb.close();
    }
  }

  static void view(final Database d, final String name, final String body) throws Exception {
    d.command("sql", "CREATE GRAPH ANALYTICAL VIEW " + name + " " + body);
    for (int i = 0; i < 6000; i++) {
      try (ResultSet rs = d.query("sql", "SELECT FROM schema:graphAnalyticalViews WHERE name = ?", name)) {
        if (rs.hasNext() && "READY".equals(rs.next().getProperty("status")))
          return;
      }
      Thread.sleep(20);
    }
    throw new IllegalStateException("view not ready");
  }

  static String count(final String q) {
    try (ResultSet rs = db.query("opencypher", q)) {
      return String.valueOf(((Number) rs.next().getProperty("n")).longValue());
    } catch (final Exception e) {
      return "EXC " + e.getClass().getSimpleName();
    }
  }
}

Output on main cbf701d66ed1f8633111233d9d9db3d9d7464b98, Temurin 25.0.4 (identical on the 26.10.1 jars and on Temurin 21.0.12):

CASE Q2 one KNOWS p1->p2 (control) | expected 1 | row pipeline 1 | none view 1 | narrow view 1 | wide view 1
CASE Q2 two parallel KNOWS p1->p2 | expected 2 | row pipeline 2 | none view 2 | narrow view 2 | wide view 1
CASE Q2 KNOWS p1->p2 and p2->p1 | expected 2 | row pipeline 2 | none view 2 | narrow view 2 | wide view 1
CASE Q3 triangle p1-p2-p3 (control) | expected 6 | row pipeline 6 | none view 6 | narrow view 6 | wide view 6
CASE Q3 triangle with a second KNOWS p1->p2 | expected 12 | row pipeline 12 | none view 12 | narrow view 12 | wide view 12
CASE Q3 as the benchmark writes it (one country, one city), triangle (control) | expected 6 | row pipeline 6 | none view 6 | narrow view 6 | wide view 6
CASE Q3 as the benchmark writes it, a second KNOWS p1->p2 | expected 12 | row pipeline 12 | none view 10 | narrow view 10 | wide view 8
CASE Q3 as the benchmark writes it, KNOWS p1->p2 and p2->p1 | expected 12 | row pipeline 12 | none view 10 | narrow view 10 | wide view 8
CASE Q3 as the benchmark writes it, a self loop on p1 next to the triangle | expected 6 | row pipeline 6 | none view 20 | narrow view 20 | wide view 18

Expected

Every CASE line reads the same in all four columns: a parallel or a reciprocal edge is a match of its own, so the push-downs multiply by the multiplicity (the length of the equal run in each range) instead of testing for existence or pairing the runs one to one.

Near misses, ruled out

#9283 and #8750 are the self-loop double count, which is a different cause (the loop is listed twice in the BOTH view); the fourth Q3 case above shows it next to this one. #9290 is the COUNT ANTI-JOIN CHAIN shapes. #5663 (closed) is a cycle pattern on the row pipeline. Searched open and closed issues for "PairHashJoinOp", "PartitionedTriangleOp", "parallel edges count", and "count push-down parallel": nothing on the multiplicity of a parallel or reciprocal edge in these two operators.

Activity

  1. added
    engine
    severity:minor
    on Oct 6, 2026
  2. self-assigned this
    on Oct 6, 2026
  3. added
    in progress
    on Oct 6, 2026
  4. added this to the 26.11.1 milestone on Oct 6, 2026
  5. removed
    in progress
    on Oct 6, 2026
  6. lvca commented on Oct 6, 2026

    @lvca
    Member

    Fixed in #9300 (26.11.1).

    The count shortcuts used for the LSQB Q2 and Q3 shapes counted a parallel or reciprocal KNOWS edge once, so count(*) could come out lower than the row-by-row answer. Each edge is a match of its own, and they are now counted that way, with or without a Graph Analytical View. The self-loop double count (#9283) is a separate problem and is not part of this fix.


    ❤️ Thanks for helping make ArcadeDB better! ArcadeDB is free and open source, sustained by its community. If it's useful to you or your company, please consider becoming a sponsor to keep development going.

  7. added a commit that references this issue on Oct 8, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

bugengineopencypherseverity:minor

Type

No type

Projects

No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions