Search in sources :

Example 91 with VariableReferenceExpression

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

the class WindowMatcher method detailMatches.

@Override
public MatchResult detailMatches(PlanNode node, StatsProvider stats, Session session, Metadata metadata, SymbolAliases symbolAliases) {
    checkState(shapeMatches(node), "Plan testing framework error: shapeMatches returned false in detailMatches in %s", this.getClass().getName());
    WindowNode windowNode = (WindowNode) node;
    if (!prePartitionedInputs.map(expectedInputs -> expectedInputs.stream().map(alias -> alias.toSymbol(symbolAliases)).collect(toImmutableSet()).equals(windowNode.getPrePartitionedInputs().stream().map(VariableReferenceExpression::getName).map(Symbol::new).collect(toImmutableSet()))).orElse(true)) {
        return NO_MATCH;
    }
    if (!specification.map(expectedSpecification -> matchSpecification(windowNode.getSpecification(), expectedSpecification.getExpectedValue(symbolAliases))).orElse(true)) {
        return NO_MATCH;
    }
    if (!preSortedOrderPrefix.map(Integer.valueOf(windowNode.getPreSortedOrderPrefix())::equals).orElse(true)) {
        return NO_MATCH;
    }
    if (!hashSymbol.map(expectedHashSymbol -> expectedHashSymbol.map(alias -> alias.toSymbol(symbolAliases)).map(Symbol::getName).equals(windowNode.getHashVariable().map(VariableReferenceExpression::getName))).orElse(true)) {
        return NO_MATCH;
    }
    /*
         * Window functions produce a symbol (the result of the function call) that we might
         * want to bind to an alias so we can reference it further up the tree. As such,
         * they need to be matched with an Alias matcher so we can bind the symbol if desired.
         */
    return match();
}
Also used : SpecificationProvider.matchSpecification(com.facebook.presto.sql.planner.assertions.SpecificationProvider.matchSpecification) SortOrder(com.facebook.presto.common.block.SortOrder) WindowNode(com.facebook.presto.sql.planner.plan.WindowNode) Session(com.facebook.presto.Session) StatsProvider(com.facebook.presto.cost.StatsProvider) Set(java.util.Set) VariableReferenceExpression(com.facebook.presto.spi.relation.VariableReferenceExpression) Preconditions.checkState(com.google.common.base.Preconditions.checkState) PlanNode(com.facebook.presto.spi.plan.PlanNode) List(java.util.List) Symbol(com.facebook.presto.sql.planner.Symbol) Map(java.util.Map) MatchResult.match(com.facebook.presto.sql.planner.assertions.MatchResult.match) Objects.requireNonNull(java.util.Objects.requireNonNull) FunctionHandle(com.facebook.presto.spi.function.FunctionHandle) Optional(java.util.Optional) ImmutableSet.toImmutableSet(com.google.common.collect.ImmutableSet.toImmutableSet) NO_MATCH(com.facebook.presto.sql.planner.assertions.MatchResult.NO_MATCH) LinkedList(java.util.LinkedList) Metadata(com.facebook.presto.metadata.Metadata) FunctionCall(com.facebook.presto.sql.tree.FunctionCall) PlanMatchPattern.node(com.facebook.presto.sql.planner.assertions.PlanMatchPattern.node) MoreObjects.toStringHelper(com.google.common.base.MoreObjects.toStringHelper) WindowNode(com.facebook.presto.sql.planner.plan.WindowNode) VariableReferenceExpression(com.facebook.presto.spi.relation.VariableReferenceExpression) Symbol(com.facebook.presto.sql.planner.Symbol)

Example 92 with VariableReferenceExpression

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

the class AggregationMatcher method detailMatches.

@Override
public MatchResult detailMatches(PlanNode node, StatsProvider stats, Session session, Metadata metadata, SymbolAliases symbolAliases) {
    checkState(shapeMatches(node), "Plan testing framework error: shapeMatches returned false in detailMatches in %s", this.getClass().getName());
    AggregationNode aggregationNode = (AggregationNode) node;
    if (groupId.isPresent() != aggregationNode.getGroupIdVariable().isPresent()) {
        return NO_MATCH;
    }
    if (!matches(groupingSets.getGroupingKeys(), aggregationNode.getGroupingKeys(), symbolAliases)) {
        return NO_MATCH;
    }
    if (groupingSets.getGroupingSetCount() != aggregationNode.getGroupingSetCount()) {
        return NO_MATCH;
    }
    if (!groupingSets.getGlobalGroupingSets().equals(aggregationNode.getGlobalGroupingSets())) {
        return NO_MATCH;
    }
    List<VariableReferenceExpression> aggregationsWithMask = aggregationNode.getAggregations().entrySet().stream().filter(entry -> entry.getValue().getMask().isPresent()).map(Map.Entry::getKey).collect(Collectors.toList());
    if (aggregationsWithMask.size() != masks.keySet().size()) {
        return NO_MATCH;
    }
    for (VariableReferenceExpression variable : aggregationsWithMask) {
        if (!masks.keySet().contains(new Symbol(variable.getName()))) {
            return NO_MATCH;
        }
    }
    if (step != aggregationNode.getStep()) {
        return NO_MATCH;
    }
    if (!matches(preGroupedSymbols, aggregationNode.getPreGroupedVariables(), symbolAliases)) {
        return NO_MATCH;
    }
    return match();
}
Also used : VariableReferenceExpression(com.facebook.presto.spi.relation.VariableReferenceExpression) Symbol(com.facebook.presto.sql.planner.Symbol) AggregationNode(com.facebook.presto.spi.plan.AggregationNode) Map(java.util.Map)

