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()));
}
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());
}
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);
}
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")));
}
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;
}
Aggregations