You are viewing a plain text version of this content. The canonical link for it is here.
Posted to issues@iceberg.apache.org by GitBox <gi...@apache.org> on 2020/12/03 19:13:16 UTC

[GitHub] [iceberg] jackye1995 commented on a change in pull request #1783: Custom catalogs from `IcebergSource`

jackye1995 commented on a change in pull request #1783:
URL: https://github.com/apache/iceberg/pull/1783#discussion_r535503947



##########
File path: spark3/src/main/java/org/apache/iceberg/spark/source/IcebergSource.java
##########
@@ -56,48 +62,62 @@ public boolean supportsExternalMetadata() {
   }
 
   @Override
-  public SparkTable getTable(StructType schema, Transform[] partitioning, Map<String, String> options) {
-    // Get Iceberg table from options
-    Configuration conf = SparkSession.active().sessionState().newHadoopConf();
-    Table icebergTable = getTableAndResolveHadoopConfiguration(options, conf);
-
-    // Build Spark table based on Iceberg table, and return it
-    // Eagerly refresh the table before reading to ensure views containing this table show up-to-date data
-    return new SparkTable(icebergTable, schema, true);
+  public Table getTable(StructType schema, Transform[] partitioning, Map<String, String> options) {
+    String catalogName = extractCatalog(new CaseInsensitiveStringMap(options));
+    Identifier ident = extractIdentifier(new CaseInsensitiveStringMap(options));
+    CatalogManager catalogManager = SparkSession.active().sessionState().catalogManager();
+    CatalogPlugin catalog = catalogManager.catalog(catalogName);
+    try {
+      if (catalog instanceof TableCatalog) {
+        return ((TableCatalog) catalog).loadTable(ident);
+      }
+    } catch (NoSuchTableException e) {
+      // throwing an iceberg NoSuchTableException because the Spark one is typed and cant be thrown from this interface
+      throw new org.apache.iceberg.exceptions.NoSuchTableException(e, "Cannot find table for %s.", ident);
+    }
+    // throwing an iceberg NoSuchTableException because the Spark one is typed and cant be thrown from this interface
+    throw new org.apache.iceberg.exceptions.NoSuchTableException("Cannot find table for %s.", ident);
   }
 
-  protected Table findTable(Map<String, String> options, Configuration conf) {
+  private Pair<String, TableIdentifier> tableIdentifier(CaseInsensitiveStringMap options) {
+    CatalogManager catalogManager = SparkSession.active().sessionState().catalogManager();
+    String currentCatalogName = catalogManager.currentCatalog().name();
+    Namespace defaultNamespace = Namespace.of(catalogManager.currentNamespace());
     Preconditions.checkArgument(options.containsKey("path"), "Cannot open table: path is not set");
     String path = options.get("path");
+    List<String> ident;
+    try {
+      ident = scala.collection.JavaConverters.seqAsJavaList(SparkSession.active().sessionState().sqlParser().parseMultipartIdentifier(path));
+    } catch (ParseException e) {
+      ident = new ArrayList<>();
+      ident.add(path);
+    }
 
-    if (path.contains("/")) {
-      HadoopTables tables = new HadoopTables(conf);
-      return tables.load(path);
+    if (ident.size() == 1) {
+      return Pair.of(currentCatalogName, TableIdentifier.of(defaultNamespace, ident.get(0)));
+    } else if (ident.size() == 2) {

Review comment:
       Because we are now assigning an empty namespace for `size==2` case,  think it can be merged together with the `else` case. We only get `ident.subList(1, ident.size()).toArray(new String[0])` simplified to `ident.get(1)`, but the rest are all duplicates.

##########
File path: spark3/src/test/java/org/apache/iceberg/spark/source/TestIcebergSource.java
##########
@@ -1,27 +1,23 @@
 /*
- * Licensed to the Apache Software Foundation (ASF) under one

Review comment:
       nit: should not change license




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

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



---------------------------------------------------------------------
To unsubscribe, e-mail: issues-unsubscribe@iceberg.apache.org
For additional commands, e-mail: issues-help@iceberg.apache.org