-
Notifications
You must be signed in to change notification settings - Fork 3k
Spark: Support renaming views #9343
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,31 @@ | ||
| /* | ||
| * 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.plans.logical.views | ||
|
|
||
| import org.apache.spark.sql.catalyst.analysis.LeafNodeWithoutStats | ||
| import org.apache.spark.sql.catalyst.expressions.Attribute | ||
| import org.apache.spark.sql.connector.catalog.Identifier | ||
| import org.apache.spark.sql.connector.catalog.ViewCatalog | ||
|
|
||
| case class ResolvedV2View( | ||
| catalog: ViewCatalog, | ||
| identifier: Identifier) extends LeafNodeWithoutStats { | ||
| override def output: Seq[Attribute] = Nil | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,45 @@ | ||
| /* | ||
| * 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 org.apache.spark.sql.catalyst.InternalRow | ||
| import org.apache.spark.sql.catalyst.expressions.Attribute | ||
| import org.apache.spark.sql.connector.catalog.Identifier | ||
| import org.apache.spark.sql.connector.catalog.ViewCatalog | ||
|
|
||
|
|
||
| case class RenameV2ViewExec( | ||
| catalog: ViewCatalog, | ||
| oldIdent: Identifier, | ||
| newIdent: Identifier) extends LeafV2CommandExec { | ||
|
|
||
| override lazy val output: Seq[Attribute] = Nil | ||
|
|
||
| override protected def run(): Seq[InternalRow] = { | ||
| catalog.renameView(oldIdent, newIdent) | ||
|
|
||
| Seq.empty | ||
| } | ||
|
|
||
|
|
||
| override def simpleString(maxFields: Int): String = { | ||
| s"RenameV2View ${oldIdent} to {newIdent}" | ||
| } | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -23,6 +23,7 @@ | |
|
|
||
| import java.util.List; | ||
| import java.util.Map; | ||
| import java.util.Random; | ||
| import java.util.UUID; | ||
| import java.util.stream.Collectors; | ||
| import java.util.stream.IntStream; | ||
|
|
@@ -590,10 +591,7 @@ public void fullFunctionIdentifier() { | |
| @Test | ||
| public void fullFunctionIdentifierNotRewrittenLoadFailure() { | ||
| String viewName = "fullFunctionIdentifierNotRewrittenLoadFailure"; | ||
| String sql = | ||
| String.format( | ||
| "SELECT spark_catalog.system.bucket(100, 'a') AS bucket_result, 'a' AS value", | ||
| catalogName); | ||
| String sql = "SELECT spark_catalog.system.bucket(100, 'a') AS bucket_result, 'a' AS value"; | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 😅 |
||
|
|
||
| // avoid namespace failures | ||
| sql("USE spark_catalog"); | ||
|
|
@@ -635,6 +633,157 @@ private Catalog tableCatalog() { | |
| return Spark3Util.loadIcebergCatalog(spark, catalogName); | ||
| } | ||
|
|
||
| @Test | ||
| public void renameView() throws NoSuchTableException { | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can you add tests for names that reference temp views? I think we want to know what Spark does. My initial opinion (which could change if Spark does something else!) is that it should fail because the reference is ambiguous.
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I added a test that renames a view which is hidden by a temp view. The behavior is in-line with what we've seen when reading from a view that is hidden by a temp view. Doing a rename will first rename the temp view and re-executing the same rename will then do it for the Iceberg view. |
||
| insertRows(10); | ||
| String viewName = viewName("originalView"); | ||
| String renamedView = viewName("renamedView"); | ||
| String sql = String.format("SELECT id FROM %s", tableName); | ||
|
|
||
| ViewCatalog viewCatalog = viewCatalog(); | ||
|
|
||
| viewCatalog | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The SQL tests will be more cleaner if we support the CREATE VIEW SQL first before RENAME VIEW SQL?
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @rdblue and I talked about this and view creation via SQL requires some additional complexity, so we decided to get a few other smaller PRs/functionality in. #9421 is introducing some of that complexity and once #9421 is in, adding view creation via SQL should be much smaller in terms of complexity/diffset.
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. No, I don't think this is a good direction. We want the tests to be independent. This allows us to more easily test just rename. |
||
| .buildView(TableIdentifier.of(NAMESPACE, viewName)) | ||
| .withQuery("spark", sql) | ||
| .withDefaultNamespace(NAMESPACE) | ||
| .withDefaultCatalog(catalogName) | ||
| .withSchema(schema(sql)) | ||
| .create(); | ||
|
|
||
| sql("ALTER VIEW %s RENAME TO %s", viewName, renamedView); | ||
|
|
||
| List<Object[]> expected = | ||
| IntStream.rangeClosed(1, 10).mapToObj(this::row).collect(Collectors.toList()); | ||
| assertThat(sql("SELECT * FROM %s", renamedView)) | ||
| .hasSize(10) | ||
| .containsExactlyInAnyOrderElementsOf(expected); | ||
| } | ||
|
|
||
| @Test | ||
| public void renameViewHiddenByTempView() throws NoSuchTableException { | ||
| insertRows(10); | ||
| String viewName = viewName("originalView"); | ||
| String renamedView = viewName("renamedView"); | ||
| String sql = String.format("SELECT id FROM %s WHERE id > 5", tableName); | ||
|
|
||
| ViewCatalog viewCatalog = viewCatalog(); | ||
|
|
||
| sql("CREATE TEMPORARY VIEW %s AS SELECT id FROM %s WHERE id <= 5", viewName, tableName); | ||
|
|
||
| viewCatalog | ||
| .buildView(TableIdentifier.of(NAMESPACE, viewName)) | ||
| .withQuery("spark", sql) | ||
| .withDefaultNamespace(NAMESPACE) | ||
| .withDefaultCatalog(catalogName) | ||
| .withSchema(schema(sql)) | ||
| .create(); | ||
|
|
||
| // renames the TEMP VIEW | ||
| sql("ALTER VIEW %s RENAME TO %s", viewName, renamedView); | ||
| assertThat(sql("SELECT * FROM %s", renamedView)) | ||
| .hasSize(5) | ||
| .containsExactlyInAnyOrderElementsOf( | ||
| IntStream.rangeClosed(1, 5).mapToObj(this::row).collect(Collectors.toList())); | ||
|
|
||
| // original view still exists with its name | ||
| assertThat(viewCatalog.viewExists(TableIdentifier.of(NAMESPACE, viewName))).isTrue(); | ||
| assertThat(viewCatalog.viewExists(TableIdentifier.of(NAMESPACE, renamedView))).isFalse(); | ||
| assertThat(sql("SELECT * FROM %s", viewName)) | ||
| .hasSize(5) | ||
| .containsExactlyInAnyOrderElementsOf( | ||
| IntStream.rangeClosed(6, 10).mapToObj(this::row).collect(Collectors.toList())); | ||
|
|
||
| // will rename the Iceberg view | ||
| sql("ALTER VIEW %s RENAME TO %s", viewName, renamedView); | ||
| assertThat(viewCatalog.viewExists(TableIdentifier.of(NAMESPACE, renamedView))).isTrue(); | ||
| } | ||
|
|
||
| @Test | ||
| public void renameViewToDifferentTargetCatalog() { | ||
| String viewName = viewName("originalView"); | ||
| String renamedView = viewName("renamedView"); | ||
| String sql = String.format("SELECT id FROM %s", tableName); | ||
|
|
||
| ViewCatalog viewCatalog = viewCatalog(); | ||
|
|
||
| viewCatalog | ||
| .buildView(TableIdentifier.of(NAMESPACE, viewName)) | ||
| .withQuery("spark", sql) | ||
| .withDefaultNamespace(NAMESPACE) | ||
| .withDefaultCatalog(catalogName) | ||
| .withSchema(schema(sql)) | ||
| .create(); | ||
|
|
||
| assertThatThrownBy(() -> sql("ALTER VIEW %s RENAME TO spark_catalog.%s", viewName, renamedView)) | ||
| .isInstanceOf(AnalysisException.class) | ||
| .hasMessageContaining( | ||
| "Cannot move view between catalogs: from=spark_with_views and to=spark_catalog"); | ||
| } | ||
|
|
||
| @Test | ||
| public void renameNonExistingView() { | ||
| assertThatThrownBy(() -> sql("ALTER VIEW non_existing RENAME TO target")) | ||
| .isInstanceOf(AnalysisException.class) | ||
| .hasMessageContaining("The table or view `non_existing` cannot be found"); | ||
| } | ||
|
|
||
| @Test | ||
| public void renameViewTargetAlreadyExistsAsView() { | ||
| String viewName = viewName("renameViewSource"); | ||
| String target = viewName("renameViewTarget"); | ||
| String sql = String.format("SELECT id FROM %s", tableName); | ||
|
|
||
| ViewCatalog viewCatalog = viewCatalog(); | ||
|
|
||
| viewCatalog | ||
| .buildView(TableIdentifier.of(NAMESPACE, viewName)) | ||
| .withQuery("spark", sql) | ||
| .withDefaultNamespace(NAMESPACE) | ||
| .withDefaultCatalog(catalogName) | ||
| .withSchema(schema(sql)) | ||
| .create(); | ||
|
|
||
| viewCatalog | ||
| .buildView(TableIdentifier.of(NAMESPACE, target)) | ||
| .withQuery("spark", sql) | ||
| .withDefaultNamespace(NAMESPACE) | ||
| .withDefaultCatalog(catalogName) | ||
| .withSchema(schema(sql)) | ||
| .create(); | ||
|
|
||
| assertThatThrownBy(() -> sql("ALTER VIEW %s RENAME TO %s", viewName, target)) | ||
| .isInstanceOf(AnalysisException.class) | ||
| .hasMessageContaining( | ||
| String.format("Cannot create view default.%s because it already exists", target)); | ||
| } | ||
|
|
||
| @Test | ||
| public void renameViewTargetAlreadyExistsAsTable() { | ||
| String viewName = viewName("renameViewSource"); | ||
| String target = viewName("renameViewTarget"); | ||
| String sql = String.format("SELECT id FROM %s", tableName); | ||
|
|
||
| ViewCatalog viewCatalog = viewCatalog(); | ||
|
|
||
| viewCatalog | ||
| .buildView(TableIdentifier.of(NAMESPACE, viewName)) | ||
| .withQuery("spark", sql) | ||
| .withDefaultNamespace(NAMESPACE) | ||
| .withDefaultCatalog(catalogName) | ||
| .withSchema(schema(sql)) | ||
| .create(); | ||
|
|
||
| sql("CREATE TABLE %s (id INT, data STRING)", target); | ||
| assertThatThrownBy(() -> sql("ALTER VIEW %s RENAME TO %s", viewName, target)) | ||
| .isInstanceOf(AnalysisException.class) | ||
| .hasMessageContaining( | ||
| String.format("Cannot create view default.%s because it already exists", target)); | ||
| } | ||
|
|
||
| private String viewName(String viewName) { | ||
| return viewName + new Random().nextInt(1000000); | ||
| } | ||
|
|
||
| private void insertRows(int numRows) throws NoSuchTableException { | ||
| List<SimpleRecord> records = Lists.newArrayListWithCapacity(numRows); | ||
| for (int i = 1; i <= numRows; i++) { | ||
|
|
||
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.
Do we need to check whether this matches temp views?