Skip to content

Commit 94d52d7

Browse files
committed
[SPARK-17269][SQL] Move finish analysis optimization stage into its own file
As part of breaking Optimizer.scala apart, this patch moves various finish analysis optimization stage rules into a single file. I'm submitting separate pull requests so we can more easily merge this in branch-2.0 to simplify optimizer backports. This should be covered by existing tests. Author: Reynold Xin <[email protected]> Closes #14838 from rxin/SPARK-17269. (cherry picked from commit dcefac4) Signed-off-by: Reynold Xin <[email protected]>
1 parent 9c0ac6b commit 94d52d7

File tree

3 files changed

+66
-39
lines changed

3 files changed

+66
-39
lines changed

sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/optimizer/Optimizer.scala

Lines changed: 0 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -1582,44 +1582,6 @@ object RemoveRepetitionFromGroupExpressions extends Rule[LogicalPlan] {
15821582
}
15831583
}
15841584

1585-
/**
1586-
* Finds all [[RuntimeReplaceable]] expressions and replace them with the expressions that can
1587-
* be evaluated. This is mainly used to provide compatibility with other databases.
1588-
* For example, we use this to support "nvl" by replacing it with "coalesce".
1589-
*/
1590-
object ReplaceExpressions extends Rule[LogicalPlan] {
1591-
def apply(plan: LogicalPlan): LogicalPlan = plan transformAllExpressions {
1592-
case e: RuntimeReplaceable => e.replaced
1593-
}
1594-
}
1595-
1596-
/**
1597-
* Computes the current date and time to make sure we return the same result in a single query.
1598-
*/
1599-
object ComputeCurrentTime extends Rule[LogicalPlan] {
1600-
def apply(plan: LogicalPlan): LogicalPlan = {
1601-
val dateExpr = CurrentDate()
1602-
val timeExpr = CurrentTimestamp()
1603-
val currentDate = Literal.create(dateExpr.eval(EmptyRow), dateExpr.dataType)
1604-
val currentTime = Literal.create(timeExpr.eval(EmptyRow), timeExpr.dataType)
1605-
1606-
plan transformAllExpressions {
1607-
case CurrentDate() => currentDate
1608-
case CurrentTimestamp() => currentTime
1609-
}
1610-
}
1611-
}
1612-
1613-
/** Replaces the expression of CurrentDatabase with the current database name. */
1614-
case class GetCurrentDatabase(sessionCatalog: SessionCatalog) extends Rule[LogicalPlan] {
1615-
def apply(plan: LogicalPlan): LogicalPlan = {
1616-
plan transformAllExpressions {
1617-
case CurrentDatabase() =>
1618-
Literal.create(sessionCatalog.getCurrentDatabase, StringType)
1619-
}
1620-
}
1621-
}
1622-
16231585
/**
16241586
* Typed [[Filter]] is by default surrounded by a [[DeserializeToObject]] beneath it and a
16251587
* [[SerializeFromObject]] above it. If these serializations can't be eliminated, we should embed
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
* limitations under the License.
1616
*/
1717

18-
package org.apache.spark.sql.catalyst.analysis
18+
package org.apache.spark.sql.catalyst.optimizer
1919

2020
import org.apache.spark.sql.catalyst.expressions._
2121
import org.apache.spark.sql.catalyst.expressions.aggregate.{AggregateExpression, AggregateFunction, Complete}
Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
/*
2+
* Licensed to the Apache Software Foundation (ASF) under one or more
3+
* contributor license agreements. See the NOTICE file distributed with
4+
* this work for additional information regarding copyright ownership.
5+
* The ASF licenses this file to You under the Apache License, Version 2.0
6+
* (the "License"); you may not use this file except in compliance with
7+
* the License. You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*/
17+
18+
package org.apache.spark.sql.catalyst.optimizer
19+
20+
import org.apache.spark.sql.catalyst.catalog.SessionCatalog
21+
import org.apache.spark.sql.catalyst.expressions._
22+
import org.apache.spark.sql.catalyst.plans.logical._
23+
import org.apache.spark.sql.catalyst.rules._
24+
import org.apache.spark.sql.types._
25+
26+
27+
/**
28+
* Finds all [[RuntimeReplaceable]] expressions and replace them with the expressions that can
29+
* be evaluated. This is mainly used to provide compatibility with other databases.
30+
* For example, we use this to support "nvl" by replacing it with "coalesce".
31+
*/
32+
object ReplaceExpressions extends Rule[LogicalPlan] {
33+
def apply(plan: LogicalPlan): LogicalPlan = plan transformAllExpressions {
34+
case e: RuntimeReplaceable => e.replaced
35+
}
36+
}
37+
38+
39+
/**
40+
* Computes the current date and time to make sure we return the same result in a single query.
41+
*/
42+
object ComputeCurrentTime extends Rule[LogicalPlan] {
43+
def apply(plan: LogicalPlan): LogicalPlan = {
44+
val dateExpr = CurrentDate()
45+
val timeExpr = CurrentTimestamp()
46+
val currentDate = Literal.create(dateExpr.eval(EmptyRow), dateExpr.dataType)
47+
val currentTime = Literal.create(timeExpr.eval(EmptyRow), timeExpr.dataType)
48+
49+
plan transformAllExpressions {
50+
case CurrentDate() => currentDate
51+
case CurrentTimestamp() => currentTime
52+
}
53+
}
54+
}
55+
56+
57+
/** Replaces the expression of CurrentDatabase with the current database name. */
58+
case class GetCurrentDatabase(sessionCatalog: SessionCatalog) extends Rule[LogicalPlan] {
59+
def apply(plan: LogicalPlan): LogicalPlan = {
60+
plan transformAllExpressions {
61+
case CurrentDatabase() =>
62+
Literal.create(sessionCatalog.getCurrentDatabase, StringType)
63+
}
64+
}
65+
}

0 commit comments

Comments
 (0)