pvary commented on code in PR #14435:
URL: https://github.com/apache/iceberg/pull/14435#discussion_r2665212805


##########
spark/v4.0/spark/src/test/java/org/apache/iceberg/spark/actions/TestRewriteDataFilesAction.java:
##########
@@ -2657,4 +2682,411 @@ public boolean matches(RewriteFileGroup argument) {
       return groupIDs.contains(argument.info().globalIndex());
     }
   }
+
+  @TestTemplate
+  public void testBinPackUsesCorrectRunnerBasedOnOption() {
+    Table table = createTable(4);
+    shouldHaveFiles(table, 4);
+
+    // Test that binPack() respects the configuration option
+    // When enabled, should use SparkParquetFileMergeRunner
+    RewriteDataFiles.Result resultWithMerger =
+        basicRewrite(table)
+            .option(RewriteDataFiles.USE_PARQUET_ROW_GROUP_MERGE, "true")
+            .binPack()
+            .execute();
+
+    assertThat(resultWithMerger.rewrittenDataFilesCount()).isEqualTo(4);
+    assertThat(resultWithMerger.addedDataFilesCount()).isGreaterThan(0);
+
+    // Write more data to the table so we can test again
+    writeRecords(100, SCALE);
+
+    // When disabled, should use SparkBinPackFileRewriteRunner
+    RewriteDataFiles.Result resultWithoutMerger =
+        basicRewrite(table)
+            .option(RewriteDataFiles.USE_PARQUET_ROW_GROUP_MERGE, "false")
+            .binPack()
+            .execute();
+
+    // Should rewrite the newly added files
+    assertThat(resultWithoutMerger.rewrittenDataFilesCount()).isGreaterThan(0);
+  }
+
+  @TestTemplate
+  public void testParquetFileMergerExplicitlyEnabledAndDisabled() {
+    Table table = createTable(4);
+    shouldHaveFiles(table, 4);
+
+    long countBefore = currentData().size();
+
+    // Test explicitly enabling ParquetFileMerger
+    RewriteDataFiles.Result resultEnabled =
+        basicRewrite(table)
+            .option(RewriteDataFiles.USE_PARQUET_ROW_GROUP_MERGE, "true")
+            .binPack()
+            .execute();
+
+    assertThat(resultEnabled.rewrittenDataFilesCount()).isEqualTo(4);
+    assertThat(resultEnabled.addedDataFilesCount()).isGreaterThan(0);
+    assertThat(currentData()).hasSize((int) countBefore);
+
+    // Write more data for second test
+    writeRecords(4, SCALE);
+
+    // Test explicitly disabling ParquetFileMerger
+    RewriteDataFiles.Result resultDisabled =
+        basicRewrite(table)
+            .option(RewriteDataFiles.USE_PARQUET_ROW_GROUP_MERGE, "false")
+            .binPack()
+            .execute();
+
+    assertThat(resultDisabled.rewrittenDataFilesCount()).isGreaterThan(0);
+    assertThat(resultDisabled.addedDataFilesCount()).isGreaterThan(0);
+  }
+
+  @TestTemplate
+  public void 
testParquetFileMergerProduceConsistentRowLineageWithBinPackMerger()
+      throws IOException {
+    // Test that both binpack and ParquetFileMerger convert virtual row IDs to 
physical
+    // and produce equivalent results for row lineage preservation
+    assumeThat(formatVersion).isGreaterThanOrEqualTo(3);
+
+    // Test binpack approach
+    Table binpackTable = createTable(4);
+    shouldHaveFiles(binpackTable, 4);
+    verifyInitialVirtualRowIds(binpackTable);
+    long binpackCountBefore = currentData().size();
+
+    RewriteDataFiles.Result binpackResult =
+        basicRewrite(binpackTable)
+            .option(RewriteDataFiles.USE_PARQUET_ROW_GROUP_MERGE, "false")
+            .binPack()
+            .execute();
+
+    assertThat(binpackResult.rewrittenDataFilesCount()).isEqualTo(4);
+    assertThat(binpackResult.addedDataFilesCount()).isGreaterThan(0);
+    assertThat(currentData()).hasSize((int) binpackCountBefore);
+    verifyPhysicalRowIdsAfterMerge(binpackTable, "Binpack");
+
+    // Test ParquetFileMerger approach with a different table location
+    String originalTableLocation = tableLocation;
+    tableLocation = new File(tableDir, "merger-table").toURI().toString();
+    Table mergerTable = createTable(4);
+    shouldHaveFiles(mergerTable, 4);
+    verifyInitialVirtualRowIds(mergerTable);
+    long mergerCountBefore = currentData().size();
+
+    RewriteDataFiles.Result mergerResult =
+        basicRewrite(mergerTable)
+            .option(RewriteDataFiles.USE_PARQUET_ROW_GROUP_MERGE, "true")
+            .binPack()
+            .execute();
+
+    assertThat(mergerResult.rewrittenDataFilesCount()).isEqualTo(4);
+    assertThat(mergerResult.addedDataFilesCount()).isGreaterThan(0);
+    assertThat(currentData()).hasSize((int) mergerCountBefore);
+    verifyPhysicalRowIdsAfterMerge(mergerTable, "ParquetFileMerger");
+
+    // Restore original table location
+    tableLocation = originalTableLocation;
+
+    // Verify both approaches produce equivalent results
+    assertThat(binpackCountBefore)
+        .as("Both tables should have same initial record count")
+        .isEqualTo(mergerCountBefore);
+    assertThat(binpackResult.addedDataFilesCount())
+        .as("Both approaches should produce same number of output files")
+        .isEqualTo(mergerResult.addedDataFilesCount());
+  }
+
+  private void verifyInitialVirtualRowIds(Table table) throws IOException {
+    List<DataFile> dataFiles = TestHelpers.dataFiles(table);
+    assertThat(dataFiles).isNotEmpty();
+    for (DataFile dataFile : dataFiles) {
+      assertThat(dataFile.firstRowId())
+          .as("Files should have virtual row IDs (first_row_id != null)")
+          .isNotNull();
+
+      // Verify files don't have physical _row_id column
+      ParquetFileReader reader =
+          ParquetFileReader.open(
+              HadoopInputFile.fromPath(
+                  new Path(dataFile.path().toString()), 
spark.sessionState().newHadoopConf()));
+      MessageType schema = reader.getFooter().getFileMetaData().getSchema();
+      assertThat(schema.containsField("_row_id"))
+          .as("Files should not have physical _row_id column before merge")
+          .isFalse();
+      reader.close();
+    }
+  }
+
+  private void verifyPhysicalRowIdsAfterMerge(Table table, String approach) 
throws IOException {
+    List<DataFile> dataFiles = TestHelpers.dataFiles(table);
+    assertThat(dataFiles).isNotEmpty();
+    for (DataFile dataFile : dataFiles) {
+      assertThat(dataFile.firstRowId())
+          .as(approach + " should extract firstRowId from min(_row_id) column 
statistics")
+          .isNotNull();
+
+      // Verify files have physical _row_id column
+      ParquetFileReader reader =
+          ParquetFileReader.open(
+              HadoopInputFile.fromPath(
+                  new Path(dataFile.path().toString()), 
spark.sessionState().newHadoopConf()));
+      MessageType schema = reader.getFooter().getFileMetaData().getSchema();
+      assertThat(schema.containsField("_row_id"))
+          .as(approach + " should write physical _row_id column")
+          .isTrue();
+      reader.close();
+    }
+  }
+
+  @TestTemplate
+  public void testParquetFileMergerPreservesPhysicalRowIds() throws 
IOException {
+    // Test scenario 2: Tables with physical _row_id column
+    // After merging, the physical _row_id should be preserved (not changed)
+    assumeThat(formatVersion).isGreaterThanOrEqualTo(3);

Review Comment:
   Or if we decide it is worth to test for the `useParquetFileMerger` `false` 
case as well, we could just set `USE_PARQUET_ROW_GROUP_MERGE` based on 
`useParquetFileMerger`



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

Reply via email to