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,10 @@ public Expression visitCast(Cast cast, ExpressionRewriteContext context) {
}
Expression child = cast.child();
DataType dataType = cast.getDataType();
if (!CheckCast.check(child.getDataType(), dataType, SessionVariable.enableStrictCast())) {
throw new AnalysisException("cannot cast " + child.getDataType().toSql()
+ " to " + dataType.toSql());
}
// 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,11 @@ 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));
AnalysisException exception = Assertions.assertThrows(
AnalysisException.class, () -> executor.rewrite(unsupportedCast, context));
Assertions.assertEquals("cannot cast BIGINT to TIMESTAMPTZ(6)", exception.getMessage());
}

@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