This is an automated email from the ASF dual-hosted git repository.

jt2594838 pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/iotdb.git


The following commit(s) were added to refs/heads/master by this push:
     new 594a237e79c Fix JDBC batch failure reporting and database sync (#19245)
594a237e79c is described below

commit 594a237e79c55533591ef60f7773a9dbb3dc9316
Author: Caideyipi <[email protected]>
AuthorDate: Fri Nov 13 14:33:05 2026 +0800

    Fix JDBC batch failure reporting and database sync (#19245)
    
    * Fix JDBC batch failure status handling
    
    * Fix invalid integration test fixtures exposed by batch error reporting
    
    * Fix legacy table IT setup data
---
 .../scalar/IoTDBReplaceFunctionIT.java             |   2 +-
 .../scalar/IoTDBSubStringFunctionIT.java           |   2 +-
 .../it/query/IoTDBAggregationFirstInGroupIT.java   |   2 +-
 .../db/it/IoTDBTablePreparedStatementJDBCIT.java   | 128 +++++++++++++++++++++
 ...oTDBNoSelectExpressionAfterAnalyzedTableIT.java |   7 +-
 .../view/recent/IoTDBComplexQueryTableViewIT.java  |   2 -
 .../it/schema/IoTDBAlterColumnTypeIT.java          |   1 -
 .../protocol/thrift/impl/ClientRPCServiceImpl.java |   9 ++
 8 files changed, 145 insertions(+), 8 deletions(-)

diff --git 
a/integration-test/src/test/java/org/apache/iotdb/db/it/builtinfunction/scalar/IoTDBReplaceFunctionIT.java
 
b/integration-test/src/test/java/org/apache/iotdb/db/it/builtinfunction/scalar/IoTDBReplaceFunctionIT.java
index 12c60856234..19cf720ef73 100644
--- 
a/integration-test/src/test/java/org/apache/iotdb/db/it/builtinfunction/scalar/IoTDBReplaceFunctionIT.java
+++ 
b/integration-test/src/test/java/org/apache/iotdb/db/it/builtinfunction/scalar/IoTDBReplaceFunctionIT.java
@@ -55,7 +55,7 @@ public class IoTDBReplaceFunctionIT {
         "CREATE TIMESERIES root.sg.s8 WITH DATATYPE=TIMESTAMP, ENCODING=PLAIN",
         "CREATE TIMESERIES root.sg.s9 WITH DATATYPE=STRING, ENCODING=PLAIN",
         "CREATE TIMESERIES root.sg.s10 WITH DATATYPE=BLOB, ENCODING=PLAIN",
-        "INSERT INTO root.sg(timestamp,s1,s2,s3,s4,s5,s6,s7,s8,s9,s10) 
values(1, 'abcd', 1, 1, 1, 1, true, '2021-10-01', 1633046400000, 
'abcd','abcd')",
+        "INSERT INTO root.sg(timestamp,s1,s2,s3,s4,s5,s6,s7,s8,s9,s10) 
values(1, 'abcd', 1, 1, 1, 1, true, '2021-10-01', 1633046400000, 'abcd', 
X'61626364')",
         "INSERT INTO root.sg(timestamp,s1) values(2, 'test\\\\')",
         "INSERT INTO root.sg(timestamp,s1) values(3, 'abcd\\\\')",
         "INSERT INTO root.sg(timestamp,s9) values(2, 'test\\\\')",
diff --git 
a/integration-test/src/test/java/org/apache/iotdb/db/it/builtinfunction/scalar/IoTDBSubStringFunctionIT.java
 
b/integration-test/src/test/java/org/apache/iotdb/db/it/builtinfunction/scalar/IoTDBSubStringFunctionIT.java
index 15e2d400497..8cc8023fe18 100644
--- 
a/integration-test/src/test/java/org/apache/iotdb/db/it/builtinfunction/scalar/IoTDBSubStringFunctionIT.java
+++ 
b/integration-test/src/test/java/org/apache/iotdb/db/it/builtinfunction/scalar/IoTDBSubStringFunctionIT.java
@@ -56,7 +56,7 @@ public class IoTDBSubStringFunctionIT {
         "CREATE TIMESERIES root.sg.s8 WITH DATATYPE=TIMESTAMP, ENCODING=PLAIN",
         "CREATE TIMESERIES root.sg.s9 WITH DATATYPE=STRING, ENCODING=PLAIN",
         "CREATE TIMESERIES root.sg.s10 WITH DATATYPE=BLOB, ENCODING=PLAIN",
-        "INSERT INTO root.sg(timestamp,s1,s2,s3,s4,s5,s6,s7,s8,s9,s10) 
values(1, 'abcd', 1, 1, 1, 1, true, '2021-10-01', 1633046400000, 'abcd', 
'abcd')",
+        "INSERT INTO root.sg(timestamp,s1,s2,s3,s4,s5,s6,s7,s8,s9,s10) 
values(1, 'abcd', 1, 1, 1, 1, true, '2021-10-01', 1633046400000, 'abcd', 
X'61626364')",
         "INSERT INTO root.sg(timestamp,s1) values(2, 'test')",
         "INSERT INTO root.sg(timestamp,s1) values(3, 'abcdefg')",
         "INSERT INTO root.sg(timestamp,s9) values(2, 'test')",
diff --git 
a/integration-test/src/test/java/org/apache/iotdb/db/it/query/IoTDBAggregationFirstInGroupIT.java
 
b/integration-test/src/test/java/org/apache/iotdb/db/it/query/IoTDBAggregationFirstInGroupIT.java
index c6fa3510230..962f1f57cda 100644
--- 
a/integration-test/src/test/java/org/apache/iotdb/db/it/query/IoTDBAggregationFirstInGroupIT.java
+++ 
b/integration-test/src/test/java/org/apache/iotdb/db/it/query/IoTDBAggregationFirstInGroupIT.java
@@ -73,7 +73,7 @@ public class IoTDBAggregationFirstInGroupIT {
             + "(-21,  40,   40,   40.0,   40.0,   false, '40s',   40,   
'p2')," // Previous Valid
             + "(-102, 20,   20,   20.0,   20.0,   true,  '20s',   NULL, 
'p2')," // Null Time
             + "(33,   NULL, 5,    5.0,    NULL,   false,  NULL,   5,    'p2'),"
-            + "(100,  100,  100,  100.0,  100,    100,    100,    100,  'p2'),"
+            + "(100,  100,  100,  100.0,  100,    true,   100,    100,  'p2'),"
 
             // Partition 'p3': Only Null Time Scenario
             // Logic: No Valid Time. "First Null Wins" strategy applies.
diff --git 
a/integration-test/src/test/java/org/apache/iotdb/relational/it/db/it/IoTDBTablePreparedStatementJDBCIT.java
 
b/integration-test/src/test/java/org/apache/iotdb/relational/it/db/it/IoTDBTablePreparedStatementJDBCIT.java
index 0e22de86042..acee0923c21 100644
--- 
a/integration-test/src/test/java/org/apache/iotdb/relational/it/db/it/IoTDBTablePreparedStatementJDBCIT.java
+++ 
b/integration-test/src/test/java/org/apache/iotdb/relational/it/db/it/IoTDBTablePreparedStatementJDBCIT.java
@@ -24,6 +24,7 @@ import org.apache.iotdb.it.framework.IoTDBTestRunner;
 import org.apache.iotdb.itbase.category.TableClusterIT;
 import org.apache.iotdb.itbase.category.TableLocalStandaloneIT;
 import org.apache.iotdb.itbase.runtime.ClusterTestConnection;
+import org.apache.iotdb.rpc.TSStatusCode;
 
 import org.junit.AfterClass;
 import org.junit.BeforeClass;
@@ -31,6 +32,7 @@ import org.junit.Test;
 import org.junit.experimental.categories.Category;
 import org.junit.runner.RunWith;
 
+import java.sql.BatchUpdateException;
 import java.sql.Connection;
 import java.sql.Date;
 import java.sql.ParameterMetaData;
@@ -45,6 +47,7 @@ import java.time.LocalDate;
 import static org.junit.Assert.assertArrayEquals;
 import static org.junit.Assert.assertEquals;
 import static org.junit.Assert.assertFalse;
+import static org.junit.Assert.assertThrows;
 import static org.junit.Assert.assertTrue;
 
 @RunWith(IoTDBTestRunner.class)
@@ -353,6 +356,131 @@ public class IoTDBTablePreparedStatementJDBCIT {
     }
   }
 
+  @Test
+  public void testPreparedStatementBatchFailureAndReuse() throws SQLException {
+    assertPreparedStatementBatchFailureAndReuse(
+        "prepared_batch_failure_test", true, TSStatusCode.SEMANTIC_ERROR);
+  }
+
+  @Test
+  public void testPreparedStatementBatchStatusFailureAndReuse() throws 
SQLException {
+    assertPreparedStatementBatchFailureAndReuse(
+        "prepared_batch_status_failure_test", false, 
TSStatusCode.METADATA_ERROR);
+  }
+
+  @Test
+  public void testBatchFailureKeepsDefaultDatabaseInSync() throws SQLException 
{
+    String database = "prepared_batch_database_sync_test";
+    String table = "prepared_batch_database_sync_table";
+    try (Connection connection = EnvFactory.getEnv().getTableConnection();
+        Statement statement = connection.createStatement()) {
+      statement.execute("CREATE DATABASE " + database);
+      statement.execute("USE " + database);
+      statement.execute("CREATE TABLE " + table + "(device_id STRING TAG, 
reading INT32 FIELD)");
+      statement.execute("USE " + DATABASE_NAME);
+
+      Connection batchConnection =
+          connection instanceof ClusterTestConnection
+              ? ((ClusterTestConnection) 
connection).writeConnection.getUnderlyingConnection()
+              : connection;
+      assertEquals(DATABASE_NAME, batchConnection.getSchema());
+      try (Statement batchStatement = batchConnection.createStatement()) {
+        batchStatement.addBatch("USE " + database);
+        batchStatement.addBatch(
+            "INSERT INTO "
+                + table
+                + "(time, device_id, reading) VALUES (1, 'rejected', 
'not_an_int')");
+
+        BatchUpdateException error =
+            assertThrows(BatchUpdateException.class, 
batchStatement::executeBatch);
+        int[] updateCounts = error.getUpdateCounts();
+        assertEquals(2, updateCounts.length);
+        assertBatchSuccessStatus(updateCounts[0]);
+        assertEquals(TSStatusCode.METADATA_ERROR.getStatusCode(), 
updateCounts[1]);
+        assertEquals(database, batchConnection.getSchema());
+      }
+    }
+  }
+
+  private void assertPreparedStatementBatchFailureAndReuse(
+      String table, boolean invalidTime, TSStatusCode expectedFailureStatus) 
throws SQLException {
+    try (Connection connection = EnvFactory.getEnv().getTableConnection();
+        Statement statement = connection.createStatement()) {
+      statement.execute("USE " + DATABASE_NAME);
+      statement.execute("CREATE TABLE " + table + "(device_id STRING TAG, 
reading INT32 FIELD)");
+
+      Connection preparedConnection =
+          connection instanceof ClusterTestConnection
+              ? ((ClusterTestConnection) 
connection).writeConnection.getUnderlyingConnection()
+              : connection;
+      try (PreparedStatement preparedStatement =
+          preparedConnection.prepareStatement(
+              "INSERT INTO " + table + "(time, device_id, reading) VALUES (?, 
?, ?)")) {
+        preparedStatement.setLong(1, 1L);
+        preparedStatement.setString(2, "before_failure");
+        preparedStatement.setInt(3, 11);
+        preparedStatement.addBatch();
+
+        if (invalidTime) {
+          preparedStatement.setString(1, "not_a_timestamp");
+          preparedStatement.setInt(3, 22);
+        } else {
+          preparedStatement.setLong(1, 2L);
+          preparedStatement.setString(3, "not_an_int");
+        }
+        preparedStatement.setString(2, "rejected");
+        preparedStatement.addBatch();
+
+        preparedStatement.setLong(1, 3L);
+        preparedStatement.setString(2, "after_failure");
+        preparedStatement.setInt(3, 33);
+        preparedStatement.addBatch();
+
+        BatchUpdateException error =
+            assertThrows(BatchUpdateException.class, 
preparedStatement::executeBatch);
+        int[] updateCounts = error.getUpdateCounts();
+        assertEquals(3, updateCounts.length);
+        assertBatchSuccessStatus(updateCounts[0]);
+        assertEquals(expectedFailureStatus.getStatusCode(), updateCounts[1]);
+        assertBatchSuccessStatus(updateCounts[2]);
+
+        preparedStatement.setLong(1, 4L);
+        preparedStatement.setString(2, "after_reuse");
+        preparedStatement.setInt(3, 44);
+        preparedStatement.addBatch();
+        int[] retryCounts = preparedStatement.executeBatch();
+        assertEquals(1, retryCounts.length);
+        assertBatchSuccessStatus(retryCounts[0]);
+      }
+
+      try (ResultSet resultSet =
+          statement.executeQuery(
+              "SELECT time, device_id, reading FROM " + table + " ORDER BY 
time")) {
+        assertTrue(resultSet.next());
+        assertEquals(1L, resultSet.getLong("time"));
+        assertEquals("before_failure", resultSet.getString("device_id"));
+        assertEquals(11, resultSet.getInt("reading"));
+
+        assertTrue(resultSet.next());
+        assertEquals(3L, resultSet.getLong("time"));
+        assertEquals("after_failure", resultSet.getString("device_id"));
+        assertEquals(33, resultSet.getInt("reading"));
+
+        assertTrue(resultSet.next());
+        assertEquals(4L, resultSet.getLong("time"));
+        assertEquals("after_reuse", resultSet.getString("device_id"));
+        assertEquals(44, resultSet.getInt("reading"));
+        assertFalse(resultSet.next());
+      }
+    }
+  }
+
+  private void assertBatchSuccessStatus(int statusCode) {
+    assertTrue(
+        statusCode == TSStatusCode.SUCCESS_STATUS.getStatusCode()
+            || statusCode == 
TSStatusCode.REDIRECTION_RECOMMEND.getStatusCode());
+  }
+
   @Test
   public void testPreparedStatementAggregation() throws SQLException {
     try (Connection connection = getConnection();
diff --git 
a/integration-test/src/test/java/org/apache/iotdb/relational/it/query/old/query/IoTDBNoSelectExpressionAfterAnalyzedTableIT.java
 
b/integration-test/src/test/java/org/apache/iotdb/relational/it/query/old/query/IoTDBNoSelectExpressionAfterAnalyzedTableIT.java
index 27d14c0e1fc..ab02d5e62f4 100644
--- 
a/integration-test/src/test/java/org/apache/iotdb/relational/it/query/old/query/IoTDBNoSelectExpressionAfterAnalyzedTableIT.java
+++ 
b/integration-test/src/test/java/org/apache/iotdb/relational/it/query/old/query/IoTDBNoSelectExpressionAfterAnalyzedTableIT.java
@@ -43,14 +43,17 @@ public class IoTDBNoSelectExpressionAfterAnalyzedTableIT {
         "CREATE DATABASE " + DATABASE_NAME,
         "USE " + DATABASE_NAME,
         "CREATE TABLE sg(device STRING TAG, s1 INT32 FIELD)",
-        "insert into sg(time,device,s1) values(1,'d1',1)",
-        "insert into sg(time,device,s1,s2) values(1,'d1',1,1)"
+        "insert into sg(time,device,s1) values(1,'d1',1)"
       };
 
   @BeforeClass
   public static void setUp() throws Exception {
     EnvFactory.getEnv().initClusterEnvironment();
     prepareTableData(SQLs);
+    tableAssertTestFail(
+        "insert into sg(time,device,s1,s2) values(1,'d1',1,1)",
+        "616: Unknown column category for s2. Cannot auto create column.",
+        DATABASE_NAME);
   }
 
   @AfterClass
diff --git 
a/integration-test/src/test/java/org/apache/iotdb/relational/it/query/view/recent/IoTDBComplexQueryTableViewIT.java
 
b/integration-test/src/test/java/org/apache/iotdb/relational/it/query/view/recent/IoTDBComplexQueryTableViewIT.java
index b6617ec50dd..bea6e50889e 100644
--- 
a/integration-test/src/test/java/org/apache/iotdb/relational/it/query/view/recent/IoTDBComplexQueryTableViewIT.java
+++ 
b/integration-test/src/test/java/org/apache/iotdb/relational/it/query/view/recent/IoTDBComplexQueryTableViewIT.java
@@ -44,8 +44,6 @@ public class IoTDBComplexQueryTableViewIT {
       new String[] {
         "CREATE DATABASE " + TREE_DB_NAME,
         "create aligned timeseries root.test.employees.D001(name TEXT,Gender 
TEXT,Status BOOLEAN,employee_id INT32,salary DOUBLE,date_of_birth 
DATE,Contac_info string)",
-        "create aligned timeseries root.test.employees.D001(name TEXT,Gender 
TEXT,Status BOOLEAN,employee_id INT32,salary DOUBLE,date_of_birth 
DATE,Contac_info string)",
-        "create aligned timeseries root.test.employees.D002(name TEXT,Gender 
TEXT,Status BOOLEAN,employee_id INT32,salary DOUBLE,date_of_birth 
DATE,Contac_info string)",
         "create aligned timeseries root.test.employees.D002(name TEXT,Gender 
TEXT,Status BOOLEAN,employee_id INT32,salary DOUBLE,date_of_birth 
DATE,Contac_info string)",
         "create aligned timeseries root.test.employees.D003(name TEXT,Gender 
TEXT,Status BOOLEAN,employee_id INT32,salary DOUBLE,date_of_birth 
DATE,Contac_info string)",
         "create aligned timeseries root.test.departments.D001(`1department_id` 
STRING,dep_name TEXT,dep_phone TEXT,dep_status BOOLEAN,dep_member 
INT32,employee_id INT32)",
diff --git 
a/integration-test/src/test/java/org/apache/iotdb/relational/it/schema/IoTDBAlterColumnTypeIT.java
 
b/integration-test/src/test/java/org/apache/iotdb/relational/it/schema/IoTDBAlterColumnTypeIT.java
index 00c3df4f390..7d810c59023 100644
--- 
a/integration-test/src/test/java/org/apache/iotdb/relational/it/schema/IoTDBAlterColumnTypeIT.java
+++ 
b/integration-test/src/test/java/org/apache/iotdb/relational/it/schema/IoTDBAlterColumnTypeIT.java
@@ -1087,7 +1087,6 @@ public class IoTDBAlterColumnTypeIT {
     };
 
     String[] createTableSqls = {
-      "CREATE DATABASE test",
       "USE test",
       "CREATE VIEW view1 (battery TAG, voltage INT32 FIELD, current FLOAT 
FIELD) as root.db.battery.**",
     };
