jerryshao commented on code in PR #14296: URL: https://github.com/apache/gravitino/pull/14296#discussion_r4490096112
########## spark-connector/spark-common/src/test/java/org/apache/gravitino/spark/connector/iceberg/extensions/TestIcebergExtendedDataSourceV2Strategy.java: ########## @@ -0,0 +1,82 @@ +/* + * 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.gravitino.spark.connector.iceberg.extensions; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +import java.nio.file.Path; +import java.util.List; +import org.apache.iceberg.spark.SparkCatalog; +import org.apache.iceberg.spark.extensions.IcebergSparkSessionExtensions; +import org.apache.spark.sql.Row; +import org.apache.spark.sql.SparkSession; +import org.junit.jupiter.api.AfterAll; +import org.junit.jupiter.api.BeforeAll; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.io.TempDir; + +/** + * Tests {@link IcebergExtendedDataSourceV2Strategy} on an Iceberg catalog that Gravitino does not + * manage. + */ +public class TestIcebergExtendedDataSourceV2Strategy { + + @TempDir static Path warehouse; + + private static SparkSession spark; + + @BeforeAll + static void startSpark() { + // Same extension order as GravitinoDriverPlugin: the Gravitino strategy runs first. + spark = + SparkSession.builder() + .master("local[1]") + .appName("test-iceberg-extended-strategy") + .config("spark.ui.enabled", "false") + .config( + "spark.sql.extensions", + GravitinoIcebergSparkSessionExtensions.class.getName() + + "," + + IcebergSparkSessionExtensions.class.getName()) + .config("spark.sql.catalog.local", SparkCatalog.class.getName()) + .config("spark.sql.catalog.local.type", "hadoop") + .config("spark.sql.catalog.local.warehouse", warehouse.toUri().toString()) + .getOrCreate(); + } + + @AfterAll + static void stopSpark() { + if (spark != null) { + spark.stop(); + } + } + + @Test + void testIcebergDdlOnPlainIcebergCatalog() { Review Comment: The fix touches ten plan branches, but this test only exercises `CREATE TAG` and `CREATE BRANCH`. Since a Spark session is already up, it would be cheap to also run a few of the others on the same table (e.g. `DROP TAG`, `DROP BRANCH`, `ADD PARTITION FIELD`, `SET IDENTIFIER FIELDS`) so that a missed or reverted `getOrElse` in any branch is caught. It would also be worth having a small test that a `GravitinoIcebergCatalog` still takes the Gravitino branch, since that path is currently only covered by the ITs. ########## spark-connector/spark-common/src/main/java/org/apache/gravitino/spark/connector/iceberg/extensions/IcebergExtendedDataSourceV2Strategy.java: ########## @@ -269,14 +269,17 @@ static Option<IcebergCatalogAndIdentifier> buildCatalogAndIdentifier( return new Some<>( IcebergCatalogAndIdentifier.of( (TableCatalog) catalog, catalogAndIdentifier.identifier())); - } else { - // TODO: support SparkSessionCatalog - throw new UnsupportedOperationException( - "Unsupported catalog type: " + catalog.getClass().getName()); } + // Not a Gravitino catalog: the caller falls back to the parent Iceberg strategy. + return Option.empty(); } } + private Seq<SparkPlan> superFallback(LogicalPlan plan) { + // On Scala 2.12 Strategy.apply returns scala.collection.Seq; the parent builds immutable Lists. + return (Seq<SparkPlan>) (Object) super.apply(plan); Review Comment: Nit: the `(Seq<SparkPlan>) (Object) super.apply(plan)` cast is unchecked and relies on the parent always returning an immutable `Seq`. The `else` branch of `apply` a few lines above already handles this by calling `.toIndexedSeq()` on the parent's result and handling `null`. Could `superFallback` do the same, and could the `else` branch call `superFallback` so the conversion lives in one place? -- 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: [email protected] For queries about this service, please contact Infrastructure at: [email protected]
