singhpk234 commented on code in PR #19001:
URL: https://github.com/apache/iceberg/pull/19001#discussion_r4705162684
##########
spark/v4.1/spark-extensions/src/main/scala/org/apache/spark/sql/catalyst/analysis/ResolveViews.scala:
##########
@@ -104,34 +121,136 @@ case class ResolveViews(spark: SparkSession) extends
Rule[LogicalPlan] with Look
}
}
- private def createViewRelation(nameParts: Seq[String], view: View):
LogicalPlan = {
- val parsed = parseViewText(nameParts.quoted, view.query)
+ private def resolveRelationFromView(
+ relation: UnResolvedRelationFromView,
+ catalog: CatalogPlugin,
+ ident: Identifier): LogicalPlan = {
+ val parts = relation.tableMultipartIdentifier
+ val referencedBy = ViewUtil.buildReferencedByChain(relation.viewChain,
catalog.name())
+ catalog match {
+ case contextual: TableCatalog with SparkSupportsLoadContext if
!relation.isStreaming =>
+ val context = LoadContext.builder().referencedBy(referencedBy).build()
+ val timeTravelSpec = TimeTravelSpec.create(
+ relation.timeTravelTimestamp,
+ relation.timeTravelVersion,
+ conf.sessionLocalTimeZone)
+ try {
+ ViewUtil.loadTable(contextual, ident, context, timeTravelSpec) match
{
+ case _: V1Table =>
+ toUnresolvedRelation(relation)
+ case table =>
+ val relationCatalog = if (timeTravelSpec.isEmpty) {
+ LoadContextTableCatalog(contextual, context)
+ } else {
+ contextual
+ }
+ SubqueryAlias(
+ catalog.name +: ident.asMultipartIdentifier,
+ DataSourceV2Relation.create(
+ table,
+ Some(relationCatalog),
+ Some(ident),
+ relation.options,
+ timeTravelSpec))
+ }
+ } catch {
+ case _: NoSuchTableException =>
+ ViewUtil
+ .loadView(catalog, ident, context)
+ .map(createViewRelation(parts, catalog, ident, _,
relation.viewChain))
+ .getOrElse(toUnresolvedRelation(relation))
+ }
+
+ case _ =>
+ toUnresolvedRelation(relation)
+ }
+ }
+
+ private def toUnresolvedRelation(relation: UnResolvedRelationFromView):
LogicalPlan = {
+ val unresolved =
+ UnresolvedRelation(relation.tableMultipartIdentifier, relation.options,
relation.isStreaming)
+ if (relation.timeTravelVersion.isEmpty &&
relation.timeTravelTimestamp.isEmpty) {
+ unresolved
+ } else {
+ RelationTimeTravel(unresolved, relation.timeTravelTimestamp,
relation.timeTravelVersion)
+ }
+ }
+
+ private def createViewRelation(
+ nameParts: Seq[String],
+ catalog: CatalogPlugin,
+ ident: Identifier,
+ view: View,
+ existingChain: Seq[Seq[String]] = Seq.empty): LogicalPlan = {
+ // Session settings are read before switching to the view's captured SQL
configs, which
+ // don't include them
+ val sessionConf = spark.sessionState.conf
+ val viewIdent = catalog.name +: ident.asMultipartIdentifier
+ val viewChain = if (ViewUtil.referencedByEnabled(sessionConf)) {
+ existingChain :+ viewIdent
+ } else {
+ Seq.empty
+ }
+ val compensate = viewSchemaMode(sessionConf) ==
SparkSQLProperties.VIEW_SCHEMA_MODE_COMPENSATION
- // Apply any necessary rewrites to preserve correct resolution
- val viewCatalogAndNamespace: Seq[String] = view.currentCatalog +:
view.currentNamespace.toSeq
- val rewritten = rewriteIdentifiers(parsed, viewCatalogAndNamespace);
+ // Like Spark's own views, the view body is parsed and analyzed with the
SQL configs captured
+ // at creation, and nested views are expanded within the outer view's
analysis
+ ViewUtil.withNestedView(viewIdent, sessionConf.maxNestedViewDepth) {
+ val viewConf = ViewNode.effectiveSQLConf(ViewUtil.sqlConfigs(view),
isTempView = false)
+ SQLConf.withExistingConf(viewConf) {
+ val parsed = parseViewText(nameParts.quoted, view.query)
+
+ // Apply any necessary rewrites to preserve correct resolution
+ val viewCatalogAndNamespace: Seq[String] =
+ view.currentCatalog +: view.currentNamespace.toSeq
+ val rewritten = rewriteIdentifiers(parsed, viewCatalogAndNamespace,
viewChain)
+
+ val columns = viewColumns(nameParts.quoted, view, viewConf, compensate)
+ val analyzed = spark.sessionState.analyzer.execute(Project(columns,
rewritten))
+ spark.sessionState.analyzer.checkAnalysis(analyzed)
+ SubqueryAlias(nameParts, analyzed)
+ }
+ }
+ }
+
+ private def viewColumns(
+ viewName: String,
+ view: View,
+ viewConf: SQLConf,
+ compensate: Boolean): Seq[NamedExpression] = {
+ val queryColumnNames = view.queryColumnNames.toIndexedSeq
+ val bindByName = queryColumnNames.length == view.schema.length
Review Comment:
Agreed. 4.2 picks this up from Spark's native view resolution, but here it
would change binding for existing 3.5/4.0/4.1 views regardless of the flag.
Reverted to positional binding in d9b2889346; matching 4.2's semantics can be a
separate PR if we want it.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]