Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(query): add func date_format and str_to_date and str_to_timestamp #11442

Merged
merged 1 commit into from
May 14, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
74 changes: 74 additions & 0 deletions src/query/functions/src/scalars/datetime.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@

use std::io::Write;

use chrono::prelude::*;
use chrono::Datelike;
use chrono::Utc;
use common_arrow::arrow::temporal_conversions::EPOCH_DAYS_FROM_CE;
Expand Down Expand Up @@ -148,6 +149,59 @@ fn register_string_to_timestamp(registry: &mut FunctionRegistry) {
}
})(val, ctx)
}

registry.register_combine_nullable_2_arg::<StringType, StringType, TimestampType, _, _>(
"str_to_timestamp",
|_, _| FunctionDomain::MayThrow,
vectorize_with_builder_2_arg::<StringType, StringType, NullableType<TimestampType>>(
|timestamp, format, output, ctx| {
if format.is_empty() {
output.push_null();
} else {
match (std::str::from_utf8(timestamp), std::str::from_utf8(format)) {
(Ok(date), Ok(format)) => {
// date need has timezone info.
if let Ok(res) = DateTime::parse_from_str(date, format) {
output.push(
res.with_timezone(&ctx.func_ctx.tz.tz).timestamp_micros(),
);
} else {
output.push_null();
}
}
_ => {
output.push_null();
}
}
}
},
),
);

registry.register_combine_nullable_2_arg::<StringType, StringType, DateType, _, _>(
"str_to_date",
|_, _| FunctionDomain::MayThrow,
vectorize_with_builder_2_arg::<StringType, StringType, NullableType<DateType>>(
|date, format, output, _| {
if format.is_empty() {
output.push_null();
} else {
match (std::str::from_utf8(date), std::str::from_utf8(format)) {
(Ok(date), Ok(format)) => {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why not throw the error?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Refer to MySQL behavior here. Return null directly if there is any invalid

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we should not follow MySQL's behavior, we should throw if any error happens.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we should not follow MySQL's behavior, we should throw if any error happens.

Modify in this pr: #11455

if let Ok(res) = NaiveDate::parse_from_str(date, format) {
output.push(res.num_days_from_ce() - EPOCH_DAYS_FROM_CE);
} else {
output.push_null();
}
}
_ => {
output.push_null();
}
}
}
},
),
);
}

fn register_date_to_timestamp(registry: &mut FunctionRegistry) {
Expand Down Expand Up @@ -326,6 +380,26 @@ fn register_number_to_date(registry: &mut FunctionRegistry) {
}

fn register_to_string(registry: &mut FunctionRegistry) {
registry.register_combine_nullable_2_arg::<TimestampType, StringType, StringType, _, _>(
"date_format",
|_, _| FunctionDomain::MayThrow,
vectorize_with_builder_2_arg::<TimestampType, StringType, NullableType<StringType>>(
|date, format, output, ctx| {
if format.is_empty() {
output.push_null();
} else {
let ts = date.to_timestamp(ctx.func_ctx.tz.tz);
if let Ok(format) = std::str::from_utf8(format) {
let res = ts.format(format).to_string();
output.push(res.as_bytes());
} else {
output.push_null();
}
}
},
),
);

registry.register_passthrough_nullable_1_arg::<DateType, StringType, _, _>(
"to_string",
|_| FunctionDomain::Full,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1120,6 +1120,8 @@ Functions overloads:
1 cot(Float64 NULL) :: Float64 NULL
0 crc32(String) :: UInt32
1 crc32(String NULL) :: UInt32 NULL
0 date_format(Timestamp, String) :: String NULL
1 date_format(Timestamp NULL, String NULL) :: String NULL
0 degrees(Float64) :: Float64
1 degrees(Float64 NULL) :: Float64 NULL
0 div(UInt8, UInt8) :: UInt8
Expand Down Expand Up @@ -2980,6 +2982,10 @@ Functions overloads:
17 sqrt(Float32 NULL) :: Float64 NULL
18 sqrt(Float64) :: Float64
19 sqrt(Float64 NULL) :: Float64 NULL
0 str_to_date(String, String) :: Date NULL
1 str_to_date(String NULL, String NULL) :: Date NULL
0 str_to_timestamp(String, String) :: Timestamp NULL
1 str_to_timestamp(String NULL, String NULL) :: Timestamp NULL
0 strcmp(String, String) :: Int8
1 strcmp(String NULL, String NULL) :: Int8 NULL
0 substr(String, Int64) :: String
Expand Down
12 changes: 5 additions & 7 deletions src/query/service/src/servers/http/clickhouse_handler.rs
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ use tracing::info;
use crate::interpreters::InterpreterFactory;
use crate::interpreters::InterpreterPtr;
use crate::servers::http::v1::HttpQueryContext;
use crate::sessions::short_sql;
use crate::sessions::QueryContext;
use crate::sessions::SessionType;
use crate::sessions::TableContext;
Expand Down Expand Up @@ -286,14 +287,11 @@ pub async fn clickhouse_handler_post(
sql.push(' ');
}
sql.push_str(body.into_string().await?.as_str());
let n = 100;
let n = 64;
// other parts of the request already logged in middleware
let msg = if sql.len() > n {
format!(
"{}...(omit {} bytes)",
&sql[0..n].to_string(),
sql.len() - n
)
let len = sql.len();
let msg = if len > n {
format!("{}...(omit {} bytes)", short_sql(sql.clone()), len - n)
} else {
sql.to_string()
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -893,3 +893,38 @@ select * from t order by b
statement ok
drop table t

query T
select date_format('2022-02-02', '精彩的%Y年,美丽的%mmonth,激动の%dd');
----
精彩的2022年,美丽的02month,激动の02d

query T
select str_to_date('精彩的2022年,美丽的02month,激动の02d', '精彩的%Y年,美丽的%mmonth,激动の%dd');
BohuTANG marked this conversation as resolved.
Show resolved Hide resolved
----
2022-02-02

statement error 1001
select date_format('', '');

statement error 1001
select date_format('2022-2-04T03:58:59', '%Y年%m月%d日,%H时%M分%S秒');

query T
select date_format('2022-02-04T03:58:59', '%Y年%m月%d日,%H时%M分%S秒');
----
2022年02月04日,03时58分59秒

query T
select str_to_timestamp('2022年02月04日,03时58分59秒', '%Y年%m月%d日,%H时%M分%S秒');
----
NULL

query T
select str_to_timestamp('2022年02月04日,8时58分59秒,时区:+0000', '%Y年%m月%d日,%H时%M分%S秒,时区:%z');
----
2022-02-04 08:58:59.000000

query T
select str_to_timestamp('2022年02月04日,8时58分59秒,时区:+0800', '%Y年%m月%d日,%H时%M分%S秒,时区:%z');
----
2022-02-04 00:58:59.000000