-
Notifications
You must be signed in to change notification settings - Fork 29k
[SPARK-31797][SQL] Adds TIMESTAMP_SECONDS, TIMESTAMP_MILLIS and TIMESTAMP_MICROS functions #28534
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
Changes from 1 commit
5e60ec5
27a162b
f879987
4c17d49
3411935
17a33f5
344e5e1
407dcfc
944c644
642668c
62bf8b5
7da94b5
3704fee
f5b0a5a
f4f7c35
3c949ac
fdfd75c
f0cc631
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
…MICROSECONDS to timestamp transfer
- Loading branch information
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||||
|---|---|---|---|---|---|---|---|---|
|
|
@@ -401,6 +401,92 @@ case class DayOfYear(child: Expression) extends UnaryExpression with ImplicitCas | |||||||
| } | ||||||||
| } | ||||||||
|
|
||||||||
| @ExpressionDescription( | ||||||||
| usage = "_FUNC_(date) - Returns timestamp from seconds.", | ||||||||
|
||||||||
| examples = """ | ||||||||
| Examples: | ||||||||
| > SELECT _FUNC_(1230219000); | ||||||||
| "2008-12-25 07:30:00.0" | ||||||||
| """, | ||||||||
| group = "datetime_funcs", | ||||||||
| since = "3.1.0") | ||||||||
| case class SecondsToTimestamp(child: Expression) | ||||||||
| extends NumberToTimestampBase { | ||||||||
|
|
||||||||
| override def upScaleFactor: SQLTimestamp = MICROS_PER_SECOND | ||||||||
|
||||||||
| override def upScaleFactor: SQLTimestamp = MICROS_PER_SECOND | |
| override def upScaleFactor: Long = MICROS_PER_SECOND |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ok, I will correct it, good job.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
+1
Outdated
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
_FUNC_(seconds) - Creates timestamp from the number of milliseconds since UTC epoch.
Outdated
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Include precision in the example, e.g.
| > SELECT _FUNC_(1230219000000); | |
| > SELECT _FUNC_(12302190000123); | |
| "2008-12-25 07:30:00.123" |
Same for micros below.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
+1
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ok, datetime.sql is also syncronized.
Outdated
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
ditto
Outdated
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
From a readability perspective it may be better to put the base class before the subclasses.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
done
Outdated
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
nit: add a space before {
Outdated
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
we should accept LongType only. The analyzer will cast the input to long for you, if possible.
Outdated
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
we can override nullSafeEval, to skip the null check
Outdated
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
ditto
Outdated
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
this should be ev.value, otherwise you get class cast exception, as ev.value is a string.
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -3495,6 +3495,28 @@ class SQLQuerySuite extends QueryTest with SharedSparkSession with AdaptiveSpark | |
| assert(df4.schema.head.name === "randn(1)") | ||
| checkIfSeedExistsInExplain(df2) | ||
| } | ||
|
|
||
| test("SPARK-31710: " + | ||
|
||
| "TIMESTAMP_SECONDS, TIMESTAMP_MILLISECONDS and TIMESTAMP_MICROSECONDS to timestamp transfer") { | ||
| val df1 = sql("select TIMESTAMP_SECONDS(1230219000) as timestamp") | ||
| checkAnswer(df1, Row(Timestamp.valueOf("2008-12-25 07:30:00.0"))) | ||
|
|
||
| val df2 = sql("select TIMESTAMP_MILLISECONDS(1230219000000) as timestamp") | ||
| checkAnswer(df2, Row(Timestamp.valueOf("2008-12-25 07:30:00.0"))) | ||
|
|
||
| val df3 = sql("select TIMESTAMP_MICROSECONDS(1230219000000000) as timestamp") | ||
| checkAnswer(df3, Row(Timestamp.valueOf("2008-12-25 07:30:00.0"))) | ||
|
|
||
| val df4 = sql("select TIMESTAMP_SECONDS(-1230219000) as timestamp") | ||
| checkAnswer(df4, Row(Timestamp.valueOf("1931-01-07 00:30:00.0"))) | ||
|
|
||
| val df5 = sql("select TIMESTAMP_MILLISECONDS(-1230219000000) as timestamp") | ||
| checkAnswer(df5, Row(Timestamp.valueOf("1931-01-07 00:30:00.0"))) | ||
|
|
||
| val df6 = sql("select TIMESTAMP_MICROSECONDS(-1230219000000000) as timestamp") | ||
| checkAnswer(df6, Row(Timestamp.valueOf("1931-01-07 00:30:00.0"))) | ||
|
|
||
| } | ||
| } | ||
|
|
||
| case class Foo(bar: Option[String]) | ||
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think it's okay to have expressions presumably to get away from
cast(ts as long)behaviour which is not invasive. But it would be more interesting to see how other DBMSes solved this problem. From arbitrary googling, vendors have different approaches.Seems like BigQuery has these three functions (https://cloud.google.com/bigquery/docs/reference/standard-sql/timestamp_functions?hl=en#timestamp_seconds). Shall we match the naming at least?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
oh, I didn't realize it's
MILLISnotMILLISECONDS. Let's follow the short name.Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@cloud-fan, is it means we shall rename
MilliSecondsToTimestamptoMillisToTimestampandMicroSecondsToTimestamptoMicrosToTimestamp. I will correct it if needed.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Not a big deal but yeah let's do that.