Skip to content

Commit 37b8d70

Browse files
TCeasonXuanwei Zhang
authored and
Xuanwei Zhang
committed
fix(query): to_date(9999-12-31, format_string) should success (#17652)
1 parent 78d73e6 commit 37b8d70

File tree

4 files changed

+37
-15
lines changed

4 files changed

+37
-15
lines changed

Cargo.lock

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/query/functions/src/scalars/timestamp/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ version = "0.1.0"
44
edition = "2021"
55

66
[dependencies]
7+
chrono = { workspace = true }
78
databend-common-column = { workspace = true }
89
databend-common-exception = { workspace = true }
910
databend-common-expression = { workspace = true }

src/query/functions/src/scalars/timestamp/src/datetime.rs

Lines changed: 12 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,12 @@ use std::borrow::Cow;
1717
use std::fmt::Display;
1818
use std::io::Write;
1919

20+
use chrono::Datelike;
21+
use chrono::NaiveDate;
2022
use databend_common_column::types::months_days_micros;
2123
use databend_common_exception::ErrorCode;
2224
use databend_common_expression::error_to_null;
25+
use databend_common_expression::serialize::EPOCH_DAYS_FROM_CE;
2326
use databend_common_expression::types::date::clamp_date;
2427
use databend_common_expression::types::date::date_to_string;
2528
use databend_common_expression::types::date::string_to_date;
@@ -347,36 +350,32 @@ fn register_string_to_timestamp(registry: &mut FunctionRegistry) {
347350
if format.is_empty() {
348351
output.push_null();
349352
} else {
350-
match string_to_format_datetime(date, format, ctx, false) {
351-
Ok((res, false)) => {
352-
output.push((res / MICROS_PER_SEC / 24 / 3600) as _);
353-
}
354-
Ok((_, true)) => {
355-
output.push_null();
353+
match NaiveDate::parse_from_str(date, format) {
354+
Ok(res) => {
355+
output.push(res.num_days_from_ce() - EPOCH_DAYS_FROM_CE);
356356
}
357357
Err(e) => {
358358
ctx.set_error(output.len(), e.to_string());
359-
output.push(0);
359+
output.push_null();
360360
}
361361
}
362362
}
363363
},
364364
),
365365
);
366-
367366
registry.register_combine_nullable_2_arg::<StringType, StringType, DateType, _, _>(
368367
"try_to_date",
369368
|_, _, _| FunctionDomain::MayThrow,
370369
vectorize_with_builder_2_arg::<StringType, StringType, NullableType<DateType>>(
371-
|date, format, output, ctx| {
370+
|date, format, output, _| {
372371
if format.is_empty() {
373372
output.push_null();
374373
} else {
375-
match string_to_format_datetime(date, format, ctx, false) {
376-
Ok((res, false)) => {
377-
output.push((res / MICROS_PER_SEC / 24 / 3600) as _);
374+
match NaiveDate::parse_from_str(date, format) {
375+
Ok(res) => {
376+
output.push(res.num_days_from_ce() - EPOCH_DAYS_FROM_CE);
378377
}
379-
_ => {
378+
Err(_) => {
380379
output.push_null();
381380
}
382381
}

tests/sqllogictests/suites/query/functions/02_0012_function_datetimes_tz.test

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1123,17 +1123,38 @@ statement ok
11231123
set timezone='Asia/Shanghai';
11241124

11251125
query T
1126-
select to_date('2024-03-01-1','%Y-%m-%d');
1126+
select to_date('2024-03-01','%Y-%m-%d');
11271127
----
11281128
2024-03-01
11291129

1130+
query T
1131+
select to_date('9999/12/31','%Y/%m/%d');
1132+
----
1133+
9999-12-31
1134+
1135+
query T
1136+
select to_date('9999-12-31','%Y-%m-%d');
1137+
----
1138+
9999-12-31
1139+
1140+
11301141
statement ok
11311142
set timezone='America/Los_Angeles';
11321143

11331144
query T
1134-
select to_date('2024-03-01-1','%Y-%m-%d');
1145+
select to_date('2024-03-01','%Y-%m-%d');
11351146
----
11361147
2024-03-01
11371148

1149+
query T
1150+
select to_date('9999-12-31','%Y-%m-%d');
1151+
----
1152+
9999-12-31
1153+
1154+
query T
1155+
select to_date('9999/12/31','%Y/%m/%d');
1156+
----
1157+
9999-12-31
1158+
11381159
statement ok
11391160
unset timezone;

0 commit comments

Comments
 (0)