This is an automated email from the ASF dual-hosted git repository.
JackieTien97 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 15870ea69a2 Fix error message is unreadable when use IN and NULLIF
expression
15870ea69a2 is described below
commit 15870ea69a2eb73976c67e9b65879427207d7771
Author: Weihao Li <[email protected]>
AuthorDate: Thu Dec 3 17:54:22 2026 +0800
Fix error message is unreadable when use IN and NULLIF expression
---
.../it/db/it/IoTDBCaseWhenThenTableIT.java | 63 +++++++++++
.../it/query/old/query/IoTDBInTableIT.java | 52 +++++++++
.../org/apache/iotdb/calc/i18n/CalcMessages.java | 3 +
.../org/apache/iotdb/calc/i18n/CalcMessages.java | 3 +
.../relational/ColumnTransformerBuilder.java | 82 ++++++++++++--
.../process/ColumnTransformerBuilderTest.java | 121 +++++++++++++++++++++
.../relational/analyzer/ExpressionAnalyzer.java | 9 ++
.../plan/relational/planner/IrTypeAnalyzer.java | 21 ++--
.../plan/relational/sql/ast/BinaryLiteral.java | 5 +
.../plan/relational/sql/ast/BooleanLiteral.java | 5 +
.../plan/relational/sql/ast/DecimalLiteral.java | 5 +
.../plan/relational/sql/ast/DoubleLiteral.java | 5 +
.../plan/relational/sql/ast/Expression.java | 3 +
.../plan/relational/sql/ast/FloatLiteral.java | 5 +
.../plan/relational/sql/ast/GenericLiteral.java | 5 +
.../plan/relational/sql/ast/Literal.java | 5 +
.../plan/relational/sql/ast/LongLiteral.java | 5 +
.../plan/relational/sql/ast/NullIfExpression.java | 28 ++++-
.../plan/relational/sql/ast/NullLiteral.java | 5 +
.../plan/relational/sql/ast/StringLiteral.java | 5 +
.../relational/sql/ast/TableExpressionType.java | 3 +-
.../relational/sql/ast/TimeDurationLiteral.java | 5 +
22 files changed, 422 insertions(+), 21 deletions(-)
diff --git
a/integration-test/src/test/java/org/apache/iotdb/relational/it/db/it/IoTDBCaseWhenThenTableIT.java
b/integration-test/src/test/java/org/apache/iotdb/relational/it/db/it/IoTDBCaseWhenThenTableIT.java
index 7ccab4ac4bc..2578b3767fb 100644
---
a/integration-test/src/test/java/org/apache/iotdb/relational/it/db/it/IoTDBCaseWhenThenTableIT.java
+++
b/integration-test/src/test/java/org/apache/iotdb/relational/it/db/it/IoTDBCaseWhenThenTableIT.java
@@ -19,7 +19,9 @@
package org.apache.iotdb.relational.it.db.it;
+import org.apache.iotdb.calc.i18n.CalcMessages;
import org.apache.iotdb.it.env.EnvFactory;
+import org.apache.iotdb.rpc.TSStatusCode;
import org.junit.AfterClass;
import org.junit.BeforeClass;
@@ -91,6 +93,67 @@ public abstract class IoTDBCaseWhenThenTableIT {
DATABASE);
}
+ @Test
+ public void testNullIfNotSupported() {
+ // Originally reported: NULLIF(CAST(status AS STRING), CAST('normal' AS
STRING)) fails with
+ // "301: Unsupported expression". NULLIF has no execution operator yet, so
it is rejected as
+ // a semantic error when a query actually needs to evaluate it.
+ String msg =
+ TSStatusCode.SEMANTIC_ERROR.getStatusCode()
+ + ": "
+ + CalcMessages.NULLIF_IS_NOT_SUPPORTED_YET;
+ tableAssertTestFail(
+ "select nullif(cast(s6 as string), cast('text100' as string)) from
table1", msg, DATABASE);
+ tableAssertTestFail(
+ "select device_id, sum(case when s6 = 'a' then 1 else 0 end) /
nullif(cast(count(case when s6 = 'a' then 1 end) as double), cast(0 as double))
from table1 group by device_id",
+ msg,
+ DATABASE);
+ }
+
+ @Test
+ public void testNullIfPrunedByOuterCount() {
+ // The outer count does not consume the NULLIF output, so the optimizer
prunes the whole
+ // projection and the unsupported expression is never evaluated. Only the
argument types
+ // are checked during analysis, so this query must succeed.
+ tableResultSetEqualTest(
+ "select count(*) from (select nullif(s1, 0) as v from table1) q",
+ expectedHeader,
+ new String[] {"4,"},
+ DATABASE);
+ }
+
+ @Test
+ public void testInt32Int64WideningBothBranchOrders() {
+ // INT32 and INT64 branches widen to INT64 regardless of the branch order,
including the
+ // folded CAST literal (stored as an INT32-valued LongLiteral) before or
after the INT64
+ // column.
+ tableResultSetEqualTest(
+ "select case when s2 = 1 then cast(0 as int64) else s2 end from table3
limit 4",
+ expectedHeader,
+ new String[] {"0,", "0,", "2,", "3,"},
+ DATABASE);
+ tableResultSetEqualTest(
+ "select case when s2 = 1 then s2 else cast(0 as int64) end from table3
limit 4",
+ expectedHeader,
+ new String[] {"0,", "1,", "0,", "0,"},
+ DATABASE);
+ tableResultSetEqualTest(
+ "select if(s2 > 1, cast(0 as int64), s2) from table3 limit 4",
+ expectedHeader,
+ new String[] {"0,", "1,", "0,", "0,"},
+ DATABASE);
+ tableResultSetEqualTest(
+ "select if(s2 > 1, s2, cast(0 as int64)) from table3 limit 4",
+ expectedHeader,
+ new String[] {"0,", "0,", "2,", "3,"},
+ DATABASE);
+ tableResultSetEqualTest(
+ "select coalesce(cast(null as int64), s2) from table3 limit 4",
+ expectedHeader,
+ new String[] {"0,", "1,", "2,", "3,"},
+ DATABASE);
+ }
+
@Test
public void testKind1Basic() {
String[] retArray = new String[] {"99,", "9999,", "9999,", "999,"};
diff --git
a/integration-test/src/test/java/org/apache/iotdb/relational/it/query/old/query/IoTDBInTableIT.java
b/integration-test/src/test/java/org/apache/iotdb/relational/it/query/old/query/IoTDBInTableIT.java
index c0a5b8948d3..2c1c2281350 100644
---
a/integration-test/src/test/java/org/apache/iotdb/relational/it/query/old/query/IoTDBInTableIT.java
+++
b/integration-test/src/test/java/org/apache/iotdb/relational/it/query/old/query/IoTDBInTableIT.java
@@ -19,6 +19,7 @@
package org.apache.iotdb.relational.it.query.old.query;
+import org.apache.iotdb.calc.i18n.CalcMessages;
import org.apache.iotdb.it.env.EnvFactory;
import org.apache.iotdb.itbase.env.BaseEnv;
import org.apache.iotdb.rpc.TSStatusCode;
@@ -67,6 +68,9 @@ public abstract class IoTDBInTableIT extends
IoTDBOldQueryITBase {
"insert into sg(time,device1,device2,qrcode,date_v,blob_v,timestamp_v)
values(1509465900000,'d2','s1','qrcode004', '2024-08-11',
X'abca',1509465900000)",
"insert into sg(time,device1,device2,qrcode,date_v,blob_v,timestamp_v)
values(1509465960000,'d2','s1','qrcode005', '2024-08-12',
X'abcb',1509465960000)",
"CREATE TABLE table1(device STRING TAG, s1 INT32 FIELD, s2 INT64
FIELD, s3 FLOAT FIELD, s4 DOUBLE FIELD, s5 BOOLEAN FIELD)",
+ "insert into table1(time, device, s1, s2, s3, s4, s5) values(1, 'd1',
10, 100, 10.5, 10.5, true)",
+ "insert into table1(time, device, s1, s2, s3, s4, s5) values(2, 'd2',
20, 200, 20.5, 20.5, false)",
+ "insert into table1(time, device, s1, s2, s3, s4, s5) values(3, 'd3',
30, 300, 30.5, 30.5, true)",
};
@Test
@@ -98,6 +102,54 @@ public abstract class IoTDBInTableIT extends
IoTDBOldQueryITBase {
DATABASE_NAME);
}
+ @Test
+ public void testInExpressionTypeMismatch() {
+ // In expression context (not pushed down into the table scan filter) the
IN transformer
+ // casts every list item to the column type. Items of a different literal
kind cannot be
+ // converted there and must be reported as a semantic error instead of a
raw
+ // ClassCastException. Shapes follow the originally reported failures:
+ // CASE WHEN temperature = 18 OR temperature = 27 THEN ... (normalized
to IN (18, 27))
+ // COUNT(DISTINCT CASE WHEN temperature = 18 OR temperature = 27 THEN
...)
+ // SUM(CASE WHEN temperature = 21 OR temperature = 30 THEN ...)
+ String intToFloat =
+ TSStatusCode.SEMANTIC_ERROR.getStatusCode()
+ + ": "
+ + String.format(
+ CalcMessages.IN_LIST_LITERAL_CANNOT_BE_CONVERTED_TO_TYPE,
"INT32", "FLOAT");
+ String intToDouble =
+ TSStatusCode.SEMANTIC_ERROR.getStatusCode()
+ + ": "
+ + String.format(
+ CalcMessages.IN_LIST_LITERAL_CANNOT_BE_CONVERTED_TO_TYPE,
"INT32", "DOUBLE");
+ String doubleToInt64 =
+ TSStatusCode.SEMANTIC_ERROR.getStatusCode()
+ + ": "
+ + String.format(
+ CalcMessages.IN_LIST_LITERAL_CANNOT_BE_CONVERTED_TO_TYPE,
"DOUBLE", "INT64");
+ tableAssertTestFail(
+ "select case when s3 = 18 or s3 = 27 then 1 else 0 end from table1",
+ intToFloat,
+ DATABASE_NAME);
+ tableAssertTestFail(
+ "select case when s3 in (18, 27) then 1 else 0 end from table1",
intToFloat, DATABASE_NAME);
+ tableAssertTestFail(
+ "select count(device), sum(case when s4 = 21 or s4 = 30 then 1 else 0
end) from table1 group by device",
+ intToDouble,
+ DATABASE_NAME);
+ tableAssertTestFail("select s3 in (18, 27) from table1", intToFloat,
DATABASE_NAME);
+ tableAssertTestFail("select s3 in (18, 27.5) from table1", intToFloat,
DATABASE_NAME);
+ tableAssertTestFail(
+ "select case when s2 in (1.5, 2.5) then 1 else 0 end from table1",
+ doubleToInt64,
+ DATABASE_NAME);
+ // Pushed down as a table scan filter, the items are converted by column
type. Unchanged.
+ tableResultSetEqualTest(
+ "select count(*) from table1 where s3 in (18, 27)",
+ new String[] {"_col0"},
+ new String[] {"0,"},
+ DATABASE_NAME);
+ }
+
@Test
public void selectWithAlignByDeviceTest() {
String[] retArray =
diff --git
a/iotdb-core/calc-commons/src/main/i18n/en/org/apache/iotdb/calc/i18n/CalcMessages.java
b/iotdb-core/calc-commons/src/main/i18n/en/org/apache/iotdb/calc/i18n/CalcMessages.java
index df1b628f47a..ead89065193 100644
---
a/iotdb-core/calc-commons/src/main/i18n/en/org/apache/iotdb/calc/i18n/CalcMessages.java
+++
b/iotdb-core/calc-commons/src/main/i18n/en/org/apache/iotdb/calc/i18n/CalcMessages.java
@@ -76,6 +76,8 @@ public final class CalcMessages {
"InputRowUtils should not be instantiated.";
public static final String IN_LIST_LITERAL_FOR_TIMESTAMP_TYPE_RESTRICTION =
"InList Literal for TIMESTAMP can only be LongLiteral, DoubleLiteral and
GenericLiteral, current is %s";
+ public static final String IN_LIST_LITERAL_CANNOT_BE_CONVERTED_TO_TYPE =
+ "IN list literal of type %s cannot be converted to the column type %s";
public static final String INVALID_AGGREGATION_FUNCTION = "Invalid
Aggregation function: ";
public static final String INVALID_TEXT_INPUT_FOR_BOOLEAN =
"Invalid text input for boolean type: %s";
@@ -92,6 +94,7 @@ public final class CalcMessages {
public static final String
MULTIPLE_I_TEMPORARY_QUERY_DATA_FILE_SERVICE_PROVIDER_FOUND =
"Multiple ITemporaryQueryDataFileServiceProvider found";
public static final String NOT_ENOUGH_MEMORY_FOR_SORTING = "Not enough
memory for sorting";
+ public static final String NULLIF_IS_NOT_SUPPORTED_YET = "NULLIF is not
supported yet.";
public static final String NOT_YET_IMPLEMENTED = "not yet implemented";
public static final String NO_I_OBJECT_FILE_SERVICE_PROVIDER_FOUND =
"No IObjectFileServiceProvider found";
diff --git
a/iotdb-core/calc-commons/src/main/i18n/zh/org/apache/iotdb/calc/i18n/CalcMessages.java
b/iotdb-core/calc-commons/src/main/i18n/zh/org/apache/iotdb/calc/i18n/CalcMessages.java
index 49b48f6a2d6..a3c370fd927 100644
---
a/iotdb-core/calc-commons/src/main/i18n/zh/org/apache/iotdb/calc/i18n/CalcMessages.java
+++
b/iotdb-core/calc-commons/src/main/i18n/zh/org/apache/iotdb/calc/i18n/CalcMessages.java
@@ -74,6 +74,8 @@ public final class CalcMessages {
"InputRowUtils 不应被实例化。";
public static final String IN_LIST_LITERAL_FOR_TIMESTAMP_TYPE_RESTRICTION =
"TIMESTAMP 的 InList 字面量只能是 LongLiteral、DoubleLiteral 和
GenericLiteral,当前为 %s";
+ public static final String IN_LIST_LITERAL_CANNOT_BE_CONVERTED_TO_TYPE =
+ "IN 列表项字面量类型 %s 无法转换为列类型 %s";
public static final String INVALID_AGGREGATION_FUNCTION = "无效的聚合函数:";
public static final String INVALID_TEXT_INPUT_FOR_BOOLEAN =
"布尔类型的文本输入无效:%s";
@@ -89,6 +91,7 @@ public final class CalcMessages {
public static final String
MULTIPLE_I_TEMPORARY_QUERY_DATA_FILE_SERVICE_PROVIDER_FOUND =
"找到多个 ITemporaryQueryDataFileServiceProvider";
public static final String NOT_ENOUGH_MEMORY_FOR_SORTING = "排序内存不足";
+ public static final String NULLIF_IS_NOT_SUPPORTED_YET = "暂不支持 NULLIF 函数。";
public static final String NOT_YET_IMPLEMENTED = "尚未实现";
public static final String NO_I_OBJECT_FILE_SERVICE_PROVIDER_FOUND =
"未找到 IObjectFileServiceProvider";
diff --git
a/iotdb-core/calc-commons/src/main/java/org/apache/iotdb/calc/execution/relational/ColumnTransformerBuilder.java
b/iotdb-core/calc-commons/src/main/java/org/apache/iotdb/calc/execution/relational/ColumnTransformerBuilder.java
index 3b075b6196b..bf8aebd6279 100644
---
a/iotdb-core/calc-commons/src/main/java/org/apache/iotdb/calc/execution/relational/ColumnTransformerBuilder.java
+++
b/iotdb-core/calc-commons/src/main/java/org/apache/iotdb/calc/execution/relational/ColumnTransformerBuilder.java
@@ -194,6 +194,7 @@ import
org.apache.iotdb.commons.queryengine.plan.relational.type.TypeNotFoundExc
import org.apache.iotdb.commons.queryengine.plan.udf.TableUDFUtils;
import
org.apache.iotdb.commons.udf.builtin.relational.TableBuiltinScalarFunction;
import org.apache.iotdb.commons.udf.utils.UDFDataTypeTransformer;
+import org.apache.iotdb.commons.utils.TestOnly;
import org.apache.iotdb.udf.api.customizer.analysis.ScalarFunctionAnalysis;
import org.apache.iotdb.udf.api.customizer.parameter.FunctionArguments;
import org.apache.iotdb.udf.api.relational.ScalarFunction;
@@ -1526,9 +1527,50 @@ public class ColumnTransformerBuilder
private static InMultiColumnTransformer constructInColumnTransformer(
Type childType, List<ColumnTransformer> valueColumnTransformerList,
List<Literal> values) {
- return TypeServices.IN_MULTI_COLUMN_TRANSFORMER_SERVICE
- .call(childType)
- .create(valueColumnTransformerList, values);
+ try {
+ return TypeServices.IN_MULTI_COLUMN_TRANSFORMER_SERVICE
+ .call(childType)
+ .create(valueColumnTransformerList, values);
+ } catch (ClassCastException e) {
+ // The IN transformers cast each literal to the value column's type; a
literal of an
+ // incompatible kind surfaces here as a raw ClassCastException. Report
the specific
+ // conversion the transformer cannot perform instead of leaking the cast
failure.
+ for (Literal value : values) {
+ try {
+ TypeServices.IN_MULTI_COLUMN_TRANSFORMER_SERVICE
+ .call(childType)
+ .create(valueColumnTransformerList,
Collections.singletonList(value));
+ } catch (ClassCastException ignored) {
+ throw new SemanticException(
+ String.format(
+ CalcMessages.IN_LIST_LITERAL_CANNOT_BE_CONVERTED_TO_TYPE,
+ value.getLiteralTypeName(),
+ childType));
+ }
+ }
+ throw new SemanticException(
+ String.format(
+ CalcMessages.IN_LIST_LITERAL_CANNOT_BE_CONVERTED_TO_TYPE,
+ describeLiteralTypes(values),
+ childType));
+ }
+ }
+
+ private static String describeLiteralTypes(List<Literal> values) {
+ StringBuilder builder = new StringBuilder();
+ for (Literal value : values) {
+ if (builder.length() > 0) {
+ builder.append(',');
+ }
+ builder.append(value.getLiteralTypeName());
+ }
+ return builder.toString();
+ }
+
+ @TestOnly
+ public static InMultiColumnTransformer constructInColumnTransformerForTest(
+ Type childType, List<ColumnTransformer> valueColumnTransformerList,
List<Literal> values) {
+ return constructInColumnTransformer(childType, valueColumnTransformerList,
values);
}
@Override
@@ -1689,7 +1731,7 @@ public class ColumnTransformerBuilder
} else {
List<ColumnTransformer> children =
node.getChildren().stream().map(c -> process(c,
context)).collect(Collectors.toList());
- context.cache.put(node, new
CoalesceColumnTransformer(children.get(0).getType(), children));
+ context.cache.put(node, new
CoalesceColumnTransformer(getWidenedType(children), children));
}
}
ColumnTransformer res = context.cache.get(node);
@@ -1724,10 +1766,12 @@ public class ColumnTransformerBuilder
ColumnTransformer elseColumnTransformer =
process(node.getDefaultValue().orElse(new NullLiteral()), context);
+ List<ColumnTransformer> branches = new ArrayList<>(thenList);
+ branches.add(elseColumnTransformer);
context.cache.put(
node,
new TableCaseWhenThenColumnTransformer(
- thenList.get(0).getType(), whenList, thenList,
elseColumnTransformer));
+ getWidenedType(branches), whenList, thenList,
elseColumnTransformer));
}
}
return getColumnTransformerFromCacheAndAddReferenceCount(node, context);
@@ -1756,15 +1800,36 @@ public class ColumnTransformerBuilder
ColumnTransformer elseColumnTransformer =
process(node.getDefaultValue().orElse(new NullLiteral()), context);
+ List<ColumnTransformer> branches = new ArrayList<>(thenList);
+ branches.add(elseColumnTransformer);
context.cache.put(
node,
new TableCaseWhenThenColumnTransformer(
- thenList.get(0).getType(), whenList, thenList,
elseColumnTransformer));
+ getWidenedType(branches), whenList, thenList,
elseColumnTransformer));
}
}
return getColumnTransformerFromCacheAndAddReferenceCount(node, context);
}
+ // The result column must hold the widest common type of the branches: the
IR type analyzer
+ // admits INT32/INT64 mixes and widens them to INT64, and the Long column
builder consumes an
+ // Int column of a narrower branch through getLong() widening. Picking the
first branch's type
+ // instead would make the outcome depend on the branch order.
+ private static Type getWidenedType(List<ColumnTransformer> branches) {
+ Type widened = branches.get(0).getType();
+ for (int i = 1; i < branches.size(); i++) {
+ widened = widenType(widened, branches.get(i).getType());
+ }
+ return widened;
+ }
+
+ private static Type widenType(Type left, Type right) {
+ if (INT64.equals(left) || INT64.equals(right)) {
+ return INT64;
+ }
+ return left;
+ }
+
@Override
public ColumnTransformer visitTrim(Trim node, Context context) {
throw new
UnsupportedOperationException(String.format(UNSUPPORTED_EXPRESSION, node));
@@ -1777,7 +1842,10 @@ public class ColumnTransformerBuilder
@Override
public ColumnTransformer visitNullIfExpression(NullIfExpression node,
Context context) {
- throw new
UnsupportedOperationException(String.format(UNSUPPORTED_EXPRESSION, node));
+ // NULLIF has no execution operator yet. Reject it here (as a semantic
error instead of a
+ // generic unsupported-expression failure) so that queries whose NULLIF is
pruned by the
+ // optimizer can still succeed.
+ throw new SemanticException(CalcMessages.NULLIF_IS_NOT_SUPPORTED_YET);
}
private void appendIdentityColumnTransformer(
diff --git
a/iotdb-core/calc-commons/src/test/java/org/apache/iotdb/calc/execution/operator/process/ColumnTransformerBuilderTest.java
b/iotdb-core/calc-commons/src/test/java/org/apache/iotdb/calc/execution/operator/process/ColumnTransformerBuilderTest.java
new file mode 100644
index 00000000000..ebf86246350
--- /dev/null
+++
b/iotdb-core/calc-commons/src/test/java/org/apache/iotdb/calc/execution/operator/process/ColumnTransformerBuilderTest.java
@@ -0,0 +1,121 @@
+/*
+ * 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.calc.execution.operator.process;
+
+import org.apache.iotdb.calc.execution.relational.ColumnTransformerBuilder;
+import org.apache.iotdb.calc.i18n.CalcMessages;
+import org.apache.iotdb.calc.transformation.dag.column.ColumnTransformer;
+import org.apache.iotdb.commons.exception.SemanticException;
+import
org.apache.iotdb.commons.queryengine.plan.relational.sql.ast.DoubleLiteral;
+import
org.apache.iotdb.commons.queryengine.plan.relational.sql.ast.FloatLiteral;
+import
org.apache.iotdb.commons.queryengine.plan.relational.sql.ast.GenericLiteral;
+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.rpc.TSStatusCode;
+
+import org.apache.ratis.thirdparty.com.google.common.collect.ImmutableList;
+import org.apache.tsfile.read.common.type.FloatType;
+import org.apache.tsfile.read.common.type.IntType;
+import org.apache.tsfile.read.common.type.LongType;
+import org.junit.Test;
+
+import java.util.List;
+
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertNotNull;
+import static org.junit.Assert.assertThrows;
+import static org.junit.Assert.assertTrue;
+
+public class ColumnTransformerBuilderTest {
+
+ private static final List<ColumnTransformer> NO_VALUE_TRANSFORMERS =
ImmutableList.of();
+
+ @Test
+ public void testInTransformerWithMatchingLiteralTypes() {
+ // INT64 value accepts INT32 literals; FLOAT value accepts FLOAT literals.
+ assertNotNull(
+ ColumnTransformerBuilder.constructInColumnTransformerForTest(
+ LongType.INT64,
+ NO_VALUE_TRANSFORMERS,
+ ImmutableList.of(new LongLiteral("1"), new
LongLiteral("2147483648"))));
+ assertNotNull(
+ ColumnTransformerBuilder.constructInColumnTransformerForTest(
+ FloatType.FLOAT, NO_VALUE_TRANSFORMERS, ImmutableList.of(new
FloatLiteral("1.5"))));
+ }
+
+ @Test
+ public void testInTransformerMismatchReportsSpecificConversion() {
+ // A FLOAT value column with INT literals throws ClassCastException inside
the IN
+ // transformer; the builder must convert it into a semantic error naming
the specific
+ // conversion instead of leaking the raw cast failure.
+ Throwable e =
+ assertThrows(
+ SemanticException.class,
+ () ->
+ ColumnTransformerBuilder.constructInColumnTransformerForTest(
+ FloatType.FLOAT,
+ NO_VALUE_TRANSFORMERS,
+ ImmutableList.of(new LongLiteral("18"), new
LongLiteral("27"))));
+ // The raw ClassCastException must be converted into a semantic error
(701), so that it is
+ // reported as a user-facing query error instead of an internal failure.
+ assertTrue(e instanceof SemanticException);
+ assertEquals(
+ TSStatusCode.SEMANTIC_ERROR.getStatusCode(), ((SemanticException)
e).getErrorCode());
+ assertEquals(
+
String.format(CalcMessages.IN_LIST_LITERAL_CANNOT_BE_CONVERTED_TO_TYPE,
"INT32", "FLOAT"),
+ e.getMessage());
+ }
+
+ @Test
+ public void testInTransformerMismatchReportsFirstFailingLiteral() {
+ // The reported literal type is the first one the transformer cannot
convert, in list order.
+ Throwable e =
+ assertThrows(
+ SemanticException.class,
+ () ->
+ ColumnTransformerBuilder.constructInColumnTransformerForTest(
+ IntType.INT32,
+ NO_VALUE_TRANSFORMERS,
+ ImmutableList.<Literal>of(
+ new DoubleLiteral("1.5"), new GenericLiteral("DATE",
"2024-08-01"))));
+ assertTrue(e instanceof SemanticException);
+ assertEquals(
+ TSStatusCode.SEMANTIC_ERROR.getStatusCode(), ((SemanticException)
e).getErrorCode());
+ assertEquals(
+
String.format(CalcMessages.IN_LIST_LITERAL_CANNOT_BE_CONVERTED_TO_TYPE,
"DOUBLE", "INT32"),
+ e.getMessage());
+ }
+
+ @Test
+ public void testInTransformerMismatchReportsGenericLiteralType() {
+ // A GenericLiteral reports its own type name (here DATE) when it is the
failing item.
+ Throwable e =
+ assertThrows(
+ SemanticException.class,
+ () ->
+ ColumnTransformerBuilder.constructInColumnTransformerForTest(
+ IntType.INT32,
+ NO_VALUE_TRANSFORMERS,
+ ImmutableList.<Literal>of(new GenericLiteral("DATE",
"2024-08-01"))));
+ assertEquals(
+
String.format(CalcMessages.IN_LIST_LITERAL_CANNOT_BE_CONVERTED_TO_TYPE, "DATE",
"INT32"),
+ e.getMessage());
+ }
+}
diff --git
a/iotdb-core/datanode/src/main/java/org/apache/iotdb/db/queryengine/plan/relational/analyzer/ExpressionAnalyzer.java
b/iotdb-core/datanode/src/main/java/org/apache/iotdb/db/queryengine/plan/relational/analyzer/ExpressionAnalyzer.java
index 10733e34975..59498ee31ef 100644
---
a/iotdb-core/datanode/src/main/java/org/apache/iotdb/db/queryengine/plan/relational/analyzer/ExpressionAnalyzer.java
+++
b/iotdb-core/datanode/src/main/java/org/apache/iotdb/db/queryengine/plan/relational/analyzer/ExpressionAnalyzer.java
@@ -106,6 +106,7 @@ import
org.apache.iotdb.db.queryengine.plan.relational.metadata.Metadata;
import
org.apache.iotdb.db.queryengine.plan.relational.metadata.OperatorNotFoundException;
import org.apache.iotdb.db.queryengine.plan.relational.security.AccessControl;
import
org.apache.iotdb.db.queryengine.plan.relational.sql.ast.StackableAstVisitor;
+import org.apache.iotdb.db.queryengine.plan.relational.type.TypeCoercionUtils;
import org.apache.ratis.thirdparty.com.google.common.collect.HashMultimap;
import org.apache.ratis.thirdparty.com.google.common.collect.ImmutableList;
@@ -787,6 +788,9 @@ public class ExpressionAnalyzer {
@Override
public Type visitNullIfExpression(
NullIfExpression node, StackableAstVisitorContext<Context> context) {
+ // NULLIF itself has no execution operator yet, so it is rejected in the
execution layer
+ // (701: NULLIF is not supported yet). Only the argument types are
checked here, so that
+ // queries whose NULLIF never gets evaluated (e.g. pruned by the
optimizer) keep working.
Type firstType = process(node.getFirst(), context);
Type secondType = process(node.getSecond(), context);
@@ -2389,6 +2393,11 @@ public class ExpressionAnalyzer {
for (Type type : types) {
if (superType == UNKNOWN) {
superType = type;
+ } else if (TypeCoercionUtils.canCoerceTo(type, superType)) {
+ // The type is coercible to the common type, keep the common type.
+ } else if (TypeCoercionUtils.canCoerceTo(superType, type)) {
+ // The type is wider (e.g. INT32 widen to INT64), take it as the
common type.
+ superType = type;
} else {
if (!isTwoTypeComparable(Arrays.asList(superType, type))) {
throw new SemanticException(
diff --git
a/iotdb-core/datanode/src/main/java/org/apache/iotdb/db/queryengine/plan/relational/planner/IrTypeAnalyzer.java
b/iotdb-core/datanode/src/main/java/org/apache/iotdb/db/queryengine/plan/relational/planner/IrTypeAnalyzer.java
index 300d1b0a0a1..3c17fdf730a 100644
---
a/iotdb-core/datanode/src/main/java/org/apache/iotdb/db/queryengine/plan/relational/planner/IrTypeAnalyzer.java
+++
b/iotdb-core/datanode/src/main/java/org/apache/iotdb/db/queryengine/plan/relational/planner/IrTypeAnalyzer.java
@@ -337,16 +337,12 @@ public class IrTypeAnalyzer {
@Override
public Type visitCoalesceExpression(CoalesceExpression node, Context
context) {
- LinkedHashSet<Type> types =
- node.getOperands().stream()
- .map(operand -> process(operand, context))
- .collect(Collectors.toCollection(LinkedHashSet::new));
-
- if (types.size() != 1) {
- throw new SemanticException(
-
String.format(DataNodeQueryMessages.ALL_OPERANDS_MUST_HAVE_THE_SAME_TYPE_S,
types));
- }
- return setExpressionType(node, types.iterator().next());
+ Type type =
+ coerceToSingleType(
+ context,
+ node.getOperands(),
+ DataNodeQueryMessages.EXCEPTION_ALL_COALESCE_OPERANDS_A5374A6A);
+ return setExpressionType(node, type);
}
@Override
@@ -538,7 +534,7 @@ public class IrTypeAnalyzer {
private void coerceType(
Context context, Expression expression, Type expectedType,
Function<Type, String> message) {
Type actualType = process(expression, context);
- coerceType(expression, expectedType, actualType, message);
+ coerceType(expression, actualType, expectedType, message);
}
private Type coerceToSingleType(
@@ -560,8 +556,11 @@ public class IrTypeAnalyzer {
if (TypeCoercionUtils.canCoerceTo(current, superType)) {
continue;
}
+ // The current type is wider than the common type (e.g. INT32 widen to
INT64): take it
+ // as the new common type and keep checking the remaining branches.
if (TypeCoercionUtils.canCoerceTo(superType, current)) {
superType = current;
+ continue;
}
throw new SemanticException(
String.format(description + DataNodeQueryMessages.COLON_S_VS_S,
superType, current));
diff --git
a/iotdb-core/node-commons/src/main/java/org/apache/iotdb/commons/queryengine/plan/relational/sql/ast/BinaryLiteral.java
b/iotdb-core/node-commons/src/main/java/org/apache/iotdb/commons/queryengine/plan/relational/sql/ast/BinaryLiteral.java
index 38c6c4284b7..15d5ab55b4a 100644
---
a/iotdb-core/node-commons/src/main/java/org/apache/iotdb/commons/queryengine/plan/relational/sql/ast/BinaryLiteral.java
+++
b/iotdb-core/node-commons/src/main/java/org/apache/iotdb/commons/queryengine/plan/relational/sql/ast/BinaryLiteral.java
@@ -148,6 +148,11 @@ public class BinaryLiteral extends Literal {
return new Binary(value);
}
+ @Override
+ public String getLiteralTypeName() {
+ return "BLOB";
+ }
+
@Override
public long ramBytesUsed() {
return INSTANCE_SIZE
diff --git
a/iotdb-core/node-commons/src/main/java/org/apache/iotdb/commons/queryengine/plan/relational/sql/ast/BooleanLiteral.java
b/iotdb-core/node-commons/src/main/java/org/apache/iotdb/commons/queryengine/plan/relational/sql/ast/BooleanLiteral.java
index 6d9a9cc9a66..a2de71e8cd3 100644
---
a/iotdb-core/node-commons/src/main/java/org/apache/iotdb/commons/queryengine/plan/relational/sql/ast/BooleanLiteral.java
+++
b/iotdb-core/node-commons/src/main/java/org/apache/iotdb/commons/queryengine/plan/relational/sql/ast/BooleanLiteral.java
@@ -116,6 +116,11 @@ public class BooleanLiteral extends Literal {
return value;
}
+ @Override
+ public String getLiteralTypeName() {
+ return "BOOLEAN";
+ }
+
@Override
public long ramBytesUsed() {
return INSTANCE_SIZE
diff --git
a/iotdb-core/node-commons/src/main/java/org/apache/iotdb/commons/queryengine/plan/relational/sql/ast/DecimalLiteral.java
b/iotdb-core/node-commons/src/main/java/org/apache/iotdb/commons/queryengine/plan/relational/sql/ast/DecimalLiteral.java
index 0842b68e393..fe28bc388e8 100644
---
a/iotdb-core/node-commons/src/main/java/org/apache/iotdb/commons/queryengine/plan/relational/sql/ast/DecimalLiteral.java
+++
b/iotdb-core/node-commons/src/main/java/org/apache/iotdb/commons/queryengine/plan/relational/sql/ast/DecimalLiteral.java
@@ -107,6 +107,11 @@ public class DecimalLiteral extends Literal {
return new Binary(value.getBytes(StandardCharsets.UTF_8));
}
+ @Override
+ public String getLiteralTypeName() {
+ return "DECIMAL";
+ }
+
@Override
public long ramBytesUsed() {
return INSTANCE_SIZE
diff --git
a/iotdb-core/node-commons/src/main/java/org/apache/iotdb/commons/queryengine/plan/relational/sql/ast/DoubleLiteral.java
b/iotdb-core/node-commons/src/main/java/org/apache/iotdb/commons/queryengine/plan/relational/sql/ast/DoubleLiteral.java
index 3584dde5d02..2fa59235728 100644
---
a/iotdb-core/node-commons/src/main/java/org/apache/iotdb/commons/queryengine/plan/relational/sql/ast/DoubleLiteral.java
+++
b/iotdb-core/node-commons/src/main/java/org/apache/iotdb/commons/queryengine/plan/relational/sql/ast/DoubleLiteral.java
@@ -117,6 +117,11 @@ public class DoubleLiteral extends Literal {
return value;
}
+ @Override
+ public String getLiteralTypeName() {
+ return "DOUBLE";
+ }
+
@Override
public long ramBytesUsed() {
return INSTANCE_SIZE
diff --git
a/iotdb-core/node-commons/src/main/java/org/apache/iotdb/commons/queryengine/plan/relational/sql/ast/Expression.java
b/iotdb-core/node-commons/src/main/java/org/apache/iotdb/commons/queryengine/plan/relational/sql/ast/Expression.java
index 9afed2396f2..a7fcbd7ce31 100644
---
a/iotdb-core/node-commons/src/main/java/org/apache/iotdb/commons/queryengine/plan/relational/sql/ast/Expression.java
+++
b/iotdb-core/node-commons/src/main/java/org/apache/iotdb/commons/queryengine/plan/relational/sql/ast/Expression.java
@@ -175,6 +175,9 @@ public abstract class Expression extends Node {
case 33:
expression = new FloatLiteral(byteBuffer);
break;
+ case 35:
+ expression = new NullIfExpression(byteBuffer);
+ break;
default:
throw new IllegalArgumentException(
String.format(QueryMessages.INVALID_EXPRESSION_TYPE, type));
diff --git
a/iotdb-core/node-commons/src/main/java/org/apache/iotdb/commons/queryengine/plan/relational/sql/ast/FloatLiteral.java
b/iotdb-core/node-commons/src/main/java/org/apache/iotdb/commons/queryengine/plan/relational/sql/ast/FloatLiteral.java
index dc8c80abf11..54e5529c239 100644
---
a/iotdb-core/node-commons/src/main/java/org/apache/iotdb/commons/queryengine/plan/relational/sql/ast/FloatLiteral.java
+++
b/iotdb-core/node-commons/src/main/java/org/apache/iotdb/commons/queryengine/plan/relational/sql/ast/FloatLiteral.java
@@ -111,6 +111,11 @@ public class FloatLiteral extends Literal {
return value;
}
+ @Override
+ public String getLiteralTypeName() {
+ return "FLOAT";
+ }
+
@Override
public long ramBytesUsed() {
return INSTANCE_SIZE
diff --git
a/iotdb-core/node-commons/src/main/java/org/apache/iotdb/commons/queryengine/plan/relational/sql/ast/GenericLiteral.java
b/iotdb-core/node-commons/src/main/java/org/apache/iotdb/commons/queryengine/plan/relational/sql/ast/GenericLiteral.java
index 87df4eabab3..7fd5bc11088 100644
---
a/iotdb-core/node-commons/src/main/java/org/apache/iotdb/commons/queryengine/plan/relational/sql/ast/GenericLiteral.java
+++
b/iotdb-core/node-commons/src/main/java/org/apache/iotdb/commons/queryengine/plan/relational/sql/ast/GenericLiteral.java
@@ -132,6 +132,11 @@ public class GenericLiteral extends Literal {
return new Binary(value.getBytes(StandardCharsets.UTF_8));
}
+ @Override
+ public String getLiteralTypeName() {
+ return type;
+ }
+
@Override
public long ramBytesUsed() {
return INSTANCE_SIZE
diff --git
a/iotdb-core/node-commons/src/main/java/org/apache/iotdb/commons/queryengine/plan/relational/sql/ast/Literal.java
b/iotdb-core/node-commons/src/main/java/org/apache/iotdb/commons/queryengine/plan/relational/sql/ast/Literal.java
index 5f11f326fb8..a2470b082b9 100644
---
a/iotdb-core/node-commons/src/main/java/org/apache/iotdb/commons/queryengine/plan/relational/sql/ast/Literal.java
+++
b/iotdb-core/node-commons/src/main/java/org/apache/iotdb/commons/queryengine/plan/relational/sql/ast/Literal.java
@@ -44,4 +44,9 @@ public abstract class Literal extends Expression {
* @return a value that can be processed by IoTDB storage engine
*/
public abstract Object getTsValue();
+
+ /**
+ * @return the SQL type name of this literal, e.g. INT32, INT64, DOUBLE,
used in error messages
+ */
+ public abstract String getLiteralTypeName();
}
diff --git
a/iotdb-core/node-commons/src/main/java/org/apache/iotdb/commons/queryengine/plan/relational/sql/ast/LongLiteral.java
b/iotdb-core/node-commons/src/main/java/org/apache/iotdb/commons/queryengine/plan/relational/sql/ast/LongLiteral.java
index 694e7af0019..1ee221a681d 100644
---
a/iotdb-core/node-commons/src/main/java/org/apache/iotdb/commons/queryengine/plan/relational/sql/ast/LongLiteral.java
+++
b/iotdb-core/node-commons/src/main/java/org/apache/iotdb/commons/queryengine/plan/relational/sql/ast/LongLiteral.java
@@ -141,6 +141,11 @@ public class LongLiteral extends Literal {
return parsedValue;
}
+ @Override
+ public String getLiteralTypeName() {
+ return parsedValue >= Integer.MIN_VALUE && parsedValue <=
Integer.MAX_VALUE ? "INT32" : "INT64";
+ }
+
@Override
public long ramBytesUsed() {
return INSTANCE_SIZE
diff --git
a/iotdb-core/node-commons/src/main/java/org/apache/iotdb/commons/queryengine/plan/relational/sql/ast/NullIfExpression.java
b/iotdb-core/node-commons/src/main/java/org/apache/iotdb/commons/queryengine/plan/relational/sql/ast/NullIfExpression.java
index 10d279c2bd0..b8cfd1a58f2 100644
---
a/iotdb-core/node-commons/src/main/java/org/apache/iotdb/commons/queryengine/plan/relational/sql/ast/NullIfExpression.java
+++
b/iotdb-core/node-commons/src/main/java/org/apache/iotdb/commons/queryengine/plan/relational/sql/ast/NullIfExpression.java
@@ -24,6 +24,9 @@ import org.apache.iotdb.commons.i18n.QueryMessages;
import org.apache.ratis.thirdparty.com.google.common.collect.ImmutableList;
import org.apache.tsfile.utils.RamUsageEstimator;
+import java.io.DataOutputStream;
+import java.io.IOException;
+import java.nio.ByteBuffer;
import java.util.List;
import java.util.Objects;
@@ -40,13 +43,34 @@ public class NullIfExpression extends Expression {
public NullIfExpression(Expression first, Expression second) {
super(null);
this.first = requireNonNull(first,
QueryMessages.EXCEPTION_FIRST_IS_NULL_DC679129);
- this.second = requireNonNull(first,
QueryMessages.EXCEPTION_SECOND_IS_NULL_989FAA15);
+ this.second = requireNonNull(second,
QueryMessages.EXCEPTION_SECOND_IS_NULL_989FAA15);
}
public NullIfExpression(NodeLocation location, Expression first, Expression
second) {
super(requireNonNull(location,
QueryMessages.EXCEPTION_LOCATION_IS_NULL_F134D388));
this.first = requireNonNull(first,
QueryMessages.EXCEPTION_FIRST_IS_NULL_DC679129);
- this.second = requireNonNull(first,
QueryMessages.EXCEPTION_SECOND_IS_NULL_989FAA15);
+ this.second = requireNonNull(second,
QueryMessages.EXCEPTION_SECOND_IS_NULL_989FAA15);
+ }
+
+ public NullIfExpression(ByteBuffer byteBuffer) {
+ this(Expression.deserialize(byteBuffer),
Expression.deserialize(byteBuffer));
+ }
+
+ @Override
+ public TableExpressionType getExpressionType() {
+ return TableExpressionType.NULL_IF;
+ }
+
+ @Override
+ protected void serialize(ByteBuffer byteBuffer) {
+ Expression.serialize(first, byteBuffer);
+ Expression.serialize(second, byteBuffer);
+ }
+
+ @Override
+ protected void serialize(DataOutputStream stream) throws IOException {
+ Expression.serialize(first, stream);
+ Expression.serialize(second, stream);
}
public Expression getFirst() {
diff --git
a/iotdb-core/node-commons/src/main/java/org/apache/iotdb/commons/queryengine/plan/relational/sql/ast/NullLiteral.java
b/iotdb-core/node-commons/src/main/java/org/apache/iotdb/commons/queryengine/plan/relational/sql/ast/NullLiteral.java
index d111def6af6..cde8795e6c8 100644
---
a/iotdb-core/node-commons/src/main/java/org/apache/iotdb/commons/queryengine/plan/relational/sql/ast/NullLiteral.java
+++
b/iotdb-core/node-commons/src/main/java/org/apache/iotdb/commons/queryengine/plan/relational/sql/ast/NullLiteral.java
@@ -86,6 +86,11 @@ public class NullLiteral extends Literal {
return null;
}
+ @Override
+ public String getLiteralTypeName() {
+ return "UNKNOWN";
+ }
+
@Override
public long ramBytesUsed() {
return INSTANCE_SIZE
diff --git
a/iotdb-core/node-commons/src/main/java/org/apache/iotdb/commons/queryengine/plan/relational/sql/ast/StringLiteral.java
b/iotdb-core/node-commons/src/main/java/org/apache/iotdb/commons/queryengine/plan/relational/sql/ast/StringLiteral.java
index 63096646232..0812c9cfc98 100644
---
a/iotdb-core/node-commons/src/main/java/org/apache/iotdb/commons/queryengine/plan/relational/sql/ast/StringLiteral.java
+++
b/iotdb-core/node-commons/src/main/java/org/apache/iotdb/commons/queryengine/plan/relational/sql/ast/StringLiteral.java
@@ -116,6 +116,11 @@ public class StringLiteral extends Literal {
return new Binary(value.getBytes(StandardCharsets.UTF_8));
}
+ @Override
+ public String getLiteralTypeName() {
+ return "STRING";
+ }
+
@Override
public long ramBytesUsed() {
return INSTANCE_SIZE
diff --git
a/iotdb-core/node-commons/src/main/java/org/apache/iotdb/commons/queryengine/plan/relational/sql/ast/TableExpressionType.java
b/iotdb-core/node-commons/src/main/java/org/apache/iotdb/commons/queryengine/plan/relational/sql/ast/TableExpressionType.java
index 33aa23698d3..11b7b1724e0 100644
---
a/iotdb-core/node-commons/src/main/java/org/apache/iotdb/commons/queryengine/plan/relational/sql/ast/TableExpressionType.java
+++
b/iotdb-core/node-commons/src/main/java/org/apache/iotdb/commons/queryengine/plan/relational/sql/ast/TableExpressionType.java
@@ -53,7 +53,8 @@ public enum TableExpressionType {
ROW((short) 31),
EXTRACT((short) 32),
FLOAT_LITERAL((short) 33),
- TIME_DURATION_LITERAL((short) 34);
+ TIME_DURATION_LITERAL((short) 34),
+ NULL_IF((short) 35);
TableExpressionType(short type) {
this.type = type;
diff --git
a/iotdb-core/node-commons/src/main/java/org/apache/iotdb/commons/queryengine/plan/relational/sql/ast/TimeDurationLiteral.java
b/iotdb-core/node-commons/src/main/java/org/apache/iotdb/commons/queryengine/plan/relational/sql/ast/TimeDurationLiteral.java
index 8ceb5e9bad7..8b2a31b2c8b 100644
---
a/iotdb-core/node-commons/src/main/java/org/apache/iotdb/commons/queryengine/plan/relational/sql/ast/TimeDurationLiteral.java
+++
b/iotdb-core/node-commons/src/main/java/org/apache/iotdb/commons/queryengine/plan/relational/sql/ast/TimeDurationLiteral.java
@@ -62,6 +62,11 @@ public class TimeDurationLiteral extends Literal {
return value;
}
+ @Override
+ public String getLiteralTypeName() {
+ return "INTERVAL";
+ }
+
@Override
public int hashCode() {
return Objects.hash(value);