Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,20 @@ public void testIfWithCastedDefaultType() {
DATABASE);
}

@Test
public void testNullIfNotSupported() {
// Originally reported: NULLIF(CAST(status AS STRING), CAST('normal' AS STRING)) fails with
// "301: Unsupported expression". It is unsupported, so report a clear semantic error during
// analysis instead of an execution-time failure.
String msg = "701: 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 testKind1Basic() {
String[] retArray = new String[] {"99,", "9999,", "9999,", "999,"};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,9 @@ public class IoTDBInTableIT {
"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)",
};

@BeforeClass
Expand Down Expand Up @@ -115,6 +118,38 @@ public void testCastException() {
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 msg =
TSStatusCode.SEMANTIC_ERROR.getStatusCode()
+ ": IN value and list items must be the same type";
tableAssertTestFail(
"select case when s3 = 18 or s3 = 27 then 1 else 0 end from table1", msg, DATABASE_NAME);
tableAssertTestFail(
"select case when s3 in (18, 27) then 1 else 0 end from table1", msg, DATABASE_NAME);
tableAssertTestFail(
"select count(device), sum(case when s4 = 21 or s4 = 30 then 1 else 0 end) from table1 group by device",
msg,
DATABASE_NAME);
tableAssertTestFail("select s3 in (18, 27) from table1", msg, DATABASE_NAME);
tableAssertTestFail("select s3 in (18, 27.5) from table1", msg, DATABASE_NAME);
tableAssertTestFail(
"select case when s2 in (1.5, 2.5) then 1 else 0 end from table1", msg, 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 =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,8 @@ private 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_VALUE_AND_LIST_ITEMS_MUST_BE_THE_SAME_TYPE =
"IN value and list items must be the same type: %s vs %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";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,8 @@ private CalcMessages() {}
"InputRowUtils 不应被实例化。";
public static final String IN_LIST_LITERAL_FOR_TIMESTAMP_TYPE_RESTRICTION =
"TIMESTAMP 的 InList 字面量只能是 LongLiteral、DoubleLiteral 和 GenericLiteral,当前为 %s";
public static final String IN_VALUE_AND_LIST_ITEMS_MUST_BE_THE_SAME_TYPE =
"IN 的值和列表项必须为相同类型:%s vs %s";
public static final String INVALID_AGGREGATION_FUNCTION = "无效的聚合函数:";
public static final String INVALID_TEXT_INPUT_FOR_BOOLEAN =
"布尔类型的文本输入无效:%s";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1526,9 +1526,31 @@ public ColumnTransformer visitInPredicate(InPredicate node, Context context) {

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 offending
// types instead of leaking the cast failure.
throw new SemanticException(
String.format(
CalcMessages.IN_VALUE_AND_LIST_ITEMS_MUST_BE_THE_SAME_TYPE,
childType,
describeLiteralTypes(values)));
}
}

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();
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -830,6 +830,7 @@ public final class DataNodeQueryMessages {
"Unknown sign: ";
public static final String DECIMALLITERAL_IS_NOT_SUPPORTED_YET =
"DecimalLiteral is not supported yet.";
public static final String NULLIF_IS_NOT_SUPPORTED_YET = "NULLIF is not supported yet.";
public static final String GENERICLITERAL_IS_NOT_SUPPORTED_YET =
"GenericLiteral is not supported yet.";
public static final String DISTINCT_IS_NOT_SUPPORTED_FOR_NON_AGGREGATION_FUNCTIONS =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -810,6 +810,7 @@ public final class DataNodeQueryMessages {
"未知的符号:";
public static final String DECIMALLITERAL_IS_NOT_SUPPORTED_YET =
"暂不支持 DecimalLiteral。";
public static final String NULLIF_IS_NOT_SUPPORTED_YET = "暂不支持 NULLIF 函数。";
public static final String GENERICLITERAL_IS_NOT_SUPPORTED_YET =
"暂不支持 GenericLiteral。";
public static final String DISTINCT_IS_NOT_SUPPORTED_FOR_NON_AGGREGATION_FUNCTIONS =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -787,18 +787,7 @@ public Type visitIsNotNullPredicate(
@Override
public Type visitNullIfExpression(
NullIfExpression node, StackableAstVisitorContext<Context> context) {
Type firstType = process(node.getFirst(), context);
Type secondType = process(node.getSecond(), context);

if (!firstType.equals(secondType)) {
throw new SemanticException(
String.format(
DataNodeQueryMessages.TYPES_ARE_NOT_COMPARABLE_WITH_NULLIF_S_VS_S,
firstType,
secondType));
}

return setExpressionType(node, firstType);
throw new SemanticException(DataNodeQueryMessages.NULLIF_IS_NOT_SUPPORTED_YET);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -538,7 +538,7 @@ public Type visitNode(Node node, Context context) {
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(
Expand All @@ -560,9 +560,9 @@ private Type coerceToSingleType(
if (TypeCoercionUtils.canCoerceTo(current, superType)) {
continue;
}
if (TypeCoercionUtils.canCoerceTo(superType, current)) {
superType = current;
}
// Report the pair being compared as-is: superType must not be overwritten before the
// message is built, otherwise an INT32-widenable pair such as (INT32, INT64) is reported
// as "INT64 vs INT64".
throw new SemanticException(
String.format(description + DataNodeQueryMessages.COLON_S_VS_S, superType, current));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,11 @@ public Object getTsValue() {
return new Binary(value);
}

@Override
public String getLiteralTypeName() {
return "BLOB";
}

@Override
public long ramBytesUsed() {
return INSTANCE_SIZE
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,11 @@ public Object getTsValue() {
return value;
}

@Override
public String getLiteralTypeName() {
return "BOOLEAN";
}

@Override
public long ramBytesUsed() {
return INSTANCE_SIZE
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,11 @@ public Object getTsValue() {
return new Binary(value.getBytes(StandardCharsets.UTF_8));
}

@Override
public String getLiteralTypeName() {
return "DECIMAL";
}

@Override
public long ramBytesUsed() {
return INSTANCE_SIZE
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,11 @@ public Object getTsValue() {
return value;
}

@Override
public String getLiteralTypeName() {
return "DOUBLE";
}

@Override
public long ramBytesUsed() {
return INSTANCE_SIZE
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,11 @@ public Object getTsValue() {
return value;
}

@Override
public String getLiteralTypeName() {
return "FLOAT";
}

@Override
public long ramBytesUsed() {
return INSTANCE_SIZE
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,11 @@ public Object getTsValue() {
return new Binary(value.getBytes(StandardCharsets.UTF_8));
}

@Override
public String getLiteralTypeName() {
return type;
}

@Override
public long ramBytesUsed() {
return INSTANCE_SIZE
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,4 +44,9 @@ public List<Node> getChildren() {
* @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();
}
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,11 @@ public Object getTsValue() {
return parsedValue;
}

@Override
public String getLiteralTypeName() {
return parsedValue >= Integer.MIN_VALUE && parsedValue <= Integer.MAX_VALUE ? "INT32" : "INT64";
}

@Override
public long ramBytesUsed() {
return INSTANCE_SIZE
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,13 +40,13 @@ 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 Expression getFirst() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,11 @@ public Object getTsValue() {
return null;
}

@Override
public String getLiteralTypeName() {
return "UNKNOWN";
}

@Override
public long ramBytesUsed() {
return INSTANCE_SIZE
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,11 @@ public Object getTsValue() {
return new Binary(value.getBytes(StandardCharsets.UTF_8));
}

@Override
public String getLiteralTypeName() {
return "STRING";
}

@Override
public long ramBytesUsed() {
return INSTANCE_SIZE
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,11 @@ public Object getTsValue() {
return value;
}

@Override
public String getLiteralTypeName() {
return "INTERVAL";
}

@Override
public int hashCode() {
return Objects.hash(value);
Expand Down