Skip to content

Commit bb8c52c

Browse files
branch-4.0: [fix](function)Let second and microsecond functions deal time literal #56659 (#58419)
Cherry-picked from #56659 Co-authored-by: dwdwqfwe <[email protected]>
1 parent 692dd73 commit bb8c52c

File tree

6 files changed

+144
-11
lines changed

6 files changed

+144
-11
lines changed

be/src/vec/functions/function_time_value_to_field.cpp

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
#include "vec/data_types/data_type_number.h"
2323
#include "vec/data_types/data_type_time.h"
2424
#include "vec/functions/function.h"
25+
#include "vec/functions/function_date_or_datetime_computation.h"
2526
#include "vec/functions/simple_function_factory.h"
2627
#include "vec/runtime/time_value.h"
2728
#include "vec/utils/template_helpers.hpp"
@@ -83,10 +84,16 @@ struct SecondImpl {
8384
static inline auto execute(const TimeValue::TimeType& t) { return TimeValue::second(t); }
8485
};
8586

87+
struct MicroImpl {
88+
constexpr static auto name = "microsecond";
89+
static inline auto execute(const TimeValue::TimeType& t) { return TimeValue::microsecond(t); }
90+
};
91+
8692
void register_function_time_value_field(SimpleFunctionFactory& factory) {
8793
factory.register_function<FunctionTimeValueToField<DataTypeInt32, HourImpl>>();
8894
factory.register_function<FunctionTimeValueToField<DataTypeInt8, MintuImpl>>();
8995
factory.register_function<FunctionTimeValueToField<DataTypeInt8, SecondImpl>>();
96+
factory.register_function<FunctionTimeValueToField<DataTypeInt32, MicroImpl>>();
9097
}
9198
#include "common/compile_check_end.h"
9299
} // namespace doris::vectorized

fe/fe-core/src/main/java/org/apache/doris/nereids/trees/expressions/functions/executable/DateTimeExtractAndTransform.java

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -201,6 +201,14 @@ public static Expression second(DateTimeV2Literal date) {
201201
return new TinyIntLiteral(((byte) date.getSecond()));
202202
}
203203

204+
/**
205+
* Executable time extract second
206+
*/
207+
@ExecFunction(name = "second")
208+
public static Expression second(TimeV2Literal time) {
209+
return new TinyIntLiteral(((byte) time.getSecond()));
210+
}
211+
204212
/**
205213
* Executable datetime extract microsecond
206214
*/
@@ -209,6 +217,14 @@ public static Expression microsecond(DateTimeV2Literal date) {
209217
return new IntegerLiteral(((int) date.getMicroSecond()));
210218
}
211219

220+
/**
221+
* Executable time extract microsecond
222+
*/
223+
@ExecFunction(name = "microsecond")
224+
public static Expression microsecond(TimeV2Literal time) {
225+
return new IntegerLiteral(((int) time.getMicroSecond()));
226+
}
227+
212228
/**
213229
* Executable datetime extract dayofyear
214230
*/

fe/fe-core/src/main/java/org/apache/doris/nereids/trees/expressions/functions/scalar/Microsecond.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
import org.apache.doris.nereids.trees.expressions.visitor.ExpressionVisitor;
3030
import org.apache.doris.nereids.types.DateTimeV2Type;
3131
import org.apache.doris.nereids.types.IntegerType;
32+
import org.apache.doris.nereids.types.TimeV2Type;
3233

