Skip to content

GetCurrentDatabase Logical Optimization

GetCurrentDatabase is a base logical optimization that <> for current_database SQL function.

GetCurrentDatabase is part of the Finish Analysis once-executed batch in the standard batches of the Logical Optimizer.

GetCurrentDatabase is simply a <> for transforming <>, i.e. Rule[LogicalPlan].

val q = sql("SELECT current_database() AS db")
val analyzedPlan = q.queryExecution.analyzed

scala> println(analyzedPlan.numberedTreeString)
00 Project [current_database() AS db#22]
01 +- OneRowRelation

import org.apache.spark.sql.catalyst.optimizer.GetCurrentDatabase

val afterGetCurrentDatabase = GetCurrentDatabase(spark.sessionState.catalog)(analyzedPlan)
scala> println(afterGetCurrentDatabase.numberedTreeString)
00 Project [default AS db#22]
01 +- OneRowRelation


GetCurrentDatabase corresponds to SQL's current_database() function.

You can access the current database in Scala using

scala> val database = spark.catalog.currentDatabase
database: String = default

Executing Rule

apply(plan: LogicalPlan): LogicalPlan


apply is part of the Rule abstraction.

Last update: 2020-08-05