morrySnow commented on code in PR #53697:
URL: https://github.com/apache/doris/pull/53697#discussion_r2230020590


##########
fe/fe-core/src/main/java/org/apache/doris/nereids/rules/expression/check/CheckCast.java:
##########
@@ -25,23 +25,239 @@
 import org.apache.doris.nereids.trees.expressions.Expression;
 import org.apache.doris.nereids.types.AggStateType;
 import org.apache.doris.nereids.types.ArrayType;
+import org.apache.doris.nereids.types.BigIntType;
+import org.apache.doris.nereids.types.BitmapType;
+import org.apache.doris.nereids.types.BooleanType;
 import org.apache.doris.nereids.types.DataType;
+import org.apache.doris.nereids.types.DateTimeType;
+import org.apache.doris.nereids.types.DateTimeV2Type;
+import org.apache.doris.nereids.types.DateType;
+import org.apache.doris.nereids.types.DateV2Type;
+import org.apache.doris.nereids.types.DecimalV2Type;
+import org.apache.doris.nereids.types.DecimalV3Type;
+import org.apache.doris.nereids.types.DoubleType;
+import org.apache.doris.nereids.types.FloatType;
+import org.apache.doris.nereids.types.HllType;
+import org.apache.doris.nereids.types.IPv4Type;
+import org.apache.doris.nereids.types.IPv6Type;
+import org.apache.doris.nereids.types.IntegerType;
 import org.apache.doris.nereids.types.JsonType;
+import org.apache.doris.nereids.types.LargeIntType;
 import org.apache.doris.nereids.types.MapType;
+import org.apache.doris.nereids.types.SmallIntType;
 import org.apache.doris.nereids.types.StructField;
 import org.apache.doris.nereids.types.StructType;
+import org.apache.doris.nereids.types.TimeV2Type;
+import org.apache.doris.nereids.types.TinyIntType;
 import org.apache.doris.nereids.types.coercion.CharacterType;
 import org.apache.doris.nereids.types.coercion.PrimitiveType;
+import org.apache.doris.qe.SessionVariable;
 
 import com.google.common.collect.ImmutableList;
+import com.google.common.collect.Maps;
+import com.google.common.collect.Sets;
 
 import java.util.List;
