GetCurrentDatabase Logical Optimization

GetCurrentDatabase is a base logical optimization that gives the current database for current_database SQL function.

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

GetCurrentDatabase is simply a Catalyst rule for transforming logical plans, 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 Method

apply(plan: LogicalPlan): LogicalPlan
apply is part of the Rule Contract to execute (apply) a rule on a TreeNode (e.g. LogicalPlan).


results matching ""

    No results matching ""