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 90515489a24 Support constant folding for insert values (#17850)
90515489a24 is described below
commit 90515489a24171bda15f53c9f39bbbdda549c64c
Author: Caideyipi <[email protected]>
AuthorDate: Thu Nov 12 09:40:20 2026 +0800
Support constant folding for insert values (#17850)
* Support constant folding for insert values
* Fix relational insert values IT regressions
* Optimize insert constant folding
* Fix wrong time insert IT schema
* Remove client-go submodule change
* Fix numeric predicate folding for insert values
* Fix insert values test cache initialization compatibility
* Preserve deferred insert values for prepared execution and memory
accounting
* Use exhaustive switches in insert value constant folding
---
.../relational/it/db/it/IoTDBInsertTableIT.java | 56 +++-
.../it/session/IoTDBSessionRelationalIT.java | 29 +-
.../iotdb/db/i18n/DataNodeQueryMessages.java | 33 ++
.../iotdb/db/i18n/DataNodeQueryMessages.java | 33 ++
.../plan/node/write/RelationalInsertRowsNode.java | 12 +-
.../relational/analyzer/StatementAnalyzer.java | 6 +
.../planner/IrExpressionInterpreter.java | 326 +++++++++++++------
.../plan/relational/sql/ast/InsertValues.java | 346 +++++++++++++++++++++
.../plan/relational/sql/parser/AstBuilder.java | 234 +-------------
.../plan/relational/sql/util/AstUtil.java | 83 +++++
.../relational/sql/AstMemoryEstimatorTest.java | 29 +-
.../plan/relational/sql/ast/InsertValuesTest.java | 204 ++++++++++++
12 files changed, 1041 insertions(+), 350 deletions(-)
diff --git
a/integration-test/src/test/java/org/apache/iotdb/relational/it/db/it/IoTDBInsertTableIT.java
b/integration-test/src/test/java/org/apache/iotdb/relational/it/db/it/IoTDBInsertTableIT.java
index 9605e92104e..4a36707974a 100644
---
a/integration-test/src/test/java/org/apache/iotdb/relational/it/db/it/IoTDBInsertTableIT.java
+++
b/integration-test/src/test/java/org/apache/iotdb/relational/it/db/it/IoTDBInsertTableIT.java
@@ -403,7 +403,7 @@ public class IoTDBInsertTableIT {
statement.execute(
"CREATE TABLE rejected_custom_time_expression "
+ "(device_id STRING TAG, event_time TIMESTAMP TIME, value INT64
FIELD)");
- String[] expressions = {"1735660800000 + 1000", "?", "1.5", "true",
"abs(1)"};
+ String[] expressions = {"?", "1.5", "true", "abs(1)"};
for (String expression : expressions) {
for (String columns : new String[] {" (time, device_id, value)", ""}) {
assertUnsupportedInsertExpression(
@@ -455,7 +455,10 @@ public class IoTDBInsertTableIT {
SQLException exception = assertThrows(SQLException.class, () ->
statement.execute(sql));
assertEquals(sql, TSStatusCode.SEMANTIC_ERROR.getStatusCode(),
exception.getErrorCode());
String message = exception.getMessage();
- assertTrue(message, message.contains("Unsupported expression:"));
+ assertTrue(
+ message,
+ message.contains("Unsupported expression:")
+ || message.contains("Insert expression must be constant after
constant folding:"));
assertFalse(message, message.contains("ClassCastException"));
assertFalse(message, message.contains("cannot be cast"));
assertFalse(message, message.contains("org.apache.iotdb"));
@@ -469,7 +472,12 @@ public class IoTDBInsertTableIT {
statement.execute(
"CREATE TABLE supported_time_expression (device_id STRING TAG, value
INT64 FIELD)");
String[] expressions = {
- "1735660800000", "'2025-01-01T00:00:00+08:00'", "NOW()", "NOW() + 1d",
"NOW() - 1d"
+ "1735660800000",
+ "'2025-01-01T00:00:00+08:00'",
+ "1735660799000 + 1000",
+ "NOW()",
+ "NOW() + 1d",
+ "NOW() - 1d"
};
for (boolean explicitColumns : new boolean[] {true, false}) {
for (int i = 0; i < expressions.length; i++) {
@@ -493,10 +501,10 @@ public class IoTDBInsertTableIT {
+ "'")) {
assertTrue(resultSet.next());
long timestamp = resultSet.getLong(1);
- if (i < 2) {
+ if (i < 3) {
assertEquals(1735660800000L, timestamp);
} else {
- long offset = i == 3 ? 86400000L : i == 4 ? -86400000L : 0;
+ long offset = i == 4 ? 86400000L : i == 5 ? -86400000L : 0;
// Allow clock skew between the test client and cluster nodes.
assertTrue(timestamp >= before + offset - 10000);
assertTrue(timestamp <= after + offset + 10000);
@@ -509,6 +517,44 @@ public class IoTDBInsertTableIT {
}
}
+ @Test
+ public void testInsertFoldedPredicates() throws SQLException {
+ try (Connection connection = EnvFactory.getEnv().getTableConnection();
+ Statement statement = connection.createStatement()) {
+ statement.execute("USE test");
+ statement.execute("CREATE TABLE folded_predicates (device_id STRING TAG,
value INT32 FIELD)");
+ String[] predicates = {
+ "1 + 1 IN (2, 3)",
+ "2 IN (1 + 1, 3)",
+ "1 + 1 IN (2.0, 3.0)",
+ "1.0 < (0.0 / 0.0)",
+ "(0.0 / 0.0) >= 1.0",
+ "(0.0 / 0.0) = (0.0 / 0.0)",
+ "(0.0 / 0.0) <> 1.0"
+ };
+ for (boolean explicitColumns : new boolean[] {true, false}) {
+ for (int i = 0; i < predicates.length; i++) {
+ String deviceId = "d" + explicitColumns + i;
+ statement.execute(
+ "INSERT INTO folded_predicates"
+ + (explicitColumns ? " (time, device_id, value)" : "")
+ + " VALUES (1, '"
+ + deviceId
+ + "', if("
+ + predicates[i]
+ + ", 11, 22))");
+ try (ResultSet resultSet =
+ statement.executeQuery(
+ "SELECT value FROM folded_predicates WHERE device_id = '" +
deviceId + "'")) {
+ assertTrue(resultSet.next());
+ assertEquals(predicates[i], i < 3 ? 11 : 22, resultSet.getInt(1));
+ assertFalse(resultSet.next());
+ }
+ }
+ }
+ }
+ }
+
@Test
public void testInsertNaN() throws SQLException {
try (Connection connection =
EnvFactory.getEnv().getConnection(BaseEnv.TABLE_SQL_DIALECT);
diff --git
a/integration-test/src/test/java/org/apache/iotdb/relational/it/session/IoTDBSessionRelationalIT.java
b/integration-test/src/test/java/org/apache/iotdb/relational/it/session/IoTDBSessionRelationalIT.java
index 0f1e995055b..73e9881dfca 100644
---
a/integration-test/src/test/java/org/apache/iotdb/relational/it/session/IoTDBSessionRelationalIT.java
+++
b/integration-test/src/test/java/org/apache/iotdb/relational/it/session/IoTDBSessionRelationalIT.java
@@ -272,7 +272,8 @@ public class IoTDBSessionRelationalIT {
session.executeNonQueryStatement("USE \"db1\"");
// Include a FIELD column so schema validation does not mask invalid
timestamps.
session.executeNonQueryStatement(
- "create table wrong_time(color string tag, device_id string tag,city
string attribute, value int32 field)");
+ "create table wrong_time(color string tag, device_id string tag,city
string attribute,"
+ + " value int32 field)");
try {
session.executeNonQueryStatement("insert into wrong_time
values('aa','bb','cc','dd',1)");
fail("No exception thrown");
@@ -281,11 +282,17 @@ public class IoTDBSessionRelationalIT {
"701: Input time format aa error. Input like yyyy-MM-dd HH:mm:ss,
yyyy-MM-ddTHH:mm:ss or refer to user document for more info.",
e.getMessage());
}
- try {
- session.executeNonQueryStatement("insert into wrong_time
values(1+1,'bb','cc','dd',1)");
- fail("No exception thrown");
- } catch (StatementExecutionException e) {
- assertEquals("701: Unsupported expression: (1 + 1)", e.getMessage());
+ session.executeNonQueryStatement("insert into wrong_time
values(1+1,'bb','cc','dd',1)");
+ try (SessionDataSet dataSet =
+ session.executeQueryStatement("select * from wrong_time where time =
2")) {
+ assertTrue(dataSet.hasNext());
+ RowRecord rowRecord = dataSet.next();
+ assertEquals(2L, rowRecord.getFields().get(0).getLongV());
+ assertEquals("bb",
rowRecord.getFields().get(1).getBinaryV().toString());
+ assertEquals("cc",
rowRecord.getFields().get(2).getBinaryV().toString());
+ assertEquals("dd",
rowRecord.getFields().get(3).getBinaryV().toString());
+ assertEquals(1, rowRecord.getFields().get(4).getIntV());
+ assertFalse(dataSet.hasNext());
}
try {
session.executeNonQueryStatement("insert into wrong_time
values(1.0,'bb','cc','dd',1)");
@@ -347,6 +354,14 @@ public class IoTDBSessionRelationalIT {
row, "tag:" + row, "attr:" + row, row));
}
+ session.executeNonQueryStatement(
+ "INSERT INTO table1 (time, tag1, attr1, m1) "
+ + "VALUES (if(true, 50, 0), if(true, 'tag:50', 'x'), "
+ + "if(false, 'x', 'attr:50'), if(true, 50.0, 0.0))");
+ session.executeNonQueryStatement(
+ "INSERT INTO table1 VALUES (if(true, 51, 0), if(true, 'tag:51',
'x'), "
+ + "if(true, 'attr:51', 'x'), if(false, 0, 51))");
+
SessionDataSet dataSet = session.executeQueryStatement("select * from
table1 order by time");
int cnt = 0;
while (dataSet.hasNext()) {
@@ -357,7 +372,7 @@ public class IoTDBSessionRelationalIT {
assertEquals(timestamp * 1.0,
rowRecord.getFields().get(3).getDoubleV(), 0.0001);
cnt++;
}
- assertEquals(50, cnt);
+ assertEquals(52, cnt);
// sql cannot create column
assertThrows(
diff --git
a/iotdb-core/datanode/src/main/i18n/en/org/apache/iotdb/db/i18n/DataNodeQueryMessages.java
b/iotdb-core/datanode/src/main/i18n/en/org/apache/iotdb/db/i18n/DataNodeQueryMessages.java
index 355d5a00176..df18eaa2e4d 100644
---
a/iotdb-core/datanode/src/main/i18n/en/org/apache/iotdb/db/i18n/DataNodeQueryMessages.java
+++
b/iotdb-core/datanode/src/main/i18n/en/org/apache/iotdb/db/i18n/DataNodeQueryMessages.java
@@ -4144,6 +4144,39 @@ public final class DataNodeQueryMessages {
public static final String MESSAGE_TAGCOLUMNS_ARG_6520A0C2 = "TagColumns:
%s";
public static final String MESSAGE_MEMORYTHRESHOLD_ARG_3676AB46 =
"MemoryThreshold: %s";
+
+ public static final String EXCEPTION_DATABASENAME_IS_NULL_A811DE1B =
+ "databaseName is null";
+
+ public static final String EXCEPTION_VALUES_IS_NULL_A580EDAA =
+ "values is null";
+
+ public static final String EXCEPTION_ZONEID_IS_NULL_7DAA5E2C =
+ "zoneId is null";
+
+ public static final String
EXCEPTION_INSERT_TARGET_TABLE_DOES_NOT_EXIST_ARG_ARG_78F05787 =
+ "Insert target table does not exist: %s.%s";
+
+ public static final String
EXCEPTION_EXPRESSIONS_AND_COLUMNS_DO_NOT_MATCH_EXPRESSIONS_SIZE_ARG_COLUMNS_SIZE_ARG_F933A8C1
=
+ "expressions and columns do not match, expressions size: %s, columns
size: %s";
+
+ public static final String
EXCEPTION_INSERT_EXPRESSION_MUST_BE_CONSTANT_AFTER_CONSTANT_FOLDING_ARG_FOLDED_TO_ARG_D9EE1433
=
+ "Insert expression must be constant after constant folding: %s (folded
to %s)";
+
+ public static final String
EXCEPTION_CANNOT_EVALUATE_INSERT_EXPRESSION_AS_A_CONSTANT_ARG_ARG_F478C134 =
+ "Cannot evaluate insert expression as a constant: %s. %s";
+
+ public static final String EXCEPTION_THE_ARG_IS_OUT_OF_RANGE_OF_INT_F8E58471
=
+ "The %s is out of range of int.";
+
+ public static final String
EXCEPTION_THE_ARG_IS_OUT_OF_RANGE_OF_LONG_9B1C872A =
+ "The %s is out of range of long.";
+
+ public static final String EXCEPTION_INT_OVERFLOW_ARG_ARG_BB287017 =
+ "int overflow: %s / %s";
+
+ public static final String EXCEPTION_LONG_OVERFLOW_ARG_ARG_46CC95E2 =
+ "long overflow: %s / %s";
public static final String
EXCEPTION_CHUNKDATA_CONTAINS_NO_PHYSICAL_CHUNK_607BCFC8 =
"ChunkData contains no physical chunk";
public static final String EXCEPTION_DEVICE_CANNOT_BE_NULL_F1EB20B6 =
"device cannot be null";
diff --git
a/iotdb-core/datanode/src/main/i18n/zh/org/apache/iotdb/db/i18n/DataNodeQueryMessages.java
b/iotdb-core/datanode/src/main/i18n/zh/org/apache/iotdb/db/i18n/DataNodeQueryMessages.java
index bc8b004bbec..b2514babdfc 100644
---
a/iotdb-core/datanode/src/main/i18n/zh/org/apache/iotdb/db/i18n/DataNodeQueryMessages.java
+++
b/iotdb-core/datanode/src/main/i18n/zh/org/apache/iotdb/db/i18n/DataNodeQueryMessages.java
@@ -4896,6 +4896,39 @@ public final class DataNodeQueryMessages {
public static final String MESSAGE_TAGCOLUMNS_ARG_6520A0C2 = "TAG 列:%s";
public static final String MESSAGE_MEMORYTHRESHOLD_ARG_3676AB46 = "内存阈值:%s";
+
+ public static final String EXCEPTION_DATABASENAME_IS_NULL_A811DE1B =
+ "databaseName 为空";
+
+ public static final String EXCEPTION_VALUES_IS_NULL_A580EDAA =
+ "values 为空";
+
+ public static final String EXCEPTION_ZONEID_IS_NULL_7DAA5E2C =
+ "zoneId 为空";
+
+ public static final String
EXCEPTION_INSERT_TARGET_TABLE_DOES_NOT_EXIST_ARG_ARG_78F05787 =
+ "写入目标表不存在:%s.%s";
+
+ public static final String
EXCEPTION_EXPRESSIONS_AND_COLUMNS_DO_NOT_MATCH_EXPRESSIONS_SIZE_ARG_COLUMNS_SIZE_ARG_F933A8C1
=
+ "表达式与列不匹配,表达式数量:%s,列数量:%s";
+
+ public static final String
EXCEPTION_INSERT_EXPRESSION_MUST_BE_CONSTANT_AFTER_CONSTANT_FOLDING_ARG_FOLDED_TO_ARG_D9EE1433
=
+ "写入表达式在常量折叠后必须为常量:%s(折叠结果为 %s)";
+
+ public static final String
EXCEPTION_CANNOT_EVALUATE_INSERT_EXPRESSION_AS_A_CONSTANT_ARG_ARG_F478C134 =
+ "无法将写入表达式计算为常量:%s。%s";
+
+ public static final String EXCEPTION_THE_ARG_IS_OUT_OF_RANGE_OF_INT_F8E58471
=
+ "%s 超出 int 的范围。";
+
+ public static final String
EXCEPTION_THE_ARG_IS_OUT_OF_RANGE_OF_LONG_9B1C872A =
+ "%s 超出 long 的范围。";
+
+ public static final String EXCEPTION_INT_OVERFLOW_ARG_ARG_BB287017 =
+ "int 溢出:%s / %s";
+
+ public static final String EXCEPTION_LONG_OVERFLOW_ARG_ARG_46CC95E2 =
+ "long 溢出:%s / %s";
public static final String
EXCEPTION_CHUNKDATA_CONTAINS_NO_PHYSICAL_CHUNK_607BCFC8 =
"ChunkData 中不包含任何物理 chunk";
public static final String EXCEPTION_DEVICE_CANNOT_BE_NULL_F1EB20B6 =
"device 不能为 null";
diff --git
a/iotdb-core/datanode/src/main/java/org/apache/iotdb/db/queryengine/plan/planner/plan/node/write/RelationalInsertRowsNode.java
b/iotdb-core/datanode/src/main/java/org/apache/iotdb/db/queryengine/plan/planner/plan/node/write/RelationalInsertRowsNode.java
index 5fff5ad4409..bd5b7b2c169 100644
---
a/iotdb-core/datanode/src/main/java/org/apache/iotdb/db/queryengine/plan/planner/plan/node/write/RelationalInsertRowsNode.java
+++
b/iotdb-core/datanode/src/main/java/org/apache/iotdb/db/queryengine/plan/planner/plan/node/write/RelationalInsertRowsNode.java
@@ -197,17 +197,17 @@ public class RelationalInsertRowsNode extends
InsertRowsNode {
List<WritePlanNode> writePlanNodeList = new ArrayList<>();
Map<TRegionReplicaSet, RelationalInsertRowsNode> splitMap = new
HashMap<>();
List<TDataNodeLocation> redirectInfo = new ArrayList<>();
+ String databaseName = getDatabaseName(analysis);
for (int i = 0; i < getInsertRowNodeList().size(); i++) {
InsertRowNode insertRowNode = getInsertRowNodeList().get(i);
// Data region for insert row node
- // each row may belong to different database, pass null for
auto-detection
TRegionReplicaSet dataRegionReplicaSet =
analysis
.getDataPartitionInfo()
.getDataRegionReplicaSetForWriting(
insertRowNode.getDeviceID(),
TimePartitionUtils.getTimePartitionSlot(insertRowNode.getTime()),
- analysis.getDatabaseName());
+ databaseName);
// Collect redirectInfo
redirectInfo.add(dataRegionReplicaSet.getDataNodeLocations().get(0));
@@ -230,6 +230,14 @@ public class RelationalInsertRowsNode extends
InsertRowsNode {
return writePlanNodeList;
}
+ private String getDatabaseName(IAnalysis analysis) {
+ if (analysis.getDataPartitionInfo() != null
+ && analysis.getDataPartitionInfo().getDataPartitionMap().size() == 1) {
+ return
analysis.getDataPartitionInfo().getDataPartitionMap().keySet().iterator().next();
+ }
+ return analysis.getDatabaseName();
+ }
+
public RelationalInsertRowsNode emptyClone() {
return new RelationalInsertRowsNode(this.getPlanNodeId());
}
diff --git
a/iotdb-core/datanode/src/main/java/org/apache/iotdb/db/queryengine/plan/relational/analyzer/StatementAnalyzer.java
b/iotdb-core/datanode/src/main/java/org/apache/iotdb/db/queryengine/plan/relational/analyzer/StatementAnalyzer.java
index 9e73c44952d..6d06b3b5351 100644
---
a/iotdb-core/datanode/src/main/java/org/apache/iotdb/db/queryengine/plan/relational/analyzer/StatementAnalyzer.java
+++
b/iotdb-core/datanode/src/main/java/org/apache/iotdb/db/queryengine/plan/relational/analyzer/StatementAnalyzer.java
@@ -191,6 +191,7 @@ import
org.apache.iotdb.db.queryengine.plan.relational.sql.ast.Insert;
import org.apache.iotdb.db.queryengine.plan.relational.sql.ast.InsertRow;
import org.apache.iotdb.db.queryengine.plan.relational.sql.ast.InsertRows;
import org.apache.iotdb.db.queryengine.plan.relational.sql.ast.InsertTablet;
+import org.apache.iotdb.db.queryengine.plan.relational.sql.ast.InsertValues;
import org.apache.iotdb.db.queryengine.plan.relational.sql.ast.LoadTsFile;
import org.apache.iotdb.db.queryengine.plan.relational.sql.ast.PipeEnriched;
import org.apache.iotdb.db.queryengine.plan.relational.sql.ast.Property;
@@ -1386,6 +1387,11 @@ public class StatementAnalyzer {
}
private Scope visitInsert(WrappedInsertStatement insert, Optional<Scope>
scope) {
+ if (insert instanceof InsertValues) {
+ ((InsertValues) insert)
+ .materialize(new PlannerContext(metadata, typeManager),
sessionContext);
+ }
+
final Scope ret = Scope.create();
final MPPQueryContext context = insert.getContext();
diff --git
a/iotdb-core/datanode/src/main/java/org/apache/iotdb/db/queryengine/plan/relational/planner/IrExpressionInterpreter.java
b/iotdb-core/datanode/src/main/java/org/apache/iotdb/db/queryengine/plan/relational/planner/IrExpressionInterpreter.java
index 464c18019bd..2cae6ccbdf1 100644
---
a/iotdb-core/datanode/src/main/java/org/apache/iotdb/db/queryengine/plan/relational/planner/IrExpressionInterpreter.java
+++
b/iotdb-core/datanode/src/main/java/org/apache/iotdb/db/queryengine/plan/relational/planner/IrExpressionInterpreter.java
@@ -73,7 +73,6 @@ import java.util.stream.Stream;
import static com.google.common.base.Preconditions.checkState;
import static com.google.common.base.Predicates.instanceOf;
-import static com.google.common.base.Throwables.throwIfInstanceOf;
import static com.google.common.base.Verify.verify;
import static com.google.common.collect.ImmutableList.toImmutableList;
import static com.google.common.collect.Iterables.getOnlyElement;
@@ -84,6 +83,11 @@ import static
org.apache.iotdb.commons.queryengine.plan.relational.sql.ast.Arith
import static
org.apache.iotdb.commons.queryengine.plan.relational.type.TypeSignatureTranslator.toTypeSignature;
import static
org.apache.iotdb.db.queryengine.plan.relational.planner.ir.DeterminismEvaluator.isDeterministic;
import static
org.apache.iotdb.db.queryengine.plan.relational.planner.ir.IrUtils.isEffectivelyLiteral;
+import static org.apache.tsfile.read.common.type.DoubleType.DOUBLE;
+import static org.apache.tsfile.read.common.type.FloatType.FLOAT;
+import static org.apache.tsfile.read.common.type.IntType.INT32;
+import static org.apache.tsfile.read.common.type.LongType.INT64;
+import static org.apache.tsfile.read.common.type.TimestampType.TIMESTAMP;
public class IrExpressionInterpreter {
@@ -422,17 +426,21 @@ public class IrExpressionInterpreter {
// optimization
if (!inListCache.containsKey(valueList)) {
if
(valueList.getValues().stream().allMatch(Literal.class::isInstance)
- &&
valueList.getValues().stream().noneMatch(NullLiteral.class::isInstance)) {
+ &&
valueList.getValues().stream().noneMatch(NullLiteral.class::isInstance)
+ && valueList.getValues().stream()
+ .allMatch(item -> type(item).equals(type(node.getValue()))))
{
set =
valueList.getValues().stream()
.map(expr -> processWithExceptionHandling(expr, context))
+ // INT32 literals and folded arithmetic may use different
Java containers.
+ .map(item -> normalizeArithmeticValue(item,
type(node.getValue())))
.collect(Collectors.toSet());
}
inListCache.put(valueList, set);
}
if (set != null) {
- return set.contains(value);
+ return set.contains(normalizeArithmeticValue(value,
type(node.getValue())));
}
}
@@ -467,7 +475,11 @@ public class IrExpressionInterpreter {
if (inValue == null) {
hasNullValue = true;
} else {
- Boolean result = value.equals(inValue);
+ Boolean result =
+ value instanceof Number && inValue instanceof Number
+ ? evaluateNumericComparison(
+ ComparisonExpression.Operator.EQUAL, (Number) value,
(Number) inValue)
+ : value.equals(inValue);
if (result == null) {
hasNullValue = true;
} else if (!found && result) {
@@ -528,23 +540,13 @@ public class IrExpressionInterpreter {
}
if (node.getSign() == PLUS) {
- return value;
+ return normalizeArithmeticValue(value, type(node));
} else {
- try {
- Expression valueExpression = toExpression(value,
type(node.getValue()));
- if (valueExpression instanceof ArithmeticUnaryExpression
- && ((ArithmeticUnaryExpression)
valueExpression).getSign().equals(MINUS)) {
- return ((ArithmeticUnaryExpression) valueExpression).getValue();
- }
- return new ArithmeticUnaryExpression(MINUS, valueExpression);
- // TODO use the following after we implement
InterpretedFunctionInvoker
- // return invokeOperator(OperatorType.NEGATION,
types(node.getValue()),
- // Collections.singletonList(value));
- } catch (Throwable throwable) {
- throwIfInstanceOf(throwable, RuntimeException.class);
- throwIfInstanceOf(throwable, Error.class);
- throw new RuntimeException(throwable.getMessage(), throwable);
+ Type returnType = type(node);
+ if (isSupportedArithmeticUnaryType(returnType)) {
+ return evaluateArithmeticNegation(value, returnType);
}
+ return new ArithmeticUnaryExpression(MINUS, toExpression(value,
type(node.getValue())));
}
}
@@ -564,16 +566,21 @@ public class IrExpressionInterpreter {
node.getOperator(),
toExpression(left, type(node.getLeft())),
toExpression(right, type(node.getRight())));
- } else {
- return new ArithmeticBinaryExpression(
- node.getOperator(),
- toExpression(left, type(node.getLeft())),
- toExpression(right, type(node.getRight())));
- // TODO use the following after we implement InterpretedFunctionInvoker
- // return
invokeOperator(OperatorType.valueOf(node.getOperator().name()),
- // types(node.getLeft(), node.getRight()),
- // ImmutableList.of(left, right));
}
+
+ Type leftType = type(node.getLeft());
+ Type rightType = type(node.getRight());
+ Type returnType = type(node);
+ if (isSupportedArithmeticBinaryType(leftType)
+ && isSupportedArithmeticBinaryType(rightType)
+ && isSupportedArithmeticBinaryType(returnType)) {
+ return evaluateArithmeticBinary(node.getOperator(), left, leftType,
right, returnType);
+ }
+
+ return new ArithmeticBinaryExpression(
+ node.getOperator(),
+ toExpression(left, type(node.getLeft())),
+ toExpression(right, type(node.getRight())));
}
@Override
@@ -585,19 +592,8 @@ public class IrExpressionInterpreter {
if (operator == ComparisonExpression.Operator.IS_DISTINCT_FROM) {
return processIsDistinctFrom(context, left, right);
}
- // Execution engine does not have not equal and greater than operators,
so interpret with
- // equal or less than, but do not flip operator in result, as many
optimizers depend on
- // operators not flipping
- if (node.getOperator() == ComparisonExpression.Operator.NOT_EQUAL) {
- Object result = visitComparisonExpression(flipComparison(node),
context);
- if (result == null) {
- return null;
- }
- if (result instanceof ComparisonExpression) {
- return flipComparison((ComparisonExpression) result);
- }
- return !(Boolean) result;
- }
+ // Interpret greater-than comparisons with operands swapped, but
preserve the original
+ // operator in unresolved results, as many optimizers depend on
operators not flipping.
if (node.getOperator() == ComparisonExpression.Operator.GREATER_THAN
|| node.getOperator() ==
ComparisonExpression.Operator.GREATER_THAN_OR_EQUAL) {
Object result = visitComparisonExpression(flipComparison(node),
context);
@@ -657,67 +653,12 @@ public class IrExpressionInterpreter {
toExpression(left, type(leftExpression)),
toExpression(right, type(rightExpression)));
} else {
- if (!(left instanceof Number) || !(right instanceof Number)) {
- throw new IllegalArgumentException(
- DataNodeQueryMessages.BOTH_OBJECT_MUST_BE_TYPE_OF_NUMBER);
- }
-
- if (left instanceof Integer && right instanceof Integer) {
- Integer leftNum = (Integer) left;
- Integer rightNum = (Integer) right;
- if (operator == ComparisonExpression.Operator.LESS_THAN) {
- return leftNum < rightNum;
- } else if (operator ==
ComparisonExpression.Operator.LESS_THAN_OR_EQUAL) {
- return leftNum <= rightNum;
- } else if (operator == ComparisonExpression.Operator.EQUAL) {
- return leftNum.equals(rightNum);
- }
- }
-
- if (left instanceof Long && right instanceof Long) {
- Long leftNum = (Long) left;
- Long rightNum = (Long) right;
- if (operator == ComparisonExpression.Operator.LESS_THAN) {
- return leftNum < rightNum;
- } else if (operator ==
ComparisonExpression.Operator.LESS_THAN_OR_EQUAL) {
- return leftNum <= rightNum;
- } else if (operator == ComparisonExpression.Operator.EQUAL) {
- return leftNum.equals(rightNum);
- }
+ if (left instanceof Number && right instanceof Number) {
+ return evaluateNumericComparison(operator, (Number) left, (Number)
right);
}
- if (left instanceof Float && right instanceof Float) {
- Float leftNum = (Float) left;
- Float rightNum = (Float) right;
- if (operator == ComparisonExpression.Operator.LESS_THAN) {
- return leftNum < rightNum;
- } else if (operator ==
ComparisonExpression.Operator.LESS_THAN_OR_EQUAL) {
- return leftNum <= rightNum;
- } else if (operator == ComparisonExpression.Operator.EQUAL) {
- return leftNum.equals(rightNum);
- }
- }
-
- if (left instanceof Double && right instanceof Double) {
- Double leftNum = (Double) left;
- Double rightNum = (Double) right;
- if (operator == ComparisonExpression.Operator.LESS_THAN) {
- return leftNum < rightNum;
- } else if (operator ==
ComparisonExpression.Operator.LESS_THAN_OR_EQUAL) {
- return leftNum <= rightNum;
- } else if (operator == ComparisonExpression.Operator.EQUAL) {
- return leftNum.equals(rightNum);
- }
- }
-
- return new ComparisonExpression(
- operator,
- toExpression(left, type(leftExpression)),
- toExpression(right, type(rightExpression)));
- // TODO use the following after we implement InterpretedFunctionInvoker
- // return invokeOperator(OperatorType.valueOf(operator.name()),
types(leftExpression,
- // rightExpression),
- // ImmutableList.of(left, right));
+ throw new IllegalArgumentException(
+ DataNodeQueryMessages.BOTH_OBJECT_MUST_BE_TYPE_OF_NUMBER);
}
}
@@ -981,6 +922,191 @@ public class IrExpressionInterpreter {
return values.stream().anyMatch(instanceOf(Expression.class));
}
+ private boolean isSupportedArithmeticBinaryType(Type type) {
+ return type.equals(INT32) || type.equals(INT64) || type.equals(FLOAT) ||
type.equals(DOUBLE);
+ }
+
+ private boolean isSupportedArithmeticUnaryType(Type type) {
+ return isSupportedArithmeticBinaryType(type) || type.equals(TIMESTAMP);
+ }
+
+ private Object evaluateArithmeticNegation(Object value, Type returnType) {
+ if (returnType.equals(INT32)) {
+ int intValue = toInt(value);
+ if (intValue == Integer.MIN_VALUE) {
+ throw new SemanticException(
+ String.format(
+
DataNodeQueryMessages.EXCEPTION_THE_ARG_IS_OUT_OF_RANGE_OF_INT_F8E58471,
+ intValue));
+ }
+ return -intValue;
+ }
+ if (returnType.equals(INT64) || returnType.equals(TIMESTAMP)) {
+ long longValue = toLong(value);
+ if (longValue == Long.MIN_VALUE) {
+ throw new SemanticException(
+ String.format(
+
DataNodeQueryMessages.EXCEPTION_THE_ARG_IS_OUT_OF_RANGE_OF_LONG_9B1C872A,
+ longValue));
+ }
+ return -longValue;
+ }
+ if (returnType.equals(FLOAT)) {
+ return -toFloat(value);
+ }
+ if (returnType.equals(DOUBLE)) {
+ return -toDouble(value);
+ }
+ throw new SemanticException(DataNodeQueryMessages.UNKNOWN_TYPE_2 +
returnType);
+ }
+
+ private Object evaluateArithmeticBinary(
+ ArithmeticBinaryExpression.Operator operator,
+ Object left,
+ Type leftType,
+ Object right,
+ Type returnType) {
+ if (returnType.equals(INT32)) {
+ return evaluateIntArithmetic(operator, toInt(left), toInt(right));
+ }
+ if (returnType.equals(INT64)) {
+ return evaluateLongArithmetic(operator, toLong(left), leftType,
toLong(right));
+ }
+ if (returnType.equals(FLOAT)) {
+ return evaluateFloatArithmetic(operator, toFloat(left),
toFloat(right));
+ }
+ if (returnType.equals(DOUBLE)) {
+ return evaluateDoubleArithmetic(operator, toDouble(left),
toDouble(right));
+ }
+ throw new SemanticException(DataNodeQueryMessages.UNKNOWN_TYPE_2 +
returnType);
+ }
+
+ private int evaluateIntArithmetic(
+ ArithmeticBinaryExpression.Operator operator, int left, int right) {
+ try {
+ return switch (operator) {
+ case ADD -> Math.addExact(left, right);
+ case SUBTRACT -> Math.subtractExact(left, right);
+ case MULTIPLY -> Math.multiplyExact(left, right);
+ case DIVIDE -> {
+ if (left == Integer.MIN_VALUE && right == -1) {
+ throw new SemanticException(
+ String.format(
+
DataNodeQueryMessages.EXCEPTION_INT_OVERFLOW_ARG_ARG_BB287017, left, right));
+ }
+ yield left / right;
+ }
+ case MODULUS -> left % right;
+ };
+ } catch (ArithmeticException e) {
+ throw new SemanticException(e.getMessage());
+ }
+ }
+
+ private long evaluateLongArithmetic(
+ ArithmeticBinaryExpression.Operator operator, long left, Type
leftType, long right) {
+ try {
+ return switch (operator) {
+ case ADD -> Math.addExact(left, right);
+ case SUBTRACT -> Math.subtractExact(left, right);
+ case MULTIPLY -> Math.multiplyExact(left, right);
+ case DIVIDE -> {
+ if ((leftType.equals(INT32) && left == Integer.MIN_VALUE && right
== -1)
+ || (leftType.equals(INT64) && left == Long.MIN_VALUE && right
== -1)) {
+ throw new SemanticException(
+ String.format(
+
DataNodeQueryMessages.EXCEPTION_LONG_OVERFLOW_ARG_ARG_46CC95E2, left, right));
+ }
+ yield left / right;
+ }
+ case MODULUS -> left % right;
+ };
+ } catch (ArithmeticException e) {
+ throw new SemanticException(e.getMessage());
+ }
+ }
+
+ private float evaluateFloatArithmetic(
+ ArithmeticBinaryExpression.Operator operator, float left, float right)
{
+ return switch (operator) {
+ case ADD -> left + right;
+ case SUBTRACT -> left - right;
+ case MULTIPLY -> left * right;
+ case DIVIDE -> left / right;
+ case MODULUS -> left % right;
+ };
+ }
+
+ private double evaluateDoubleArithmetic(
+ ArithmeticBinaryExpression.Operator operator, double left, double
right) {
+ return switch (operator) {
+ case ADD -> left + right;
+ case SUBTRACT -> left - right;
+ case MULTIPLY -> left * right;
+ case DIVIDE -> left / right;
+ case MODULUS -> left % right;
+ };
+ }
+
+ private Object normalizeArithmeticValue(Object value, Type type) {
+ if (type.equals(INT32)) {
+ return toInt(value);
+ }
+ if (type.equals(INT64) || type.equals(TIMESTAMP)) {
+ return toLong(value);
+ }
+ if (type.equals(FLOAT)) {
+ return toFloat(value);
+ }
+ if (type.equals(DOUBLE)) {
+ return toDouble(value);
+ }
+ return value;
+ }
+
+ private Boolean evaluateNumericComparison(
+ ComparisonExpression.Operator operator, Number left, Number right) {
+ if (Double.isNaN(left.doubleValue()) ||
Double.isNaN(right.doubleValue())) {
+ return false;
+ }
+ int comparison =
+ (left instanceof Float
+ || left instanceof Double
+ || right instanceof Float
+ || right instanceof Double)
+ ? Double.compare(left.doubleValue(), right.doubleValue())
+ : Long.compare(left.longValue(), right.longValue());
+ switch (operator) {
+ case LESS_THAN:
+ return comparison < 0;
+ case LESS_THAN_OR_EQUAL:
+ return comparison <= 0;
+ case EQUAL:
+ return comparison == 0;
+ case NOT_EQUAL:
+ return comparison != 0;
+ default:
+ throw new UnsupportedOperationException(
+ DataNodeQueryMessages.UNSUPPORTED_EXPRESSION + operator);
+ }
+ }
+
+ private int toInt(Object value) {
+ return ((Number) value).intValue();
+ }
+
+ private long toLong(Object value) {
+ return ((Number) value).longValue();
+ }
+
+ private float toFloat(Object value) {
+ return ((Number) value).floatValue();
+ }
+
+ private double toDouble(Object value) {
+ return ((Number) value).doubleValue();
+ }
+
private Object invokeOperator(
OperatorType operatorType,
List<? extends Type> argumentTypes,
diff --git
a/iotdb-core/datanode/src/main/java/org/apache/iotdb/db/queryengine/plan/relational/sql/ast/InsertValues.java
b/iotdb-core/datanode/src/main/java/org/apache/iotdb/db/queryengine/plan/relational/sql/ast/InsertValues.java
new file mode 100644
index 00000000000..3966a4ada29
--- /dev/null
+++
b/iotdb-core/datanode/src/main/java/org/apache/iotdb/db/queryengine/plan/relational/sql/ast/InsertValues.java
@@ -0,0 +1,346 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+package org.apache.iotdb.db.queryengine.plan.relational.sql.ast;
+
+import org.apache.iotdb.calc.exception.QueryProcessException;
+import org.apache.iotdb.commons.exception.SemanticException;
+import org.apache.iotdb.commons.path.PartialPath;
+import org.apache.iotdb.commons.queryengine.common.SessionInfo;
+import org.apache.iotdb.commons.queryengine.plan.relational.sql.ast.Expression;
+import org.apache.iotdb.commons.queryengine.plan.relational.sql.ast.Identifier;
+import org.apache.iotdb.commons.queryengine.plan.relational.sql.ast.Row;
+import org.apache.iotdb.commons.queryengine.plan.relational.sql.ast.Values;
+import org.apache.iotdb.commons.queryengine.utils.TimestampPrecisionUtils;
+import org.apache.iotdb.commons.schema.table.TsTable;
+import org.apache.iotdb.commons.schema.table.column.TsTableColumnCategory;
+import org.apache.iotdb.commons.schema.table.column.TsTableColumnSchema;
+import org.apache.iotdb.commons.utils.CommonDateTimeUtils;
+import org.apache.iotdb.db.i18n.DataNodeQueryMessages;
+import org.apache.iotdb.db.queryengine.plan.relational.planner.PlannerContext;
+import org.apache.iotdb.db.queryengine.plan.relational.sql.util.AstUtil;
+import org.apache.iotdb.db.queryengine.plan.statement.crud.InsertRowStatement;
+import org.apache.iotdb.db.queryengine.plan.statement.crud.InsertRowsStatement;
+import org.apache.iotdb.db.schemaengine.table.DataNodeTableCache;
+
+import org.apache.tsfile.enums.TSDataType;
+import org.apache.tsfile.utils.RamUsageEstimator;
+import org.apache.tsfile.write.schema.MeasurementSchema;
+
+import javax.annotation.Nullable;
+
+import java.time.ZoneId;
+import java.util.ArrayList;
+import java.util.Collections;
+import java.util.List;
+import java.util.stream.Collectors;
+
+import static java.util.Objects.requireNonNull;
+import static
org.apache.iotdb.commons.queryengine.plan.relational.sql.ast.AstMemoryEstimationHelper.getEstimatedSizeOfNodeList;
+import static
org.apache.iotdb.commons.queryengine.plan.relational.sql.ast.AstMemoryEstimationHelper.getEstimatedSizeOfStringList;
+import static
org.apache.iotdb.commons.schema.table.column.TsTableColumnCategory.TIME;
+
+public class InsertValues extends InsertRows {
+
+ private static final long INSTANCE_SIZE =
+ RamUsageEstimator.shallowSizeOfInstance(InsertValues.class);
+
+ private final String databaseName;
+ private final String tableName;
+ @Nullable private final List<String> columnNames;
+ private final List<List<Expression>> rows;
+ private final ZoneId zoneId;
+ private boolean materialized;
+
+ public InsertValues(
+ final String databaseName,
+ final String tableName,
+ @Nullable final List<Identifier> columnIdentifiers,
+ final Values values,
+ final ZoneId zoneId) {
+ super(createPlaceholderStatement(databaseName), null);
+ this.databaseName =
+ requireNonNull(databaseName,
DataNodeQueryMessages.EXCEPTION_DATABASENAME_IS_NULL_A811DE1B);
+ this.tableName =
+ requireNonNull(tableName,
DataNodeQueryMessages.EXCEPTION_TABLENAME_IS_NULL_20708596);
+ this.columnNames =
+ columnIdentifiers == null
+ ? null
+ :
columnIdentifiers.stream().map(Identifier::getValue).collect(Collectors.toList());
+ this.rows =
+ requireNonNull(values,
DataNodeQueryMessages.EXCEPTION_VALUES_IS_NULL_A580EDAA)
+ .getRows()
+ .stream()
+ .map(InsertValues::normalizeRow)
+ .collect(Collectors.toList());
+ this.zoneId = requireNonNull(zoneId,
DataNodeQueryMessages.EXCEPTION_ZONEID_IS_NULL_7DAA5E2C);
+ }
+
+ private InsertValues(InsertValues original) {
+ super(createPlaceholderStatement(original.databaseName), null);
+ this.databaseName = original.databaseName;
+ this.tableName = original.tableName;
+ this.columnNames = original.columnNames;
+ this.rows = original.rows;
+ this.zoneId = original.zoneId;
+ }
+
+ @Override
+ public InsertValues copyForPreparedExecution() {
+ // Only the immutable parsed expressions are shared. Each execution
materializes fresh rows.
+ return new InsertValues(this);
+ }
+
+ @Override
+ public long ramBytesUsed() {
+ long size =
+ INSTANCE_SIZE
+ + getInnerTreeStatement().ramBytesUsed()
+ + RamUsageEstimator.sizeOf(databaseName)
+ + RamUsageEstimator.sizeOf(tableName)
+ + getEstimatedSizeOfStringList(columnNames)
+ + RamUsageEstimator.shallowSizeOf(rows);
+ for (List<Expression> row : rows) {
+ size += getEstimatedSizeOfNodeList(row);
+ }
+ return size;
+ }
+
+ public void materialize(PlannerContext plannerContext, SessionInfo session) {
+ if (materialized) {
+ return;
+ }
+
+ InsertRowsStatement insertRowsStatement = new InsertRowsStatement();
+ insertRowsStatement.setInsertRowStatementList(
+ columnNames == null
+ ? toInsertRowStatementsWithTableSchema(plannerContext, session)
+ : toInsertRowStatementsWithColumns(plannerContext, session));
+ insertRowsStatement.setWriteToTable(true);
+ // All rows of an INSERT VALUES statement share the same table and columns.
+ insertRowsStatement.setUniformTargets(true);
+ insertRowsStatement.setDatabaseName(databaseName);
+ setInnerTreeStatement(insertRowsStatement);
+ materialized = true;
+ }
+
+ private static InsertRowsStatement createPlaceholderStatement(String
databaseName) {
+ InsertRowsStatement statement = new InsertRowsStatement();
+ statement.setInsertRowStatementList(Collections.emptyList());
+ statement.setWriteToTable(true);
+ statement.setDatabaseName(databaseName);
+ return statement;
+ }
+
+ @Override
+ public String getDatabase() {
+ return databaseName;
+ }
+
+ private static List<Expression> normalizeRow(Expression row) {
+ if (row instanceof Row) {
+ return ((Row) row).getItems();
+ }
+ return Collections.singletonList(row);
+ }
+
+ private List<InsertRowStatement> toInsertRowStatementsWithTableSchema(
+ PlannerContext plannerContext, SessionInfo session) {
+ final TsTable table = getTable();
+ return rows.stream()
+ .map(row -> toInsertRowStatement(row, table, plannerContext, session))
+ .collect(Collectors.toList());
+ }
+
+ private List<InsertRowStatement> toInsertRowStatementsWithColumns(
+ PlannerContext plannerContext, SessionInfo session) {
+ final List<String> nonTimeColumnNames = new ArrayList<>(columnNames);
+ final int timeColumnIndex = findTimeColumnIndex(nonTimeColumnNames);
+ if (timeColumnIndex != -1) {
+ nonTimeColumnNames.remove(timeColumnIndex);
+ }
+ if (timeColumnIndex == -1 && rows.size() > 1) {
+ throw new
SemanticException(DataNodeQueryMessages.NEED_TIMESTAMPS_WHEN_INSERT_MULTI_ROWS);
+ }
+
+ final String[] nonTimeColumnArray = nonTimeColumnNames.toArray(new
String[0]);
+ return rows.stream()
+ .map(
+ row ->
+ toInsertRowStatement(
+ row, timeColumnIndex, nonTimeColumnArray.clone(),
plannerContext, session))
+ .collect(Collectors.toList());
+ }
+
+ private TsTable getTable() {
+ TsTable table = DataNodeTableCache.getInstance().getTable(databaseName,
tableName);
+ if (table == null) {
+ throw new SemanticException(
+ String.format(
+
DataNodeQueryMessages.EXCEPTION_INSERT_TARGET_TABLE_DOES_NOT_EXIST_ARG_ARG_78F05787,
+ databaseName,
+ tableName));
+ }
+ return table;
+ }
+
+ private int findTimeColumnIndex(final List<String> columnNames) {
+ List<TsTableColumnSchema> timeColumnCandidates =
+ getTable().getColumnList().stream()
+ .filter(column -> column.getColumnCategory() == TIME)
+ .collect(Collectors.toList());
+ if (timeColumnCandidates.size() != 1) {
+ throw new SemanticException(
+ DataNodeQueryMessages.THE_TABLE_SHOULD_ONLY_HAVE_ONE_COLUMN_FOUND);
+ }
+
+ int timeColumnIndex = -1;
+ String timeColumnName = timeColumnCandidates.get(0).getColumnName();
+ for (int i = 0; i < columnNames.size(); i++) {
+ if (timeColumnName.equalsIgnoreCase(columnNames.get(i))) {
+ if (timeColumnIndex == -1) {
+ timeColumnIndex = i;
+ } else {
+ throw new SemanticException(
+ DataNodeQueryMessages.ONE_ROW_SHOULD_ONLY_HAVE_ONE_TIME_VALUE);
+ }
+ }
+ }
+ return timeColumnIndex;
+ }
+
+ private InsertRowStatement toInsertRowStatement(
+ List<Expression> expressions,
+ TsTable table,
+ PlannerContext plannerContext,
+ SessionInfo session) {
+ InsertRowStatement insertRowStatement = new InsertRowStatement();
+ insertRowStatement.setWriteToTable(true);
+ insertRowStatement.setDevicePath(new PartialPath(new String[]
{table.getTableName()}));
+
+ List<TsTableColumnSchema> columnList = table.getColumnList();
+ if (expressions.size() != columnList.size()) {
+ throw new SemanticException(
+ String.format(
+ DataNodeQueryMessages
+
.EXCEPTION_EXPRESSIONS_AND_COLUMNS_DO_NOT_MATCH_EXPRESSIONS_SIZE_ARG_COLUMNS_SIZE_ARG_F933A8C1,
+ expressions.size(),
+ columnList.size()));
+ }
+
+ String[] nonTimeColumnNames = new String[columnList.size() - 1];
+ Object[] nonTimeValues = new Object[columnList.size() - 1];
+ TsTableColumnCategory[] nonTimeColumnCategories =
+ new TsTableColumnCategory[columnList.size() - 1];
+ MeasurementSchema[] columnSchemas = new
MeasurementSchema[columnList.size() - 1];
+ TSDataType[] dataTypes = new TSDataType[columnList.size() - 1];
+ int nonTimeColumnIndex = 0;
+ long timestamp = -1;
+ for (int i = 0; i < columnList.size(); i++) {
+ TsTableColumnSchema columnSchema = columnList.get(i);
+ Expression expression = expressions.get(i);
+
+ if (columnSchema.getColumnCategory().equals(TIME)) {
+ timestamp = AstUtil.expressionToTimestamp(expression, zoneId,
plannerContext, session);
+ } else {
+ Object value = AstUtil.expressionToTsValue(expression, plannerContext,
session);
+ nonTimeValues[nonTimeColumnIndex] = value;
+ nonTimeColumnNames[nonTimeColumnIndex] = columnSchema.getColumnName();
+ dataTypes[nonTimeColumnIndex] = columnSchema.getDataType();
+ nonTimeColumnCategories[nonTimeColumnIndex] =
columnSchema.getColumnCategory();
+ columnSchemas[nonTimeColumnIndex] =
+ new MeasurementSchema(columnSchema.getColumnName(),
columnSchema.getDataType());
+ nonTimeColumnIndex++;
+ }
+ }
+
+ TimestampPrecisionUtils.checkTimestampPrecision(timestamp);
+ insertRowStatement.setTime(timestamp);
+ insertRowStatement.setMeasurements(nonTimeColumnNames);
+ insertRowStatement.setDataTypes(dataTypes);
+ insertRowStatement.setMeasurementSchemas(columnSchemas);
+ insertRowStatement.setValues(nonTimeValues);
+ insertRowStatement.setColumnCategories(nonTimeColumnCategories);
+ insertRowStatement.setNeedInferType(false);
+ insertRowStatement.setDatabaseName(databaseName);
+
+ try {
+ insertRowStatement.transferType(zoneId);
+ } catch (QueryProcessException e) {
+ throw new SemanticException(e);
+ }
+ return insertRowStatement;
+ }
+
+ private InsertRowStatement toInsertRowStatement(
+ List<Expression> expressions,
+ int timeColumnIndex,
+ String[] nonTimeColumnNames,
+ PlannerContext plannerContext,
+ SessionInfo session) {
+ InsertRowStatement insertRowStatement = new InsertRowStatement();
+ insertRowStatement.setWriteToTable(true);
+ insertRowStatement.setDevicePath(new PartialPath(new String[]
{tableName}));
+ long timestamp;
+ int nonTimeColumnCount;
+ if (timeColumnIndex == -1) {
+ timestamp = CommonDateTimeUtils.currentTime();
+ nonTimeColumnCount = expressions.size();
+ } else {
+ if (timeColumnIndex >= expressions.size()) {
+ throw new SemanticException(
+ String.format(
+ DataNodeQueryMessages.TIMECOLUMNINDEX_OUT_OF_BOUND_D_D,
+ timeColumnIndex,
+ expressions.size()));
+ }
+
+ timestamp =
+ AstUtil.expressionToTimestamp(
+ expressions.get(timeColumnIndex), zoneId, plannerContext,
session);
+ nonTimeColumnCount = expressions.size() - 1;
+ }
+
+ if (nonTimeColumnCount != nonTimeColumnNames.length) {
+ throw new SemanticException(
+ String.format(
+
DataNodeQueryMessages.INCONSISTENT_NUMBERS_OF_NON_TIME_COLUMN_NAMES_AND_VALUES_D_D,
+ nonTimeColumnNames.length,
+ nonTimeColumnCount));
+ }
+
+ TimestampPrecisionUtils.checkTimestampPrecision(timestamp);
+ insertRowStatement.setTime(timestamp);
+ insertRowStatement.setMeasurements(nonTimeColumnNames);
+
+ Object[] values = new Object[nonTimeColumnNames.length];
+ int valuePosition = 0;
+ for (int i = 0; i < expressions.size(); i++) {
+ if (i != timeColumnIndex) {
+ values[valuePosition++] =
+ AstUtil.expressionToTsValue(expressions.get(i), plannerContext,
session);
+ }
+ }
+
+ insertRowStatement.setValues(values);
+ insertRowStatement.setNeedInferType(true);
+ insertRowStatement.setDatabaseName(databaseName);
+ return insertRowStatement;
+ }
+}
diff --git
a/iotdb-core/datanode/src/main/java/org/apache/iotdb/db/queryengine/plan/relational/sql/parser/AstBuilder.java
b/iotdb-core/datanode/src/main/java/org/apache/iotdb/db/queryengine/plan/relational/sql/parser/AstBuilder.java
index 6df9b790c13..189589365b0 100644
---
a/iotdb-core/datanode/src/main/java/org/apache/iotdb/db/queryengine/plan/relational/sql/parser/AstBuilder.java
+++
b/iotdb-core/datanode/src/main/java/org/apache/iotdb/db/queryengine/plan/relational/sql/parser/AstBuilder.java
@@ -19,7 +19,6 @@
package org.apache.iotdb.db.queryengine.plan.relational.sql.parser;
-import org.apache.iotdb.calc.exception.QueryProcessException;
import org.apache.iotdb.common.rpc.thrift.TConsensusGroupType;
import org.apache.iotdb.commons.auth.entity.PrivilegeType;
import org.apache.iotdb.commons.cluster.NodeStatus;
@@ -143,12 +142,9 @@ import
org.apache.iotdb.commons.queryengine.plan.relational.sql.ast.ZeroOrMoreQu
import
org.apache.iotdb.commons.queryengine.plan.relational.sql.ast.ZeroOrOneQuantifier;
import
org.apache.iotdb.commons.queryengine.plan.relational.sql.parser.ParsingException;
import
org.apache.iotdb.commons.queryengine.plan.statement.component.FillPolicy;
-import org.apache.iotdb.commons.queryengine.utils.TimestampPrecisionUtils;
import org.apache.iotdb.commons.schema.cache.CacheClearOptions;
import org.apache.iotdb.commons.schema.table.InformationSchema;
-import org.apache.iotdb.commons.schema.table.TsTable;
import org.apache.iotdb.commons.schema.table.column.TsTableColumnCategory;
-import org.apache.iotdb.commons.schema.table.column.TsTableColumnSchema;
import
org.apache.iotdb.commons.udf.builtin.relational.TableBuiltinScalarFunction;
import org.apache.iotdb.commons.utils.CommonDateTimeUtils;
import org.apache.iotdb.commons.utils.PathUtils;
@@ -199,7 +195,7 @@ import
org.apache.iotdb.db.queryengine.plan.relational.sql.ast.ExplainOutputForm
import org.apache.iotdb.db.queryengine.plan.relational.sql.ast.ExtendRegion;
import org.apache.iotdb.db.queryengine.plan.relational.sql.ast.Flush;
import org.apache.iotdb.db.queryengine.plan.relational.sql.ast.Insert;
-import org.apache.iotdb.db.queryengine.plan.relational.sql.ast.InsertRows;
+import org.apache.iotdb.db.queryengine.plan.relational.sql.ast.InsertValues;
import org.apache.iotdb.db.queryengine.plan.relational.sql.ast.KillQuery;
import
org.apache.iotdb.db.queryengine.plan.relational.sql.ast.LoadConfiguration;
import org.apache.iotdb.db.queryengine.plan.relational.sql.ast.LoadModel;
@@ -262,12 +258,9 @@ import
org.apache.iotdb.db.queryengine.plan.relational.sql.ast.Update;
import
org.apache.iotdb.db.queryengine.plan.relational.sql.ast.UpdateAssignment;
import org.apache.iotdb.db.queryengine.plan.relational.sql.ast.Use;
import
org.apache.iotdb.db.queryengine.plan.relational.sql.ast.ViewFieldDefinition;
-import org.apache.iotdb.db.queryengine.plan.relational.sql.util.AstUtil;
import org.apache.iotdb.db.queryengine.plan.relational.sql.util.QueryUtil;
import org.apache.iotdb.db.queryengine.plan.relational.type.AuthorRType;
import org.apache.iotdb.db.queryengine.plan.statement.StatementType;
-import org.apache.iotdb.db.queryengine.plan.statement.crud.InsertRowStatement;
-import org.apache.iotdb.db.queryengine.plan.statement.crud.InsertRowsStatement;
import org.apache.iotdb.db.queryengine.plan.statement.sys.FlushStatement;
import
org.apache.iotdb.db.queryengine.plan.statement.sys.LoadConfigurationStatement;
import
org.apache.iotdb.db.queryengine.plan.statement.sys.SetConfigurationStatement;
@@ -278,7 +271,6 @@ import
org.apache.iotdb.db.queryengine.plan.statement.sys.StopRepairDataStatemen
import org.apache.iotdb.db.relational.grammar.sql.RelationalSqlBaseVisitor;
import org.apache.iotdb.db.relational.grammar.sql.RelationalSqlLexer;
import org.apache.iotdb.db.relational.grammar.sql.RelationalSqlParser;
-import org.apache.iotdb.db.schemaengine.table.DataNodeTableCache;
import org.apache.iotdb.db.storageengine.load.config.LoadTsFileConfigurator;
import org.apache.iotdb.db.utils.DataNodeDateTimeUtils;
@@ -288,9 +280,7 @@ import org.antlr.v4.runtime.Token;
import org.antlr.v4.runtime.tree.ParseTree;
import org.antlr.v4.runtime.tree.TerminalNode;
import org.apache.tsfile.common.constant.TsFileConstant;
-import org.apache.tsfile.enums.TSDataType;
import org.apache.tsfile.utils.TimeDuration;
-import org.apache.tsfile.write.schema.MeasurementSchema;
import javax.annotation.Nullable;
@@ -845,234 +835,18 @@ public class AstBuilder extends
RelationalSqlBaseVisitor<Node> {
final List<Identifier> identifiers =
visit(ctx.columnAliases().identifier(), Identifier.class);
if (query.getQueryBody() instanceof Values) {
- return visitInsertValues(
- databaseName, tableName, identifiers, ((Values)
query.getQueryBody()));
+ return new InsertValues(
+ databaseName, tableName, identifiers, ((Values)
query.getQueryBody()), zoneId);
} else {
return new Insert(new Table(qualifiedName), identifiers, query);
}
} else {
return query.getQueryBody() instanceof Values
- ? visitInsertValues(
- databaseName,
- DataNodeTableCache.getInstance().getTable(databaseName,
tableName),
- ((Values) query.getQueryBody()))
+ ? new InsertValues(databaseName, tableName, null, ((Values)
query.getQueryBody()), zoneId)
: new Insert(new Table(qualifiedName), query);
}
}
- private Node visitInsertValues(
- final String databaseName, final TsTable table, final Values queryBody) {
- final List<Expression> rows = queryBody.getRows();
- final List<InsertRowStatement> rowStatements =
- rows.stream()
- .map(
- r -> {
- List<Expression> expressions;
- if (r instanceof Row) {
- expressions = ((Row) r).getItems();
- } else if (r instanceof Literal) {
- expressions = Collections.singletonList(r);
- } else {
- throw new
SemanticException(DataNodeQueryMessages.UNEXPECTED_EXPRESSION_2 + r);
- }
- return toInsertRowStatement(expressions, table,
databaseName);
- })
- .collect(toList());
-
- InsertRowsStatement insertRowsStatement = new InsertRowsStatement();
- insertRowsStatement.setInsertRowStatementList(rowStatements);
- insertRowsStatement.setWriteToTable(true);
- // All rows of a parsed `INSERT INTO <table> ... VALUES ...` share the
same table and columns.
- insertRowsStatement.setUniformTargets(true);
- return new InsertRows(insertRowsStatement, null);
- }
-
- private Node visitInsertValues(
- final String databaseName,
- final String tableName,
- final List<Identifier> identifiers,
- final Values queryBody) {
- final List<String> columnNames =
- identifiers.stream().map(Identifier::getValue).collect(toList());
- int timeColumnIndex = -1;
-
- // retrieve the table schema to identify the actual time column
- TsTable table = DataNodeTableCache.getInstance().getTable(databaseName,
tableName);
- List<TsTableColumnSchema> timeColumnCandidates =
- table.getColumnList().stream()
- .filter(col -> col.getColumnCategory() == TIME)
- .collect(toList());
- if (timeColumnCandidates.size() != 1) {
- throw new SemanticException(
- DataNodeQueryMessages.THE_TABLE_SHOULD_ONLY_HAVE_ONE_COLUMN_FOUND);
- } else {
- // locate the time column index in the input identifiers if time column
exists in the schema
- for (int i = 0; i < columnNames.size(); i++) {
- if
(timeColumnCandidates.get(0).getColumnName().equalsIgnoreCase(columnNames.get(i)))
{
- if (timeColumnIndex == -1) {
- timeColumnIndex = i;
- } else {
- throw new SemanticException(
- DataNodeQueryMessages.ONE_ROW_SHOULD_ONLY_HAVE_ONE_TIME_VALUE);
- }
- }
- }
- }
-
- if (timeColumnIndex != -1) {
- columnNames.remove(timeColumnIndex);
- }
-
- List<Expression> rows = queryBody.getRows();
- if (timeColumnIndex == -1 && rows.size() > 1) {
- throw new
SemanticException(DataNodeQueryMessages.NEED_TIMESTAMPS_WHEN_INSERT_MULTI_ROWS);
- }
- int finalTimeColumnIndex = timeColumnIndex;
- List<InsertRowStatement> rowStatements =
- rows.stream()
- .map(
- r -> {
- List<Expression> expressions;
- if (r instanceof Row) {
- expressions = ((Row) r).getItems();
- } else if (r instanceof Literal) {
- expressions = Collections.singletonList(r);
- } else {
- throw new
SemanticException(DataNodeQueryMessages.UNEXPECTED_EXPRESSION_2 + r);
- }
- String[] columnNameArray = columnNames.toArray(new
String[0]);
- return toInsertRowStatement(
- expressions,
- finalTimeColumnIndex,
- columnNameArray,
- tableName,
- databaseName,
- columnNames.size());
- })
- .collect(toList());
-
- InsertRowsStatement insertRowsStatement = new InsertRowsStatement();
- insertRowsStatement.setInsertRowStatementList(rowStatements);
- insertRowsStatement.setWriteToTable(true);
- // All rows of a parsed `INSERT INTO <table> ... VALUES ...` share the
same table and columns.
- insertRowsStatement.setUniformTargets(true);
- return new InsertRows(insertRowsStatement, null);
- }
-
- private InsertRowStatement toInsertRowStatement(
- List<Expression> expressions, TsTable table, String databaseName) {
- InsertRowStatement insertRowStatement = new InsertRowStatement();
- insertRowStatement.setWriteToTable(true);
- insertRowStatement.setDevicePath(new PartialPath(new String[]
{table.getTableName()}));
-
- List<TsTableColumnSchema> columnList = table.getColumnList();
- if (expressions.size() != columnList.size()) {
- throw new SemanticException(
-
DataNodeQueryMessages.EXPRESSIONS_AND_COLUMNS_DO_NOT_MATCH_EXPRESSIONS_SIZE
- + expressions.size()
- + DataNodeQueryMessages.COLUMNS_SIZE
- + columnList.size());
- }
-
- String[] nonTimeColumnNames = new String[columnList.size() - 1];
- Object[] nonTimeValues = new Object[columnList.size() - 1];
- TsTableColumnCategory[] nonTimeColumnCategories =
- new TsTableColumnCategory[columnList.size() - 1];
- MeasurementSchema[] columnSchemas = new
MeasurementSchema[columnList.size() - 1];
- TSDataType[] dataTypes = new TSDataType[columnList.size() - 1];
- int nonTimeColumnIndex = 0;
- long timestamp = -1;
- for (int i = 0; i < columnList.size(); i++) {
- TsTableColumnSchema columnSchema = columnList.get(i);
- Expression expression = expressions.get(i);
-
- if (columnSchema.getColumnCategory().equals(TIME)) {
- timestamp = AstUtil.expressionToTimestamp(expression, zoneId);
- } else {
- Object value = AstUtil.expressionToTsValue(expression);
- nonTimeValues[nonTimeColumnIndex] = value;
- nonTimeColumnNames[nonTimeColumnIndex] = columnSchema.getColumnName();
- dataTypes[nonTimeColumnIndex] = columnSchema.getDataType();
- nonTimeColumnCategories[nonTimeColumnIndex] =
columnSchema.getColumnCategory();
- columnSchemas[nonTimeColumnIndex] =
- new MeasurementSchema(columnSchema.getColumnName(),
columnSchema.getDataType());
- nonTimeColumnIndex++;
- }
- }
-
- TimestampPrecisionUtils.checkTimestampPrecision(timestamp);
- insertRowStatement.setTime(timestamp);
- insertRowStatement.setMeasurements(nonTimeColumnNames);
- insertRowStatement.setDataTypes(dataTypes);
- insertRowStatement.setMeasurementSchemas(columnSchemas);
- insertRowStatement.setValues(nonTimeValues);
- insertRowStatement.setColumnCategories(nonTimeColumnCategories);
- insertRowStatement.setNeedInferType(false);
- insertRowStatement.setDatabaseName(databaseName);
-
- try {
- insertRowStatement.transferType(zoneId);
- } catch (QueryProcessException e) {
- throw new SemanticException(e);
- }
- return insertRowStatement;
- }
-
- private InsertRowStatement toInsertRowStatement(
- List<Expression> expressions,
- int timeColumnIndex,
- String[] nonTimeColumnNames,
- String tableName,
- String databaseName,
- int columnSize) {
- InsertRowStatement insertRowStatement = new InsertRowStatement();
- insertRowStatement.setWriteToTable(true);
- insertRowStatement.setDevicePath(new PartialPath(new String[]
{tableName}));
- long timestamp;
- int nonTimeColCnt;
- if (timeColumnIndex == -1) {
- timestamp = CommonDateTimeUtils.currentTime();
- nonTimeColCnt = expressions.size();
- } else {
- if (timeColumnIndex >= expressions.size()) {
- throw new SemanticException(
- String.format(
- DataNodeQueryMessages.TIMECOLUMNINDEX_OUT_OF_BOUND_D_D,
- timeColumnIndex,
- expressions.size()));
- }
-
- Expression timeExpression = expressions.get(timeColumnIndex);
- timestamp = AstUtil.expressionToTimestamp(timeExpression, zoneId);
- nonTimeColCnt = expressions.size() - 1;
- }
-
- if (nonTimeColCnt != nonTimeColumnNames.length) {
- throw new SemanticException(
- String.format(
-
DataNodeQueryMessages.INCONSISTENT_NUMBERS_OF_NON_TIME_COLUMN_NAMES_AND_VALUES_D_D,
- nonTimeColumnNames.length,
- nonTimeColCnt));
- }
-
- TimestampPrecisionUtils.checkTimestampPrecision(timestamp);
- insertRowStatement.setTime(timestamp);
- insertRowStatement.setMeasurements(nonTimeColumnNames);
-
- Object[] values = new Object[nonTimeColumnNames.length];
- int valuePos = 0;
- for (int i = 0; i < expressions.size(); i++) {
- if (i != timeColumnIndex) {
- values[valuePos++] = AstUtil.expressionToTsValue(expressions.get(i));
- }
- }
-
- insertRowStatement.setValues(values);
- insertRowStatement.setNeedInferType(true);
- insertRowStatement.setDatabaseName(databaseName);
- return insertRowStatement;
- }
-
@Override
public Node visitDeleteStatement(RelationalSqlParser.DeleteStatementContext
ctx) {
if (ctx.booleanExpression() != null) {
diff --git
a/iotdb-core/datanode/src/main/java/org/apache/iotdb/db/queryengine/plan/relational/sql/util/AstUtil.java
b/iotdb-core/datanode/src/main/java/org/apache/iotdb/db/queryengine/plan/relational/sql/util/AstUtil.java
index 92d4a7032e1..e49e49e7d97 100644
---
a/iotdb-core/datanode/src/main/java/org/apache/iotdb/db/queryengine/plan/relational/sql/util/AstUtil.java
+++
b/iotdb-core/datanode/src/main/java/org/apache/iotdb/db/queryengine/plan/relational/sql/util/AstUtil.java
@@ -20,21 +20,33 @@
package org.apache.iotdb.db.queryengine.plan.relational.sql.util;
import org.apache.iotdb.commons.exception.SemanticException;
+import org.apache.iotdb.commons.queryengine.common.SessionInfo;
+import org.apache.iotdb.commons.queryengine.plan.relational.analyzer.NodeRef;
import org.apache.iotdb.commons.queryengine.plan.relational.sql.ast.Expression;
import org.apache.iotdb.commons.queryengine.plan.relational.sql.ast.Identifier;
import org.apache.iotdb.commons.queryengine.plan.relational.sql.ast.Literal;
import
org.apache.iotdb.commons.queryengine.plan.relational.sql.ast.LongLiteral;
import org.apache.iotdb.commons.queryengine.plan.relational.sql.ast.Node;
import
org.apache.iotdb.commons.queryengine.plan.relational.sql.ast.NullLiteral;
+import org.apache.iotdb.commons.queryengine.plan.relational.sql.ast.Parameter;
import
org.apache.iotdb.commons.queryengine.plan.relational.sql.ast.StringLiteral;
import org.apache.iotdb.commons.utils.CommonDateTimeUtils;
import org.apache.iotdb.db.i18n.DataNodeQueryMessages;
+import org.apache.iotdb.db.queryengine.plan.analyze.TypeProvider;
+import
org.apache.iotdb.db.queryengine.plan.relational.planner.IrExpressionInterpreter;
+import org.apache.iotdb.db.queryengine.plan.relational.planner.IrTypeAnalyzer;
+import
org.apache.iotdb.db.queryengine.plan.relational.planner.NoOpSymbolResolver;
+import org.apache.iotdb.db.queryengine.plan.relational.planner.PlannerContext;
import com.google.common.graph.SuccessorsFunction;
import com.google.common.graph.Traverser;
+import org.apache.tsfile.common.conf.TSFileConfig;
+import org.apache.tsfile.read.common.type.Type;
+import org.apache.tsfile.utils.Binary;
import java.time.ZoneId;
import java.util.List;
+import java.util.Map;
import java.util.OptionalInt;
import java.util.function.BiFunction;
import java.util.function.Function;
@@ -120,6 +132,14 @@ public final class AstUtil {
throw new SemanticException(DataNodeQueryMessages.UNSUPPORTED_EXPRESSION +
expression);
}
+ public static Object expressionToTsValue(
+ Expression expression, PlannerContext plannerContext, SessionInfo
session) {
+ if (expression instanceof Literal) {
+ return expressionToTsValue(expression);
+ }
+ return evaluateInsertConstantExpression(expression, plannerContext,
session);
+ }
+
public static long expressionToTimestamp(Expression expression, ZoneId
zoneId) {
long timestamp;
if (expression instanceof LongLiteral) {
@@ -136,5 +156,68 @@ public final class AstUtil {
return timestamp;
}
+ public static long expressionToTimestamp(
+ Expression expression, ZoneId zoneId, PlannerContext plannerContext,
SessionInfo session) {
+ if (expression instanceof LongLiteral
+ || expression instanceof NullLiteral
+ || expression instanceof StringLiteral) {
+ return expressionToTimestamp(expression, zoneId);
+ }
+
+ Object value = evaluateInsertConstantExpression(expression,
plannerContext, session);
+ if (value == null) {
+ throw new
SemanticException(DataNodeQueryMessages.TIMESTAMP_CANNOT_BE_NULL);
+ }
+ if (value instanceof Integer || value instanceof Long) {
+ return ((Number) value).longValue();
+ }
+ if (value instanceof Binary) {
+ return parseDateTimeFormat(
+ ((Binary) value).getStringValue(TSFileConfig.STRING_CHARSET),
+ CommonDateTimeUtils.currentTime(),
+ zoneId);
+ }
+ throw new SemanticException(DataNodeQueryMessages.UNSUPPORTED_EXPRESSION +
expression);
+ }
+
+ private static Object evaluateInsertConstantExpression(
+ Expression expression, PlannerContext plannerContext, SessionInfo
session) {
+ if (preOrder(expression).anyMatch(Parameter.class::isInstance)) {
+ throw new SemanticException(DataNodeQueryMessages.UNSUPPORTED_EXPRESSION
+ expression);
+ }
+ if (expression instanceof Identifier) {
+ throw new SemanticException(
+ String.format(
+
DataNodeQueryMessages.CANNOT_INSERT_IDENTIFIER_S_PLEASE_USE_STRING_LITERAL,
+ expression));
+ }
+
+ try {
+ Map<NodeRef<Expression>, Type> types =
+ new IrTypeAnalyzer(plannerContext).getTypes(session,
TypeProvider.empty(), expression);
+ Object value =
+ new IrExpressionInterpreter(expression, plannerContext, session,
types)
+ .optimize(NoOpSymbolResolver.INSTANCE);
+ if (value instanceof Expression) {
+ throw new SemanticException(
+ String.format(
+ DataNodeQueryMessages
+
.EXCEPTION_INSERT_EXPRESSION_MUST_BE_CONSTANT_AFTER_CONSTANT_FOLDING_ARG_FOLDED_TO_ARG_D9EE1433,
+ expression,
+ value));
+ }
+ return value;
+ } catch (SemanticException e) {
+ throw e;
+ } catch (RuntimeException e) {
+ throw new SemanticException(
+ String.format(
+ DataNodeQueryMessages
+
.EXCEPTION_CANNOT_EVALUATE_INSERT_EXPRESSION_AS_A_CONSTANT_ARG_ARG_F478C134,
+ expression,
+ e.getMessage()));
+ }
+ }
+
private AstUtil() {}
}
diff --git
a/iotdb-core/datanode/src/test/java/org/apache/iotdb/db/queryengine/plan/relational/sql/AstMemoryEstimatorTest.java
b/iotdb-core/datanode/src/test/java/org/apache/iotdb/db/queryengine/plan/relational/sql/AstMemoryEstimatorTest.java
index c107593c84f..cbac0f40d23 100644
---
a/iotdb-core/datanode/src/test/java/org/apache/iotdb/db/queryengine/plan/relational/sql/AstMemoryEstimatorTest.java
+++
b/iotdb-core/datanode/src/test/java/org/apache/iotdb/db/queryengine/plan/relational/sql/AstMemoryEstimatorTest.java
@@ -20,7 +20,10 @@
package org.apache.iotdb.db.queryengine.plan.relational.sql;
import org.apache.iotdb.commons.memory.IMemoryBlock;
+import org.apache.iotdb.commons.queryengine.common.SessionInfo;
+import org.apache.iotdb.commons.queryengine.common.SqlDialect;
import org.apache.iotdb.commons.queryengine.plan.relational.sql.ast.Statement;
+import
org.apache.iotdb.commons.queryengine.plan.relational.type.InternalTypeManager;
import org.apache.iotdb.commons.schema.table.TsTable;
import org.apache.iotdb.commons.schema.table.column.FieldColumnSchema;
import org.apache.iotdb.commons.schema.table.column.TagColumnSchema;
@@ -33,7 +36,10 @@ import
org.apache.iotdb.db.protocol.session.InternalClientSession;
import org.apache.iotdb.db.protocol.session.PreparedStatementInfo;
import org.apache.iotdb.db.queryengine.plan.Coordinator;
import
org.apache.iotdb.db.queryengine.plan.execution.config.session.PreparedStatementHelper;
+import
org.apache.iotdb.db.queryengine.plan.relational.metadata.TableMetadataImpl;
+import org.apache.iotdb.db.queryengine.plan.relational.planner.PlannerContext;
import org.apache.iotdb.db.queryengine.plan.relational.sql.ast.InsertRows;
+import org.apache.iotdb.db.queryengine.plan.relational.sql.ast.InsertValues;
import org.apache.iotdb.db.queryengine.plan.relational.sql.ast.Prepare;
import org.apache.iotdb.db.queryengine.plan.relational.sql.parser.SqlParser;
import org.apache.iotdb.db.queryengine.plan.statement.crud.InsertRowStatement;
@@ -68,6 +74,12 @@ public class AstMemoryEstimatorTest {
return sqlParser.createStatement(sql, ZoneId.systemDefault(),
clientSession);
}
+ private void materialize(InsertValues insert) {
+ insert.materialize(
+ new PlannerContext(new TableMetadataImpl(), new InternalTypeManager()),
+ new SessionInfo(0, "root", ZoneId.systemDefault(), "testdb",
SqlDialect.TABLE));
+ }
+
private long estimateMemorySize(Statement statement) {
return statement == null ? 0L : statement.ramBytesUsed();
}
@@ -851,12 +863,15 @@ public class AstMemoryEstimatorTest {
config.setEnablePartialInsert(true);
config.setDataNodeId(0);
try {
- InsertRows small =
- (InsertRows) parseSQL("INSERT INTO insert_failure_memory
VALUES(1,'d1',11,'bad')");
- InsertRows large =
- (InsertRows)
+ InsertValues small =
+ (InsertValues) parseSQL("INSERT INTO insert_failure_memory
VALUES(1,'d1',11,'bad')");
+ InsertValues large =
+ (InsertValues)
parseSQL(
"INSERT INTO insert_failure_memory VALUES(1,'d1',11,'" +
"x".repeat(4096) + "')");
+ assertTrue(large.ramBytesUsed() > small.ramBytesUsed() + 4000);
+ materialize(small);
+ materialize(large);
InsertRowStatement row =
large.getInnerTreeStatement().getInsertRowStatementList().get(0);
assertEquals(1, row.getFailedMeasurementNumber());
assertNull(row.getValues()[2]);
@@ -899,12 +914,14 @@ public class AstMemoryEstimatorTest {
parseSQL(
"PREPARE insert_partial FROM INSERT INTO insert_partial "
+ "VALUES(1,'d1',11,'invalid integer')");
- InsertRows original = (InsertRows) prepare.getSql();
+ InsertValues original = (InsertValues) prepare.getSql();
+ materialize(original);
InsertRowStatement originalRow =
original.getInnerTreeStatement().getInsertRowStatementList().get(0);
assertEquals(1, originalRow.getFailedMeasurementNumber());
for (int i = 0; i < 2; i++) {
- InsertRows execution = original.copyForPreparedExecution();
+ InsertValues execution = original.copyForPreparedExecution();
+ materialize(execution);
InsertRowStatement executionRow =
execution.getInnerTreeStatement().getInsertRowStatementList().get(0);
execution.getInnerTreeStatement().semanticCheck();
diff --git
a/iotdb-core/datanode/src/test/java/org/apache/iotdb/db/queryengine/plan/relational/sql/ast/InsertValuesTest.java
b/iotdb-core/datanode/src/test/java/org/apache/iotdb/db/queryengine/plan/relational/sql/ast/InsertValuesTest.java
new file mode 100644
index 00000000000..30d1247b939
--- /dev/null
+++
b/iotdb-core/datanode/src/test/java/org/apache/iotdb/db/queryengine/plan/relational/sql/ast/InsertValuesTest.java
@@ -0,0 +1,204 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+package org.apache.iotdb.db.queryengine.plan.relational.sql.ast;
+
+import org.apache.iotdb.commons.exception.SemanticException;
+import org.apache.iotdb.commons.queryengine.common.SessionInfo;
+import org.apache.iotdb.commons.queryengine.common.SqlDialect;
+import
org.apache.iotdb.commons.queryengine.plan.relational.type.InternalTypeManager;
+import org.apache.iotdb.commons.schema.table.TsTable;
+import org.apache.iotdb.commons.schema.table.TsTableInternalRPCUtil;
+import org.apache.iotdb.commons.schema.table.column.FieldColumnSchema;
+import org.apache.iotdb.commons.schema.table.column.TagColumnSchema;
+import org.apache.iotdb.commons.schema.table.column.TimeColumnSchema;
+import org.apache.iotdb.db.i18n.DataNodeQueryMessages;
+import org.apache.iotdb.db.protocol.session.InternalClientSession;
+import
org.apache.iotdb.db.queryengine.plan.relational.metadata.TableMetadataImpl;
+import org.apache.iotdb.db.queryengine.plan.relational.planner.PlannerContext;
+import org.apache.iotdb.db.queryengine.plan.relational.sql.parser.SqlParser;
+import org.apache.iotdb.db.queryengine.plan.statement.crud.InsertRowStatement;
+import org.apache.iotdb.db.queryengine.plan.statement.crud.InsertRowsStatement;
+import org.apache.iotdb.db.schemaengine.table.DataNodeTableCache;
+
+import com.google.common.collect.ImmutableList;
+import com.google.common.collect.ImmutableMap;
+import org.apache.tsfile.enums.TSDataType;
+import org.junit.After;
+import org.junit.Before;
+import org.junit.Test;
+
+import java.time.ZoneId;
+
+import static org.junit.Assert.assertArrayEquals;
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertNotSame;
+import static org.junit.Assert.assertSame;
+import static org.junit.Assert.assertThrows;
+import static org.junit.Assert.assertTrue;
+
+public class InsertValuesTest {
+ private static final String DATABASE = "insert_values_test";
+ private static final String TABLE = "values_table";
+ private static final ZoneId ZONE = ZoneId.of("UTC");
+ private final PlannerContext plannerContext =
+ new PlannerContext(new TableMetadataImpl(), new InternalTypeManager());
+ private final SessionInfo session = new SessionInfo(0, "root", ZONE,
DATABASE, SqlDialect.TABLE);
+
+ @Before
+ public void setUp() {
+ TsTable table = new TsTable(TABLE);
+ table.addColumnSchema(new TimeColumnSchema("time", TSDataType.TIMESTAMP));
+ table.addColumnSchema(new TagColumnSchema("device_id", TSDataType.STRING));
+ table.addColumnSchema(new FieldColumnSchema("value", TSDataType.INT32));
+ cacheTable(table);
+ }
+
+ @After
+ public void tearDown() {
+ DataNodeTableCache.getInstance().invalid(DATABASE);
+ }
+
+ @Test
+ public void testPreparedExecutionsMaterializeIndependentRows() {
+ for (String columns : new String[] {" (time, device_id, value)", ""}) {
+ InsertValues original = parse(columns + " VALUES (1 + 1, 'd1', 2 + 3)");
+ long originalSize = original.ramBytesUsed();
+ InsertRowStatement previousRow = null;
+ for (int i = 0; i < 2; i++) {
+ InsertValues execution = original.copyForPreparedExecution();
+ assertNotSame(original, execution);
+ assertEquals(DATABASE, execution.getDatabase());
+
assertTrue(execution.getInnerTreeStatement().getInsertRowStatementList().isEmpty());
+ execution.materialize(plannerContext, session);
+ InsertRowsStatement statement = execution.getInnerTreeStatement();
+ InsertRowStatement row = statement.getInsertRowStatementList().get(0);
+ assertNotSame(previousRow, row);
+ assertEquals(DATABASE, row.getDatabaseName().get());
+ assertEquals(2, row.getTime());
+ assertArrayEquals(new String[] {"device_id", "value"},
row.getMeasurements());
+ assertEquals(5, ((Number) row.getValues()[1]).intValue());
+ execution.materialize(plannerContext, session);
+ assertSame(statement, execution.getInnerTreeStatement());
+ row.getMeasurements()[0] = "changed";
+ row.getValues()[1] = -1;
+ previousRow = row;
+ }
+
assertTrue(original.getInnerTreeStatement().getInsertRowStatementList().isEmpty());
+ assertEquals(originalSize, original.ramBytesUsed());
+ }
+ }
+
+ @Test
+ public void testFoldedRowsRetainUniformTargets() {
+ for (String columns : new String[] {" (time, device_id, value)", ""}) {
+ InsertValues insert = parse(columns + " VALUES (1 + 1, 'd1', 2 + 3), (2
+ 1, 'd2', 3 + 4)");
+ insert.materialize(plannerContext, session);
+ InsertRowsStatement statement = insert.getInnerTreeStatement();
+ assertTrue(statement.isUniformTargets());
+ assertEquals(2, statement.getInsertRowStatementList().size());
+ for (int i = 0; i < 2; i++) {
+ InsertRowStatement row = statement.getInsertRowStatementList().get(i);
+ assertEquals(i + 2, row.getTime());
+ assertArrayEquals(new String[] {"device_id", "value"},
row.getMeasurements());
+ assertEquals(i * 2 + 5, ((Number) row.getValues()[1]).intValue());
+ }
+ }
+ }
+
+ @Test
+ public void testFoldedCustomTimeColumn() {
+ cacheTable(
+ new TsTable(
+ TABLE,
+ ImmutableList.of(
+ new TagColumnSchema("device_id", TSDataType.STRING),
+ new TimeColumnSchema("event_time", TSDataType.TIMESTAMP),
+ new FieldColumnSchema("value", TSDataType.INT32))));
+ for (String columns : new String[] {" (device_id, event_time, value)",
""}) {
+ InsertValues insert = parse(columns + " VALUES ('d1', 10 + 5, 20 + 2)");
+ insert.materialize(plannerContext, session);
+ InsertRowStatement row =
insert.getInnerTreeStatement().getInsertRowStatementList().get(0);
+ assertEquals(15, row.getTime());
+ assertEquals(22, ((Number) row.getValues()[1]).intValue());
+ }
+ }
+
+ @Test
+ public void testFoldedNumericInPredicates() {
+ for (String columns : new String[] {" (time, device_id, value)", ""}) {
+ for (String predicate :
+ new String[] {"1 + 1 IN (2, 3)", "2 IN (1 + 1, 3)", "1 + 1 IN (2.0,
3.0)"}) {
+ InsertValues insert = parse(columns + " VALUES (1, 'd1', if(" +
predicate + ", 11, 22))");
+ insert.materialize(plannerContext, session);
+ InsertRowStatement row =
insert.getInnerTreeStatement().getInsertRowStatementList().get(0);
+ assertEquals(predicate, 11, ((Number) row.getValues()[1]).intValue());
+ }
+ }
+ }
+
+ @Test
+ public void testFoldedNaNComparisons() {
+ for (String columns : new String[] {" (time, device_id, value)", ""}) {
+ for (String predicate :
+ new String[] {
+ "1.0 < (0.0 / 0.0)",
+ "(0.0 / 0.0) >= 1.0",
+ "(0.0 / 0.0) = (0.0 / 0.0)",
+ "(0.0 / 0.0) <> 1.0"
+ }) {
+ InsertValues insert = parse(columns + " VALUES (1, 'd1', if(" +
predicate + ", 11, 22))");
+ insert.materialize(plannerContext, session);
+ InsertRowStatement row =
insert.getInnerTreeStatement().getInsertRowStatementList().get(0);
+ assertEquals(predicate, 22, ((Number) row.getValues()[1]).intValue());
+ }
+ }
+ }
+
+ @Test
+ public void testUnboundParametersRemainSemanticErrors() {
+ for (String columns : new String[] {" (time, device_id, value)", ""}) {
+ for (String values :
+ new String[] {" (?, 'd1', 1)", " (1, 'd1', 1 + ?)", " (1, 'd1', 1),
(?, 'd2', 2)"}) {
+ InsertValues insert = parse(columns + " VALUES" + values);
+ SemanticException exception =
+ assertThrows(
+ SemanticException.class, () ->
insert.materialize(plannerContext, session));
+
assertTrue(exception.getMessage().startsWith(DataNodeQueryMessages.UNSUPPORTED_EXPRESSION));
+
assertTrue(insert.getInnerTreeStatement().getInsertRowStatementList().isEmpty());
+ }
+ }
+ }
+
+ private InsertValues parse(String suffix) {
+ return (InsertValues)
+ new SqlParser()
+ .createStatement(
+ "INSERT INTO " + DATABASE + "." + TABLE + suffix,
+ ZONE,
+ new InternalClientSession("insert_values_test"));
+ }
+
+ private void cacheTable(TsTable table) {
+ DataNodeTableCache.getInstance()
+ .init(
+ TsTableInternalRPCUtil.serializeTableInitializationInfo(
+ ImmutableMap.of(DATABASE, ImmutableList.of(table)),
ImmutableMap.of()));
+ }
+}