Example 93 with VariableReferenceExpression

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

the class RowNumberMatcher method detailMatches.

@Override
public MatchResult detailMatches(PlanNode node, StatsProvider stats, Session session, Metadata metadata, SymbolAliases symbolAliases) {
    checkState(shapeMatches(node), "Plan testing framework error: shapeMatches returned false in detailMatches in %s", this.getClass().getName());
    RowNumberNode rowNumberNode = (RowNumberNode) node;
    if (!partitionBy.map(expectedPartitionBy -> expectedPartitionBy.stream().map(alias -> alias.toSymbol(symbolAliases)).map(Symbol::getName).collect(toImmutableList()).equals(rowNumberNode.getPartitionBy().stream().map(VariableReferenceExpression::getName).collect(toImmutableList()))).orElse(true)) {
        return NO_MATCH;
    }
    if (!maxRowCountPerPartition.map(expectedMaxRowCountPerPartition -> expectedMaxRowCountPerPartition.equals(rowNumberNode.getMaxRowCountPerPartition())).orElse(true)) {
        return NO_MATCH;
    }
    if (!rowNumberSymbol.map(expectedRowNumberSymbol -> expectedRowNumberSymbol.toSymbol(symbolAliases).getName().equals(rowNumberNode.getRowNumberVariable().getName())).orElse(true)) {
        return NO_MATCH;
    }
    if (!hashSymbol.map(expectedHashSymbol -> expectedHashSymbol.map(symbolAlias -> symbolAlias.toSymbol(symbolAliases)).map(Symbol::getName).equals(rowNumberNode.getHashVariable().map(VariableReferenceExpression::getName))).orElse(true)) {
        return NO_MATCH;
    }
    return match();
}
Also used : RowNumberNode(com.facebook.presto.sql.planner.plan.RowNumberNode) Session(com.facebook.presto.Session) ImmutableList.toImmutableList(com.google.common.collect.ImmutableList.toImmutableList) StatsProvider(com.facebook.presto.cost.StatsProvider) VariableReferenceExpression(com.facebook.presto.spi.relation.VariableReferenceExpression) Preconditions.checkState(com.google.common.base.Preconditions.checkState) PlanNode(com.facebook.presto.spi.plan.PlanNode) List(java.util.List) Symbol(com.facebook.presto.sql.planner.Symbol) MatchResult.match(com.facebook.presto.sql.planner.assertions.MatchResult.match) Objects.requireNonNull(java.util.Objects.requireNonNull) Optional(java.util.Optional) NO_MATCH(com.facebook.presto.sql.planner.assertions.MatchResult.NO_MATCH) Metadata(com.facebook.presto.metadata.Metadata) PlanMatchPattern.node(com.facebook.presto.sql.planner.assertions.PlanMatchPattern.node) MoreObjects.toStringHelper(com.google.common.base.MoreObjects.toStringHelper) Symbol(com.facebook.presto.sql.planner.Symbol) VariableReferenceExpression(com.facebook.presto.spi.relation.VariableReferenceExpression) RowNumberNode(com.facebook.presto.sql.planner.plan.RowNumberNode)

Example 94 with VariableReferenceExpression

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

the class ColumnReference method getAssignedVariable.

