Search in sources :

Example 41 with VariableReferenceExpression

use of com.facebook.presto.spi.relation.VariableReferenceExpression in project presto by prestodb.

the class RewriteAggregationIfToFilter method canUnwrapIf.

private boolean canUnwrapIf(SpecialFormExpression ifExpression, AggregationIfToFilterRewriteStrategy rewriteStrategy) {
    if (rewriteStrategy == FILTER_WITH_IF) {
        return false;
    }
    // Some use cases use IF expression to avoid returning errors when evaluating the true branch. For example, IF(CARDINALITY(array) > 0, array[1])).
    // We shouldn't unwrap the IF for those cases.
    // But if the condition expression doesn't reference any variables referenced in the true branch, unwrapping the if should not cause exceptions for the true branch.
    Set<VariableReferenceExpression> ifConditionReferences = VariablesExtractor.extractUnique(ifExpression.getArguments().get(0));
    Set<VariableReferenceExpression> ifResultReferences = VariablesExtractor.extractUnique(ifExpression.getArguments().get(1));
    if (ifConditionReferences.stream().noneMatch(ifResultReferences::contains)) {
        return true;
    }
    if (rewriteStrategy != UNWRAP_IF) {
        return false;
    }
    AtomicBoolean result = new AtomicBoolean(true);
    ifExpression.getArguments().get(1).accept(new DefaultRowExpressionTraversalVisitor<AtomicBoolean>() {

        @Override
        public Void visitLambda(LambdaDefinitionExpression lambda, AtomicBoolean result) {
            // Unwrapping the IF expression in the aggregate might cause issues if the true branch return errors for rows not matching the filters.
            // To be safe, we don't unwrap the IF expressions when the true branch has lambdas.
            result.set(false);
            return null;
        }

        @Override
        public Void visitCall(CallExpression call, AtomicBoolean result) {
            Optional<OperatorType> operatorType = functionAndTypeManager.getFunctionMetadata(call.getFunctionHandle()).getOperatorType();
            // For example, array[1] could return out of bound error and a / b could return DIVISION_BY_ZERO error. So we doesn't unwrap the IF expression in these cases.
            if (operatorType.isPresent() && (operatorType.get() == OperatorType.DIVIDE || operatorType.get() == OperatorType.SUBSCRIPT)) {
                result.set(false);
                return null;
            }
            return super.visitCall(call, result);
        }
    }, result);
    return result.get();
}
Also used : AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) Optional(java.util.Optional) VariableReferenceExpression(com.facebook.presto.spi.relation.VariableReferenceExpression) CallExpression(com.facebook.presto.spi.relation.CallExpression) LambdaDefinitionExpression(com.facebook.presto.spi.relation.LambdaDefinitionExpression)

Example 42 with VariableReferenceExpression

use of com.facebook.presto.spi.relation.VariableReferenceExpression in project presto by prestodb.

the class PushPartialAggregationThroughJoin method pushPartialToLeftChild.

private PlanNode pushPartialToLeftChild(AggregationNode node, JoinNode child, Context context) {
    Set<VariableReferenceExpression> joinLeftChildVariables = ImmutableSet.copyOf(child.getLeft().getOutputVariables());
    List<VariableReferenceExpression> groupingSet = getPushedDownGroupingSet(node, joinLeftChildVariables, intersection(getJoinRequiredVariables(child), joinLeftChildVariables));
    AggregationNode pushedAggregation = replaceAggregationSource(node, child.getLeft(), groupingSet);
    return pushPartialToJoin(node, child, pushedAggregation, child.getRight(), context);
}
Also used : VariableReferenceExpression(com.facebook.presto.spi.relation.VariableReferenceExpression) AggregationNode(com.facebook.presto.spi.plan.AggregationNode)

Example 43 with VariableReferenceExpression

use of com.facebook.presto.spi.relation.VariableReferenceExpression in project presto by prestodb.

the class PushPartialAggregationThroughJoin method pushPartialToRightChild.

private PlanNode pushPartialToRightChild(AggregationNode node, JoinNode child, Context context) {
    Set<VariableReferenceExpression> joinRightChildVariables = ImmutableSet.copyOf(child.getRight().getOutputVariables());
    List<VariableReferenceExpression> groupingSet = getPushedDownGroupingSet(node, joinRightChildVariables, intersection(getJoinRequiredVariables(child), joinRightChildVariables));
    AggregationNode pushedAggregation = replaceAggregationSource(node, child.getRight(), groupingSet);
    return pushPartialToJoin(node, child, child.getLeft(), pushedAggregation, context);
}
Also used : VariableReferenceExpression(com.facebook.presto.spi.relation.VariableReferenceExpression) AggregationNode(com.facebook.presto.spi.plan.AggregationNode)

Example 44 with VariableReferenceExpression

use of com.facebook.presto.spi.relation.VariableReferenceExpression in project presto by prestodb.

the class TestPhasedExecutionSchedule method createBroadcastJoinPlanFragment.

