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

fix: Support double quotes in date_part #10833

Merged
merged 8 commits into from
Jun 10, 2024
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
8 changes: 7 additions & 1 deletion datafusion/functions/src/datetime/date_part.rs
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,13 @@ impl ScalarUDFImpl for DatePartFunc {
ColumnarValue::Scalar(scalar) => scalar.to_array()?,
};

let arr = match part.to_lowercase().as_str() {
// to remove quotes at most 2 characters
let part_trim = part.trim_matches(|c| c == '\'' || c == '\"');
if ![2, 0].contains(&(part.len() - part_trim.len())) {
return exec_err!("Date part '{part}' not supported");
}

let arr = match part_trim.to_lowercase().as_str() {
"year" => date_part_f64(array.as_ref(), DatePart::Year)?,
"quarter" => date_part_f64(array.as_ref(), DatePart::Quarter)?,
"month" => date_part_f64(array.as_ref(), DatePart::Month)?,
Expand Down
110 changes: 110 additions & 0 deletions datafusion/sqllogictest/test_files/expr.slt
Original file line number Diff line number Diff line change
Expand Up @@ -827,6 +827,12 @@ SELECT

# test_extract_date_part

query error
SELECT EXTRACT("'''year'''" FROM timestamp '2020-09-08T12:00:00+00:00')

query error
SELECT EXTRACT("'year'" FROM timestamp '2020-09-08T12:00:00+00:00')

query R
SELECT date_part('YEAR', CAST('2000-01-01' AS DATE))
----
Expand All @@ -837,6 +843,14 @@ SELECT EXTRACT(year FROM timestamp '2020-09-08T12:00:00+00:00')
----
2020

query R
SELECT EXTRACT("year" FROM timestamp '2020-09-08T12:00:00+00:00')
----
2020

query error
SELECT EXTRACT('year' FROM timestamp '2020-09-08T12:00:00+00:00')

query R
SELECT date_part('QUARTER', CAST('2000-01-01' AS DATE))
----
Expand All @@ -847,6 +861,14 @@ SELECT EXTRACT(quarter FROM to_timestamp('2020-09-08T12:00:00+00:00'))
----
3

query R
SELECT EXTRACT("quarter" FROM to_timestamp('2020-09-08T12:00:00+00:00'))
----
3

query error
SELECT EXTRACT('quarter' FROM to_timestamp('2020-09-08T12:00:00+00:00'))

query R
SELECT date_part('MONTH', CAST('2000-01-01' AS DATE))
----
Expand All @@ -857,6 +879,14 @@ SELECT EXTRACT(month FROM to_timestamp('2020-09-08T12:00:00+00:00'))
----
9

query R
SELECT EXTRACT("month" FROM to_timestamp('2020-09-08T12:00:00+00:00'))
----
9

query error
SELECT EXTRACT('month' FROM to_timestamp('2020-09-08T12:00:00+00:00'))

query R
SELECT date_part('WEEK', CAST('2003-01-01' AS DATE))
----
Expand All @@ -867,6 +897,14 @@ SELECT EXTRACT(WEEK FROM to_timestamp('2020-09-08T12:00:00+00:00'))
----
37

query R
SELECT EXTRACT("WEEK" FROM to_timestamp('2020-09-08T12:00:00+00:00'))
----
37

query error
SELECT EXTRACT('WEEK' FROM to_timestamp('2020-09-08T12:00:00+00:00'))

query R
SELECT date_part('DAY', CAST('2000-01-01' AS DATE))
----
Expand All @@ -877,6 +915,14 @@ SELECT EXTRACT(day FROM to_timestamp('2020-09-08T12:00:00+00:00'))
----
8

query R
SELECT EXTRACT("day" FROM to_timestamp('2020-09-08T12:00:00+00:00'))
----
8

query error
SELECT EXTRACT('day' FROM to_timestamp('2020-09-08T12:00:00+00:00'))

query R
SELECT date_part('DOY', CAST('2000-01-01' AS DATE))
----
Expand All @@ -887,6 +933,14 @@ SELECT EXTRACT(doy FROM to_timestamp('2020-09-08T12:00:00+00:00'))
----
252

query R
SELECT EXTRACT("doy" FROM to_timestamp('2020-09-08T12:00:00+00:00'))
----
252

query error
SELECT EXTRACT('doy' FROM to_timestamp('2020-09-08T12:00:00+00:00'))

query R
SELECT date_part('DOW', CAST('2000-01-01' AS DATE))
----
Expand All @@ -897,6 +951,14 @@ SELECT EXTRACT(dow FROM to_timestamp('2020-09-08T12:00:00+00:00'))
----
2

query R
SELECT EXTRACT("dow" FROM to_timestamp('2020-09-08T12:00:00+00:00'))
----
2

query error
SELECT EXTRACT('dow' FROM to_timestamp('2020-09-08T12:00:00+00:00'))

query R
SELECT date_part('HOUR', CAST('2000-01-01' AS DATE))
----
Expand All @@ -907,11 +969,27 @@ SELECT EXTRACT(hour FROM to_timestamp('2020-09-08T12:03:03+00:00'))
----
12

query R
SELECT EXTRACT("hour" FROM to_timestamp('2020-09-08T12:03:03+00:00'))
----
12

query error
SELECT EXTRACT('hour' FROM to_timestamp('2020-09-08T12:03:03+00:00'))

query R
SELECT EXTRACT(minute FROM to_timestamp('2020-09-08T12:12:00+00:00'))
----
12

query R
SELECT EXTRACT("minute" FROM to_timestamp('2020-09-08T12:12:00+00:00'))
----
12

query error
SELECT EXTRACT('minute' FROM to_timestamp('2020-09-08T12:12:00+00:00'))

query R
SELECT date_part('minute', to_timestamp('2020-09-08T12:12:00+00:00'))
----
Expand All @@ -937,6 +1015,38 @@ SELECT EXTRACT(nanosecond FROM timestamp '2020-09-08T12:00:12.12345678+00:00')
----
12123456780

query R
SELECT EXTRACT("second" FROM timestamp '2020-09-08T12:00:12.12345678+00:00')
----
12.12345678

query R
SELECT EXTRACT("millisecond" FROM timestamp '2020-09-08T12:00:12.12345678+00:00')
----
12123.45678

query R
SELECT EXTRACT("microsecond" FROM timestamp '2020-09-08T12:00:12.12345678+00:00')
----
12123456.78

query R
SELECT EXTRACT("nanosecond" FROM timestamp '2020-09-08T12:00:12.12345678+00:00')
----
12123456780

query error
SELECT EXTRACT('second' FROM timestamp '2020-09-08T12:00:12.12345678+00:00')

query error
SELECT EXTRACT('millisecond' FROM timestamp '2020-09-08T12:00:12.12345678+00:00')

query error
SELECT EXTRACT('microsecond' FROM timestamp '2020-09-08T12:00:12.12345678+00:00')

query error
SELECT EXTRACT('nanosecond' FROM timestamp '2020-09-08T12:00:12.12345678+00:00')

# Keep precision when coercing Utf8 to Timestamp
query R
SELECT date_part('second', timestamp '2020-09-08T12:00:12.12345678+00:00')
Expand Down