@Override
public Optional<VariableReferenceExpression> getAssignedVariable(PlanNode node, Session session, Metadata metadata, SymbolAliases symbolAliases) {
    TableHandle tableHandle;
    Map<VariableReferenceExpression, ColumnHandle> assignments;
    if (node instanceof TableScanNode) {
        TableScanNode tableScanNode = (TableScanNode) node;
        tableHandle = tableScanNode.getTable();
        assignments = tableScanNode.getAssignments();
    } else if (node instanceof IndexSourceNode) {
        IndexSourceNode indexSourceNode = (IndexSourceNode) node;
        tableHandle = indexSourceNode.getTableHandle();
        assignments = indexSourceNode.getAssignments();
    } else {
        return Optional.empty();
    }
    TableMetadata tableMetadata = metadata.getTableMetadata(session, tableHandle);
    String actualTableName = tableMetadata.getTable().getTableName();
    // Wrong table -> doesn't match.
    if (!tableName.equalsIgnoreCase(actualTableName)) {
        return Optional.empty();
    }
    Optional<ColumnHandle> columnHandle = getColumnHandle(tableHandle, session, metadata);
    checkState(columnHandle.isPresent(), format("Table %s doesn't have column %s. Typo in test?", tableName, columnName));
    return getAssignedVariable(assignments, columnHandle.get());
}
Also used : TableMetadata(com.facebook.presto.metadata.TableMetadata) ColumnHandle(com.facebook.presto.spi.ColumnHandle) TableScanNode(com.facebook.presto.spi.plan.TableScanNode) VariableReferenceExpression(com.facebook.presto.spi.relation.VariableReferenceExpression) IndexSourceNode(com.facebook.presto.sql.planner.plan.IndexSourceNode) TableHandle(com.facebook.presto.spi.TableHandle)

Example 95 with VariableReferenceExpression

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

the class DynamicFilterMatcher method match.

private boolean match() {
    checkState(symbolAliases != null, "symbolAliases is null");
    // both nodes must be provided to do the matching
    if (filterNode == null || joinNode == null) {
        return true;
    }
    Map<String, VariableReferenceExpression> idToProbeSymbolMap = extractDynamicFilters(filterNode.getPredicate()).getDynamicConjuncts().stream().collect(toImmutableMap(DynamicFilters.DynamicFilterPlaceholder::getId, filter -> (VariableReferenceExpression) filter.getInput()));
    Map<String, VariableReferenceExpression> idToBuildSymbolMap = joinNode.getDynamicFilters();
    if (idToProbeSymbolMap == null) {
        return false;
    }
    if (idToProbeSymbolMap.size() != expectedDynamicFilters.size()) {
        return false;
    }
    Map<Symbol, Symbol> actual = new HashMap<>();
    for (Map.Entry<String, VariableReferenceExpression> idToProbeSymbol : idToProbeSymbolMap.entrySet()) {
        String id = idToProbeSymbol.getKey();
        VariableReferenceExpression probe = idToProbeSymbol.getValue();
        VariableReferenceExpression build = idToBuildSymbolMap.get(id);
        if (build == null) {
            return false;
        }
        actual.put(new Symbol(probe.getName()), new Symbol(build.getName()));
    }
    Map<Symbol, Symbol> expected = expectedDynamicFilters.entrySet().stream().collect(toImmutableMap(entry -> entry.getKey().toSymbol(symbolAliases), entry -> entry.getValue().toSymbol(symbolAliases)));
    return expected.equals(actual);
}
Also used : RowExpression(com.facebook.presto.spi.relation.RowExpression) JoinNode(com.facebook.presto.sql.planner.plan.JoinNode) Session(com.facebook.presto.Session) ImmutableList.toImmutableList(com.google.common.collect.ImmutableList.toImmutableList) StatsProvider(com.facebook.presto.cost.StatsProvider) VariableReferenceExpression(com.facebook.presto.spi.relation.VariableReferenceExpression) HashMap(java.util.HashMap) RowExpressionDeterminismEvaluator(com.facebook.presto.sql.relational.RowExpressionDeterminismEvaluator) Preconditions.checkState(com.google.common.base.Preconditions.checkState) FilterNode(com.facebook.presto.spi.plan.FilterNode) PlanNode(com.facebook.presto.spi.plan.PlanNode) ImmutableMap.toImmutableMap(com.google.common.collect.ImmutableMap.toImmutableMap) Expression(com.facebook.presto.sql.tree.Expression) LogicalRowExpressions(com.facebook.presto.expressions.LogicalRowExpressions) Symbol(com.facebook.presto.sql.planner.Symbol) Map(java.util.Map) Objects.requireNonNull(java.util.Objects.requireNonNull) Optional(java.util.Optional) FunctionResolution(com.facebook.presto.sql.relational.FunctionResolution) DynamicFilters(com.facebook.presto.expressions.DynamicFilters) Metadata(com.facebook.presto.metadata.Metadata) Joiner(com.google.common.base.Joiner) DynamicFilters.extractDynamicFilters(com.facebook.presto.expressions.DynamicFilters.extractDynamicFilters) MoreObjects.toStringHelper(com.google.common.base.MoreObjects.toStringHelper) DynamicFilters(com.facebook.presto.expressions.DynamicFilters) DynamicFilters.extractDynamicFilters(com.facebook.presto.expressions.DynamicFilters.extractDynamicFilters) HashMap(java.util.HashMap) VariableReferenceExpression(com.facebook.presto.spi.relation.VariableReferenceExpression) Symbol(com.facebook.presto.sql.planner.Symbol) HashMap(java.util.HashMap) ImmutableMap.toImmutableMap(com.google.common.collect.ImmutableMap.toImmutableMap) Map(java.util.Map)

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