use of io.crate.analyze.relations.select.SelectAnalysis in project crate by crate.
the class RelationAnalyzer method visitQuerySpecification.
@Override
protected AnalyzedRelation visitQuerySpecification(QuerySpecification node, StatementAnalysisContext statementContext) {
List<Relation> from = node.getFrom() != null ? node.getFrom() : SYS_CLUSTER_SOURCE;
statementContext.startRelation();
for (Relation relation : from) {
process(relation, statementContext);
}
RelationAnalysisContext context = statementContext.currentRelationContext();
ExpressionAnalyzer expressionAnalyzer = new ExpressionAnalyzer(functions, statementContext.sessionContext(), statementContext.convertParamFunction(), new FullQualifedNameFieldProvider(context.sources()), new SubqueryAnalyzer(this, statementContext));
ExpressionAnalysisContext expressionAnalysisContext = context.expressionAnalysisContext();
Symbol querySymbol = expressionAnalyzer.generateQuerySymbol(node.getWhere(), expressionAnalysisContext);
WhereClause whereClause = new WhereClause(querySymbol);
SelectAnalysis selectAnalysis = SelectAnalyzer.analyzeSelect(node.getSelect(), context, expressionAnalyzer, expressionAnalysisContext);
List<Symbol> groupBy = analyzeGroupBy(selectAnalysis, node.getGroupBy(), expressionAnalyzer, expressionAnalysisContext);
if (!node.getGroupBy().isEmpty() || expressionAnalysisContext.hasAggregates) {
ensureNonAggregatesInGroupBy(selectAnalysis.outputSymbols(), selectAnalysis.outputNames(), groupBy);
}
boolean isDistinct = false;
if (node.getSelect().isDistinct() && groupBy.isEmpty()) {
groupBy = rewriteGlobalDistinct(selectAnalysis.outputSymbols());
isDistinct = true;
}
if (groupBy != null && groupBy.isEmpty()) {
groupBy = null;
}
QuerySpec querySpec = new QuerySpec().orderBy(analyzeOrderBy(selectAnalysis, node.getOrderBy(), expressionAnalyzer, expressionAnalysisContext, expressionAnalysisContext.hasAggregates || groupBy != null, isDistinct)).having(analyzeHaving(node.getHaving(), groupBy, expressionAnalyzer, context.expressionAnalysisContext())).limit(optionalLongSymbol(node.getLimit(), expressionAnalyzer, expressionAnalysisContext)).offset(optionalLongSymbol(node.getOffset(), expressionAnalyzer, expressionAnalysisContext)).outputs(selectAnalysis.outputSymbols()).where(whereClause).groupBy(groupBy).hasAggregates(expressionAnalysisContext.hasAggregates);
QueriedRelation relation;
if (context.sources().size() == 1) {
AnalyzedRelation source = Iterables.getOnlyElement(context.sources().values());
if (source instanceof DocTableRelation) {
relation = new QueriedDocTable((DocTableRelation) source, selectAnalysis.outputNames(), querySpec);
} else if (source instanceof TableRelation) {
relation = new QueriedTable((TableRelation) source, selectAnalysis.outputNames(), querySpec);
} else {
assert source instanceof QueriedRelation : "expecting relation to be an instance of QueriedRelation";
relation = new QueriedSelectRelation((QueriedRelation) source, selectAnalysis.outputNames(), querySpec);
}
} else {
relation = new MultiSourceSelect(context.sources(), selectAnalysis.outputSymbols(), selectAnalysis.outputNames(), querySpec, context.joinPairs());
}
statementContext.endRelation();
return relation;
}
Aggregations