private static PlanFragment createBroadcastJoinPlanFragment(String name, PlanFragment buildFragment) {
    VariableReferenceExpression variable = new VariableReferenceExpression(Optional.empty(), "column", BIGINT);
    PlanNode tableScan = new TableScanNode(Optional.empty(), new PlanNodeId(name), new TableHandle(new ConnectorId("test"), new TestingTableHandle(), TestingTransactionHandle.create(), Optional.empty()), ImmutableList.of(variable), ImmutableMap.of(variable, new TestingColumnHandle("column")), TupleDomain.all(), TupleDomain.all());
    RemoteSourceNode remote = new RemoteSourceNode(Optional.empty(), new PlanNodeId("build_id"), buildFragment.getId(), ImmutableList.of(), false, Optional.empty(), REPLICATE);
    PlanNode join = new JoinNode(Optional.empty(), new PlanNodeId(name + "_id"), INNER, tableScan, remote, ImmutableList.of(), ImmutableList.<VariableReferenceExpression>builder().addAll(tableScan.getOutputVariables()).addAll(remote.getOutputVariables()).build(), Optional.empty(), Optional.empty(), Optional.empty(), Optional.of(REPLICATED), ImmutableMap.of());
    return createFragment(join);
}
Also used : PlanNodeId(com.facebook.presto.spi.plan.PlanNodeId) TestingColumnHandle(com.facebook.presto.testing.TestingMetadata.TestingColumnHandle) RemoteSourceNode(com.facebook.presto.sql.planner.plan.RemoteSourceNode) PlanNode(com.facebook.presto.spi.plan.PlanNode) TableScanNode(com.facebook.presto.spi.plan.TableScanNode) VariableReferenceExpression(com.facebook.presto.spi.relation.VariableReferenceExpression) TestingTableHandle(com.facebook.presto.testing.TestingMetadata.TestingTableHandle) JoinNode(com.facebook.presto.sql.planner.plan.JoinNode) TableHandle(com.facebook.presto.spi.TableHandle) TestingTableHandle(com.facebook.presto.testing.TestingMetadata.TestingTableHandle) ConnectorId(com.facebook.presto.spi.ConnectorId)

Example 45 with VariableReferenceExpression

use of com.facebook.presto.spi.relation.VariableReferenceExpression in project presto by prestodb.

the class TestPhasedExecutionSchedule method createTableScanPlanFragment.

private static PlanFragment createTableScanPlanFragment(String name) {
    VariableReferenceExpression variable = new VariableReferenceExpression(Optional.empty(), "column", BIGINT);
    PlanNode planNode = new TableScanNode(Optional.empty(), new PlanNodeId(name), new TableHandle(new ConnectorId("test"), new TestingTableHandle(), TestingTransactionHandle.create(), Optional.empty()), ImmutableList.of(variable), ImmutableMap.of(variable, new TestingColumnHandle("column")), TupleDomain.all(), TupleDomain.all());
    return createFragment(planNode);
}
Also used : PlanNodeId(com.facebook.presto.spi.plan.PlanNodeId) TestingColumnHandle(com.facebook.presto.testing.TestingMetadata.TestingColumnHandle) PlanNode(com.facebook.presto.spi.plan.PlanNode) TableScanNode(com.facebook.presto.spi.plan.TableScanNode) VariableReferenceExpression(com.facebook.presto.spi.relation.VariableReferenceExpression) TestingTableHandle(com.facebook.presto.testing.TestingMetadata.TestingTableHandle) TableHandle(com.facebook.presto.spi.TableHandle) TestingTableHandle(com.facebook.presto.testing.TestingMetadata.TestingTableHandle) ConnectorId(com.facebook.presto.spi.ConnectorId)

Aggregations

VariableReferenceExpression (com.facebook.presto.spi.relation.VariableReferenceExpression)340 Test (org.testng.annotations.Test)129 ImmutableList (com.google.common.collect.ImmutableList)109 RowExpression (com.facebook.presto.spi.relation.RowExpression)93 ImmutableMap (com.google.common.collect.ImmutableMap)89 PlanNode (com.facebook.presto.spi.plan.PlanNode)85 Optional (java.util.Optional)84 Map (java.util.Map)73 JoinNode (com.facebook.presto.sql.planner.plan.JoinNode)61 ImmutableList.toImmutableList (com.google.common.collect.ImmutableList.toImmutableList)58 List (java.util.List)58 ProjectNode (com.facebook.presto.spi.plan.ProjectNode)52 CallExpression (com.facebook.presto.spi.relation.CallExpression)49 AggregationNode (com.facebook.presto.spi.plan.AggregationNode)48 BIGINT (com.facebook.presto.common.type.BigintType.BIGINT)45 Expression (com.facebook.presto.sql.tree.Expression)44 Assignments (com.facebook.presto.spi.plan.Assignments)42 PlanNodeId (com.facebook.presto.spi.plan.PlanNodeId)42 TableScanNode (com.facebook.presto.spi.plan.TableScanNode)42 ColumnHandle (com.facebook.presto.spi.ColumnHandle)40