Skip to content

Commit 8601c02

Browse files
authored
Merge pull request #2149 from ClickHouse/resolve-null-dtos
Send null when nullable
2 parents eb27cad + 3ccfeb0 commit 8601c02

File tree

2 files changed

+19
-2
lines changed

2 files changed

+19
-2
lines changed

client-v2/src/main/java/com/clickhouse/client/api/data_formats/internal/SerializerUtils.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -462,6 +462,12 @@ private static void serializeMapData(OutputStream stream, Object value, ClickHou
462462
}
463463

464464
private static void serializePrimitiveData(OutputStream stream, Object value, ClickHouseColumn column) throws IOException {
465+
//Handle null values
466+
if (value == null && column.isNullable()) {//Only nullable columns can have null values
467+
BinaryStreamUtils.writeNull(stream);
468+
return;
469+
}
470+
465471
//Serialize the value to the stream based on the type
466472
switch (column.getDataType()) {
467473
case Int8:

client-v2/src/test/java/com/clickhouse/client/insert/SamplePOJO.java

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ public class SamplePOJO {
2222
private byte byteValue;
2323
private Byte boxedByte;
2424
private int int8;
25+
private Integer int8_null;
2526
private int int8_default;
2627
private int int16;
2728
private Short boxedShort;
@@ -100,6 +101,7 @@ public SamplePOJO() {
100101
byteValue = (byte) random.nextInt();
101102
boxedByte = (byte) random.nextInt();
102103
int8 = random.nextInt(128);
104+
int8_null = null;
103105
int16 = random.nextInt(32768);
104106
boxedShort = (short) random.nextInt();
105107
int32 = random.nextInt();
@@ -175,7 +177,7 @@ public SamplePOJO() {
175177
}
176178

177179
array = Arrays.asList("a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m", "n", "o", "p", "q", "r", "s", "t", "u", "v", "w", "x", "y", "z");
178-
tuple = Arrays.asList(uint64, int32, string);
180+
tuple = Arrays.asList(uint64, int32, string, null);
179181
map = new HashMap<>();
180182
for (int i = 0; i < 10; i++) {
181183
map.put(String.valueOf((char) ('a' + i)), i + 1);
@@ -278,6 +280,14 @@ public void setInt8(int int8) {
278280
this.int8 = int8;
279281
}
280282

283+
public Integer getInt8_null() {
284+
return int8_null;
285+
}
286+
287+
public void setInt8_null(Integer int8_null) {
288+
this.int8_null = int8_null;
289+
}
290+
281291
public int getInt8Default() {
282292
return int8_default;
283293
}
@@ -721,6 +731,7 @@ public static String generateTableCreateSQL(String tableName) {
721731
return "CREATE TABLE " + tableName + " (" +
722732
"byteValue Int8," +
723733
"int8 Int8, " +
734+
"int8_null Nullable(Int8), " +
724735
"boxedByte Int8, " +
725736
"int8_default Int8 DEFAULT 0, " +
726737
"int16 Int16, " +
@@ -770,7 +781,7 @@ public static String generateTableCreateSQL(String tableName) {
770781
"ipv4 IPv4, " +
771782
"ipv6 IPv6, " +
772783
"array Array(String), " +
773-
"tuple Tuple(UInt64, Int32, String), " +
784+
"tuple Tuple(UInt64, Int32, String, Nullable(Int16)), " +
774785
"map Map(String, Int32), " +
775786
"nested Nested (innerInt Int32, innerString String, " +
776787
"innerNullableInt Nullable(Int32)), " +

0 commit comments

Comments
 (0)