-
Notifications
You must be signed in to change notification settings - Fork 3k
Spark: Fix CREATE VIEW IF NOT EXISTS failure when non-Iceberg view exists in SparkSessionCatalog #14930
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
base: main
Are you sure you want to change the base?
Spark: Fix CREATE VIEW IF NOT EXISTS failure when non-Iceberg view exists in SparkSessionCatalog #14930
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,99 @@ | ||
| /* | ||
| * 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.iceberg.spark.extensions; | ||
|
|
||
| import static org.assertj.core.api.Assertions.assertThatThrownBy; | ||
|
|
||
| import org.apache.iceberg.ParameterizedTestExtension; | ||
| import org.apache.iceberg.Parameters; | ||
| import org.apache.iceberg.spark.SparkCatalogConfig; | ||
| import org.apache.spark.sql.AnalysisException; | ||
| import org.junit.jupiter.api.AfterEach; | ||
| import org.junit.jupiter.api.BeforeEach; | ||
| import org.junit.jupiter.api.TestTemplate; | ||
| import org.junit.jupiter.api.extension.ExtendWith; | ||
|
|
||
| @ExtendWith(ParameterizedTestExtension.class) | ||
| public class TestCreateView extends ExtensionsTestBase { | ||
|
|
||
| @Parameters(name = "catalogName = {0}, implementation = {1}, config = {2}") | ||
| public static Object[][] parameters() { | ||
| return new Object[][] { | ||
| { | ||
| SparkCatalogConfig.SPARK_SESSION.catalogName(), | ||
| SparkCatalogConfig.SPARK_SESSION.implementation(), | ||
| SparkCatalogConfig.SPARK_SESSION.properties() | ||
| } | ||
| }; | ||
| } | ||
|
|
||
| @BeforeEach | ||
| public void useIcebergCatalog() { | ||
| spark | ||
| .conf() | ||
| .set("spark.sql.catalog.spark_catalog", "org.apache.iceberg.spark.SparkSessionCatalog"); | ||
| spark.conf().set("spark.sql.catalog.spark_catalog.type", "hive"); | ||
| spark.sessionState().catalogManager().reset(); | ||
| } | ||
|
|
||
| @AfterEach | ||
| public void useHiveCatalog() { | ||
| spark.conf().unset("spark.sql.catalog.spark_catalog"); | ||
| spark.conf().unset("spark.sql.catalog.spark_catalog.type"); | ||
| spark.sessionState().catalogManager().reset(); | ||
| } | ||
|
|
||
| @TestTemplate | ||
| public void testCreateViewIfNotExistsWithExistingHiveView() { | ||
|
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 please also add the same set of tests for tables, where we have a v1 hive table and we want to create another one using/not using IF NOT EXISTS
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. this is so that it's easier to check how tables/views behave exactly and to align their behavior |
||
| String viewName = "default.existing_hive_view"; | ||
|
|
||
| useHiveCatalog(); | ||
| try { | ||
| // create Hive view | ||
| spark.sql(String.format("CREATE VIEW %s AS SELECT 1 AS id", viewName)); | ||
| } finally { | ||
|
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. I don't think we need any of those try-finally blocks |
||
| useIcebergCatalog(); | ||
| } | ||
|
|
||
| try { | ||
| spark.sql(String.format("CREATE VIEW IF NOT EXISTS %s AS SELECT 2 AS id", viewName)); | ||
|
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. instead of using |
||
| } finally { | ||
| spark.sql(String.format("DROP VIEW IF EXISTS %s", viewName)); | ||
| } | ||
| } | ||
|
|
||
| @TestTemplate | ||
| public void testCreateViewWithExistingHiveView() { | ||
|
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. nit: no need to use |
||
| String viewName = "default.collision_view"; | ||
|
|
||
| useHiveCatalog(); | ||
| try { | ||
| // create Hive view | ||
| spark.sql(String.format("CREATE VIEW %s AS SELECT 1 AS id", viewName)); | ||
| } finally { | ||
| useIcebergCatalog(); | ||
| } | ||
|
|
||
| assertThatThrownBy(() -> spark.sql(String.format("CREATE VIEW %s AS SELECT 2 AS id", viewName))) | ||
| .isInstanceOf(AnalysisException.class) | ||
| .hasMessageContaining("VIEW_ALREADY_EXISTS"); | ||
|
|
||
| spark.sql(String.format("DROP VIEW IF EXISTS %s", viewName)); | ||
| } | ||
| } | ||
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.
I believe this is then going to use Spark's
HiveSessionCatalogright? We might want to be clearer here as otherwise one would expect that we're using Iceberg'sHiveCatalog