ashvina commented on code in PR #589:
URL: https://github.com/apache/incubator-xtable/pull/589#discussion_r1875037190


##########
xtable-core/src/test/java/org/apache/xtable/delta/ITDeltaDeleteVectorConvert.java:
##########
@@ -0,0 +1,155 @@
+/*
+ * 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.xtable.delta;
+
+import static org.junit.jupiter.api.Assertions.assertEquals;
+
+import java.nio.file.Path;
+import java.util.ArrayList;
+import java.util.List;
+import java.util.stream.Collectors;
+
+import org.apache.hadoop.conf.Configuration;
+import org.apache.spark.serializer.KryoSerializer;
+import org.apache.spark.sql.Row;
+import org.apache.spark.sql.SparkSession;
+import org.junit.jupiter.api.AfterAll;
+import org.junit.jupiter.api.BeforeAll;
+import org.junit.jupiter.api.BeforeEach;
+import org.junit.jupiter.api.Test;
+import org.junit.jupiter.api.io.TempDir;
+
+import org.apache.xtable.GenericTable;
+import org.apache.xtable.TestSparkDeltaTable;
+import org.apache.xtable.model.TableChange;
+
+public class ITDeltaDeleteVectorConvert {
+  @TempDir private static Path tempDir;
+  private static SparkSession sparkSession;
+
+  private DeltaConversionSourceProvider conversionSourceProvider;
+
+  @BeforeAll
+  public static void setupOnce() {
+    sparkSession =
+        SparkSession.builder()
+            .appName("TestDeltaTable")
+            .master("local[4]")
+            .config("spark.sql.extensions", 
"io.delta.sql.DeltaSparkSessionExtension")
+            .config(
+                "spark.sql.catalog.spark_catalog",
+                "org.apache.spark.sql.delta.catalog.DeltaCatalog")
+            .config("spark.databricks.delta.retentionDurationCheck.enabled", 
"false")
+            .config("spark.databricks.delta.schema.autoMerge.enabled", "true")
+            .config("spark.sql.shuffle.partitions", "1")
+            .config("spark.default.parallelism", "1")
+            .config("spark.serializer", KryoSerializer.class.getName())
+            .getOrCreate();
+  }
+
+  @AfterAll
+  public static void teardown() {
+    if (sparkSession != null) {
+      sparkSession.close();
+    }
+  }
+
+  @BeforeEach
+  void setUp() {
+    Configuration hadoopConf = new Configuration();
+    hadoopConf.set("fs.defaultFS", "file:///");
+
+    conversionSourceProvider = new DeltaConversionSourceProvider();
+    conversionSourceProvider.init(hadoopConf);
+  }
+
+  @Test
+  public void testInsertsUpsertsAndDeletes() {
+    String tableName = GenericTable.getTableName();
+    TestSparkDeltaTable testSparkDeltaTable =
+        new TestSparkDeltaTable(tableName, tempDir, sparkSession, null, false);
+
+    List<List<String>> allActiveFiles = new ArrayList<>();
+    List<TableChange> allTableChanges = new ArrayList<>();
+    List<Row> rows = testSparkDeltaTable.insertRows(50);
+    Long timestamp1 = testSparkDeltaTable.getLastCommitTimestamp();
+    allActiveFiles.add(testSparkDeltaTable.getAllActiveFiles());
+
+    // enable deletion vectors for the test table
+    testSparkDeltaTable
+        .getSparkSession()
+        .sql(
+            "ALTER TABLE "
+                + tableName
+                + " SET TBLPROPERTIES ('delta.enableDeletionVectors' = true)");
+
+    List<Row> rows1 = testSparkDeltaTable.insertRows(50);
+    allActiveFiles.add(testSparkDeltaTable.getAllActiveFiles());
+    assertEquals(100L, testSparkDeltaTable.getNumRows());
+
+    // upsert does not create delete vectors
+    testSparkDeltaTable.upsertRows(rows.subList(0, 20));
+    allActiveFiles.add(testSparkDeltaTable.getAllActiveFiles());
+    assertEquals(100L, testSparkDeltaTable.getNumRows());
+
+    testSparkDeltaTable.insertRows(50);
+    allActiveFiles.add(testSparkDeltaTable.getAllActiveFiles());
+    assertEquals(150L, testSparkDeltaTable.getNumRows());
+
+    // delete a few rows with gaps in ids
+    List<Row> rowsToDelete =
+        rows1.subList(0, 10).stream()
+            .filter(row -> (row.get(0).hashCode() % 2) == 0)
+            .collect(Collectors.toList());
+    rowsToDelete.addAll(rows.subList(35, 45));
+    testSparkDeltaTable.deleteRows(rowsToDelete);
+    allActiveFiles.add(testSparkDeltaTable.getAllActiveFiles());
+    assertEquals(135L, testSparkDeltaTable.getNumRows());
+
+    testSparkDeltaTable.insertRows(50);
+    allActiveFiles.add(testSparkDeltaTable.getAllActiveFiles());
+    assertEquals(185L, testSparkDeltaTable.getNumRows());
+
+    // delete a few rows from a file which already has a deletion vector, this 
should generate a
+    // merged deletion vector file. Some rows were already deleted in the 
previous delete step.
+    // This deletion step intentionally deletes the same rows again to test 
the merge.
+    rowsToDelete = rows1.subList(5, 15);
+    testSparkDeltaTable.deleteRows(rowsToDelete);
+    allActiveFiles.add(testSparkDeltaTable.getAllActiveFiles());
+    assertEquals(178L, testSparkDeltaTable.getNumRows());
+
+    testSparkDeltaTable.insertRows(50);
+    allActiveFiles.add(testSparkDeltaTable.getAllActiveFiles());
+    assertEquals(228L, testSparkDeltaTable.getNumRows());
+
+    // TODO conversion fails if delete vectors are enabled, this is because of 
missing handlers for

Review Comment:
   Currently, XTable doesn't identify and convert deletion vectors. There are 
two different representations of delete vectors in Delta and Iceberg today, and 
we can expect new representation to be added in the coming months. XTable will 
need to understand and convert all of these. Right now, XTable conversion isn't 
accurate because it lacks deletion vector support. I'm collaborating with new 
community members who are interested in adding this support. This change 
provides some utilities to help them get started. As we make progress, I can 
work on a document to explain the current formats and how XTable handles them.



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