This is an automated email from the ASF dual-hosted git repository.
blue pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/iceberg.git
The following commit(s) were added to refs/heads/main by this push:
new 212355e13b Core: Add strictness flag to prevent loss of view
representation when replacing a view (#9620)
212355e13b is described below
commit 212355e13b3a8c40441725a260ae6e69bb1a0a9e
Author: Eduard Tudenhoefner <[email protected]>
AuthorDate: Thu Feb 15 20:18:48 2024 +0100
Core: Add strictness flag to prevent loss of view representation when
replacing a view (#9620)
---
.../java/org/apache/iceberg/view/ViewMetadata.java | 39 ++-
.../org/apache/iceberg/view/ViewProperties.java | 2 +
.../org/apache/iceberg/view/TestViewMetadata.java | 302 ++++++++++++++++++++-
.../org/apache/iceberg/view/ViewCatalogTests.java | 5 +-
.../org/apache/iceberg/nessie/BaseTestIceberg.java | 2 +
.../apache/iceberg/spark/extensions/TestViews.java | 102 +++++++
.../apache/iceberg/spark/extensions/TestViews.java | 102 +++++++
7 files changed, 550 insertions(+), 4 deletions(-)
diff --git a/core/src/main/java/org/apache/iceberg/view/ViewMetadata.java
b/core/src/main/java/org/apache/iceberg/view/ViewMetadata.java
index 68d8c64587..ae837ff968 100644
--- a/core/src/main/java/org/apache/iceberg/view/ViewMetadata.java
+++ b/core/src/main/java/org/apache/iceberg/view/ViewMetadata.java
@@ -21,6 +21,7 @@ package org.apache.iceberg.view;
import java.io.Serializable;
import java.util.Comparator;
import java.util.List;
+import java.util.Locale;
import java.util.Map;
import java.util.Objects;
import java.util.Set;
@@ -158,6 +159,7 @@ public interface ViewMetadata extends Serializable {
private Integer lastAddedVersionId = null;
private Integer lastAddedSchemaId = null;
private ViewHistoryEntry historyEntry = null;
+ private ViewVersion previousViewVersion = null;
// indexes
private final Map<Integer, ViewVersion> versionsById;
@@ -187,6 +189,7 @@ public interface ViewMetadata extends Serializable {
this.location = base.location();
this.uuid = base.uuid();
this.metadataLocation = null;
+ this.previousViewVersion = base.currentVersion();
}
public Builder upgradeFormatVersion(int newFormatVersion) {
@@ -298,9 +301,9 @@ public interface ViewMetadata extends Serializable {
if (repr instanceof SQLViewRepresentation) {
SQLViewRepresentation sql = (SQLViewRepresentation) repr;
Preconditions.checkArgument(
- dialects.add(sql.dialect()),
+ dialects.add(sql.dialect().toLowerCase(Locale.ROOT)),
"Invalid view version: Cannot add multiple queries for dialect
%s",
- sql.dialect());
+ sql.dialect().toLowerCase(Locale.ROOT));
}
}
@@ -444,6 +447,14 @@ public interface ViewMetadata extends Serializable {
history.add(historyEntry);
}
+ if (null != previousViewVersion
+ && !PropertyUtil.propertyAsBoolean(
+ properties,
+ ViewProperties.REPLACE_DROP_DIALECT_ALLOWED,
+ ViewProperties.REPLACE_DROP_DIALECT_ALLOWED_DEFAULT)) {
+ checkIfDialectIsDropped(previousViewVersion,
versionsById.get(currentVersionId));
+ }
+
int historySize =
PropertyUtil.propertyAsInt(
properties,
@@ -518,5 +529,29 @@ public interface ViewMetadata extends Serializable {
private <U extends MetadataUpdate> Stream<U> changes(Class<U> updateClass)
{
return
changes.stream().filter(updateClass::isInstance).map(updateClass::cast);
}
+
+ private void checkIfDialectIsDropped(ViewVersion previous, ViewVersion
current) {
+ Set<String> baseDialects = sqlDialectsFor(previous);
+ Set<String> updatedDialects = sqlDialectsFor(current);
+
+ Preconditions.checkState(
+ updatedDialects.containsAll(baseDialects),
+ "Cannot replace view due to loss of view dialects
(%s=false):\nPrevious dialects: %s\nNew dialects: %s",
+ ViewProperties.REPLACE_DROP_DIALECT_ALLOWED,
+ baseDialects,
+ updatedDialects);
+ }
+
+ private Set<String> sqlDialectsFor(ViewVersion viewVersion) {
+ Set<String> dialects = Sets.newHashSet();
+ for (ViewRepresentation repr : viewVersion.representations()) {
+ if (repr instanceof SQLViewRepresentation) {
+ SQLViewRepresentation sql = (SQLViewRepresentation) repr;
+ dialects.add(sql.dialect().toLowerCase(Locale.ROOT));
+ }
+ }
+
+ return dialects;
+ }
}
}
diff --git a/core/src/main/java/org/apache/iceberg/view/ViewProperties.java
b/core/src/main/java/org/apache/iceberg/view/ViewProperties.java
index 12b63659bc..ecade26bde 100644
--- a/core/src/main/java/org/apache/iceberg/view/ViewProperties.java
+++ b/core/src/main/java/org/apache/iceberg/view/ViewProperties.java
@@ -26,6 +26,8 @@ public class ViewProperties {
public static final String METADATA_COMPRESSION =
"write.metadata.compression-codec";
public static final String METADATA_COMPRESSION_DEFAULT = "gzip";
public static final String COMMENT = "comment";
+ public static final String REPLACE_DROP_DIALECT_ALLOWED =
"replace.drop-dialect.allowed";
+ public static final boolean REPLACE_DROP_DIALECT_ALLOWED_DEFAULT = false;
private ViewProperties() {}
}
diff --git a/core/src/test/java/org/apache/iceberg/view/TestViewMetadata.java
b/core/src/test/java/org/apache/iceberg/view/TestViewMetadata.java
index 70f87e3974..60e615a993 100644
--- a/core/src/test/java/org/apache/iceberg/view/TestViewMetadata.java
+++ b/core/src/test/java/org/apache/iceberg/view/TestViewMetadata.java
@@ -858,7 +858,7 @@ public class TestViewMetadata {
.build())
.addRepresentations(
ImmutableSQLViewRepresentation.builder()
- .dialect("spark")
+ .dialect("SpArK")
.sql("select * from ns.tbl2")
.build())
.build())
@@ -881,4 +881,304 @@ public class TestViewMetadata {
.isInstanceOf(ValidationException.class)
.hasMessage("Cannot set last added schema: no schema has been added");
}
+
+ @Test
+ public void droppingDialectFailsByDefault() {
+ Schema schema = new Schema(Types.NestedField.required(1, "x",
Types.LongType.get()));
+ ViewRepresentation spark =
+ ImmutableSQLViewRepresentation.builder().dialect("spark").sql("select
* from tbl").build();
+ ViewRepresentation trino =
+ ImmutableSQLViewRepresentation.builder().dialect("trino").sql("select
* from tbl").build();
+
+ ViewMetadata metadata =
+ ViewMetadata.builder()
+ .setLocation("custom-location")
+ .addSchema(schema)
+ .setCurrentVersion(
+ ImmutableViewVersion.builder()
+ .versionId(1)
+ .schemaId(0)
+ .timestampMillis(System.currentTimeMillis())
+ .defaultNamespace(Namespace.empty())
+ .addRepresentations(spark)
+ .build(),
+ schema)
+ .build();
+
+ assertThatThrownBy(
+ () ->
+ ViewMetadata.buildFrom(metadata)
+ .setCurrentVersion(
+ ImmutableViewVersion.builder()
+ .versionId(1)
+ .schemaId(0)
+ .timestampMillis(System.currentTimeMillis())
+ .defaultNamespace(Namespace.empty())
+ .addRepresentations(trino)
+ .build(),
+ schema)
+ .build())
+ .isInstanceOf(IllegalStateException.class)
+ .hasMessage(
+ "Cannot replace view due to loss of view dialects
(replace.drop-dialect.allowed=false):\n"
+ + "Previous dialects: [spark]\n"
+ + "New dialects: [trino]");
+ }
+
+ @Test
+ public void droppingDialectDoesNotFailWhenAllowed() {
+ Schema schema = new Schema(Types.NestedField.required(1, "x",
Types.LongType.get()));
+ ViewRepresentation spark =
+ ImmutableSQLViewRepresentation.builder().dialect("spark").sql("select
* from tbl").build();
+ ViewRepresentation trino =
+ ImmutableSQLViewRepresentation.builder().dialect("trino").sql("select
* from tbl").build();
+
+ ViewMetadata metadata =
+ ViewMetadata.builder()
+ .setLocation("custom-location")
+ .addSchema(schema)
+ .setCurrentVersion(
+ ImmutableViewVersion.builder()
+ .versionId(1)
+ .schemaId(0)
+ .timestampMillis(System.currentTimeMillis())
+ .defaultNamespace(Namespace.empty())
+ .addRepresentations(spark)
+ .build(),
+ schema)
+ .build();
+
+
assertThat(metadata.currentVersion().representations()).containsExactly(spark);
+
+ // replacing spark with trino should work when dropping the dialect is
allowed
+ ViewMetadata updated =
+ ViewMetadata.buildFrom(metadata)
+ .setCurrentVersion(
+ ImmutableViewVersion.builder()
+ .versionId(1)
+ .schemaId(0)
+ .timestampMillis(System.currentTimeMillis())
+ .defaultNamespace(Namespace.empty())
+ .addRepresentations(trino)
+ .build(),
+ schema)
+
.setProperties(ImmutableMap.of(ViewProperties.REPLACE_DROP_DIALECT_ALLOWED,
"true"))
+ .build();
+
+
assertThat(updated.currentVersion().representations()).containsExactly(trino);
+ }
+
+ @Test
+ public void droppingDialectDoesNotFailWhenKeepingPreviousRepresentation() {
+ Schema schema = new Schema(Types.NestedField.required(1, "x",
Types.LongType.get()));
+ ViewRepresentation spark =
+ ImmutableSQLViewRepresentation.builder().dialect("spark").sql("select
* from tbl").build();
+ ViewRepresentation trino =
+ ImmutableSQLViewRepresentation.builder().dialect("trino").sql("select
* from tbl").build();
+
+ ViewMetadata metadata =
+ ViewMetadata.builder()
+ .setLocation("custom-location")
+ .addSchema(schema)
+ .setCurrentVersion(
+ ImmutableViewVersion.builder()
+ .versionId(1)
+ .schemaId(0)
+ .timestampMillis(System.currentTimeMillis())
+ .defaultNamespace(Namespace.empty())
+ .addRepresentations(spark)
+ .build(),
+ schema)
+ .build();
+
+ // keeping the previous representation and adding a new one shouldn't fail
+ ViewMetadata updated =
+ ViewMetadata.buildFrom(metadata)
+ .setCurrentVersion(
+ ImmutableViewVersion.builder()
+ .versionId(1)
+ .schemaId(0)
+ .timestampMillis(System.currentTimeMillis())
+ .defaultNamespace(Namespace.empty())
+ .addRepresentations(spark)
+ .addRepresentations(trino)
+ .build(),
+ schema)
+ .build();
+
+
assertThat(updated.currentVersion().representations()).containsExactly(spark,
trino);
+ }
+
+ @Test
+ public void droppingDialectDoesNotFailWhenAddingNewRepresentation() {
+ Schema schema = new Schema(Types.NestedField.required(1, "x",
Types.LongType.get()));
+ ViewMetadata metadata =
+ ViewMetadata.builder()
+ .setLocation("custom-location")
+ .addSchema(schema)
+ .setCurrentVersion(
+ ImmutableViewVersion.builder()
+ .versionId(1)
+ .schemaId(0)
+ .timestampMillis(System.currentTimeMillis())
+ .defaultNamespace(Namespace.empty())
+ .build(),
+ schema)
+ .build();
+
+ // this shouldn't fail as there are no previous representations
+
assertThat(ViewMetadata.buildFrom(metadata).build().currentVersion().representations())
+ .isEmpty();
+
+ ViewRepresentation spark =
+ ImmutableSQLViewRepresentation.builder().dialect("spark").sql("select
* from tbl").build();
+ ViewMetadata updated =
+ ViewMetadata.buildFrom(metadata)
+ .setCurrentVersion(
+ ImmutableViewVersion.builder()
+ .versionId(1)
+ .schemaId(0)
+ .timestampMillis(System.currentTimeMillis())
+ .defaultNamespace(Namespace.empty())
+ // this shouldn't fail as there are no previous
representations
+ .addRepresentations(spark)
+ .build(),
+ schema)
+ .build();
+
+
assertThat(updated.currentVersion().representations()).containsExactly(spark);
+ }
+
+ @Test
+ public void droppingDialectFailsWhenSwitchingViewVersionId() {
+ Schema schema = new Schema(Types.NestedField.required(1, "x",
Types.LongType.get()));
+ ViewRepresentation spark =
+ ImmutableSQLViewRepresentation.builder().dialect("spark").sql("select
* from tbl").build();
+ ViewRepresentation trino =
+ ImmutableSQLViewRepresentation.builder().dialect("trino").sql("select
* from tbl").build();
+
+ ViewVersion sparkOnly =
+ ImmutableViewVersion.builder()
+ .versionId(1)
+ .schemaId(0)
+ .timestampMillis(System.currentTimeMillis())
+ .defaultNamespace(Namespace.empty())
+ .addRepresentations(spark)
+ .build();
+ ViewVersion trinoOnly =
+ ImmutableViewVersion.builder()
+ .versionId(2)
+ .schemaId(0)
+ .timestampMillis(System.currentTimeMillis())
+ .defaultNamespace(Namespace.empty())
+ .addRepresentations(trino)
+ .build();
+ ViewVersion trinoAndSpark =
+ ImmutableViewVersion.builder()
+ .versionId(3)
+ .schemaId(0)
+ .timestampMillis(System.currentTimeMillis())
+ .defaultNamespace(Namespace.empty())
+ .addRepresentations(trino)
+ .addRepresentations(spark)
+ .build();
+
+ // add all different view versions and set it to using sparkOnly
+ ViewMetadata metadata =
+ ViewMetadata.builder()
+ .setLocation("custom-location")
+ .addSchema(schema)
+ .addVersion(sparkOnly)
+ .addVersion(trinoOnly)
+ .addVersion(trinoAndSpark)
+ .setCurrentVersionId(1)
+ .build();
+
+ assertThatThrownBy(
+ () ->
+ ViewMetadata.buildFrom(metadata)
+ // switches from sparkOnly to trinoOnly. Intermediate
versionId updates are
+ // ignored
+ .setCurrentVersionId(trinoAndSpark.versionId())
+ .setCurrentVersionId(sparkOnly.versionId())
+ .setCurrentVersionId(trinoOnly.versionId())
+ .build())
+ .isInstanceOf(IllegalStateException.class)
+ .hasMessage(
+ "Cannot replace view due to loss of view dialects
(replace.drop-dialect.allowed=false):\n"
+ + "Previous dialects: [spark]\n"
+ + "New dialects: [trino]");
+
+ assertThatThrownBy(
+ () ->
+ ViewMetadata.buildFrom(
+ ViewMetadata.buildFrom(metadata)
+ .setCurrentVersionId(trinoAndSpark.versionId())
+ .build())
+ // switches from trinoAndSpark back to trinoOnly.
Intermediate versionId
+ // updates are ignored
+ .setCurrentVersionId(sparkOnly.versionId())
+ .setCurrentVersionId(trinoOnly.versionId())
+ .build())
+ .isInstanceOf(IllegalStateException.class)
+ .hasMessage(
+ "Cannot replace view due to loss of view dialects
(replace.drop-dialect.allowed=false):\n"
+ + "Previous dialects: [trino, spark]\n"
+ + "New dialects: [trino]");
+ }
+
+ @Test
+ public void droppingDialectAllowedAndThenDisallowed() {
+ Schema schema = new Schema(Types.NestedField.required(1, "x",
Types.LongType.get()));
+ ViewRepresentation spark =
+ ImmutableSQLViewRepresentation.builder().dialect("spark").sql("select
* from tbl").build();
+ ViewRepresentation trino =
+ ImmutableSQLViewRepresentation.builder().dialect("trino").sql("select
* from tbl").build();
+
+ ViewMetadata metadata =
+ ViewMetadata.builder()
+ .setLocation("custom-location")
+ .addSchema(schema)
+ .setCurrentVersion(
+ ImmutableViewVersion.builder()
+ .versionId(1)
+ .schemaId(0)
+ .timestampMillis(System.currentTimeMillis())
+ .defaultNamespace(Namespace.empty())
+ .addRepresentations(spark)
+ .build(),
+ schema)
+ .build();
+
+ ViewMetadata updated =
+ ViewMetadata.buildFrom(metadata)
+ .setCurrentVersion(
+ ImmutableViewVersion.builder()
+ .versionId(1)
+ .schemaId(0)
+ .timestampMillis(System.currentTimeMillis())
+ .defaultNamespace(Namespace.empty())
+ // this drops spark and is allowed
+ .addRepresentations(trino)
+ .build(),
+ schema)
+
.setProperties(ImmutableMap.of(ViewProperties.REPLACE_DROP_DIALECT_ALLOWED,
"true"))
+ .build();
+
+
assertThat(updated.currentVersion().representations()).containsExactly(trino);
+
+ assertThatThrownBy(
+ () ->
+ ViewMetadata.buildFrom(updated)
+ // switches back to spark and isn't allowed
+ .setCurrentVersionId(1)
+ .setProperties(
+
ImmutableMap.of(ViewProperties.REPLACE_DROP_DIALECT_ALLOWED, "false"))
+ .build())
+ .isInstanceOf(IllegalStateException.class)
+ .hasMessage(
+ "Cannot replace view due to loss of view dialects
(replace.drop-dialect.allowed=false):\n"
+ + "Previous dialects: [trino]\n"
+ + "New dialects: [spark]");
+ }
}
diff --git a/core/src/test/java/org/apache/iceberg/view/ViewCatalogTests.java
b/core/src/test/java/org/apache/iceberg/view/ViewCatalogTests.java
index 58727e4588..b3765bb1ea 100644
--- a/core/src/test/java/org/apache/iceberg/view/ViewCatalogTests.java
+++ b/core/src/test/java/org/apache/iceberg/view/ViewCatalogTests.java
@@ -863,7 +863,8 @@ public abstract class ViewCatalogTests<C extends
ViewCatalog & SupportsNamespace
.withDefaultNamespace(identifier.namespace())
.withQuery("trino", "select count(*) from ns.tbl")
.withProperty("replacedProp1", "val1")
- .withProperty("replacedProp2", "val2");
+ .withProperty("replacedProp2", "val2")
+ .withProperty(ViewProperties.REPLACE_DROP_DIALECT_ALLOWED, "true");
View replacedView = useCreateOrReplace ? viewBuilder.createOrReplace() :
viewBuilder.replace();
// validate replaced view settings
@@ -1092,6 +1093,7 @@ public abstract class ViewCatalogTests<C extends
ViewCatalog & SupportsNamespace
.withDefaultNamespace(identifier.namespace())
.withQuery(trino.dialect(), trino.sql())
.withQuery(spark.dialect(), spark.sql())
+ .withProperty(ViewProperties.REPLACE_DROP_DIALECT_ALLOWED, "true")
.create();
assertThat(catalog().viewExists(identifier)).as("View should
exist").isTrue();
@@ -1552,6 +1554,7 @@ public abstract class ViewCatalogTests<C extends
ViewCatalog & SupportsNamespace
.withSchema(SCHEMA)
.withDefaultNamespace(identifier.namespace())
.withQuery("trino", "select * from ns.tbl")
+ .withProperty(ViewProperties.REPLACE_DROP_DIALECT_ALLOWED, "true")
.create();
assertThat(catalog().viewExists(identifier)).as("View should
exist").isTrue();
diff --git
a/nessie/src/test/java/org/apache/iceberg/nessie/BaseTestIceberg.java
b/nessie/src/test/java/org/apache/iceberg/nessie/BaseTestIceberg.java
index c2fa8d4da1..d56d07f7d2 100644
--- a/nessie/src/test/java/org/apache/iceberg/nessie/BaseTestIceberg.java
+++ b/nessie/src/test/java/org/apache/iceberg/nessie/BaseTestIceberg.java
@@ -53,6 +53,7 @@ import org.apache.iceberg.types.Types;
import org.apache.iceberg.types.Types.LongType;
import org.apache.iceberg.view.BaseView;
import org.apache.iceberg.view.View;
+import org.apache.iceberg.view.ViewProperties;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.extension.ExtendWith;
@@ -200,6 +201,7 @@ public abstract class BaseTestIceberg {
.withSchema(schema)
.withDefaultNamespace(tableIdentifier.namespace())
.withQuery("spark", "select * from ns.tbl")
+ .withProperty(ViewProperties.REPLACE_DROP_DIALECT_ALLOWED, "true")
.create();
}
diff --git
a/spark/v3.4/spark-extensions/src/test/java/org/apache/iceberg/spark/extensions/TestViews.java
b/spark/v3.4/spark-extensions/src/test/java/org/apache/iceberg/spark/extensions/TestViews.java
index 9e78c54518..ea9ccc9133 100644
---
a/spark/v3.4/spark-extensions/src/test/java/org/apache/iceberg/spark/extensions/TestViews.java
+++
b/spark/v3.4/spark-extensions/src/test/java/org/apache/iceberg/spark/extensions/TestViews.java
@@ -39,12 +39,18 @@ import org.apache.iceberg.spark.SparkCatalogConfig;
import org.apache.iceberg.spark.SparkSchemaUtil;
import org.apache.iceberg.spark.source.SimpleRecord;
import org.apache.iceberg.types.Types;
+import org.apache.iceberg.view.ImmutableSQLViewRepresentation;
+import org.apache.iceberg.view.SQLViewRepresentation;
import org.apache.iceberg.view.View;
+import org.apache.iceberg.view.ViewHistoryEntry;
+import org.apache.iceberg.view.ViewProperties;
+import org.apache.iceberg.view.ViewVersion;
import org.apache.spark.sql.AnalysisException;
import org.apache.spark.sql.Dataset;
import org.apache.spark.sql.Row;
import org.apache.spark.sql.catalyst.analysis.NoSuchTableException;
import org.apache.spark.sql.catalyst.catalog.SessionCatalog;
+import org.assertj.core.api.InstanceOfAssertFactories;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
@@ -1604,6 +1610,102 @@ public class TestViews extends SparkExtensionsTestBase {
.asStruct());
}
+ @Test
+ public void replacingTrinoViewShouldFail() {
+ String viewName = viewName("trinoView");
+ String sql = String.format("SELECT id FROM %s", tableName);
+
+ ViewCatalog viewCatalog = viewCatalog();
+
+ viewCatalog
+ .buildView(TableIdentifier.of(NAMESPACE, viewName))
+ .withQuery("trino", sql)
+ .withDefaultNamespace(NAMESPACE)
+ .withDefaultCatalog(catalogName)
+ .withSchema(schema(sql))
+ .create();
+
+ assertThatThrownBy(() -> sql("CREATE OR REPLACE VIEW %s AS %s", viewName,
sql))
+ .isInstanceOf(IllegalStateException.class)
+ .hasMessage(
+ "Cannot replace view due to loss of view dialects
(replace.drop-dialect.allowed=false):\n"
+ + "Previous dialects: [trino]\n"
+ + "New dialects: [spark]");
+ }
+
+ @Test
+ public void replacingTrinoAndSparkViewShouldFail() {
+ String viewName = viewName("trinoAndSparkView");
+ String sql = String.format("SELECT id FROM %s", tableName);
+
+ ViewCatalog viewCatalog = viewCatalog();
+
+ viewCatalog
+ .buildView(TableIdentifier.of(NAMESPACE, viewName))
+ .withQuery("trino", sql)
+ .withQuery("spark", sql)
+ .withDefaultNamespace(NAMESPACE)
+ .withDefaultCatalog(catalogName)
+ .withSchema(schema(sql))
+ .create();
+
+ assertThatThrownBy(() -> sql("CREATE OR REPLACE VIEW %s AS %s", viewName,
sql))
+ .isInstanceOf(IllegalStateException.class)
+ .hasMessage(
+ "Cannot replace view due to loss of view dialects
(replace.drop-dialect.allowed=false):\n"
+ + "Previous dialects: [trino, spark]\n"
+ + "New dialects: [spark]");
+ }
+
+ @Test
+ public void replacingViewWithDialectDropAllowed() {
+ String viewName = viewName("trinoView");
+ String sql = String.format("SELECT id FROM %s", tableName);
+
+ ViewCatalog viewCatalog = viewCatalog();
+
+ viewCatalog
+ .buildView(TableIdentifier.of(NAMESPACE, viewName))
+ .withQuery("trino", sql)
+ .withDefaultNamespace(NAMESPACE)
+ .withDefaultCatalog(catalogName)
+ .withSchema(schema(sql))
+ .create();
+
+ // allowing to drop the trino dialect should replace the view
+ sql(
+ "CREATE OR REPLACE VIEW %s TBLPROPERTIES ('%s'='true') AS SELECT id
FROM %s",
+ viewName, ViewProperties.REPLACE_DROP_DIALECT_ALLOWED, tableName);
+
+ View view = viewCatalog.loadView(TableIdentifier.of(NAMESPACE, viewName));
+ assertThat(view.currentVersion().representations())
+ .hasSize(1)
+ .first()
+
.asInstanceOf(InstanceOfAssertFactories.type(SQLViewRepresentation.class))
+
.isEqualTo(ImmutableSQLViewRepresentation.builder().dialect("spark").sql(sql).build());
+
+ // trino view should show up in the view versions & history
+ assertThat(view.history()).hasSize(2);
+
assertThat(view.history()).element(0).extracting(ViewHistoryEntry::versionId).isEqualTo(1);
+
assertThat(view.history()).element(1).extracting(ViewHistoryEntry::versionId).isEqualTo(2);
+
+ assertThat(view.versions()).hasSize(2);
+
assertThat(view.versions()).element(0).extracting(ViewVersion::versionId).isEqualTo(1);
+
assertThat(view.versions()).element(1).extracting(ViewVersion::versionId).isEqualTo(2);
+
+ assertThat(Lists.newArrayList(view.versions()).get(0).representations())
+ .hasSize(1)
+ .first()
+
.asInstanceOf(InstanceOfAssertFactories.type(SQLViewRepresentation.class))
+
.isEqualTo(ImmutableSQLViewRepresentation.builder().dialect("trino").sql(sql).build());
+
+ assertThat(Lists.newArrayList(view.versions()).get(1).representations())
+ .hasSize(1)
+ .first()
+
.asInstanceOf(InstanceOfAssertFactories.type(SQLViewRepresentation.class))
+
.isEqualTo(ImmutableSQLViewRepresentation.builder().dialect("spark").sql(sql).build());
+ }
+
private void insertRows(int numRows) throws NoSuchTableException {
List<SimpleRecord> records = Lists.newArrayListWithCapacity(numRows);
for (int i = 1; i <= numRows; i++) {
diff --git
a/spark/v3.5/spark-extensions/src/test/java/org/apache/iceberg/spark/extensions/TestViews.java
b/spark/v3.5/spark-extensions/src/test/java/org/apache/iceberg/spark/extensions/TestViews.java
index ee23d4a766..267628cd06 100644
---
a/spark/v3.5/spark-extensions/src/test/java/org/apache/iceberg/spark/extensions/TestViews.java
+++
b/spark/v3.5/spark-extensions/src/test/java/org/apache/iceberg/spark/extensions/TestViews.java
@@ -39,12 +39,18 @@ import org.apache.iceberg.spark.SparkCatalogConfig;
import org.apache.iceberg.spark.SparkSchemaUtil;
import org.apache.iceberg.spark.source.SimpleRecord;
import org.apache.iceberg.types.Types;
+import org.apache.iceberg.view.ImmutableSQLViewRepresentation;
+import org.apache.iceberg.view.SQLViewRepresentation;
import org.apache.iceberg.view.View;
+import org.apache.iceberg.view.ViewHistoryEntry;
+import org.apache.iceberg.view.ViewProperties;
+import org.apache.iceberg.view.ViewVersion;
import org.apache.spark.sql.AnalysisException;
import org.apache.spark.sql.Dataset;
import org.apache.spark.sql.Row;
import org.apache.spark.sql.catalyst.analysis.NoSuchTableException;
import org.apache.spark.sql.catalyst.catalog.SessionCatalog;
+import org.assertj.core.api.InstanceOfAssertFactories;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
@@ -1604,6 +1610,102 @@ public class TestViews extends SparkExtensionsTestBase {
.asStruct());
}
+ @Test
+ public void replacingTrinoViewShouldFail() {
+ String viewName = viewName("trinoView");
+ String sql = String.format("SELECT id FROM %s", tableName);
+
+ ViewCatalog viewCatalog = viewCatalog();
+
+ viewCatalog
+ .buildView(TableIdentifier.of(NAMESPACE, viewName))
+ .withQuery("trino", sql)
+ .withDefaultNamespace(NAMESPACE)
+ .withDefaultCatalog(catalogName)
+ .withSchema(schema(sql))
+ .create();
+
+ assertThatThrownBy(() -> sql("CREATE OR REPLACE VIEW %s AS %s", viewName,
sql))
+ .isInstanceOf(IllegalStateException.class)
+ .hasMessage(
+ "Cannot replace view due to loss of view dialects
(replace.drop-dialect.allowed=false):\n"
+ + "Previous dialects: [trino]\n"
+ + "New dialects: [spark]");
+ }
+
+ @Test
+ public void replacingTrinoAndSparkViewShouldFail() {
+ String viewName = viewName("trinoAndSparkView");
+ String sql = String.format("SELECT id FROM %s", tableName);
+
+ ViewCatalog viewCatalog = viewCatalog();
+
+ viewCatalog
+ .buildView(TableIdentifier.of(NAMESPACE, viewName))
+ .withQuery("trino", sql)
+ .withQuery("spark", sql)
+ .withDefaultNamespace(NAMESPACE)
+ .withDefaultCatalog(catalogName)
+ .withSchema(schema(sql))
+ .create();
+
+ assertThatThrownBy(() -> sql("CREATE OR REPLACE VIEW %s AS %s", viewName,
sql))
+ .isInstanceOf(IllegalStateException.class)
+ .hasMessage(
+ "Cannot replace view due to loss of view dialects
(replace.drop-dialect.allowed=false):\n"
+ + "Previous dialects: [trino, spark]\n"
+ + "New dialects: [spark]");
+ }
+
+ @Test
+ public void replacingViewWithDialectDropAllowed() {
+ String viewName = viewName("trinoView");
+ String sql = String.format("SELECT id FROM %s", tableName);
+
+ ViewCatalog viewCatalog = viewCatalog();
+
+ viewCatalog
+ .buildView(TableIdentifier.of(NAMESPACE, viewName))
+ .withQuery("trino", sql)
+ .withDefaultNamespace(NAMESPACE)
+ .withDefaultCatalog(catalogName)
+ .withSchema(schema(sql))
+ .create();
+
+ // allowing to drop the trino dialect should replace the view
+ sql(
+ "CREATE OR REPLACE VIEW %s TBLPROPERTIES ('%s'='true') AS SELECT id
FROM %s",
+ viewName, ViewProperties.REPLACE_DROP_DIALECT_ALLOWED, tableName);
+
+ View view = viewCatalog.loadView(TableIdentifier.of(NAMESPACE, viewName));
+ assertThat(view.currentVersion().representations())
+ .hasSize(1)
+ .first()
+
.asInstanceOf(InstanceOfAssertFactories.type(SQLViewRepresentation.class))
+
.isEqualTo(ImmutableSQLViewRepresentation.builder().dialect("spark").sql(sql).build());
+
+ // trino view should show up in the view versions & history
+ assertThat(view.history()).hasSize(2);
+
assertThat(view.history()).element(0).extracting(ViewHistoryEntry::versionId).isEqualTo(1);
+
assertThat(view.history()).element(1).extracting(ViewHistoryEntry::versionId).isEqualTo(2);
+
+ assertThat(view.versions()).hasSize(2);
+
assertThat(view.versions()).element(0).extracting(ViewVersion::versionId).isEqualTo(1);
+
assertThat(view.versions()).element(1).extracting(ViewVersion::versionId).isEqualTo(2);
+
+ assertThat(Lists.newArrayList(view.versions()).get(0).representations())
+ .hasSize(1)
+ .first()
+
.asInstanceOf(InstanceOfAssertFactories.type(SQLViewRepresentation.class))
+
.isEqualTo(ImmutableSQLViewRepresentation.builder().dialect("trino").sql(sql).build());
+
+ assertThat(Lists.newArrayList(view.versions()).get(1).representations())
+ .hasSize(1)
+ .first()
+
.asInstanceOf(InstanceOfAssertFactories.type(SQLViewRepresentation.class))
+
.isEqualTo(ImmutableSQLViewRepresentation.builder().dialect("spark").sql(sql).build());
+ }
+
private void insertRows(int numRows) throws NoSuchTableException {
List<SimpleRecord> records = Lists.newArrayListWithCapacity(numRows);
for (int i = 1; i <= numRows; i++) {