Search in sources :

Example 31 with Expression

use of com.facebook.presto.sql.tree.Expression in project presto by prestodb.

the class ImplementFilteredAggregations method apply.

@Override
public Optional<PlanNode> apply(PlanNode node, Lookup lookup, PlanNodeIdAllocator idAllocator, SymbolAllocator symbolAllocator) {
    if (!(node instanceof AggregationNode)) {
        return Optional.empty();
    }
    AggregationNode aggregation = (AggregationNode) node;
    boolean hasFilters = aggregation.getAggregations().entrySet().stream().anyMatch(e -> e.getValue().getFilter().isPresent() && // can't handle filtered aggregations with DISTINCT (conservatively, if they have a mask)
    !aggregation.getMasks().containsKey(e.getKey()));
    if (!hasFilters) {
        return Optional.empty();
    }
    Assignments.Builder newAssignments = Assignments.builder();
    ImmutableMap.Builder<Symbol, Symbol> masks = ImmutableMap.<Symbol, Symbol>builder().putAll(aggregation.getMasks());
    ImmutableMap.Builder<Symbol, FunctionCall> calls = ImmutableMap.builder();
    for (Map.Entry<Symbol, FunctionCall> entry : aggregation.getAggregations().entrySet()) {
        Symbol output = entry.getKey();
        // strip the filters
        FunctionCall call = entry.getValue();
        calls.put(output, new FunctionCall(call.getName(), call.getWindow(), Optional.empty(), call.isDistinct(), call.getArguments()));
        if (call.getFilter().isPresent()) {
            Expression filter = entry.getValue().getFilter().get();
            Symbol symbol = symbolAllocator.newSymbol(filter, BOOLEAN);
            newAssignments.put(symbol, filter);
            masks.put(output, symbol);
        }
    }
    // identity projection for all existing inputs
    newAssignments.putIdentities(aggregation.getSource().getOutputSymbols());
    return Optional.of(new AggregationNode(idAllocator.getNextId(), new ProjectNode(idAllocator.getNextId(), aggregation.getSource(), newAssignments.build()), calls.build(), aggregation.getFunctions(), masks.build(), aggregation.getGroupingSets(), aggregation.getStep(), aggregation.getHashSymbol(), aggregation.getGroupIdSymbol()));
}
Also used : Symbol(com.facebook.presto.sql.planner.Symbol) Assignments(com.facebook.presto.sql.planner.plan.Assignments) AggregationNode(com.facebook.presto.sql.planner.plan.AggregationNode) ImmutableMap(com.google.common.collect.ImmutableMap) Expression(com.facebook.presto.sql.tree.Expression) ProjectNode(com.facebook.presto.sql.planner.plan.ProjectNode) FunctionCall(com.facebook.presto.sql.tree.FunctionCall) ImmutableMap(com.google.common.collect.ImmutableMap) Map(java.util.Map)

Example 32 with Expression

use of com.facebook.presto.sql.tree.Expression in project presto by prestodb.

the class JoinGraph method joinWith.

private JoinGraph joinWith(JoinGraph other, List<JoinNode.EquiJoinClause> joinClauses, Context context, PlanNodeId newRoot) {
    for (PlanNode node : other.nodes) {
        checkState(!edges.containsKey(node.getId()), format("Node [%s] appeared in two JoinGraphs", node));
    }
    List<PlanNode> nodes = ImmutableList.<PlanNode>builder().addAll(this.nodes).addAll(other.nodes).build();
    ImmutableMultimap.Builder<PlanNodeId, Edge> edges = ImmutableMultimap.<PlanNodeId, Edge>builder().putAll(this.edges).putAll(other.edges);
    List<Expression> joinedFilters = ImmutableList.<Expression>builder().addAll(this.filters).addAll(other.filters).build();
    for (JoinNode.EquiJoinClause edge : joinClauses) {
        Symbol leftSymbol = edge.getLeft();
        Symbol rightSymbol = edge.getRight();
        checkState(context.containsSymbol(leftSymbol));
        checkState(context.containsSymbol(rightSymbol));
        PlanNode left = context.getSymbolSource(leftSymbol);
        PlanNode right = context.getSymbolSource(rightSymbol);
        edges.put(left.getId(), new Edge(right, leftSymbol, rightSymbol));
        edges.put(right.getId(), new Edge(left, rightSymbol, leftSymbol));
    }
    return new JoinGraph(nodes, edges.build(), newRoot, joinedFilters, Optional.empty());
}
Also used : PlanNodeId(com.facebook.presto.sql.planner.plan.PlanNodeId) PlanNode(com.facebook.presto.sql.planner.plan.PlanNode) Expression(com.facebook.presto.sql.tree.Expression) JoinNode(com.facebook.presto.sql.planner.plan.JoinNode) Symbol(com.facebook.presto.sql.planner.Symbol) ImmutableMultimap(com.google.common.collect.ImmutableMultimap)

Example 33 with Expression

use of com.facebook.presto.sql.tree.Expression in project presto by prestodb.

the class LocalQueryRunner method createPlan.

public Plan createPlan(Session session, @Language("SQL") String sql, List<PlanOptimizer> optimizers, LogicalPlanner.Stage stage) {
    Statement wrapped = sqlParser.createStatement(sql);
    Statement statement = unwrapExecuteStatement(wrapped, sqlParser, session);
    List<Expression> parameters = emptyList();
    if (wrapped instanceof Execute) {
        parameters = ((Execute) wrapped).getParameters();
    }
    validateParameters(statement, parameters);
    assertFormattedSql(sqlParser, statement);
    PlanNodeIdAllocator idAllocator = new PlanNodeIdAllocator();
    QueryExplainer queryExplainer = new QueryExplainer(optimizers, metadata, accessControl, sqlParser, dataDefinitionTask);
    Analyzer analyzer = new Analyzer(session, metadata, sqlParser, accessControl, Optional.of(queryExplainer), parameters);
    LogicalPlanner logicalPlanner = new LogicalPlanner(session, optimizers, idAllocator, metadata, sqlParser);
    Analysis analysis = analyzer.analyze(statement);
    return logicalPlanner.plan(analysis, stage);
}
Also used : QueryExplainer(com.facebook.presto.sql.analyzer.QueryExplainer) LogicalPlanner(com.facebook.presto.sql.planner.LogicalPlanner) Execute(com.facebook.presto.sql.tree.Execute) Expression(com.facebook.presto.sql.tree.Expression) PlanNodeIdAllocator(com.facebook.presto.sql.planner.PlanNodeIdAllocator) SqlQueryManager.unwrapExecuteStatement(com.facebook.presto.execution.SqlQueryManager.unwrapExecuteStatement) Statement(com.facebook.presto.sql.tree.Statement) Analysis(com.facebook.presto.sql.analyzer.Analysis) Analyzer(com.facebook.presto.sql.analyzer.Analyzer)

Example 34 with Expression

use of com.facebook.presto.sql.tree.Expression in project presto by prestodb.

the class TestSetSessionTask method testSetSessionWithParameters.

@Test
public void testSetSessionWithParameters() throws Exception {
    List<Expression> expressionList = new ArrayList<>();
    expressionList.add(new StringLiteral("ban"));
    expressionList.add(new Parameter(0));
    testSetSessionWithParameters(new FunctionCall(QualifiedName.of("concat"), expressionList), "banana", ImmutableList.of(new StringLiteral("ana")));
}
Also used : StringLiteral(com.facebook.presto.sql.tree.StringLiteral) Expression(com.facebook.presto.sql.tree.Expression) ArrayList(java.util.ArrayList) Parameter(com.facebook.presto.sql.tree.Parameter) FunctionCall(com.facebook.presto.sql.tree.FunctionCall) Test(org.testng.annotations.Test)

Example 35 with Expression

use of com.facebook.presto.sql.tree.Expression in project presto by prestodb.

the class FunctionAssertions method executeFilterWithAll.

private List<Boolean> executeFilterWithAll(String filter, Session session, boolean executeWithNoInputColumns, ExpressionCompiler compiler) {
    requireNonNull(filter, "filter is null");
    Expression filterExpression = createExpression(filter, metadata, SYMBOL_TYPES);
    List<Boolean> results = new ArrayList<>();
    // execute as standalone operator
    OperatorFactory operatorFactory = compileFilterProject(filterExpression, TRUE_LITERAL, compiler);
    results.add(executeFilter(operatorFactory, session));
    if (executeWithNoInputColumns) {
        // execute as standalone operator
        operatorFactory = compileFilterWithNoInputColumns(filterExpression, compiler);
        results.add(executeFilterWithNoInputColumns(operatorFactory, session));
    }
    // interpret
    boolean interpretedValue = executeFilter(interpretedFilterProject(filterExpression, TRUE_LITERAL, session));
    results.add(interpretedValue);
    // execute over normal operator
    SourceOperatorFactory scanProjectOperatorFactory = compileScanFilterProject(filterExpression, TRUE_LITERAL, compiler);
    boolean scanOperatorValue = executeFilter(scanProjectOperatorFactory, createNormalSplit(), session);
    results.add(scanOperatorValue);
    // execute over record set
    boolean recordValue = executeFilter(scanProjectOperatorFactory, createRecordSetSplit(), session);
    results.add(recordValue);
    // If the filter does not need bound values, execute query using full engine
    if (!needsBoundValue(filterExpression)) {
        MaterializedResult result = runner.execute("SELECT TRUE WHERE " + filter);
        assertEquals(result.getTypes().size(), 1);
        Boolean queryResult;
        if (result.getMaterializedRows().isEmpty()) {
            queryResult = false;
        } else {
            assertEquals(result.getMaterializedRows().size(), 1);
            queryResult = (Boolean) Iterables.getOnlyElement(result.getMaterializedRows()).getField(0);
        }
        results.add(queryResult);
    }
    return results;
}
Also used : RowExpression(com.facebook.presto.sql.relational.RowExpression) CanonicalizeExpressions.canonicalizeExpression(com.facebook.presto.sql.planner.optimizations.CanonicalizeExpressions.canonicalizeExpression) DereferenceExpression(com.facebook.presto.sql.tree.DereferenceExpression) Expression(com.facebook.presto.sql.tree.Expression) SourceOperatorFactory(com.facebook.presto.operator.SourceOperatorFactory) OperatorFactory(com.facebook.presto.operator.OperatorFactory) ArrayList(java.util.ArrayList) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) MaterializedResult(com.facebook.presto.testing.MaterializedResult) SourceOperatorFactory(com.facebook.presto.operator.SourceOperatorFactory)

Aggregations

Expression (com.facebook.presto.sql.tree.Expression)137 ComparisonExpression (com.facebook.presto.sql.tree.ComparisonExpression)74 Test (org.testng.annotations.Test)46 NotExpression (com.facebook.presto.sql.tree.NotExpression)42 InListExpression (com.facebook.presto.sql.tree.InListExpression)40 DereferenceExpression (com.facebook.presto.sql.tree.DereferenceExpression)33 Type (com.facebook.presto.spi.type.Type)26 PlanNode (com.facebook.presto.sql.planner.plan.PlanNode)26 LiteralInterpreter.toExpression (com.facebook.presto.sql.planner.LiteralInterpreter.toExpression)25 LogicalBinaryExpression (com.facebook.presto.sql.tree.LogicalBinaryExpression)22 ImmutableList (com.google.common.collect.ImmutableList)22 LambdaExpression (com.facebook.presto.sql.tree.LambdaExpression)19 ArithmeticBinaryExpression (com.facebook.presto.sql.tree.ArithmeticBinaryExpression)18 Cast (com.facebook.presto.sql.tree.Cast)17 ArrayList (java.util.ArrayList)17 ExtractionResult (com.facebook.presto.sql.planner.DomainTranslator.ExtractionResult)16 CoalesceExpression (com.facebook.presto.sql.tree.CoalesceExpression)16 SubqueryExpression (com.facebook.presto.sql.tree.SubqueryExpression)16 SubscriptExpression (com.facebook.presto.sql.tree.SubscriptExpression)16 QuantifiedComparisonExpression (com.facebook.presto.sql.tree.QuantifiedComparisonExpression)15