Search in sources :

Example 96 with VariableReferenceExpression

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

the class GroupIdMatcher 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());
    GroupIdNode groudIdNode = (GroupIdNode) node;
    List<List<VariableReferenceExpression>> actualGroups = groudIdNode.getGroupingSets();
    List<VariableReferenceExpression> actualAggregationArguments = groudIdNode.getAggregationArguments();
    if (actualGroups.size() != groups.size()) {
        return NO_MATCH;
    }
    for (int i = 0; i < actualGroups.size(); i++) {
        if (!AggregationMatcher.matches(groups.get(i), actualGroups.get(i), symbolAliases)) {
            return NO_MATCH;
        }
    }
    if (!AggregationMatcher.matches(identityMappings.keySet(), actualAggregationArguments, symbolAliases)) {
        return NO_MATCH;
    }
    return match(groupIdAlias, createSymbolReference(groudIdNode.getGroupIdVariable()));
}
Also used : GroupIdNode(com.facebook.presto.sql.planner.plan.GroupIdNode) VariableReferenceExpression(com.facebook.presto.spi.relation.VariableReferenceExpression) List(java.util.List)

Example 97 with VariableReferenceExpression

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

the class TableScanStatsRule method doCalculate.

@Override
protected Optional<PlanNodeStatsEstimate> doCalculate(TableScanNode node, StatsProvider sourceStats, Lookup lookup, Session session, TypeProvider types) {
    // TODO Construct predicate like AddExchanges's LayoutConstraintEvaluator
    Constraint<ColumnHandle> constraint = new Constraint<>(node.getCurrentConstraint());
    TableStatistics tableStatistics = metadata.getTableStatistics(session, node.getTable(), ImmutableList.copyOf(node.getAssignments().values()), constraint);
    Map<VariableReferenceExpression, VariableStatsEstimate> outputVariableStats = new HashMap<>();
    for (Map.Entry<VariableReferenceExpression, ColumnHandle> entry : node.getAssignments().entrySet()) {
        Optional<ColumnStatistics> columnStatistics = Optional.ofNullable(tableStatistics.getColumnStatistics().get(entry.getValue()));
        outputVariableStats.put(entry.getKey(), columnStatistics.map(statistics -> StatsUtil.toVariableStatsEstimate(tableStatistics, statistics)).orElse(VariableStatsEstimate.unknown()));
    }
    return Optional.of(PlanNodeStatsEstimate.builder().setOutputRowCount(tableStatistics.getRowCount().getValue()).setTotalSize(tableStatistics.getTotalSize().getValue()).setConfident(true).addVariableStatistics(outputVariableStats).build());
}
Also used : ColumnStatistics(com.facebook.presto.spi.statistics.ColumnStatistics) ColumnHandle(com.facebook.presto.spi.ColumnHandle) Constraint(com.facebook.presto.spi.Constraint) HashMap(java.util.HashMap) VariableReferenceExpression(com.facebook.presto.spi.relation.VariableReferenceExpression) TableStatistics(com.facebook.presto.spi.statistics.TableStatistics) HashMap(java.util.HashMap) Map(java.util.Map)

Example 98 with VariableReferenceExpression

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

the class ValuesStatsRule method calculate.

@Override
public Optional<PlanNodeStatsEstimate> calculate(ValuesNode node, StatsProvider sourceStats, Lookup lookup, Session session, TypeProvider types) {
    PlanNodeStatsEstimate.Builder statsBuilder = PlanNodeStatsEstimate.builder();
    statsBuilder.setOutputRowCount(node.getRows().size()).setConfident(true);
    for (int variableId = 0; variableId < node.getOutputVariables().size(); ++variableId) {
        VariableReferenceExpression variable = node.getOutputVariables().get(variableId);
        List<Object> symbolValues = getVariableValues(node, variableId, session, variable.getType());
        statsBuilder.addVariableStatistics(variable, buildVariableStatistics(symbolValues, session, variable.getType()));
    }
    return Optional.of(statsBuilder.build());
}
Also used : VariableReferenceExpression(com.facebook.presto.spi.relation.VariableReferenceExpression)

Example 99 with VariableReferenceExpression

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

the class UnnestStatsRule method calculate.