3334
import com.google.common.base.Preconditions;
3435
import com.google.common.collect.ImmutableList;
@@ -42,7 +43,8 @@ public class Microsecond extends ScalarFunction
4243
implements UnaryExpression, ExplicitlyCastableSignature, PropagateNullable, Monotonic {
4344

4445
public static final List<FunctionSignature> SIGNATURES = ImmutableList.of(
45-
FunctionSignature.ret(IntegerType.INSTANCE).args(DateTimeV2Type.WILDCARD)
46+
FunctionSignature.ret(IntegerType.INSTANCE).args(DateTimeV2Type.WILDCARD),
47+
FunctionSignature.ret(IntegerType.INSTANCE).args(TimeV2Type.WILDCARD)
4648
);
4749

4850
/**

regression-test/data/query_p0/sql_functions/datetime_functions/test_date_function.out

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -300,9 +300,39 @@ February
300300
-- !sql --
301301
0
302302

303+
-- !sql_second_time_with_micro1 --
304+
1
305+
306+
-- !sql_second_time_with_micro2 --
307+
59
308+
309+
-- !sql_second_time_with_micro3 --
310+
0
311+
312+
-- !sql_second_time_with_micro4 --
313+
45
314+
303315
-- !sql --
304316
767890
305317

318+
-- !sql_microsecond_special_format --
319+
\N
320+
321+
-- !sql_microsecond_time_with_micro1 --
322+
123456
323+
324+
-- !sql_microsecond_time_with_micro2 --
325+
999999
326+
327+
-- !sql_microsecond_time_with_micro3 --
328+
1
329+
330+
-- !sql_microsecond_time_with_micro4 --
331+
500000
332+
333+
-- !sql_microsecond_time_with_micro5 --
334+
789000
335+
306336
-- !sql --
307337
2014-12-21T12:34:56
308338

@@ -600,6 +630,37 @@ February
600630

601631
-- !sql --
602632
767891
633+
123456
634+
1
635+
999999
636+
637+
-- !sql --
638+
767891 1999-01-02T10:11:12.767891
639+
999999 2024-12-31T23:59:59.999999
640+
641+
-- !sql --
642+
1 2024-01-01T00:00:00.000001
643+
644+
-- !sql_second_simple --
645+
56
646+
647+
-- !sql_second_datetime_with_micro --
648+
56
649+
650+
-- !sql_microsecond_datetime_with_micro --
651+
789123
652+
653+
-- !sql_table_second_all --
654+
\N
655+
\N
656+
\N
657+
56
658+
659+
-- !sql_table_microsecond_all --
660+
\N
661+
\N
662+
\N
663+
789123
603664

604665
-- !sql --
605666
1 2022-08-01

regression-test/suites/nereids_syntax_p0/test_cast_datetime.groovy

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -73,14 +73,6 @@ suite("test_cast_datetime") {
7373
"date_add('10-30',1)",
7474
"date_add('10-30 10:10:10',1)",
7575
"date_add('2020-01 00:00:00', 1)",
76-
"MICROSECOND('invalid_time')",
77-
"MICROSECOND('12.34.56.123456')",
78-
"MICROSECOND('12:34:56')",
79-
"MICROSECOND('12:34:56.1234')",
80-
"MICROSECOND('12345')",
81-
"MICROSECOND('12:34:56.1')",
82-
"MICROSECOND('12:34:56.01')",
83-
"MICROSECOND('12:34:56.abcdef')",
8476
"MICROSECOND('NaN')",
8577
"MonthName('abcd-ef-gh')",
8678
"DATE('2023-02-28 24:00:00')",

regression-test/suites/query_p0/sql_functions/datetime_functions/test_date_function.groovy

Lines changed: 57 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -428,9 +428,21 @@ suite("test_date_function") {
428428
// SECOND
429429
qt_sql """ select second('2018-12-31 23:59:59') """
430430
qt_sql """ select second('2018-12-31 00:00:00') """
431-
431+
// Test SECOND function with time strings containing microseconds
432+
qt_sql_second_time_with_micro1 """ select second('12:00:01.12') """
433+
qt_sql_second_time_with_micro2 """ select second('23:59:59.999999') """
434+
qt_sql_second_time_with_micro3 """ select second('00:00:00.000001') """
435+
qt_sql_second_time_with_micro4 """ select second('15:30:45.5') """
436+
432437
// MICROSECOND
433438
qt_sql """ select microsecond(cast('1999-01-02 10:11:12.767890' as datetimev2(6))) """
439+
// Test MICROSECOND function with time strings containing microseconds
440+
qt_sql_microsecond_special_format """ select microsecond('120000.12') """
441+
qt_sql_microsecond_time_with_micro1 """ select microsecond('12:00:00.123456') """
442+
qt_sql_microsecond_time_with_micro2 """ select microsecond('23:59:59.999999') """
443+
qt_sql_microsecond_time_with_micro3 """ select microsecond('00:00:00.000001') """
444+
qt_sql_microsecond_time_with_micro4 """ select microsecond('15:30:45.5') """
445+
qt_sql_microsecond_time_with_micro5 """ select microsecond('12:34:56.789') """
434446

435447
// STR_TO_DATE
436448
sql """ truncate table ${tableName} """
@@ -616,8 +628,50 @@ suite("test_date_function") {
616628
CREATE TABLE IF NOT EXISTS ${tableName} (k1 datetimev2(6)) duplicate key(k1) distributed by hash(k1) buckets 1 properties('replication_num' = '1');
617629
"""
618630
sql """ insert into ${tableName} values('1999-01-02 10:11:12.767891') """
631+
sql """ insert into ${tableName} values('2023-05-15 08:30:45.123456') """
632+
sql """ insert into ${tableName} values('2024-01-01 00:00:00.000001') """
633+
sql """ insert into ${tableName} values('2024-12-31 23:59:59.999999') """
619634

620-
qt_sql """ select microsecond(k1) from ${tableName}; """
635+
qt_sql """ select microsecond(k1) from ${tableName} order by k1; """
636+
637+
// Test microsecond extraction from different datetime formats
638+
qt_sql """ select microsecond(k1), k1 from ${tableName} where microsecond(k1) > 500000 order by k1; """
639+
qt_sql """ select microsecond(k1), k1 from ${tableName} where microsecond(k1) < 100000 order by k1; """
640+
641+
// Simple tests for basic functionality
642+
qt_sql_second_simple """ select second('12:34:56') """
643+
// qt_sql_microsecond_simple """ select microsecond('12:34:56.123456') """
644+
645+
// Test with datetime values
646+
qt_sql_second_datetime_with_micro """ select second('2024-01-01 12:34:56.789') """
647+
qt_sql_microsecond_datetime_with_micro """ select microsecond('2024-01-01 12:34:56.789123') """
648+
649+
// Test SECOND and MICROSECOND functions with table containing time_str column
650+
tableName = "test_time_str_functions"
651+
sql """ DROP TABLE IF EXISTS ${tableName} """
652+
sql """
653+
CREATE TABLE IF NOT EXISTS ${tableName} (
654+
`id` INT NOT NULL COMMENT "ID",
655+
`time_str` VARCHAR(50) NOT NULL COMMENT "时间字符串"
656+
) ENGINE=OLAP
657+
DUPLICATE KEY(`id`)
658+
DISTRIBUTED BY HASH(`id`) BUCKETS 1
659+
PROPERTIES("replication_num" = "1");
660+
"""
661+
662+
// Insert key test data
663+
sql """ insert into ${tableName} values
664+
(1, '120000.12'),
665+
(2, '12:00:01.12'),
666+
(3, '23:59:59.999999'),
667+
(4, '2024-01-01 12:34:56.789123');
668+
"""
669+
670+
// Test SECOND function with time_str column
671+
qt_sql_table_second_all """ select second(time_str) from ${tableName} order by id; """
672+
673+
// Test MICROSECOND function with time_str column
674+
qt_sql_table_microsecond_all """ select microsecond(time_str) from ${tableName} order by id; """
621675

622676
tableName = "test_from_unixtime"
623677

@@ -966,4 +1020,5 @@ suite("test_date_function") {
9661020
FROM date_add_test123; """
9671021

9681022
order_qt_sql2 """ SELECT invalid_col, DATE_ADD(invalid_col, INTERVAL 1+2 DAY) FROM date_add_test123 """
1023+
9691024
}

0 commit comments

Comments
 (0)