MaxGekk commented on code in PR #36654:
URL: https://github.com/apache/spark/pull/36654#discussion_r887935173
##########
sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/optimizer/finishAnalysis.scala:
##########
@@ -73,29 +75,32 @@ object RewriteNonCorrelatedExists extends Rule[LogicalPlan]
{
*/
object ComputeCurrentTime extends Rule[LogicalPlan] {
def apply(plan: LogicalPlan): LogicalPlan = {
- val currentDates = mutable.Map.empty[String, Literal]
- val timeExpr = CurrentTimestamp()
- val timestamp = timeExpr.eval(EmptyRow).asInstanceOf[Long]
- val currentTime = Literal.create(timestamp, timeExpr.dataType)
+ val instant = Instant.now()
+ val currentTimestampMicros = instantToMicros(instant)
+ val currentTime = Literal.create(currentTimestampMicros, TimestampType)
val timezone = Literal.create(conf.sessionLocalTimeZone, StringType)
- val localTimestamps = mutable.Map.empty[String, Literal]
- plan.transformAllExpressionsWithPruning(_.containsPattern(CURRENT_LIKE)) {
- case currentDate @ CurrentDate(Some(timeZoneId)) =>
- currentDates.getOrElseUpdate(timeZoneId, {
- Literal.create(currentDate.eval().asInstanceOf[Int], DateType)
- })
- case CurrentTimestamp() | Now() => currentTime
- case CurrentTimeZone() => timezone
- case localTimestamp @ LocalTimestamp(Some(timeZoneId)) =>
- localTimestamps.getOrElseUpdate(timeZoneId, {
- Literal.create(localTimestamp.eval().asInstanceOf[Long],
TimestampNTZType)
- })
+ def transformCondition(treePatternbits: TreePatternBits): Boolean = {
+ treePatternbits.containsPattern(CURRENT_LIKE)
+ }
+
+ plan.transformDownWithSubqueries(transformCondition) {
+ case subQuery =>
+ subQuery.transformAllExpressionsWithPruning(transformCondition) {
+ case cd: CurrentDate =>
+ Literal.create(
+ DateTimeUtils.microsToDays(currentTimestampMicros,
cd.zoneId).asInstanceOf[Int],
+ DateType)
+ case CurrentTimestamp() | Now() => currentTime
+ case CurrentTimeZone() => timezone
+ case localTimestamp: LocalTimestamp =>
+ val asDateTime = LocalDateTime.ofInstant(instant,
localTimestamp.zoneId)
+
Literal.create(localDateTimeToMicros(asDateTime).asInstanceOf[Long],
TimestampNTZType)
Review Comment:
Please, remove `.asInstanceOf[Long]`
##########
sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/optimizer/finishAnalysis.scala:
##########
@@ -73,29 +75,32 @@ object RewriteNonCorrelatedExists extends Rule[LogicalPlan]
{
*/
object ComputeCurrentTime extends Rule[LogicalPlan] {
def apply(plan: LogicalPlan): LogicalPlan = {
- val currentDates = mutable.Map.empty[String, Literal]
- val timeExpr = CurrentTimestamp()
- val timestamp = timeExpr.eval(EmptyRow).asInstanceOf[Long]
- val currentTime = Literal.create(timestamp, timeExpr.dataType)
+ val instant = Instant.now()
+ val currentTimestampMicros = instantToMicros(instant)
+ val currentTime = Literal.create(currentTimestampMicros, TimestampType)
val timezone = Literal.create(conf.sessionLocalTimeZone, StringType)
- val localTimestamps = mutable.Map.empty[String, Literal]
- plan.transformAllExpressionsWithPruning(_.containsPattern(CURRENT_LIKE)) {
- case currentDate @ CurrentDate(Some(timeZoneId)) =>
- currentDates.getOrElseUpdate(timeZoneId, {
- Literal.create(currentDate.eval().asInstanceOf[Int], DateType)
- })
- case CurrentTimestamp() | Now() => currentTime
- case CurrentTimeZone() => timezone
- case localTimestamp @ LocalTimestamp(Some(timeZoneId)) =>
- localTimestamps.getOrElseUpdate(timeZoneId, {
- Literal.create(localTimestamp.eval().asInstanceOf[Long],
TimestampNTZType)
- })
+ def transformCondition(treePatternbits: TreePatternBits): Boolean = {
+ treePatternbits.containsPattern(CURRENT_LIKE)
+ }
+
+ plan.transformDownWithSubqueries(transformCondition) {
+ case subQuery =>
+ subQuery.transformAllExpressionsWithPruning(transformCondition) {
+ case cd: CurrentDate =>
+ Literal.create(
+ DateTimeUtils.microsToDays(currentTimestampMicros,
cd.zoneId).asInstanceOf[Int],
Review Comment:
I think this `.asInstanceOf[Int]` since `microsToDays()` return Int
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]