Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 1 addition & 2 deletions core/src/main/java/org/apache/iceberg/AllManifestsTable.java
Original file line number Diff line number Diff line change
Expand Up @@ -145,8 +145,7 @@ protected CloseableIterable<FileScanTask> doPlanFiles() {
snap.snapshotId());
} else {
return StaticDataTask.of(
io.newInputFile(
((BaseTable) table()).operations().current().metadataFileLocation()),
((BaseTable) table()).operations().current().metadataFileLocation(),
MANIFEST_FILE_SCHEMA,
schema(),
snap.allManifests(io),
Expand Down
2 changes: 1 addition & 1 deletion core/src/main/java/org/apache/iceberg/HistoryTable.java
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ MetadataTableType metadataTableType() {

private DataTask task(TableScan scan) {
return StaticDataTask.of(
table().io().newInputFile(table().operations().current().metadataFileLocation()),
table().operations().current().metadataFileLocation(),
schema(),
scan.schema(),
table().history(),
Expand Down
3 changes: 1 addition & 2 deletions core/src/main/java/org/apache/iceberg/ManifestsTable.java
Original file line number Diff line number Diff line change
Expand Up @@ -81,8 +81,7 @@ protected DataTask task(TableScan scan) {
Map<Integer, PartitionSpec> specs = Maps.newHashMap(table().specs());

return StaticDataTask.of(
io.newInputFile(
location != null ? location : table().operations().current().metadataFileLocation()),
location != null ? location : table().operations().current().metadataFileLocation(),
schema(),
scan.schema(),
scan.snapshot().allManifests(io),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ private DataTask task(TableScan scan) {
new TableMetadata.MetadataLogEntry(
current.lastUpdatedMillis(), current.metadataFileLocation()));
return StaticDataTask.of(
table().io().newInputFile(current.metadataFileLocation()),
current.metadataFileLocation(),
schema(),
scan.schema(),
metadataLogEntries,
Expand Down
4 changes: 2 additions & 2 deletions core/src/main/java/org/apache/iceberg/PartitionsTable.java
Original file line number Diff line number Diff line change
Expand Up @@ -153,7 +153,7 @@ private DataTask task(StaticTableScan scan) {
if (unpartitionedTable) {
// the table is unpartitioned, partitions contains only the root partition
return StaticDataTask.of(
io().newInputFile(table().operations().current().metadataFileLocation()),
table().operations().current().metadataFileLocation(),
schema(),
scan.schema(),
partitions,
Expand All @@ -170,7 +170,7 @@ private DataTask task(StaticTableScan scan) {
root.lastUpdatedSnapshotId));
} else {
return StaticDataTask.of(
io().newInputFile(table().operations().current().metadataFileLocation()),
table().operations().current().metadataFileLocation(),
schema(),
scan.schema(),
partitions,
Expand Down
2 changes: 1 addition & 1 deletion core/src/main/java/org/apache/iceberg/RefsTable.java
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ public Schema schema() {
private DataTask task(BaseTableScan scan) {
Collection<String> refNames = table().refs().keySet();
return StaticDataTask.of(
table().io().newInputFile(table().operations().current().metadataFileLocation()),
table().operations().current().metadataFileLocation(),
schema(),
scan.schema(),
refNames,
Expand Down
2 changes: 1 addition & 1 deletion core/src/main/java/org/apache/iceberg/SnapshotsTable.java
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ public Schema schema() {

private DataTask task(BaseTableScan scan) {
return StaticDataTask.of(
table().io().newInputFile(table().operations().current().metadataFileLocation()),
table().operations().current().metadataFileLocation(),
schema(),
scan.schema(),
table().snapshots(),
Expand Down
36 changes: 16 additions & 20 deletions core/src/main/java/org/apache/iceberg/StaticDataTask.java
Original file line number Diff line number Diff line change
Expand Up @@ -25,45 +25,41 @@
import org.apache.iceberg.expressions.Expression;
import org.apache.iceberg.expressions.Expressions;
import org.apache.iceberg.io.CloseableIterable;
import org.apache.iceberg.io.InputFile;
import org.apache.iceberg.relocated.com.google.common.collect.ImmutableList;
import org.apache.iceberg.relocated.com.google.common.collect.Iterables;
import org.apache.iceberg.relocated.com.google.common.collect.Lists;
import org.apache.iceberg.util.StructProjection;

class StaticDataTask implements DataTask {

/**
* Builds a {@link StaticDataTask} from in-memory rows. The {@code metadataLocation} is kept on
* the synthetic {@link DataFile} for identification only and is never read, so its size is
* reported as 0 rather than looked up.
*/
static <T> DataTask of(
InputFile metadata,
String metadataLocation,
Schema tableSchema,
Schema projectedSchema,
Iterable<T> values,
Function<T, Row> transform) {
return new StaticDataTask(
metadata,
tableSchema,
projectedSchema,
Lists.newArrayList(Iterables.transform(values, transform::apply)).toArray(new Row[0]));
Row[] rows =
Lists.newArrayList(Iterables.transform(values, transform::apply)).toArray(new Row[0]);
DataFile syntheticFile =
DataFiles.builder(PartitionSpec.unpartitioned())
.withPath(metadataLocation)
.withFileSizeInBytes(0L)
.withRecordCount(rows.length)
.withFormat(FileFormat.METADATA)
.build();
return new StaticDataTask(syntheticFile, tableSchema, projectedSchema, rows);
}

private final DataFile metadataFile;
private final StructLike[] rows;
private final Schema tableSchema;
private final Schema projectedSchema;

private StaticDataTask(
InputFile metadata, Schema tableSchema, Schema projectedSchema, StructLike[] rows) {
this.tableSchema = tableSchema;
this.projectedSchema = projectedSchema;
this.metadataFile =
DataFiles.builder(PartitionSpec.unpartitioned())
.withInputFile(metadata)
.withRecordCount(rows.length)
.withFormat(FileFormat.METADATA)
.build();
this.rows = rows;
}

StaticDataTask(
DataFile metadataFile, Schema tableSchema, Schema projectedSchema, StructLike[] rows) {
this.tableSchema = tableSchema;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -236,7 +236,7 @@ private DataTask createDataTask() {
null));

return StaticDataTask.of(
Files.localInput("file:/tmp/metadata2.json"),
"/tmp/metadata2.json",
SNAPSHOT_SCHEMA,
SNAPSHOT_SCHEMA,
snapshots,
Expand Down
104 changes: 104 additions & 0 deletions core/src/test/java/org/apache/iceberg/TestStaticDataTask.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
/*
* 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;

import static org.apache.iceberg.types.Types.NestedField.required;
import static org.assertj.core.api.Assertions.assertThat;

import java.util.List;
import org.apache.iceberg.io.CloseableIterable;
import org.apache.iceberg.relocated.com.google.common.collect.ImmutableList;
import org.apache.iceberg.types.Types;
import org.junit.jupiter.api.Test;

public class TestStaticDataTask {

private static final Schema TEST_SCHEMA = new Schema(required(1, "value", Types.LongType.get()));

@Test
public void ofStringPathDoesNotReadFile() {
// Even a clearly-bogus path must be tolerated: the new overload promises no I/O against the
// location, only preserving it for identification. If the implementation regressed to building
// the DataFile via withInputFile (which calls InputFile.getLength()), this call would fail
// when the FileIO eventually tried to HEAD the path.
String bogusLocation = "file:///does/not/exist/never/HEADed.metadata.json";
List<Long> values = ImmutableList.of(1L, 2L, 3L);

DataTask task =
StaticDataTask.of(
bogusLocation, TEST_SCHEMA, TEST_SCHEMA, values, v -> StaticDataTask.Row.of(v));

assertThat(task.isDataTask()).as("should be a DataTask").isTrue();
assertThat(task.length()).as("length must be 0 (no file bytes processed)").isEqualTo(0L);
assertThat(task.file().fileSizeInBytes())
.as("DataFile size must be 0 (no HEAD on synthetic file)")
.isEqualTo(0L);
assertThat(task.file().format())
.as("DataFile format must be METADATA")
.isEqualTo(FileFormat.METADATA);
assertThat(task.file().location())
.as("DataFile path must preserve the supplied location for identification")
.isEqualTo(bogusLocation);
assertThat(task.file().recordCount())
.as("DataFile record count must reflect the row count")
.isEqualTo(values.size());
}

@Test
public void ofStringPathRowsProjectCorrectly() {
String location = "file:///irrelevant/path.metadata.json";
List<Long> values = ImmutableList.of(10L, 20L, 30L);

DataTask task =
StaticDataTask.of(
location, TEST_SCHEMA, TEST_SCHEMA, values, v -> StaticDataTask.Row.of(v));

try (CloseableIterable<StructLike> rows = task.rows()) {
List<Long> read =
ImmutableList.copyOf(
org.apache.iceberg.relocated.com.google.common.collect.Iterables.transform(
rows, row -> row.get(0, Long.class)));
assertThat(read).containsExactlyElementsOf(values);
} catch (Exception e) {
throw new RuntimeException(e);
}
}

@Test
public void ofStringPathWithNoValuesYieldsEmptyTask() {
String location = "file:///irrelevant/path.metadata.json";

DataTask task =
StaticDataTask.of(
location,
TEST_SCHEMA,
TEST_SCHEMA,
ImmutableList.<Long>of(),
v -> StaticDataTask.Row.of(v));

assertThat(task.length()).isEqualTo(0L);
assertThat(task.file().recordCount()).isEqualTo(0L);

try (CloseableIterable<StructLike> rows = task.rows()) {
assertThat(rows).isEmpty();
} catch (Exception e) {
throw new RuntimeException(e);
}
}
}