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


##########
fe/fe-core/src/main/java/org/apache/doris/nereids/util/TypeCoercionUtils.java:
##########
@@ -0,0 +1,292 @@
+// 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.util;
+
+import org.apache.doris.nereids.annotation.Developing;
+import org.apache.doris.nereids.trees.expressions.Cast;
+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.DataType;
+import org.apache.doris.nereids.types.DecimalType;
+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.NullType;
+import org.apache.doris.nereids.types.SmallIntType;
+import org.apache.doris.nereids.types.StringType;
+import org.apache.doris.nereids.types.TinyIntType;
+import org.apache.doris.nereids.types.VarcharType;
+import org.apache.doris.nereids.types.coercion.AbstractDataType;
+import org.apache.doris.nereids.types.coercion.CharacterType;
+import org.apache.doris.nereids.types.coercion.FractionalType;
+import org.apache.doris.nereids.types.coercion.IntegralType;
+import org.apache.doris.nereids.types.coercion.NumericType;
+import org.apache.doris.nereids.types.coercion.PrimitiveType;
+import org.apache.doris.nereids.types.coercion.TypeCollection;
+
+import com.google.common.collect.ImmutableList;
+import com.google.common.collect.Lists;
+import com.google.common.collect.Sets;
+
+import java.util.List;
+import java.util.Map;
+import java.util.Optional;
+import java.util.function.Supplier;
+import java.util.stream.Collectors;
+import java.util.stream.Stream;
+
+/**
+ * Utils for type coercion.
+ */
+public class TypeCoercionUtils {
+
+    /**
+     * numeric type precedence for type promotion.
+     */
+    public static final List<DataType> NUMERIC_PRECEDENCE = ImmutableList.of(
+            DoubleType.INSTANCE,
+            LargeIntType.INSTANCE,
+            FloatType.INSTANCE,
+            BigIntType.INSTANCE,
+            IntegerType.INSTANCE,
+            SmallIntType.INSTANCE,
+            TinyIntType.INSTANCE
+    );
+
+    /**
+     * Return Optional.empty() if cannot do implicit cast.
+     * TODO: datetime and date type
+     */
+    @Developing
+    public static Optional<DataType> implicitCast(DataType input, 
AbstractDataType expected) {
+        DataType returnType = null;
+        if (expected.acceptsType(input)) {
+            // If the expected type is already a parent of the input type, no 
need to cast.
+            return Optional.of(input);
+        }
+        if (expected instanceof TypeCollection) {
+            TypeCollection typeCollection = (TypeCollection) expected;
+            // use origin datatype first. use implicit cast instead if origin 
type cannot be accepted.
+            return Stream.<Supplier<Optional<DataType>>>of(
+                            () -> typeCollection.getTypes().stream()
+                                    .filter(e -> e.acceptsType(input))
+                                    .map(e -> input)
+                                    .findFirst(),
+                            () -> typeCollection.getTypes().stream()
+                                    .map(e -> implicitCast(input, e))
+                                    .filter(Optional::isPresent)
+                                    .map(Optional::get)
+                                    .findFirst())
+                    .map(Supplier::get)
+                    .filter(Optional::isPresent)
+                    .map(Optional::get)
+                    .findFirst();
+        }
+        if (input.isNullType()) {
+            // Cast null type (usually from null literals) into target types
+            returnType = expected.defaultConcreteType();
+        } else if (input.isNumericType() && expected instanceof DecimalType) {
+            // If input is a numeric type but not decimal, and we expect a 
decimal type,
+            // cast the input to decimal.
+            returnType = DecimalType.forType(input);
+        } else if (input.isNumericType() && expected instanceof NumericType) {
+            // For any other numeric types, implicitly cast to each other, 
e.g. bigint -> int, int -> bigint
+            returnType = (DataType) expected;
+        } else if (input.isStringType()) {
+            if (expected instanceof DecimalType) {
+                returnType = DecimalType.SYSTEM_DEFAULT;
+            } else if (expected instanceof NumericType) {
+                returnType = expected.defaultConcreteType();
+            }
+        } else if (input.isPrimitive() && !input.isStringType() && expected 
instanceof CharacterType) {
+            returnType = StringType.INSTANCE;
+        }
+
+        // could not do implicit cast, just return null. Throw exception in 
check analysis.
+        return Optional.ofNullable(returnType);
+    }
+
+    /**
+     * return ture if two type could do type coercion.
+     */
+    public static boolean canHandleTypeCoercion(DataType leftType, DataType 
rightType) {
+        if (leftType instanceof DecimalType && rightType instanceof NullType) {
+            return true;
+        }
+        if (leftType instanceof NullType && rightType instanceof DecimalType) {
+            return true;
+        }
+        if (!(leftType instanceof DecimalType) && !(rightType instanceof 
DecimalType) && !leftType.equals(rightType)) {
+            return true;
+        }
+        return false;
+    }
+
+
+
+    /**
+     * find the tightest common type for two type
+     */
+    @Developing
+    public static Optional<DataType> findTightestCommonType(DataType left, 
DataType right) {
+        // TODO: compatible with origin planner and BE
+        // TODO: when add new type, add it to here
+        DataType tightestCommonType = null;
+        if (left.equals(right)) {
+            tightestCommonType = left;
+        } else if (left instanceof NullType) {
+            tightestCommonType = right;

Review Comment:
   this is not nullable, but is NullType. NullType is just for NullLiteral



-- 
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: commits-unsubscr...@doris.apache.org

For queries about this service, please contact Infrastructure at:
us...@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscr...@doris.apache.org
For additional commands, e-mail: commits-h...@doris.apache.org

Reply via email to