Skip to content

ResolveCreateNamedStruct Logical Resolution Rule

ResolveCreateNamedStruct is a logical resolution rule that <> in an entire logical query plan.

ResolveCreateNamedStruct is part of the Resolution fixed-point batch in the standard batches of the Analyzer.

ResolveCreateNamedStruct is simply a Catalyst rule for transforming logical plans, i.e. Rule[LogicalPlan].

scala> :type spark
org.apache.spark.sql.SparkSession

val q = spark.range(1).select(struct($"id"))
val logicalPlan = q.queryExecution.logical
scala> println(logicalPlan.numberedTreeString)
00 'Project [unresolvedalias(named_struct(NamePlaceholder, 'id), None)]
01 +- AnalysisBarrier
02       +- Range (0, 1, step=1, splits=Some(8))

// Let's resolve references first
import spark.sessionState.analyzer.ResolveReferences
val planWithRefsResolved = ResolveReferences(logicalPlan)

import org.apache.spark.sql.catalyst.analysis.ResolveCreateNamedStruct
val afterResolveCreateNamedStruct = ResolveCreateNamedStruct(planWithRefsResolved)
scala> println(afterResolveCreateNamedStruct.numberedTreeString)
00 'Project [unresolvedalias(named_struct(id, id#4L), None)]
01 +- AnalysisBarrier
02       +- Range (0, 1, step=1, splits=Some(8))

=== [[apply]] Executing Rule

[source, scala]

apply(plan: LogicalPlan): LogicalPlan

apply <> (in the input <>) that are <> expressions which are not <> yet and replaces NamePlaceholders with <> expressions.

In other words, apply finds unresolved <> expressions with NamePlaceholder expressions in the <> and replaces them with the <> of corresponding <>, but only if the NamedExpression is resolved.

In the end, apply creates a <> with new children.

apply is part of the Rule abstraction.