+import java.util.Map;
+import java.util.Set;
 
 /**
  * check cast valid
  */
 public class CheckCast implements ExpressionPatternRuleFactory {
     public static CheckCast INSTANCE = new CheckCast();
+    private static Map<Class<? extends DataType>, Set<Class<? extends 
DataType>>> strictForbiddenCast;
+    private static Map<Class<? extends DataType>, Set<Class<? extends 
DataType>>> unStrictAllowedCast;
+
+    static {
+        Set<Class<? extends DataType>> forbiddenTypes = Sets.newHashSet();
+        strictForbiddenCast = Maps.newHashMap();
+        unStrictAllowedCast = Maps.newHashMap();
+
+        /*------------------------------FOR STRICT 
CAST--------------------------------------*/
+        // Boolean
+        forbiddenTypes.add(DateType.class);

Review Comment:
   it is better to use white-list



##########
fe/fe-core/src/main/java/org/apache/doris/nereids/rules/expression/check/CheckCast.java:
##########
@@ -76,6 +286,20 @@ private static boolean check(DataType originalType, 
DataType targetType) {
         if (originalType.equals(targetType)) {
             return true;
         }
+        // New check strict and un-strict cast logic, the check logic is not 
completed yet.
+        // So for now, if the new check logic return false,
+        // we return false to disable this cast, otherwise, we still go 
through the old check logic.
+        if (strictForbiddenCast.containsKey(originalType.getClass())
+                && 
strictForbiddenCast.get(originalType.getClass()).contains(targetType.getClass()))
 {
+            if (SessionVariable.enableStrictCast()) {

Review Comment:
   get `enableStrictCast` from `ExpressionRewriteContext` or other context as 
far as possible to avoid get it from thread local variable for better 
perfermance



##########
fe/fe-core/src/test/java/org/apache/doris/nereids/rules/expression/check/CheckCastTest.java:
##########
@@ -0,0 +1,245 @@
+// 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.doris.nereids.rules.expression.check;
+
+import org.apache.doris.nereids.types.ArrayType;
+import org.apache.doris.nereids.types.BigIntType;
+import org.apache.doris.nereids.types.BitmapType;
+import org.apache.doris.nereids.types.BooleanType;
+import org.apache.doris.nereids.types.DataType;
+import org.apache.doris.nereids.types.DateTimeV2Type;
+import org.apache.doris.nereids.types.DateType;
+import org.apache.doris.nereids.types.DecimalV2Type;
+import org.apache.doris.nereids.types.DecimalV3Type;
+import org.apache.doris.nereids.types.DoubleType;
+import org.apache.doris.nereids.types.FloatType;
+import org.apache.doris.nereids.types.HllType;
+import org.apache.doris.nereids.types.IPv4Type;
+import org.apache.doris.nereids.types.IPv6Type;
+import org.apache.doris.nereids.types.IntegerType;
+import org.apache.doris.nereids.types.LargeIntType;
+import org.apache.doris.nereids.types.MapType;
+import org.apache.doris.nereids.types.NullType;
+import org.apache.doris.nereids.types.SmallIntType;
+import org.apache.doris.nereids.types.StructType;
+import org.apache.doris.nereids.types.TimeV2Type;
+import org.apache.doris.nereids.types.TinyIntType;
+import org.apache.doris.nereids.types.VariantType;
+import org.apache.doris.qe.SessionVariable;
+
+import org.junit.jupiter.api.Assertions;
+import org.junit.jupiter.api.Test;
+import org.mockito.MockedStatic;
+import org.mockito.Mockito;
+
+public class CheckCastTest {

Review Comment:
   This UT is written a bit messy. The classification logic behind the three 
test functions isn't clear. It could be categorized by Original type, Target 
type, or Cast Mode. Grouping tests directly by each original type enum, and 
covering all target type results under both loose and strict mode, might be the 
most readable approach.



##########
fe/fe-core/src/test/java/org/apache/doris/nereids/rules/expression/check/CheckCastTest.java:
##########
@@ -0,0 +1,245 @@
+// 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.doris.nereids.rules.expression.check;
+
+import org.apache.doris.nereids.types.ArrayType;
+import org.apache.doris.nereids.types.BigIntType;
+import org.apache.doris.nereids.types.BitmapType;
+import org.apache.doris.nereids.types.BooleanType;
+import org.apache.doris.nereids.types.DataType;
+import org.apache.doris.nereids.types.DateTimeV2Type;
+import org.apache.doris.nereids.types.DateType;
+import org.apache.doris.nereids.types.DecimalV2Type;
+import org.apache.doris.nereids.types.DecimalV3Type;
+import org.apache.doris.nereids.types.DoubleType;
+import org.apache.doris.nereids.types.FloatType;
+import org.apache.doris.nereids.types.HllType;
+import org.apache.doris.nereids.types.IPv4Type;
+import org.apache.doris.nereids.types.IPv6Type;
+import org.apache.doris.nereids.types.IntegerType;
+import org.apache.doris.nereids.types.LargeIntType;
+import org.apache.doris.nereids.types.MapType;
+import org.apache.doris.nereids.types.NullType;
+import org.apache.doris.nereids.types.SmallIntType;
+import org.apache.doris.nereids.types.StructType;
+import org.apache.doris.nereids.types.TimeV2Type;
+import org.apache.doris.nereids.types.TinyIntType;
+import org.apache.doris.nereids.types.VariantType;
+import org.apache.doris.qe.SessionVariable;
+
+import org.junit.jupiter.api.Assertions;
+import org.junit.jupiter.api.Test;
+import org.mockito.MockedStatic;
+import org.mockito.Mockito;
+
+public class CheckCastTest {
+    @Test
+    public void testCommonCheckCast() {
+        // 1. Variant type.
+        DataType originType = VariantType.INSTANCE;
+        DataType targetType = ArrayType.of(IntegerType.INSTANCE);
+        Assertions.assertTrue(CheckCast.check(originType, targetType));
+        targetType = IntegerType.INSTANCE;
+        Assertions.assertTrue(CheckCast.check(originType, targetType));
+        targetType = MapType.SYSTEM_DEFAULT;
+        Assertions.assertFalse(CheckCast.check(originType, targetType));
+
+        // 2. Null type.
+        originType = NullType.INSTANCE;
+        Assertions.assertTrue(CheckCast.check(originType, targetType));
+
+        // 3. Same type.
+        originType = MapType.SYSTEM_DEFAULT;
+        Assertions.assertTrue(CheckCast.check(originType, targetType));
+
+        // 4. ipv4 can't cast to boolean, numeric, date like and time
+        checkCastToBasicType(IPv4Type.INSTANCE);
+
+        // 5. ipv6 can't cast to boolean, numeric, date like and time
+        checkCastToBasicType(IPv6Type.INSTANCE);
+
+        // 6. bitmap can't cast to boolean, numeric, date like and time
+        checkCastToBasicType(BitmapType.INSTANCE);
+        checkCastToIpType(BitmapType.INSTANCE);
+
+        // 7. bitmap can't cast to boolean, numeric, date like and time
+        checkCastToBasicType(HllType.INSTANCE);
+        checkCastToIpType(HllType.INSTANCE);
+
+        // 8. array can't cast to boolean, numeric, date like and time
+        checkCastToBasicType(ArrayType.SYSTEM_DEFAULT);
+        checkCastToIpType(ArrayType.SYSTEM_DEFAULT);
+
+        // 9. array can't cast to boolean, numeric, date like and time
+        checkCastToBasicType(MapType.SYSTEM_DEFAULT);
+        checkCastToIpType(MapType.SYSTEM_DEFAULT);
+
+        // 10. array can't cast to boolean, numeric, date like and time
+        checkCastToBasicType(StructType.SYSTEM_DEFAULT);
+        checkCastToIpType(StructType.SYSTEM_DEFAULT);
+
+        // 11. boolean can't cast to date like and time
+        originType = BooleanType.INSTANCE;
+        targetType = DateType.INSTANCE;
+        Assertions.assertFalse(CheckCast.check(originType, targetType));
+        targetType = DateTimeV2Type.MAX;
+        Assertions.assertFalse(CheckCast.check(originType, targetType));
+        targetType = TimeV2Type.MAX;
+        Assertions.assertFalse(CheckCast.check(originType, targetType));
+        checkCastToIpType(BooleanType.INSTANCE);
+
+        // 12. date like, time and ip can't cast to boolean
+        originType = DateType.INSTANCE;
+        targetType = BooleanType.INSTANCE;
+        Assertions.assertFalse(CheckCast.check(originType, targetType));
+        originType = DateTimeV2Type.MAX;
+        Assertions.assertFalse(CheckCast.check(originType, targetType));
+        originType = TimeV2Type.MAX;
+        Assertions.assertFalse(CheckCast.check(originType, targetType));
+        originType = IPv4Type.INSTANCE;
+        Assertions.assertFalse(CheckCast.check(originType, targetType));
+        originType = IPv6Type.INSTANCE;
+        Assertions.assertFalse(CheckCast.check(originType, targetType));
+    }
+
+    private void checkCastToBasicType(DataType originType) {
+        DataType targetType = BooleanType.INSTANCE;
+        Assertions.assertFalse(CheckCast.check(originType, targetType));
+        targetType = TinyIntType.INSTANCE;
+        Assertions.assertFalse(CheckCast.check(originType, targetType));

Review Comment:
   It would be clearer not to use local variables
   ```suggestion
           Assertions.assertFalse(CheckCast.check(originType, 
TinyIntType.INSTANCE));
   ```



##########
fe/fe-core/src/test/java/org/apache/doris/nereids/trees/expressions/CastTest.java:
##########
@@ -0,0 +1,568 @@
+// 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.doris.nereids.trees.expressions;
+
+import org.apache.doris.nereids.types.BigIntType;
+import org.apache.doris.nereids.types.BooleanType;
+import org.apache.doris.nereids.types.CharType;
+import org.apache.doris.nereids.types.DateTimeType;
+import org.apache.doris.nereids.types.DateTimeV2Type;
+import org.apache.doris.nereids.types.DateType;
+import org.apache.doris.nereids.types.DecimalV2Type;
+import org.apache.doris.nereids.types.DecimalV3Type;
+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.LargeIntType;
+import org.apache.doris.nereids.types.SmallIntType;
+import org.apache.doris.nereids.types.StringType;
+import org.apache.doris.nereids.types.TimeV2Type;
+import org.apache.doris.nereids.types.TinyIntType;
+import org.apache.doris.nereids.types.VarcharType;
+import org.apache.doris.qe.ConnectContext;
+import org.apache.doris.qe.SessionVariable;
+
+import org.junit.jupiter.api.Assertions;
+import org.junit.jupiter.api.Test;
+import org.mockito.MockedStatic;
+import org.mockito.Mockito;
+
+public class CastTest {
+    @Test
+    public void testNullable() {

Review Comment:
   let each small part be a identity UT test. we could split it by mode or 
original type



##########
fe/fe-core/src/main/java/org/apache/doris/nereids/rules/expression/check/CheckCast.java:
##########
@@ -76,6 +286,20 @@ private static boolean check(DataType originalType, 
DataType targetType) {
         if (originalType.equals(targetType)) {
             return true;
         }
+        // New check strict and un-strict cast logic, the check logic is not 
completed yet.
+        // So for now, if the new check logic return false,
+        // we return false to disable this cast, otherwise, we still go 
through the old check logic.
+        if (strictForbiddenCast.containsKey(originalType.getClass())
+                && 
strictForbiddenCast.get(originalType.getClass()).contains(targetType.getClass()))
 {
+            if (SessionVariable.enableStrictCast()) {
+                return false;
+            }
+            if (!unStrictAllowedCast.containsKey(originalType.getClass())
+                    || 
!unStrictAllowedCast.get(originalType.getClass()).contains(targetType.getClass()))
 {
+                return false;
+            }
+        }

Review Comment:
   for better perfermance
   ```
   if (strict) {
     strict logic
   } else {
     loose logic
   }
   ```



##########
fe/fe-core/src/test/java/org/apache/doris/nereids/trees/expressions/literal/BooleanLiteralTest.java:
##########
@@ -0,0 +1,120 @@
+// 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.doris.nereids.trees.expressions.literal;
+
+import org.apache.doris.nereids.exceptions.CastException;
+import org.apache.doris.nereids.trees.expressions.Expression;
+import org.apache.doris.nereids.types.BigIntType;
+import org.apache.doris.nereids.types.BooleanType;
+import org.apache.doris.nereids.types.DecimalV2Type;
+import org.apache.doris.nereids.types.DecimalV3Type;
+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.LargeIntType;
+import org.apache.doris.nereids.types.SmallIntType;
+import org.apache.doris.nereids.types.TinyIntType;
+
+import org.junit.jupiter.api.Assertions;
+import org.junit.jupiter.api.Test;
+
+public class BooleanLiteralTest {
+
+    @Test
+    public void testUncheckedCast() {
+        BooleanLiteral aTrue = BooleanLiteral.TRUE;
+        BooleanLiteral aFalse = BooleanLiteral.FALSE;
+        Assertions.assertSame(aTrue.uncheckedCastTo(BooleanType.INSTANCE), 
aTrue);
+        Assertions.assertSame(aFalse.uncheckedCastTo(BooleanType.INSTANCE), 
aFalse);
+
+        Expression expression = aTrue.uncheckedCastTo(FloatType.INSTANCE);
+        Assertions.assertTrue(expression instanceof FloatLiteral);
+        Assertions.assertEquals(1, ((FloatLiteral) expression).getValue());
+        expression = aFalse.uncheckedCastTo(FloatType.INSTANCE);
+        Assertions.assertTrue(expression instanceof FloatLiteral);
+        Assertions.assertEquals(0, ((FloatLiteral) expression).getValue());
+
+        expression = aTrue.uncheckedCastTo(DoubleType.INSTANCE);
+        Assertions.assertTrue(expression instanceof DoubleLiteral);
+        Assertions.assertEquals(1, ((DoubleLiteral) expression).getValue());
+        expression = aFalse.uncheckedCastTo(DoubleType.INSTANCE);
+        Assertions.assertTrue(expression instanceof DoubleLiteral);
+        Assertions.assertEquals(0, ((DoubleLiteral) expression).getValue());
+
+        expression = aTrue.uncheckedCastTo(DecimalV2Type.SYSTEM_DEFAULT);
+        Assertions.assertTrue(expression instanceof DecimalLiteral);
+        Assertions.assertEquals(1, ((DecimalLiteral) 
expression).getValue().intValue());
+        expression = aFalse.uncheckedCastTo(DecimalV2Type.SYSTEM_DEFAULT);
+        Assertions.assertTrue(expression instanceof DecimalLiteral);
+        Assertions.assertEquals(0, ((DecimalLiteral) 
expression).getValue().intValue());
+
+        expression = aTrue.uncheckedCastTo(DecimalV3Type.SYSTEM_DEFAULT);
+        Assertions.assertTrue(expression instanceof DecimalV3Literal);
+        Assertions.assertEquals(1, ((DecimalV3Literal) 
expression).getValue().intValue());
+        expression = aFalse.uncheckedCastTo(DecimalV3Type.SYSTEM_DEFAULT);
+        Assertions.assertTrue(expression instanceof DecimalV3Literal);
+        Assertions.assertEquals(0, ((DecimalV3Literal) 
expression).getValue().intValue());
+
+        try {
+            aTrue.uncheckedCastTo(DecimalV2Type.createDecimalV2Type(1, 1));
+        } catch (Exception e) {
+            Assertions.assertTrue(e instanceof CastException);
+        }
+
+        try {
+            aTrue.uncheckedCastTo(DecimalV3Type.createDecimalV3Type(2, 2));
+        } catch (Exception e) {
+            Assertions.assertTrue(e instanceof CastException);
+        }

Review Comment:
   use assertions.assertThrow



##########
fe/fe-core/src/test/java/org/apache/doris/nereids/rules/expression/check/CheckCastTest.java:
##########
@@ -0,0 +1,245 @@
+// 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.doris.nereids.rules.expression.check;
+
+import org.apache.doris.nereids.types.ArrayType;
+import org.apache.doris.nereids.types.BigIntType;
+import org.apache.doris.nereids.types.BitmapType;
+import org.apache.doris.nereids.types.BooleanType;
+import org.apache.doris.nereids.types.DataType;
+import org.apache.doris.nereids.types.DateTimeV2Type;
+import org.apache.doris.nereids.types.DateType;
+import org.apache.doris.nereids.types.DecimalV2Type;
+import org.apache.doris.nereids.types.DecimalV3Type;
+import org.apache.doris.nereids.types.DoubleType;
+import org.apache.doris.nereids.types.FloatType;
+import org.apache.doris.nereids.types.HllType;
+import org.apache.doris.nereids.types.IPv4Type;
+import org.apache.doris.nereids.types.IPv6Type;
+import org.apache.doris.nereids.types.IntegerType;
+import org.apache.doris.nereids.types.LargeIntType;
+import org.apache.doris.nereids.types.MapType;
+import org.apache.doris.nereids.types.NullType;
+import org.apache.doris.nereids.types.SmallIntType;
+import org.apache.doris.nereids.types.StructType;
+import org.apache.doris.nereids.types.TimeV2Type;
+import org.apache.doris.nereids.types.TinyIntType;
+import org.apache.doris.nereids.types.VariantType;
+import org.apache.doris.qe.SessionVariable;
+
+import org.junit.jupiter.api.Assertions;
+import org.junit.jupiter.api.Test;
+import org.mockito.MockedStatic;
+import org.mockito.Mockito;
+
+public class CheckCastTest {
+    @Test
+    public void testCommonCheckCast() {
+        // 1. Variant type.
+        DataType originType = VariantType.INSTANCE;
+        DataType targetType = ArrayType.of(IntegerType.INSTANCE);
+        Assertions.assertTrue(CheckCast.check(originType, targetType));
+        targetType = IntegerType.INSTANCE;
+        Assertions.assertTrue(CheckCast.check(originType, targetType));
+        targetType = MapType.SYSTEM_DEFAULT;
+        Assertions.assertFalse(CheckCast.check(originType, targetType));
+
+        // 2. Null type.
+        originType = NullType.INSTANCE;
+        Assertions.assertTrue(CheckCast.check(originType, targetType));
+
+        // 3. Same type.
+        originType = MapType.SYSTEM_DEFAULT;
+        Assertions.assertTrue(CheckCast.check(originType, targetType));
+
+        // 4. ipv4 can't cast to boolean, numeric, date like and time
+        checkCastToBasicType(IPv4Type.INSTANCE);
+
+        // 5. ipv6 can't cast to boolean, numeric, date like and time
+        checkCastToBasicType(IPv6Type.INSTANCE);
+
+        // 6. bitmap can't cast to boolean, numeric, date like and time
+        checkCastToBasicType(BitmapType.INSTANCE);
+        checkCastToIpType(BitmapType.INSTANCE);
+
+        // 7. bitmap can't cast to boolean, numeric, date like and time
+        checkCastToBasicType(HllType.INSTANCE);
+        checkCastToIpType(HllType.INSTANCE);
+
+        // 8. array can't cast to boolean, numeric, date like and time
+        checkCastToBasicType(ArrayType.SYSTEM_DEFAULT);
+        checkCastToIpType(ArrayType.SYSTEM_DEFAULT);
+
+        // 9. array can't cast to boolean, numeric, date like and time
+        checkCastToBasicType(MapType.SYSTEM_DEFAULT);
+        checkCastToIpType(MapType.SYSTEM_DEFAULT);
+
+        // 10. array can't cast to boolean, numeric, date like and time
+        checkCastToBasicType(StructType.SYSTEM_DEFAULT);
+        checkCastToIpType(StructType.SYSTEM_DEFAULT);
+
+        // 11. boolean can't cast to date like and time
+        originType = BooleanType.INSTANCE;
+        targetType = DateType.INSTANCE;
+        Assertions.assertFalse(CheckCast.check(originType, targetType));
+        targetType = DateTimeV2Type.MAX;
+        Assertions.assertFalse(CheckCast.check(originType, targetType));
+        targetType = TimeV2Type.MAX;
+        Assertions.assertFalse(CheckCast.check(originType, targetType));
+        checkCastToIpType(BooleanType.INSTANCE);
+
+        // 12. date like, time and ip can't cast to boolean
+        originType = DateType.INSTANCE;
+        targetType = BooleanType.INSTANCE;
+        Assertions.assertFalse(CheckCast.check(originType, targetType));
+        originType = DateTimeV2Type.MAX;
+        Assertions.assertFalse(CheckCast.check(originType, targetType));
+        originType = TimeV2Type.MAX;
+        Assertions.assertFalse(CheckCast.check(originType, targetType));
+        originType = IPv4Type.INSTANCE;
+        Assertions.assertFalse(CheckCast.check(originType, targetType));
+        originType = IPv6Type.INSTANCE;
+        Assertions.assertFalse(CheckCast.check(originType, targetType));
+    }
+
+    private void checkCastToBasicType(DataType originType) {
+        DataType targetType = BooleanType.INSTANCE;
+        Assertions.assertFalse(CheckCast.check(originType, targetType));
+        targetType = TinyIntType.INSTANCE;
+        Assertions.assertFalse(CheckCast.check(originType, targetType));
+        targetType = SmallIntType.INSTANCE;
+        Assertions.assertFalse(CheckCast.check(originType, targetType));
+        targetType = IntegerType.INSTANCE;
+        Assertions.assertFalse(CheckCast.check(originType, targetType));
+        targetType = BigIntType.INSTANCE;
+        Assertions.assertFalse(CheckCast.check(originType, targetType));
+        targetType = LargeIntType.INSTANCE;
+        Assertions.assertFalse(CheckCast.check(originType, targetType));
+        targetType = FloatType.INSTANCE;
+        Assertions.assertFalse(CheckCast.check(originType, targetType));
+        targetType = DoubleType.INSTANCE;
+        Assertions.assertFalse(CheckCast.check(originType, targetType));
+        targetType = DecimalV2Type.createDecimalV2Type(15, 0);
+        Assertions.assertFalse(CheckCast.check(originType, targetType));
+        targetType = DecimalV3Type.createDecimalV3Type(15, 0);
+        Assertions.assertFalse(CheckCast.check(originType, targetType));
+        targetType = DateType.INSTANCE;
+        Assertions.assertFalse(CheckCast.check(originType, targetType));
+        targetType = DateTimeV2Type.MAX;
+        Assertions.assertFalse(CheckCast.check(originType, targetType));
+        targetType = TimeV2Type.INSTANCE;
+        Assertions.assertFalse(CheckCast.check(originType, targetType));
+    }
+
+    private void checkCastToIpType(DataType originType) {

Review Comment:
   change name to `checkShouldNotCastToIpType`



##########
fe/fe-core/src/test/java/org/apache/doris/nereids/trees/expressions/literal/BooleanLiteralTest.java:
##########
@@ -0,0 +1,120 @@
+// 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.doris.nereids.trees.expressions.literal;
+
+import org.apache.doris.nereids.exceptions.CastException;
+import org.apache.doris.nereids.trees.expressions.Expression;
+import org.apache.doris.nereids.types.BigIntType;
+import org.apache.doris.nereids.types.BooleanType;
+import org.apache.doris.nereids.types.DecimalV2Type;
+import org.apache.doris.nereids.types.DecimalV3Type;
+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.LargeIntType;
+import org.apache.doris.nereids.types.SmallIntType;
+import org.apache.doris.nereids.types.TinyIntType;
+
+import org.junit.jupiter.api.Assertions;
+import org.junit.jupiter.api.Test;
+
+public class BooleanLiteralTest {
+
+    @Test
+    public void testUncheckedCast() {
+        BooleanLiteral aTrue = BooleanLiteral.TRUE;
+        BooleanLiteral aFalse = BooleanLiteral.FALSE;
+        Assertions.assertSame(aTrue.uncheckedCastTo(BooleanType.INSTANCE), 
aTrue);
+        Assertions.assertSame(aFalse.uncheckedCastTo(BooleanType.INSTANCE), 
aFalse);
+
+        Expression expression = aTrue.uncheckedCastTo(FloatType.INSTANCE);
+        Assertions.assertTrue(expression instanceof FloatLiteral);
+        Assertions.assertEquals(1, ((FloatLiteral) expression).getValue());
+        expression = aFalse.uncheckedCastTo(FloatType.INSTANCE);
+        Assertions.assertTrue(expression instanceof FloatLiteral);
+        Assertions.assertEquals(0, ((FloatLiteral) expression).getValue());
+
+        expression = aTrue.uncheckedCastTo(DoubleType.INSTANCE);
+        Assertions.assertTrue(expression instanceof DoubleLiteral);
+        Assertions.assertEquals(1, ((DoubleLiteral) expression).getValue());
+        expression = aFalse.uncheckedCastTo(DoubleType.INSTANCE);
+        Assertions.assertTrue(expression instanceof DoubleLiteral);
+        Assertions.assertEquals(0, ((DoubleLiteral) expression).getValue());
+
+        expression = aTrue.uncheckedCastTo(DecimalV2Type.SYSTEM_DEFAULT);
+        Assertions.assertTrue(expression instanceof DecimalLiteral);
+        Assertions.assertEquals(1, ((DecimalLiteral) 
expression).getValue().intValue());
+        expression = aFalse.uncheckedCastTo(DecimalV2Type.SYSTEM_DEFAULT);
+        Assertions.assertTrue(expression instanceof DecimalLiteral);
+        Assertions.assertEquals(0, ((DecimalLiteral) 
expression).getValue().intValue());
+
+        expression = aTrue.uncheckedCastTo(DecimalV3Type.SYSTEM_DEFAULT);
+        Assertions.assertTrue(expression instanceof DecimalV3Literal);
+        Assertions.assertEquals(1, ((DecimalV3Literal) 
expression).getValue().intValue());
+        expression = aFalse.uncheckedCastTo(DecimalV3Type.SYSTEM_DEFAULT);
+        Assertions.assertTrue(expression instanceof DecimalV3Literal);

Review Comment:
   use assertInstance



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to