Search in sources :

Example 31 with VariableReferenceExpression

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

the class TransformExistsApplyToLateralNode method rewriteToDefaultAggregation.

private PlanNode rewriteToDefaultAggregation(ApplyNode parent, Context context) {
    VariableReferenceExpression count = context.getVariableAllocator().newVariable("count", BIGINT);
    VariableReferenceExpression exists = getOnlyElement(parent.getSubqueryAssignments().getVariables());
    return new LateralJoinNode(parent.getSourceLocation(), parent.getId(), parent.getInput(), new ProjectNode(context.getIdAllocator().getNextId(), new AggregationNode(parent.getSourceLocation(), context.getIdAllocator().getNextId(), parent.getSubquery(), ImmutableMap.of(count, new Aggregation(new CallExpression(exists.getSourceLocation(), "count", functionResolution.countFunction(), BIGINT, ImmutableList.of()), Optional.empty(), Optional.empty(), false, Optional.empty())), globalAggregation(), ImmutableList.of(), AggregationNode.Step.SINGLE, Optional.empty(), Optional.empty()), Assignments.of(exists, castToRowExpression(new ComparisonExpression(GREATER_THAN, asSymbolReference(count), new Cast(new LongLiteral("0"), BIGINT.toString()))))), parent.getCorrelation(), INNER, parent.getOriginSubqueryError());
}
Also used : AggregationNode.globalAggregation(com.facebook.presto.spi.plan.AggregationNode.globalAggregation) Aggregation(com.facebook.presto.spi.plan.AggregationNode.Aggregation) Cast(com.facebook.presto.sql.tree.Cast) ComparisonExpression(com.facebook.presto.sql.tree.ComparisonExpression) LateralJoinNode(com.facebook.presto.sql.planner.plan.LateralJoinNode) LongLiteral(com.facebook.presto.sql.tree.LongLiteral) VariableReferenceExpression(com.facebook.presto.spi.relation.VariableReferenceExpression) ProjectNode(com.facebook.presto.spi.plan.ProjectNode) AggregationNode(com.facebook.presto.spi.plan.AggregationNode) CallExpression(com.facebook.presto.spi.relation.CallExpression)

Example 32 with VariableReferenceExpression

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

the class TransformCorrelatedInPredicateToJoin method apply.

@Override
public Result apply(ApplyNode apply, Captures captures, Context context) {
    Assignments subqueryAssignments = apply.getSubqueryAssignments();
    if (subqueryAssignments.size() != 1) {
        return Result.empty();
    }
    Expression assignmentExpression = castToExpression(getOnlyElement(subqueryAssignments.getExpressions()));
    if (!(assignmentExpression instanceof InPredicate)) {
        return Result.empty();
    }
    InPredicate inPredicate = (InPredicate) assignmentExpression;
    VariableReferenceExpression inPredicateOutputVariable = getOnlyElement(subqueryAssignments.getVariables());
    return apply(apply, inPredicate, inPredicateOutputVariable, context.getLookup(), context.getIdAllocator(), context.getVariableAllocator());
}
Also used : VariableReferenceExpression(com.facebook.presto.spi.relation.VariableReferenceExpression) OriginalExpressionUtils.castToExpression(com.facebook.presto.sql.relational.OriginalExpressionUtils.castToExpression) CallExpression(com.facebook.presto.spi.relation.CallExpression) NotExpression(com.facebook.presto.sql.tree.NotExpression) SearchedCaseExpression(com.facebook.presto.sql.tree.SearchedCaseExpression) ComparisonExpression(com.facebook.presto.sql.tree.ComparisonExpression) Expression(com.facebook.presto.sql.tree.Expression) OriginalExpressionUtils.castToRowExpression(com.facebook.presto.sql.relational.OriginalExpressionUtils.castToRowExpression) VariableReferenceExpression(com.facebook.presto.spi.relation.VariableReferenceExpression) Assignments(com.facebook.presto.spi.plan.Assignments) InPredicate(com.facebook.presto.sql.tree.InPredicate)

