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()));
}
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());
}
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());
}
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());
}
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);
}
Aggregations