This is an automated email from the ASF dual-hosted git repository.
uros-b pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/spark.git
The following commit(s) were added to refs/heads/master by this push:
new d4a7fa5888c0 [SPARK-60748][SQL] Extract shared CREATE VIEW analysis
helpers
d4a7fa5888c0 is described below
commit d4a7fa5888c0e108808f1f61de9cb5d0290201c1
Author: Vladan Vasić <[email protected]>
AuthorDate: Wed Nov 4 09:04:29 2026 +0000
[SPARK-60748][SQL] Extract shared CREATE VIEW analysis helpers
### What changes were proposed in this pull request?
Extract shared CREATE VIEW analysis helpers without changing SQL behavior:
- Move session-catalog lowering into `ViewCommandHelper.rewriteCreateView`;
non-session targets retain their V2 command.
- Expose `ApplyDefaultCollation.resolveCreateViewCollation`, preserving
explicit collations, origin, tags, and the existing metadata-lookup failure
behavior.
- Move temporary-variable dependency collectors into
`IdentifierResolution`, preserving root expressions, nested subqueries, plan
children, and distinct variable names.
- Add `CatalogIdentifierResolution.resolveUnresolvedIdentifier` for command
targets without column output, preserving origin and tags, and use it in
`ResolveCatalogs`.
Jira: [SPARK-60748](https://issues.apache.org/jira/browse/SPARK-60748).
### Why are the changes needed?
These operations are currently embedded in fixed-point analyzer rules.
Sharing the targeted operations allows command resolution and IDENTIFIER
preprocessing to reuse the existing implementation rather than invoke whole
rules or duplicate behavior. This is preparation only; it does not enable new
single-pass analyzer coverage.
### Does this PR introduce _any_ user-facing change?
No. Existing command lowering, default-collation selection,
variable-dependency collection, and catalog routing are preserved.
### How was this patch tested?
Added focused Catalyst tests for root-variable and subquery dependency
collection, distinct names, plan children, identifier origin/tags, and
temporary-view routing.
Passed with Java 17:
```text
build/sbt 'catalyst/testOnly
org.apache.spark.sql.catalyst.analysis.AnalysisSuite -- -z "collect temporary
variables"'
build/sbt 'catalyst/testOnly
org.apache.spark.sql.catalyst.analysis.CatalogSuite'
build/sbt 'sql/testOnly
org.apache.spark.sql.execution.command.v1.CreateViewSuite
org.apache.spark.sql.execution.command.v2.CreateViewSuite'
build/sbt 'sql/testOnly
org.apache.spark.sql.collation.DefaultCollationStringTestSuiteV1
org.apache.spark.sql.collation.DefaultCollationStringTestSuiteV2 -- -z "CREATE
VIEW with schema level collation"'
```
These runs passed 46 tests: one variable-collection test, four catalog
tests, 28 CREATE VIEW tests, and 13 schema-level collation cases. The V2
collation suite has no tests matching this filter; V2 explicit-collation
behavior is covered by `execution.command.v2.CreateViewSuite`.
The following style checks also passed with no errors or warnings:
```text
build/sbt catalyst/Compile/scalastyle catalyst/Test/scalastyle
sql/Compile/scalastyle sql/Test/scalastyle
```
`git diff --check` passed.
### Was this patch authored or co-authored using generative AI tooling?
Generated-by: OpenAI Codex CLI 0.165.0
Closes #59959 from vladanvasi-db/vladanvasi-db/create-view-analysis-helpers.
Authored-by: Vladan Vasić <[email protected]>
Signed-off-by: Uros Bojanic <[email protected]>
---
.../catalyst/analysis/ApplyDefaultCollation.scala | 35 ++++++++++++++++------
.../analysis/CatalogIdentifierResolution.scala | 14 +++++++++
.../catalyst/analysis/IdentifierResolution.scala | 25 +++++++++++++++-
.../sql/catalyst/analysis/ResolveCatalogs.scala | 4 +--
.../analysis/ResolveIdentifierClause.scala | 22 ++------------
.../sql/catalyst/analysis/AnalysisSuite.scala | 35 ++++++++++++++++++++--
.../spark/sql/catalyst/analysis/CatalogSuite.scala | 34 +++++++++++++++++++++
.../catalyst/analysis/ResolveSessionCatalog.scala | 23 ++------------
.../sql/catalyst/analysis/ViewCommandHelper.scala | 33 ++++++++++++++++++--
9 files changed, 168 insertions(+), 57 deletions(-)
diff --git
a/sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/analysis/ApplyDefaultCollation.scala
b/sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/analysis/ApplyDefaultCollation.scala
index bf31177c83a5..86140ab5ca61 100644
---
a/sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/analysis/ApplyDefaultCollation.scala
+++
b/sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/analysis/ApplyDefaultCollation.scala
@@ -198,15 +198,8 @@ object ApplyDefaultCollation extends Rule[LogicalPlan] {
replaceTable.copy(tableSpec = tableSpec.copy(
collation = getCollationFromSchemaMetadata(catalog,
identifier.namespace())))
- case createView@CreateView(ResolvedIdentifier(
- catalog: SupportsNamespaces, identifier), _, _, _, _, _, _, _, _, _,
_, _, _)
- if createView.collation.isEmpty =>
- val newCreateView = CurrentOrigin.withOrigin(createView.origin) {
- createView.copy(
- collation = getCollationFromSchemaMetadata(catalog,
identifier.namespace()))
- }
- newCreateView.copyTagsFrom(createView)
- newCreateView
+ case createView: CreateView =>
+ resolveCreateViewCollation(createView)
case createUserDefinedFunction@CreateUserDefinedFunction(
ResolvedIdentifier(catalog: SupportsNamespaces, identifier),
@@ -228,6 +221,30 @@ object ApplyDefaultCollation extends Rule[LogicalPlan] {
}
}
+ /**
+ * Fills an unspecified view collation from the target schema, preserving
origin and tags.
+ * Returns the original command if the target is unresolved or metadata
lookup fails.
+ */
+ def resolveCreateViewCollation(createView: CreateView): CreateView = {
+ try {
+ createView.child match {
+ case ResolvedIdentifier(catalog: SupportsNamespaces, identifier)
+ if createView.collation.isEmpty =>
+ val newCreateView = CurrentOrigin.withOrigin(createView.origin) {
+ createView.copy(
+ collation = getCollationFromSchemaMetadata(catalog,
identifier.namespace()))
+ }
+ newCreateView.copyTagsFrom(createView)
+ newCreateView
+ case _ =>
+ createView
+ }
+ } catch {
+ case NonFatal(_) =>
+ createView
+ }
+ }
+
/**
* Retrieves the schema's default collation from the metadata of the given
catalog and schema
* name. Returns None if the default collation is not specified for the
schema.
diff --git
a/sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/analysis/CatalogIdentifierResolution.scala
b/sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/analysis/CatalogIdentifierResolution.scala
index aba685b7c13a..f36db09eea66 100644
---
a/sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/analysis/CatalogIdentifierResolution.scala
+++
b/sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/analysis/CatalogIdentifierResolution.scala
@@ -18,6 +18,7 @@
package org.apache.spark.sql.catalyst.analysis
import org.apache.spark.sql.catalyst.expressions.Attribute
+import org.apache.spark.sql.catalyst.trees.CurrentOrigin
import org.apache.spark.sql.connector.catalog.{CatalogManager, Identifier,
LookupCatalog}
/**
@@ -26,6 +27,19 @@ import
org.apache.spark.sql.connector.catalog.{CatalogManager, Identifier, Looku
class CatalogIdentifierResolution(override val catalogManager: CatalogManager)
extends LookupCatalog {
+ /** Resolves a command target without column output, preserving its origin
and tags. */
+ def resolveUnresolvedIdentifier(
+ unresolvedIdentifier: UnresolvedIdentifier): ResolvedIdentifier = {
+ CurrentOrigin.withOrigin(unresolvedIdentifier.origin) {
+ val resolvedIdentifier = resolveIdentifier(
+ nameParts = unresolvedIdentifier.nameParts,
+ allowTemp = unresolvedIdentifier.allowTemp,
+ columnOutput = Nil)
+ resolvedIdentifier.copyTagsFrom(unresolvedIdentifier)
+ resolvedIdentifier
+ }
+ }
+
/** A temporary view resolves to [[FakeSystemCatalog]] when `allowTemp` is
set. */
def resolveIdentifier(
nameParts: Seq[String],
diff --git
a/sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/analysis/IdentifierResolution.scala
b/sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/analysis/IdentifierResolution.scala
index e274c1e2162a..b0355b58ff8b 100644
---
a/sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/analysis/IdentifierResolution.scala
+++
b/sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/analysis/IdentifierResolution.scala
@@ -17,8 +17,9 @@
package org.apache.spark.sql.catalyst.analysis
-import org.apache.spark.sql.catalyst.expressions.{AliasHelper, EvalHelper,
Expression}
+import org.apache.spark.sql.catalyst.expressions.{AliasHelper, EvalHelper,
Expression, SubqueryExpression, VariableReference}
import org.apache.spark.sql.catalyst.parser.CatalystSqlParser
+import org.apache.spark.sql.catalyst.plans.logical.LogicalPlan
import org.apache.spark.sql.types.StringType
object IdentifierResolution extends AliasHelper with EvalHelper {
@@ -48,4 +49,26 @@ object IdentifierResolution extends AliasHelper with
EvalHelper {
}
}
}
+
+ /**
+ * Collects distinct original name parts of session and SQL-script local
variable references
+ * from a plan, including its subqueries.
+ */
+ def collectTemporaryVariablesInLogicalPlan(plan: LogicalPlan):
Seq[Seq[String]] = {
+ plan.flatMap { operator =>
+ operator.expressions.flatMap(collectTemporaryVariablesInExpressionTree)
+ }.distinct
+ }
+
+ /**
+ * Collects distinct original name parts of session and SQL-script local
variable references
+ * from an expression, including its root and subqueries.
+ */
+ def collectTemporaryVariablesInExpressionTree(expression: Expression):
Seq[Seq[String]] = {
+ expression.flatMap {
+ case subquery: SubqueryExpression =>
collectTemporaryVariablesInLogicalPlan(subquery.plan)
+ case variable: VariableReference => Seq(variable.originalNameParts)
+ case _ => Seq.empty
+ }.distinct
+ }
}
diff --git
a/sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/analysis/ResolveCatalogs.scala
b/sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/analysis/ResolveCatalogs.scala
index faadf6267e51..b8d4cf42ca31 100644
---
a/sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/analysis/ResolveCatalogs.scala
+++
b/sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/analysis/ResolveCatalogs.scala
@@ -144,8 +144,8 @@ class ResolveCatalogs(val catalogManager: CatalogManager)
rejectTimeTravelOptionsForWrite(resolvedIdentifier, writeOptions)
r.copy(name = resolvedIdentifier)
- case UnresolvedIdentifier(nameParts, allowTemp) =>
- resolveIdentifier(nameParts, allowTemp, Nil)
+ case unresolvedIdentifier: UnresolvedIdentifier =>
+
catalogIdentifierResolution.resolveUnresolvedIdentifier(unresolvedIdentifier)
case CurrentNamespace =>
ResolvedNamespace(currentCatalog,
catalogManager.currentNamespace.toImmutableArraySeq)
diff --git
a/sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/analysis/ResolveIdentifierClause.scala
b/sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/analysis/ResolveIdentifierClause.scala
index de3214d59253..24a2300ee1b1 100644
---
a/sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/analysis/ResolveIdentifierClause.scala
+++
b/sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/analysis/ResolveIdentifierClause.scala
@@ -20,7 +20,8 @@ package org.apache.spark.sql.catalyst.analysis
import scala.collection.mutable
import org.apache.spark.SparkException
-import org.apache.spark.sql.catalyst.expressions.{Expression,
SubqueryExpression, VariableReference}
+import
org.apache.spark.sql.catalyst.analysis.IdentifierResolution.{collectTemporaryVariablesInExpressionTree,
collectTemporaryVariablesInLogicalPlan}
+import org.apache.spark.sql.catalyst.expressions.Expression
import org.apache.spark.sql.catalyst.plans.logical.{AlterViewAs,
CacheTableAsSelect, Command, CreateView, LogicalPlan, V2WriteCommand}
import org.apache.spark.sql.catalyst.rules.{Rule, RuleExecutor}
import org.apache.spark.sql.catalyst.trees.TreePattern._
@@ -167,23 +168,4 @@ class ResolveIdentifierClause(earlyBatches:
Seq[RuleExecutor[LogicalPlan]#Batch]
AnalysisContext.get.referredTempVariableNamesUnderIdentifier ++=
collectTemporaryVariablesInExpressionTree(identifierExpr)
}
-
- private def collectTemporaryVariablesInLogicalPlan(child: LogicalPlan):
Seq[Seq[String]] = {
- def collectTempVars(child: LogicalPlan): Seq[Seq[String]] = {
- child.flatMap { plan =>
- plan.expressions.flatMap { e =>
collectTemporaryVariablesInExpressionTree(e) }
- }.distinct
- }
- collectTempVars(child)
- }
-
- // Visits `child` itself as well as its descendants, so that an identifier
expression which is
- // just a variable reference is collected too.
- private def collectTemporaryVariablesInExpressionTree(child: Expression):
Seq[Seq[String]] = {
- child.flatMap {
- case e: SubqueryExpression =>
collectTemporaryVariablesInLogicalPlan(e.plan)
- case r: VariableReference => Seq(r.originalNameParts)
- case _ => Seq.empty
- }.distinct
- }
}
diff --git
a/sql/catalyst/src/test/scala/org/apache/spark/sql/catalyst/analysis/AnalysisSuite.scala
b/sql/catalyst/src/test/scala/org/apache/spark/sql/catalyst/analysis/AnalysisSuite.scala
index 0ddee98ea441..37101372260a 100644
---
a/sql/catalyst/src/test/scala/org/apache/spark/sql/catalyst/analysis/AnalysisSuite.scala
+++
b/sql/catalyst/src/test/scala/org/apache/spark/sql/catalyst/analysis/AnalysisSuite.scala
@@ -31,7 +31,7 @@ import org.apache.spark.SparkException
import org.apache.spark.api.python.PythonEvalType
import org.apache.spark.sql.AnalysisException
import org.apache.spark.sql.catalyst.{AliasIdentifier, QueryPlanningTracker,
TableIdentifier}
-import org.apache.spark.sql.catalyst.catalog.{InMemoryCatalog, SessionCatalog}
+import org.apache.spark.sql.catalyst.catalog.{InMemoryCatalog, SessionCatalog,
VariableDefinition}
import org.apache.spark.sql.catalyst.dsl.expressions._
import org.apache.spark.sql.catalyst.dsl.plans._
import org.apache.spark.sql.catalyst.encoders.ExpressionEncoder
@@ -43,7 +43,7 @@ import org.apache.spark.sql.catalyst.plans.logical._
import org.apache.spark.sql.catalyst.plans.physical.{HashPartitioning,
Partitioning, RangePartitioning, RoundRobinPartitioning}
import org.apache.spark.sql.catalyst.types.DataTypeUtils
import org.apache.spark.sql.catalyst.util._
-import org.apache.spark.sql.connector.catalog.InMemoryTable
+import org.apache.spark.sql.connector.catalog.{Identifier, InMemoryTable}
import org.apache.spark.sql.errors.QueryCompilationErrors
import org.apache.spark.sql.execution.datasources.v2.DataSourceV2Relation
import org.apache.spark.sql.internal.SQLConf
@@ -1922,6 +1922,37 @@ class AnalysisSuite extends AnalysisTest with Matchers {
}
}
+ test("collect temporary variables from expression roots, subqueries and plan
children") {
+ def variable(name: String): VariableReference = {
+ val identifier = Identifier.of(Array("session"), name)
+ VariableReference(
+ originalNameParts = Seq("session", name),
+ catalog = FakeSystemCatalog,
+ identifier = identifier,
+ varDef = VariableDefinition(identifier, "1", Literal(1)))
+ }
+
+ val first = variable("first")
+ val second = variable("second")
+ val third = variable("third")
+ val subqueryPlan = Project(Seq(Alias(Add(second, first), "value")()),
OneRowRelation())
+ val subquery = ScalarSubquery(subqueryPlan)
+ val expression = Add(first, subquery)
+ val childPlan = Project(Seq(Alias(third, "child_value")()),
OneRowRelation())
+ val plan = Project(Seq(Alias(expression, "value")()), childPlan)
+ val expected = Seq(first.originalNameParts, second.originalNameParts)
+
+
assert(IdentifierResolution.collectTemporaryVariablesInExpressionTree(first) ==
+ Seq(first.originalNameParts))
+
assert(IdentifierResolution.collectTemporaryVariablesInExpressionTree(subquery)
==
+ Seq(second.originalNameParts, first.originalNameParts))
+
assert(IdentifierResolution.collectTemporaryVariablesInExpressionTree(expression)
== expected)
+ assert(IdentifierResolution.collectTemporaryVariablesInLogicalPlan(plan) ==
+ Seq(first.originalNameParts, second.originalNameParts,
third.originalNameParts))
+
assert(IdentifierResolution.collectTemporaryVariablesInExpressionTree(Literal(1)).isEmpty)
+
assert(IdentifierResolution.collectTemporaryVariablesInLogicalPlan(OneRowRelation()).isEmpty)
+ }
+
test("IDENTIFIER with alias and RuntimeReplaceable") {
val name = Literal("a").as("name")
val replaceable = new Nvl(Literal("a"), Literal("b"))
diff --git
a/sql/catalyst/src/test/scala/org/apache/spark/sql/catalyst/analysis/CatalogSuite.scala
b/sql/catalyst/src/test/scala/org/apache/spark/sql/catalyst/analysis/CatalogSuite.scala
index 0ac25e628a8e..7786fd742944 100644
---
a/sql/catalyst/src/test/scala/org/apache/spark/sql/catalyst/analysis/CatalogSuite.scala
+++
b/sql/catalyst/src/test/scala/org/apache/spark/sql/catalyst/analysis/CatalogSuite.scala
@@ -20,11 +20,45 @@ package org.apache.spark.sql.catalyst.analysis
import org.apache.spark.sql.catalyst.CurrentUserContext.CURRENT_USER
import org.apache.spark.sql.catalyst.TableIdentifier
import org.apache.spark.sql.catalyst.catalog.{CatalogStorageFormat,
CatalogTable, CatalogTableType}
+import org.apache.spark.sql.catalyst.trees.{CurrentOrigin, Origin, TreeNodeTag}
import org.apache.spark.sql.types.StructType
class CatalogSuite extends AnalysisTest {
+ test("resolve command identifiers with their origin and tags") {
+ val catalogManager = getAnalyzer.catalogManager
+ val resolution = new CatalogIdentifierResolution(catalogManager)
+ val origin = Origin(line = Some(7), startPosition = Some(11))
+ val tag = TreeNodeTag[String]("command-identifier")
+ val unresolved = CurrentOrigin.withOrigin(origin) {
+ UnresolvedIdentifier(Seq("view"), allowTemp = false)
+ }
+ unresolved.setTagValue(tag, "preserved")
+
+ val resolved = resolution.resolveUnresolvedIdentifier(unresolved)
+
+ assert(resolved.catalog == catalogManager.currentCatalog)
+ assert(resolved.identifier.namespace().toSeq == Seq("default"))
+ assert(resolved.identifier.name() == "view")
+ assert(resolved.output.isEmpty)
+ assert(resolved.origin == origin)
+ assert(resolved.getTagValue(tag) == Some("preserved"))
+ }
+
+ test("resolve command identifiers only uses temporary views when allowed") {
+ val catalogManager = getAnalyzer.catalogManager
+ val resolution = new CatalogIdentifierResolution(catalogManager)
+ Seq(true, false).foreach { allowTemp =>
+ val resolved = resolution.resolveUnresolvedIdentifier(
+ UnresolvedIdentifier(Seq("TaBlE"), allowTemp = allowTemp))
+ val expectedCatalog = if (allowTemp) FakeSystemCatalog else
catalogManager.currentCatalog
+ assert(resolved.catalog == expectedCatalog)
+ assert(resolved.identifier.name() == "TaBlE")
+ assert(resolved.output.isEmpty)
+ }
+ }
+
test("desc table when owner is set to null") {
val table = CatalogTable(
identifier = TableIdentifier("tbl", Some("db1")),
diff --git
a/sql/core/src/main/scala/org/apache/spark/sql/catalyst/analysis/ResolveSessionCatalog.scala
b/sql/core/src/main/scala/org/apache/spark/sql/catalyst/analysis/ResolveSessionCatalog.scala
index 85f4a8ebbc44..2413607af28c 100644
---
a/sql/core/src/main/scala/org/apache/spark/sql/catalyst/analysis/ResolveSessionCatalog.scala
+++
b/sql/core/src/main/scala/org/apache/spark/sql/catalyst/analysis/ResolveSessionCatalog.scala
@@ -561,25 +561,8 @@ class ResolveSessionCatalog(val catalogManager:
CatalogManager)
case AlterViewSchemaBinding(ResolvedViewIdentifier(ident), viewSchemaMode)
=>
AlterViewSchemaBindingCommand(ident, viewSchemaMode)
- // The final `_, _, _` are CreateView.isAnalyzed, referredTempFunctions and
- // referredTempVariablesUnderIdentifier. We drop them all:
CreateViewCommand is a separate
- // AnalysisOnlyCommand and gets its own markAsAnalyzed pass from
HandleSpecialCommand after
- // this rewrite.
- case CreateView(CreateViewInSessionCatalog(ident), userSpecifiedColumns,
comment,
- collation, properties, originalText, query, allowExisting, replace,
viewSchemaMode,
- _, _, _) =>
- CreateViewCommand(
- name = ident,
- userSpecifiedColumns = userSpecifiedColumns,
- comment = comment,
- collation = collation,
- properties = properties,
- originalText = originalText,
- plan = query,
- allowExisting = allowExisting,
- replace = replace,
- viewType = PersistedView,
- viewSchemaMode = viewSchemaMode)
+ case createView: CreateView =>
+ ViewCommandHelper.rewriteCreateView(createView)
// CREATE VIEW ... WITH METRICS on the session catalog -> V1 runnable
command. Non-session
// v2 catalogs leave [[CreateMetricView]] in place for
`DataSourceV2Strategy` to dispatch
@@ -843,8 +826,6 @@ class ResolveSessionCatalog(val catalogManager:
CatalogManager)
)
}
- private object CreateViewInSessionCatalog
- extends ResolvedIdentifierInSessionCatalog("CREATE", "VIEW")
private object CreateFunctionInSessionCatalog
extends ResolvedIdentifierInSessionCatalog("CREATE", "FUNCTION")
diff --git
a/sql/core/src/main/scala/org/apache/spark/sql/catalyst/analysis/ViewCommandHelper.scala
b/sql/core/src/main/scala/org/apache/spark/sql/catalyst/analysis/ViewCommandHelper.scala
index ee65c8b88ef6..e9a4ac7790fd 100644
---
a/sql/core/src/main/scala/org/apache/spark/sql/catalyst/analysis/ViewCommandHelper.scala
+++
b/sql/core/src/main/scala/org/apache/spark/sql/catalyst/analysis/ViewCommandHelper.scala
@@ -17,10 +17,10 @@
package org.apache.spark.sql.catalyst.analysis
-import org.apache.spark.sql.catalyst.plans.logical.{DropView, LogicalPlan}
+import org.apache.spark.sql.catalyst.plans.logical.{CreateView, DropView,
LogicalPlan}
import org.apache.spark.sql.connector.catalog.ViewCatalog
import org.apache.spark.sql.errors.QueryCompilationErrors
-import org.apache.spark.sql.execution.command.{DropTableCommand,
DropTempViewCommand}
+import org.apache.spark.sql.execution.command.{CreateViewCommand,
DropTableCommand, DropTempViewCommand}
/**
* Rewrites resolved view commands to their session-catalog commands in
[[ResolveSessionCatalog]].
@@ -61,6 +61,35 @@ object ViewCommandHelper {
dropView
}
+ /**
+ * Lowers a session-catalog [[CreateView]] to [[CreateViewCommand]], leaving
other targets intact.
+ * Fails if a session-catalog target's namespace does not have exactly one
part.
+ * Analysis-only state is not transferred: HandleSpecialCommand marks the
new command analyzed.
+ */
+ def rewriteCreateView(createView: CreateView): LogicalPlan = createView
match {
+ case CreateView(CreateViewInSessionCatalog(ident), userSpecifiedColumns,
comment,
+ collation, properties, originalText, query, allowExisting, replace,
viewSchemaMode,
+ _, _, _) =>
+ CreateViewCommand(
+ name = ident,
+ userSpecifiedColumns = userSpecifiedColumns,
+ comment = comment,
+ collation = collation,
+ properties = properties,
+ originalText = originalText,
+ plan = query,
+ allowExisting = allowExisting,
+ replace = replace,
+ viewType = PersistedView,
+ viewSchemaMode = viewSchemaMode)
+
+ case _ =>
+ createView
+ }
+
private object DropViewInSessionCatalog
extends ResolvedIdentifierInSessionCatalog("DROP", "VIEW")
+
+ private object CreateViewInSessionCatalog
+ extends ResolvedIdentifierInSessionCatalog("CREATE", "VIEW")
}
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]