-
Notifications
You must be signed in to change notification settings - Fork 29.3k
[SPARK-57846][SQL] Add the try_make_time function #56932
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
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
|
|
@@ -890,6 +890,23 @@ object functions { | |||||
| Column.fn("make_time", hour, minute, second) | ||||||
| } | ||||||
|
|
||||||
| /** | ||||||
| * Try to create time from hour, minute and second fields. The function returns NULL on invalid | ||||||
| * inputs. | ||||||
| * | ||||||
| * @param hour | ||||||
| * the hour to represent, from 0 to 23 | ||||||
| * @param minute | ||||||
| * the minute to represent, from 0 to 59 | ||||||
| * @param second | ||||||
| * the second to represent, from 0 to 59.999999 | ||||||
| * @group datetime_funcs | ||||||
| * @since 4.1.0 | ||||||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Same versioning issue — update the Scala API
Suggested change
|
||||||
| */ | ||||||
| def try_make_time(hour: Column, minute: Column, second: Column): Column = { | ||||||
| Column.fn("try_make_time", hour, minute, second) | ||||||
| } | ||||||
|
|
||||||
| /** | ||||||
| * Aggregate function: returns the most frequent value in a group. | ||||||
| * | ||||||
|
|
||||||
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
|
|
@@ -623,6 +623,57 @@ case class MakeTime( | |||||
| copy(hours = newChildren(0), minutes = newChildren(1), secsAndMicros = newChildren(2)) | ||||||
| } | ||||||
|
|
||||||
| // scalastyle:off line.size.limit | ||||||
| @ExpressionDescription( | ||||||
| usage = "_FUNC_(hour, minute, second) - Try to create time from hour, minute and second fields. The function returns NULL on invalid inputs.", | ||||||
| arguments = """ | ||||||
| Arguments: | ||||||
| * hour - the hour to represent, from 0 to 23 | ||||||
| * minute - the minute to represent, from 0 to 59 | ||||||
| * second - the second to represent, from 0 to 59.999999 | ||||||
| """, | ||||||
| examples = """ | ||||||
| Examples: | ||||||
| > SELECT _FUNC_(6, 30, 45.887); | ||||||
| 06:30:45.887 | ||||||
| > SELECT _FUNC_(NULL, 30, 0); | ||||||
| NULL | ||||||
| > SELECT _FUNC_(25, 30, 0); | ||||||
| NULL | ||||||
| """, | ||||||
| group = "datetime_funcs", | ||||||
| since = "4.1.0") | ||||||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
If this is intended to be master-only (unusual for a new user-facing function), use |
||||||
| // scalastyle:on line.size.limit | ||||||
| object TryMakeTimeExpressionBuilder extends ExpressionBuilder { | ||||||
| override def build(funcName: String, expressions: Seq[Expression]): Expression = { | ||||||
| val numArgs = expressions.length | ||||||
| if (numArgs == 3) { | ||||||
| new TryMakeTime(expressions(0), expressions(1), expressions(2)) | ||||||
| } else { | ||||||
| throw QueryCompilationErrors.wrongNumArgsError(funcName, Seq(3), numArgs) | ||||||
| } | ||||||
| } | ||||||
| } | ||||||
|
|
||||||
| case class TryMakeTime( | ||||||
| hours: Expression, | ||||||
| minutes: Expression, | ||||||
| secsAndMicros: Expression, | ||||||
| replacement: Expression) | ||||||
| extends RuntimeReplaceable with InheritAnalysisRules { | ||||||
|
|
||||||
| def this(hours: Expression, minutes: Expression, secsAndMicros: Expression) = | ||||||
| this(hours, minutes, secsAndMicros, | ||||||
| TryEval(MakeTime(hours, minutes, secsAndMicros))) | ||||||
|
|
||||||
| override def prettyName: String = "try_make_time" | ||||||
|
|
||||||
| override def parameters: Seq[Expression] = Seq(hours, minutes, secsAndMicros) | ||||||
|
|
||||||
| override protected def withNewChildInternal(newChild: Expression): TryMakeTime = | ||||||
| copy(replacement = newChild) | ||||||
| } | ||||||
|
|
||||||
| /** | ||||||
| * Adds day-time interval to time. | ||||||
| */ | ||||||
|
|
||||||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -233,6 +233,44 @@ class TimeExpressionsSuite extends SparkFunSuite with ExpressionEvalHelper { | |
| ) | ||
| } | ||
|
|
||
| test("creating values of TimeType via try_make_time") { | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Nit (non-blocking): this test builds |
||
| // Valid cases should return the same result as make_time | ||
| checkEvaluation( | ||
| TryEval(MakeTime(Literal(13), Literal(2), Literal(Decimal(23.5, 16, 6)))), | ||
| LocalTime.of(13, 2, 23, 500000000)) | ||
| checkEvaluation( | ||
| TryEval(MakeTime(Literal(0), Literal(0), Literal(Decimal(0.0, 16, 6)))), | ||
| LocalTime.of(0, 0, 0, 0)) | ||
| checkEvaluation( | ||
| TryEval(MakeTime(Literal(23), Literal(59), Literal(Decimal(59.999999, 16, 6)))), | ||
| LocalTime.of(23, 59, 59, 999999000)) | ||
|
|
||
| // Null cases | ||
| checkEvaluation( | ||
| TryEval(MakeTime( | ||
| Literal.create(null, IntegerType), Literal(18), Literal(Decimal(23.5, 16, 6)))), | ||
| null) | ||
| checkEvaluation( | ||
| TryEval(MakeTime( | ||
| Literal(13), Literal.create(null, IntegerType), Literal(Decimal(23.5, 16, 6)))), | ||
| null) | ||
| checkEvaluation( | ||
| TryEval(MakeTime( | ||
| Literal(13), Literal(18), Literal.create(null, DecimalType(16, 6)))), | ||
| null) | ||
|
|
||
| // Invalid cases return null instead of throwing | ||
| checkEvaluation( | ||
| TryEval(MakeTime(Literal(25), Literal(2), Literal(Decimal(23.5, 16, 6)))), | ||
| null) | ||
| checkEvaluation( | ||
| TryEval(MakeTime(Literal(23), Literal(-1), Literal(Decimal(23.5, 16, 6)))), | ||
| null) | ||
| checkEvaluation( | ||
| TryEval(MakeTime(Literal(23), Literal(12), Literal(Decimal(100.5, 16, 6)))), | ||
| null) | ||
| } | ||
|
|
||
| test("SecondExpressionBuilder") { | ||
| // Empty expressions list | ||
| checkError( | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,2 @@ | ||
| Project [tryeval(static_invoke(DateTimeUtils.makeTime(12, 13, cast(14 as decimal(16,6))))) AS try_make_time(12, 13, 14)#0] | ||
| +- LocalRelation <empty>, [d#0, t#0, s#0, x#0L, wt#0] |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,102 @@ | ||
| { | ||
| "common": { | ||
| "planId": "1" | ||
| }, | ||
| "project": { | ||
| "input": { | ||
| "common": { | ||
| "planId": "0" | ||
| }, | ||
| "localRelation": { | ||
| "schema": "struct\u003cd:date,t:timestamp,s:string,x:bigint,wt:struct\u003cstart:timestamp,end:timestamp\u003e\u003e" | ||
| } | ||
| }, | ||
| "expressions": [{ | ||
| "unresolvedFunction": { | ||
| "functionName": "try_make_time", | ||
| "arguments": [{ | ||
| "literal": { | ||
| "integer": 12 | ||
| }, | ||
| "common": { | ||
| "origin": { | ||
| "jvmOrigin": { | ||
| "stackTrace": [{ | ||
| "classLoaderName": "app", | ||
| "declaringClass": "org.apache.spark.sql.functions$", | ||
| "methodName": "lit", | ||
| "fileName": "functions.scala" | ||
| }, { | ||
| "classLoaderName": "app", | ||
| "declaringClass": "org.apache.spark.sql.PlanGenerationTestSuite", | ||
| "methodName": "~~trimmed~anonfun~~", | ||
| "fileName": "PlanGenerationTestSuite.scala" | ||
| }] | ||
| } | ||
| } | ||
| } | ||
| }, { | ||
| "literal": { | ||
| "integer": 13 | ||
| }, | ||
| "common": { | ||
| "origin": { | ||
| "jvmOrigin": { | ||
| "stackTrace": [{ | ||
| "classLoaderName": "app", | ||
| "declaringClass": "org.apache.spark.sql.functions$", | ||
| "methodName": "lit", | ||
| "fileName": "functions.scala" | ||
| }, { | ||
| "classLoaderName": "app", | ||
| "declaringClass": "org.apache.spark.sql.PlanGenerationTestSuite", | ||
| "methodName": "~~trimmed~anonfun~~", | ||
| "fileName": "PlanGenerationTestSuite.scala" | ||
| }] | ||
| } | ||
| } | ||
| } | ||
| }, { | ||
| "literal": { | ||
| "integer": 14 | ||
| }, | ||
| "common": { | ||
| "origin": { | ||
| "jvmOrigin": { | ||
| "stackTrace": [{ | ||
| "classLoaderName": "app", | ||
| "declaringClass": "org.apache.spark.sql.functions$", | ||
| "methodName": "lit", | ||
| "fileName": "functions.scala" | ||
| }, { | ||
| "classLoaderName": "app", | ||
| "declaringClass": "org.apache.spark.sql.PlanGenerationTestSuite", | ||
| "methodName": "~~trimmed~anonfun~~", | ||
| "fileName": "PlanGenerationTestSuite.scala" | ||
| }] | ||
| } | ||
| } | ||
| } | ||
| }], | ||
| "isInternal": false | ||
| }, | ||
| "common": { | ||
| "origin": { | ||
| "jvmOrigin": { | ||
| "stackTrace": [{ | ||
| "classLoaderName": "app", | ||
| "declaringClass": "org.apache.spark.sql.functions$", | ||
| "methodName": "try_make_time", | ||
| "fileName": "functions.scala" | ||
| }, { | ||
| "classLoaderName": "app", | ||
| "declaringClass": "org.apache.spark.sql.PlanGenerationTestSuite", | ||
| "methodName": "~~trimmed~anonfun~~", | ||
| "fileName": "PlanGenerationTestSuite.scala" | ||
| }] | ||
| } | ||
| } | ||
| } | ||
| }] | ||
| } | ||
| } |
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.
Same versioning issue — update the Python
versionaddedto match (4.3.0). (The Connectbuiltin.pyforwards__doc__, so no separate change is needed there.)