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

Add to_unixtime function #9077

Merged
merged 3 commits into from
Mar 8, 2024
Merged

Add to_unixtime function #9077

merged 3 commits into from
Mar 8, 2024

Conversation

Tangruilin
Copy link
Contributor

Which issue does this PR close?

Closes #5568 .

Rationale for this change

What changes are included in this PR?

Are these changes tested?

Are there any user-facing changes?

@Tangruilin Tangruilin marked this pull request as draft January 31, 2024 01:46
@github-actions github-actions bot added logical-expr Logical plan and expressions physical-expr Physical Expressions labels Jan 31, 2024
@Tangruilin Tangruilin force-pushed the task#5568 branch 3 times, most recently from a7da055 to eab02b2 Compare March 2, 2024 09:37
@github-actions github-actions bot added the sqllogictest SQL Logic Tests (.slt) label Mar 2, 2024
@alamb
Copy link
Contributor

alamb commented Mar 2, 2024

Now that @Omega359 started moving the date time functions to datafusion-functions perhaps you can follow the same pattern for to_unixtime 🤔

@Tangruilin
Copy link
Contributor Author

Now that @Omega359 started moving the date time functions to datafusion-functions perhaps you can follow the same pattern for to_unixtime 🤔

I see that.

I will do this tomorrow

@alamb
Copy link
Contributor

alamb commented Mar 2, 2024

Thank you @Tangruilin

@github-actions github-actions bot removed logical-expr Logical plan and expressions physical-expr Physical Expressions labels Mar 3, 2024
@Tangruilin Tangruilin marked this pull request as ready for review March 3, 2024 04:00
@Tangruilin Tangruilin force-pushed the task#5568 branch 2 times, most recently from 10fb2d4 to 91f3eb6 Compare March 3, 2024 06:39
@Tangruilin
Copy link
Contributor Author

Thank you @Tangruilin

This PR is ready to review

Copy link
Contributor

@alamb alamb left a comment

Choose a reason for hiding this comment

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

Thanks @Tangruilin -- this is looking like a good start. Perhaps @waynexia has some thoughts on expected behavior and time to review given he filed #5568

Could you please also add an entry for to_unixtime in the documentation? https://github.com/apache/arrow-datafusion/blob/main/docs/source/user-guide/sql/scalar_functions.md#to_timestamp

@@ -0,0 +1,60 @@
// Licensed to the Apache Software Foundation (ASF) under one
Copy link
Contributor

Choose a reason for hiding this comment

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

FWIW I don't think we need to make a to_unixtime example -- I think the other examples for using the functions are plenty thorough. In fact I recommend we remove this example so it doesn't become a pattern for all newly added datetime functions ( could do this as a follow on PR )


fn invoke(&self, args: &[ColumnarValue]) -> Result<ColumnarValue> {
if args.is_empty() {
return exec_err!("to_date function requires 1 or more arguments, got 0");
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
return exec_err!("to_date function requires 1 or more arguments, got 0");
return exec_err!("to_unixtime function requires 1 or more arguments, got 0");

| DataType::Null
| DataType::Float64
| DataType::Date32
| DataType::Date64 => args[0].cast_to(&DataType::Int64, None),
Copy link
Contributor

Choose a reason for hiding this comment

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

These types look a little strange to me. Why is Int32/Int64/Float supported? If that is intentional, can you please add tests of calling to_unixtime with arguments of those types?

Also, the spark to_unixtime function https://spark.apache.org/docs/2.3.0/api/sql/index.html#to_unix_timestamp seems to also accept timestamps (DateTime::Timestamp(..)) -- should this function do so as well?

Also I think casting a Date32 to an Int64 will yield the number of seconds
https://docs.rs/arrow/latest/arrow/datatypes/enum.DataType.html#variant.Date32

However, casting a Date64 to an Int64 I think will yield the number of milliseconds since the unix epoch https://docs.rs/arrow/latest/arrow/datatypes/enum.DataType.html#variant.Date64

which doesn't sound correct.

Perhaps you can write a test like the following to be sure:

select to_unixtime(arrow_cast('2020-09-08T12:00:00+00:00', 'Date64'));

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Good Suggestion,I will do it this week

@waynexia waynexia self-requested a review March 5, 2024 02:24
@Tangruilin Tangruilin force-pushed the task#5568 branch 3 times, most recently from eff4770 to 79889b1 Compare March 7, 2024 05:14
Signed-off-by: tangruilin <tang.ruilin@foxmail.com>
Copy link
Member

@waynexia waynexia left a comment

Choose a reason for hiding this comment

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

The rest LGTM, thanks!

Comment on lines 64 to 68
if args.len() > 1 {
if let Some(value) = validate_data_types(args, "to_unixtime") {
return value;
}
}
Copy link
Member

Choose a reason for hiding this comment

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

I should say the validate_data_types is a bit misleading... It won't return Some(Ok(_)). We can submit a follow-up PR to change its signature to (...) -> Option<Result<()>> to avoid confusion. Here we can use value? instead of returning it.

Copy link
Member

Choose a reason for hiding this comment

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

Copy link
Contributor

@alamb alamb left a comment

Choose a reason for hiding this comment

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

Thank you @Tangruilin and @waynexia

}
DataType::Date64 | DataType::Date32 | DataType::Timestamp(_, None) => args[0]
.cast_to(&DataType::Timestamp(TimeUnit::Second, None), None)?
.cast_to(&DataType::Int64, None),
Copy link
Contributor

Choose a reason for hiding this comment

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

👍

@alamb alamb changed the title [task #5568] add_to_unixtime_function Add to_unixtime function Mar 8, 2024
@alamb
Copy link
Contributor

alamb commented Mar 8, 2024

I merged up from main to resolve the CI failures

@alamb alamb merged commit 32bb26d into apache:main Mar 8, 2024
23 checks passed
@alamb
Copy link
Contributor

alamb commented Mar 8, 2024

Thanks again @Tangruilin and @waynexia

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
sqllogictest SQL Logic Tests (.slt)
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Implement to_unixtime function
3 participants