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


##########
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:
   TryCast inherits the new `explicitTypeSql` state from Cast, but its 
`equals()`/`computeHashCode()` logic (via `super.equals(o)` and the override) 
still ignores `explicitTypeSql`. This can cause expression-key collisions and 
incorrect rewrite/CSE behavior for TRY_CAST paths (and therefore TYPEOF 
output). Update equality/hash to incorporate `explicitTypeSql` (and 
`isExplicitType`) consistently.



##########
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:
   `explicitTypeSql` is now part of Cast’s semantic state (it affects TYPEOF 
rewrite), but `equals()`/`computeHashCode()` currently ignore it. Since 
Expressions are widely used as HashMap/HashSet keys (e.g. CSE, rewrites), 
different casts like `CAST(x AS DECIMAL)` vs `CAST(x AS DECIMALV3)` can collide 
and cause incorrect reuse/dedup, potentially changing TYPEOF results. Include 
`explicitTypeSql` (and likely `isExplicitType`) in 
`equals()`/`computeHashCode()` to keep expression identity consistent with its 
behavior.



##########
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:
   TypeOfDisplayName introduces several Trino-specific mappings (e.g. 
float->`real`, varbinary, date/time/timestamptz formatting), but the current 
unit tests only cover a subset 
(ints/strings/decimals/arrays/maps/rows/timestamp). Add a few targeted 
assertions for the remaining newly introduced mappings to prevent regressions 
in dialect compatibility.



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