Exemple #1
0
  @Override
  protected RelationPlan visitUnnest(Unnest node, Void context) {
    TupleDescriptor descriptor = analysis.getOutputDescriptor(node);
    ImmutableList.Builder<Symbol> outputSymbolsBuilder = ImmutableList.builder();
    for (Field field : descriptor.getVisibleFields()) {
      Symbol symbol = symbolAllocator.newSymbol(field);
      outputSymbolsBuilder.add(symbol);
    }
    List<Symbol> unnestedSymbols = outputSymbolsBuilder.build();

    // If we got here, then we must be unnesting a constant, and not be in a join (where there could
    // be column references)
    ImmutableList.Builder<Symbol> argumentSymbols = ImmutableList.builder();
    ImmutableList.Builder<Expression> values = ImmutableList.builder();
    ImmutableMap.Builder<Symbol, List<Symbol>> unnestSymbols = ImmutableMap.builder();
    Iterator<Symbol> unnestedSymbolsIterator = unnestedSymbols.iterator();
    for (Expression expression : node.getExpressions()) {
      Object constantValue =
          evaluateConstantExpression(expression, analysis.getCoercions(), metadata, session);
      Type type = analysis.getType(expression);
      values.add(LiteralInterpreter.toExpression(constantValue, type));
      Symbol inputSymbol = symbolAllocator.newSymbol(expression, type);
      argumentSymbols.add(inputSymbol);
      if (type instanceof ArrayType) {
        unnestSymbols.put(inputSymbol, ImmutableList.of(unnestedSymbolsIterator.next()));
      } else if (type instanceof MapType) {
        unnestSymbols.put(
            inputSymbol,
            ImmutableList.of(unnestedSymbolsIterator.next(), unnestedSymbolsIterator.next()));
      } else {
        throw new IllegalArgumentException("Unsupported type for UNNEST: " + type);
      }
    }
    Optional<Symbol> ordinalitySymbol =
        node.isWithOrdinality() ? Optional.of(unnestedSymbolsIterator.next()) : Optional.empty();
    checkState(
        !unnestedSymbolsIterator.hasNext(),
        "Not all output symbols were matched with input symbols");
    ValuesNode valuesNode =
        new ValuesNode(
            idAllocator.getNextId(),
            argumentSymbols.build(),
            ImmutableList.<List<Expression>>of(values.build()));

    UnnestNode unnestNode =
        new UnnestNode(
            idAllocator.getNextId(),
            valuesNode,
            ImmutableList.<Symbol>of(),
            unnestSymbols.build(),
            ordinalitySymbol);
    return new RelationPlan(unnestNode, descriptor, unnestedSymbols, Optional.empty());
  }
Exemple #2
0
  private RelationPlan planCrossJoinUnnest(RelationPlan leftPlan, Join joinNode, Unnest node) {
    TupleDescriptor outputDescriptor = analysis.getOutputDescriptor(joinNode);
    TupleDescriptor unnestOutputDescriptor = analysis.getOutputDescriptor(node);
    // Create symbols for the result of unnesting
    ImmutableList.Builder<Symbol> unnestedSymbolsBuilder = ImmutableList.builder();
    for (Field field : unnestOutputDescriptor.getVisibleFields()) {
      Symbol symbol = symbolAllocator.newSymbol(field);
      unnestedSymbolsBuilder.add(symbol);
    }
    ImmutableList<Symbol> unnestedSymbols = unnestedSymbolsBuilder.build();

    // Add a projection for all the unnest arguments
    PlanBuilder planBuilder = initializePlanBuilder(leftPlan);
    planBuilder = appendProjections(planBuilder, node.getExpressions());
    TranslationMap translations = planBuilder.getTranslations();
    ProjectNode projectNode =
        checkType(planBuilder.getRoot(), ProjectNode.class, "planBuilder.getRoot()");

    ImmutableMap.Builder<Symbol, List<Symbol>> unnestSymbols = ImmutableMap.builder();
    UnmodifiableIterator<Symbol> unnestedSymbolsIterator = unnestedSymbols.iterator();
    for (Expression expression : node.getExpressions()) {
      Type type = analysis.getType(expression);
      Symbol inputSymbol = translations.get(expression);
      if (type instanceof ArrayType) {
        unnestSymbols.put(inputSymbol, ImmutableList.of(unnestedSymbolsIterator.next()));
      } else if (type instanceof MapType) {
        unnestSymbols.put(
            inputSymbol,
            ImmutableList.of(unnestedSymbolsIterator.next(), unnestedSymbolsIterator.next()));
      } else {
        throw new IllegalArgumentException("Unsupported type for UNNEST: " + type);
      }
    }
    Optional<Symbol> ordinalitySymbol =
        node.isWithOrdinality() ? Optional.of(unnestedSymbolsIterator.next()) : Optional.empty();
    checkState(
        !unnestedSymbolsIterator.hasNext(),
        "Not all output symbols were matched with input symbols");

    UnnestNode unnestNode =
        new UnnestNode(
            idAllocator.getNextId(),
            projectNode,
            leftPlan.getOutputSymbols(),
            unnestSymbols.build(),
            ordinalitySymbol);
    return new RelationPlan(
        unnestNode, outputDescriptor, unnestNode.getOutputSymbols(), Optional.empty());
  }