ahmedsaid47 commented on code in PR #61500:
URL: https://github.com/apache/doris/pull/61500#discussion_r2959702225


##########
fe/fe-core/src/test/java/org/apache/doris/nereids/trees/expressions/functions/scalar/TypeOfTest.java:
##########
@@ -0,0 +1,97 @@
+// 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.functions.scalar;
+
+import org.apache.doris.nereids.parser.NereidsParser;
+import org.apache.doris.nereids.rules.expression.ExpressionRewriteTestHelper;
+import org.apache.doris.nereids.trees.expressions.Expression;
+import org.apache.doris.nereids.trees.expressions.Slot;
+import org.apache.doris.nereids.trees.expressions.literal.VarcharLiteral;
+import org.apache.doris.nereids.types.ArrayType;
+import org.apache.doris.nereids.types.DateTimeV2Type;
+import org.apache.doris.nereids.types.DecimalV3Type;
+import org.apache.doris.nereids.types.IntegerType;
+import org.apache.doris.nereids.types.MapType;
+import org.apache.doris.nereids.types.StructField;
+import org.apache.doris.nereids.types.StructType;
+import org.apache.doris.nereids.types.TimeStampTzType;
+
+import com.google.common.collect.ImmutableList;
+import org.junit.jupiter.api.Assertions;
+import org.junit.jupiter.api.Test;
+
+import java.util.HashMap;
+import java.util.Map;
+
+class TypeOfTest {
+
+    private static final NereidsParser PARSER = new NereidsParser();
+
+    @Test
+    void testAnalyzeTimeRewriteForTrinoExamples() {
+        assertTypeOfRewrite("typeof(123)", "integer");
+        assertTypeOfRewrite("typeof('cat')", "varchar(3)");
+        assertTypeOfRewrite("typeof(cast(1 as bigint))", "bigint");
+        assertTypeOfRewrite("typeof(cast(1 as varchar))", "varchar");
+        assertTypeOfRewrite("typeof(cast(null as varchar(10)))", 
"varchar(10)");
+        assertTypeOfRewrite("typeof(try_cast(null as varchar(10)))", 
"varchar(10)");
+        assertTypeOfRewrite("typeof(cast(null as char(3)))", "char(3)");
+        assertTypeOfRewrite("typeof(cast(null as decimal(5,1)))", 
"decimal(5,1)");
+        assertTypeOfRewrite("typeof(cast(null as decimal))", "decimal(38,0)");
+        assertTypeOfRewrite("typeof(try_cast(null as decimal))", 
"decimal(38,0)");
+        assertTypeOfRewrite("typeof(concat('ala', 'ma', 'kota'))", "varchar");
+        assertTypeOfRewrite("typeof(typeof(123))", "varchar");
+        assertTypeOfRewrite("typeof(2 + sin(2) + 2.3)", "double");
+    }

Review Comment:
   Added targeted assertions in 0cbb1d90 for the remaining mappings introduced 
by `TypeOfDisplayName`, including `float -> real`, `varbinary -> varbinary`, 
`datev2 -> date`, and `timev2 -> time(0)`.



##########
fe/fe-core/src/main/java/org/apache/doris/nereids/trees/expressions/Cast.java:
##########
@@ -47,25 +48,39 @@ public class Cast extends Expression implements 
UnaryExpression, Monotonic {
     protected final boolean isExplicitType; //FIXME: now not useful
 
     protected final DataType targetType;
+    protected final String explicitTypeSql;
 
     public Cast(Expression child, DataType targetType) {
         this(child, targetType, false);
     }
 
     public Cast(Expression child, DataType targetType, boolean isExplicitType) 
{
-        this(ImmutableList.of(child), targetType, isExplicitType);
+        this(child, targetType, isExplicitType, null);
+    }
+
+    public Cast(Expression child, DataType targetType, boolean isExplicitType, 
String explicitTypeSql) {
+        this(ImmutableList.of(child), targetType, isExplicitType, 
explicitTypeSql);
     }
 
     protected Cast(List<Expression> child, DataType targetType, boolean 
isExplicitType) {
+        this(child, targetType, isExplicitType, null);
+    }
+
+    protected Cast(List<Expression> child, DataType targetType, boolean 
isExplicitType, String explicitTypeSql) {
         super(child);
         this.targetType = Objects.requireNonNull(targetType, "targetType can 
not be null");
         this.isExplicitType = isExplicitType;
+        this.explicitTypeSql = explicitTypeSql;
     }
 
     public boolean isExplicitType() {
         return isExplicitType;
     }
 
+    public Optional<String> getExplicitTypeSql() {
+        return Optional.ofNullable(explicitTypeSql);
+    }

Review Comment:
   Fixed in 0cbb1d90. `Cast.equals()` and `computeHashCode()` now include both 
`isExplicitType` and `explicitTypeSql`, so casts with the same analyzed target 
type but different explicit type text no longer collide in expression-keyed 
rewrites/CSE.



##########
fe/fe-core/src/main/java/org/apache/doris/nereids/trees/expressions/TryCast.java:
##########
@@ -64,7 +74,7 @@ public boolean originCastNullable() {
     @Override
     public TryCast withChildren(List<Expression> children) {
         Preconditions.checkArgument(children.size() == 1);
-        return new TryCast(children, targetType, isExplicitType);
+        return new TryCast(children, targetType, isExplicitType, 
explicitTypeSql);
     }

Review Comment:
   Fixed in 0cbb1d90 as well. `TryCast` now inherits the metadata-aware 
equality semantics from `Cast`, and its hash code also includes the explicit 
type metadata so TRY_CAST expressions stay consistent with their behavior in 
rewrite/CSE paths.



-- 
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