github-actions[bot] commented on code in PR #67890:
URL: https://github.com/apache/doris/pull/67890#discussion_r4002109364


##########
fe/fe-core/src/main/java/org/apache/doris/nereids/trees/expressions/literal/TimeV2Literal.java:
##########
@@ -319,7 +320,9 @@ protected Expression uncheckedCastTo(DataType targetType) 
throws AnalysisExcepti
             return new TimeStampNsLiteral(time.getYear(), time.getMonth(), 
time.getDay(),
                     time.getHour(), time.getMinute(), time.getSecond(), 
time.getMicroSecond() * 1000L);
         } else if (targetType.isDateTimeV2Type()) {
-            return time;
+            return new DateTimeV2Literal((DateTimeV2Type) targetType,
+                    time.getYear(), time.getMonth(), time.getDay(), 
time.getHour(), time.getMinute(),
+                    time.getSecond(), time.getMicroSecond());
         }
         return super.uncheckedCastTo(targetType);

Review Comment:
   [P1] Preserve FE/BE parity at source-rounding boundaries
   
   This target-scale rounding composes with FE's binary-double TIME parser and 
can make a folded constant disagree with the same runtime cast. For 
`MICROSECOND(CAST(CAST('00:00:00.1264995' AS TIME(6)) AS DATETIMEV2(3)))`, FE 
computes `126499.49999999999` before `Math.round`, stores 126499 us, and this 
constructor yields 126000; BE's digit parser rounds the seventh digit to 126500 
us, after which the new `transform_date_scale(3, 6, ...)` yields 127000. Please 
align FE TIME fractional parsing with BE's exact digit/next-digit rule (or 
avoid folding until aligned) and add a constant-versus-VARCHAR regression 
across this boundary.



##########
regression-test/suites/datatype_p0/date/test_from_unixtime.groovy:
##########
@@ -143,12 +175,106 @@ suite("test_from_unixtime") {
         sql """ SELECT MICROSECOND(FROM_UNIXTIME(k1)) FROM test1; """
         contains "microsecond_from_unixtime"
     }
+    qt_microsecond_explicit_cast_projection """
+        SELECT id, MICROSECOND(CAST(FROM_UNIXTIME(ts) AS DATETIMEV2(3)))
+        FROM test_from_unixtime_explicit_cast ORDER BY id;
+    """
+    qt_microsecond_explicit_cast_filter """
+        SELECT id FROM test_from_unixtime_explicit_cast
+        WHERE MICROSECOND(CAST(FROM_UNIXTIME(ts) AS DATETIMEV2(3))) = 123000
+        ORDER BY id;
+    """
+    explain {
+        sql """
+            SELECT MICROSECOND(CAST(FROM_UNIXTIME(ts) AS DATETIMEV2(3)))
+            FROM test_from_unixtime_explicit_cast;
+        """
+        notContains "microsecond_from_unixtime"
+    }
+    qt_time_fields_explicit_lossy_cast """
+        SELECT id,
+            HOUR(CAST(FROM_UNIXTIME(ts) AS DATETIMEV2(3))),
+            MINUTE(CAST(FROM_UNIXTIME(ts) AS DATETIMEV2(3))),
+            SECOND(CAST(FROM_UNIXTIME(ts) AS DATETIMEV2(3))),
+            HOUR(CAST(FROM_UNIXTIME(ts) AS DATETIMEV2(0))),
+            MINUTE(CAST(FROM_UNIXTIME(ts) AS DATETIMEV2(0))),
+            SECOND(CAST(FROM_UNIXTIME(ts) AS DATETIMEV2(0)))
+        FROM test_from_unixtime_explicit_cast ORDER BY id;
+    """
+    explain {
+        sql """
+            SELECT
+                HOUR(CAST(FROM_UNIXTIME(ts) AS DATETIMEV2(3))),
+                MINUTE(CAST(FROM_UNIXTIME(ts) AS DATETIMEV2(3))),
+                SECOND(CAST(FROM_UNIXTIME(ts) AS DATETIMEV2(3))),
+                HOUR(CAST(FROM_UNIXTIME(ts) AS DATETIMEV2(0))),
+                MINUTE(CAST(FROM_UNIXTIME(ts) AS DATETIMEV2(0))),
+                SECOND(CAST(FROM_UNIXTIME(ts) AS DATETIMEV2(0)))
+            FROM test_from_unixtime_explicit_cast;
+        """
+        notContains "hour_from_unixtime"
+        notContains "minute_from_unixtime"
+        notContains "second_from_unixtime"
+    }
+    explain {
+        sql """
+            SELECT
+                HOUR(CAST(FROM_UNIXTIME(ts) AS DATETIMEV2(6))),
+                MINUTE(CAST(FROM_UNIXTIME(ts) AS DATETIMEV2(6))),
+                SECOND(CAST(FROM_UNIXTIME(ts) AS DATETIMEV2(6))),
+                MICROSECOND(CAST(FROM_UNIXTIME(ts) AS DATETIMEV2(6)))
+            FROM test_from_unixtime_explicit_cast;
+        """
+        contains "hour_from_unixtime"
+        contains "minute_from_unixtime"
+        contains "second_from_unixtime"
+        contains "microsecond_from_unixtime"
+    }
+    explain {
+        sql """
+            SELECT
+                HOUR(CAST(FROM_UNIXTIME(k0) AS DATETIME)),
+                SECOND(CAST(FROM_UNIXTIME(k0) AS DATETIMEV2(0)))
+            FROM test1;
+        """
+        contains "hour_from_unixtime"
+        contains "second_from_unixtime"
+    }
+    explain {
+        sql """
+            SELECT id FROM test_from_unixtime_explicit_cast
+            WHERE MICROSECOND(CAST(FROM_UNIXTIME(ts) AS DATETIMEV2(3))) = 
123000;
+        """
+        notContains "microsecond_from_unixtime"
+    }
     explain {
         sql """SELECT MICROSECOND(FROM_UNIXTIME(k1, 'yyyy-MM-dd HH:mm:ss')) 
FROM test1;"""
         notContains "microsecond_from_unixtime"
     }
     testFoldConst("SELECT MICROSECOND_FROM_UNIXTIME(1145.14);")
     testFoldConst("SELECT MICROSECOND_FROM_UNIXTIME(NULL);")
     testFoldConst("SELECT MICROSECOND_FROM_UNIXTIME(32536771200);")
+
+    sql "set debug_skip_fold_constant = false;"
+    qt_timev2_to_datetimev2_fe_fold """

Review Comment:
   [P2] Guard this current-date fold against midnight
   
   Each TIMEV2-to-DATETIMEV2 fold calls `LocalDateTime.now(...)` independently. 
Normally the scale-3 operand carries from day D to D+1 while the scale-6 
operand remains on D, but if the two folds straddle midnight both can land on 
the same date, so this checked-in `DATEDIFF = 1` becomes nondeterministic. 
Existing TIME-to-DATETIME regressions use 
`waitUntilSafeExecutionTime("NOT_CROSS_DAY_BOUNDARY", 2)` for this dependency; 
please add that guard immediately before this block or formulate the carry 
check without two independent current-date captures.



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