Skip to content

World's most powerful open data catalog for building a high-performance, geo-distributed and federated metadata lake.

[#14799] fix(catalog-hive): reject default catalog and schema on Spark views (#14800)

Qi Yu92c590b73a20
diff --git a/catalogs/catalog-hive/src/main/java/org/apache/gravitino/catalog/hive/HiveViewCatalogOperations.java b/catalogs/catalog-hive/src/main/java/org/apache/gravitino/catalog/hive/HiveViewCatalogOperations.java
index a636fe142a..1fe7b313ad 100644
--- a/catalogs/catalog-hive/src/main/java/org/apache/gravitino/catalog/hive/HiveViewCatalogOperations.java
+++ b/catalogs/catalog-hive/src/main/java/org/apache/gravitino/catalog/hive/HiveViewCatalogOperations.java
@@ -637,14 +637,7 @@ class HiveViewCatalogOperations implements ViewCatalog {
SQLRepresentation selected = (SQLRepresentation) firstRepresentation;
switch (selected.dialect().toLowerCase(Locale.ROOT)) {
case Dialects.HIVE:
- Preconditions.checkArgument(
- defaultCatalog == null && defaultSchema == null,
- "Dialect '%s' does not support non-null defaultCatalog/defaultSchema, but got "
- + "defaultCatalog=%s, defaultSchema=%s for view %s",
- selected.dialect(),
- defaultCatalog,
- defaultSchema,
- ident);
+ checkNoDefaultCatalogAndSchema(selected, defaultCatalog, defaultSchema, ident);
return selected;
case Dialects.TRINO:
// The default catalog/schema are encoded into the Trino native view payload by
@@ -664,14 +657,7 @@ class HiveViewCatalogOperations implements ViewCatalog {
ident);
return selected;
case Dialects.FLINK:
- Preconditions.checkArgument(
- defaultCatalog == null && defaultSchema == null,
- "Dialect '%s' does not support non-null defaultCatalog/defaultSchema, but got "
- + "defaultCatalog=%s, defaultSchema=%s for view %s",
- selected.dialect(),
- defaultCatalog,
- defaultSchema,
- ident);
+ checkNoDefaultCatalogAndSchema(selected, defaultCatalog, defaultSchema, ident);
Preconditions.checkArgument(
properties.keySet().stream()
.anyMatch(k -> k.startsWith(HiveView.FLINK_PROPERTY_PREFIX)),
@@ -681,6 +667,7 @@ class HiveViewCatalogOperations implements ViewCatalog {
HiveView.FLINK_PROPERTY_PREFIX);
return selected;
case Dialects.SPARK:
+ checkNoDefaultCatalogAndSchema(selected, defaultCatalog, defaultSchema, ident);
Preconditions.checkArgument(
properties.containsKey(HiveView.SPARK_VERSION_KEY),
"Spark dialect view '%s' requires property '%s' to be set; "
@@ -696,6 +683,21 @@ class HiveViewCatalogOperations implements ViewCatalog {
}
}
+ private void checkNoDefaultCatalogAndSchema(
+ SQLRepresentation representation,
+ String defaultCatalog,
+ String defaultSchema,
+ NameIdentifier ident) {
+ Preconditions.checkArgument(
+ defaultCatalog == null && defaultSchema == null,
+ "Dialect '%s' does not support non-null defaultCatalog/defaultSchema, but got "
+ + "defaultCatalog=%s, defaultSchema=%s for view %s",
+ representation.dialect(),
+ defaultCatalog,
+ defaultSchema,
+ ident);
+ }
+
/**
* Sets or clears the {@code presto_view} marker in the given HMS property map, so that a Trino
* dialect view is recognized as a native Trino view (see {@link TrinoNativeViewCodec}).
diff --git a/catalogs/catalog-hive/src/test/java/org/apache/gravitino/catalog/hive/TestHiveCatalogOperations.java b/catalogs/catalog-hive/src/test/java/org/apache/gravitino/catalog/hive/TestHiveCatalogOperations.java
index afa3b7f7ac..d92e55d44c 100644
--- a/catalogs/catalog-hive/src/test/java/org/apache/gravitino/catalog/hive/TestHiveCatalogOperations.java
+++ b/catalogs/catalog-hive/src/test/java/org/apache/gravitino/catalog/hive/TestHiveCatalogOperations.java
@@ -808,6 +808,48 @@ class TestHiveCatalogOperations {
exception.getMessage().contains("does not support non-null defaultCatalog/defaultSchema"));
}
+ @Test
+ void testCreateSparkViewRejectsNonNullDefaultCatalogOrSchema() throws Exception {
+ HiveCatalogOperations op = new HiveCatalogOperations();
+ op.initialize(Maps.newHashMap(), null, HIVE_PROPERTIES_METADATA);
+
+ CachedClientPool clientPool = mock(CachedClientPool.class);
+ HiveClient hiveClient = mock(HiveClient.class);
+ HiveSchema schema = HiveSchema.builder().withCatalogName("hive").withName("db").build();
+ when(hiveClient.getDatabase(anyString(), anyString())).thenReturn(schema);
+ when(clientPool.run(any()))
+ .thenAnswer(
+ invocation -> {
+ ClientPool.Action<?, HiveClient, ?> action = invocation.getArgument(0);
+ return action.run(hiveClient);
+ });
+ op.clientPool = clientPool;
+
+ String[][] defaults = {{"analytics", "mart"}, {"analytics", null}, {null, "mart"}};
+ for (String[] pair : defaults) {
+ IllegalArgumentException exception =
+ Assertions.assertThrows(
+ IllegalArgumentException.class,
+ () ->
+ op.createView(
+ NameIdentifier.of("db", "v_spark"),
+ null,
+ new Column[0],
+ new SQLRepresentation[] {
+ SQLRepresentation.builder().withDialect("spark").withSql("SELECT 1").build()
+ },
+ pair[0],
+ pair[1],
+ Maps.newHashMap(ImmutableMap.of("spark.sql.create.version", "3.5.3"))));
+
+ Assertions.assertTrue(
+ exception
+ .getMessage()
+ .contains("does not support non-null defaultCatalog/defaultSchema"));
+ }
+ verify(hiveClient, never()).createTable(any());
+ }
+
@Test
void testCreateViewAcceptsSparkDialect() throws Exception {
HiveCatalogOperations op = new HiveCatalogOperations();
@@ -2067,6 +2109,59 @@ class TestHiveCatalogOperations {
exception.getMessage().contains("does not support non-null defaultCatalog/defaultSchema"));
}
+ @Test
+ void testAlterSparkViewReplaceRejectsNonNullDefaultCatalogAndSchema() throws Exception {
+ HiveCatalogOperations op = new HiveCatalogOperations();
+ op.initialize(Maps.newHashMap(), null, HIVE_PROPERTIES_METADATA);
+
+ CachedClientPool clientPool = mock(CachedClientPool.class);
+ HiveClient hiveClient = mock(HiveClient.class);
+ HiveTable currentTable =
+ HiveTable.builder()
+ .withName("v_spark")
+ .withCatalogName("hive")
+ .withDatabaseName("db")
+ .withColumns(new Column[0])
+ .withProperties(
+ Maps.newHashMap(
+ ImmutableMap.of(
+ HiveConstants.TABLE_TYPE,
+ TableType.VIRTUAL_VIEW.name(),
+ "spark.sql.create.version",
+ "3.5.3")))
+ .withViewOriginalText("SELECT 1")
+ .build();
+ when(hiveClient.getTable(anyString(), anyString(), anyString())).thenReturn(currentTable);
+ when(clientPool.run(any()))
+ .thenAnswer(
+ invocation -> {
+ ClientPool.Action<?, HiveClient, ?> action = invocation.getArgument(0);
+ return action.run(hiveClient);
+ });
+ op.clientPool = clientPool;
+
+ IllegalArgumentException exception =
+ Assertions.assertThrows(
+ IllegalArgumentException.class,
+ () ->
+ op.alterView(
+ NameIdentifier.of("db", "v_spark"),
+ ViewChange.replaceView(
+ new Column[0],
+ new SQLRepresentation[] {
+ SQLRepresentation.builder()
+ .withDialect("spark")
+ .withSql("SELECT 2")
+ .build()
+ },
+ "analytics",
+ "mart",
+ null)));
+
+ Assertions.assertTrue(
+ exception.getMessage().contains("does not support non-null defaultCatalog/defaultSchema"));
+ }
+
@Test
void testDropViewThrowsForUnexpectedException() throws Exception {
HiveCatalogOperations op = new HiveCatalogOperations();
diff --git a/spark-connector/spark-common/src/test/java/org/apache/gravitino/spark/connector/integration/test/hive/SparkHiveCatalogIT.java b/spark-connector/spark-common/src/test/java/org/apache/gravitino/spark/connector/integration/test/hive/SparkHiveCatalogIT.java
index 9e9ab3b44d..b2e58eeca7 100644
--- a/spark-connector/spark-common/src/test/java/org/apache/gravitino/spark/connector/integration/test/hive/SparkHiveCatalogIT.java
+++ b/spark-connector/spark-common/src/test/java/org/apache/gravitino/spark/connector/integration/test/hive/SparkHiveCatalogIT.java
@@ -631,8 +631,8 @@ public abstract class SparkHiveCatalogIT extends SparkCommonIT {
Column.of("age", Types.IntegerType.get(), null)
},
new SQLRepresentation[] {sparkRep},
- getCatalogName(),
- schemaName,
+ null,
+ null,
ImmutableMap.of("spark.sql.create.version", getSparkSession().version()));
try {
@@ -811,8 +811,8 @@ public abstract class SparkHiveCatalogIT extends SparkCommonIT {
null,
new Column[0],
new SQLRepresentation[] {sparkRep},
- getCatalogName(),
- schemaName,
+ null,
+ null,
ImmutableMap.of("spark.sql.create.version", getSparkSession().version()));
try {