Skip to content
Open
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
Original file line number Diff line number Diff line change
Expand Up @@ -22,14 +22,18 @@
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;
import org.apache.iotdb.itbase.category.TableLocalStandaloneIT;
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;
Expand All @@ -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)
Expand All @@ -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"};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*
* <p>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.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -2930,6 +2931,26 @@ public void insertRelationalTablet(Tablet tablet)
}
}

/**
* Sends a relational tablet without inserting data or validating it on the server.
*
* <p>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<SessionConnection, Tablet> relationalTabletGroup = new HashMap<>();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
Loading