JonasJ-ap commented on code in PR #6449:
URL: https://github.com/apache/iceberg/pull/6449#discussion_r1082097202


##########
delta-lake/src/main/java/org/apache/iceberg/delta/BaseSnapshotDeltaLakeTableAction.java:
##########
@@ -0,0 +1,370 @@
+/*
+ * 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.iceberg.delta;
+
+import io.delta.standalone.DeltaLog;
+import io.delta.standalone.VersionLog;
+import io.delta.standalone.actions.Action;
+import io.delta.standalone.actions.AddFile;
+import io.delta.standalone.actions.RemoveFile;
+import java.io.File;
+import java.net.URI;
+import java.util.Iterator;
+import java.util.List;
+import java.util.Map;
+import java.util.stream.Collectors;
+import org.apache.hadoop.conf.Configuration;
+import org.apache.iceberg.AppendFiles;
+import org.apache.iceberg.DataFile;
+import org.apache.iceberg.DataFiles;
+import org.apache.iceberg.DeleteFiles;
+import org.apache.iceberg.FileFormat;
+import org.apache.iceberg.Metrics;
+import org.apache.iceberg.MetricsConfig;
+import org.apache.iceberg.OverwriteFiles;
+import org.apache.iceberg.PartitionField;
+import org.apache.iceberg.PartitionSpec;
+import org.apache.iceberg.Schema;
+import org.apache.iceberg.SnapshotSummary;
+import org.apache.iceberg.Table;
+import org.apache.iceberg.TableProperties;
+import org.apache.iceberg.Transaction;
+import org.apache.iceberg.avro.Avro;
+import org.apache.iceberg.catalog.Catalog;
+import org.apache.iceberg.catalog.TableIdentifier;
+import org.apache.iceberg.exceptions.ValidationException;
+import org.apache.iceberg.hadoop.HadoopFileIO;
+import org.apache.iceberg.io.InputFile;
+import org.apache.iceberg.mapping.MappingUtil;
+import org.apache.iceberg.mapping.NameMapping;
+import org.apache.iceberg.mapping.NameMappingParser;
+import org.apache.iceberg.orc.OrcMetrics;
+import org.apache.iceberg.parquet.ParquetUtil;
+import org.apache.iceberg.relocated.com.google.common.base.Preconditions;
+import org.apache.iceberg.relocated.com.google.common.collect.ImmutableMap;
+import org.apache.iceberg.relocated.com.google.common.collect.Iterables;
+import org.apache.iceberg.relocated.com.google.common.collect.Lists;
+import org.apache.iceberg.types.Type;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+/**
+ * Takes a Delta Lake table's location and attempts to create an Iceberg table 
snapshot in an
+ * optional user-specified location (default to the Delta Lake table's 
location) with a different
+ * identifier.
+ */
+class BaseSnapshotDeltaLakeTableAction implements SnapshotDeltaLakeTable {
+
+  private static final Logger LOG = 
LoggerFactory.getLogger(BaseSnapshotDeltaLakeTableAction.class);
+
+  private static final String SNAPSHOT_SOURCE_PROP = "snapshot_source";
+  private static final String DELTA_SOURCE_VALUE = "delta";
+  private static final String ORIGINAL_LOCATION_PROP = "original_location";
+  private static final String PARQUET_SUFFIX = ".parquet";
+  private static final String AVRO_SUFFIX = ".avro";
+  private static final String ORC_SUFFIX = ".orc";
+  private final ImmutableMap.Builder<String, String> 
additionalPropertiesBuilder =
+      ImmutableMap.builder();
+  private DeltaLog deltaLog;
+  private Catalog icebergCatalog;
+  private final String deltaTableLocation;
+  private TableIdentifier newTableIdentifier;
+  private String newTableLocation;
+  private HadoopFileIO deltaLakeFileIO;
+
+  /**
+   * Snapshot a delta lake table to be an iceberg table. The action will read 
the delta lake table's
+   * log through the table's path, create a new iceberg table using the given 
icebergCatalog and
+   * newTableIdentifier, and commit all changes in one iceberg transaction.
+   *
+   * <p>The new table will only be created if the snapshot is successful.
+   *
+   * @param deltaTableLocation the delta lake table's path
+   */
+  BaseSnapshotDeltaLakeTableAction(String deltaTableLocation) {
+    this.deltaTableLocation = deltaTableLocation;
+    this.newTableLocation = deltaTableLocation;
+  }
+
+  @Override
+  public SnapshotDeltaLakeTable tableProperties(Map<String, String> 
properties) {
+    additionalPropertiesBuilder.putAll(properties);
+    return this;
+  }
+
+  @Override
+  public SnapshotDeltaLakeTable tableProperty(String name, String value) {
+    additionalPropertiesBuilder.put(name, value);
+    return this;
+  }
+
+  @Override
+  public SnapshotDeltaLakeTable tableLocation(String location) {
+    this.newTableLocation = location;
+    return this;
+  }
+
+  @Override
+  public SnapshotDeltaLakeTable as(TableIdentifier identifier) {
+    this.newTableIdentifier = identifier;
+    return this;
+  }
+
+  @Override
+  public SnapshotDeltaLakeTable icebergCatalog(Catalog catalog) {
+    this.icebergCatalog = catalog;
+    return this;
+  }
+
+  @Override
+  public SnapshotDeltaLakeTable deltaLakeConfiguration(Configuration conf) {
+    this.deltaLog = DeltaLog.forTable(conf, deltaTableLocation);
+    this.deltaLakeFileIO = new HadoopFileIO(conf);
+    return this;
+  }
+
+  @Override
+  public Result execute() {
+    Preconditions.checkArgument(

Review Comment:
   Thank you for your suggestion. Based on my test with 
`delta-standalone:0.6.0`, the exception will be thrown when we try to 
initialize the `DeltaLog` for table with higher reader/writer version by 
`DeltaLog.forTable(conf, deltaTableLocation)`. 
   
   Since we need `DeltaLog` to access table properties and version log of a 
delta table, It seems that additional check related to the protocol version 
will never be reached as the exception is thrown earlier. 
   
   I think we can add relevant checks and the support for column mapping when 
newer version of `delta-standalone` is published. What do you think?
   
   Here is a full stacktrace that copied from AWS Glue when I tried to snapshot 
a delta lake table using column mapping and got the 
`InvalidProtocolVersionException`:
   ```
   23/01/05 00:27:50 ERROR ProcessLauncher: Exception in User Class
   
io.delta.standalone.internal.exception.DeltaErrors$InvalidProtocolVersionException:
 
   Delta protocol version (2,5) is too new for this version of Delta
   Standalone Reader/Writer (1,2). Please upgrade to a newer release.
   
        at 
io.delta.standalone.internal.DeltaLogImpl.assertProtocolRead(DeltaLogImpl.scala:214)
 ~[delta-standalone_2.12-0.6.0.jar:0.6.0]
        at 
io.delta.standalone.internal.SnapshotImpl.<init>(SnapshotImpl.scala:272) 
~[delta-standalone_2.12-0.6.0.jar:0.6.0]
        at 
io.delta.standalone.internal.SnapshotManagement.createSnapshot(SnapshotManagement.scala:257)
 ~[delta-standalone_2.12-0.6.0.jar:0.6.0]
        at 
io.delta.standalone.internal.SnapshotManagement.getSnapshotAtInit(SnapshotManagement.scala:224)
 ~[delta-standalone_2.12-0.6.0.jar:0.6.0]
        at 
io.delta.standalone.internal.SnapshotManagement.$init$(SnapshotManagement.scala:37)
 ~[delta-standalone_2.12-0.6.0.jar:0.6.0]
        at 
io.delta.standalone.internal.DeltaLogImpl.<init>(DeltaLogImpl.scala:47) 
~[delta-standalone_2.12-0.6.0.jar:0.6.0]
        at 
io.delta.standalone.internal.DeltaLogImpl$.apply(DeltaLogImpl.scala:263) 
~[delta-standalone_2.12-0.6.0.jar:0.6.0]
        at 
io.delta.standalone.internal.DeltaLogImpl$.forTable(DeltaLogImpl.scala:241) 
~[delta-standalone_2.12-0.6.0.jar:0.6.0]
        at 
io.delta.standalone.internal.DeltaLogImpl.forTable(DeltaLogImpl.scala) 
~[delta-standalone_2.12-0.6.0.jar:0.6.0]
        at io.delta.standalone.DeltaLog.forTable(DeltaLog.java:164) 
~[delta-standalone_2.12-0.6.0.jar:0.6.0]
        at 
org.apache.iceberg.delta.BaseMigrateDeltaLakeTableAction.<init>(BaseMigrateDeltaLakeTableAction.java:104)
 ~[iceberg-spark-runtime-3.3_migrate_delta_to_iceberg.jar:?]
        at 
org.apache.iceberg.spark.actions.MigrateDeltaLakeTableSparkAction.<init>(MigrateDeltaLakeTableSparkAction.java:37)
 ~[iceberg-spark-runtime-3.3_migrate_delta_to_iceberg.jar:?]
        at 
org.apache.iceberg.spark.actions.SparkActions.migrateDeltaLakeTable(SparkActions.java:83)
 ~[iceberg-spark-runtime-3.3_migrate_delta_to_iceberg.jar:?]
        at GlueApp$.main(delta_iceberg_migration_test.scala:21) 
~[delta_iceberg_migration_test.scala.jar:?]
        at GlueApp.main(delta_iceberg_migration_test.scala) 
~[delta_iceberg_migration_test.scala.jar:?]
        at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) 
~[?:1.8.0_352]
        at 
sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) 
~[?:1.8.0_352]
        at 
sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
 ~[?:1.8.0_352]
        at java.lang.reflect.Method.invoke(Method.java:498) ~[?:1.8.0_352]
        at 
com.amazonaws.services.glue.SparkProcessLauncherPlugin.invoke(ProcessLauncher.scala:50)
 ~[AWSGlueSparkResourceManager-1.0.jar:?]
        at 
com.amazonaws.services.glue.SparkProcessLauncherPlugin.invoke$(ProcessLauncher.scala:50)
 ~[AWSGlueSparkResourceManager-1.0.jar:?]
        at 
com.amazonaws.services.glue.ProcessLauncher$$anon$1.invoke(ProcessLauncher.scala:80)
 ~[AWSGlueSparkResourceManager-1.0.jar:?]
        at 
com.amazonaws.services.glue.ProcessLauncher.launch(ProcessLauncher.scala:145) 
~[AWSGlueSparkResourceManager-1.0.jar:?]
        at 
com.amazonaws.services.glue.ProcessLauncher$.main(ProcessLauncher.scala:30) 
~[AWSGlueSparkResourceManager-1.0.jar:?]
        at 
com.amazonaws.services.glue.ProcessLauncher.main(ProcessLauncher.scala) 
~[AWSGlueSparkResourceManager-1.0.jar:?]
   ```



-- 
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: issues-unsubscr...@iceberg.apache.org

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


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

Reply via email to