vladislav-sidorovich commented on code in PR #15407: URL: https://github.com/apache/iceberg/pull/15407#discussion_r3571285634
########## delta-lake/src/integration/java/org/apache/iceberg/delta/TestSnapshotDeltaLakeKernelTable.java: ########## @@ -0,0 +1,351 @@ +/* + * 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 static org.assertj.core.api.Assertions.assertThat; + +import io.delta.kernel.defaults.engine.DefaultEngine; +import java.io.File; +import java.io.IOException; +import java.nio.file.Files; +import java.nio.file.Paths; +import java.sql.Timestamp; +import java.util.Arrays; +import java.util.List; +import java.util.Map; +import java.util.concurrent.ThreadLocalRandom; +import org.apache.iceberg.Snapshot; +import org.apache.iceberg.SnapshotRef; +import org.apache.iceberg.Table; +import org.apache.iceberg.catalog.TableIdentifier; +import org.apache.iceberg.relocated.com.google.common.collect.ImmutableMap; +import org.apache.iceberg.relocated.com.google.common.collect.Lists; +import org.apache.iceberg.spark.Spark3Util; +import org.apache.iceberg.spark.SparkCatalog; +import org.apache.iceberg.util.LocationUtil; +import org.apache.spark.sql.DataFrameWriter; +import org.apache.spark.sql.Dataset; +import org.apache.spark.sql.Row; +import org.apache.spark.sql.RowFactory; +import org.apache.spark.sql.SaveMode; +import org.apache.spark.sql.connector.catalog.CatalogPlugin; +import org.apache.spark.sql.delta.catalog.DeltaCatalog; +import org.apache.spark.sql.types.DataTypes; +import org.apache.spark.sql.types.Metadata; +import org.apache.spark.sql.types.StructField; +import org.apache.spark.sql.types.StructType; +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; + +public class TestSnapshotDeltaLakeKernelTable extends SparkDeltaLakeSnapshotTestBase { + private static final String NAMESPACE = "delta_conversion_test_ns"; + private static final String DEFAULT_SPARK_CATALOG = "spark_catalog"; + private static final String ICEBERG_CATALOG_NAME = "iceberg_hive"; + private static final Map<String, String> BASE_SPARK_CONFIG = + ImmutableMap.of( + "type", + "hive", + "default-namespace", + "default", + "parquet-enabled", + "true", + "cache-enabled", + "false" // Spark will delete tables using v1, leaving the cache out of sync + ); + + private static Dataset<Row> genericDataFrame; + + @TempDir private File sourceLocation; + @TempDir private File destinationLocation; + + public TestSnapshotDeltaLakeKernelTable() { + super(ICEBERG_CATALOG_NAME, SparkCatalog.class.getName(), BASE_SPARK_CONFIG); + spark.conf().set("spark.sql.catalog." + DEFAULT_SPARK_CATALOG, DeltaCatalog.class.getName()); + } + + @BeforeAll + public static void beforeClass() { + spark.sql(String.format("CREATE DATABASE IF NOT EXISTS %s", NAMESPACE)); + StructType schema = + new StructType( + new StructField[] { + new StructField("id", DataTypes.IntegerType, false, Metadata.empty()), + new StructField("created_at", DataTypes.TimestampType, true, Metadata.empty()), + new StructField("event_name", DataTypes.StringType, true, Metadata.empty()), + new StructField("is_active", DataTypes.BooleanType, true, Metadata.empty()), + new StructField("price", DataTypes.DoubleType, true, Metadata.empty()) + }); + List<Row> data = + Arrays.asList( + RowFactory.create(1, Timestamp.valueOf("2025-01-01 10:00:00"), "Signup", true, 0.00), + RowFactory.create( + 2, Timestamp.valueOf("2025-01-02 14:30:00"), "Purchase", true, 199.99), + RowFactory.create( + 3, Timestamp.valueOf("2025-01-03 09:15:00"), "Deactivation", false, 0.00), + RowFactory.create(4, Timestamp.valueOf("2025-01-03 09:16:00"), "Refund", false, 0.00), + RowFactory.create(5, Timestamp.valueOf("2025-01-03 09:17:00"), "Refund", false, 0.00)); + genericDataFrame = spark.createDataFrame(data, schema); + } + + @AfterAll + public static void afterClass() { + spark.sql(String.format("DROP DATABASE IF EXISTS %s CASCADE", NAMESPACE)); + } + + @Test + public void testBasicPartitionedInsertsOnly() { + String sourceTable = toFullTableName(DEFAULT_SPARK_CATALOG, "partitioned_table"); + String sourceTableLocation = sourceLocation.toURI().toString(); + + writeDeltaTable(genericDataFrame, sourceTable, sourceTableLocation, "id"); + spark.sql("INSERT INTO " + sourceTable + " VALUES (10, current_date(), null, null, null);"); + spark.sql("INSERT INTO " + sourceTable + " VALUES (11, current_date(), null, null, null);"); + spark.sql("INSERT INTO " + sourceTable + " VALUES (12, current_date(), null, null, null);"); + + String newTableIdentifier = toFullTableName(ICEBERG_CATALOG_NAME, "iceberg_partitioned_table"); + + // Act + SnapshotDeltaLakeTable conversionAction = + DeltaLakeToIcebergMigrationSparkIntegration.snapshotDeltaLakeKernelTable( + spark, newTableIdentifier, sourceTableLocation); + SnapshotDeltaLakeTable.Result result = conversionAction.execute(); + + // Assert + checkLatestSnapshotIntegrity(sourceTable, newTableIdentifier); + checkTagContentAndOrder(sourceTable, sourceTableLocation, newTableIdentifier, 0); + checkIcebergTableLocation(newTableIdentifier, sourceTableLocation); + } + + @Test + public void testInsertUpdateDeleteSqls() { + String sourceTable = toFullTableName(DEFAULT_SPARK_CATALOG, "crud_table"); + String sourceTableLocation = sourceLocation.toURI().toString(); + + writeDeltaTable(genericDataFrame, sourceTable, sourceTableLocation, "id"); + spark.sql("INSERT INTO " + sourceTable + " VALUES (10, current_date(), null, null, null);"); + spark.sql("DELETE FROM " + sourceTable + " WHERE id=3;"); + spark.sql("UPDATE " + sourceTable + " SET id=3 WHERE id=1;"); + spark.sql("INSERT INTO " + sourceTable + " VALUES (11, current_date(), null, null, null);"); + + String newTableIdentifier = toFullTableName(ICEBERG_CATALOG_NAME, "iceberg_crud_table"); + + // Act + SnapshotDeltaLakeTable conversionAction = + DeltaLakeToIcebergMigrationSparkIntegration.snapshotDeltaLakeKernelTable( + spark, newTableIdentifier, sourceTableLocation); + SnapshotDeltaLakeTable.Result result = conversionAction.execute(); + + // Assert + checkLatestSnapshotIntegrity(sourceTable, newTableIdentifier); + checkTagContentAndOrder(sourceTable, sourceTableLocation, newTableIdentifier, 0); + checkIcebergTableLocation(newTableIdentifier, sourceTableLocation); + } + + @Test + public void testConversionAfterVacuum() throws IOException { + String sourceTable = toFullTableName(DEFAULT_SPARK_CATALOG, "vacuumed_table"); + String sourceTableLocation = sourceLocation.toURI().toString(); + + writeDeltaTable(genericDataFrame, sourceTable, sourceTableLocation, "id"); + for (int i = 0; i < 5; i++) { + spark.sql( + "UPDATE " + + sourceTable + + " SET price=" + + ThreadLocalRandom.current().nextDouble(1000) + + " where id=" + + i + + ";"); + } + spark.sql("UPDATE " + sourceTable + " SET created_at=current_date() ;"); + spark.sql("INSERT INTO " + sourceTable + " VALUES (10, current_date(), null, null, null);"); + spark.sql("INSERT INTO " + sourceTable + " VALUES (11, current_date(), null, null, null);"); + spark.sql("DELETE FROM " + sourceTable + " WHERE id>=10;"); + spark.sql("VACUUM " + sourceTable + " RETAIN 0 HOURS"); + spark.sql( + "INSERT INTO " + sourceTable + " VALUES (12, current_date(), 'after_vacuum', null, null);"); + spark.sql("UPDATE " + sourceTable + " SET id=13 WHERE id=5;"); + + // Checkpoint generated. Simulate logs clean-up + assertThat(deleteDeltaLogFile("00000000000000000000.json")).isTrue(); + assertThat(deleteDeltaLogFile("00000000000000000001.json")).isTrue(); + assertThat(deleteDeltaLogFile("00000000000000000002.json")).isTrue(); Review Comment: You are right, I reproduced the edge case scenario. Test: ``` String sourceTable = toFullTableName(DEFAULT_SPARK_CATALOG, "vacuumed_table"); String sourceTableLocation = sourceLocation.toURI().toString(); writeDeltaTable(genericDataFrame, sourceTable, sourceTableLocation, "id"); spark.sql("UPDATE " + sourceTable + " SET created_at=current_date() ;"); spark.sql("VACUUM " + sourceTable + " RETAIN 0 HOURS"); ``` I explicitly added it here https://github.com/apache/iceberg/issues/17146 I will handle this edge case as well. -- 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] --------------------------------------------------------------------- To unsubscribe, e-mail: [email protected] For additional commands, e-mail: [email protected]
