Skip to content

SQL p IS NULL through a UNIQUE or UNIQUE_HASH index with NULL_STRATEGY INDEX returns one of the records whose p is null, and count(*) counts one #9237

Closed

Description

@tae898

A unique index created with NULL_STRATEGY INDEX accepts several records whose key is null (an all-null key is exempt from uniqueness, as the docs say for the sorted build: "As with online unique indexes, an all-null key is exempt (SQL NULL != NULL)", indexes.adoc L270). It keeps an entry for each of them (iterating such an LSM_TREE index lists one null entry per record), but WHERE p IS NULL answered through that index returns only one record, and SELECT count(*) ... WHERE p IS NULL counts one. The same type without an index, and a NOTUNIQUE index with NULL_STRATEGY INDEX, return all of them, and so do p <=> null and openCypher WHERE n.p IS NULL on every type.

In the repro each type holds four records, two with p null, one without p, and one with p = 1, each written in its own transaction. Through UniqueIdx (LSM_TREE) and UniqueHashIdx the query returns the last one written.

Where (at 1addb51950): FetchFromIndexStep.processIsNullCondition answers IS NULL with index.get(new Object[keyCount]), the point lookup of a key. On a unique index that lookup returns one entry for the null key (the repro prints index.get([null]) returning 1 entry on both unique indexes and 3 on the NOTUNIQUE one), although three records have the null key.

Repro

# lib/ holds the engine jar built at the commit under test and its dependencies
javac -cp 'lib/*' -d cls UniqueNullIsNull.java
java -Xmx2g -XX:+UseCompactObjectHeaders --add-modules=jdk.incubator.vector --enable-native-access=ALL-UNNAMED -cp 'lib/*:cls' UniqueNullIsNull ./db
import com.arcadedb.database.*;
import com.arcadedb.index.*;
import com.arcadedb.query.sql.executor.*;

import java.util.*;

public class UniqueNullIsNull {
  public static void main(String[] args) {
    System.out.println("ArcadeDB " + com.arcadedb.Constants.getRawVersion() + " (" + com.arcadedb.Constants.getBuildNumber() + "), Java "
        + System.getProperty("java.version"));
    try (Database db = new DatabaseFactory(args[0]).create()) {
      final String[][] types = { { "Scan", null }, { "NotUniqueIdx", "NOTUNIQUE" }, { "UniqueIdx", "UNIQUE" }, { "UniqueHashIdx", "UNIQUE_HASH" } };
      for (String[] t : types) {
        db.command("sql", "CREATE VERTEX TYPE " + t[0]);
        db.command("sql", "CREATE PROPERTY " + t[0] + ".id INTEGER");
        db.command("sql", "CREATE PROPERTY " + t[0] + ".p INTEGER");
        if (t[1] != null)
          db.command("sql", "CREATE INDEX ON " + t[0] + " (p) " + t[1] + " NULL_STRATEGY INDEX");
        db.transaction(() -> db.newVertex(t[0]).set("id", 1, "p", 1).save());
        db.transaction(() -> db.newVertex(t[0]).set("id", 2, "p", null).save());
        db.transaction(() -> db.newVertex(t[0]).set("id", 3).save()); // p not set
        db.transaction(() -> db.newVertex(t[0]).set("id", 4, "p", null).save());
      }
      System.out.println("Each type holds (id, p) = (1, 1), (2, null), (3, not set), (4, null), one transaction per record");
      for (String q : new String[] { "SELECT id FROM %s WHERE p IS NULL", "SELECT id FROM %s WHERE p IS NULL AND id > 0",
          "SELECT id FROM %s WHERE p <=> null", "SELECT count(*) AS id FROM %s WHERE p IS NULL", "MATCH (n:%s) WHERE n.p IS NULL RETURN n.id AS id" }) {
        final StringBuilder sb = new StringBuilder();
        for (String[] t : types)
          sb.append(String.format("%s %-12s ", t[0], ids(db, q.startsWith("MATCH") ? "cypher" : "sql", String.format(q, t[0]))));
        System.out.printf("  %-46s %s%n", q.replace("%s", "T"), sb);
      }
      for (String[] t : types)
        if (t[1] != null) {
          final TypeIndex index = db.getSchema().getType(t[0]).getIndexByProperties("p");
          int entries = 0;
          for (IndexCursor c = index.get(new Object[] { null }); c.hasNext(); c.next())
            entries++;
          try (ResultSet rs = db.query("sql", "EXPLAIN SELECT id FROM " + t[0] + " WHERE p IS NULL")) {
            System.out.printf("  %-10s index.get([null]) returns %d entries; plan of p IS NULL: %s%n", t[0], entries,
                rs.next().<String>getProperty("executionPlanAsString").split("\n")[0].trim());
          }
        }
    }
  }