diff --git 
a/iotdb-core/datanode/src/main/java/org/apache/iotdb/db/protocol/thrift/impl/ClientRPCServiceImpl.java
 
b/iotdb-core/datanode/src/main/java/org/apache/iotdb/db/protocol/thrift/impl/ClientRPCServiceImpl.java
index a1d04180437..e6eedc4b636 100644
--- 
a/iotdb-core/datanode/src/main/java/org/apache/iotdb/db/protocol/thrift/impl/ClientRPCServiceImpl.java
+++ 
b/iotdb-core/datanode/src/main/java/org/apache/iotdb/db/protocol/thrift/impl/ClientRPCServiceImpl.java
@@ -2311,6 +2311,10 @@ public class ClientRPCServiceImpl implements 
IClientRPCServiceWithHandler {
             }
           }
 
+          if (result.status.getCode() != 
TSStatusCode.SUCCESS_STATUS.getStatusCode()
+              && result.status.getCode() != 
TSStatusCode.REDIRECTION_RECOMMEND.getStatusCode()) {
+            isAllSuccessful = false;
+          }
           results.add(result.status);
         } catch (Exception e) {
           LOGGER.warn(DataNodeMiscMessages.ERROR_EXECUTING_BATCH_STATEMENT, e);
@@ -2345,6 +2349,11 @@ public class ClientRPCServiceImpl implements 
IClientRPCServiceWithHandler {
       }
       return res;
     } else {
+      if (useDatabase || isDatabaseSetBefore && 
Objects.isNull(clientSession.getDatabaseName())) {
+        // Keep the JDBC connection's cached database synchronized after a 
partially failed batch.
+        results.add(
+            RpcUtils.getStatus(TSStatusCode.USE_OR_DROP_DB, 
clientSession.getDatabaseName()));
+      }
       return RpcUtils.getStatus(results);
     }
   }

Reply via email to