Github user anabranch commented on a diff in the pull request:
https://github.com/apache/spark/pull/16138#discussion_r91156380
--- Diff:
sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/expressions/datetimeExpressions.scala
---
@@ -1047,6 +1047,53 @@ case class ToDate(child: Expression) extends
UnaryExpression with ImplicitCastIn
}
/**
+ * Parses a column with a format to a timestamp.
+ */
+@ExpressionDescription(
+ usage = "_FUNC_(timestamp_str, fmt) - Parses the `timestamp` expression
" +
+ "with the `fmt` expression.",
+ extended = """
+ Examples:
+ > SELECT _FUNC_('2016-12-31', 'yyyy-MM-dd');
+ 2016-12-31
+ """)
+case class ParseToDate(left: Expression, right: Expression, child:
Expression)
+ extends RuntimeReplaceable with ImplicitCastInputTypes {
+
+ def this(left: Expression, format: Expression) = {
+ this(left, format, ToDate(new ParseToTimestamp(left, format)))
+ }
+
+ override def inputTypes: Seq[AbstractDataType] = Seq(StringType,
StringType)
+ override def dataType: DataType = DateType
+ override def flatArguments: Iterator[Any] = Iterator(left, right)
+ override def sql: String = s"$prettyName(${left.sql}, ${right.sql})"
+}
+
+/**
+ * Parses a column with a format to a timestamp.
+ */
+@ExpressionDescription(
+ usage = "_FUNC_(timestamp, fmt) - Parses the `timestamp` expression with
the `fmt` expression.",
+ extended = """
+ Examples:
+ > SELECT _FUNC_('2016-12-31', 'yyyy-MM-dd');
+ 2016-12-31 00:00:00.0
+ """)
+case class ParseToTimestamp(left: Expression, right: Expression, child:
Expression)
+ extends RuntimeReplaceable with ImplicitCastInputTypes {
+
+ def this(left: Expression, format: Expression) = {
+ this(left, format, Cast(UnixTimestamp(left, format), TimestampType))
--- End diff --
```
scala> spark.range(1).select(to_date(to_timestamp(lit("2016-12-31"),
"yyyy-MM-dd"))).explain(true)
== Parsed Logical Plan ==
'Project [to_date(parsetotimestamp(2016-12-31, yyyy-MM-dd)) AS
to_date(parsetotimestamp('2016-12-31', 'yyyy-MM-dd'))#48]
+- Range (0, 1, step=1, splits=Some(8))
== Analyzed Logical Plan ==
to_date(parsetotimestamp('2016-12-31', 'yyyy-MM-dd')): date
Project [to_date(cast(parsetotimestamp(2016-12-31, yyyy-MM-dd) as date)) AS
to_date(parsetotimestamp('2016-12-31', 'yyyy-MM-dd'))#48]
+- Range (0, 1, step=1, splits=Some(8))
== Optimized Logical Plan ==
Project [17166 AS to_date(parsetotimestamp('2016-12-31', 'yyyy-MM-dd'))#48]
+- Range (0, 1, step=1, splits=Some(8))
== Physical Plan ==
*Project [17166 AS to_date(parsetotimestamp('2016-12-31', 'yyyy-MM-dd'))#48]
+- *Range (0, 1, step=1, splits=Some(8))
```
Calling `to_date` on the result of my function works (that's before I do
that cast).
However
```
scala> spark.range(1).select(to_timestamp(lit("2016-12-31"),
"yyyy-MM-dd")).explain(true)
== Parsed Logical Plan ==
'Project [parsetotimestamp(2016-12-31, yyyy-MM-dd) AS
parsetotimestamp('2016-12-31', 'yyyy-MM-dd')#34]
+- Range (0, 1, step=1, splits=Some(8))
== Analyzed Logical Plan ==
parsetotimestamp('2016-12-31', 'yyyy-MM-dd'): timestamp
Project [parsetotimestamp(2016-12-31, yyyy-MM-dd) AS
parsetotimestamp('2016-12-31', 'yyyy-MM-dd')#34]
+- Range (0, 1, step=1, splits=Some(8))
== Optimized Logical Plan ==
Project [2016-12-31 00:00:00 AS parsetotimestamp('2016-12-31',
'yyyy-MM-dd')#34]
+- Range (0, 1, step=1, splits=Some(8))
== Physical Plan ==
*Project [2016-12-31 00:00:00 AS parsetotimestamp('2016-12-31',
'yyyy-MM-dd')#34]
+- *Range (0, 1, step=1, splits=Some(8))
```
Doesn't return the correct result. It appears to be in microseconds.
---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at [email protected] or file a JIRA ticket
with INFRA.
---
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]