From 803d3a1545c6f59b32cb983ed868f9eaec623bfe Mon Sep 17 00:00:00 2001 From: yiguolei Date: Sun, 26 Jan 2025 12:53:11 +0800 Subject: [PATCH] Revert "[fix](date_function) fix str_to_date function return wrong microsecond issue" (#47441) --- .../apache/doris/analysis/DateLiteral.java | 12 +-- .../doris/analysis/FunctionCallExpr.java | 6 +- .../DateTimeExtractAndTransform.java | 4 +- .../functions/scalar/StrToDate.java | 3 +- .../test_array_with_scale_type.out | 2 +- .../jdbc/test_oracle_jdbc_catalog.groovy | 2 +- .../test_date_function_v2.groovy | 84 ------------------- 7 files changed, 7 insertions(+), 106 deletions(-) delete mode 100644 regression-test/suites/nereids_p0/sql_functions/datetime_functions/test_date_function_v2.groovy diff --git a/fe/fe-core/src/main/java/org/apache/doris/analysis/DateLiteral.java b/fe/fe-core/src/main/java/org/apache/doris/analysis/DateLiteral.java index 7685a24ab1b770..67d99b1b06ea73 100644 --- a/fe/fe-core/src/main/java/org/apache/doris/analysis/DateLiteral.java +++ b/fe/fe-core/src/main/java/org/apache/doris/analysis/DateLiteral.java @@ -111,7 +111,6 @@ public class DateLiteral extends LiteralExpr { private static Map WEEK_DAY_NAME_DICT = Maps.newHashMap(); private static Set TIME_PART_SET = Sets.newHashSet(); private static final int[] DAYS_IN_MONTH = new int[] {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31}; - private static String MICRO_SECOND_FORMATTER = "%f"; private static final WeekFields weekFields = WeekFields.of(DayOfWeek.SUNDAY, 7); static { @@ -718,7 +717,7 @@ public String getStringValue() { int scale = ((ScalarType) type).getScalarScale(); long scaledMicroseconds = (long) (microsecond / SCALE_FACTORS[scale]); - if (scale > 0) { + if (scaledMicroseconds != 0) { dateTimeChars[19] = '.'; fillPaddedValue(dateTimeChars, 20, (int) scaledMicroseconds, scale); return new String(dateTimeChars, 0, 20 + scale); @@ -1063,10 +1062,6 @@ public static boolean hasTimePart(String format) { return format.chars().anyMatch(c -> TIME_PART_SET.contains((char) c)); } - public static boolean hasMicroSecondPart(String format) { - return format.indexOf(MICRO_SECOND_FORMATTER) != -1; - } - // Return the date stored in the dateliteral as pattern format. // eg : "%Y-%m-%d" or "%Y-%m-%d %H:%i:%s" public String dateFormat(String pattern) throws AnalysisException { @@ -1609,9 +1604,6 @@ public int fromDateFormatStr(String format, String value, boolean hasSubVal) thr case 'T': partUsed |= timePart; break; - case 'f': - partUsed |= fracPart; - break; default: break; } @@ -1673,7 +1665,7 @@ public int fromDateFormatStr(String format, String value, boolean hasSubVal) thr // Compute timestamp type if ((partUsed & datePart) != 0) { // Ymd part only - if (hasMicroSecondPart(format)) { + if ((partUsed & fracPart) != 0) { this.type = Type.DATETIMEV2_WITH_MAX_SCALAR; } else if ((partUsed & timePart) != 0) { this.type = ScalarType.getDefaultDateType(Type.DATETIME); diff --git a/fe/fe-core/src/main/java/org/apache/doris/analysis/FunctionCallExpr.java b/fe/fe-core/src/main/java/org/apache/doris/analysis/FunctionCallExpr.java index c87ef4d144f07e..8d71bdc35573a7 100644 --- a/fe/fe-core/src/main/java/org/apache/doris/analysis/FunctionCallExpr.java +++ b/fe/fe-core/src/main/java/org/apache/doris/analysis/FunctionCallExpr.java @@ -1993,11 +1993,7 @@ && collectChildReturnTypes()[0].isDecimalV3()) { Expr child1Result = getChild(1).getResultValue(false); if (child1Result instanceof StringLiteral) { if (DateLiteral.hasTimePart(child1Result.getStringValue())) { - if (DateLiteral.hasMicroSecondPart(child1Result.getStringValue())) { - this.type = Type.DATETIMEV2_WITH_MAX_SCALAR; - } else { - this.type = Type.DEFAULT_DATETIMEV2; - } + this.type = Type.DATETIMEV2_WITH_MAX_SCALAR; } else { this.type = Type.DATEV2; } diff --git a/fe/fe-core/src/main/java/org/apache/doris/nereids/trees/expressions/functions/executable/DateTimeExtractAndTransform.java b/fe/fe-core/src/main/java/org/apache/doris/nereids/trees/expressions/functions/executable/DateTimeExtractAndTransform.java index d0023ed2ff6a03..1f6f3afd7d4086 100644 --- a/fe/fe-core/src/main/java/org/apache/doris/nereids/trees/expressions/functions/executable/DateTimeExtractAndTransform.java +++ b/fe/fe-core/src/main/java/org/apache/doris/nereids/trees/expressions/functions/executable/DateTimeExtractAndTransform.java @@ -628,10 +628,8 @@ public static Expression strToDate(StringLikeLiteral str, StringLikeLiteral form if (org.apache.doris.analysis.DateLiteral.hasTimePart(format.getStringValue())) { DataType returnType = DataType.fromCatalogType(ScalarType.getDefaultDateType(Type.DATETIME)); if (returnType instanceof DateTimeV2Type) { - boolean hasMicroPart = org.apache.doris.analysis.DateLiteral - .hasMicroSecondPart(format.getStringValue()); return DateTimeV2Literal.fromJavaDateType(DateUtils.getTime(DateUtils.formatBuilder(format.getValue()) - .toFormatter(), str.getValue()), hasMicroPart ? 6 : 0); + .toFormatter(), str.getValue())); } else { return DateTimeLiteral.fromJavaDateType(DateUtils.getTime(DateUtils.formatBuilder(format.getValue()) .toFormatter(), str.getValue())); diff --git a/fe/fe-core/src/main/java/org/apache/doris/nereids/trees/expressions/functions/scalar/StrToDate.java b/fe/fe-core/src/main/java/org/apache/doris/nereids/trees/expressions/functions/scalar/StrToDate.java index c768a25ba3835e..ff014db6bca7a5 100644 --- a/fe/fe-core/src/main/java/org/apache/doris/nereids/trees/expressions/functions/scalar/StrToDate.java +++ b/fe/fe-core/src/main/java/org/apache/doris/nereids/trees/expressions/functions/scalar/StrToDate.java @@ -88,8 +88,7 @@ public FunctionSignature computeSignature(FunctionSignature signature) { if (getArgument(1) instanceof StringLikeLiteral) { if (DateLiteral.hasTimePart(((StringLikeLiteral) getArgument(1)).getStringValue())) { returnType = DataType.fromCatalogType(ScalarType.getDefaultDateType(Type.DATETIME)); - if (returnType.isDateTimeV2Type() - && DateLiteral.hasMicroSecondPart(((StringLikeLiteral) getArgument(1)).getStringValue())) { + if (returnType.isDateTimeV2Type()) { returnType = DataType.fromCatalogType(Type.DATETIMEV2_WITH_MAX_SCALAR); } } else { diff --git a/regression-test/data/query_p0/sql_functions/array_functions/test_array_with_scale_type.out b/regression-test/data/query_p0/sql_functions/array_functions/test_array_with_scale_type.out index 9388c8018789c5..446a96fa2ae73c 100644 --- a/regression-test/data/query_p0/sql_functions/array_functions/test_array_with_scale_type.out +++ b/regression-test/data/query_p0/sql_functions/array_functions/test_array_with_scale_type.out @@ -108,7 +108,7 @@ ["2022-12-02 22:23:25.000", "2022-12-02 22:23:23.998"] -- !select -- -["2022-12-02 22:23:25.000", "2022-12-02 22:23:23.998"] +["2022-12-02 22:23:25", "2022-12-02 22:23:23.998"] -- !select -- [] diff --git a/regression-test/suites/external_table_p0/jdbc/test_oracle_jdbc_catalog.groovy b/regression-test/suites/external_table_p0/jdbc/test_oracle_jdbc_catalog.groovy index cb2633e404c9e9..d2b3669ab457a3 100644 --- a/regression-test/suites/external_table_p0/jdbc/test_oracle_jdbc_catalog.groovy +++ b/regression-test/suites/external_table_p0/jdbc/test_oracle_jdbc_catalog.groovy @@ -134,7 +134,7 @@ suite("test_oracle_jdbc_catalog", "p0,external,oracle,external_docker,external_d order_qt_date4 """ select * from TEST_DATE where (T1 > '2022-01-21 00:00:00' and T1 < '2022-01-22 00:00:00') or (T1 > '2022-01-20 00:00:00' and T1 < '2022-01-23 00:00:00'); """ order_qt_date5 """ select * from TEST_DATE where T1 < '2022-01-22 00:00:00' or T1 = '2022-01-21 05:23:01'; """ order_qt_date6 """ select * from TEST_DATE where (T1 < '2022-01-22 00:00:00' or T1 > '2022-01-20 00:00:00') and (T1 < '2022-01-23 00:00:00' or T1 > '2022-01-19 00:00:00'); """ - order_qt_date7 """select * from TEST_TIMESTAMP where T2 < str_to_date('2020-12-21 12:34:56', '%Y-%m-%d %H:%i:%s.%f');""" + order_qt_date7 """select * from TEST_TIMESTAMP where T2 < str_to_date('2020-12-21 12:34:56', '%Y-%m-%d %H:%i:%s');""" // test nvl explain { diff --git a/regression-test/suites/nereids_p0/sql_functions/datetime_functions/test_date_function_v2.groovy b/regression-test/suites/nereids_p0/sql_functions/datetime_functions/test_date_function_v2.groovy deleted file mode 100644 index 8b24c9d9c3fefd..00000000000000 --- a/regression-test/suites/nereids_p0/sql_functions/datetime_functions/test_date_function_v2.groovy +++ /dev/null @@ -1,84 +0,0 @@ -// 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. - -suite("test_date_function_v2") { - sql """ - admin set frontend config ("enable_date_conversion"="true"); - """ - sql """SET enable_nereids_planner=true""" - - def tableName = "test_date_function_v2" - - sql """ DROP TABLE IF EXISTS ${tableName} """ - sql """ - CREATE TABLE IF NOT EXISTS ${tableName} ( - `id` INT, - `name` varchar(32), - `dt` varchar(32) - ) ENGINE=OLAP - UNIQUE KEY(`id`) - DISTRIBUTED BY HASH(`id`) BUCKETS 1 - PROPERTIES ( - "replication_allocation" = "tag.location.default: 1" - ) - """ - sql """ insert into ${tableName} values (3, 'Carl','2024-12-29 10:11:12') """ - def result1 = try_sql """ - select cast(str_to_date(dt, '%Y-%m-%d %H:%i:%s') as string) from ${tableName}; - """ - assertEquals(result1[0][0], "2024-12-29 10:11:12"); - - def result2 = try_sql """ - select cast(str_to_date(dt, '%Y-%m-%d %H:%i:%s.%f') as string) from ${tableName}; - """ - assertEquals(result2[0][0], "2024-12-29 10:11:12.000000"); - - def result3 = try_sql """ - select cast(str_to_date("2025-01-17 11:59:30", '%Y-%m-%d %H:%i:%s') as string); - """ - assertEquals(result3[0][0], "2025-01-17 11:59:30"); - - def result4 = try_sql """ - select cast(str_to_date("2025-01-17 11:59:30", '%Y-%m-%d %H:%i:%s.%f') as string); - """ - assertEquals(result4[0][0], "2025-01-17 11:59:30.000000"); - - // test legacy planner - sql """SET enable_nereids_planner=false""" - def result5 = try_sql """ - select cast(str_to_date(dt, '%Y-%m-%d %H:%i:%s') as string) from ${tableName}; - """ - assertEquals(result5[0][0], "2024-12-29 10:11:12"); - - result5 = try_sql """ - select cast(str_to_date(dt, '%Y-%m-%d %H:%i:%s.%f') as string) from ${tableName}; - """ - assertEquals(result5[0][0], "2024-12-29 10:11:12.000000"); - - result5 = try_sql """ - select cast(str_to_date('2025-01-17 11:59:30', '%Y-%m-%d %H:%i:%s') as string); - """ - assertEquals(result5[0][0], "2025-01-17 11:59:30"); - - result5 = try_sql """ - select cast(str_to_date('2025-01-17 11:59:30', '%Y-%m-%d %H:%i:%s.%f') as string); - """ - assertEquals(result5[0][0], "2025-01-17 11:59:30.000000"); - - - sql """ drop table ${tableName} """ -}