aokolnychyi commented on a change in pull request #2500:
URL: https://github.com/apache/iceberg/pull/2500#discussion_r617900532



##########
File path: 
spark3/src/test/java/org/apache/iceberg/spark/TestFileRewriteCoordinator.java
##########
@@ -0,0 +1,234 @@
+/*
+ * 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.spark;
+
+import java.io.IOException;
+import java.util.List;
+import java.util.Map;
+import java.util.UUID;
+import org.apache.iceberg.FileScanTask;
+import org.apache.iceberg.Table;
+import org.apache.iceberg.io.CloseableIterable;
+import org.apache.iceberg.relocated.com.google.common.collect.ImmutableMap;
+import org.apache.iceberg.relocated.com.google.common.collect.ImmutableSet;
+import org.apache.iceberg.relocated.com.google.common.collect.Iterables;
+import org.apache.iceberg.relocated.com.google.common.collect.Lists;
+import org.apache.iceberg.spark.source.SimpleRecord;
+import org.apache.spark.sql.Dataset;
+import org.apache.spark.sql.Encoders;
+import org.apache.spark.sql.Row;
+import org.apache.spark.sql.catalyst.analysis.NoSuchTableException;
+import org.junit.After;
+import org.junit.Assert;
+import org.junit.Test;
+
+public class TestFileRewriteCoordinator extends SparkCatalogTestBase {
+
+  public TestFileRewriteCoordinator(String catalogName, String implementation, 
Map<String, String> config) {
+    super(catalogName, implementation, config);
+  }
+
+  @After
+  public void removeTables() {
+    sql("DROP TABLE IF EXISTS %s", tableName);
+  }
+
+  @Test
+  public void testBinPackRewrite() throws NoSuchTableException, IOException {
+    sql("CREATE TABLE %s (id INT, data STRING) USING iceberg", tableName);
+
+    Dataset<Row> df = newDF(1000);
+    df.coalesce(1).writeTo(tableName).append();
+    df.coalesce(1).writeTo(tableName).append();
+    df.coalesce(1).writeTo(tableName).append();
+    df.coalesce(1).writeTo(tableName).append();
+
+    Table table = validationCatalog.loadTable(tableIdent);
+    Assert.assertEquals("Should produce 4 snapshots", 4, 
Iterables.size(table.snapshots()));
+
+    Dataset<Row> fileDF = 
spark.read().format("iceberg").load(tableName(tableIdent.name() + ".files"));
+    List<Long> fileSizes = 
fileDF.select("file_size_in_bytes").as(Encoders.LONG()).collectAsList();
+    long avgFileSize = fileSizes.stream().mapToLong(i -> i).sum() / 
fileSizes.size();
+
+    try (CloseableIterable<FileScanTask> fileScanTasks = 
table.newScan().planFiles()) {
+      String fileSetID = UUID.randomUUID().toString();
+
+      FileScanTaskSetManager taskSetManager = FileScanTaskSetManager.get();
+      taskSetManager.stageTasks(table, fileSetID, 
Lists.newArrayList(fileScanTasks));
+
+      // read and pack original 4 files into 2 splits
+      Dataset<Row> scanDF = spark.read().format("iceberg")
+          .option(SparkReadOptions.FILE_SCAN_TASK_SET_ID, fileSetID)
+          .option(SparkReadOptions.SPLIT_SIZE, Long.toString(avgFileSize * 2))
+          .option(SparkReadOptions.FILE_OPEN_COST, "0")
+          .load(tableName);
+
+      // write the packed data into new files where each split becomes a new 
file
+      scanDF.writeTo(tableName)
+          .option(SparkWriteOptions.REWRITTEN_FILE_SCAN_TASK_SET_ID, fileSetID)
+          .append();
+
+      // commit the rewrite
+      FileRewriteCoordinator rewriteCoordinator = FileRewriteCoordinator.get();
+      rewriteCoordinator.commitRewrite(table, fileSetID);
+    }
+
+    table.refresh();
+
+    Map<String, String> summary = table.currentSnapshot().summary();
+    Assert.assertEquals("Deleted files count must match", "4", 
summary.get("deleted-data-files"));
+    Assert.assertEquals("Added files count must match", "2", 
summary.get("added-data-files"));
+
+    Object rowCount = scalarSql("SELECT count(*) FROM %s", tableName);
+    Assert.assertEquals("Row count must match", 4000L, rowCount);
+  }
+
+  @Test
+  public void testSortRewrite() throws NoSuchTableException, IOException {
+    sql("CREATE TABLE %s (id INT, data STRING) USING iceberg", tableName);
+
+    Dataset<Row> df = newDF(1000);
+    df.coalesce(1).writeTo(tableName).append();
+    df.coalesce(1).writeTo(tableName).append();
+    df.coalesce(1).writeTo(tableName).append();
+    df.coalesce(1).writeTo(tableName).append();
+
+    Table table = validationCatalog.loadTable(tableIdent);
+    Assert.assertEquals("Should produce 4 snapshots", 4, 
Iterables.size(table.snapshots()));
+
+    try (CloseableIterable<FileScanTask> fileScanTasks = 
table.newScan().planFiles()) {
+      String fileSetID = UUID.randomUUID().toString();
+
+      FileScanTaskSetManager taskSetManager = FileScanTaskSetManager.get();
+      taskSetManager.stageTasks(table, fileSetID, 
Lists.newArrayList(fileScanTasks));
+
+      // read original 4 files as 4 splits
+      Dataset<Row> scanDF = spark.read().format("iceberg")
+          .option(SparkReadOptions.FILE_SCAN_TASK_SET_ID, fileSetID)
+          .option(SparkReadOptions.SPLIT_SIZE, "134217728")
+          .option(SparkReadOptions.FILE_OPEN_COST, "134217728")
+          .load(tableName);
+
+      // make sure we disable AQE and set the number of shuffle partitions as 
the target num files
+      ImmutableMap<String, String> sqlConf = ImmutableMap.of(
+          "spark.sql.shuffle.partitions", "2",
+          "spark.sql.adaptive.enabled", "false"
+      );
+
+      withSQLConf(sqlConf, () -> {
+        try {
+          // write new files with sorted records
+          scanDF.sort("id").writeTo(tableName)

Review comment:
       That's how the sort-based rewrites will look like. 




-- 
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:
[email protected]



---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to