-
Notifications
You must be signed in to change notification settings - Fork 28.9k
[SPARK-53762][SQL] Add date and time conversions simplifier rule to optimizer #52493
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 |
---|---|---|
|
@@ -1142,6 +1142,48 @@ object SimplifyCaseConversionExpressions extends Rule[LogicalPlan] { | |
} | ||
} | ||
|
||
/** | ||
* Removes date and time related functions that are unnecessary. | ||
*/ | ||
object SimplifyDateTimeConversions extends Rule[LogicalPlan] { | ||
def apply(plan: LogicalPlan): LogicalPlan = plan.transformWithPruning( | ||
_.containsPattern(DATETIME), ruleId) { | ||
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. ditto. nit. Indentation? 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. Fixed in dcf49a8. |
||
case q: LogicalPlan => q.transformExpressionsUpWithPruning( | ||
_.containsPattern(DATETIME), ruleId) { | ||
// Remove a string to timestamp conversions followed by a timestamp to string conversions if | ||
// original string is in the same format. | ||
case DateFormatClass( | ||
GetTimestamp( | ||
e @ DateFormatClass(_, pattern, timeZoneId), | ||
pattern2, | ||
TimestampType, | ||
_, | ||
timeZoneId2, | ||
_), | ||
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. Just a question. Maybe, the following is better? case DateFormatClass(
GetTimestamp(
- e @ DateFormatClass(
- _,
- pattern,
- timeZoneId),
+ e @ DateFormatClass(_, pattern, timeZoneId),
pattern2,
TimestampType,
_, 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. Done in dcf49a8. |
||
pattern3, | ||
timeZoneId3) | ||
if pattern.semanticEquals(pattern2) && pattern.semanticEquals(pattern3) | ||
&& timeZoneId == timeZoneId2 && timeZoneId == timeZoneId3 => | ||
e | ||
|
||
// Remove a timestamp to string conversion followed by a string to timestamp conversions if | ||
// original timestamp is built with the same format. | ||
case GetTimestamp( | ||
DateFormatClass( | ||
e @ GetTimestamp(_, pattern, TimestampType, _, timeZoneId, _), | ||
pattern2, | ||
timeZoneId2), | ||
pattern3, | ||
TimestampType, | ||
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. ditto. Maybe? case GetTimestamp(
DateFormatClass(
- e @ GetTimestamp(
- _,
- pattern,
- TimestampType,
- _,
- timeZoneId,
- _),
+ e @ GetTimestamp(_, pattern, TimestampType, _, timeZoneId, _),
pattern2,
timeZoneId2), 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. Yeah, looks nicer. Fixed in dcf49a8. |
||
_, | ||
timeZoneId3, | ||
_) | ||
if pattern.semanticEquals(pattern2) && pattern.semanticEquals(pattern3) | ||
&& timeZoneId == timeZoneId2 && timeZoneId == timeZoneId3 => | ||
e | ||
} | ||
} | ||
} | ||
|
||
/** | ||
* Combine nested [[Concat]] expressions. | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
/* | ||
* Licensed to the Apache Software Foundation (ASF) under one or more | ||
* contributor license agreements. See the NOTICE file distributed with | ||
* this work for additional information regarding copyright ownership. | ||
* The ASF licenses this file to You under the Apache License, Version 2.0 | ||
* (the "License"); you may not use this file except in compliance with | ||
* the License. You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package org.apache.spark.sql.catalyst.optimizer | ||
|
||
import org.apache.spark.sql.catalyst.dsl.expressions._ | ||
import org.apache.spark.sql.catalyst.dsl.plans._ | ||
import org.apache.spark.sql.catalyst.expressions.{DateFormatClass, GetTimestamp} | ||
import org.apache.spark.sql.catalyst.plans.PlanTest | ||
import org.apache.spark.sql.catalyst.plans.logical._ | ||
import org.apache.spark.sql.catalyst.rules.RuleExecutor | ||
import org.apache.spark.sql.types._ | ||
|
||
class SimplifyDateTimeConversionsSuite extends PlanTest { | ||
|
||
object Optimize extends RuleExecutor[LogicalPlan] { | ||
val batches = | ||
Batch("SimplifyDateTimeConversions", FixedPoint(50), SimplifyDateTimeConversions) :: Nil | ||
} | ||
|
||
val testRelation = LocalRelation($"ts".timestamp, $"s".string) | ||
|
||
test("SPARK-53762: Remove DateFormat - GetTimestamp groups") { | ||
val pattern = "yyyy-MM-dd" | ||
|
||
val df = DateFormatClass($"ts", pattern) | ||
val gt = GetTimestamp($"s", pattern, TimestampType) | ||
|
||
val originalQuery = testRelation | ||
.select( | ||
DateFormatClass( | ||
GetTimestamp( | ||
df, | ||
pattern, | ||
TimestampType), | ||
pattern) as "c1", | ||
GetTimestamp( | ||
DateFormatClass( | ||
gt, | ||
pattern), | ||
pattern, | ||
TimestampType) as "c2") | ||
.analyze | ||
|
||
val optimized = Optimize.execute(originalQuery) | ||
|
||
val expected = testRelation | ||
.select( | ||
df as "c1", | ||
gt as "c2") | ||
.analyze | ||
|
||
comparePlans(optimized, expected) | ||
} | ||
} |
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.
Do you think we can elaborate more what is the definition of
uncecessary
as of now? It would be great an itemized list because we can add more when we improveSimplifyDateTimeConversions
in the future.Uh oh!
There was an error while loading. Please reload this page.
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.
Added comments to cases one by one in dcf49a8.