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 @@ -36,6 +36,7 @@
import org.apache.doris.nereids.rules.expression.ExpressionRuleType;
import org.apache.doris.nereids.rules.expression.ExpressionTraverseListener;
import org.apache.doris.nereids.rules.expression.ExpressionTraverseListenerFactory;
import org.apache.doris.nereids.rules.expression.check.CheckCast;
import org.apache.doris.nereids.trees.expressions.AggregateExpression;
import org.apache.doris.nereids.trees.expressions.And;
import org.apache.doris.nereids.trees.expressions.BinaryArithmetic;
Expand Down Expand Up @@ -501,6 +502,9 @@ public Expression visitCast(Cast cast, ExpressionRewriteContext context) {
}
Expression child = cast.child();
DataType dataType = cast.getDataType();
if (!CheckCast.check(child.getDataType(), dataType, SessionVariable.enableStrictCast())) {
return cast;

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This early return relies on a later CheckCast, but validation currently happens after rules that can erase constant expressions. For example:

Sort(CAST(CAST(20240229112233 AS BIGINT) AS TIMESTAMPTZ(6)))

is processed by EliminateOrderByConstant before expression normalization; because the invalid deterministic cast is still isConstant(), the sort key is removed and CheckCast never sees it. Similarly, NormalizeAggregate.eliminateGroupByConstant folds a GROUP BY-only occurrence, treats the unchanged cast as constant, and removes it before validation. Thus the corresponding ORDER BY and GROUP BY queries can be accepted while the projected expression in the new regression errors. The default fast INSERT ... VALUES analyzer also invokes this folder without any CheckCast and skips normal rewrite, so that path fails only during BE execution. Please enforce cast legality before constant-expression elimination/translation, and add regressions for these paths.

}
// todo: process other null case
if (child.isNullLiteral()) {
return new NullLiteral(dataType);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,7 @@
import org.apache.doris.nereids.types.DoubleType;
import org.apache.doris.nereids.types.FloatType;
import org.apache.doris.nereids.types.IntegerType;
import org.apache.doris.nereids.types.TimeStampTzType;
import org.apache.doris.nereids.types.TinyIntType;
import org.apache.doris.nereids.types.VarcharType;
import org.apache.doris.nereids.util.MemoTestUtils;
Expand Down Expand Up @@ -276,6 +277,9 @@ void testCastFold() {
Expression rewritten = executor.rewrite(c, context);
Literal expected = Literal.of((byte) 1);
Assertions.assertEquals(rewritten, expected);

Cast unsupportedCast = new Cast(new BigIntLiteral(20240229112233L), TimeStampTzType.of(6));
Assertions.assertEquals(unsupportedCast, executor.rewrite(unsupportedCast, context));
}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@
import org.apache.doris.nereids.types.StringType;
import org.apache.doris.nereids.types.StructField;
import org.apache.doris.nereids.types.StructType;
import org.apache.doris.nereids.types.TimeStampTzType;
import org.apache.doris.nereids.types.TimeV2Type;
import org.apache.doris.nereids.types.TinyIntType;
import org.apache.doris.nereids.types.VarcharType;
Expand All @@ -65,6 +66,12 @@ public void testCastBetweenVariantTypes() {
Assertions.assertTrue(CheckCast.check(v1Source, v1DifferentProperties, true));
}

@Test
public void testCastFromBigIntToTimeStampTz() {
Assertions.assertFalse(CheckCast.check(BigIntType.INSTANCE, TimeStampTzType.of(6), true));
Assertions.assertFalse(CheckCast.check(BigIntType.INSTANCE, TimeStampTzType.of(6), false));
}

@Test
public void testCastFromBoolean() {
// Strict mode
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,10 @@ suite("test_timestamptz_cast") {
qt_cast_from_string1 """
select cast("2020-01-01 23:59:59.999999+08:00" as timestamptz(5));
"""
test {
sql """select cast(cast(20240229112233 as bigint) as timestamptz(6));"""
exception "cannot cast BIGINT to TIMESTAMPTZ(6)"
}
sql " set debug_skip_fold_constant = true; "
qt_cast_from_string2 """
select cast("2020-01-01 00:00:00.123456+08:00" as timestamptz(5));
Expand Down
Loading