forked from apache/iceberg
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Spark 3.5: Support metadata columns in staged scan (apache#8872)
- Loading branch information
1 parent
642d47d
commit 3b4da15
Showing
3 changed files
with
183 additions
and
6 deletions.
There are no files selected for viewing
127 changes: 127 additions & 0 deletions
127
.../test/java/org/apache/iceberg/spark/extensions/TestMetaColumnProjectionWithStageScan.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,127 @@ | ||
/* | ||
* 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.extensions; | ||
|
||
import java.util.List; | ||
import java.util.Map; | ||
import java.util.UUID; | ||
import org.apache.iceberg.ScanTask; | ||
import org.apache.iceberg.Table; | ||
import org.apache.iceberg.io.CloseableIterable; | ||
import org.apache.iceberg.relocated.com.google.common.collect.ImmutableList; | ||
import org.apache.iceberg.relocated.com.google.common.collect.Lists; | ||
import org.apache.iceberg.spark.ScanTaskSetManager; | ||
import org.apache.iceberg.spark.Spark3Util; | ||
import org.apache.iceberg.spark.SparkCatalogConfig; | ||
import org.apache.iceberg.spark.SparkReadOptions; | ||
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.assertj.core.api.Assertions; | ||
import org.junit.After; | ||
import org.junit.Test; | ||
import org.junit.runners.Parameterized; | ||
|
||
public class TestMetaColumnProjectionWithStageScan extends SparkExtensionsTestBase { | ||
|
||
public TestMetaColumnProjectionWithStageScan( | ||
String catalogName, String implementation, Map<String, String> config) { | ||
super(catalogName, implementation, config); | ||
} | ||
|
||
@Parameterized.Parameters(name = "catalogName = {0}, implementation = {1}, config = {2}") | ||
public static Object[][] parameters() { | ||
return new Object[][] { | ||
{ | ||
SparkCatalogConfig.HADOOP.catalogName(), | ||
SparkCatalogConfig.HADOOP.implementation(), | ||
SparkCatalogConfig.HADOOP.properties() | ||
} | ||
}; | ||
} | ||
|
||
@After | ||
public void removeTables() { | ||
sql("DROP TABLE IF EXISTS %s", tableName); | ||
} | ||
|
||
private <T extends ScanTask> void stageTask( | ||
Table tab, String fileSetID, CloseableIterable<T> tasks) { | ||
ScanTaskSetManager taskSetManager = ScanTaskSetManager.get(); | ||
taskSetManager.stageTasks(tab, fileSetID, Lists.newArrayList(tasks)); | ||
} | ||
|
||
@Test | ||
public void testReadStageTableMeta() throws Exception { | ||
sql( | ||
"CREATE TABLE %s (id bigint, data string) USING iceberg TBLPROPERTIES" | ||
+ "('format-version'='2', 'write.delete.mode'='merge-on-read')", | ||
tableName); | ||
|
||
List<SimpleRecord> records = | ||
Lists.newArrayList( | ||
new SimpleRecord(1, "a"), | ||
new SimpleRecord(2, "b"), | ||
new SimpleRecord(3, "c"), | ||
new SimpleRecord(4, "d")); | ||
|
||
spark | ||
.createDataset(records, Encoders.bean(SimpleRecord.class)) | ||
.coalesce(1) | ||
.writeTo(tableName) | ||
.append(); | ||
|
||
Table table = Spark3Util.loadIcebergTable(spark, tableName); | ||
table.refresh(); | ||
String tableLocation = table.location(); | ||
|
||
try (CloseableIterable<ScanTask> tasks = table.newBatchScan().planFiles()) { | ||
String fileSetID = UUID.randomUUID().toString(); | ||
stageTask(table, fileSetID, tasks); | ||
Dataset<Row> scanDF2 = | ||
spark | ||
.read() | ||
.format("iceberg") | ||
.option(SparkReadOptions.FILE_OPEN_COST, "0") | ||
.option(SparkReadOptions.SCAN_TASK_SET_ID, fileSetID) | ||
.load(tableLocation); | ||
|
||
Assertions.assertThat(scanDF2.columns().length).isEqualTo(2); | ||
} | ||
|
||
try (CloseableIterable<ScanTask> tasks = table.newBatchScan().planFiles()) { | ||
String fileSetID = UUID.randomUUID().toString(); | ||
stageTask(table, fileSetID, tasks); | ||
Dataset<Row> scanDF = | ||
spark | ||
.read() | ||
.format("iceberg") | ||
.option(SparkReadOptions.FILE_OPEN_COST, "0") | ||
.option(SparkReadOptions.SCAN_TASK_SET_ID, fileSetID) | ||
.load(tableLocation) | ||
.select("*", "_pos"); | ||
|
||
List<Row> rows = scanDF.collectAsList(); | ||
ImmutableList<Object[]> expectedRows = | ||
ImmutableList.of(row(1L, "a", 0L), row(2L, "b", 1L), row(3L, "c", 2L), row(4L, "d", 3L)); | ||
assertEquals("result should match", expectedRows, rowsToJava(rows)); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters