Repository navigation
Description
Target client repo
ClickHouse/clickhouse-java (module: client-v2)
Severity
sev:1 — Visible error (NPE), not silent corruption. Bug only triggers when a user passes null into a non-nullable Enum8/Enum16 column, which is itself a schema/usage mismatch; the workaround is to either declare the column as Nullable(Enum8(...)) (which client-v2 handles correctly) or to filter null values client-side before insert. The bug is purely a confusing error message rather than data loss, so per the rubric (easy workaround) it is sev:1.
Description
In client-v2, when serializing a row to RowBinary, the null-handling fast path only fires for nullable columns:
// SerializerUtils.java:495
if (value == null && column.isNullable()) {
BinaryStreamUtils.writeNull(stream);
return;
}For a non-nullable Enum8/Enum16 column, execution falls through to serializeEnumData(stream, column, value):
// SerializerUtils.java:689-708
public static void serializeEnumData(OutputStream stream, ClickHouseColumn column, Object value) throws IOException {
int enumValue = -1;
if (value instanceof String) {
enumValue = column.getEnumConstants().value((String) value);
} else if (value instanceof Number) {
enumValue = ((Number) value).intValue();
} else if (value instanceof Enum<?>) {
enumValue = ((Enum<?>)value).ordinal();
} else {
throw new IllegalArgumentException("Cannot write value of class " + value.getClass() + " into column with Enum type " + column.getOriginalTypeName());
}
...
}When value is null, all three instanceof checks return false, so control reaches the else branch — which dereferences value.getClass() and throws NullPointerException instead of the intended IllegalArgumentException with a clear message.
This is the Java analogue of the original Python clickhouse-connect bug, where [int(x) for x in column] blows up with a confusing TypeError: int() argument must be a string, a bytes-like object or a number, not 'NoneType'. In both clients, a null/None in a non-nullable Enum column surfaces as a low-level type-system error rather than a clear domain error.
For Nullable(Enum8) columns the Java client behaves correctly (the early-return at line 495 fires), so this only affects the non-nullable case.
ClickHouse server version
Code analysis only; not verified against a running server.
Reproduction
A focused unit test against client-v2:
import com.clickhouse.client.api.data_formats.internal.SerializerUtils;
import com.clickhouse.data.ClickHouseColumn;
import org.testng.annotations.Test;
import java.io.ByteArrayOutputStream;
import java.util.Collections;
import java.util.HashMap;
import java.util.Map;
public class EnumNullSerializationTest {
@Test
public void nullIntoNonNullableEnumGivesClearError() throws Exception {
ClickHouseColumn col = ClickHouseColumn.parse("bs_flag Enum8('B' = 1, 'S' = 2)").get(0);
Map<String, Object> row = new HashMap<>();
row.put("bs_flag", null);
ByteArrayOutputStream buf = new ByteArrayOutputStream();
// Triggers SerializerUtils.serializePrimitiveData -> serializeEnumData(null)
SerializerUtils.serializeData(buf, row.get("bs_flag"), col);
// Expected: IllegalArgumentException / clear "cannot insert null into non-nullable Enum" message.
// Actual: NullPointerException from `value.getClass()` at SerializerUtils.java:698
}
}Equivalent end-to-end reproduction with the high-level insert API (illustrative; assumes a non-nullable Enum8 column):
Client client = new Client.Builder().addEndpoint(...).build();
client.execute("CREATE TABLE t (bs_flag Enum8('B' = 1, 'S' = 2)) ENGINE = Memory").get();
List<Map<String, Object>> rows = new ArrayList<>();
Map<String, Object> r = new HashMap<>();
r.put("bs_flag", null);
rows.add(r);
// Throws NullPointerException from SerializerUtils.serializeEnumData
client.insert("t", rows, new InsertSettings()).get();Expected: either a clear IllegalArgumentException ("Cannot insert null into non-nullable Enum column 'bs_flag'") or, if the project wants Python-style permissive behavior, treat null as the column default.
Actual: NullPointerException from value.getClass() deep inside SerializerUtils#serializeEnumData.
Suggested fix
In client-v2/src/main/java/com/clickhouse/client/api/data_formats/internal/SerializerUtils.java:
-
In
serializePrimitiveData(around line 495), after thevalue == null && column.isNullable()early-return, add a second branch that raises a clear error whenvalue == nulland the column is not nullable — ideally before dispatching oncolumn.getDataType(). This would fix the symptom for all non-nullable types uniformly, not just Enum. -
Alternatively, in
serializeEnumData(line 698), guard againstvalue == nullbefore callingvalue.getClass():
} else {
if (value == null) {
throw new IllegalArgumentException("Cannot write null into non-nullable Enum column " + column.getColumnName());
}
throw new IllegalArgumentException("Cannot write value of class " + value.getClass() + " into column with Enum type " + column.getOriginalTypeName());
}The same null-vs-non-nullable improvement would benefit convertToInteger, convertToLong, and similar primitives, all of which currently fail with instanceof-cascade IllegalArgumentExceptions that include a literal null in the message rather than a column-aware error.
Dedup searches run
gh issue list --repo ClickHouse/clickhouse-java --search \"null enum\" --state all --limit 20→ 8 hits, none about null/Enum serialization.gh pr list --repo ClickHouse/clickhouse-java --search \"null enum\" --state all --limit 20→ 10 hits, none about null/Enum serialization (closest is Implement BinaryStreamReader.EnumValue.toString() #2209 "Implement BinaryStreamReader.EnumValue.toString()" which is on the read path).gh issue list --repo ClickHouse/integrations-ai-playground --label backfill --label \"target:clickhouse-java\" --search \"null enum\" --state all --limit 20→ 0 hits.gh issue list --repo ClickHouse/integrations-ai-playground --label backfill --label \"target:clickhouse-java\" --search \"enum\" --state all --limit 20→ 0 hits.gh pr list --repo ClickHouse/integrations-ai-playground --search \"null enum\" --state all --limit 20→ 0 hits.
Source bug
Relayed from clickhouse-connect: ClickHouse/clickhouse-connect#256
Activity
Clarify non-nullable Enum null error and add integration + JDBC tests
fix(client-v2): reject null in non-nullable Enum with clear error