-
Notifications
You must be signed in to change notification settings - Fork 3k
Views, Spark: Add support for Materialized Views; Integrate with Spark SQL #9830
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
Closed
Closed
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
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
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
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
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
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
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
190 changes: 190 additions & 0 deletions
190
...main/scala/org/apache/spark/sql/execution/datasources/v2/CreateMaterializedViewExec.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,190 @@ | ||
| /* | ||
| * 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.execution.datasources.v2 | ||
|
|
||
|
|
||
| import java.util.UUID | ||
| import org.apache.iceberg.relocated.com.google.common.base.Preconditions | ||
| import org.apache.iceberg.relocated.com.google.common.collect.ImmutableMap | ||
| import org.apache.iceberg.spark.MaterializedViewUtil | ||
| import org.apache.iceberg.spark.Spark3Util | ||
| import org.apache.iceberg.spark.SparkCatalog | ||
| import org.apache.iceberg.spark.source.SparkTable | ||
| import org.apache.iceberg.spark.source.SparkView | ||
| import org.apache.spark.sql.catalyst.InternalRow | ||
| import org.apache.spark.sql.catalyst.analysis.ViewAlreadyExistsException | ||
| import org.apache.spark.sql.catalyst.expressions.Attribute | ||
| import org.apache.spark.sql.connector.catalog.Identifier | ||
| import org.apache.spark.sql.connector.catalog.Table | ||
| import org.apache.spark.sql.connector.catalog.TableChange | ||
| import org.apache.spark.sql.connector.catalog.View | ||
| import org.apache.spark.sql.connector.catalog.ViewCatalog | ||
| import org.apache.spark.sql.connector.expressions.Transform | ||
| import org.apache.spark.sql.types.StructType | ||
| import scala.collection.JavaConverters._ | ||
|
|
||
| case class CreateMaterializedViewExec( | ||
| catalog: ViewCatalog, | ||
| ident: Identifier, | ||
| queryText: String, | ||
| viewSchema: StructType, | ||
| columnAliases: Seq[String], | ||
| columnComments: Seq[Option[String]], | ||
| queryColumnNames: Seq[String], | ||
| comment: Option[String], | ||
| properties: Map[String, String], | ||
| allowExisting: Boolean, | ||
| replace: Boolean, | ||
| storageTableIdentifier: Option[String]) extends LeafV2CommandExec { | ||
|
|
||
| override def output: Seq[Attribute] = Nil | ||
|
|
||
| override protected def run(): Seq[InternalRow] = { | ||
|
|
||
| // Check if storageTableIdentifier is provided, if not, generate a default identifier | ||
| val sparkStorageTableIdentifier = storageTableIdentifier match { | ||
| case Some(identifier) => { | ||
| val catalogAndIdentifier = Spark3Util.catalogAndIdentifier(session, identifier) | ||
| val storageTableCatalogName = catalogAndIdentifier.catalog().name() | ||
| Preconditions.checkState( | ||
| storageTableCatalogName.equals(catalog.name()), | ||
| "Storage table identifier must be in the same catalog as the view." + | ||
| " Found storage table in catalog: %s, expected: %s", | ||
| Array[Object](storageTableCatalogName, catalog.name()) | ||
| ) | ||
| catalogAndIdentifier.identifier() | ||
| } | ||
| case None => MaterializedViewUtil.getDefaultMaterializedViewStorageTableIdentifier(ident) | ||
| } | ||
|
|
||
| val view = createView(sparkStorageTableIdentifier.toString) | ||
|
|
||
| view match { | ||
| case Some(v) => { | ||
| // TODO: Add support for partitioning the storage table | ||
| catalog.asInstanceOf[SparkCatalog].createTable( | ||
| sparkStorageTableIdentifier, | ||
| viewSchema, new Array[Transform](0), ImmutableMap.of[String, String]() | ||
| ) | ||
|
|
||
| // Capture base table state before inserting into the storage table | ||
| val baseTables = MaterializedViewUtil.extractBaseTables(queryText).asScala.toList | ||
| val baseTableSnapshots = getBaseTableSnapshots(baseTables) | ||
| val baseTableSnapshotsProperties = baseTableSnapshots.map { | ||
| case (key, value) => ( | ||
| MaterializedViewUtil.MATERIALIZED_VIEW_BASE_SNAPSHOT_PROPERTY_KEY_PREFIX + key.toString | ||
| ) -> value.toString | ||
| } | ||
|
|
||
|
|
||
| val storageTableProperties = baseTableSnapshotsProperties + | ||
| (MaterializedViewUtil.MATERIALIZED_VIEW_VERSION_PROPERTY_KEY -> getViewVersion(v).toString) | ||
|
|
||
| // Insert into the storage table | ||
| session.sql("INSERT INTO " + sparkStorageTableIdentifier + " " + queryText) | ||
|
|
||
|
|
||
| // Update the storage table properties | ||
| val storageTablePropertyChanges = storageTableProperties.map { | ||
| case (key, value) => TableChange.setProperty(key, value) | ||
| }.toArray | ||
|
|
||
| catalog.asInstanceOf[SparkCatalog].alterTable(sparkStorageTableIdentifier, storageTablePropertyChanges: _*) | ||
| } | ||
| case None => | ||
| } | ||
| Nil | ||
| } | ||
|
|
||
| override def simpleString(maxFields: Int): String = { | ||
| s"CreateMaterializedViewExec: ${ident}" | ||
| } | ||
|
|
||
| private def createView(storageTableIdentifier: String): Option[View] = { | ||
| val currentCatalogName = session.sessionState.catalogManager.currentCatalog.name | ||
| val currentCatalog = if (!catalog.name().equals(currentCatalogName)) currentCatalogName else null | ||
| val currentNamespace = session.sessionState.catalogManager.currentNamespace | ||
|
|
||
| val engineVersion = "Spark " + org.apache.spark.SPARK_VERSION | ||
| val newProperties = properties ++ | ||
| comment.map(ViewCatalog.PROP_COMMENT -> _) + | ||
| (ViewCatalog.PROP_CREATE_ENGINE_VERSION -> engineVersion, | ||
| ViewCatalog.PROP_ENGINE_VERSION -> engineVersion) + | ||
| (MaterializedViewUtil.MATERIALIZED_VIEW_PROPERTY_KEY -> "true") + | ||
| (MaterializedViewUtil.MATERIALIZED_VIEW_STORAGE_TABLE_PROPERTY_KEY -> storageTableIdentifier) | ||
|
|
||
| if (replace) { | ||
| // CREATE OR REPLACE VIEW | ||
| if (catalog.viewExists(ident)) { | ||
| catalog.dropView(ident) | ||
| } | ||
| // FIXME: replaceView API doesn't exist in Spark 3.5 | ||
| val view = catalog.createView( | ||
| ident, | ||
| queryText, | ||
| currentCatalog, | ||
| currentNamespace, | ||
| viewSchema, | ||
| queryColumnNames.toArray, | ||
| columnAliases.toArray, | ||
| columnComments.map(c => c.orNull).toArray, | ||
| newProperties.asJava) | ||
| Some(view) | ||
| } else { | ||
| try { | ||
| // CREATE VIEW [IF NOT EXISTS] | ||
| val view = catalog.createView( | ||
| ident, | ||
| queryText, | ||
| currentCatalog, | ||
| currentNamespace, | ||
| viewSchema, | ||
| queryColumnNames.toArray, | ||
| columnAliases.toArray, | ||
| columnComments.map(c => c.orNull).toArray, | ||
| newProperties.asJava) | ||
| Some(view) | ||
| } catch { | ||
| // TODO: Make sure the existing view is also a materialized view | ||
| case _: ViewAlreadyExistsException if allowExisting => None | ||
| } | ||
| } | ||
| } | ||
|
|
||
| private def getBaseTableSnapshots(baseTables: List[Table]): Map[UUID, Long] = { | ||
| baseTables.map { | ||
| case sparkTable: SparkTable => | ||
| val snapshot = Option(sparkTable.table().currentSnapshot()) | ||
| val snapshotId = snapshot.map(_.snapshotId().longValue()).getOrElse(0L) | ||
| (sparkTable.table().uuid(), snapshotId) | ||
| case _ => | ||
| throw new UnsupportedOperationException("Only Spark tables are supported") | ||
| }.toMap | ||
| } | ||
|
|
||
| private def getViewVersion(view: View): Long = { | ||
| view match { | ||
| case sparkView: SparkView => | ||
| sparkView.view().currentVersion().versionId() | ||
| case _ => | ||
| throw new UnsupportedOperationException("Only Spark views are supported") | ||
| } | ||
| } | ||
| } |
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
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Redundant change