From 326ef12ac7e737ad75d20bd230bb00a352595b0e Mon Sep 17 00:00:00 2001 From: 761417898 <761417898@qq.com> Date: Mon, 16 Nov 2026 10:52:11 +0800 Subject: [PATCH] Add testInsert API for Java table sessions --- .../it/session/IoTDBTableModelSessionIT.java | 58 +++++++++++++++++++ .../apache/iotdb/isession/ITableSession.java | 13 +++++ .../org/apache/iotdb/session/Session.java | 21 +++++++ .../apache/iotdb/session/TableSession.java | 6 ++ .../session/pool/TableSessionWrapper.java | 13 +++++ 5 files changed, 111 insertions(+) diff --git a/integration-test/src/test/java/org/apache/iotdb/relational/it/session/IoTDBTableModelSessionIT.java b/integration-test/src/test/java/org/apache/iotdb/relational/it/session/IoTDBTableModelSessionIT.java index d6e0af9467be9..574f65e7effb9 100644 --- a/integration-test/src/test/java/org/apache/iotdb/relational/it/session/IoTDBTableModelSessionIT.java +++ b/integration-test/src/test/java/org/apache/iotdb/relational/it/session/IoTDBTableModelSessionIT.java @@ -22,6 +22,7 @@ import org.apache.iotdb.db.it.utils.TestUtils; import org.apache.iotdb.isession.ITableSession; import org.apache.iotdb.isession.SessionDataSet; +import org.apache.iotdb.isession.pool.ITableSessionPool; import org.apache.iotdb.it.env.EnvFactory; import org.apache.iotdb.it.framework.IoTDBTestRunner; import org.apache.iotdb.itbase.category.TableClusterIT; @@ -29,7 +30,10 @@ import org.apache.iotdb.rpc.IoTDBConnectionException; import org.apache.iotdb.rpc.StatementExecutionException; +import org.apache.tsfile.enums.ColumnCategory; +import org.apache.tsfile.enums.TSDataType; import org.apache.tsfile.read.common.RowRecord; +import org.apache.tsfile.write.record.Tablet; import org.junit.After; import org.junit.AfterClass; import org.junit.BeforeClass; @@ -40,10 +44,13 @@ import java.sql.Connection; import java.sql.SQLException; import java.sql.Statement; +import java.util.Arrays; import static org.apache.iotdb.commons.schema.column.ColumnHeaderConstant.showTablesColumnHeaders; import static org.apache.iotdb.itbase.env.BaseEnv.TABLE_SQL_DIALECT; import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertTrue; import static org.junit.Assert.fail; @RunWith(IoTDBTestRunner.class) @@ -70,6 +77,57 @@ public static void tearDownClass() throws Exception { EnvFactory.getEnv().cleanClusterEnvironment(); } + @Test + public void testInsertWithoutWriting() throws Exception { + Tablet tablet = + new Tablet( + "test_insert", + Arrays.asList("device", "model", "value"), + Arrays.asList(TSDataType.STRING, TSDataType.STRING, TSDataType.INT64), + Arrays.asList(ColumnCategory.TAG, ColumnCategory.ATTRIBUTE, ColumnCategory.FIELD), + 2); + try (ITableSession session = EnvFactory.getEnv().getTableSessionConnection()) { + session.executeNonQueryStatement("create database test1"); + session.executeNonQueryStatement("use test1"); + session.executeNonQueryStatement( + "create table test_insert (device string tag, model string attribute, value int64 field)"); + session.testInsert(tablet); + for (int row = 0; row < 2; row++) { + long time = 2 - row; + tablet.addTimestamp(row, time); + tablet.addValue("device", row, "d" + time); + tablet.addValue("model", row, "m" + time); + tablet.addValue("value", row, time * 10); + } + session.testInsert(tablet); + try (SessionDataSet dataSet = session.executeQueryStatement("select * from test_insert")) { + assertFalse(dataSet.hasNext()); + } + } + try (ITableSessionPool pool = EnvFactory.getEnv().getTableSessionPool(1); + ITableSession session = pool.getSession()) { + session.executeNonQueryStatement("use test1"); + session.testInsert(tablet); + try (SessionDataSet dataSet = session.executeQueryStatement("select * from test_insert")) { + assertFalse(dataSet.hasNext()); + } + session.insert(tablet); + try (SessionDataSet dataSet = + session.executeQueryStatement( + "select time, device, model, value from test_insert order by time")) { + for (long time = 1; time <= 2; time++) { + assertTrue(dataSet.hasNext()); + RowRecord row = dataSet.next(); + assertEquals(time, row.getFields().get(0).getLongV()); + assertEquals("d" + time, row.getFields().get(1).getStringValue()); + assertEquals("m" + time, row.getFields().get(2).getStringValue()); + assertEquals(time * 10, row.getFields().get(3).getLongV()); + } + assertFalse(dataSet.hasNext()); + } + } + } + @Test public void testUseDatabase() { final String[] table1Names = new String[] {"table1"}; diff --git a/iotdb-client/isession/src/main/java/org/apache/iotdb/isession/ITableSession.java b/iotdb-client/isession/src/main/java/org/apache/iotdb/isession/ITableSession.java index 75a37f8384719..ec9b0aa9abdd6 100644 --- a/iotdb-client/isession/src/main/java/org/apache/iotdb/isession/ITableSession.java +++ b/iotdb-client/isession/src/main/java/org/apache/iotdb/isession/ITableSession.java @@ -46,6 +46,19 @@ public interface ITableSession extends AutoCloseable { */ void insert(Tablet tablet) throws StatementExecutionException, IoTDBConnectionException; + /** + * Sends a {@link Tablet} to measure client processing and RPC time without inserting data. The + * server returns after receiving the request without validating the table schema or data. + * + *

The tablet may be sorted in place. Empty tablets are ignored. The request is sent through + * the default connection without device-based routing or leader-cache updates. + * + * @param tablet the tablet to serialize and send. + * @throws StatementExecutionException if the server returns an unsuccessful status. + * @throws IoTDBConnectionException if there is an issue with the IoTDB connection. + */ + void testInsert(Tablet tablet) throws StatementExecutionException, IoTDBConnectionException; + /** * Executes a non-query SQL statement, such as a DDL or DML command. * diff --git a/iotdb-client/session/src/main/java/org/apache/iotdb/session/Session.java b/iotdb-client/session/src/main/java/org/apache/iotdb/session/Session.java index d8bcce5e13def..0511f7075e2f6 100644 --- a/iotdb-client/session/src/main/java/org/apache/iotdb/session/Session.java +++ b/iotdb-client/session/src/main/java/org/apache/iotdb/session/Session.java @@ -23,6 +23,7 @@ import org.apache.iotdb.common.rpc.thrift.TEndPoint; import org.apache.iotdb.isession.INodeSupplier; import org.apache.iotdb.isession.ISession; +import org.apache.iotdb.isession.ITableSession; import org.apache.iotdb.isession.SessionConfig; import org.apache.iotdb.isession.SessionDataSet; import org.apache.iotdb.isession.template.Template; @@ -2930,6 +2931,26 @@ public void insertRelationalTablet(Tablet tablet) } } + /** + * Sends a relational tablet without inserting data or validating it on the server. + * + *

This method is for internal use only; use {@link ITableSession#testInsert(Tablet)} instead. + * + * @param tablet data batch + */ + public void testInsertRelationalTablet(Tablet tablet) + throws IoTDBConnectionException, StatementExecutionException { + if (tablet.getRowSize() == 0) { + return; + } + TSInsertTabletReq request = genTSInsertTabletReq(tablet, false, false); + if (request == null) { + return; + } + request.setWriteToTable(true); + getDefaultSessionConnection().testInsertTablet(request); + } + private void insertRelationalTabletWithLeaderCache(Tablet tablet) throws IoTDBConnectionException, StatementExecutionException { Map relationalTabletGroup = new HashMap<>(); diff --git a/iotdb-client/session/src/main/java/org/apache/iotdb/session/TableSession.java b/iotdb-client/session/src/main/java/org/apache/iotdb/session/TableSession.java index 059fdea346948..c51bc734d6684 100644 --- a/iotdb-client/session/src/main/java/org/apache/iotdb/session/TableSession.java +++ b/iotdb-client/session/src/main/java/org/apache/iotdb/session/TableSession.java @@ -39,6 +39,12 @@ public void insert(Tablet tablet) throws StatementExecutionException, IoTDBConne session.insertRelationalTablet(tablet); } + @Override + public void testInsert(Tablet tablet) + throws StatementExecutionException, IoTDBConnectionException { + session.testInsertRelationalTablet(tablet); + } + @Override public void executeNonQueryStatement(String sql) throws IoTDBConnectionException, StatementExecutionException { diff --git a/iotdb-client/session/src/main/java/org/apache/iotdb/session/pool/TableSessionWrapper.java b/iotdb-client/session/src/main/java/org/apache/iotdb/session/pool/TableSessionWrapper.java index 4cf7ca980f347..f12b3acc71a87 100644 --- a/iotdb-client/session/src/main/java/org/apache/iotdb/session/pool/TableSessionWrapper.java +++ b/iotdb-client/session/src/main/java/org/apache/iotdb/session/pool/TableSessionWrapper.java @@ -68,6 +68,19 @@ public void insert(Tablet tablet) throws StatementExecutionException, IoTDBConne } } + @Override + public void testInsert(Tablet tablet) + throws StatementExecutionException, IoTDBConnectionException { + try { + session.testInsertRelationalTablet(tablet); + } catch (IoTDBConnectionException e) { + sessionPool.cleanSessionAndMayThrowConnectionException(session); + closed.set(true); + session = null; + throw e; + } + } + @Override public SessionDataSet executeQueryStatement(String sql) throws StatementExecutionException, IoTDBConnectionException {