Skip to content

Commit

Permalink
Consume pushed predicate is new table scan domain is equal or wider
Browse files Browse the repository at this point in the history
  • Loading branch information
sopel39 committed Sep 11, 2019
1 parent b9c01c0 commit 7ba01da
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,7 @@ public static Optional<PlanNode> pushFilterIntoTableScan(
{
// don't include non-deterministic predicates
Expression deterministicPredicate = filterDeterministicConjuncts(predicate);
Expression nonDeterministicPredicate = filterNonDeterministicConjuncts(predicate);

DomainTranslator.ExtractionResult decomposedPredicate = DomainTranslator.fromPredicate(
metadata,
Expand Down Expand Up @@ -186,10 +187,17 @@ public static Optional<PlanNode> pushFilterIntoTableScan(
TableHandle newTable;
TupleDomain<ColumnHandle> remainingFilter;
if (!metadata.usesLegacyTableLayouts(session, node.getTable())) {
// check if new domain is wider than domain already provided by table scan
if (!constraint.predicate().isPresent() && newDomain.contains(node.getEnforcedConstraint())) {
// new domain is wider than domain already provided by table scan
// TODO: remove deterministic filter predicate
return Optional.empty();
Expression resultingPredicate = combineConjuncts(
nonDeterministicPredicate,
decomposedPredicate.getRemainingExpression());

if (!TRUE_LITERAL.equals(resultingPredicate)) {
return Optional.of(new FilterNode(idAllocator.getNextId(), node, resultingPredicate));
}

return Optional.of(node);
}

if (newDomain.isNone()) {
Expand Down Expand Up @@ -247,7 +255,7 @@ public static Optional<PlanNode> pushFilterIntoTableScan(
// to failures of previously successful queries.
Expression resultingPredicate = combineConjuncts(
domainTranslator.toPredicate(remainingFilter.transform(assignments::get)),
filterNonDeterministicConjuncts(predicate),
nonDeterministicPredicate,
decomposedPredicate.getRemainingExpression());

if (!TRUE_LITERAL.equals(resultingPredicate)) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ public void replaceWithExistsWhenNoLayoutExist()
}

@Test
public void doesNotFireIfNewDomainIsSame()
public void consumesDeterministicPredicateIfNewDomainIsSame()
{
ColumnHandle columnHandle = new TpchColumnHandle("nationkey", BIGINT);
tester().assertThat(pushPredicateIntoTableScan)
Expand All @@ -121,11 +121,14 @@ public void doesNotFireIfNewDomainIsSame()
ImmutableMap.of(p.symbol("nationkey", BIGINT), columnHandle),
TupleDomain.fromFixedValues(ImmutableMap.of(
columnHandle, NullableValue.of(BIGINT, (long) 44))))))
.doesNotFire();
.matches(constrainedTableScanWithTableLayout(
"nation",
ImmutableMap.of("nationkey", singleValue(BIGINT, (long) 44)),
ImmutableMap.of("nationkey", "nationkey")));
}

@Test
public void doesNotFireIfNewDomainIsWider()
public void consumesDeterministicPredicateIfNewDomainIsWider()
{
ColumnHandle columnHandle = new TpchColumnHandle("nationkey", BIGINT);
tester().assertThat(pushPredicateIntoTableScan)
Expand All @@ -136,6 +139,44 @@ public void doesNotFireIfNewDomainIsWider()
ImmutableMap.of(p.symbol("nationkey", BIGINT), columnHandle),
TupleDomain.fromFixedValues(ImmutableMap.of(
columnHandle, NullableValue.of(BIGINT, (long) 44))))))
.matches(constrainedTableScanWithTableLayout(
"nation",
ImmutableMap.of("nationkey", singleValue(BIGINT, (long) 44)),
ImmutableMap.of("nationkey", "nationkey")));
}

@Test
public void doesNotConsumeRemainingPredicateIfNewDomainIsWider()
{
ColumnHandle columnHandle = new TpchColumnHandle("nationkey", BIGINT);
tester().assertThat(pushPredicateIntoTableScan)
.on(p -> p.filter(expression("rand() = 42 AND nationkey % 17 = BIGINT '44' AND (nationkey = BIGINT '44' OR nationkey = BIGINT '45')"),
p.tableScan(
nationTableHandle,
ImmutableList.of(p.symbol("nationkey", BIGINT)),
ImmutableMap.of(p.symbol("nationkey", BIGINT), columnHandle),
TupleDomain.fromFixedValues(ImmutableMap.of(
columnHandle, NullableValue.of(BIGINT, (long) 44))))))
.matches(
filter(
expression("rand() = 42 AND nationkey % 17 = BIGINT '44'"),
constrainedTableScanWithTableLayout(
"nation",
ImmutableMap.of("nationkey", singleValue(BIGINT, (long) 44)),
ImmutableMap.of("nationkey", "nationkey"))));
}

@Test
public void doesNotFireOnNonDeterministicPredicate()
{
ColumnHandle columnHandle = new TpchColumnHandle("nationkey", BIGINT);
tester().assertThat(pushPredicateIntoTableScan)
.on(p -> p.filter(expression("rand() = 42"),
p.tableScan(
nationTableHandle,
ImmutableList.of(p.symbol("nationkey", BIGINT)),
ImmutableMap.of(p.symbol("nationkey", BIGINT), columnHandle),
TupleDomain.all())))
.doesNotFire();
}

Expand Down

0 comments on commit 7ba01da

Please sign in to comment.