Example 33 with VariableReferenceExpression

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

the class TransformCorrelatedInPredicateToJoin method buildInPredicateEquivalent.

private PlanNode buildInPredicateEquivalent(ApplyNode apply, InPredicate inPredicate, VariableReferenceExpression inPredicateOutputVariable, Decorrelated decorrelated, PlanNodeIdAllocator idAllocator, PlanVariableAllocator variableAllocator) {
    Expression correlationCondition = and(decorrelated.getCorrelatedPredicates());
    PlanNode decorrelatedBuildSource = decorrelated.getDecorrelatedNode();
    AssignUniqueId probeSide = new AssignUniqueId(apply.getSourceLocation(), idAllocator.getNextId(), apply.getInput(), variableAllocator.newVariable("unique", BIGINT));
    VariableReferenceExpression buildSideKnownNonNull = variableAllocator.newVariable(inPredicateOutputVariable.getSourceLocation(), "buildSideKnownNonNull", BIGINT);
    ProjectNode buildSide = new ProjectNode(idAllocator.getNextId(), decorrelatedBuildSource, Assignments.builder().putAll(identitiesAsSymbolReferences(decorrelatedBuildSource.getOutputVariables())).put(buildSideKnownNonNull, castToRowExpression(bigint(0))).build());
    checkArgument(inPredicate.getValue() instanceof SymbolReference, "Unexpected expression: %s", inPredicate.getValue());
    SymbolReference probeSideSymbolReference = (SymbolReference) inPredicate.getValue();
    checkArgument(inPredicate.getValueList() instanceof SymbolReference, "Unexpected expression: %s", inPredicate.getValueList());
    SymbolReference buildSideSymbolReference = (SymbolReference) inPredicate.getValueList();
    Expression joinExpression = and(or(new IsNullPredicate(probeSideSymbolReference), new ComparisonExpression(ComparisonExpression.Operator.EQUAL, probeSideSymbolReference, buildSideSymbolReference), new IsNullPredicate(buildSideSymbolReference)), correlationCondition);
    JoinNode leftOuterJoin = leftOuterJoin(idAllocator, probeSide, buildSide, joinExpression);
    VariableReferenceExpression countMatchesVariable = variableAllocator.newVariable(getSourceLocation(buildSideSymbolReference.getLocation()), "countMatches", BIGINT);
    VariableReferenceExpression countNullMatchesVariable = variableAllocator.newVariable(getSourceLocation(buildSideSymbolReference.getLocation()), "countNullMatches", BIGINT);
    Expression matchCondition = and(new IsNotNullPredicate(probeSideSymbolReference), new IsNotNullPredicate(buildSideSymbolReference));
    Expression nullMatchCondition = and(new IsNotNullPredicate(createSymbolReference(buildSideKnownNonNull)), new NotExpression(matchCondition));
    AggregationNode aggregation = new AggregationNode(apply.getSourceLocation(), idAllocator.getNextId(), leftOuterJoin, ImmutableMap.<VariableReferenceExpression, AggregationNode.Aggregation>builder().put(countMatchesVariable, countWithFilter(matchCondition)).put(countNullMatchesVariable, countWithFilter(nullMatchCondition)).build(), singleGroupingSet(probeSide.getOutputVariables()), ImmutableList.of(), AggregationNode.Step.SINGLE, Optional.empty(), Optional.empty());
    // TODO since we care only about "some count > 0", we could have specialized node instead of leftOuterJoin that does the job without materializing join results
    SearchedCaseExpression inPredicateEquivalent = new SearchedCaseExpression(ImmutableList.of(new WhenClause(isGreaterThan(countMatchesVariable, 0), booleanConstant(true)), new WhenClause(isGreaterThan(countNullMatchesVariable, 0), booleanConstant(null))), Optional.of(booleanConstant(false)));
    return new ProjectNode(idAllocator.getNextId(), aggregation, Assignments.builder().putAll(identitiesAsSymbolReferences(apply.getInput().getOutputVariables())).put(inPredicateOutputVariable, castToRowExpression(inPredicateEquivalent)).build());
}
Also used : SymbolReference(com.facebook.presto.sql.tree.SymbolReference) ExpressionTreeUtils.createSymbolReference(com.facebook.presto.sql.analyzer.ExpressionTreeUtils.createSymbolReference) JoinNode(com.facebook.presto.sql.planner.plan.JoinNode) NotExpression(com.facebook.presto.sql.tree.NotExpression) AggregationNode(com.facebook.presto.spi.plan.AggregationNode) IsNotNullPredicate(com.facebook.presto.sql.tree.IsNotNullPredicate) ComparisonExpression(com.facebook.presto.sql.tree.ComparisonExpression) WhenClause(com.facebook.presto.sql.tree.WhenClause) PlanNode(com.facebook.presto.spi.plan.PlanNode) AssignUniqueId(com.facebook.presto.sql.planner.plan.AssignUniqueId) SearchedCaseExpression(com.facebook.presto.sql.tree.SearchedCaseExpression) VariableReferenceExpression(com.facebook.presto.spi.relation.VariableReferenceExpression) OriginalExpressionUtils.castToExpression(com.facebook.presto.sql.relational.OriginalExpressionUtils.castToExpression) CallExpression(com.facebook.presto.spi.relation.CallExpression) NotExpression(com.facebook.presto.sql.tree.NotExpression) SearchedCaseExpression(com.facebook.presto.sql.tree.SearchedCaseExpression) ComparisonExpression(com.facebook.presto.sql.tree.ComparisonExpression) Expression(com.facebook.presto.sql.tree.Expression) OriginalExpressionUtils.castToRowExpression(com.facebook.presto.sql.relational.OriginalExpressionUtils.castToRowExpression) VariableReferenceExpression(com.facebook.presto.spi.relation.VariableReferenceExpression) IsNullPredicate(com.facebook.presto.sql.tree.IsNullPredicate) ProjectNode(com.facebook.presto.spi.plan.ProjectNode)

