Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: throw null pointer exception if the set value is null in RowBuilder #3649

Merged
merged 4 commits into from
Dec 26, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion docs/en/quickstart/sdk/java_sdk.md
Original file line number Diff line number Diff line change
Expand Up @@ -241,7 +241,8 @@ try {

1. Get `InsertPrepareStatement` by calling `SqlClusterExecutor::getInsertPreparedStmt(db, insertSqlWithPlaceHolder)` interface.
2. Use `PreparedStatement::setType(index, value)` interface to fill in data to the `InsertPrepareStatement`. Note that the index starts from 1.
3. Use `PreparedStatement::execute()` interface to execute the insert statement.
3. For String, Date and Timestamp types, null objects can be set using either `setType(index, null)` or `setNull(index)`.
4. Use `PreparedStatement::execute()` interface to execute the insert statement.

```{note}
When the conditions of the `PreparedStatement` are the same, you can repeatedly call the set method of the same object to fill in data before executing `execute`. There is no need to create a new `PreparedStatement` object.
Expand Down
3 changes: 2 additions & 1 deletion docs/zh/quickstart/sdk/java_sdk.md
Original file line number Diff line number Diff line change
Expand Up @@ -245,7 +245,8 @@ try {

1. 使用 `SqlClusterExecutor::getInsertPreparedStmt(db, insertSqlWithPlaceHolder)` 接口获取 InsertPrepareStatement。
2. 调用 `PreparedStatement::setType(index, value)` 接口,填充数据到 InsertPrepareStatement中。注意 index 从 1 开始。
3. 使用 `PreparedStatement::execute()` 接口执行 insert 语句。
3. 对于String, Date和Timestamp类型, 可以通过`setType(index, null)`和`setNull(index)`两种方式来设置null对象。
4. 使用 `PreparedStatement::execute()` 接口执行 insert 语句。
```{note}
PreparedStatment条件相同时,可以对同一个对象反复set填充数据后,再执行execute,不需要重新创建PreparedStatement。
```
Expand Down
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();
dl239 marked this conversation as resolved.
Show resolved Hide resolved
}
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,11 +266,14 @@ public boolean appendDate(Date date) {

@Override
public boolean appendString(String val) {
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 (val == null) {
return appendNULL();
}
byte[] bytes = val.getBytes(CodecUtil.CHARSET);
int length = bytes.length;
if (strOffset + length > size) {
return false;
}
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
Loading