-
Notifications
You must be signed in to change notification settings - Fork 28.5k
[SPARK-51562][SQL] Add the time() function #50557
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
Open
senthh
wants to merge
1
commit into
apache:master
Choose a base branch
from
senthh:SPARK-51562_time_function
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change | ||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|
|
@@ -20,12 +20,13 @@ package org.apache.spark.sql.catalyst.expressions | |||||||||||
import java.time.DateTimeException | ||||||||||||
|
||||||||||||
import org.apache.spark.sql.catalyst.analysis.ExpressionBuilder | ||||||||||||
import org.apache.spark.sql.catalyst.expressions.codegen.{CodegenContext, ExprCode} | ||||||||||||
import org.apache.spark.sql.catalyst.expressions.objects.{Invoke, StaticInvoke} | ||||||||||||
import org.apache.spark.sql.catalyst.util.DateTimeUtils | ||||||||||||
import org.apache.spark.sql.catalyst.util.TimeFormatter | ||||||||||||
import org.apache.spark.sql.errors.{QueryCompilationErrors, QueryExecutionErrors} | ||||||||||||
import org.apache.spark.sql.internal.types.StringTypeWithCollation | ||||||||||||
import org.apache.spark.sql.types.{AbstractDataType, IntegerType, ObjectType, TimeType, TypeCollection} | ||||||||||||
import org.apache.spark.sql.types.{AbstractDataType, AnyDataType, DataType, IntegerType, ObjectType, TimeType, TypeCollection} | ||||||||||||
import org.apache.spark.unsafe.types.UTF8String | ||||||||||||
|
||||||||||||
/** | ||||||||||||
|
@@ -162,6 +163,186 @@ object TryToTimeExpressionBuilder extends ExpressionBuilder { | |||||||||||
} | ||||||||||||
} | ||||||||||||
|
||||||||||||
/** | ||||||||||||
* Converts or extracts time from various input types. | ||||||||||||
*/ | ||||||||||||
// scalastyle:off line.size.limit | ||||||||||||
@ExpressionDescription( | ||||||||||||
usage = """ | ||||||||||||
_FUNC_(expr) - Extracts the time part from or converts the given expression to a time. | ||||||||||||
The expression can be a string, timestamp, or numeric value. | ||||||||||||
""", | ||||||||||||
arguments = """ | ||||||||||||
Arguments: | ||||||||||||
* expr - An expression that can be one of: | ||||||||||||
- A string representing a time | ||||||||||||
- A timestamp value | ||||||||||||
- A numeric value representing total seconds | ||||||||||||
""", | ||||||||||||
examples = """ | ||||||||||||
Examples: | ||||||||||||
> SELECT _FUNC_('12:25:13.45'); | ||||||||||||
12:25:13.45 | ||||||||||||
> SELECT _FUNC_(timestamp'2020-04-30 12:25:13.45'); | ||||||||||||
12:25:13.45 | ||||||||||||
> SELECT _FUNC_(123); | ||||||||||||
00:02:03 | ||||||||||||
""", | ||||||||||||
group = "datetime_funcs", | ||||||||||||
since = "4.1.0") | ||||||||||||
// scalastyle:on line.size.limit | ||||||||||||
case class Time(expr: Expression) | ||||||||||||
extends UnaryExpression with ExpectsInputTypes { | ||||||||||||
|
||||||||||||
override def child: Expression = expr | ||||||||||||
|
||||||||||||
override def dataType: DataType = TimeType() | ||||||||||||
|
||||||||||||
override def inputTypes: Seq[AbstractDataType] = Seq(AnyDataType) | ||||||||||||
|
||||||||||||
override def prettyName: String = "time" | ||||||||||||
|
||||||||||||
override protected def doGenCode(ctx: CodegenContext, ev: ExprCode): ExprCode = { | ||||||||||||
val castExpr = Cast(child, TimeType()) | ||||||||||||
castExpr.genCode(ctx) | ||||||||||||
} | ||||||||||||
|
||||||||||||
override protected def nullSafeEval(input: Any): Any = { | ||||||||||||
Cast(child, TimeType()).eval(null) | ||||||||||||
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. This is not right way of implementing a replacement. Please, take a look at Lines 2180 to 2184 in 203942c
|
||||||||||||
} | ||||||||||||
|
||||||||||||
override protected def withNewChildInternal(newChild: Expression): | ||||||||||||
Expression = copy(expr = newChild) | ||||||||||||
|
||||||||||||
} | ||||||||||||
|
||||||||||||
/** | ||||||||||||
* Similar to Time but returns NULL instead of raising an error on invalid input. | ||||||||||||
*/ | ||||||||||||
@ExpressionDescription( | ||||||||||||
usage = """ | ||||||||||||
_FUNC_(expr) - Extracts the time part from or converts the given expression to a time. | ||||||||||||
If the conversion fails, it returns NULL. The expression can be a string, timestamp, | ||||||||||||
or numeric value. | ||||||||||||
""", | ||||||||||||
arguments = """ | ||||||||||||
Arguments: | ||||||||||||
* expr - An expression that can be one of: | ||||||||||||
- A string representing a time | ||||||||||||
- A timestamp value | ||||||||||||
- A numeric value representing total seconds | ||||||||||||
""", | ||||||||||||
examples = """ | ||||||||||||
Examples: | ||||||||||||
> SELECT _FUNC_('12:25:13.45'); | ||||||||||||
12:25:13.45 | ||||||||||||
> SELECT _FUNC_('invalid'); | ||||||||||||
NULL | ||||||||||||
> SELECT _FUNC_(timestamp'2020-04-30 12:25:13.45'); | ||||||||||||
12:25:13.45 | ||||||||||||
""", | ||||||||||||
group = "datetime_funcs", | ||||||||||||
since = "4.1.0") | ||||||||||||
case class TryTime(expr: Expression) | ||||||||||||
extends UnaryExpression with ExpectsInputTypes { | ||||||||||||
|
||||||||||||
override def child: Expression = expr | ||||||||||||
|
||||||||||||
override def dataType: DataType = TimeType() | ||||||||||||
|
||||||||||||
override def inputTypes: Seq[AbstractDataType] = Seq(AnyDataType) | ||||||||||||
|
||||||||||||
override def prettyName: String = "time" | ||||||||||||
|
||||||||||||
override protected def doGenCode(ctx: CodegenContext, ev: ExprCode): ExprCode = { | ||||||||||||
val castExpr = Cast(child, TimeType()) | ||||||||||||
castExpr.genCode(ctx) | ||||||||||||
} | ||||||||||||
|
||||||||||||
override protected def nullSafeEval(input: Any): Any = { | ||||||||||||
Cast(child, TimeType()).eval(null) | ||||||||||||
} | ||||||||||||
|
||||||||||||
override protected def withNewChildInternal(newChild: Expression): | ||||||||||||
Expression = copy(expr = newChild) | ||||||||||||
} | ||||||||||||
|
||||||||||||
// scalastyle:off line.size.limit | ||||||||||||
@ExpressionDescription( | ||||||||||||
usage = """ | ||||||||||||
_FUNC_(expr) - Parses the `expr` expression to a time. If the parsing fails, it returns NULL. | ||||||||||||
The expression can be a string, timestamp, or numeric value. | ||||||||||||
""", | ||||||||||||
arguments = """ | ||||||||||||
Arguments: | ||||||||||||
* expr - An expression that can be one of: | ||||||||||||
- A string representing a time | ||||||||||||
- A timestamp value | ||||||||||||
- A numeric value representing total seconds | ||||||||||||
""", | ||||||||||||
examples = """ | ||||||||||||
Examples: | ||||||||||||
> SELECT _FUNC_('12:25:13.45'); | ||||||||||||
12:25:13.45 | ||||||||||||
> SELECT _FUNC_('invalid time'); | ||||||||||||
NULL | ||||||||||||
> SELECT _FUNC_(timestamp'2020-04-30 12:25:13.45'); | ||||||||||||
12:25:13.45 | ||||||||||||
> SELECT _FUNC_(123); | ||||||||||||
00:02:03 | ||||||||||||
""", | ||||||||||||
group = "datetime_funcs", | ||||||||||||
since = "4.1.0") | ||||||||||||
// scalastyle:on line.size.limit | ||||||||||||
object TimeExpressionBuilder extends ExpressionBuilder { | ||||||||||||
override def build(funcName: String, expressions: Seq[Expression]): Expression = { | ||||||||||||
val numArgs = expressions.length | ||||||||||||
if (numArgs == 1) { | ||||||||||||
Time(expressions.head) | ||||||||||||
} else { | ||||||||||||
throw QueryCompilationErrors.wrongNumArgsError(funcName, Seq(1), numArgs) | ||||||||||||
} | ||||||||||||
} | ||||||||||||
} | ||||||||||||
|
||||||||||||
// scalastyle:off line.size.limit | ||||||||||||
@ExpressionDescription( | ||||||||||||
usage = """ | ||||||||||||
_FUNC_(expr) - Parses the `expr` expression to a time. If the parsing fails, it returns NULL. | ||||||||||||
The expression can be a string, timestamp, or numeric value. | ||||||||||||
""", | ||||||||||||
arguments = """ | ||||||||||||
Arguments: | ||||||||||||
* expr - An expression that can be one of: | ||||||||||||
- A string representing a time | ||||||||||||
- A timestamp value | ||||||||||||
- A numeric value representing total seconds | ||||||||||||
""", | ||||||||||||
examples = """ | ||||||||||||
Examples: | ||||||||||||
> SELECT _FUNC_('12:25:13.45'); | ||||||||||||
12:25:13.45 | ||||||||||||
> SELECT _FUNC_('invalid time'); | ||||||||||||
NULL | ||||||||||||
> SELECT _FUNC_(timestamp'2020-04-30 12:25:13.45'); | ||||||||||||
12:25:13.45 | ||||||||||||
> SELECT _FUNC_(123); | ||||||||||||
00:02:03 | ||||||||||||
""", | ||||||||||||
group = "datetime_funcs", | ||||||||||||
since = "4.1.0") | ||||||||||||
// scalastyle:on line.size.limit | ||||||||||||
object TryTimeExpressionBuilder extends ExpressionBuilder { | ||||||||||||
override def build(funcName: String, expressions: Seq[Expression]): Expression = { | ||||||||||||
val numArgs = expressions.length | ||||||||||||
if (numArgs == 1) { | ||||||||||||
TryTime(expressions.head) | ||||||||||||
} else { | ||||||||||||
throw QueryCompilationErrors.wrongNumArgsError(funcName, Seq(1), numArgs) | ||||||||||||
} | ||||||||||||
} | ||||||||||||
} | ||||||||||||
|
||||||||||||
// scalastyle:off line.size.limit | ||||||||||||
@ExpressionDescription( | ||||||||||||
usage = """ | ||||||||||||
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
The scope of this PR should be adding the
time()
function alias forCAST
. Let's implement CAST and test it separately, please.