Skip to content

Commit 0bcff44

Browse files
szehon-hobeliefer
authored andcommitted
[SPARK-51222][SQL] Optimize ReplaceCurrentLike
### What changes were proposed in this pull request? Improve ReplaceCurrentLike FinishAnalysis rule. This was found as a result of debugging SPARK-51119. ### Why are the changes needed? The rule calls the catalog unnecessarily, and this can be done lazily only if it is resolving current_catalog() and current_database() expressions. ### Does this PR introduce _any_ user-facing change? No ### How was this patch tested? Existing unit test ### Was this patch authored or co-authored using generative AI tooling? No Closes apache#49963 from szehon-ho/SPARK-51222. Authored-by: Szehon Ho <[email protected]> Signed-off-by: beliefer <[email protected]>
1 parent c36916f commit 0bcff44

File tree

1 file changed

+5
-5
lines changed

1 file changed

+5
-5
lines changed

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

+5-5
Original file line numberDiff line numberDiff line change
@@ -142,22 +142,22 @@ object ComputeCurrentTime extends Rule[LogicalPlan] {
142142
}
143143

144144
/**
145-
* Replaces the expression of CurrentDatabase with the current database name.
146-
* Replaces the expression of CurrentCatalog with the current catalog name.
145+
* Replaces the expression of CurrentDatabase, CurrentCatalog, and CurrentUser
146+
* with the current values.
147147
*/
148148
case class ReplaceCurrentLike(catalogManager: CatalogManager) extends Rule[LogicalPlan] {
149149
def apply(plan: LogicalPlan): LogicalPlan = {
150150
import org.apache.spark.sql.connector.catalog.CatalogV2Implicits._
151-
val currentNamespace = catalogManager.currentNamespace.quoted
152-
val currentCatalog = catalogManager.currentCatalog.name()
153-
val currentUser = CurrentUserContext.getCurrentUser
154151

155152
plan.transformAllExpressionsWithPruning(_.containsPattern(CURRENT_LIKE)) {
156153
case CurrentDatabase() =>
154+
val currentNamespace = catalogManager.currentNamespace.quoted
157155
Literal.create(currentNamespace, StringType)
158156
case CurrentCatalog() =>
157+
val currentCatalog = catalogManager.currentCatalog.name()
159158
Literal.create(currentCatalog, StringType)
160159
case CurrentUser() =>
160+
val currentUser = CurrentUserContext.getCurrentUser
161161
Literal.create(currentUser, StringType)
162162
}
163163
}

0 commit comments

Comments
 (0)