-
Notifications
You must be signed in to change notification settings - Fork 3k
Spark: Add support for reading Iceberg views #9340
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
13 commits
Select commit
Hold shift + click to select a range
038229e
Spark: Add support for reading Iceberg views
nastra 14c2109
simplifications
nastra 2291895
tests
nastra bea811f
fix tests
nastra 21b5c97
remove spark session catalog support (for now)
nastra 4643097
test for reading from a stale view
nastra d999343
view should not be able to ref a temp view
nastra d397cb6
add test with invalid view SQL / view referencing global temp view
nastra 9b9529a
more simplifications
nastra 071b6dc
throw UOE for unexercised methods
nastra 41b0580
Update resolution to apply aliases and comments.
rdblue 84fa509
Fix projection and aliasing, update tests.
rdblue d8b7888
Add function identifier rewriting.
rdblue File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
146 changes: 146 additions & 0 deletions
146
...spark-extensions/src/main/scala/org/apache/spark/sql/catalyst/analysis/ResolveViews.scala
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,146 @@ | ||
| /* | ||
| * 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.spark.sql.SparkSession | ||
| import org.apache.spark.sql.catalyst.FunctionIdentifier | ||
| import org.apache.spark.sql.catalyst.expressions.Alias | ||
| import org.apache.spark.sql.catalyst.expressions.UpCast | ||
| import org.apache.spark.sql.catalyst.parser.ParseException | ||
| 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.rules.Rule | ||
| import org.apache.spark.sql.catalyst.trees.CurrentOrigin | ||
| import org.apache.spark.sql.catalyst.trees.Origin | ||
| import org.apache.spark.sql.connector.catalog.CatalogManager | ||
| import org.apache.spark.sql.connector.catalog.CatalogPlugin | ||
| import org.apache.spark.sql.connector.catalog.Identifier | ||
| import org.apache.spark.sql.connector.catalog.LookupCatalog | ||
| import org.apache.spark.sql.connector.catalog.View | ||
| import org.apache.spark.sql.connector.catalog.ViewCatalog | ||
| import org.apache.spark.sql.errors.QueryCompilationErrors | ||
|
|
||
| case class ResolveViews(spark: SparkSession) extends Rule[LogicalPlan] with LookupCatalog { | ||
|
|
||
| import org.apache.spark.sql.connector.catalog.CatalogV2Implicits._ | ||
|
|
||
| protected lazy val catalogManager: CatalogManager = spark.sessionState.catalogManager | ||
|
|
||
| override def apply(plan: LogicalPlan): LogicalPlan = plan resolveOperators { | ||
| case u@UnresolvedRelation(nameParts, _, _) | ||
| if catalogManager.v1SessionCatalog.isTempView(nameParts) => | ||
| u | ||
|
|
||
| case u@UnresolvedRelation(parts@CatalogAndIdentifier(catalog, ident), _, _) => | ||
| loadView(catalog, ident) | ||
| .map(createViewRelation(parts, _)) | ||
| .getOrElse(u) | ||
| } | ||
|
|
||
| def loadView(catalog: CatalogPlugin, ident: Identifier): Option[View] = catalog match { | ||
| case viewCatalog: ViewCatalog => | ||
| try { | ||
| Option(viewCatalog.loadView(ident)) | ||
| } catch { | ||
| case _: NoSuchViewException => None | ||
| } | ||
| case _ => None | ||
| } | ||
|
|
||
| private def createViewRelation(nameParts: Seq[String], view: View): LogicalPlan = { | ||
| 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); | ||
|
|
||
| // Apply the field aliases and column comments | ||
| // This logic differs from how Spark handles views in SessionCatalog.fromCatalogTable. | ||
| // This is more strict because it doesn't allow resolution by field name. | ||
| val aliases = view.schema.fields.zipWithIndex.map { case (expected, pos) => | ||
| val attr = GetColumnByOrdinal(pos, expected.dataType) | ||
| Alias(UpCast(attr, expected.dataType), expected.name)(explicitMetadata = Some(expected.metadata)) | ||
| } | ||
|
|
||
| SubqueryAlias(nameParts, Project(aliases, rewritten)) | ||
| } | ||
|
|
||
| private def parseViewText(name: String, viewText: String): LogicalPlan = { | ||
| val origin = Origin( | ||
| objectType = Some("VIEW"), | ||
| objectName = Some(name) | ||
| ) | ||
|
|
||
| try { | ||
| CurrentOrigin.withOrigin(origin) { | ||
| spark.sessionState.sqlParser.parseQuery(viewText) | ||
| } | ||
| } catch { | ||
| case _: ParseException => | ||
| throw QueryCompilationErrors.invalidViewText(viewText, name) | ||
| } | ||
| } | ||
|
|
||
| private def rewriteIdentifiers( | ||
| plan: LogicalPlan, | ||
| catalogAndNamespace: Seq[String]): LogicalPlan = { | ||
| // Substitute CTEs within the view, then rewrite unresolved functions and relations | ||
| qualifyTableIdentifiers( | ||
| qualifyFunctionIdentifiers( | ||
| CTESubstitution.apply(plan), | ||
| catalogAndNamespace), | ||
| catalogAndNamespace) | ||
| } | ||
|
|
||
| private def qualifyFunctionIdentifiers( | ||
| plan: LogicalPlan, | ||
| catalogAndNamespace: Seq[String]): LogicalPlan = plan transformExpressions { | ||
| case u@UnresolvedFunction(Seq(name), _, _, _, _) => | ||
| if (!isBuiltinFunction(name)) { | ||
| u.copy(nameParts = catalogAndNamespace :+ name) | ||
| } else { | ||
| u | ||
| } | ||
| case u@UnresolvedFunction(parts, _, _, _, _) if !isCatalog(parts.head) => | ||
| u.copy(nameParts = catalogAndNamespace.head +: parts) | ||
| } | ||
|
|
||
| /** | ||
| * Qualify table identifiers with default catalog and namespace if necessary. | ||
| */ | ||
| private def qualifyTableIdentifiers( | ||
| child: LogicalPlan, | ||
| catalogAndNamespace: Seq[String]): LogicalPlan = | ||
| child transform { | ||
| case u@UnresolvedRelation(Seq(table), _, _) => | ||
| u.copy(multipartIdentifier = catalogAndNamespace :+ table) | ||
| case u@UnresolvedRelation(parts, _, _) if !isCatalog(parts.head) => | ||
| u.copy(multipartIdentifier = catalogAndNamespace.head +: parts) | ||
| } | ||
|
|
||
| private def isCatalog(name: String): Boolean = { | ||
| spark.sessionState.catalogManager.isCatalogRegistered(name) | ||
| } | ||
|
|
||
| private def isBuiltinFunction(name: String): Boolean = { | ||
| spark.sessionState.catalogManager.v1SessionCatalog.isBuiltinFunction(FunctionIdentifier(name)) | ||
| } | ||
| } | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.