gh-yzou commented on code in PR #1303: URL: https://github.com/apache/polaris/pull/1303#discussion_r2029667733
########## plugins/spark/v3.5/src/main/java/org/apache/polaris/spark/utils/PolarisCatalogUtils.java: ########## @@ -0,0 +1,125 @@ +/* + * 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.polaris.spark.utils; + +import com.google.common.collect.Maps; +import java.lang.reflect.Field; +import java.util.Map; +import org.apache.iceberg.CachingCatalog; +import org.apache.iceberg.catalog.Catalog; +import org.apache.iceberg.catalog.TableIdentifier; +import org.apache.iceberg.exceptions.NoSuchTableException; +import org.apache.iceberg.rest.RESTCatalog; +import org.apache.iceberg.rest.RESTSessionCatalog; +import org.apache.iceberg.rest.auth.OAuth2Util; +import org.apache.iceberg.spark.SparkCatalog; +import org.apache.polaris.service.types.GenericTable; +import org.apache.spark.sql.SparkSession; +import org.apache.spark.sql.connector.catalog.Table; +import org.apache.spark.sql.connector.catalog.TableCatalog; +import org.apache.spark.sql.connector.catalog.TableProvider; +import org.apache.spark.sql.execution.datasources.DataSource; +import org.apache.spark.sql.execution.datasources.v2.DataSourceV2Utils; +import org.apache.spark.sql.types.StructType; +import org.apache.spark.sql.util.CaseInsensitiveStringMap; + +public class PolarisCatalogUtils { + public static final String TABLE_PROVIDER_KEY = "provider"; + public static final String TABLE_PATH_KEY = "path"; + + // whether enable the inMemory catalogs, used by testing. + public static final String ENABLE_IN_MEMORY_CATALOG_KEY = "enable_in_memory_catalog"; + + public static void checkIdentifierIsValid(TableIdentifier tableIdentifier) { + if (tableIdentifier.namespace().isEmpty()) { + throw new NoSuchTableException("Invalid table identifier: %s", tableIdentifier); + } + } + + /** Check whether the table provider is iceberg. */ + public static boolean useIceberg(String provider) { + return provider == null || "iceberg".equalsIgnoreCase(provider); + } + + /** Check whether the table provider is delta. */ + public static boolean useDelta(String provider) { + return "delta".equalsIgnoreCase(provider); + } + + /** + * Load spark table using DataSourceV2. + * + * @return V2Table if DataSourceV2 is available for the table format. For delta table, it returns + * DeltaTableV2. + */ + public static Table loadSparkTable(GenericTable genericTable) { + SparkSession sparkSession = SparkSession.active(); + TableProvider provider = + DataSource.lookupDataSourceV2(genericTable.getFormat(), sparkSession.sessionState().conf()) + .get(); + Map<String, String> properties = genericTable.getProperties(); + boolean hasLocationClause = properties.get(TableCatalog.PROP_LOCATION) != null; + boolean hasPathClause = properties.get(TABLE_PATH_KEY) != null; + Map<String, String> tableProperties = Maps.newHashMap(); + tableProperties.putAll(properties); + if (!hasPathClause && hasLocationClause) { + // DataSourceV2 requires the path property on table loading. However, spark today + // doesn't create the corresponding path property if the path keyword is not + // provided by user when location is provided. Here, we duplicate the location + // property as path to make sure the table can be loaded. + tableProperties.put(TABLE_PATH_KEY, properties.get(TableCatalog.PROP_LOCATION)); + } + CaseInsensitiveStringMap property_map = new CaseInsensitiveStringMap(tableProperties); + return DataSourceV2Utils.getTableFromProvider( + provider, property_map, scala.Option$.MODULE$.<StructType>empty()); + } + + /** + * Get the catalogAuth field inside the RESTSessionCatalog used by Iceberg Spark Catalog use + * reflection. TODO: Deprecate this function once the iceberg client is updated to 1.9.0 to use + * AuthManager and the capability of injecting an AuthManger is available. Related iceberg PR: + * https://github.com/apache/iceberg/pull/12655 + */ + public static OAuth2Util.AuthSession getAuthSession(SparkCatalog sparkCatalog) { + try { + Field icebergCatalogField = sparkCatalog.getClass().getDeclaredField("icebergCatalog"); Review Comment: I think even without the auth injection change, i might still have a way to reuse the AuthManager. Basically, i can provide a customized AuthManager implementation that have a member points to a statically initialized AuthManager, which is not so intuitive. -- 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]
