Skip to content

Commit

Permalink
Don't insert redundant filter after index source
Browse files Browse the repository at this point in the history
  • Loading branch information
martint committed Apr 27, 2015
1 parent cfd1778 commit 1392ecb
Showing 1 changed file with 19 additions and 13 deletions.
Expand Up @@ -53,6 +53,7 @@
import java.util.Set; import java.util.Set;
import java.util.concurrent.atomic.AtomicBoolean; import java.util.concurrent.atomic.AtomicBoolean;


import static com.facebook.presto.sql.ExpressionUtils.combineConjuncts;
import static com.facebook.presto.sql.tree.BooleanLiteral.TRUE_LITERAL; import static com.facebook.presto.sql.tree.BooleanLiteral.TRUE_LITERAL;
import static com.google.common.base.Preconditions.checkArgument; import static com.google.common.base.Preconditions.checkArgument;
import static com.google.common.base.Preconditions.checkNotNull; import static com.google.common.base.Preconditions.checkNotNull;
Expand Down Expand Up @@ -246,8 +247,13 @@ public PlanNode visitTableScan(TableScanNode node, RewriteContext<Context> conte
@NotNull @NotNull
private PlanNode planTableScan(TableScanNode node, Expression predicate, Context context) private PlanNode planTableScan(TableScanNode node, Expression predicate, Context context)
{ {
TupleDomain<ColumnHandle> constraint = DomainTranslator.fromPredicate(metadata, session, predicate, symbolAllocator.getTypes()) DomainTranslator.ExtractionResult decomposedPredicate = DomainTranslator.fromPredicate(
.getTupleDomain() metadata,
session,
predicate,
symbolAllocator.getTypes());

TupleDomain<ColumnHandle> simplifiedConstraint = decomposedPredicate.getTupleDomain()
.transform(node.getAssignments()::get) .transform(node.getAssignments()::get)
.intersect(node.getCurrentConstraint()); .intersect(node.getCurrentConstraint());


Expand All @@ -257,15 +263,14 @@ private PlanNode planTableScan(TableScanNode node, Expression predicate, Context
.transform(Functions.forMap(node.getAssignments())) .transform(Functions.forMap(node.getAssignments()))
.toSet(); .toSet();


Optional<ResolvedIndex> optionalResolvedIndex = indexManager.resolveIndex(node.getTable(), lookupColumns, constraint); Optional<ResolvedIndex> optionalResolvedIndex = indexManager.resolveIndex(node.getTable(), lookupColumns, simplifiedConstraint);
if (!optionalResolvedIndex.isPresent()) { if (!optionalResolvedIndex.isPresent()) {
// No index available, so give up by returning something // No index available, so give up by returning something
return node; return node;
} }
ResolvedIndex resolvedIndex = optionalResolvedIndex.get(); ResolvedIndex resolvedIndex = optionalResolvedIndex.get();


Map<ColumnHandle, Symbol> inverseAssignments = ImmutableBiMap.copyOf(node.getAssignments()).inverse(); Map<ColumnHandle, Symbol> inverseAssignments = ImmutableBiMap.copyOf(node.getAssignments()).inverse();
Expression unresolvedExpression = DomainTranslator.toPredicate(resolvedIndex.getUnresolvedTupleDomain().transform(inverseAssignments::get), symbolAllocator.getTypes());


PlanNode source = new IndexSourceNode( PlanNode source = new IndexSourceNode(
idAllocator.getNextId(), idAllocator.getNextId(),
Expand All @@ -274,11 +279,17 @@ private PlanNode planTableScan(TableScanNode node, Expression predicate, Context
context.getLookupSymbols(), context.getLookupSymbols(),
node.getOutputSymbols(), node.getOutputSymbols(),
node.getAssignments(), node.getAssignments(),
constraint); simplifiedConstraint);

Expression resultingPredicate = combineConjuncts(
DomainTranslator.toPredicate(
resolvedIndex.getUnresolvedTupleDomain().transform(inverseAssignments::get),
symbolAllocator.getTypes()),
decomposedPredicate.getRemainingExpression());


if (!unresolvedExpression.equals(TRUE_LITERAL)) { if (!resultingPredicate.equals(TRUE_LITERAL)) {
// todo it is likely we end up with redundant filters here because the predicate push down has already been run... the fix is to run predicate push down again // todo it is likely we end up with redundant filters here because the predicate push down has already been run... the fix is to run predicate push down again
source = new FilterNode(idAllocator.getNextId(), source, unresolvedExpression); source = new FilterNode(idAllocator.getNextId(), source, resultingPredicate);
} }
context.markSuccess(); context.markSuccess();
return source; return source;
Expand All @@ -305,12 +316,7 @@ public PlanNode visitProject(ProjectNode node, RewriteContext<Context> context)
public PlanNode visitFilter(FilterNode node, RewriteContext<Context> context) public PlanNode visitFilter(FilterNode node, RewriteContext<Context> context)
{ {
if (node.getSource() instanceof TableScanNode) { if (node.getSource() instanceof TableScanNode) {
TableScanNode tableScan = (TableScanNode) node.getSource(); return planTableScan((TableScanNode) node.getSource(), node.getPredicate(), context.get());

return new FilterNode(
node.getId(),
planTableScan(tableScan, node.getPredicate(), context.get()),
node.getPredicate());
} }


return context.defaultRewrite(node, new Context(context.get().getLookupSymbols(), context.get().getSuccess())); return context.defaultRewrite(node, new Context(context.get().getLookupSymbols(), context.get().getSuccess()));
Expand Down

0 comments on commit 1392ecb

Please sign in to comment.