-
Notifications
You must be signed in to change notification settings - Fork 29k
[SPARK-29838][SQL] PostgreSQL dialect: cast to timestamp #26472
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 4 commits
d705adc
8b56ae9
00ffde3
6a93060
1024d37
700143d
111d673
4fc3d71
08c71f1
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 |
|---|---|---|
|
|
@@ -19,15 +19,15 @@ package org.apache.spark.sql.catalyst.analysis | |
|
|
||
| import org.apache.spark.internal.Logging | ||
| import org.apache.spark.sql.catalyst.expressions.Cast | ||
| import org.apache.spark.sql.catalyst.expressions.postgreSQL.PostgreCastToBoolean | ||
| import org.apache.spark.sql.catalyst.expressions.postgreSQL.{PostgreCastToBoolean, PostgreCastToTimestamp} | ||
| import org.apache.spark.sql.catalyst.plans.logical.LogicalPlan | ||
| import org.apache.spark.sql.catalyst.rules.Rule | ||
| import org.apache.spark.sql.internal.SQLConf | ||
| import org.apache.spark.sql.types.{BooleanType, StringType} | ||
| import org.apache.spark.sql.types.{BooleanType, TimestampType} | ||
|
|
||
| object PostgreSQLDialect { | ||
| val postgreSQLDialectRules: List[Rule[LogicalPlan]] = | ||
| CastToBoolean :: | ||
| CastToBoolean :: CastToTimestamp :: | ||
| Nil | ||
|
|
||
| object CastToBoolean extends Rule[LogicalPlan] with Logging { | ||
|
|
@@ -46,4 +46,19 @@ object PostgreSQLDialect { | |
| } | ||
| } | ||
| } | ||
|
|
||
| object CastToTimestamp extends Rule[LogicalPlan] { | ||
| override def apply(plan: LogicalPlan): LogicalPlan = { | ||
| val conf = SQLConf.get | ||
| if (conf.usePostgreSQLDialect) { | ||
amanomer marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| plan.transformExpressions { | ||
| case Cast(child, dataType, timeZoneId) | ||
| if dataType == TimestampType => | ||
|
||
| PostgreCastToTimestamp(child, timeZoneId) | ||
| } | ||
| } else { | ||
| plan | ||
| } | ||
| } | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -16,19 +16,29 @@ | |
| */ | ||
| package org.apache.spark.sql.catalyst.expressions.postgreSQL | ||
|
|
||
| import java.time.ZoneId | ||
|
|
||
| import org.apache.spark.sql.AnalysisException | ||
| import org.apache.spark.sql.catalyst.analysis.TypeCheckResult | ||
| import org.apache.spark.sql.catalyst.expressions.{CastBase, Expression, TimeZoneAwareExpression} | ||
| import org.apache.spark.sql.catalyst.expressions.codegen.{CodegenContext, JavaCode} | ||
| import org.apache.spark.sql.catalyst.expressions.codegen.Block._ | ||
| import org.apache.spark.sql.catalyst.util.DateTimeUtils | ||
| import org.apache.spark.sql.catalyst.util.postgreSQL.StringUtils | ||
| import org.apache.spark.sql.types._ | ||
amanomer marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| import org.apache.spark.sql.types.{BooleanType, DataType, DateType, IntegerType, NullType, StringType, TimestampType} | ||
| import org.apache.spark.unsafe.types.UTF8String | ||
|
|
||
| case class PostgreCastToBoolean(child: Expression, timeZoneId: Option[String]) | ||
| extends CastBase { | ||
| abstract class PostgreCastBase extends CastBase{ | ||
amanomer marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
|
||
| override protected def ansiEnabled = | ||
| override protected def ansiEnabled: Boolean = | ||
| throw new UnsupportedOperationException("PostgreSQL dialect doesn't support ansi mode") | ||
|
|
||
| override def nullable: Boolean = child.nullable | ||
| } | ||
|
|
||
| case class PostgreCastToBoolean(child: Expression, timeZoneId: Option[String]) | ||
| extends PostgreCastBase { | ||
|
|
||
| override def withTimeZone(timeZoneId: String): TimeZoneAwareExpression = | ||
| copy(timeZoneId = Option(timeZoneId)) | ||
|
|
||
|
|
@@ -75,9 +85,57 @@ case class PostgreCastToBoolean(child: Expression, timeZoneId: Option[String]) | |
|
|
||
| override def dataType: DataType = BooleanType | ||
|
|
||
| override def nullable: Boolean = child.nullable | ||
|
|
||
| override def toString: String = s"PostgreCastToBoolean($child as ${dataType.simpleString})" | ||
|
|
||
| override def sql: String = s"CAST(${child.sql} AS ${dataType.sql})" | ||
| } | ||
|
|
||
| case class PostgreCastToTimestamp(child: Expression, timeZoneId: Option[String]) | ||
|
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. btw, we need to define a new rule and a new cast expr for each Pg cast pattern? I mean we cannot define all the Pg cast patterns in a single rule and a cast expr? cc: @cloud-fan @Ngone51
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. I think we can and should combine them into a single one(both rule and expression) when more types get in. Just like the original
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. yea, I personally think so. @cloud-fan |
||
| extends PostgreCastBase { | ||
| override def dataType: DataType = TimestampType | ||
|
|
||
| override def checkInputDataTypes(): TypeCheckResult = child.dataType match { | ||
| case StringType | DateType => | ||
| TypeCheckResult.TypeCheckSuccess | ||
| case _ => | ||
| TypeCheckResult.TypeCheckFailure(s"cannot cast type ${child.dataType} to timestamp") | ||
| } | ||
| /** Returns a copy of this expression with the specified timeZoneId. */ | ||
| override def withTimeZone(timeZoneId: String): TimeZoneAwareExpression = | ||
| copy(timeZoneId = Option(timeZoneId)) | ||
|
|
||
| override def castToTimestamp(from: DataType): Any => Any = from match { | ||
| case StringType => | ||
| buildCast[UTF8String](_, utfs => DateTimeUtils.stringToTimestamp(utfs, zoneId) | ||
|
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. I believe that postgre could correctly parse string
Contributor
Author
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. Thanks for your suggestion. I will check this.
Contributor
Author
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. Spark results with NULL for all of them.
Contributor
Author
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. @maropu kindly review latest changes and give your feedback on supporting above queries. Do we need to support them in this PR? If yes, we need to list all formats for timestamps which postgres supports but spark don't.
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. I personally think that the support above is not a main issue of this pr, so better to separate the two work: the timestamp cast support and the timestamp format support for the pg dialect. |
||
| .getOrElse(throw new AnalysisException(s"invalid input syntax for type timestamp:$utfs"))) | ||
| case DateType => | ||
| super.castToTimestamp(from) | ||
| } | ||
|
|
||
| override def castToTimestampCode( | ||
| from: DataType, | ||
| ctx: CodegenContext): CastFunction = from match { | ||
| case StringType => | ||
| val zoneIdClass = classOf[ZoneId] | ||
| val zid = JavaCode.global( | ||
| ctx.addReferenceObj("zoneId", zoneId, zoneIdClass.getName), | ||
| zoneIdClass) | ||
| val longOpt = ctx.freshVariable("longOpt", classOf[Option[Long]]) | ||
| (c, evPrim, evNull) => | ||
| code""" | ||
| scala.Option<Long> $longOpt = | ||
| org.apache.spark.sql.catalyst.util.DateTimeUtils.stringToTimestamp($c, $zid); | ||
| if ($longOpt.isDefined()) { | ||
| $evPrim = ((Long) $longOpt.get()).longValue(); | ||
| } else { | ||
| $evNull = throw new AnalysisException(s"invalid input syntax for type timestamp:$c"); | ||
| } | ||
| """ | ||
| case DateType => | ||
| super.castToTimestampCode(from, ctx) | ||
| } | ||
|
|
||
| override def toString: String = s"PostgreCastToTimestamp($child as ${dataType.simpleString})" | ||
|
|
||
| override def sql: String = s"CAST(${child.sql} AS ${dataType.sql})" | ||
amanomer marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -39,4 +39,14 @@ class PostgreSQLDialectQuerySuite extends QueryTest with SharedSparkSession { | |
| intercept[IllegalArgumentException](sql(s"select cast('$input' as boolean)").collect()) | ||
| } | ||
| } | ||
|
|
||
| test("cast to timestamp") { | ||
| Seq(1, 0.1, 1.toDouble, 5.toFloat, true, 3.toByte, 4.toShort) foreach { value => | ||
| val actualResult = intercept[AnalysisException]( | ||
| sql(s"SELECT CAST(${value} AS timestamp)") | ||
| ).getMessage | ||
| val expectedResult = s"Cannot cast type ${value.getClass} to Timestamp." | ||
| assert(actualResult == expectedResult) | ||
| } | ||
| } | ||
|
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. Can you move these tests to
Contributor
Author
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. I have moved these test cases.
Contributor
Author
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. Need to delete this test case. |
||
| } | ||
Uh oh!
There was an error while loading. Please reload this page.