github-actions[bot] commented on code in PR #68830:
URL: https://github.com/apache/doris/pull/68830#discussion_r4244651493
##########
fe/fe-core/src/main/java/org/apache/doris/nereids/trees/expressions/functions/scalar/HourMinute.java:
##########
@@ -56,6 +61,15 @@ public HourMinute withChildren(List<Expression> children) {
return new HourMinute(getFunctionParams(children));
}
+ @Override
+ public Expression rewriteWhenAnalyze() {
+ Expression argument = getArgument(0);
+ if (!(argument.getDataType() instanceof TimeV2Type)) {
+ return this;
Review Comment:
[P2] Align negative-zero TIME formatting across FE and BE.
`Project(hour_minute('-00:00:00'))` now rewrites to `time_format(TIMEV2,
'%H:%i')`: FE folding keeps `TimeV2Literal.negative` and returns `-00:00`,
while BE receives numeric zero and returns `00:00` because its formatter
prefixes `-` only for `time < 0`. The other three new rewrites differ the same
way. Normalize zero's sign consistently and compare fold and runtime results
for this input.
##########
fe/fe-core/src/main/java/org/apache/doris/nereids/trees/expressions/functions/scalar/SecondMicrosecond.java:
##########
@@ -56,6 +61,15 @@ public SecondMicrosecond withChildren(List<Expression>
children) {
return new SecondMicrosecond(getFunctionParams(children));
}
+ @Override
+ public Expression rewriteWhenAnalyze() {
+ Expression argument = getArgument(0);
+ if (!(argument.getDataType() instanceof TimeV2Type)) {
+ return this;
Review Comment:
[P2] Preserve the valid TIMEV2 boundary fraction in BE. For
`Project(second_microsecond(CASE WHEN id > 0 THEN CAST('838:59:59.999999' AS
TIME(6)) ELSE CAST('00:00:00' AS TIME(6)) END))`, FE folds the branch CASTs to
TIMEV2 literals but leaves the row-dependent CASE and this
`time_format('%s.%f')` rewrite for BE. BE `TimeValue::microsecond` clamps the
positive branch at `MAX_TIME = 838:59:59.000000`, returning `59.000000`;
folding the constant call in FE returns `59.999999`. Align BE's bound with the
accepted TIME(6) range and test the mixed path.
##########
fe/fe-core/src/test/java/org/apache/doris/nereids/trees/expressions/functions/scalar/CompositeTimeExtractTest.java:
##########
@@ -0,0 +1,177 @@
+// 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.exceptions.AnalysisException;
+import org.apache.doris.nereids.exceptions.CastException;
+import org.apache.doris.nereids.parser.NereidsParser;
+import org.apache.doris.nereids.rules.analysis.ExpressionAnalyzer;
+import org.apache.doris.nereids.rules.expression.ExpressionRewriteTestHelper;
+import org.apache.doris.nereids.rules.expression.rules.FoldConstantRuleOnFE;
+import org.apache.doris.nereids.trees.expressions.Cast;
+import org.apache.doris.nereids.trees.expressions.Expression;
+import org.apache.doris.nereids.trees.expressions.SlotReference;
+import org.apache.doris.nereids.trees.expressions.functions.RewriteWhenAnalyze;
+import org.apache.doris.nereids.trees.expressions.literal.VarcharLiteral;
+import org.apache.doris.nereids.types.ArrayType;
+import org.apache.doris.nereids.types.CharType;
+import org.apache.doris.nereids.types.DataType;
+import org.apache.doris.nereids.types.DateTimeV2Type;
+import org.apache.doris.nereids.types.IntegerType;
+import org.apache.doris.nereids.types.StringType;
+import org.apache.doris.nereids.types.TimeStampNsType;
+import org.apache.doris.nereids.types.TimeV2Type;
+import org.apache.doris.nereids.types.VarcharType;
+import org.apache.doris.nereids.util.TypeCoercionUtils;
+import org.apache.doris.qe.SessionVariable;
+
+import com.google.common.collect.ImmutableList;
+import org.junit.jupiter.api.Assertions;
+import org.junit.jupiter.api.Test;
+import org.mockito.MockedStatic;
+import org.mockito.Mockito;
+
+import java.util.List;
+import java.util.function.Function;
+
+class CompositeTimeExtractTest extends ExpressionRewriteTestHelper {
+ private static final List<Function<Expression, ScalarFunction>> FUNCTIONS
= ImmutableList.of(
+ HourMinute::new, HourSecond::new, MinuteSecond::new,
SecondMicrosecond::new);
+
+ @Test
+ void testTimePrecisionAndExistingTemporalSignatures() {
+ for (Function<Expression, ScalarFunction> factory : FUNCTIONS) {
+ for (int scale = 0; scale <= 6; scale++) {
+ TimeV2Type timeType = TimeV2Type.of(scale);
+ ScalarFunction function = factory.apply(SlotReference.of("t",
timeType));
+ Assertions.assertEquals(timeType,
function.getSignature().getArgType(0));
+ Expression rewritten = ((RewriteWhenAnalyze)
function).rewriteWhenAnalyze();
+ Assertions.assertInstanceOf(TimeFormat.class, rewritten);
+ Assertions.assertEquals(timeType,
rewritten.child(0).getDataType());
+ }
+ for (DataType type :
ImmutableList.of(DateTimeV2Type.SYSTEM_DEFAULT,
+ DateTimeV2Type.MAX, TimeStampNsType.INSTANCE)) {
+ ScalarFunction function = factory.apply(SlotReference.of("dt",
type));
+ Assertions.assertEquals(type,
function.getSignature().getArgType(0));
+ Assertions.assertSame(function, ((RewriteWhenAnalyze)
function).rewriteWhenAnalyze());
+ }
+ }
+ }
+
+ @Test
+ void testTimeLiteralsSelectTimeInsteadOfDateTime() {
+ for (Function<Expression, ScalarFunction> factory : FUNCTIONS) {
+ for (String value : ImmutableList.of("12:34:56", "12:34:56.789123",
+ "00:00:00.000001", "-00:00:01.5", "123:34:56.123456")) {
+ ScalarFunction function = factory.apply(new
VarcharLiteral(value));
+ Assertions.assertTrue(function.getSignature().getArgType(0)
instanceof TimeV2Type, value);
+ }
+ ScalarFunction dateTime = factory.apply(new
VarcharLiteral("2024-01-02 12:34:56.789123"));
+ Assertions.assertEquals(DateTimeV2Type.MAX,
dateTime.getSignature().getArgType(0));
+ }
+ }
+
+ @Test
+ void testDirectAndExtractSyntaxUseTheSameTimeFormatter() {
+ NereidsParser parser = new NereidsParser();
+ for (String name : ImmutableList.of("hour_minute", "hour_second",
"minute_second", "second_microsecond")) {
+ for (String argument : ImmutableList.of("'12:34:56.789123'",
+ "cast('12:34:56.789123' as time(6))")) {
+ Expression direct = ExpressionAnalyzer.analyzeFunction(null,
null,
+ parser.parseExpression(name + "(" + argument + ")"));
+ Expression extract = ExpressionAnalyzer.analyzeFunction(null,
null,
+ parser.parseExpression("extract(" + name + " from " +
argument + ")"));
+ Assertions.assertInstanceOf(TimeFormat.class, direct);
+ Assertions.assertEquals(direct, extract);
+ Assertions.assertEquals(TimeV2Type.MAX,
direct.child(0).getDataType());
+ Assertions.assertTrue(direct.checkInputDataTypes().success());
+ }
+ }
+ }
+
+ @Test
+ void testOtherInputsKeepOriginalBinding() {
+ for (Function<Expression, ScalarFunction> factory : FUNCTIONS) {
+ ScalarFunction integer = factory.apply(SlotReference.of("i",
IntegerType.INSTANCE));
+ Assertions.assertTrue(integer.getSignature().getArgType(0)
instanceof DateTimeV2Type);
+ ScalarFunction array = factory.apply(SlotReference.of("a",
ArrayType.of(IntegerType.INSTANCE)));
+ Assertions.assertThrows(AnalysisException.class,
array::getSignature);
+ ScalarFunction volatileString = factory.apply(new Uuid());
+ Assertions.assertTrue(volatileString.getSignature().getArgType(0)
instanceof DateTimeV2Type);
+ }
+ }
+
+ @Test
+ void testStringColumnsUseTheSameBindingAsSimpleExtracts() {
+ for (Function<Expression, ScalarFunction> factory : FUNCTIONS) {
+ for (DataType type : ImmutableList.of(VarcharType.SYSTEM_DEFAULT,
+ StringType.INSTANCE, CharType.createCharType(64))) {
+ SlotReference argument = SlotReference.of("s", type);
+ ScalarFunction function = factory.apply(argument);
+ Assertions.assertEquals(new
Microsecond(argument).getSignature().getArgType(0),
+ function.getSignature().getArgType(0));
+ function = (ScalarFunction)
TypeCoercionUtils.processBoundFunction(function);
+ Expression rewritten = ((RewriteWhenAnalyze)
function).rewriteWhenAnalyze();
+ Assertions.assertSame(function, rewritten);
+ Assertions.assertInstanceOf(Cast.class, rewritten.child(0));
+ Assertions.assertSame(argument, rewritten.child(0).child(0));
+ Assertions.assertEquals(DateTimeV2Type.MAX,
rewritten.child(0).getDataType());
+ Assertions.assertTrue(rewritten.nullable());
+
Assertions.assertTrue(rewritten.checkInputDataTypes().success());
+ }
+ }
+ }
+
+ @Test
+ void testStringExpressionsKeepDateTimeBinding() {
+ NereidsParser parser = new NereidsParser();
+ for (String name : ImmutableList.of("hour_minute", "hour_second",
"minute_second", "second_microsecond")) {
+ for (String value : ImmutableList.of("12:34:56.789123",
"2024-01-02 12:34:56.789123")) {
+ String argument = "concat('" + value + "', '')";
+ Expression simple = ExpressionAnalyzer.analyzeFunction(null,
null,
+ parser.parseExpression("microsecond(" + argument +
")"));
+ for (String expression : ImmutableList.of(name + "(" +
argument + ")",
+ "extract(" + name + " from " + argument + ")")) {
+ Expression analyzed =
ExpressionAnalyzer.analyzeFunction(null, null,
+ parser.parseExpression(expression));
+ Assertions.assertInstanceOf(Cast.class, analyzed.child(0));
+ Assertions.assertEquals(simple.child(0),
analyzed.child(0));
+ Assertions.assertEquals(DateTimeV2Type.MAX,
analyzed.child(0).getDataType());
+ }
+ }
+ }
+ }
+
+ @Test
+ void testStrictErrorsInStringSourcesAreNotSwallowed() {
Review Comment:
[P2] Exercise the TRY_CAST change directly. This test and the group-by
regression use only `CAST`, and implicit coercion creates ordinary `Cast`, so
both still pass if the new `visitTryCast` catch-boundary change is reverted.
Add a strict-mode FE fold of `try_cast(cast('bad' as int) as varchar(20))` that
must propagate the inner error, plus `try_cast('bad' as int)` that must return
NULL; use GROUP BY for the SQL normalization path.
--
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]