diqiu50 commented on code in PR #11186:
URL: https://github.com/apache/gravitino/pull/11186#discussion_r3316173317


##########
spark-connector/spark-common/src/main/java/org/apache/gravitino/spark/connector/glue/GravitinoGlueCatalog.java:
##########
@@ -0,0 +1,350 @@
+/*
+ * 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.glue;
+
+import com.google.common.base.Preconditions;
+import java.util.Arrays;
+import java.util.Collections;
+import java.util.HashMap;
+import java.util.Map;
+import org.apache.gravitino.catalog.glue.GlueConstants;
+import org.apache.gravitino.spark.connector.PropertiesConverter;
+import org.apache.gravitino.spark.connector.SparkTransformConverter;
+import org.apache.gravitino.spark.connector.SparkTypeConverter;
+import org.apache.gravitino.spark.connector.catalog.BaseCatalog;
+import org.apache.gravitino.spark.connector.hive.SparkHiveTable;
+import org.apache.gravitino.spark.connector.hive.SparkHiveTypeConverter;
+import org.apache.gravitino.spark.connector.iceberg.SparkIcebergTable;
+import org.apache.iceberg.spark.SparkCatalog;
+import org.apache.iceberg.spark.source.SparkTable;
+import org.apache.kyuubi.spark.connector.hive.HiveTable;
+import org.apache.kyuubi.spark.connector.hive.HiveTableCatalog;
+import org.apache.spark.sql.catalyst.analysis.NamespaceAlreadyExistsException;
+import org.apache.spark.sql.catalyst.analysis.NoSuchNamespaceException;
+import org.apache.spark.sql.catalyst.analysis.NoSuchTableException;
+import org.apache.spark.sql.catalyst.analysis.TableAlreadyExistsException;
+import org.apache.spark.sql.connector.catalog.Identifier;
+import org.apache.spark.sql.connector.catalog.SupportsNamespaces;
+import org.apache.spark.sql.connector.catalog.Table;
+import org.apache.spark.sql.connector.catalog.TableCatalog;
+import org.apache.spark.sql.connector.expressions.Transform;
+import org.apache.spark.sql.types.DataTypes;
+import org.apache.spark.sql.types.StructField;
+import org.apache.spark.sql.types.StructType;
+import org.apache.spark.sql.util.CaseInsensitiveStringMap;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+/**
+ * Gravitino Glue catalog implementation for Apache Spark.
+ *
+ * <p>This catalog handles mixed table types stored in AWS Glue Data Catalog:
+ *
+ * <ul>
+ *   <li>Non-Iceberg tables (Hive, Delta, Parquet): routed to HiveTableCatalog 
for I/O
+ *   <li>Iceberg tables: routed to Iceberg's GlueCatalog for I/O
+ * </ul>
+ *
+ * <p>Table routing is based on the {@code table-format} property in Glue 
table parameters. Tables
+ * with {@code table-format=ICEBERG} are delegated to the Iceberg backend.
+ *
+ * <p>Derby sync: Gravitino creates/modifies tables in AWS Glue. 
HiveTableCatalog (used as
+ * sparkCatalog) uses an embedded Derby metastore for metadata validation. We 
must keep Derby in
+ * sync with Glue for loadSparkTable() to succeed. Derby is populated lazily 
on createTable() and
+ * loadTable(), and cleaned up on dropTable()/purgeTable()/renameTable().
+ */
+public class GravitinoGlueCatalog extends BaseCatalog {
+
+  private static final Logger LOG = 
LoggerFactory.getLogger(GravitinoGlueCatalog.class);
+
+  // Lazily initialized Iceberg GlueCatalog for Iceberg tables
+  private volatile SparkCatalog icebergGlueCatalog;
+
+  // Store original config for Iceberg catalog initialization
+  private String catalogName;
+  private Map<String, String> catalogProperties;
+
+  /** Creates a new GravitinoGlueCatalog. */
+  public GravitinoGlueCatalog() {}
+
+  /**
+   * Creates a new HiveTableCatalog instance. Override in tests to inject mock 
instances.
+   *
+   * @return a new HiveTableCatalog
+   */
+  protected HiveTableCatalog createHiveTableCatalog() {
+    return new HiveTableCatalog();
+  }
+
+  @Override
+  protected TableCatalog createAndInitSparkCatalog(
+      String name, CaseInsensitiveStringMap options, Map<String, String> 
properties) {
+    this.catalogName = name;
+    this.catalogProperties = properties;
+
+    TableCatalog hiveCatalog = createHiveTableCatalog();
+    Map<String, String> all =
+        getPropertiesConverter().toSparkCatalogProperties(options, properties);
+    hiveCatalog.initialize(name, new CaseInsensitiveStringMap(all));
+    return hiveCatalog;
+  }
+
+  /**
+   * Routes Spark table loading to the correct backend after Gravitino creates 
the table.
+   *
+   * <p>Iceberg tables are loaded from the Iceberg GlueCatalog; they are never 
registered in Derby.
+   * Hive tables are loaded from Derby, syncing from Glue first if the entry 
is missing.
+   */
+  @Override
+  protected Table loadSparkTable(Identifier ident) {
+    try {
+      org.apache.gravitino.rel.Table gravitinoTable = 
loadGravitinoTable(ident);
+      if (isIcebergTable(gravitinoTable)) {
+        return loadIcebergSparkTable(ident, getOrCreateIcebergGlueCatalog());
+      }
+      syncNamespaceToDerby(ident.namespace());

Review Comment:
    Using AWSGlueDataCatalogHiveClientFactory to replace the Hive metastore 
client in Spark is not feasible for Gravitino because:
   
     1. Requires patching Hive: The hive.metastore.client.factory.class 
mechanism does not exist in standard Apache Hive. It requires
     manually applying the AWS-private patch HIVE-12679 to Hive (the fork 
embedded in Spark) and rebuilding it from source.
     2. JAR not in Maven Central: aws-glue-datacatalog-spark-client depends on 
the patched Hive  and is not published to
     Maven Central. It must be manually compiled and distributed to every Spark 
node's classpath.
     3. EMR-only in practice: This approach works out of the box only on AWS 
EMR, where Amazon pre-applies the patch and pre-installs the
     JARs. It cannot be used in standard open-source Spark environments.
   
     As an Apache project, Gravitino cannot depend on non-Maven-Central 
artifacts or require users to maintain a patched Hive build. The
     current Derby sync approach, while more complex in code, works with 
standard unmodified Spark and Hive distributions and imposes no extra runtime 
dependencies on users.



-- 
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]

Reply via email to