Skip to content

Commit c50ae86

Browse files
authored
Fix the error message when the number of columns in a Insert SQL is inconsistent (apache#16083)
1 parent 7e92466 commit c50ae86

File tree

2 files changed

+48
-2
lines changed

2 files changed

+48
-2
lines changed

integration-test/src/test/java/org/apache/iotdb/relational/it/session/IoTDBSessionRelationalIT.java

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1282,6 +1282,40 @@ public void insertWithoutFieldTest() throws IoTDBConnectionException {
12821282
}
12831283
}
12841284

1285+
@Test
1286+
public void insertWrongFieldNumTest() throws IoTDBConnectionException {
1287+
try (ITableSession session = EnvFactory.getEnv().getTableSessionConnection()) {
1288+
session.executeNonQueryStatement("USE \"db1\"");
1289+
session.executeNonQueryStatement(
1290+
"create table wrong_field_num (s1 int32, s2 int32, s3 int32)");
1291+
session.executeNonQueryStatement(
1292+
"insert into wrong_field_num(s1, s2, s3, time) values (1, 2, 3)");
1293+
fail("Exception expected");
1294+
} catch (StatementExecutionException e) {
1295+
assertEquals("701: TimeColumnIndex out of bound: 3-3", e.getMessage());
1296+
}
1297+
1298+
try (ITableSession session = EnvFactory.getEnv().getTableSessionConnection()) {
1299+
session.executeNonQueryStatement("USE \"db1\"");
1300+
session.executeNonQueryStatement(
1301+
"insert into wrong_field_num(s1, s2, time, s3) values (1, 2, 3)");
1302+
fail("Exception expected");
1303+
} catch (StatementExecutionException e) {
1304+
assertEquals(
1305+
"701: Inconsistent numbers of non-time column names and values: 3-2", e.getMessage());
1306+
}
1307+
1308+
try (ITableSession session = EnvFactory.getEnv().getTableSessionConnection()) {
1309+
session.executeNonQueryStatement("USE \"db1\"");
1310+
session.executeNonQueryStatement(
1311+
"insert into wrong_field_num(s1, s2, time) values (1, 2, 3, 4)");
1312+
fail("Exception expected");
1313+
} catch (StatementExecutionException e) {
1314+
assertEquals(
1315+
"701: Inconsistent numbers of non-time column names and values: 2-3", e.getMessage());
1316+
}
1317+
}
1318+
12851319
private void testOneCastWithTablet(
12861320
TSDataType from, TSDataType to, int testNum, boolean partialInsert)
12871321
throws IoTDBConnectionException, StatementExecutionException {

iotdb-core/datanode/src/main/java/org/apache/iotdb/db/queryengine/plan/relational/sql/parser/AstBuilder.java

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -852,7 +852,12 @@ private Node visitInsertValues(
852852
}
853853
String[] columnNameArray = columnNames.toArray(new String[0]);
854854
return toInsertRowStatement(
855-
expressions, finalTimeColumnIndex, columnNameArray, tableName, databaseName);
855+
expressions,
856+
finalTimeColumnIndex,
857+
columnNameArray,
858+
tableName,
859+
databaseName,
860+
columnNames.size());
856861
})
857862
.collect(toList());
858863

@@ -926,7 +931,8 @@ private InsertRowStatement toInsertRowStatement(
926931
int timeColumnIndex,
927932
String[] nonTimeColumnNames,
928933
String tableName,
929-
String databaseName) {
934+
String databaseName,
935+
int columnSize) {
930936
InsertRowStatement insertRowStatement = new InsertRowStatement();
931937
insertRowStatement.setWriteToTable(true);
932938
insertRowStatement.setDevicePath(new PartialPath(new String[] {tableName}));
@@ -936,6 +942,12 @@ private InsertRowStatement toInsertRowStatement(
936942
timestamp = CommonDateTimeUtils.currentTime();
937943
nonTimeColCnt = expressions.size();
938944
} else {
945+
if (timeColumnIndex >= expressions.size()) {
946+
throw new SemanticException(
947+
String.format(
948+
"TimeColumnIndex out of bound: %d-%d", timeColumnIndex, expressions.size()));
949+
}
950+
939951
Expression timeExpression = expressions.get(timeColumnIndex);
940952
if (timeExpression instanceof LongLiteral) {
941953
timestamp = ((LongLiteral) timeExpression).getParsedValue();

0 commit comments

Comments
 (0)