GeezerVison is showing github.com — Follow-up to #9238: a statement first run with a value keeps its index plan, so a later run with null returns the null-key records again, and a DELETE removes them · Issue #9274 · ArcadeData/arcadedb
Follow-up to #9238: a statement first run with a value keeps its index plan, so a later run with null returns the null-key records again, and a DELETE removes them #9274DescriptionSummary
#9246 (the fix for #9238) makes an equality whose value is null match no record through an index, by deciding at planning time that the index cannot answer it. A statement that has already run with a non-null value keeps its index plan, and the next run of the same statement text with null reuses that plan, so the null-key records come back again: the bug of #9238 returns for any statement that was first run with a value. A DELETE does the same and removes the null-key records, so data is lost.
On 111aa40457 (the merge of #9246), Temurin 25.0.4 with -XX:+UseCompactObjectHeaders and Temurin 21.0.12, the output of the repro below is identical on both JDKs:
SELECT p = ? with 1, null, 1, null: [1] [2, 3] [1] [2, 3]
records left after DELETE with 99 then null (3 expected): 3
records left after DELETE (no id filter) with 99 then null (3 expected): 1
Expected [1] [] [1] [] and 3 records left each time. The first run of the same statement with null is correct ([]); only a run after a non-null value is wrong. With -Darcadedb.sqlStatementCache=0 the output is [1] [] [1] [] and 3, 3 (measured on Temurin 25), and on 354396071e, before #9246, it is [1] [2, 3] [1] [2, 3] and 3, 1, which is the original #9238.
BinaryCondition.isIndexAware returns vl != null for the equality (L259) and right.execute((Result) null, info.getContext()) != null for the other form (L277): the decision depends on the parameter value of the run that plans the statement. The plan is then cached by statement text and reused by a run with another value, as the measurements above show (the cache off gives the right answers).
Repro
Run with the engine jar and its dependencies on the classpath, as java CachedPlanNull.java <empty directory>.
import com.arcadedb.database.Database;
import com.arcadedb.database.DatabaseFactory;
import com.arcadedb.query.sql.executor.ResultSet;
import java.util.ArrayList;
import java.util.List;
/**
* After PR #9246 (#9238): a statement first run with a value keeps a plan through the index, and its next run with null reuses it.
* Usage: java -cp "<engine jars>/*" CachedPlanNull.java <empty-dir>
*/
public class CachedPlanNull {
static List<Integer> ids(final Database db, final Object value) {
final List<Integer> ids = new ArrayList<>();
try (final ResultSet rs = db.query("sql", "SELECT id FROM T WHERE p = ?", value)) {
rs.forEachRemaining(r -> ids.add(r.getProperty("id")));
}
java.util.Collections.sort(ids);
return ids;
}
public static void main(final String[] args) {
try (final DatabaseFactory factory = new DatabaseFactory(args[0] + "/db")) {
try (final Database db = factory.create()) {
db.command("sql", "CREATE DOCUMENT TYPE T");
db.command("sql", "CREATE PROPERTY T.id INTEGER");
db.command("sql", "CREATE PROPERTY T.p INTEGER");
db.command("sql", "CREATE INDEX ON T (p) NOTUNIQUE NULL_STRATEGY INDEX");
db.transaction(() -> {
db.newDocument("T").set("id", 1, "p", 1).save();
db.newDocument("T").set("id", 2, "p", null).save();
db.newDocument("T").set("id", 3).save();
});
// expected [1], [], [1], []
System.out.println("SELECT p = ? with 1, null, 1, null: " + ids(db, 1) + " " + ids(db, (Object) null) + " " + ids(db, 1) + " "
+ ids(db, (Object) null));
// expected: nothing deleted (3 records stay)
db.transaction(() -> db.command("sql", "DELETE FROM T WHERE p = ? AND id > 100", 99));
db.transaction(() -> db.command("sql", "DELETE FROM T WHERE p = ? AND id > 100", (Object) null));
System.out.println("records left after DELETE with 99 then null (3 expected): " + db.countType("T", false));
db.transaction(() -> db.command("sql", "DELETE FROM T WHERE p = ?", 99));
db.transaction(() -> db.command("sql", "DELETE FROM T WHERE p = ?", (Object) null));
System.out.println("records left after DELETE (no id filter) with 99 then null (3 expected): " + db.countType("T", false));
db.drop();
}
}
}
}
Expected
A null equality matches no record on every run of a statement, whatever value the statement was run with before: the plan must not depend on the value of the run that happened to plan it, or the value test must move to execution (the index step skipping the seek when its equality value is null, as it already does for a null range bound and a null IN element).
Near misses
#9238 and PR #9246 (this is a hole in that fix). #9245 and #9247 (the SQL cache and parameter numbers: another way a cached plan is reused for a statement it does not fit, fixed in #9252 and #9251). #9032 (the same lookup for a null IN element). Activityadded 7 commits that reference this issue on Oct 5, 2026review(#9274): cover CI, unique, null literal and range-side null; ti… review(#9274): rename parameter, wrap Javadoc, cover null-first and I… review(#9274): cover count(*) and null first slot, drop redundant reload review(#9274): null-key fixtures for composite and unique tests, asse… lvca commented on Oct 5, 2026 Fixed in #9276 (merged).
Confirmed: a statement first run with a value kept its index plan, and a later run of the same text with a null parameter reused it, returning (and for DELETE, removing) the records whose key is null. The null check was made when the plan was built; it is now made on every execution, so a null equality matches no record whatever the statement was run with before. Regression tests cover SELECT, DELETE, composite, case-insensitive and unique indexes.
This restores the intended behavior of #9238, so no documentation change is needed.
❤️ 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. tae898 commented on Oct 5, 2026 ContributorAuthor More actions Verified the fix of #9276 on upstream main d36b4ca (CI-built jars, no HA-raft shaded jar), against 111aa40, where this was filed. Temurin 25.0.4 with compact object headers and Temurin 21.0.12 give identical output.
The statement text is run with a value, then null, then the value again. Expected [1] [] [1]:
| index and parameter form |
before |
after |
NOTUNIQUE, positional (value, null, value, null) |
[1] [2, 3] [1] [2, 3] |
[1] [] [1] [] |
| NOTUNIQUE index, named parameter |
[1] [2, 3] [1] |
[1] [] [1] |
| NOTUNIQUE_HASH index, positional |
[1] [2, 3] [1] |
[1] [] [1] |
| UNIQUE index, positional |
[1] [2, 3] [1] |
[1] [] [1] |
| NULL_STRATEGY ERROR index, named |
[1] raised IllegalArgumentException: Indexed key T[p] cannot be NULL ([null]) [1] |
[1] [] [1] |
| composite (q, p), named |
[1] [] [1] |
[1] [] [1] |
The unfiltered DELETE FROM T WHERE p = ? run with 99 and then with null leaves 1 of 3 records before and 3 of 3 after.
The hash index, NULL_STRATEGY ERROR (which no longer raises on the second run), and the named-parameter form are not in your list of regression tests, and they now answer like the others. The composite index, which was not affected before, still answers correctly.
Thank you for the quick fix. added 7 commits that reference this issue on Oct 6, 2026fix(#9274): decide a null equality at execution, so a cached index pl… review(#9274): cover CI, unique, null literal and range-side null; ti… review(#9274): rename parameter, wrap Javadoc, cover null-first and I… review(#9274): cover count(*) and null first slot, drop redundant reload review(#9274): null-key fixtures for composite and unique tests, asse…
MetadataMetadataDevelopmentNo branches or pull requestsIssue actions
|