@Override public PlanNode visitValues(ValuesNode node, RewriteContext<Void> context) { List<List<Expression>> canonicalizedRows = node.getRows().stream() .map(this::canonicalize) .collect(toImmutableList()); List<Symbol> canonicalizedOutputSymbols = canonicalizeAndDistinct(node.getOutputSymbols()); checkState(node.getOutputSymbols().size() == canonicalizedOutputSymbols.size(), "Values output symbols were pruned"); return new ValuesNode( node.getId(), canonicalizedOutputSymbols, canonicalizedRows); }
@Override public Void visitValues(ValuesNode node, Integer indent) { print(indent, "- Values => [%s]", formatOutputs(node.getOutputSymbols())); printPlanNodesStatsAndCost(indent + 2, node); printStats(indent + 2, node.getId()); for (List<Expression> row : node.getRows()) { print(indent + 2, "(" + Joiner.on(", ").join(row) + ")"); } return null; }
@Override public PlanNode visitValues(ValuesNode node, RewriteContext<Void> context) { ValuesNode rewrittenNode = (ValuesNode) context.defaultRewrite(node); List<List<Expression>> rewrittenRows = rewrittenNode.getRows().stream() .map(row -> row.stream() .map(column -> replaceExpression(column, mapping)) .collect(toImmutableList())) .collect(toImmutableList()); return new ValuesNode( idAllocator.getNextId(), rewrittenNode.getOutputSymbols(), rewrittenRows); } }
public ValuesNode values(PlanNodeId id, List<Symbol> columns, List<List<Expression>> rows) { return new ValuesNode(id, columns, rows); }
@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()); for (int symbolId = 0; symbolId < node.getOutputSymbols().size(); ++symbolId) { Symbol symbol = node.getOutputSymbols().get(symbolId); List<Object> symbolValues = getSymbolValues(node, symbolId, session, types.get(symbol)); statsBuilder.addSymbolStatistics(symbol, buildSymbolStatistics(symbolValues, session, types.get(symbol))); } return Optional.of(statsBuilder.build()); }
@Override public Boolean visitValues(ValuesNode node, Void context) { return node.getRows().size() == 1; }
@Override public Void visitValues(ValuesNode node, Void context) { builder.addAll(node.getOutputSymbols()); return null; }
private RelationPlan planImplicitTable() { List<Expression> emptyRow = ImmutableList.of(); Scope scope = Scope.create(); return new RelationPlan( new ValuesNode(idAllocator.getNextId(), ImmutableList.of(), ImmutableList.of(emptyRow)), scope, ImmutableList.of()); }
if (node.getRows().isEmpty()) { OperatorFactory operatorFactory = new ValuesOperatorFactory(context.getNextOperatorId(), node.getId(), ImmutableList.of()); return new PhysicalOperation(operatorFactory, makeLayout(node), context, UNGROUPED_EXECUTION); List<Type> outputTypes = getSymbolTypes(node.getOutputSymbols(), context.getTypes()); PageBuilder pageBuilder = new PageBuilder(node.getRows().size(), outputTypes); for (List<Expression> row : node.getRows()) { pageBuilder.declarePosition(); Map<NodeRef<Expression>, Type> expressionTypes = getExpressionTypes( OperatorFactory operatorFactory = new ValuesOperatorFactory(context.getNextOperatorId(), node.getId(), ImmutableList.of(pageBuilder.build())); return new PhysicalOperation(operatorFactory, makeLayout(node), context, UNGROUPED_EXECUTION);
@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()); ValuesNode valuesNode = (ValuesNode) node; if (!expectedRows.map(rows -> rows.equals(valuesNode.getRows())).orElse(true)) { return NO_MATCH; } return match(SymbolAliases.builder() .putAll(Maps.transformValues(outputSymbolAliases, index -> valuesNode.getOutputSymbols().get(index).toSymbolReference())) .build()); }
@Override protected Optional<PlanNode> pushDownProjectOff(PlanNodeIdAllocator idAllocator, ValuesNode valuesNode, Set<Symbol> referencedOutputs) { List<Symbol> newOutputs = filteredCopy(valuesNode.getOutputSymbols(), referencedOutputs::contains); // for each output of project, the corresponding column in the values node int[] mapping = new int[newOutputs.size()]; for (int i = 0; i < mapping.length; i++) { mapping[i] = valuesNode.getOutputSymbols().indexOf(newOutputs.get(i)); } ImmutableList.Builder<List<Expression>> rowsBuilder = ImmutableList.builder(); for (List<Expression> row : valuesNode.getRows()) { rowsBuilder.add(Arrays.stream(mapping) .mapToObj(row::get) .collect(Collectors.toList())); } return Optional.of(new ValuesNode(valuesNode.getId(), newOutputs, rowsBuilder.build())); } }
@Override public Result apply(SampleNode sample, Captures captures, Context context) { return Result.ofPlanNode(new ValuesNode(sample.getId(), sample.getOutputSymbols(), ImmutableList.of())); } }
@Override public PhysicalOperation visitValues(ValuesNode node, LocalExecutionPlanContext context) { List<Type> outputTypes = new ArrayList<>(); for (Symbol symbol : node.getOutputSymbols()) { Type type = requireNonNull(context.getTypes().get(symbol), format("No type for symbol %s", symbol)); outputTypes.add(type); } if (node.getRows().isEmpty()) { OperatorFactory operatorFactory = new ValuesOperatorFactory(context.getNextOperatorId(), node.getId(), outputTypes, ImmutableList.of()); return new PhysicalOperation(operatorFactory, makeLayout(node)); } PageBuilder pageBuilder = new PageBuilder(outputTypes); for (List<Expression> row : node.getRows()) { pageBuilder.declarePosition(); IdentityHashMap<Expression, Type> expressionTypes = getExpressionTypes( context.getSession(), metadata, sqlParser, ImmutableMap.<Symbol, Type>of(), ImmutableList.copyOf(row)); for (int i = 0; i < row.size(); i++) { // evaluate the literal value Object result = ExpressionInterpreter.expressionInterpreter(row.get(i), metadata, context.getSession(), expressionTypes).evaluate(0); writeNativeValue(outputTypes.get(i), pageBuilder.getBlockBuilder(i), result); } } OperatorFactory operatorFactory = new ValuesOperatorFactory(context.getNextOperatorId(), node.getId(), outputTypes, ImmutableList.of(pageBuilder.build())); return new PhysicalOperation(operatorFactory, makeLayout(node)); }
@Override public Void visitValues(ValuesNode node, Integer indent) { print(indent, "- Values => [%s]", formatOutputs(node.getOutputSymbols())); for (List<Expression> row : node.getRows()) { print(indent + 2, "(" + Joiner.on(", ").join(row) + ")"); } return null; }
@Override public Range<Long> visitValues(ValuesNode node, Void context) { return Range.singleton((long) node.getRows().size()); }
@Override public PlanNode visitValues(ValuesNode node, RewriteContext<Set<Symbol>> context) { ImmutableList.Builder<Symbol> rewrittenOutputSymbolsBuilder = ImmutableList.builder(); ImmutableList.Builder<ImmutableList.Builder<Expression>> rowBuildersBuilder = ImmutableList.builder(); // Initialize builder for each row for (int i = 0; i < node.getRows().size(); i++) { rowBuildersBuilder.add(ImmutableList.builder()); } ImmutableList<ImmutableList.Builder<Expression>> rowBuilders = rowBuildersBuilder.build(); for (int i = 0; i < node.getOutputSymbols().size(); i++) { Symbol outputSymbol = node.getOutputSymbols().get(i); // If output symbol is used if (context.get().contains(outputSymbol)) { rewrittenOutputSymbolsBuilder.add(outputSymbol); // Add the value of the output symbol for each row for (int j = 0; j < node.getRows().size(); j++) { rowBuilders.get(j).add(node.getRows().get(j).get(i)); } } } List<List<Expression>> rewrittenRows = rowBuilders.stream() .map(ImmutableList.Builder::build) .collect(toImmutableList()); return new ValuesNode(node.getId(), rewrittenOutputSymbolsBuilder.build(), rewrittenRows); }
@Override public Result apply(LimitNode limit, Captures captures, Context context) { return Result.ofPlanNode(new ValuesNode(limit.getId(), limit.getOutputSymbols(), ImmutableList.of())); } }
@Override public Void visitValues(ValuesNode node, ImmutableList.Builder<Expression> context) { node.getRows().forEach(context::addAll); return super.visitValues(node, context); }
@Override public Result apply(ValuesNode valuesNode, Captures captures, Context context) { boolean anyRewritten = false; ImmutableList.Builder<List<Expression>> rows = ImmutableList.builder(); for (List<Expression> row : valuesNode.getRows()) { ImmutableList.Builder<Expression> newRow = ImmutableList.builder(); for (Expression expression : row) { Expression rewritten = rewriter.rewrite(expression, context); if (!expression.equals(rewritten)) { anyRewritten = true; } newRow.add(rewritten); } rows.add(newRow.build()); } if (anyRewritten) { return Result.ofPlanNode(new ValuesNode(valuesNode.getId(), valuesNode.getOutputSymbols(), rows.build())); } return Result.empty(); } }