You are viewing a plain text version of this content. The canonical link for it is here.
Posted to pr@cassandra.apache.org by "dipAch (via GitHub)" <gi...@apache.org> on 2026/10/12 21:10:12 UTC

[PR] [CASSANDRA-21743] Fix SELECT JSON NullPointerException and related empty-value handling for boolean, tinyint, and time columns [cassandra]

dipAch opened a new pull request, #5253:
URL: https://github.com/apache/cassandra/pull/5253

   ## Summary
   
   `BooleanType#toJSONString()` and `ByteType#toJSONString()` (tinyint) call `TypeSerializer#deserialize(buffer).toString()` directly with no guard against `deserialize()` returning `null` for a zero-length buffer, causing a `NullPointerException` from `SELECT JSON` on a column holding an empty (not null) value - exactly the scenario reported in the ticket. `TimeType#toJSONString()` has the same gap but doesn't crash, since `TimeSerializer#toString(null)` returns the literal string `"null"` instead of throwing, producing wrong JSON output instead.
   
   Confirmed via an exhaustive empirical audit across every `AbstractType`'s `toJSONString()` with a genuinely empty buffer, rather than relying on the ticket's own (partially incorrect) guesses about affected types: the ticket's `varint` suspicion turned out to already be fixed, while `tinyint` was correctly suspected but needed more than just this one fix, and `time` wasn't mentioned at all. `CounterColumnType` and the legacy `DateType` have the same crash at the Java API level but are confirmed **not reachable** from any real CQL statement (counters reject direct assignment; no CQL type maps to legacy `DateType`), so they're intentionally left alone.
   
   ## Fix
   
   - `BooleanType`/`ByteType#toJSONString()`: return `"\"\""` for a null/empty deserialized value instead of calling `.toString()` on it directly - matching the existing pattern already used by `Int32Type`/`LongType`/`ShortType`/etc.
   - `TimeType#toJSONString()`: same null-guard, applied before calling the serializer's `toString()`.
   
   Fixing `tinyint` and `time` surfaced two further, related gaps, also fixed here:
   - `ByteSerializer`/`TimeSerializer#validate()` unconditionally rejected a zero-length buffer (unlike `Int32Serializer`, which already allows one), which blocked even a plain bind-variable `INSERT` of an empty value for these two types.
   - `TimeType#fromString()` had no empty-string guard at all (unlike `Int32Type`/`ByteType`), so `INSERT ... JSON` with an empty string failed outright instead of round-tripping.
   - `ByteType`/`TimeType` gained `isEmptyValueMeaningless()=true` overrides (matching `Int32Type` et al.) - separately needed because `EmptyValuesTest`'s existing `testEmptyTime` relies on that flag and had been silently skipped by it the whole time, never actually exercising `TimeType`'s empty-value handling.
   
   `BooleanType#fromString("")` is left untouched - it intentionally treats an empty string as a synonym for `false`, a separate, pre-existing, deliberate feature unrelated to this bug.
   
   This bug and all the code touched here is byte-for-byte identical on `cassandra-4.0`, `cassandra-4.1`, and `cassandra-5.0`, so this targets the lowest active branch per convention, to flow forward through the normal merge chain.
   
   ## Test plan
   
   - [x] New `testEmptyBoolean`/`testEmptyByte` tests added to `EmptyValuesTest`; existing `testEmptyTime` now genuinely runs (previously silently skipped) and passes.
   - [x] Reproduced the exact ticket scenario live against unfixed code (`CREATE TABLE`, `INSERT ... blobAsBoolean(0x)`, `SELECT JSON` crashing with the reported NPE), confirmed the fix resolves it.
   - [x] `EmptyValuesTest` (17/17, 4 pre-existing unrelated skips), `TimeSerializerTest`, `TimeTypeTest`, `JsonTest`, `JsonConversionTest` all pass.
   - [x] `ant check` (checkstyle + RAT) passes clean.
   
   CASSANDRA-21743


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: pr-unsubscribe@cassandra.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: pr-unsubscribe@cassandra.apache.org
For additional commands, e-mail: pr-help@cassandra.apache.org


Re: [PR] [CASSANDRA-21743] Fix SELECT JSON NullPointerException and related empty-value handling for boolean, tinyint, and time columns [cassandra]

Posted by "gandherva (via GitHub)" <gi...@apache.org>.
gandherva commented on code in PR #5253:
URL: https://github.com/apache/cassandra/pull/5253#discussion_r4266031729


##########
src/java/org/apache/cassandra/serializers/ByteSerializer.java:
##########
@@ -39,8 +39,8 @@ public ByteBuffer serialize(Byte value)
 
     public <V> void validate(V value, ValueAccessor<V> accessor) throws MarshalException
     {
-        if (accessor.size(value) != 1)
-            throw new MarshalException(String.format("Expected 1 byte for a tinyint (%d)", accessor.size(value)));
+        if (accessor.size(value) != 1 && !accessor.isEmpty(value))

Review Comment:
   Can we add tests for this one and fix existing tests if required ? 



##########
src/java/org/apache/cassandra/serializers/TimeSerializer.java:
##########
@@ -70,8 +70,8 @@ public static Long timeStringToLong(String source) throws MarshalException
 
     public <V> void validate(V value, ValueAccessor<V> accessor) throws MarshalException
     {
-        if (accessor.size(value) != 8)
-            throw new MarshalException(String.format("Expected 8 byte long for time (%d)", accessor.size(value)));
+        if (accessor.size(value) != 8 && !accessor.isEmpty(value))

Review Comment:
   Can we add tests for this one and fix existing tests if required ? 



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: pr-unsubscribe@cassandra.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: pr-unsubscribe@cassandra.apache.org
For additional commands, e-mail: pr-help@cassandra.apache.org