  static String ids(Database db, String language, String query) {
    try (ResultSet rs = db.query(language, query)) {
      final List<Long> ids = new ArrayList<>();
      rs.forEachRemaining(r -> ids.add(((Number) r.getProperty("id")).longValue()));
      Collections.sort(ids);
      return ids.toString();
    } catch (Exception e) {
      return e.getClass().getSimpleName();
    }
  }
}

Output on main 1addb51950 (the engine jar's build stamp is 1addb51950), Temurin 25.0.4 with -XX:+UseCompactObjectHeaders:

ArcadeDB 26.10.1-SNAPSHOT (1addb519505253cbad99d1744d1abd23972928a8), Java 25.0.4
Each type holds (id, p) = (1, 1), (2, null), (3, not set), (4, null), one transaction per record
  SELECT id FROM T WHERE p IS NULL               Scan [2, 3, 4]    NotUniqueIdx [2, 3, 4]    UniqueIdx [4]          UniqueHashIdx [4]          
  SELECT id FROM T WHERE p IS NULL AND id > 0    Scan [2, 3, 4]    NotUniqueIdx [2, 3, 4]    UniqueIdx [4]          UniqueHashIdx [4]          
  SELECT id FROM T WHERE p <=> null              Scan [2, 3, 4]    NotUniqueIdx [2, 3, 4]    UniqueIdx [2, 3, 4]    UniqueHashIdx [2, 3, 4]    
  SELECT count(*) AS id FROM T WHERE p IS NULL   Scan [3]          NotUniqueIdx [3]          UniqueIdx [1]          UniqueHashIdx [1]          
  MATCH (n:T) WHERE n.p IS NULL RETURN n.id AS id Scan [2, 3, 4]    NotUniqueIdx [2, 3, 4]    UniqueIdx [2, 3, 4]    UniqueHashIdx [2, 3, 4]    
  NotUniqueIdx index.get([null]) returns 3 entries; plan of p IS NULL: + FETCH FROM INDEX NotUniqueIdx[p] ()
  UniqueIdx  index.get([null]) returns 1 entries; plan of p IS NULL: + FETCH FROM INDEX UniqueIdx[p] ()
  UniqueHashIdx index.get([null]) returns 1 entries; plan of p IS NULL: + FETCH FROM INDEX UniqueHashIdx[p] ()

Temurin 21.0.12 printed the same lines apart from the Java version on the first line.

Versions affected: main 417314c18d (2026-09-17) printed the same lines apart from the first line, on Temurin 25 and 21.

Expected: p IS NULL returns every record whose p is null or missing (2, 3, and 4) through the unique indexes as it does on Scan and NotUniqueIdx, and count(*) counts 3. Either the null-key lookup of a unique index returns every entry of the null key, or IS NULL on a unique index reads the null key's entries as a range.

Duplicates checked: gh search issues --repo ArcadeData/arcadedb for "UNIQUE null", "unique index null keys", "unique NULL_STRATEGY INDEX", "IS NULL index", and "NULL_STRATEGY". Near misses: #2453 (closed: IS NULL returned nothing through a NOTUNIQUE index with the default null strategy), #9032 (closed: IN with a null element through an index), #9074 (open: a HASH index with NULL_STRATEGY ERROR stores null keys), and the equality with a null parameter (#9238). I found no issue for IS NULL through a unique index.

Activity

  1. added
    engine
    severity:minor
    on Oct 4, 2026
  2. added
    in progress
    on Oct 4, 2026
  3. self-assigned this
    on Oct 4, 2026
  4. added this to the 26.10.1 milestone on Oct 4, 2026
  5. added a commit that references this issue on Oct 5, 2026
  6. lvca commented on Oct 5, 2026

    @lvca
    Member

    Fixed in #9252 (26.10.1). p IS NULL through a UNIQUE or UNIQUE_HASH index with NULL_STRATEGY INDEX now returns every record with a null key, and count(*) counts them all. Related fixes: deleting one null-keyed record no longer drops the index entries of the others, and several null-keyed records added in one transaction are all kept.

    ❤️ 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. removed
    in progress
    on Oct 5, 2026
  8. added a commit that references this issue on Oct 5, 2026
  9. tae898 commented on Oct 5, 2026

    @tae898
    ContributorAuthor

    Checked on upstream main 3543960 (the commit that contains #9252, #9250 and #9251; engine jars built by CI from that commit, no ha-raft shaded jar on the classpath) against 9fbef12 (main before those three merges). The repro is the one filed in this issue, unchanged. One JVM per run, in a container limited to 2 cores and 2 GB (-Xmx1g), on Temurin 25.0.4 with -XX:+UseCompactObjectHeaders and on Temurin 21.0.12.

    Verdict: fixed on both JDKs. WHERE p IS NULL through the UNIQUE index and through the UNIQUE_HASH index returns all three null-keyed records, count(*) counts three, and index.get([null]) returns three entries on every kind of index, as the filed repro prints.

    query (4 records: p = 1, null, not set, null) scan UniqueIdx before UniqueIdx after UniqueHashIdx before UniqueHashIdx after
    SELECT id FROM T WHERE p IS NULL [2, 3, 4] [4] [2, 3, 4] [2] [2, 3, 4]
    SELECT id FROM T WHERE p IS NULL AND id > 0 [2, 3, 4] [4] [2, 3, 4] [2] [2, 3, 4]
    SELECT id FROM T WHERE p <=> null [2, 3, 4] [2, 3, 4] [2, 3, 4] [2, 3, 4] [2, 3, 4]
    SELECT count(*) AS id FROM T WHERE p IS NULL [3] [1] [3] [1] [3]
    index.get([null]) entries (3 records have a null key) before after
    NotUniqueIdx 3 3
    UniqueIdx 1 3
    UniqueHashIdx 1 3

    The other things the pull request says, measured with six and then nine records on a UNIQUE and a UNIQUE_HASH index with NULL_STRATEGY INDEX: deleting one null-keyed record keeps the others (on UNIQUE_HASH it returned none before), updating one null-keyed record to a value keeps the others, three records added with p not set in one transaction are all found, the null-keyed records are all there after a close and reopen, and a second record with p = 1 is still refused with DuplicatedKeyException. All 16 cases are right after the fix on both JDKs (0 of 16 wrong, against 12 of 16 wrong before).

    case expected UNIQUE before UNIQUE after UNIQUE_HASH before UNIQUE_HASH after
    six records, ids 2..6 null: p IS NULL ids [2, 3, 4, 5, 6] WRONG [6] ok WRONG [2] ok
    six records: count(*) WHERE p IS NULL [5] WRONG [1] ok WRONG [1] ok
    after deleting id 3 (null-keyed): p IS NULL ids [2, 4, 5, 6] WRONG [6] ok WRONG [] ok
    after UPDATE id 4 SET p = 7: p IS NULL ids [2, 5, 6] WRONG [6] ok WRONG [] ok
    after UPDATE id 4 SET p = 7: p = 7 ids [4] ok ok ok ok
    three more records (p not set) in one transaction: p IS NULL ids [10, 11, 12, 2, 5, 6] WRONG [12] ok WRONG [12] ok
    second record with p = 1 (uniqueness of a non-null key) DuplicatedKeyException ok ok ok ok
    after close and reopen: p IS NULL ids [10, 11, 12, 2, 5, 6] WRONG [12] ok WRONG [12] ok

    Thanks for the quick fix.

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

Metadata

Metadata

Assignees

Labels

bugengineseverity:minorsql

Type

No type

Projects

No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions