@Override
  public Expression visitUnion(UnionNode node, Void context) {
    Expression firstUnderlyingPredicate = node.getSources().get(0).accept(this, context);
    // Rewrite in terms of output symbols
    Expression firstOutputPredicate =
        ExpressionTreeRewriter.rewriteWith(
            new ExpressionSymbolInliner(node.outputSymbolMap(0)), firstUnderlyingPredicate);

    Set<Expression> conjuncts = ImmutableSet.copyOf(extractConjuncts(firstOutputPredicate));

    // Find the intersection of all predicates
    for (int i = 1; i < node.getSources().size(); i++) {
      Expression underlyingPredicate = node.getSources().get(i).accept(this, context);
      // Rewrite in terms of output symbols
      Expression outputPredicate =
          ExpressionTreeRewriter.rewriteWith(
              new ExpressionSymbolInliner(node.outputSymbolMap(i)), underlyingPredicate);

      // TODO: use a more precise way to determine overlapping conjuncts (e.g. commutative
      // predicates)
      conjuncts =
          Sets.intersection(conjuncts, ImmutableSet.copyOf(extractConjuncts(outputPredicate)));
    }

    return combineConjuncts(conjuncts);
  }
Ejemplo n.º 2
0
  private OperatorFactory compileFilterProject(
      Expression filter, Expression projection, ExpressionCompiler compiler) {
    filter = ExpressionTreeRewriter.rewriteWith(new SymbolToInputRewriter(INPUT_MAPPING), filter);
    projection =
        ExpressionTreeRewriter.rewriteWith(new SymbolToInputRewriter(INPUT_MAPPING), projection);

    IdentityHashMap<Expression, Type> expressionTypes =
        getExpressionTypesFromInput(
            TEST_SESSION, metadata, SQL_PARSER, INPUT_TYPES, ImmutableList.of(filter, projection));

    try {
      List<RowExpression> projections =
          ImmutableList.of(toRowExpression(projection, expressionTypes));
      PageProcessor processor =
          compiler.compilePageProcessor(toRowExpression(filter, expressionTypes), projections);

      return new FilterAndProjectOperator.FilterAndProjectOperatorFactory(
          0, processor, ImmutableList.of(expressionTypes.get(projection)));
    } catch (Throwable e) {
      if (e instanceof UncheckedExecutionException) {
        e = e.getCause();
      }
      throw new RuntimeException("Error compiling " + projection + ": " + e.getMessage(), e);
    }
  }
Ejemplo n.º 3
0
  public PlanBuilder appendProjections(
      Iterable<Expression> expressions,
      SymbolAllocator symbolAllocator,
      PlanNodeIdAllocator idAllocator) {
    TranslationMap translations = copyTranslations();

    ImmutableMap.Builder<Symbol, Expression> projections = ImmutableMap.builder();

    // add an identity projection for underlying plan
    for (Symbol symbol : getRoot().getOutputSymbols()) {
      projections.put(symbol, symbol.toSymbolReference());
    }

    ImmutableMap.Builder<Symbol, Expression> newTranslations = ImmutableMap.builder();
    ParameterRewriter parameterRewriter = new ParameterRewriter(parameters, getAnalysis());
    for (Expression expression : expressions) {
      Expression rewritten = ExpressionTreeRewriter.rewriteWith(parameterRewriter, expression);
      translations.addIntermediateMapping(expression, rewritten);
      Symbol symbol =
          symbolAllocator.newSymbol(rewritten, getAnalysis().getTypeWithCoercions(expression));
      projections.put(symbol, translations.rewrite(rewritten));
      newTranslations.put(symbol, rewritten);
    }
    // Now append the new translations into the TranslationMap
    for (Map.Entry<Symbol, Expression> entry : newTranslations.build().entrySet()) {
      translations.put(entry.getValue(), entry.getKey());
    }

    return new PlanBuilder(
        translations,
        new ProjectNode(idAllocator.getNextId(), getRoot(), projections.build()),
        parameters);
  }
Ejemplo n.º 4
0
 @Override
 public PlanNode rewriteProject(
     ProjectNode node, Expression inheritedPredicate, PlanRewriter<Expression> planRewriter) {
   Expression inlinedPredicate =
       ExpressionTreeRewriter.rewriteWith(
           new ExpressionSymbolInliner(node.getOutputMap()), inheritedPredicate);
   return planRewriter.defaultRewrite(node, inlinedPredicate);
 }