@Override
public Optional<PlanNodeStatsEstimate> calculate(UnnestNode node, StatsProvider statsProvider, Lookup lookup, Session session, TypeProvider types) {
    PlanNodeStatsEstimate sourceStats = statsProvider.getStats(node.getSource());
    PlanNodeStatsEstimate.Builder calculatedStats = PlanNodeStatsEstimate.builder();
    if (sourceStats.getOutputRowCount() > UPPER_BOUND_ROW_COUNT_FOR_ESTIMATION) {
        return Optional.empty();
    }
    // Since we don't have stats for cardinality about the unnest column, we cannot estimate the row count.
    // However, when the source row count is low, the error would not matter much in query optimization.
    // Thus we'd still populate the inaccurate numbers just so stats are populated to enable optimization
    // potential.
    calculatedStats.setOutputRowCount(sourceStats.getOutputRowCount());
    for (VariableReferenceExpression variable : node.getReplicateVariables()) {
        calculatedStats.addVariableStatistics(variable, sourceStats.getVariableStatistics(variable));
    }
    for (Map.Entry<VariableReferenceExpression, List<VariableReferenceExpression>> entry : node.getUnnestVariables().entrySet()) {
        List<VariableReferenceExpression> unnestToVariables = entry.getValue();
        VariableStatsEstimate stats = sourceStats.getVariableStatistics(entry.getKey());
        for (VariableReferenceExpression variable : unnestToVariables) {
            // This is a very conservative way on estimating stats after unnest. We assume each symbol
            // after unnest would have as much data as the symbol before unnest. This would over
            // estimate, which are more likely to mean we'd loose an optimization opportunity, but at
            // least it won't cause false optimizations.
            calculatedStats.addVariableStatistics(variable, VariableStatsEstimate.builder().setAverageRowSize(stats.getAverageRowSize()).build());
        }
    }
    if (node.getOrdinalityVariable().isPresent()) {
        calculatedStats.addVariableStatistics(node.getOrdinalityVariable().get(), VariableStatsEstimate.builder().setLowValue(0).setNullsFraction(0).build());
    }
    return Optional.of(calculatedStats.build());
}
Also used : VariableReferenceExpression(com.facebook.presto.spi.relation.VariableReferenceExpression) List(java.util.List) Map(java.util.Map)

Example 100 with VariableReferenceExpression

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

the class FunctionAssertions method interpret.

private Object interpret(Expression expression, Type expectedType, Session session) {
    Map<NodeRef<Expression>, Type> expressionTypes = getExpressionTypes(session, metadata, SQL_PARSER, SYMBOL_TYPES, expression, emptyList(), WarningCollector.NOOP);
    ExpressionInterpreter evaluator = ExpressionInterpreter.expressionInterpreter(expression, metadata, session, expressionTypes);
    Object result = evaluator.evaluate(variable -> {
        Symbol symbol = new Symbol(variable.getName());
        int position = 0;
        int channel = INPUT_MAPPING.get(new VariableReferenceExpression(Optional.empty(), symbol.getName(), SYMBOL_TYPES.get(symbol.toSymbolReference())));
        Type type = SYMBOL_TYPES.get(symbol.toSymbolReference());
        Block block = SOURCE_PAGE.getBlock(channel);
        if (block.isNull(position)) {
            return null;
        }
        Class<?> javaType = type.getJavaType();
        if (javaType == boolean.class) {
            return type.getBoolean(block, position);
        } else if (javaType == long.class) {
            return type.getLong(block, position);
        } else if (javaType == double.class) {
            return type.getDouble(block, position);
        } else if (javaType == Slice.class) {
            return type.getSlice(block, position);
        } else if (javaType == Block.class) {
            return type.getObject(block, position);
        } else {
            throw new UnsupportedOperationException("not yet implemented");
        }
    });
    // convert result from stack type to Type ObjectValue
    Block block = Utils.nativeValueToBlock(expectedType, result);
    return expectedType.getObjectValue(session.getSqlFunctionProperties(), block, 0);
}
Also used : NodeRef(com.facebook.presto.sql.tree.NodeRef) RowType(com.facebook.presto.common.type.RowType) Type(com.facebook.presto.common.type.Type) Symbol(com.facebook.presto.sql.planner.Symbol) VariableReferenceExpression(com.facebook.presto.spi.relation.VariableReferenceExpression) Slice(io.airlift.slice.Slice) ExpressionInterpreter(com.facebook.presto.sql.planner.ExpressionInterpreter) BlockAssertions.createIntsBlock(com.facebook.presto.block.BlockAssertions.createIntsBlock) BlockAssertions.createRowBlock(com.facebook.presto.block.BlockAssertions.createRowBlock) BlockAssertions.createBooleansBlock(com.facebook.presto.block.BlockAssertions.createBooleansBlock) BlockAssertions.createDoublesBlock(com.facebook.presto.block.BlockAssertions.createDoublesBlock) BlockAssertions.createLongsBlock(com.facebook.presto.block.BlockAssertions.createLongsBlock) BlockAssertions.createSlicesBlock(com.facebook.presto.block.BlockAssertions.createSlicesBlock) BlockAssertions.createTimestampsWithTimezoneBlock(com.facebook.presto.block.BlockAssertions.createTimestampsWithTimezoneBlock) BlockAssertions.createStringsBlock(com.facebook.presto.block.BlockAssertions.createStringsBlock) Block(com.facebook.presto.common.block.Block)

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