Example 34 with VariableReferenceExpression

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

the class ActualProperties method translateRowExpression.

public ActualProperties translateRowExpression(Map<VariableReferenceExpression, RowExpression> assignments, TypeProvider types) {
    Map<VariableReferenceExpression, VariableReferenceExpression> inputToOutputVariables = new HashMap<>();
    for (Map.Entry<VariableReferenceExpression, RowExpression> assignment : assignments.entrySet()) {
        RowExpression expression = assignment.getValue();
        if (isExpression(expression)) {
            if (castToExpression(expression) instanceof SymbolReference) {
                inputToOutputVariables.put(toVariableReference(castToExpression(expression), types), assignment.getKey());
            }
        } else {
            if (expression instanceof VariableReferenceExpression) {
                inputToOutputVariables.put((VariableReferenceExpression) expression, assignment.getKey());
            }
        }
    }
    Map<VariableReferenceExpression, ConstantExpression> translatedConstants = new HashMap<>();
    for (Map.Entry<VariableReferenceExpression, ConstantExpression> entry : constants.entrySet()) {
        if (inputToOutputVariables.containsKey(entry.getKey())) {
            translatedConstants.put(inputToOutputVariables.get(entry.getKey()), entry.getValue());
        }
    }
    ImmutableMap.Builder<VariableReferenceExpression, RowExpression> inputToOutputMappings = ImmutableMap.builder();
    inputToOutputMappings.putAll(inputToOutputVariables);
    constants.entrySet().stream().filter(entry -> !inputToOutputVariables.containsKey(entry.getKey())).forEach(inputToOutputMappings::put);
    return builder().global(global.translateRowExpression(inputToOutputMappings.build(), assignments, types)).local(LocalProperties.translate(localProperties, variable -> Optional.ofNullable(inputToOutputVariables.get(variable)))).constants(translatedConstants).build();
}
Also used : SINGLE_DISTRIBUTION(com.facebook.presto.sql.planner.SystemPartitioningHandle.SINGLE_DISTRIBUTION) Iterables.transform(com.google.common.collect.Iterables.transform) OriginalExpressionUtils.isExpression(com.facebook.presto.sql.relational.OriginalExpressionUtils.isExpression) VariableReferenceExpression(com.facebook.presto.spi.relation.VariableReferenceExpression) HashMap(java.util.HashMap) ConstantExpression(com.facebook.presto.spi.relation.ConstantExpression) Function(java.util.function.Function) OriginalExpressionUtils.castToExpression(com.facebook.presto.sql.relational.OriginalExpressionUtils.castToExpression) Preconditions.checkArgument(com.google.common.base.Preconditions.checkArgument) ImmutableList(com.google.common.collect.ImmutableList) TypeProvider(com.facebook.presto.sql.planner.TypeProvider) MoreLists.filteredCopy(com.facebook.presto.util.MoreLists.filteredCopy) Map(java.util.Map) Objects.requireNonNull(java.util.Objects.requireNonNull) LocalProperty(com.facebook.presto.spi.LocalProperty) Partitioning(com.facebook.presto.sql.planner.Partitioning) RowExpression(com.facebook.presto.spi.relation.RowExpression) SymbolReference(com.facebook.presto.sql.tree.SymbolReference) ImmutableSet(com.google.common.collect.ImmutableSet) ImmutableMap(com.google.common.collect.ImmutableMap) Session(com.facebook.presto.Session) Collection(java.util.Collection) Set(java.util.Set) ConstantProperty(com.facebook.presto.spi.ConstantProperty) SOURCE_DISTRIBUTION(com.facebook.presto.sql.planner.SystemPartitioningHandle.SOURCE_DISTRIBUTION) Objects(java.util.Objects) PlannerUtils.toVariableReference(com.facebook.presto.sql.planner.PlannerUtils.toVariableReference) List(java.util.List) PartitioningHandle(com.facebook.presto.sql.planner.PartitioningHandle) Optional(java.util.Optional) Immutable(javax.annotation.concurrent.Immutable) Metadata(com.facebook.presto.metadata.Metadata) COORDINATOR_DISTRIBUTION(com.facebook.presto.sql.planner.SystemPartitioningHandle.COORDINATOR_DISTRIBUTION) MoreObjects.toStringHelper(com.google.common.base.MoreObjects.toStringHelper) HashMap(java.util.HashMap) SymbolReference(com.facebook.presto.sql.tree.SymbolReference) ConstantExpression(com.facebook.presto.spi.relation.ConstantExpression) RowExpression(com.facebook.presto.spi.relation.RowExpression) ImmutableMap(com.google.common.collect.ImmutableMap) VariableReferenceExpression(com.facebook.presto.spi.relation.VariableReferenceExpression) HashMap(java.util.HashMap) Map(java.util.Map) ImmutableMap(com.google.common.collect.ImmutableMap)

Example 35 with VariableReferenceExpression

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

the class HashGenerationOptimizer method getHashExpression.

public static Optional<RowExpression> getHashExpression(FunctionAndTypeManager functionAndTypeManager, List<VariableReferenceExpression> variables) {
    if (variables.isEmpty()) {
        return Optional.empty();
    }
    RowExpression result = constant(INITIAL_HASH_VALUE, BIGINT);
    for (VariableReferenceExpression variable : variables) {
        RowExpression hashField = call(functionAndTypeManager, HASH_CODE, BIGINT, variable);
        hashField = orNullHashCode(hashField);
        result = call(functionAndTypeManager, "combine_hash", BIGINT, result, hashField);
    }
    return Optional.of(result);
}
Also used : VariableReferenceExpression(com.facebook.presto.spi.relation.VariableReferenceExpression) RowExpression(com.facebook.presto.spi.relation.RowExpression)

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