Ejemplo n.º 5
0
  public static Expression createExpression(
      String expression, Metadata metadata, Map<Symbol, Type> symbolTypes) {
    Expression parsedExpression = SQL_PARSER.createExpression(expression);

    final ExpressionAnalysis analysis =
        analyzeExpressionsWithSymbols(
            TEST_SESSION, metadata, SQL_PARSER, symbolTypes, ImmutableList.of(parsedExpression));
    Expression rewrittenExpression =
        ExpressionTreeRewriter.rewriteWith(
            new ExpressionRewriter<Void>() {
              @Override
              public Expression rewriteExpression(
                  Expression node, Void context, ExpressionTreeRewriter<Void> treeRewriter) {
                Expression rewrittenExpression = treeRewriter.defaultRewrite(node, context);

                // cast expression if coercion is registered
                Type coercion = analysis.getCoercion(node);
                if (coercion != null) {
                  rewrittenExpression =
                      new Cast(rewrittenExpression, coercion.getTypeSignature().toString());
                }

                return rewrittenExpression;
              }

              @Override
              public Expression rewriteDereferenceExpression(
                  DereferenceExpression node,
                  Void context,
                  ExpressionTreeRewriter<Void> treeRewriter) {
                if (analysis.getColumnReferences().contains(node)) {
                  return rewriteExpression(node, context, treeRewriter);
                }

                // Rewrite all row field reference to function call.
                QualifiedName mangledName =
                    QualifiedName.of(mangleFieldReference(node.getFieldName()));
                FunctionCall functionCall =
                    new FunctionCall(mangledName, ImmutableList.of(node.getBase()));
                Expression rewrittenExpression =
                    rewriteFunctionCall(functionCall, context, treeRewriter);

                // cast expression if coercion is registered
                Type coercion = analysis.getCoercion(node);
                if (coercion != null) {
                  rewrittenExpression =
                      new Cast(rewrittenExpression, coercion.getTypeSignature().toString());
                }

                return rewrittenExpression;
              }
            },
            parsedExpression);

    return canonicalizeExpression(rewrittenExpression);
  }
  public static Object evaluatePropertyValue(
      Expression expression,
      Type expectedType,
      Session session,
      Metadata metadata,
      List<Expression> parameters) {
    Expression rewritten =
        ExpressionTreeRewriter.rewriteWith(new ParameterRewriter(parameters), expression);
    Object value =
        evaluateConstantExpression(rewritten, expectedType, metadata, session, parameters);

    // convert to object value type of SQL type
    BlockBuilder blockBuilder = expectedType.createBlockBuilder(new BlockBuilderStatus(), 1);
    writeNativeValue(expectedType, blockBuilder, value);
    Object objectValue = expectedType.getObjectValue(session.toConnectorSession(), blockBuilder, 0);

    if (objectValue == null) {
      throw new PrestoException(
          INVALID_SESSION_PROPERTY, "Session property value must not be null");
    }
    return objectValue;
  }
Ejemplo n.º 7
0
    @Override
    public PlanNode rewriteUnion(
        UnionNode node, Expression inheritedPredicate, PlanRewriter<Expression> planRewriter) {
      boolean modified = false;
      ImmutableList.Builder<PlanNode> builder = ImmutableList.builder();
      for (int i = 0; i < node.getSources().size(); i++) {
        Expression sourcePredicate =
            ExpressionTreeRewriter.rewriteWith(
                new ExpressionSymbolInliner(node.sourceSymbolMap(i)), inheritedPredicate);
        PlanNode source = node.getSources().get(i);
        PlanNode rewrittenSource = planRewriter.rewrite(source, sourcePredicate);
        if (rewrittenSource != source) {
          modified = true;
        }
        builder.add(rewrittenSource);
      }

      if (modified) {
        return new UnionNode(node.getId(), builder.build(), node.getSymbolMapping());
      }

      return node;
    }
Ejemplo n.º 8
0
  private OperatorFactory compileFilterWithNoInputColumns(
      Expression filter, ExpressionCompiler compiler) {
    filter =
        ExpressionTreeRewriter.rewriteWith(
            new SymbolToInputRewriter(ImmutableMap.<Symbol, Integer>of()), filter);

    IdentityHashMap<Expression, Type> expressionTypes =
        getExpressionTypesFromInput(
            TEST_SESSION, metadata, SQL_PARSER, INPUT_TYPES, ImmutableList.of(filter));

    try {
      PageProcessor processor =
          compiler.compilePageProcessor(
              toRowExpression(filter, expressionTypes), ImmutableList.of());

      return new FilterAndProjectOperator.FilterAndProjectOperatorFactory(
          0, processor, ImmutableList.<Type>of());
    } catch (Throwable e) {
      if (e instanceof UncheckedExecutionException) {
        e = e.getCause();
      }
      throw new RuntimeException("Error compiling " + filter + ": " + e.getMessage(), e);
    }
  }