From fdab61d32c6b772eb5b9ca4f1c813d39763eee34 Mon Sep 17 00:00:00 2001 From: hantmac Date: Wed, 21 Feb 2024 17:24:19 +0800 Subject: [PATCH 1/2] fix: setObject with null --- .../java/com/databend/jdbc/DatabendPreparedStatement.java | 2 +- .../test/java/com/databend/jdbc/TestPrepareStatement.java | 7 +++++-- 2 files changed, 6 insertions(+), 3 deletions(-) diff --git a/databend-jdbc/src/main/java/com/databend/jdbc/DatabendPreparedStatement.java b/databend-jdbc/src/main/java/com/databend/jdbc/DatabendPreparedStatement.java index 54fde683..569eaf5a 100644 --- a/databend-jdbc/src/main/java/com/databend/jdbc/DatabendPreparedStatement.java +++ b/databend-jdbc/src/main/java/com/databend/jdbc/DatabendPreparedStatement.java @@ -581,7 +581,7 @@ public void setObject(int parameterIndex, Object x, int targetSqlType) throws SQLException { checkOpen(); if (x == null) { - batchInsertUtils.ifPresent(insertUtils -> insertUtils.setPlaceHolderValue(parameterIndex, null)); + setNull(parameterIndex, Types.NULL); return; } switch (targetSqlType) { diff --git a/databend-jdbc/src/test/java/com/databend/jdbc/TestPrepareStatement.java b/databend-jdbc/src/test/java/com/databend/jdbc/TestPrepareStatement.java index 606e5f20..542640f8 100644 --- a/databend-jdbc/src/test/java/com/databend/jdbc/TestPrepareStatement.java +++ b/databend-jdbc/src/test/java/com/databend/jdbc/TestPrepareStatement.java @@ -91,9 +91,12 @@ public void TestBatchInsertWithNULL() throws SQLException { ps.setInt(1, 2); ps.setNull(2, Types.NULL); ps.addBatch(); + ps.setInt(1, 3); + ps.setObject(2, null, Types.NULL); + ps.addBatch(); System.out.println("execute batch insert"); int[] ans = ps.executeBatch(); - Assert.assertEquals(ans.length, 2); + Assert.assertEquals(ans.length, 3); Assert.assertEquals(ans[0], 1); Assert.assertEquals(ans[1], 1); Statement statement = c.createStatement(); @@ -105,7 +108,7 @@ public void TestBatchInsertWithNULL() throws SQLException { while (r.next()) { System.out.println(r.getInt(1)); //TODO(hantmac): need use real NULL type - Assert.assertEquals(r.getNString(2), "NULL"); + Assert.assertEquals(r.getObject(2), null); } } From 02930cba8df386555a8e57764d0771b626cdb8ad Mon Sep 17 00:00:00 2001 From: hantmac Date: Thu, 22 Feb 2024 14:43:03 +0800 Subject: [PATCH 2/2] fix executeUpdate setnul --- .../jdbc/DatabendPreparedStatement.java | 11 +++++-- .../databend/jdbc/TestPrepareStatement.java | 31 +++++++++++++++++++ 2 files changed, 39 insertions(+), 3 deletions(-) diff --git a/databend-jdbc/src/main/java/com/databend/jdbc/DatabendPreparedStatement.java b/databend-jdbc/src/main/java/com/databend/jdbc/DatabendPreparedStatement.java index 569eaf5a..df185aba 100644 --- a/databend-jdbc/src/main/java/com/databend/jdbc/DatabendPreparedStatement.java +++ b/databend-jdbc/src/main/java/com/databend/jdbc/DatabendPreparedStatement.java @@ -438,9 +438,14 @@ public int executeUpdate() throws SQLException { public void setNull(int i, int i1) throws SQLException { checkOpen(); - // Databend uses \N as default null representation for csv and tsv format - // https://github.com/datafuselabs/databend/pull/6453 - batchInsertUtils.ifPresent(insertUtils -> insertUtils.setPlaceHolderValue(i, "\\N")); + if (this.originalSql.toLowerCase().contains("insert") || + this.originalSql.toLowerCase().contains("replace")) { + // Databend uses \N as default null representation for csv and tsv format + // https://github.com/datafuselabs/databend/pull/6453 + batchInsertUtils.ifPresent(insertUtils -> insertUtils.setPlaceHolderValue(i, "\\N")); + } else { + batchInsertUtils.ifPresent(insertUtils -> insertUtils.setPlaceHolderValue(i, "null")); + } } @Override diff --git a/databend-jdbc/src/test/java/com/databend/jdbc/TestPrepareStatement.java b/databend-jdbc/src/test/java/com/databend/jdbc/TestPrepareStatement.java index 542640f8..f9837789 100644 --- a/databend-jdbc/src/test/java/com/databend/jdbc/TestPrepareStatement.java +++ b/databend-jdbc/src/test/java/com/databend/jdbc/TestPrepareStatement.java @@ -1,6 +1,7 @@ package com.databend.jdbc; import com.databend.client.StageAttachment; +import org.apache.commons.lang3.ObjectUtils; import org.junit.jupiter.api.Assertions; import org.testng.Assert; import org.testng.annotations.BeforeTest; @@ -346,6 +347,36 @@ public void testPrepareStatementExecute() throws SQLException { } } + @Test + public void testUpdateSetNull() throws SQLException { + Connection conn = createConnection(); + String sql = "insert into test_prepare_statement values (?,?)"; + try (PreparedStatement statement = conn.prepareStatement(sql)) { + statement.setInt(1, 1); + statement.setString(2, "b"); + statement.addBatch(); + int[] result = statement.executeBatch(); + System.out.println(result); + Assertions.assertEquals(1, result.length); + } + String updateSQL = "update test_prepare_statement set b = ? where a = ?"; + try (PreparedStatement statement = conn.prepareStatement(updateSQL)) { + statement.setInt(2, 1); + statement.setNull(1, Types.NULL); + int result = statement.executeUpdate(); + System.out.println(result); + Assertions.assertEquals(2, result); + } + try (PreparedStatement statement = conn.prepareStatement("select a, regexp_replace(b, '\\d', '*') from test_prepare_statement where a = ?")) { + statement.setInt(1, 1); + ResultSet r = statement.executeQuery(); + while (r.next()) { + Assertions.assertEquals(1, r.getInt(1)); + Assertions.assertEquals(null, r.getString(2)); + } + } + } + @Test public void testAllPreparedStatement() throws SQLException { String sql = "insert into test_prepare_statement values (?,?)";