Skip to content

SQL: - between a DATETIME and a null or missing field throws NullPointerException #8232

Closed

Description

@tae898

Summary

In SQL, - between a DATETIME and a null throws a NullPointerException out of the evaluator, in either direction: dt - nosuch, dt - null, nosuch - dt, null - dt. The same null is tolerated everywhere next to it: dt + nosuch returns dt, and n - nosuch on a LONG returns n. Only datetime subtraction crashes, and it takes the whole query down.

The everyday trigger is a missing field. In a schemaless document an optional property is simply absent, so SELECT dt - someOptionalField FROM ... fails as soon as one record lacks it.

Reproduction

try (DatabaseFactory f = new DatabaseFactory("/tmp/dtnull")) {
  Database db = f.create();
  db.command("sql", "CREATE DOCUMENT TYPE D");
  db.command("sql", "CREATE PROPERTY D.dt DATETIME");
  db.command("sql", "CREATE PROPERTY D.n LONG");
  db.begin(); db.command("sql", "INSERT INTO D SET dt = sysdate(), n = 5"); db.commit();
  db.query("sql", "SELECT dt - nosuch AS x FROM D").next();   // NullPointerException
  db.close();
}
query result on main at cbd0947c18
SELECT n - nosuch AS x FROM D 5
SELECT n - null AS x FROM D 5
SELECT dt + nosuch AS x FROM D the datetime, unchanged
SELECT dt - nosuch AS x FROM D NullPointerException: Cannot invoke "java.lang.Long.longValue()" because "right" is null
SELECT dt - null AS x FROM D NullPointerException, same
SELECT nosuch - dt AS x FROM D NullPointerException: ... because "left" is null
SELECT null - dt AS x FROM D NullPointerException, same
SELECT dt - 1000 AS x FROM D a Duration (control: the non-null path works)
java.lang.NullPointerException: Cannot invoke "java.lang.Long.longValue()" because "right" is null
  at com.arcadedb.query.sql.parser.MathExpression$Operator$5.apply(MathExpression.java:368)
  at com.arcadedb.query.sql.parser.MathExpression$Operator$5.apply(MathExpression.java:401)
  at com.arcadedb.query.sql.parser.MathExpression.execute(MathExpression.java:934)

Same on the published 26.8.1 (727aa4568c), so it is not a recent regression.

Cause

PLUS's generic apply(Object, Object) handles a null operand of any type first (lines 308-313: if either side is null, return the other). MINUS's only handles it when the other side is a number (lines 391-394: left instanceof Number && right == null, and the mirror). A datetime with a null therefore reaches the date branch (line 397), where DateUtils.dateTimeToTimestamp(null, ...) returns null and apply(Long, Long) unboxes it.

Suggested fix

Guard the date branch the way PLUS guards everything:

         } else if (DateUtils.isDate(left) || DateUtils.isDate(right)) {
-          final ChronoUnit highestPrecision = DateUtils.getHigherPrecision(left, right);
-          final Long leftAsLong = DateUtils.dateTimeToTimestamp(left, highestPrecision);
-          final Long rightAsLong = DateUtils.dateTimeToTimestamp(right, highestPrecision);
-          final Number r = apply(leftAsLong, rightAsLong);
-          result = Duration.of(r.longValue(), highestPrecision);
+          if (left == null || right == null) {
+            // date - null = date, as date + null and number - null already behave;
+            // null - date = null
+            result = left;
+          } else {
+            final ChronoUnit highestPrecision = DateUtils.getHigherPrecision(left, right);
+            final Long leftAsLong = DateUtils.dateTimeToTimestamp(left, highestPrecision);
+            final Long rightAsLong = DateUtils.dateTimeToTimestamp(right, highestPrecision);
+            final Number r = apply(leftAsLong, rightAsLong);
+            result = Duration.of(r.longValue(), highestPrecision);
+          }

result = left gives date - null = date, matching date + null and number - null, and null - date = null, since there is no meaningful negative of a date (the numeric path's null - n = -n has no datetime equivalent).

I checked it by compiling the patched MathExpression against the cbd0947c18 jars and putting it ahead of the engine jar on the classpath: all four crashing queries return (dt, dt, null, null), and n - nosuch, n - null, dt + nosuch, dt - 1000 and dt % 1000 return exactly what they did before. I have not run the engine's own test suite against it. If you would rather have SQL-style null propagation (dt - null = null), result = null in the same place does that, but it would then disagree with dt + null and n - null, which is why I went with left.

Related: how I hit it, on TIMESERIES types

On a native TIMESERIES type the timestamp column reads back as a LocalDateTime, so the common bucketing idiom from other SQL time-series engines, SELECT (ts - ts % 3600000) AS h ... GROUP BY h, crashes the same way: ts % 3600000 on a datetime silently yields null, and ts - null throws. The fix above removes the crash, but that query still will not bucket (it returns ts), because the % quietly produced null. ts.timeBucket('1h', ts) is the right way to write it on ArcadeDB and works. A clear error for % on a datetime, perhaps pointing at timeBucket, would save the next person the hunt; happy to open that separately if you think it is worth it.

Environment

Embedded engine API on Linux x86_64, run in the eclipse-temurin:21-jdk (21.0.12) and eclipse-temurin:25-jdk (25.0.4) images: the same exception and trace on both. Engine jars from each build's published image.

Activity

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

    @lvca
    Member

    Fixed in #8262 (merged into main, will ship in 26.10.1).

    Subtracting a null or missing field from a DATETIME (e.g. dt - someOptionalField) was throwing a NullPointerException and failing the whole query. Now date - null simply returns the date unchanged, the same as date + null already did.

    A regression test was added so this stays fixed.


    ❤️ 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 Sep 23, 2026
  8. added a commit that references this issue on Sep 23, 2026
  9. tae898 commented on Sep 23, 2026

    @tae898
    ContributorAuthor

    Verified with the repro from the issue on main at f803c14f98 (the published 26.10.1-SNAPSHOT image, which contains #8262), against cbd0947c18, the build it was filed on. Temurin 21.0.12, and Temurin 25.0.4 with -XX:+UseCompactObjectHeaders, with the same results on both:

    query cbd0947c18 f803c14f98
    SELECT dt - nosuch NullPointerException the datetime
    SELECT dt - null NullPointerException the datetime
    SELECT nosuch - dt NullPointerException null
    SELECT null - dt NullPointerException null
    n - nosuch, n - null, dt % 1000, dt + nosuch, dt - 1000 5, 5, null, the datetime, a Duration unchanged

    The Python bindings' test suite passes on a wheel built from this engine (467 passed, 2 skipped, Linux x86_64). The fixes verified earlier today on this issue batch's neighbours still hold on f803c14f98 too: #8129's txWalFlush=2 cost is back with a vector index (4.5x), and #8179's last point stays faster than the document type (0.63x).

    Thanks for the quick fix.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

bugseverity:majorsql

Type

No type

Projects

No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions