You are viewing a plain text version of this content. The canonical link for it is here.
Posted to issues@iceberg.apache.org by "nastra (via GitHub)" <gi...@apache.org> on 2024/01/18 11:58:34 UTC
[PR] Spark: Support altering views [iceberg]
nastra opened a new pull request, #9510:
URL: https://github.com/apache/iceberg/pull/9510
This adds support for the below `ALTER` cases as defined in https://spark.apache.org/docs/latest/sql-ref-syntax-ddl-alter-view.html:
* `ALTER VIEW <viewName> AS <query>`
* `ALTER VIEW <viewName> SET TBLPROPERTIES (...)`
* `ALTER VIEW <viewName> UNSET TBLPROPERTIES (...)`
--
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: issues-unsubscribe@iceberg.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: issues-unsubscribe@iceberg.apache.org
For additional commands, e-mail: issues-help@iceberg.apache.org
Re: [PR] Spark: Support altering views [iceberg]
Posted by "nastra (via GitHub)" <gi...@apache.org>.
nastra commented on code in PR #9510:
URL: https://github.com/apache/iceberg/pull/9510#discussion_r1467925434
##########
spark/v3.5/spark-extensions/src/test/java/org/apache/iceberg/spark/extensions/TestViews.java:
##########
@@ -1149,10 +1148,343 @@ public void createViewWithSubqueryExpressionUsingGlobalTempView() {
"because it references to the temporary object global_temp.%s", globalTempView));
}
+ @Test
+ public void alterView() throws NoSuchTableException {
+ insertRows(6);
+ String viewName = "alteredView";
+
+ sql("CREATE VIEW %s AS SELECT id, data FROM %s WHERE id <= 3", viewName, tableName);
+
+ assertThat(sql("SELECT id FROM %s", viewName))
+ .hasSize(3)
+ .containsExactlyInAnyOrder(row(1), row(2), row(3));
+
+ sql("ALTER VIEW %s AS SELECT id FROM %s WHERE id > 3", viewName, tableName);
+
+ assertThat(sql("SELECT * FROM %s", viewName))
+ .hasSize(3)
+ .containsExactlyInAnyOrder(row(4), row(5), row(6));
+ }
+
+ @Test
+ public void alterNonExistingView() {
+ // this is a bit tricky because the view can't be found in the view catalog and
+ // resolution bubbles up to check whether it's a V1 view but then fails, indicating that the
+ // catalog doesn't support views
+ assertThatThrownBy(
+ () -> sql("ALTER VIEW non_existing AS SELECT id FROM %s WHERE id > 3", tableName))
+ .isInstanceOf(AnalysisException.class)
+ .hasMessageContaining(
+ String.format(
+ "The feature is not supported: Catalog `%s` does not support views", catalogName));
+ }
+
+ @Test
+ public void alterViewWithInvalidSQL() {
+ String viewName = "alteredViewWithInvalidSQL";
+
+ sql("CREATE VIEW %s AS SELECT id, data FROM %s WHERE id <= 3", viewName, tableName);
+ assertThatThrownBy(() -> sql("ALTER VIEW %s AS invalid SQL", viewName, tableName))
+ .isInstanceOf(AnalysisException.class)
+ .hasMessageContaining("Syntax error");
+ }
+
+ @Test
+ public void alterViewReferencingTempView() throws NoSuchTableException {
+ insertRows(10);
+ String tempView = "temporaryViewBeingReferencedInView";
+ String viewReferencingTempView = "alteredViewReferencingTemporaryView";
+
+ sql("CREATE TEMPORARY VIEW %s AS SELECT id FROM %s WHERE id <= 5", tempView, tableName);
+ sql("CREATE VIEW %s AS SELECT id FROM %s WHERE id <= 5", viewReferencingTempView, tableName);
+
+ // altering a view that references a TEMP VIEW shouldn't be possible
+ assertThatThrownBy(
+ () -> sql("ALTER VIEW %s AS SELECT id FROM %s", viewReferencingTempView, tempView))
+ .isInstanceOf(AnalysisException.class)
+ .hasMessageContaining("Cannot create the persistent object")
+ .hasMessageContaining(viewReferencingTempView)
+ .hasMessageContaining("of the type VIEW because it references to the temporary object")
+ .hasMessageContaining(tempView);
+ }
+
+ @Test
+ public void alterViewReferencingGlobalTempView() throws NoSuchTableException {
+ insertRows(10);
+ String globalTempView = "globalTemporaryViewBeingReferencedInView";
+ String viewReferencingTempView = "alteredViewReferencingGlobalTemporaryView";
+
+ sql(
+ "CREATE GLOBAL TEMPORARY VIEW %s AS SELECT id FROM %s WHERE id <= 5",
+ globalTempView, tableName);
+
+ sql("CREATE VIEW %s AS SELECT id FROM %s WHERE id <= 5", viewReferencingTempView, tableName);
+
+ // altering a view that references a GLOBAL TEMP VIEW shouldn't be possible
+ assertThatThrownBy(
+ () ->
+ sql(
+ "ALTER VIEW %s AS SELECT id FROM global_temp.%s",
+ viewReferencingTempView, globalTempView))
+ .isInstanceOf(AnalysisException.class)
+ .hasMessageContaining("Cannot create the persistent object")
+ .hasMessageContaining(viewReferencingTempView)
+ .hasMessageContaining("of the type VIEW because it references to the temporary object")
+ .hasMessageContaining(globalTempView);
+ }
+
+ @Test
+ public void alterViewWithUpdatedQueryColumns() throws NoSuchTableException {
+ insertRows(6);
+ String viewName = viewName("viewWithQueryColumns");
+
+ sql(
+ "CREATE VIEW %s (new_id COMMENT 'ID', new_data COMMENT 'DATA') AS SELECT id, data FROM %s WHERE id <= 3",
+ viewName, tableName);
+
+ View view = viewCatalog().loadView(TableIdentifier.of(NAMESPACE, viewName));
+ assertThat(view.properties()).containsEntry("queryColumnNames", "id,data");
+
+ assertThat(view.schema().columns()).hasSize(2);
+ Types.NestedField first = view.schema().columns().get(0);
+ assertThat(first.name()).isEqualTo("new_id");
+ assertThat(first.doc()).isEqualTo("ID");
+
+ Types.NestedField second = view.schema().columns().get(1);
+ assertThat(second.name()).isEqualTo("new_data");
+ assertThat(second.doc()).isEqualTo("DATA");
+
+ assertThat(sql("SELECT new_id FROM %s", viewName))
+ .hasSize(3)
+ .containsExactlyInAnyOrder(row(1), row(2), row(3));
+
+ // query is updated to only contain id
+ sql("ALTER VIEW %s AS SELECT id from %s WHERE id > 3", viewName, tableName);
+
+ // querying new_id / new_data shouldn't work anymore
+ assertThatThrownBy(() -> sql("SELECT new_id FROM %s", viewName))
+ .isInstanceOf(AnalysisException.class)
+ .hasMessageContaining(
+ "A column or function parameter with name `new_id` cannot be resolved");
+
+ assertThatThrownBy(() -> sql("SELECT new_data FROM %s", viewName))
+ .isInstanceOf(AnalysisException.class)
+ .hasMessageContaining(
+ "A column or function parameter with name `new_data` cannot be resolved");
+
+ assertThat(sql("SELECT id FROM %s", viewName))
+ .hasSize(3)
+ .containsExactlyInAnyOrder(row(4), row(5), row(6));
+
+ view = viewCatalog().loadView(TableIdentifier.of(NAMESPACE, viewName));
+ assertThat(view.properties()).containsEntry("queryColumnNames", "id");
+
+ assertThat(view.schema().columns()).hasSize(1);
+ Types.NestedField field = view.schema().columns().get(0);
+ assertThat(field.name()).isEqualTo("id");
+ assertThat(field.doc()).isNull();
+ }
+
+ @Test
+ public void alterViewWithDuplicateQueryColumnNames() {
+ String viewName = viewName("viewWithDuplicateQueryColumnNames");
+ String sql = String.format("SELECT id, id FROM %s WHERE id <= 3", tableName);
+
+ sql("CREATE VIEW %s AS SELECT id FROM %s", viewName, tableName);
+
+ // having duplicate columns in the underlying query should fail
+ assertThatThrownBy(() -> sql("ALTER VIEW %s AS %s", viewName, sql))
+ .isInstanceOf(AnalysisException.class)
+ .hasMessageContaining("The column `id` already exists");
+ }
+
+ @Test
+ public void alterViewWithNonExistingTable() {
+ String viewName = "alteredViewUsingNonExistingTable";
+
+ sql("CREATE VIEW %s AS SELECT id, data FROM %s WHERE id <= 3", viewName, tableName);
+ assertThatThrownBy(() -> sql("ALTER VIEW %s AS SELECT id from non_existing", viewName))
+ .isInstanceOf(AnalysisException.class)
+ .hasMessageContaining("The table or view `non_existing` cannot be found");
+ }
+
+ @Test
+ public void alterViewWithNonExistingQueryColumn() {
+ String viewName = "alteredViewUsingNonExistingQueryColumn";
+
+ sql("CREATE VIEW %s AS SELECT id, data FROM %s WHERE id <= 3", viewName, tableName);
+ assertThatThrownBy(
+ () -> sql("ALTER VIEW %s AS SELECT non_existing from %s", viewName, tableName))
+ .isInstanceOf(AnalysisException.class)
+ .hasMessageContaining(
+ "A column or function parameter with name `non_existing` cannot be resolved");
+ }
+
+ /** The purpose of this test is mainly to make sure that altering normal view isn't messed up */
+ @Test
+ public void alterViewHiddenByTempView() throws NoSuchTableException {
+ insertRows(6);
+ String viewName = "alteredViewHiddenByTempView";
+ String sql = String.format("SELECT id, data FROM %s WHERE id > 3", tableName);
+
+ sql("CREATE TEMPORARY VIEW %s AS SELECT id FROM %s WHERE id <= 3", viewName, tableName);
+ sql("CREATE VIEW %s AS %s", viewName, sql);
+
+ ViewCatalog viewCatalog = viewCatalog();
+ View view = viewCatalog.loadView(TableIdentifier.of(NAMESPACE, viewName));
+ assertThat(view.sqlFor("spark").sql()).isEqualTo(sql);
+
+ // results from TEMP VIEW are returned
+ assertThat(sql("SELECT id FROM %s", viewName))
+ .hasSize(3)
+ .containsExactlyInAnyOrder(row(1), row(2), row(3));
+
+ // TEMP VIEW will be updated
+ sql("ALTER VIEW %s AS SELECT id FROM %s WHERE id >= 3 AND id < 4", viewName, tableName);
+
+ // results from TEMP VIEW are returned
+ assertThat(sql("SELECT * FROM %s", viewName)).hasSize(1).containsExactly(row(3));
+
+ // the only way to ALTER the Iceberg view would be to rename the TEMP VIEW and then do an ALTER
+ assertThat(view.sqlFor("spark").sql()).isEqualTo(sql);
+ }
+
+ @Test
+ public void alterViewWithCTE() throws NoSuchTableException {
+ insertRows(10);
+ String viewName = viewName("simpleViewWithCTE");
+ String sql =
+ String.format(
+ "WITH max_by_data AS (SELECT max(id) as max FROM %s) "
+ + "SELECT max, count(1) AS count FROM max_by_data GROUP BY max",
+ tableName);
+
+ sql("CREATE VIEW %s AS SELECT * FROM %s", viewName, tableName);
+ sql("ALTER VIEW %s AS %s", viewName, sql);
+
+ assertThat(sql("SELECT * FROM %s", viewName)).hasSize(1).containsExactly(row(10, 1L));
+ }
+
+ @Test
+ public void alterViewWithCTEReferencingTempView() {
+ String viewName = viewName("viewWithCTEReferencingTempView");
+ String tempViewInCTE = viewName("tempViewInCTE");
+ String sql =
+ String.format(
+ "WITH max_by_data AS (SELECT max(id) as max FROM %s) "
+ + "SELECT max, count(1) AS count FROM max_by_data GROUP BY max",
+ tempViewInCTE);
+
+ sql("CREATE TEMPORARY VIEW %s AS SELECT id FROM %s WHERE ID <= 5", tempViewInCTE, tableName);
+ sql("CREATE VIEW %s AS SELECT * FROM %s", viewName, tableName);
+
+ assertThatThrownBy(() -> sql("ALTER VIEW %s AS %s", viewName, sql))
+ .isInstanceOf(AnalysisException.class)
+ .hasMessageContaining("Cannot create the persistent object")
Review Comment:
the error message in Spark unfortunately doesn't distinguish between create/alter cases and throws that error for V1 views as well
--
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: issues-unsubscribe@iceberg.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: issues-unsubscribe@iceberg.apache.org
For additional commands, e-mail: issues-help@iceberg.apache.org
Re: [PR] Spark: Support altering views [iceberg]
Posted by "nastra (via GitHub)" <gi...@apache.org>.
nastra commented on code in PR #9510:
URL: https://github.com/apache/iceberg/pull/9510#discussion_r1469766873
##########
spark/v3.5/spark-extensions/src/main/scala/org/apache/spark/sql/catalyst/analysis/RewriteViewCommands.scala:
##########
@@ -60,17 +63,23 @@ case class RewriteViewCommands(spark: SparkSession) extends Rule[LogicalPlan] wi
properties = properties,
allowExisting = allowExisting,
replace = replace)
+
+ case a@AlterViewAs(ResolvedV2View(_, ident, _), _, query) =>
Review Comment:
if we want to go this route, then we'd also need to add the same handling for `SetViewProperties(UnresolvedView(...))` / `UnsetViewProperties(UnresolvedView(...))`, hence it seemed better to me to just handle `case u@UnresolvedView(ResolvedView(resolved), _, _, _)` separately.
I'm fine either way, so let me know if you'd still like me to switch to the approach you proposed.
--
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: issues-unsubscribe@iceberg.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: issues-unsubscribe@iceberg.apache.org
For additional commands, e-mail: issues-help@iceberg.apache.org
Re: [PR] Spark: Support altering views [iceberg]
Posted by "rdblue (via GitHub)" <gi...@apache.org>.
rdblue commented on code in PR #9510:
URL: https://github.com/apache/iceberg/pull/9510#discussion_r1468935466
##########
spark/v3.5/spark-extensions/src/main/scala/org/apache/spark/sql/catalyst/analysis/RewriteViewCommands.scala:
##########
@@ -123,4 +132,17 @@ case class RewriteViewCommands(spark: SparkSession) extends Rule[LogicalPlan] wi
collectTempViews(child)
}
+
+ private object IsIcebergView {
Review Comment:
Also, could this `unapply` method be added to `ResolvedView`?
--
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: issues-unsubscribe@iceberg.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: issues-unsubscribe@iceberg.apache.org
For additional commands, e-mail: issues-help@iceberg.apache.org
Re: [PR] Spark: Support altering views [iceberg]
Posted by "nastra (via GitHub)" <gi...@apache.org>.
nastra commented on code in PR #9510:
URL: https://github.com/apache/iceberg/pull/9510#discussion_r1469223182
##########
spark/v3.5/spark-extensions/src/main/scala/org/apache/spark/sql/catalyst/analysis/RewriteViewCommands.scala:
##########
@@ -123,4 +132,17 @@ case class RewriteViewCommands(spark: SparkSession) extends Rule[LogicalPlan] wi
collectTempViews(child)
}
+
+ private object IsIcebergView {
Review Comment:
I like those new names better. I've updated the existing one to `ResolvedIdent` as otherwise it clashes with the existing `ResolvedIdentifier`. Unfortunately both `unapply` methods can't be combined
--
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: issues-unsubscribe@iceberg.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: issues-unsubscribe@iceberg.apache.org
For additional commands, e-mail: issues-help@iceberg.apache.org
Re: [PR] Spark: Throw exception on `ALTER VIEW AS ` [iceberg]
Posted by "nastra (via GitHub)" <gi...@apache.org>.
nastra commented on code in PR #9510:
URL: https://github.com/apache/iceberg/pull/9510#discussion_r1473932334
##########
spark/v3.5/spark-extensions/src/main/scala/org/apache/spark/sql/catalyst/analysis/RewriteViewCommands.scala:
##########
@@ -60,6 +62,9 @@ case class RewriteViewCommands(spark: SparkSession) extends Rule[LogicalPlan] wi
properties = properties,
allowExisting = allowExisting,
replace = replace)
+
+ case AlterViewAs(ResolvedV2View(_, _), _, _) =>
+ throw new AnalysisException("ALTER VIEW <viewName> AS is not supported. Use CREATE OR REPLACE VIEW instead")
Review Comment:
I threw the error here mainly because when we supported `AlterViewAs`, most of the things had to happen in `RewriteViewCommands` but I've moved this now to `CheckViews`
--
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: issues-unsubscribe@iceberg.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: issues-unsubscribe@iceberg.apache.org
For additional commands, e-mail: issues-help@iceberg.apache.org
Re: [PR] Spark: Support altering views [iceberg]
Posted by "rdblue (via GitHub)" <gi...@apache.org>.
rdblue commented on code in PR #9510:
URL: https://github.com/apache/iceberg/pull/9510#discussion_r1468937534
##########
spark/v3.5/spark-extensions/src/main/scala/org/apache/spark/sql/execution/datasources/v2/ExtendedDataSourceV2Strategy.scala:
##########
@@ -123,6 +127,35 @@ case class ExtendedDataSourceV2Strategy(spark: SparkSession) extends Strategy wi
allowExisting = allowExisting,
replace = replace) :: Nil
+ case AlterViewAs(ResolvedV2View(catalog, ident, _), queryText, query) =>
+ CreateV2ViewExec(
+ catalog = catalog,
+ ident = ident,
+ queryText = queryText,
+ columnAliases = Seq.empty,
+ columnComments = Seq.empty,
+ queryColumnNames = query.schema.fieldNames,
+ viewSchema = query.schema,
+ comment = None,
+ properties = Map.empty,
+ allowExisting = false,
+ replace = true) :: Nil
+
+ case SetViewProperties(ResolvedV2View(catalog, ident, _), properties) =>
+ val changes = properties.map {
+ case (property, value) => ViewChange.setProperty(property, value)
+ }.toSeq
+ AlterV2ViewExec(catalog, ident, changes) :: Nil
+
+ case UnsetViewProperties(ResolvedV2View(catalog, ident, view), propertyKeys, ifExists) =>
+ if (!ifExists) {
+ propertyKeys.filterNot(view.properties.containsKey).foreach { property =>
+ throw new AnalysisException(s"Attempted to unset non-existing property '$property' in view $ident")
Review Comment:
We can handle this on the Iceberg side.
--
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: issues-unsubscribe@iceberg.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: issues-unsubscribe@iceberg.apache.org
For additional commands, e-mail: issues-help@iceberg.apache.org
Re: [PR] Spark: Throw exception on `ALTER VIEW AS ` [iceberg]
Posted by "rdblue (via GitHub)" <gi...@apache.org>.
rdblue commented on code in PR #9510:
URL: https://github.com/apache/iceberg/pull/9510#discussion_r1471786657
##########
spark/v3.5/spark-extensions/src/main/scala/org/apache/spark/sql/catalyst/analysis/RewriteViewCommands.scala:
##########
@@ -60,6 +62,9 @@ case class RewriteViewCommands(spark: SparkSession) extends Rule[LogicalPlan] wi
properties = properties,
allowExisting = allowExisting,
replace = replace)
+
+ case AlterViewAs(ResolvedV2View(_, _), _, _) =>
+ throw new AnalysisException("ALTER VIEW <viewName> AS is not supported. Use CREATE OR REPLACE VIEW instead")
Review Comment:
Does this need to happen here instead of in `CheckViews` so that Spark doesn't throw a different analysis exception?
--
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: issues-unsubscribe@iceberg.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: issues-unsubscribe@iceberg.apache.org
For additional commands, e-mail: issues-help@iceberg.apache.org
Re: [PR] Spark: Support altering views [iceberg]
Posted by "nastra (via GitHub)" <gi...@apache.org>.
nastra commented on code in PR #9510:
URL: https://github.com/apache/iceberg/pull/9510#discussion_r1467911499
##########
spark/v3.5/spark-extensions/src/main/scala/org/apache/spark/sql/catalyst/analysis/CheckViews.scala:
##########
@@ -36,6 +38,9 @@ object CheckViews extends (LogicalPlan => Unit) {
verifyColumnCount(ident, columnAliases, query)
SchemaUtils.checkColumnNameDuplication(query.schema.fieldNames, SQLConf.get.resolver)
+ case AlterViewAs(ResolvedV2View(_, _, _), _, query) =>
+ SchemaUtils.checkColumnNameDuplication(query.schema.fieldNames, SQLConf.get.resolver)
Review Comment:
we don't have to verify column counts here because ALTER doesn't support specifying column aliases
--
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: issues-unsubscribe@iceberg.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: issues-unsubscribe@iceberg.apache.org
For additional commands, e-mail: issues-help@iceberg.apache.org
Re: [PR] Spark: Support altering views [iceberg]
Posted by "nastra (via GitHub)" <gi...@apache.org>.
nastra commented on PR #9510:
URL: https://github.com/apache/iceberg/pull/9510#issuecomment-1914349245
@rdblue I have extracted setting/unsetting view properties into https://github.com/apache/iceberg/pull/9582
--
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: issues-unsubscribe@iceberg.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: issues-unsubscribe@iceberg.apache.org
For additional commands, e-mail: issues-help@iceberg.apache.org
Re: [PR] Spark: Support altering views [iceberg]
Posted by "rdblue (via GitHub)" <gi...@apache.org>.
rdblue commented on code in PR #9510:
URL: https://github.com/apache/iceberg/pull/9510#discussion_r1468936762
##########
spark/v3.5/spark-extensions/src/main/scala/org/apache/spark/sql/catalyst/analysis/RewriteViewCommands.scala:
##########
@@ -60,17 +63,23 @@ case class RewriteViewCommands(spark: SparkSession) extends Rule[LogicalPlan] wi
properties = properties,
allowExisting = allowExisting,
replace = replace)
+
+ case a@AlterViewAs(ResolvedV2View(_, ident, _), _, query) =>
Review Comment:
I don't like that this depends on the next case running before this one. I think that I would combine the two.
I think this should be:
```scala
case a@AlterViewAs(ResolvedView(resolved), _, query) =>
val q = CTESubstitution.apply(query)
verifyTemporaryObjectsDontExist(ident, q)
a.copy(child=resolved, query = q)
```
--
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: issues-unsubscribe@iceberg.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: issues-unsubscribe@iceberg.apache.org
For additional commands, e-mail: issues-help@iceberg.apache.org
Re: [PR] Spark: Support altering views [iceberg]
Posted by "nastra (via GitHub)" <gi...@apache.org>.
nastra commented on code in PR #9510:
URL: https://github.com/apache/iceberg/pull/9510#discussion_r1469684751
##########
spark/v3.5/spark-extensions/src/main/scala/org/apache/spark/sql/catalyst/analysis/CheckViews.scala:
##########
@@ -36,6 +38,9 @@ object CheckViews extends (LogicalPlan => Unit) {
verifyColumnCount(ident, columnAliases, query)
SchemaUtils.checkColumnNameDuplication(query.schema.fieldNames, SQLConf.get.resolver)
+ case AlterViewAs(ResolvedV2View(_, _, _), _, query) =>
+ SchemaUtils.checkColumnNameDuplication(query.schema.fieldNames, SQLConf.get.resolver)
Review Comment:
I agree that this is a weird case and I also had a different expectation when initially running into this. That's why I added the `alterViewWithUpdatedQueryColumns()` test to make this behavior explicit.
I also looking into how V1 views behave here and they do the same thing (aka losing the column comments from the original CREATE):
```
spark-sql (default)> create view iceberg1.v10 (x COMMENT 'comment 1', y COMMENT 'comment 2') AS SELECT count(id), zip from iceberg1.foo group by zip;
spark-sql (default)> show create table iceberg1.v10;
CREATE VIEW iceberg1.v10 (
x COMMENT 'comment 1',
y COMMENT 'comment 2')
TBLPROPERTIES (
'transient_lastDdlTime' = '1706538575')
AS SELECT count(id) AS cnt, zip from iceberg1.foo group by zip
Time taken: 0.055 seconds, Fetched 1 row(s)
spark-sql (default)> describe extended iceberg1.v10;
x bigint comment 1
y int comment 2
# Detailed Table Information
Catalog spark_catalog
Database iceberg1
Table v10
Owner nastra
Created Time Mon Jan 29 15:29:35 CET 2024
Last Access UNKNOWN
Created By Spark 3.5.0
Type VIEW
View Text SELECT count(id) AS cnt, zip from iceberg1.foo group by zip
View Original Text SELECT count(id) AS cnt, zip from iceberg1.foo group by zip
View Catalog and Namespace spark_catalog.default
View Query Output Columns [cnt, zip]
Table Properties [transient_lastDdlTime=1706538575]
Serde Library org.apache.hadoop.hive.serde2.lazy.LazySimpleSerDe
InputFormat org.apache.hadoop.mapred.SequenceFileInputFormat
OutputFormat org.apache.hadoop.hive.ql.io.HiveSequenceFileOutputFormat
Storage Properties [serialization.format=1]
Time taken: 0.063 seconds, Fetched 21 row(s)
```
```
spark-sql (default)> alter view iceberg1.v10 AS SELECT count(zip) AS cnt, zip from iceberg1.foo group by zip;
Time taken: 0.213 seconds
spark-sql (default)> show create table iceberg1.v10;
CREATE VIEW iceberg1.v10 (
cnt,
zip)
TBLPROPERTIES (
'transient_lastDdlTime' = '1706538634')
AS SELECT count(zip) AS cnt, zip from iceberg1.foo group by zip
Time taken: 0.029 seconds, Fetched 1 row(s)
spark-sql (default)> describe extended iceberg1.v10;
cnt bigint
zip int
# Detailed Table Information
Catalog spark_catalog
Database iceberg1
Table v10
Owner nastra
Created Time Mon Jan 29 15:29:35 CET 2024
Last Access UNKNOWN
Created By Spark 3.5.0
Type VIEW
View Text SELECT count(zip) AS cnt, zip from iceberg1.foo group by zip
View Original Text SELECT count(zip) AS cnt, zip from iceberg1.foo group by zip
View Catalog and Namespace spark_catalog.default
View Query Output Columns [cnt, zip]
Table Properties [transient_lastDdlTime=1706538634]
Serde Library org.apache.hadoop.hive.serde2.lazy.LazySimpleSerDe
InputFormat org.apache.hadoop.mapred.SequenceFileInputFormat
OutputFormat org.apache.hadoop.hive.ql.io.HiveSequenceFileOutputFormat
Storage Properties [serialization.format=1]
Time taken: 0.06 seconds, Fetched 21 row(s)
```
--
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: issues-unsubscribe@iceberg.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: issues-unsubscribe@iceberg.apache.org
For additional commands, e-mail: issues-help@iceberg.apache.org
Re: [PR] Spark: Support altering views [iceberg]
Posted by "nastra (via GitHub)" <gi...@apache.org>.
nastra commented on code in PR #9510:
URL: https://github.com/apache/iceberg/pull/9510#discussion_r1469223182
##########
spark/v3.5/spark-extensions/src/main/scala/org/apache/spark/sql/catalyst/analysis/RewriteViewCommands.scala:
##########
@@ -123,4 +132,17 @@ case class RewriteViewCommands(spark: SparkSession) extends Rule[LogicalPlan] wi
collectTempViews(child)
}
+
+ private object IsIcebergView {
Review Comment:
I like those new names better. I've updated the existing one to `ResolvedIdent` as otherwise it clashes with the existing `ResolvedIdentifier`. Unfortunately both `unapply` methods can't be combined into `ResolvedView`
--
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: issues-unsubscribe@iceberg.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: issues-unsubscribe@iceberg.apache.org
For additional commands, e-mail: issues-help@iceberg.apache.org
Re: [PR] Spark: Support altering views [iceberg]
Posted by "rdblue (via GitHub)" <gi...@apache.org>.
rdblue commented on code in PR #9510:
URL: https://github.com/apache/iceberg/pull/9510#discussion_r1468933314
##########
spark/v3.5/spark-extensions/src/main/scala/org/apache/spark/sql/catalyst/analysis/CheckViews.scala:
##########
@@ -36,6 +38,9 @@ object CheckViews extends (LogicalPlan => Unit) {
verifyColumnCount(ident, columnAliases, query)
SchemaUtils.checkColumnNameDuplication(query.schema.fieldNames, SQLConf.get.resolver)
+ case AlterViewAs(ResolvedV2View(_, _, _), _, query) =>
+ SchemaUtils.checkColumnNameDuplication(query.schema.fieldNames, SQLConf.get.resolver)
Review Comment:
This worries me. Are the aliases from the original create expected to be applied to the new view version? I don't think that the expected behavior is for the column comments and aliases to go away.
For example, if I ran these two statements, what should happen?
```sql
-- original create command
CREATE VIEW daily_signups (
signup_count COMMENT 'Number of signups',
day COMMENT 'Signup date') AS
SELECT count(1), day FROM signups GROUP BY day
-- updated view with DISTINCT to deduplicate
ALTER VIEW daily_signups AS
SELECT count(distinct email), day FROM signups GROUP BY day
```
To me, it seems like this should work and result in the same view schema, with `signup_count` as the first column and the column docs preserved. That probably means that we need to store the column names and comments separately after all, or at least flag that they were set somewhere (view properties?).
Also, it looks like the indentation is inconsistent between this and other cases.
--
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: issues-unsubscribe@iceberg.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: issues-unsubscribe@iceberg.apache.org
For additional commands, e-mail: issues-help@iceberg.apache.org
Re: [PR] Spark: Support altering views [iceberg]
Posted by "rdblue (via GitHub)" <gi...@apache.org>.
rdblue commented on code in PR #9510:
URL: https://github.com/apache/iceberg/pull/9510#discussion_r1468936985
##########
spark/v3.5/spark-extensions/src/main/scala/org/apache/spark/sql/execution/datasources/v2/ExtendedDataSourceV2Strategy.scala:
##########
@@ -123,6 +127,35 @@ case class ExtendedDataSourceV2Strategy(spark: SparkSession) extends Strategy wi
allowExisting = allowExisting,
replace = replace) :: Nil
+ case AlterViewAs(ResolvedV2View(catalog, ident, _), queryText, query) =>
+ CreateV2ViewExec(
+ catalog = catalog,
+ ident = ident,
+ queryText = queryText,
+ columnAliases = Seq.empty,
+ columnComments = Seq.empty,
+ queryColumnNames = query.schema.fieldNames,
+ viewSchema = query.schema,
+ comment = None,
+ properties = Map.empty,
+ allowExisting = false,
+ replace = true) :: Nil
+
+ case SetViewProperties(ResolvedV2View(catalog, ident, _), properties) =>
+ val changes = properties.map {
+ case (property, value) => ViewChange.setProperty(property, value)
+ }.toSeq
+ AlterV2ViewExec(catalog, ident, changes) :: Nil
+
+ case UnsetViewProperties(ResolvedV2View(catalog, ident, view), propertyKeys, ifExists) =>
+ if (!ifExists) {
+ propertyKeys.filterNot(view.properties.containsKey).foreach { property =>
+ throw new AnalysisException(s"Attempted to unset non-existing property '$property' in view $ident")
Review Comment:
This should be done in execution, not in the conversion to a physical plan.
--
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: issues-unsubscribe@iceberg.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: issues-unsubscribe@iceberg.apache.org
For additional commands, e-mail: issues-help@iceberg.apache.org
Re: [PR] Spark: Support altering views [iceberg]
Posted by "rdblue (via GitHub)" <gi...@apache.org>.
rdblue commented on code in PR #9510:
URL: https://github.com/apache/iceberg/pull/9510#discussion_r1468937382
##########
spark/v3.5/spark-extensions/src/main/scala/org/apache/spark/sql/catalyst/plans/logical/views/ResolvedV2View.scala:
##########
@@ -22,10 +22,12 @@ 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.View
import org.apache.spark.sql.connector.catalog.ViewCatalog
case class ResolvedV2View(
catalog: ViewCatalog,
- identifier: Identifier) extends LeafNodeWithoutStats {
+ identifier: Identifier,
+ view: View) extends LeafNodeWithoutStats {
Review Comment:
I'm not a fan of keeping the view around like this. It's only used in one case where it shouldn't be because the check should be done in query execution rather than in job planning. I don't think this change is needed so I'd roll it back.
--
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: issues-unsubscribe@iceberg.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: issues-unsubscribe@iceberg.apache.org
For additional commands, e-mail: issues-help@iceberg.apache.org
Re: [PR] Spark: Throw exception on `ALTER VIEW AS ` [iceberg]
Posted by "nastra (via GitHub)" <gi...@apache.org>.
nastra merged PR #9510:
URL: https://github.com/apache/iceberg/pull/9510
--
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: issues-unsubscribe@iceberg.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: issues-unsubscribe@iceberg.apache.org
For additional commands, e-mail: issues-help@iceberg.apache.org
Re: [PR] Spark: Throw exception on `ALTER VIEW AS ` [iceberg]
Posted by "nastra (via GitHub)" <gi...@apache.org>.
nastra commented on code in PR #9510:
URL: https://github.com/apache/iceberg/pull/9510#discussion_r1471696587
##########
spark/v3.5/spark-extensions/src/main/scala/org/apache/spark/sql/catalyst/analysis/CheckViews.scala:
##########
@@ -36,6 +38,9 @@ object CheckViews extends (LogicalPlan => Unit) {
verifyColumnCount(ident, columnAliases, query)
SchemaUtils.checkColumnNameDuplication(query.schema.fieldNames, SQLConf.get.resolver)
+ case AlterViewAs(ResolvedV2View(_, _, _), _, query) =>
+ SchemaUtils.checkColumnNameDuplication(query.schema.fieldNames, SQLConf.get.resolver)
Review Comment:
To summarize this topic, we discussed this offline and decided that we should not support `ALTER VIEW ... AS` for now and rather throw an exception (indicating to use `CREATE OR REPLACE VIEW`), as it leads to surprising behaviors that column aliases/comments are lost after updating the underlying SQL.
--
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: issues-unsubscribe@iceberg.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: issues-unsubscribe@iceberg.apache.org
For additional commands, e-mail: issues-help@iceberg.apache.org
Re: [PR] Spark: Support altering views [iceberg]
Posted by "nastra (via GitHub)" <gi...@apache.org>.
nastra commented on code in PR #9510:
URL: https://github.com/apache/iceberg/pull/9510#discussion_r1467911970
##########
spark/v3.5/spark-extensions/src/main/scala/org/apache/spark/sql/catalyst/analysis/ResolveViews.scala:
##########
@@ -87,15 +86,6 @@ case class ResolveViews(spark: SparkSession) extends Rule[LogicalPlan] with Look
}
}
- def loadView(catalog: CatalogPlugin, ident: Identifier): Option[View] = catalog match {
Review Comment:
some refactoring so that the same functionality can be used from different places
--
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: issues-unsubscribe@iceberg.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: issues-unsubscribe@iceberg.apache.org
For additional commands, e-mail: issues-help@iceberg.apache.org
Re: [PR] Spark: Support altering views [iceberg]
Posted by "nastra (via GitHub)" <gi...@apache.org>.
nastra commented on code in PR #9510:
URL: https://github.com/apache/iceberg/pull/9510#discussion_r1467933159
##########
spark/v3.5/spark-extensions/src/test/java/org/apache/iceberg/spark/extensions/TestViews.java:
##########
@@ -1149,10 +1148,343 @@ public void createViewWithSubqueryExpressionUsingGlobalTempView() {
"because it references to the temporary object global_temp.%s", globalTempView));
}
+ @Test
+ public void alterView() throws NoSuchTableException {
+ insertRows(6);
+ String viewName = "alteredView";
+
+ sql("CREATE VIEW %s AS SELECT id, data FROM %s WHERE id <= 3", viewName, tableName);
+
+ assertThat(sql("SELECT id FROM %s", viewName))
+ .hasSize(3)
+ .containsExactlyInAnyOrder(row(1), row(2), row(3));
+
+ sql("ALTER VIEW %s AS SELECT id FROM %s WHERE id > 3", viewName, tableName);
+
+ assertThat(sql("SELECT * FROM %s", viewName))
+ .hasSize(3)
+ .containsExactlyInAnyOrder(row(4), row(5), row(6));
+ }
+
+ @Test
+ public void alterNonExistingView() {
+ // this is a bit tricky because the view can't be found in the view catalog and
+ // resolution bubbles up to check whether it's a V1 view but then fails, indicating that the
+ // catalog doesn't support views
+ assertThatThrownBy(
+ () -> sql("ALTER VIEW non_existing AS SELECT id FROM %s WHERE id > 3", tableName))
+ .isInstanceOf(AnalysisException.class)
+ .hasMessageContaining(
+ String.format(
+ "The feature is not supported: Catalog `%s` does not support views", catalogName));
+ }
+
+ @Test
+ public void alterViewWithInvalidSQL() {
+ String viewName = "alteredViewWithInvalidSQL";
+
+ sql("CREATE VIEW %s AS SELECT id, data FROM %s WHERE id <= 3", viewName, tableName);
+ assertThatThrownBy(() -> sql("ALTER VIEW %s AS invalid SQL", viewName, tableName))
+ .isInstanceOf(AnalysisException.class)
+ .hasMessageContaining("Syntax error");
+ }
+
+ @Test
+ public void alterViewReferencingTempView() throws NoSuchTableException {
+ insertRows(10);
+ String tempView = "temporaryViewBeingReferencedInView";
+ String viewReferencingTempView = "alteredViewReferencingTemporaryView";
+
+ sql("CREATE TEMPORARY VIEW %s AS SELECT id FROM %s WHERE id <= 5", tempView, tableName);
+ sql("CREATE VIEW %s AS SELECT id FROM %s WHERE id <= 5", viewReferencingTempView, tableName);
+
+ // altering a view that references a TEMP VIEW shouldn't be possible
+ assertThatThrownBy(
+ () -> sql("ALTER VIEW %s AS SELECT id FROM %s", viewReferencingTempView, tempView))
+ .isInstanceOf(AnalysisException.class)
+ .hasMessageContaining("Cannot create the persistent object")
+ .hasMessageContaining(viewReferencingTempView)
+ .hasMessageContaining("of the type VIEW because it references to the temporary object")
+ .hasMessageContaining(tempView);
+ }
+
+ @Test
+ public void alterViewReferencingGlobalTempView() throws NoSuchTableException {
+ insertRows(10);
+ String globalTempView = "globalTemporaryViewBeingReferencedInView";
+ String viewReferencingTempView = "alteredViewReferencingGlobalTemporaryView";
+
+ sql(
+ "CREATE GLOBAL TEMPORARY VIEW %s AS SELECT id FROM %s WHERE id <= 5",
+ globalTempView, tableName);
+
+ sql("CREATE VIEW %s AS SELECT id FROM %s WHERE id <= 5", viewReferencingTempView, tableName);
+
+ // altering a view that references a GLOBAL TEMP VIEW shouldn't be possible
+ assertThatThrownBy(
+ () ->
+ sql(
+ "ALTER VIEW %s AS SELECT id FROM global_temp.%s",
+ viewReferencingTempView, globalTempView))
+ .isInstanceOf(AnalysisException.class)
+ .hasMessageContaining("Cannot create the persistent object")
+ .hasMessageContaining(viewReferencingTempView)
+ .hasMessageContaining("of the type VIEW because it references to the temporary object")
+ .hasMessageContaining(globalTempView);
+ }
+
+ @Test
+ public void alterViewWithUpdatedQueryColumns() throws NoSuchTableException {
+ insertRows(6);
+ String viewName = viewName("viewWithQueryColumns");
+
+ sql(
+ "CREATE VIEW %s (new_id COMMENT 'ID', new_data COMMENT 'DATA') AS SELECT id, data FROM %s WHERE id <= 3",
+ viewName, tableName);
+
+ View view = viewCatalog().loadView(TableIdentifier.of(NAMESPACE, viewName));
+ assertThat(view.properties()).containsEntry("queryColumnNames", "id,data");
+
+ assertThat(view.schema().columns()).hasSize(2);
+ Types.NestedField first = view.schema().columns().get(0);
+ assertThat(first.name()).isEqualTo("new_id");
+ assertThat(first.doc()).isEqualTo("ID");
+
+ Types.NestedField second = view.schema().columns().get(1);
+ assertThat(second.name()).isEqualTo("new_data");
+ assertThat(second.doc()).isEqualTo("DATA");
+
+ assertThat(sql("SELECT new_id FROM %s", viewName))
+ .hasSize(3)
+ .containsExactlyInAnyOrder(row(1), row(2), row(3));
+
+ // query is updated to only contain id
+ sql("ALTER VIEW %s AS SELECT id from %s WHERE id > 3", viewName, tableName);
+
+ // querying new_id / new_data shouldn't work anymore
+ assertThatThrownBy(() -> sql("SELECT new_id FROM %s", viewName))
+ .isInstanceOf(AnalysisException.class)
+ .hasMessageContaining(
+ "A column or function parameter with name `new_id` cannot be resolved");
+
+ assertThatThrownBy(() -> sql("SELECT new_data FROM %s", viewName))
+ .isInstanceOf(AnalysisException.class)
+ .hasMessageContaining(
+ "A column or function parameter with name `new_data` cannot be resolved");
+
+ assertThat(sql("SELECT id FROM %s", viewName))
+ .hasSize(3)
+ .containsExactlyInAnyOrder(row(4), row(5), row(6));
+
+ view = viewCatalog().loadView(TableIdentifier.of(NAMESPACE, viewName));
+ assertThat(view.properties()).containsEntry("queryColumnNames", "id");
+
+ assertThat(view.schema().columns()).hasSize(1);
+ Types.NestedField field = view.schema().columns().get(0);
+ assertThat(field.name()).isEqualTo("id");
+ assertThat(field.doc()).isNull();
+ }
+
+ @Test
+ public void alterViewWithDuplicateQueryColumnNames() {
+ String viewName = viewName("viewWithDuplicateQueryColumnNames");
+ sql("CREATE VIEW %s AS SELECT id FROM %s", viewName, tableName);
+
+ // having duplicate columns in the underlying query should fail
+ assertThatThrownBy(
+ () -> sql("ALTER VIEW %s AS SELECT id, id FROM %s WHERE id <= 3", viewName, tableName))
+ .isInstanceOf(AnalysisException.class)
+ .hasMessageContaining("The column `id` already exists");
+ }
+
+ @Test
+ public void alterViewWithNonExistingTable() {
+ String viewName = "alteredViewUsingNonExistingTable";
+
+ sql("CREATE VIEW %s AS SELECT id, data FROM %s WHERE id <= 3", viewName, tableName);
+ assertThatThrownBy(() -> sql("ALTER VIEW %s AS SELECT id from non_existing", viewName))
+ .isInstanceOf(AnalysisException.class)
+ .hasMessageContaining("The table or view `non_existing` cannot be found");
+ }
+
+ @Test
+ public void alterViewWithNonExistingQueryColumn() {
+ String viewName = "alteredViewUsingNonExistingQueryColumn";
+
+ sql("CREATE VIEW %s AS SELECT id, data FROM %s WHERE id <= 3", viewName, tableName);
+ assertThatThrownBy(
+ () -> sql("ALTER VIEW %s AS SELECT non_existing from %s", viewName, tableName))
+ .isInstanceOf(AnalysisException.class)
+ .hasMessageContaining(
+ "A column or function parameter with name `non_existing` cannot be resolved");
+ }
+
+ /** The purpose of this test is mainly to make sure that altering normal view isn't messed up */
+ @Test
+ public void alterViewHiddenByTempView() throws NoSuchTableException {
+ insertRows(6);
+ String viewName = "alteredViewHiddenByTempView";
+ String sql = String.format("SELECT id, data FROM %s WHERE id > 3", tableName);
+
+ sql("CREATE TEMPORARY VIEW %s AS SELECT id FROM %s WHERE id <= 3", viewName, tableName);
+ sql("CREATE VIEW %s AS %s", viewName, sql);
+
+ ViewCatalog viewCatalog = viewCatalog();
+ View view = viewCatalog.loadView(TableIdentifier.of(NAMESPACE, viewName));
+ assertThat(view.sqlFor("spark").sql()).isEqualTo(sql);
+
+ // results from TEMP VIEW are returned
+ assertThat(sql("SELECT id FROM %s", viewName))
+ .hasSize(3)
+ .containsExactlyInAnyOrder(row(1), row(2), row(3));
+
+ // TEMP VIEW will be updated
+ sql("ALTER VIEW %s AS SELECT id FROM %s WHERE id >= 3 AND id < 4", viewName, tableName);
+
+ // results from TEMP VIEW are returned
+ assertThat(sql("SELECT * FROM %s", viewName)).hasSize(1).containsExactly(row(3));
+
+ // the only way to ALTER the Iceberg view would be to rename the TEMP VIEW and then do an ALTER
+ assertThat(view.sqlFor("spark").sql()).isEqualTo(sql);
+ }
+
+ @Test
+ public void alterViewWithCTE() throws NoSuchTableException {
+ insertRows(10);
+ String viewName = viewName("simpleViewWithCTE");
+ String sql =
+ String.format(
+ "WITH max_by_data AS (SELECT max(id) as max FROM %s) "
+ + "SELECT max, count(1) AS count FROM max_by_data GROUP BY max",
+ tableName);
+
+ sql("CREATE VIEW %s AS SELECT * FROM %s", viewName, tableName);
+ sql("ALTER VIEW %s AS %s", viewName, sql);
+
+ assertThat(sql("SELECT * FROM %s", viewName)).hasSize(1).containsExactly(row(10, 1L));
+ }
+
+ @Test
+ public void alterViewWithCTEReferencingTempView() {
+ String viewName = viewName("viewWithCTEReferencingTempView");
+ String tempViewInCTE = viewName("tempViewInCTE");
+ String sql =
+ String.format(
+ "WITH max_by_data AS (SELECT max(id) as max FROM %s) "
+ + "SELECT max, count(1) AS count FROM max_by_data GROUP BY max",
+ tempViewInCTE);
+
+ sql("CREATE TEMPORARY VIEW %s AS SELECT id FROM %s WHERE ID <= 5", tempViewInCTE, tableName);
+ sql("CREATE VIEW %s AS SELECT * FROM %s", viewName, tableName);
+
+ assertThatThrownBy(() -> sql("ALTER VIEW %s AS %s", viewName, sql))
+ .isInstanceOf(AnalysisException.class)
+ .hasMessageContaining("Cannot create the persistent object")
+ .hasMessageContaining(viewName)
+ .hasMessageContaining("of the type VIEW because it references to the temporary object")
+ .hasMessageContaining(tempViewInCTE);
+ }
+
+ @Test
+ public void alterViewWithSubqueryExpressionUsingTempView() {
+ String viewName = viewName("viewWithSubqueryExpression");
+ String tempView = viewName("simpleTempView");
+ String sql =
+ String.format("SELECT * FROM %s WHERE id = (SELECT id FROM %s)", tableName, tempView);
+
+ sql("CREATE TEMPORARY VIEW %s AS SELECT id from %s WHERE id = 5", tempView, tableName);
+ sql("CREATE VIEW %s AS SELECT * from %s", viewName, tableName);
+
+ assertThatThrownBy(() -> sql("ALTER VIEW %s AS %s", viewName, sql))
+ .isInstanceOf(AnalysisException.class)
+ .hasMessageContaining(String.format("Cannot create the persistent object %s", viewName))
+ .hasMessageContaining(
+ String.format("because it references to the temporary object %s", tempView));
+ }
+
+ @Test
+ public void alterViewWithSubqueryExpressionUsingGlobalTempView() {
+ String viewName = viewName("simpleViewWithSubqueryExpression");
+ String globalTempView = viewName("simpleGlobalTempView");
+ String sql =
+ String.format(
+ "SELECT * FROM %s WHERE id = (SELECT id FROM global_temp.%s)",
+ tableName, globalTempView);
+
+ sql(
+ "CREATE GLOBAL TEMPORARY VIEW %s AS SELECT id from %s WHERE id = 5",
+ globalTempView, tableName);
+ sql("CREATE VIEW %s AS SELECT * from %s", viewName, tableName);
+
+ assertThatThrownBy(() -> sql("ALTER VIEW %s AS %s", viewName, sql))
+ .isInstanceOf(AnalysisException.class)
+ .hasMessageContaining(String.format("Cannot create the persistent object %s", viewName))
+ .hasMessageContaining(
+ String.format(
+ "because it references to the temporary object global_temp.%s", globalTempView));
+ }
+
+ @Test
+ public void alterViewSetProperties() {
+ String viewName = "viewWithSetProperties";
+
+ sql("CREATE VIEW %s AS SELECT id FROM %s WHERE id <= 3", viewName, tableName);
+
+ ViewCatalog viewCatalog = viewCatalog();
+ assertThat(viewCatalog.loadView(TableIdentifier.of(NAMESPACE, viewName)).properties())
+ .doesNotContainKey("key1")
+ .doesNotContainKey("comment");
+
+ sql("ALTER VIEW %s SET TBLPROPERTIES ('key1' = 'val1', 'comment' = 'view comment')", viewName);
+ assertThat(viewCatalog.loadView(TableIdentifier.of(NAMESPACE, viewName)).properties())
+ .containsEntry("key1", "val1")
+ .containsEntry("comment", "view comment");
+
+ sql("ALTER VIEW %s SET TBLPROPERTIES ('key1' = 'new_val1')", viewName);
+ assertThat(viewCatalog.loadView(TableIdentifier.of(NAMESPACE, viewName)).properties())
+ .containsEntry("key1", "new_val1")
+ .containsEntry("comment", "view comment");
+ }
+
+ @Test
+ public void alterViewUnsetProperties() {
+ String viewName = "viewWithUnsetProperties";
+ sql("CREATE VIEW %s AS SELECT id FROM %s WHERE id <= 3", viewName, tableName);
+
+ ViewCatalog viewCatalog = viewCatalog();
+ assertThat(viewCatalog.loadView(TableIdentifier.of(NAMESPACE, viewName)).properties())
+ .doesNotContainKey("key1")
+ .doesNotContainKey("comment");
+
+ sql("ALTER VIEW %s SET TBLPROPERTIES ('key1' = 'val1', 'comment' = 'view comment')", viewName);
+ assertThat(viewCatalog.loadView(TableIdentifier.of(NAMESPACE, viewName)).properties())
+ .containsEntry("key1", "val1")
+ .containsEntry("comment", "view comment");
+
+ sql("ALTER VIEW %s UNSET TBLPROPERTIES ('key1')", viewName);
+ assertThat(viewCatalog.loadView(TableIdentifier.of(NAMESPACE, viewName)).properties())
+ .doesNotContainKey("key1")
+ .containsEntry("comment", "view comment");
+ }
+
+ @Test
+ public void alterViewUnsetUnknownProperty() {
+ String viewName = "viewWithUnsetUnknownProp";
+ sql("CREATE VIEW %s AS SELECT id FROM %s WHERE id <= 3", viewName, tableName);
+
+ assertThatThrownBy(() -> sql("ALTER VIEW %s UNSET TBLPROPERTIES ('unknown-key')", viewName))
+ .isInstanceOf(AnalysisException.class)
+ .hasMessageContaining("Attempted to unset non-existing property 'unknown-key'");
+
+ assertThatNoException()
+ .isThrownBy(
+ () -> sql("ALTER VIEW %s UNSET TBLPROPERTIES IF EXISTS ('unknown-key')", viewName));
+ }
+
private void insertRows(int numRows) throws NoSuchTableException {
List<SimpleRecord> records = Lists.newArrayListWithCapacity(numRows);
for (int i = 1; i <= numRows; i++) {
- records.add(new SimpleRecord(i, UUID.randomUUID().toString()));
+ records.add(new SimpleRecord(i, Integer.toString(i * 2)));
Review Comment:
makes the results more predictable in case we query it
--
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: issues-unsubscribe@iceberg.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: issues-unsubscribe@iceberg.apache.org
For additional commands, e-mail: issues-help@iceberg.apache.org
Re: [PR] Spark: Support altering views [iceberg]
Posted by "rdblue (via GitHub)" <gi...@apache.org>.
rdblue commented on code in PR #9510:
URL: https://github.com/apache/iceberg/pull/9510#discussion_r1468933314
##########
spark/v3.5/spark-extensions/src/main/scala/org/apache/spark/sql/catalyst/analysis/CheckViews.scala:
##########
@@ -36,6 +38,9 @@ object CheckViews extends (LogicalPlan => Unit) {
verifyColumnCount(ident, columnAliases, query)
SchemaUtils.checkColumnNameDuplication(query.schema.fieldNames, SQLConf.get.resolver)
+ case AlterViewAs(ResolvedV2View(_, _, _), _, query) =>
+ SchemaUtils.checkColumnNameDuplication(query.schema.fieldNames, SQLConf.get.resolver)
Review Comment:
Looks like the indentation is inconsistent between this and other cases.
--
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: issues-unsubscribe@iceberg.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: issues-unsubscribe@iceberg.apache.org
For additional commands, e-mail: issues-help@iceberg.apache.org
Re: [PR] Spark: Support altering views [iceberg]
Posted by "rdblue (via GitHub)" <gi...@apache.org>.
rdblue commented on code in PR #9510:
URL: https://github.com/apache/iceberg/pull/9510#discussion_r1468935203
##########
spark/v3.5/spark-extensions/src/main/scala/org/apache/spark/sql/catalyst/analysis/RewriteViewCommands.scala:
##########
@@ -123,4 +132,17 @@ case class RewriteViewCommands(spark: SparkSession) extends Rule[LogicalPlan] wi
collectTempViews(child)
}
+
+ private object IsIcebergView {
Review Comment:
The `Is` for this name doesn't make sense because it doesn't produce a boolean. It produces a loaded v2 view. I don't even think that the v2 view needs to be an Iceberg view.
I think this should probably be renamed to `ResolvedView` and the other should be named `ResolvedIdentifier` or something. I also don't like the duplication across these two helpers. It seems like this is doing what the other rule does, but should instead be delegating to other rules.
--
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: issues-unsubscribe@iceberg.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: issues-unsubscribe@iceberg.apache.org
For additional commands, e-mail: issues-help@iceberg.apache.org