Skip to content
Open
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
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
import java.time.temporal.ChronoUnit;
import java.util.List;
import java.util.Locale;
import java.util.Map;
import java.util.concurrent.TimeUnit;
import java.util.function.Function;
import java.util.regex.Pattern;
Expand All @@ -37,6 +38,7 @@
import org.apache.iceberg.relocated.com.google.common.collect.Lists;
import org.apache.iceberg.transforms.Transforms;
import org.apache.iceberg.types.Type;
import org.apache.iceberg.types.TypeUtil;
import org.apache.iceberg.types.Types;
import org.apache.iceberg.util.DateTimeUtil;
import org.apache.iceberg.variants.PhysicalType;
Expand Down Expand Up @@ -737,11 +739,28 @@ private static String sanitizeVariantValue(

private static PartitionSpec identitySpec(Schema schema, int... ids) {
PartitionSpec.Builder specBuilder = PartitionSpec.builderFor(schema);
Map<Integer, Integer> idToParent = TypeUtil.indexParents(schema.asStruct());

for (int id : ids) {
specBuilder.identity(schema.findColumnName(id));
String columnName = schema.findColumnName(id);
if (columnName != null && canBePartitionSource(id, schema, idToParent)) {
specBuilder.identity(columnName);
}
}

return specBuilder.build();
}

private static boolean canBePartitionSource(
int fieldId, Schema schema, Map<Integer, Integer> idToParent) {
Integer parentId = idToParent.get(fieldId);
while (parentId != null) {
Type parentType = schema.findType(parentId);
if (parentType == null || !parentType.isStructType()) {
return false;
}
parentId = idToParent.get(parentId);
}
return true;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -245,6 +245,37 @@ public void testRewriteSummary() throws Exception {
EnvironmentContext.ENGINE_VERSION, v -> assertThat(v).startsWith("3.4"));
}

@TestTemplate
public void testRewritePositionDeletesWithArrayColumns() throws Exception {
sql(
"CREATE TABLE %s (id BIGINT, data STRING, items ARRAY<STRUCT<value:BIGINT, count:INT>>) "
+ "USING iceberg TBLPROPERTIES"
+ "('format-version'='2', 'write.delete.mode'='merge-on-read', 'write.update.mode'='merge-on-read')",
tableName);

sql(
"INSERT INTO %s VALUES "
+ "(1, 'a', array(named_struct('value', cast(10 as bigint), 'count', 1))), "
+ "(2, 'b', array(named_struct('value', cast(20 as bigint), 'count', 2))), "
+ "(3, 'c', array(named_struct('value', cast(30 as bigint), 'count', 3))), "
+ "(4, 'd', array(named_struct('value', cast(40 as bigint), 'count', 4))), "
+ "(5, 'e', array(named_struct('value', cast(50 as bigint), 'count', 5))), "
+ "(6, 'f', array(named_struct('value', cast(60 as bigint), 'count', 6)))",
tableName);

sql("DELETE FROM %s WHERE id = 1", tableName);
sql("DELETE FROM %s WHERE id = 2", tableName);

Table table = validationCatalog.loadTable(tableIdent);
assertThat(TestHelpers.deleteFiles(table)).hasSizeGreaterThanOrEqualTo(1);

sql(
"CALL %s.system.rewrite_position_delete_files("
+ "table => '%s',"
+ "options => map('rewrite-all','true'))",
catalogName, tableIdent);
}

private Map<String, String> snapshotSummary() {
return validationCatalog.loadTable(tableIdent).currentSnapshot().summary();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -245,6 +245,37 @@ public void testRewriteSummary() throws Exception {
EnvironmentContext.ENGINE_VERSION, v -> assertThat(v).startsWith("3.5"));
}

@TestTemplate
public void testRewritePositionDeletesWithArrayColumns() throws Exception {
sql(
"CREATE TABLE %s (id BIGINT, data STRING, items ARRAY<STRUCT<value:BIGINT, count:INT>>) "
+ "USING iceberg TBLPROPERTIES"
+ "('format-version'='2', 'write.delete.mode'='merge-on-read', 'write.update.mode'='merge-on-read')",
tableName);

sql(
"INSERT INTO %s VALUES "
+ "(1, 'a', array(named_struct('value', cast(10 as bigint), 'count', 1))), "
+ "(2, 'b', array(named_struct('value', cast(20 as bigint), 'count', 2))), "
+ "(3, 'c', array(named_struct('value', cast(30 as bigint), 'count', 3))), "
+ "(4, 'd', array(named_struct('value', cast(40 as bigint), 'count', 4))), "
+ "(5, 'e', array(named_struct('value', cast(50 as bigint), 'count', 5))), "
+ "(6, 'f', array(named_struct('value', cast(60 as bigint), 'count', 6)))",
tableName);

sql("DELETE FROM %s WHERE id = 1", tableName);
sql("DELETE FROM %s WHERE id = 2", tableName);

Table table = validationCatalog.loadTable(tableIdent);
assertThat(TestHelpers.deleteFiles(table)).hasSizeGreaterThanOrEqualTo(1);

sql(
"CALL %s.system.rewrite_position_delete_files("
+ "table => '%s',"
+ "options => map('rewrite-all','true'))",
catalogName, tableIdent);
}

private Map<String, String> snapshotSummary() {
return validationCatalog.loadTable(tableIdent).currentSnapshot().summary();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -245,6 +245,37 @@ public void testRewriteSummary() throws Exception {
EnvironmentContext.ENGINE_VERSION, v -> assertThat(v).startsWith("4.0"));
}

@TestTemplate
public void testRewritePositionDeletesWithArrayColumns() throws Exception {
sql(
"CREATE TABLE %s (id BIGINT, data STRING, items ARRAY<STRUCT<value:BIGINT, count:INT>>) "
+ "USING iceberg TBLPROPERTIES"
+ "('format-version'='2', 'write.delete.mode'='merge-on-read', 'write.update.mode'='merge-on-read')",
tableName);

sql(
"INSERT INTO %s VALUES "
+ "(1, 'a', array(named_struct('value', cast(10 as bigint), 'count', 1))), "
+ "(2, 'b', array(named_struct('value', cast(20 as bigint), 'count', 2))), "
+ "(3, 'c', array(named_struct('value', cast(30 as bigint), 'count', 3))), "
+ "(4, 'd', array(named_struct('value', cast(40 as bigint), 'count', 4))), "
+ "(5, 'e', array(named_struct('value', cast(50 as bigint), 'count', 5))), "
+ "(6, 'f', array(named_struct('value', cast(60 as bigint), 'count', 6)))",
tableName);

sql("DELETE FROM %s WHERE id = 1", tableName);
sql("DELETE FROM %s WHERE id = 2", tableName);

Table table = validationCatalog.loadTable(tableIdent);
assertThat(TestHelpers.deleteFiles(table)).hasSizeGreaterThanOrEqualTo(1);

sql(
"CALL %s.system.rewrite_position_delete_files("
+ "table => '%s',"
+ "options => map('rewrite-all','true'))",
catalogName, tableIdent);
}

private Map<String, String> snapshotSummary() {
return validationCatalog.loadTable(tableIdent).currentSnapshot().summary();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -245,6 +245,37 @@ public void testRewriteSummary() throws Exception {
EnvironmentContext.ENGINE_VERSION, v -> assertThat(v).startsWith("4.1"));
}

@TestTemplate
public void testRewritePositionDeletesWithArrayColumns() throws Exception {
sql(
"CREATE TABLE %s (id BIGINT, data STRING, items ARRAY<STRUCT<value:BIGINT, count:INT>>) "
+ "USING iceberg TBLPROPERTIES"
+ "('format-version'='2', 'write.delete.mode'='merge-on-read', 'write.update.mode'='merge-on-read')",
tableName);

sql(
"INSERT INTO %s VALUES "
+ "(1, 'a', array(named_struct('value', cast(10 as bigint), 'count', 1))), "
+ "(2, 'b', array(named_struct('value', cast(20 as bigint), 'count', 2))), "
+ "(3, 'c', array(named_struct('value', cast(30 as bigint), 'count', 3))), "
+ "(4, 'd', array(named_struct('value', cast(40 as bigint), 'count', 4))), "
+ "(5, 'e', array(named_struct('value', cast(50 as bigint), 'count', 5))), "
+ "(6, 'f', array(named_struct('value', cast(60 as bigint), 'count', 6)))",
tableName);

sql("DELETE FROM %s WHERE id = 1", tableName);
sql("DELETE FROM %s WHERE id = 2", tableName);

Table table = validationCatalog.loadTable(tableIdent);
assertThat(TestHelpers.deleteFiles(table)).hasSizeGreaterThanOrEqualTo(1);

sql(
"CALL %s.system.rewrite_position_delete_files("
+ "table => '%s',"
+ "options => map('rewrite-all','true'))",
catalogName, tableIdent);
}

private Map<String, String> snapshotSummary() {
return validationCatalog.loadTable(tableIdent).currentSnapshot().summary();
}
Expand Down