huaxingao commented on code in PR #13979:
URL: https://github.com/apache/iceberg/pull/13979#discussion_r4351436891
##########
core/src/main/java/org/apache/iceberg/CachingCatalog.java:
##########
@@ -168,6 +169,15 @@ public Table loadTable(TableIdentifier ident) {
return table;
}
+ @Override
+ public Table loadTable(TableIdentifier ident, LoadContext context) {
+ if (context == null || context.referencedBy().isEmpty()) {
+ return loadTable(ident);
+ }
+
+ return catalog.loadTable(ident, context);
Review Comment:
Should this be `catalog.loadTable(canonicalizeIdentifier(ident), context)`,
like the no-context path? With `cache.case-sensitive=false` the two paths send
different identifiers.
##########
spark/v4.2/spark-extensions/src/main/scala/org/apache/spark/sql/catalyst/analysis/ResolveReferencedByViews.scala:
##########
@@ -0,0 +1,281 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.apache.spark.sql.catalyst.analysis
+
+import org.apache.iceberg.catalog.LoadContext
+import org.apache.iceberg.spark.SparkSupportsLoadContext
+import org.apache.spark.sql.SparkSession
+import org.apache.spark.sql.catalyst.expressions.Alias
+import org.apache.spark.sql.catalyst.expressions.Cast
+import org.apache.spark.sql.catalyst.expressions.SubqueryExpression
+import org.apache.spark.sql.catalyst.expressions.UpCast
+import org.apache.spark.sql.catalyst.plans.logical.{View => ViewNode}
+import org.apache.spark.sql.catalyst.plans.logical.LogicalPlan
+import org.apache.spark.sql.catalyst.plans.logical.Project
+import org.apache.spark.sql.catalyst.plans.logical.SubqueryAlias
+import
org.apache.spark.sql.catalyst.plans.logical.views.UnResolvedRelationFromView
+import org.apache.spark.sql.catalyst.rules.Rule
+import org.apache.spark.sql.catalyst.trees.CurrentOrigin
+import org.apache.spark.sql.catalyst.trees.TreeNodeTag
+import org.apache.spark.sql.connector.catalog.CatalogManager
+import org.apache.spark.sql.connector.catalog.CatalogPlugin
+import org.apache.spark.sql.connector.catalog.DelegatingTable
+import org.apache.spark.sql.connector.catalog.Identifier
+import org.apache.spark.sql.connector.catalog.LookupCatalog
+import org.apache.spark.sql.connector.catalog.TableCatalog
+import org.apache.spark.sql.connector.catalog.V1Table
+import org.apache.spark.sql.connector.catalog.View
+import org.apache.spark.sql.errors.QueryCompilationErrors
+import org.apache.spark.sql.execution.datasources.v2.DataSourceV2Relation
+
+/**
+ * Expands Iceberg views when referenced-by is enabled, so that the tables and
views they read are
+ * loaded with the referencing view chain.
+ *
+ * Spark 4.2 expands views itself without passing the view identity to the
catalog, so with
+ * referenced-by enabled Iceberg catalogs leave views unresolved for this rule.
+ */
+case class ResolveReferencedByViews(spark: SparkSession)
+ extends Rule[LogicalPlan]
+ with LookupCatalog {
+
+ import ResolveReferencedByViews.CheckedForView
+ import org.apache.spark.sql.connector.catalog.CatalogV2Implicits._
+
+ protected lazy val catalogManager: CatalogManager =
spark.sessionState.catalogManager
+
+ override def apply(plan: LogicalPlan): LogicalPlan = {
+ if (ViewUtil.referencedByEnabled) {
+ resolveWithReferencedBy(plan)
+ } else {
+ plan
+ }
+ }
+
+ private def resolveWithReferencedBy(plan: LogicalPlan): LogicalPlan = plan
resolveOperators {
+ case u @ UnresolvedRelation(nameParts, _, _) if isTempView(nameParts) =>
+ u
+
+ case u @ UnresolvedRelation(CatalogAndIdentifier(catalog, ident), _, true)
+ if u.getTagValue(CheckedForView).isEmpty =>
+ ViewUtil.loadView(catalog, ident).foreach { _ =>
+ throw
QueryCompilationErrors.permanentViewNotSupportedByStreamingReadingAPIError(
+ ident.quoted)
+ }
+ markCheckedForView(u)
+
+ case u @ UnresolvedRelation(parts @ CatalogAndIdentifier(catalog, ident),
options, _)
+ if !options.containsKey(UnresolvedRelation.REQUIRED_WRITE_PRIVILEGES)
&&
+ u.getTagValue(CheckedForView).isEmpty =>
+ ViewUtil
+ .loadView(catalog, ident)
+ .map(createViewRelation(parts, catalog, ident, _))
+ .getOrElse(markCheckedForView(u))
+
+ case u @ UnResolvedRelationFromView(
+ tableParts @ CatalogAndIdentifier(catalog, tableIdent),
+ _,
+ _,
+ viewChain,
+ options,
+ isStreaming,
+ timeTravelVersion,
+ timeTravelTimestamp) if timeTravelTimestamp.forall(_.resolved) =>
+ val referencedBy = ViewUtil.buildReferencedByChain(viewChain,
catalog.name())
+ val context = LoadContext.builder().referencedBy(referencedBy).build()
+ val timeTravelSpec =
+ TimeTravelSpec.create(timeTravelTimestamp, timeTravelVersion,
conf.sessionLocalTimeZone)
+ try {
+ val table = ViewUtil.loadTable(catalog, tableIdent, context,
timeTravelSpec)
+ table match {
+ case _: V1Table | _: DelegatingTable =>
+ val relation = markCheckedForView(UnresolvedRelation(tableParts,
options, isStreaming))
+ if (timeTravelSpec.isEmpty) {
+ relation
+ } else {
+ RelationTimeTravel(relation, timeTravelTimestamp,
timeTravelVersion)
+ }
+
+ case _ =>
+ val relationCatalog = catalog match {
+ case contextual: TableCatalog with SparkSupportsLoadContext
+ if timeTravelSpec.isEmpty =>
+ LoadContextTableCatalog(contextual, context)
+ case _ => catalog
+ }
+ SubqueryAlias(
+ tableIdent.toQualifiedNameParts(catalog),
+ DataSourceV2Relation.create(
+ table,
+ Some(relationCatalog),
+ Some(tableIdent),
+ options,
+ timeTravelSpec))
+ }
+ } catch {
+ case _: NoSuchTableException =>
+ ViewUtil
+ .loadView(catalog, tableIdent, context)
+ .map(view => createViewRelation(tableParts, catalog, tableIdent,
view, viewChain))
Review Comment:
When the flag is off, Spark expands the view and checks the depth
(`spark.sql.view.maxNestedViewDepth`), so a view cycle fails with
`VIEW_EXCEED_MAX_NESTED_DEPTH`. With the flag on, this rule expands the view
itself and there is no depth check. So if two views read each other (e.g. `a`
reads `b`, and `b` reads `a`), it keeps expanding forever. Should we add a
check here, e.g. fail if the view is already in `viewChain`, or if `viewChain`
is longer than `maxNestedViewDepth`?
--
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]