private void map(Symbol symbol, Symbol canonical) { Preconditions.checkArgument(!symbol.equals(canonical), "Can't map symbol to itself: %s", symbol); mapping.put(symbol, canonical); }
public Symbol map(Symbol symbol) { Symbol canonical = symbol; while (mapping.containsKey(canonical) && !mapping.get(canonical).equals(canonical)) { canonical = mapping.get(canonical); } return canonical; }
private static List<Symbol> removeSymbol(List<Symbol> symbols, Symbol symbolToRemove) { return symbols.stream() .filter(symbol -> !symbolToRemove.equals(symbol)) .collect(toImmutableList()); } }
allSymbols.stream().collect(Collectors.toMap( symbol -> symbol, symbol -> (symbol.equals(duplicatedDistinctSymbol) ? distinctSymbol : symbol))), ImmutableList.of(), groupSymbol);
public static Optional<Symbol> filterOrRewrite(Collection<Symbol> columns, Collection<JoinNode.EquiJoinClause> equalities, Symbol column) { // symbol is exposed directly, so no translation needed if (columns.contains(column)) { return Optional.of(column); } // if the column is part of the equality conditions and its counterpart // is exposed, use that, instead for (JoinNode.EquiJoinClause equality : equalities) { if (equality.getLeft().equals(column) && columns.contains(equality.getRight())) { return Optional.of(equality.getRight()); } else if (equality.getRight().equals(column) && columns.contains(equality.getLeft())) { return Optional.of(equality.getLeft()); } } return Optional.empty(); }
private void mapExchangeNodeOutputToInputSymbols(ExchangeNode node) { checkState(node.getInputs().size() == 1); for (int symbolIndex = 0; symbolIndex < node.getOutputSymbols().size(); symbolIndex++) { Symbol canonicalOutput = canonicalize(node.getOutputSymbols().get(symbolIndex)); Symbol canonicalInput = canonicalize(node.getInputs().get(0).get(symbolIndex)); if (!canonicalOutput.equals(canonicalInput)) { map(canonicalOutput, canonicalInput); } } }
@Override protected Optional<PlanNode> pushDownProjectOff(PlanNodeIdAllocator idAllocator, MarkDistinctNode markDistinctNode, Set<Symbol> referencedOutputs) { if (!referencedOutputs.contains(markDistinctNode.getMarkerSymbol())) { return Optional.of(markDistinctNode.getSource()); } Set<Symbol> requiredInputs = Streams.concat( referencedOutputs.stream() .filter(symbol -> !symbol.equals(markDistinctNode.getMarkerSymbol())), markDistinctNode.getDistinctSymbols().stream(), markDistinctNode.getHashSymbol().map(Stream::of).orElse(Stream.empty())) .collect(toImmutableSet()); return restrictChildOutputs(idAllocator, markDistinctNode, requiredInputs); } }
@Override protected Optional<PlanNode> pushDownProjectOff(PlanNodeIdAllocator idAllocator, SemiJoinNode semiJoinNode, Set<Symbol> referencedOutputs) { if (!referencedOutputs.contains(semiJoinNode.getSemiJoinOutput())) { return Optional.of(semiJoinNode.getSource()); } Set<Symbol> requiredSourceInputs = Streams.concat( referencedOutputs.stream() .filter(symbol -> !symbol.equals(semiJoinNode.getSemiJoinOutput())), Stream.of(semiJoinNode.getSourceJoinSymbol()), semiJoinNode.getSourceHashSymbol().map(Stream::of).orElse(Stream.empty())) .collect(toImmutableSet()); return restrictOutputs(idAllocator, semiJoinNode.getSource(), requiredSourceInputs) .map(newSource -> semiJoinNode.replaceChildren(ImmutableList.of( newSource, semiJoinNode.getFilteringSource()))); } }
private void mapExchangeNodeSymbols(ExchangeNode node) { if (node.getInputs().size() == 1) { mapExchangeNodeOutputToInputSymbols(node); return; } // Mapping from list [node.getInput(0).get(symbolIndex), node.getInput(1).get(symbolIndex), ...] to node.getOutputSymbols(symbolIndex). // All symbols are canonical. Map<List<Symbol>, Symbol> inputsToOutputs = new HashMap<>(); // Map each same list of input symbols [I1, I2, ..., In] to the same output symbol O for (int symbolIndex = 0; symbolIndex < node.getOutputSymbols().size(); symbolIndex++) { Symbol canonicalOutput = canonicalize(node.getOutputSymbols().get(symbolIndex)); List<Symbol> canonicalInputs = canonicalizeExchangeNodeInputs(node, symbolIndex); Symbol output = inputsToOutputs.get(canonicalInputs); if (output == null || canonicalOutput.equals(output)) { inputsToOutputs.put(canonicalInputs, canonicalOutput); } else { map(canonicalOutput, output); } } }
@Override public PlanNode visitMarkDistinct(MarkDistinctNode node, RewriteContext<Set<Symbol>> context) { if (!context.get().contains(node.getMarkerSymbol())) { return context.rewrite(node.getSource(), context.get()); } ImmutableSet.Builder<Symbol> expectedInputs = ImmutableSet.<Symbol>builder() .addAll(node.getDistinctSymbols()) .addAll(context.get().stream() .filter(symbol -> !symbol.equals(node.getMarkerSymbol())) .collect(toImmutableList())); if (node.getHashSymbol().isPresent()) { expectedInputs.add(node.getHashSymbol().get()); } PlanNode source = context.rewrite(node.getSource(), expectedInputs.build()); return new MarkDistinctNode(node.getId(), source, node.getMarkerSymbol(), node.getDistinctSymbols(), node.getHashSymbol()); }
if (!symbol.equals(entry.getKey())) { map(entry.getKey(), symbol);
if (!aggregateInfo.isPresent() || !aggregateInfo.get().getMask().equals(node.getMarkerSymbol())) { return context.defaultRewrite(node, Optional.empty());
Symbol newSymbol = symbolAllocator.newSymbol(entry.getKey().toSymbolReference(), symbolAllocator.getTypes().get(entry.getKey())); aggregationOutputSymbolsMapBuilder.put(newSymbol, entry.getKey()); if (!duplicatedDistinctSymbol.equals(distinctSymbol)) {
private PlanNode rewriteFilterSource(FilterNode filterNode, PlanNode source, Symbol rowNumberSymbol, int upperBound) { ExtractionResult extractionResult = fromPredicate(metadata, session, filterNode.getPredicate(), types); TupleDomain<Symbol> tupleDomain = extractionResult.getTupleDomain(); if (!isEqualRange(tupleDomain, rowNumberSymbol, upperBound)) { return new FilterNode(filterNode.getId(), source, filterNode.getPredicate()); } // Remove the row number domain because it is absorbed into the node Map<Symbol, Domain> newDomains = tupleDomain.getDomains().get().entrySet().stream() .filter(entry -> !entry.getKey().equals(rowNumberSymbol)) .collect(toMap(Map.Entry::getKey, Map.Entry::getValue)); // Construct a new predicate TupleDomain<Symbol> newTupleDomain = TupleDomain.withColumnDomains(newDomains); Expression newPredicate = ExpressionUtils.combineConjuncts( extractionResult.getRemainingExpression(), domainTranslator.toPredicate(newTupleDomain)); if (newPredicate.equals(BooleanLiteral.TRUE_LITERAL)) { return source; } return new FilterNode(filterNode.getId(), source, newPredicate); }
.map(expectedRowNumberSymbol -> expectedRowNumberSymbol.toSymbol(symbolAliases) .equals(rowNumberNode.getRowNumberSymbol())) .orElse(true)) { return NO_MATCH;
.map(expectedRowNumberSymbol -> expectedRowNumberSymbol.toSymbol(symbolAliases) .equals(topNRowNumberNode.getRowNumberSymbol())) .orElse(true)) { return NO_MATCH;
static boolean orderingSchemeMatches(List<Ordering> expectedOrderBy, OrderingScheme orderingScheme, SymbolAliases symbolAliases) { if (expectedOrderBy.size() != orderingScheme.getOrderBy().size()) { return false; } for (int i = 0; i < expectedOrderBy.size(); ++i) { Ordering ordering = expectedOrderBy.get(i); Symbol symbol = Symbol.from(symbolAliases.get(ordering.getField())); if (!symbol.equals(orderingScheme.getOrderBy().get(i))) { return false; } if (!ordering.getSortOrder().equals(orderingScheme.getOrdering(symbol))) { return false; } } return true; } }
Symbol output = exchange.getOutputSymbols().get(outputIndex); Symbol input = exchange.getInputs().get(i).get(outputIndex); if (!output.equals(input)) { mappingsBuilder.put(output, input);
for (Symbol column : nodePartitioning.getColumns()) { for (JoinNode.EquiJoinClause equality : node.getCriteria()) { if (equality.getLeft().equals(column) || equality.getRight().equals(column)) { coalesceExpressions.add(new CoalesceExpression(ImmutableList.of(equality.getLeft().toSymbolReference(), equality.getRight().toSymbolReference())));
ImmutableMap.Builder<Symbol, Symbol> outputNonDistinctAggregateSymbols = ImmutableMap.builder(); for (Symbol symbol : source.getOutputSymbols()) { if (distinctSymbol.equals(symbol)) { Symbol newSymbol = symbolAllocator.newSymbol("expr", symbolAllocator.getTypes().get(symbol)); aggregateInfo.setNewDistinctAggregateSymbol(newSymbol);