Skip to content

Commit

Permalink
Fix issue with querying Iceberg using a structural type predicate
Browse files Browse the repository at this point in the history
Filter out Map, Array, and Row type domains from the IcebergTableHandle's
unenforced constraint.
  • Loading branch information
alexjo2144 authored and phd3 committed Aug 31, 2021
1 parent b03065e commit 6f1297f
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,7 @@
import static com.google.common.collect.ImmutableList.toImmutableList;
import static com.google.common.collect.ImmutableMap.toImmutableMap;
import static com.google.common.collect.ImmutableSet.toImmutableSet;
import static io.trino.plugin.hive.util.HiveUtil.isStructuralType;
import static io.trino.plugin.iceberg.ExpressionConverter.toIcebergExpression;
import static io.trino.plugin.iceberg.IcebergColumnHandle.primitiveIcebergColumnHandle;
import static io.trino.plugin.iceberg.IcebergErrorCode.ICEBERG_INVALID_METADATA;
Expand Down Expand Up @@ -648,9 +649,14 @@ public Optional<ConstraintApplicationResult<ConnectorTableHandle>> applyFilter(C
.filter(isIdentityPartition)
.intersect(table.getEnforcedPredicate());

TupleDomain<IcebergColumnHandle> newUnenforcedConstraint = constraint.getSummary()
TupleDomain<IcebergColumnHandle> remainingConstraint = constraint.getSummary()
.transformKeys(IcebergColumnHandle.class::cast)
.filter(isIdentityPartition.negate())
.filter(isIdentityPartition.negate());

TupleDomain<IcebergColumnHandle> newUnenforcedConstraint = remainingConstraint
// TODO: Remove after completing https://github.com/trinodb/trino/issues/8759
// Only applies to the unenforced constraint because structural types cannot be partition keys
.filter((columnHandle, predicate) -> !isStructuralType(columnHandle.getType()))
.intersect(table.getUnenforcedPredicate());

if (newEnforcedConstraint.equals(table.getEnforcedPredicate())
Expand All @@ -665,7 +671,7 @@ public Optional<ConstraintApplicationResult<ConnectorTableHandle>> applyFilter(C
table.getSnapshotId(),
newUnenforcedConstraint,
newEnforcedConstraint),
newUnenforcedConstraint.transformKeys(ColumnHandle.class::cast),
remainingConstraint.transformKeys(ColumnHandle.class::cast),
false));
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1641,6 +1641,26 @@ public void testPredicatePushdown()
dropTable(tableName.getObjectName());
}

@Test
public void testPredicatesWithStructuralTypes()
{
String tableName = "test_predicate_with_structural_types";
assertUpdate("CREATE TABLE " + tableName + " (id INT, array_t ARRAY(BIGINT), map_t MAP(BIGINT, BIGINT), struct_t ROW(f1 BIGINT, f2 BIGINT))");
assertUpdate("INSERT INTO " + tableName + " VALUES " +
"(1, ARRAY[1, 2, 3], MAP(ARRAY[1,3], ARRAY[2,4]), ROW(1, 2)), " +
"(11, ARRAY[11, 12, 13], MAP(ARRAY[11, 13], ARRAY[12, 14]), ROW(11, 12)), " +
"(11, ARRAY[111, 112, 113], MAP(ARRAY[111, 13], ARRAY[112, 114]), ROW(111, 112)), " +
"(21, ARRAY[21, 22, 23], MAP(ARRAY[21, 23], ARRAY[22, 24]), ROW(21, 22))",
4);

assertQuery("SELECT id FROM " + tableName + " WHERE array_t = ARRAY[1, 2, 3]", "VALUES 1");
assertQuery("SELECT id FROM " + tableName + " WHERE map_t = MAP(ARRAY[11, 13], ARRAY[12, 14])", "VALUES 11");
assertQuery("SELECT id FROM " + tableName + " WHERE struct_t = ROW(21, 22)", "VALUES 21");
assertQuery("SELECT struct_t.f1 FROM " + tableName + " WHERE id = 11 AND map_t = MAP(ARRAY[11, 13], ARRAY[12, 14])", "VALUES 11");

dropTable(tableName);
}

private void assertFilterPushdown(
QualifiedObjectName tableName,
Map<String, Domain> filter,
Expand Down

0 comments on commit 6f1297f

Please sign in to comment.