diff --git a/java/openmldb-common/src/main/java/com/_4paradigm/openmldb/common/codec/ClassicRowBuilder.java b/java/openmldb-common/src/main/java/com/_4paradigm/openmldb/common/codec/ClassicRowBuilder.java index 15bb50d0471..ecdf0bb0ff9 100644 --- a/java/openmldb-common/src/main/java/com/_4paradigm/openmldb/common/codec/ClassicRowBuilder.java +++ b/java/openmldb-common/src/main/java/com/_4paradigm/openmldb/common/codec/ClassicRowBuilder.java @@ -205,6 +205,9 @@ public boolean appendTimestamp(Timestamp val) { if (!check(DataType.kTimestamp)) { return false; } + if (val == null) { + return appendNULL(); + } setField(cnt); buf.position(offsetVec.get(cnt)); buf.putLong(val.getTime()); @@ -250,6 +253,9 @@ public boolean appendDouble(double val) { @Override public boolean appendDate(Date date) { + if (date == null) { + return appendNULL(); + } int dateInt = CodecUtil.dateToDateInt(date); buf.position(offsetVec.get(cnt)); buf.putInt(dateInt); @@ -260,9 +266,12 @@ public boolean appendDate(Date date) { @Override public boolean appendString(String val) { + if (val == null) { + return appendNULL(); + } byte[] bytes = val.getBytes(CodecUtil.CHARSET); int length = bytes.length; - if (val == null || (!check(DataType.kVarchar) && !check(DataType.kString))) { + if (!check(DataType.kVarchar) && !check(DataType.kString)) { return false; } if (strOffset + length > size) { diff --git a/java/openmldb-common/src/main/java/com/_4paradigm/openmldb/common/codec/FlexibleRowBuilder.java b/java/openmldb-common/src/main/java/com/_4paradigm/openmldb/common/codec/FlexibleRowBuilder.java index e9029fb7663..f11f2a31a64 100644 --- a/java/openmldb-common/src/main/java/com/_4paradigm/openmldb/common/codec/FlexibleRowBuilder.java +++ b/java/openmldb-common/src/main/java/com/_4paradigm/openmldb/common/codec/FlexibleRowBuilder.java @@ -273,6 +273,9 @@ public boolean setTimestamp(int idx, Timestamp val) { if (!checkType(idx, Type.DataType.kTimestamp)) { return false; } + if (val == null) { + return setNULL(idx); + } settedValue.atPut(idx, true); baseFieldBuf.putLong(getOffset(idx), val.getTime()); return true; @@ -313,6 +316,9 @@ public boolean setDate(int idx, Date val) { if (!checkType(idx, Type.DataType.kDate)) { return false; } + if (val == null) { + return setNULL(idx); + } settedValue.atPut(idx, true); int dateVal = CodecUtil.dateToDateInt(val); baseFieldBuf.putInt(getOffset(idx), dateVal); @@ -324,6 +330,9 @@ public boolean setString(int idx, String val) { if (!checkType(idx, Type.DataType.kString) && !checkType(idx, Type.DataType.kVarchar)) { return false; } + if (val == null) { + return setNULL(idx); + } if (settedValue.at(idx)) { return false; } diff --git a/java/openmldb-common/src/test/java/com/_4paradigm/openmldb/common/RowCodecTest.java b/java/openmldb-common/src/test/java/com/_4paradigm/openmldb/common/RowCodecTest.java index ff1c51f06ea..c562cbd70ee 100644 --- a/java/openmldb-common/src/test/java/com/_4paradigm/openmldb/common/RowCodecTest.java +++ b/java/openmldb-common/src/test/java/com/_4paradigm/openmldb/common/RowCodecTest.java @@ -89,6 +89,38 @@ public void testNull(String builderName) { } } + @Test(dataProvider = "builder") + public void testValueNull(String builderName) { + try { + List schema = new ArrayList(); + schema.add(ColumnDesc.newBuilder().setName("col1").setDataType(DataType.kTimestamp).build()); + schema.add(ColumnDesc.newBuilder().setName("col2").setDataType(DataType.kDate).build()); + schema.add(ColumnDesc.newBuilder().setName("col3").setDataType(DataType.kVarchar).build()); + RowBuilder builder; + if (builderName.equals("classic")) { + ClassicRowBuilder cBuilder = new ClassicRowBuilder(schema); + int size = cBuilder.calTotalLength(0); + ByteBuffer buffer = ByteBuffer.allocate(size).order(ByteOrder.LITTLE_ENDIAN); + cBuilder.setBuffer(buffer, size); + builder = cBuilder; + } else { + builder = new FlexibleRowBuilder(schema); + } + Assert.assertTrue(builder.appendTimestamp(null)); + Assert.assertTrue(builder.appendDate(null)); + Assert.assertTrue(builder.appendString(null)); + Assert.assertTrue(builder.build()); + ByteBuffer buffer = builder.getValue(); + RowView rowView = new RowView(schema, buffer, buffer.capacity()); + Assert.assertTrue(rowView.isNull(0)); + Assert.assertTrue(rowView.isNull(1)); + Assert.assertTrue(rowView.isNull(2)); + } catch (Exception e) { + e.printStackTrace(); + Assert.assertTrue(false); + } + } + @Test(dataProvider = "builder") public void testNormal(String builderName) { try {