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 7 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
7 changes: 6 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,12 @@ impl ScalarUDFImpl for DatePartFunc {
ColumnarValue::Scalar(scalar) => scalar.to_array()?,
};

let arr = match part.to_lowercase().as_str() {
let arr = match part
.to_lowercase()
.replacen('\'', "", 2)
.replacen('\"', "", 2)
.as_str()
Copy link
Contributor

Choose a reason for hiding this comment

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

Hmm, this will still let select extract("'epoch'" from now()); through

But this is quite a minor thing, not sure how much it matters in the wider picture 😅

Copy link
Member Author

Choose a reason for hiding this comment

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

I found a workaround, but it should be a better way to handle it in sqlparser. Because it should have many use cases.

{
"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
107 changes: 107 additions & 0 deletions datafusion/sqllogictest/test_files/expr.slt
Original file line number Diff line number Diff line change
Expand Up @@ -827,6 +827,9 @@ SELECT

# test_extract_date_part

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 +840,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 +858,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 +876,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 +894,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 +912,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 +930,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 +948,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 +966,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 +1012,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