Skip to content

Commit

Permalink
fix: set object null
Browse files Browse the repository at this point in the history
  • Loading branch information
4paradigm committed Nov 30, 2023
1 parent 71cf61e commit e54e232
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -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());
Expand Down Expand Up @@ -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);
Expand All @@ -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) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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);
Expand All @@ -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;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,38 @@ public void testNull(String builderName) {
}
}

@Test(dataProvider = "builder")
public void testValueNull(String builderName) {
try {
List<ColumnDesc> schema = new ArrayList<ColumnDesc>();
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 {
Expand Down

0 comments on commit e54e232

Please sign in to comment.