use of io.crate.analyze.QueriedSelectRelation in project crate by crate.
the class RelationAnalyzer method visitQuerySpecification.
@Override
protected AnalyzedRelation visitQuerySpecification(QuerySpecification node, StatementAnalysisContext statementContext) {
List<Relation> from = node.getFrom().isEmpty() ? EMPTY_ROW_TABLE_RELATION : node.getFrom();
RelationAnalysisContext currentRelationContext = statementContext.startRelation();
for (Relation relation : from) {
// different from relations have to be isolated from each other
RelationAnalysisContext innerContext = statementContext.startRelation();
relation.accept(this, statementContext);
statementContext.endRelation();
for (Map.Entry<RelationName, AnalyzedRelation> entry : innerContext.sources().entrySet()) {
currentRelationContext.addSourceRelation(entry.getValue());
}
for (JoinPair joinPair : innerContext.joinPairs()) {
currentRelationContext.addJoinPair(joinPair);
}
}
RelationAnalysisContext context = statementContext.currentRelationContext();
CoordinatorTxnCtx coordinatorTxnCtx = statementContext.transactionContext();
ExpressionAnalyzer expressionAnalyzer = new ExpressionAnalyzer(coordinatorTxnCtx, nodeCtx, statementContext.paramTyeHints(), new FullQualifiedNameFieldProvider(context.sources(), context.parentSources(), coordinatorTxnCtx.sessionContext().searchPath().currentSchema()), new SubqueryAnalyzer(this, statementContext));
ExpressionAnalysisContext expressionAnalysisContext = context.expressionAnalysisContext();
expressionAnalysisContext.windows(node.getWindows());
SelectAnalysis selectAnalysis = SelectAnalyzer.analyzeSelectItems(node.getSelect().getSelectItems(), context.sources(), expressionAnalyzer, expressionAnalysisContext);
List<Symbol> groupBy = analyzeGroupBy(selectAnalysis, node.getGroupBy(), expressionAnalyzer, expressionAnalysisContext);
if (!node.getGroupBy().isEmpty() || expressionAnalysisContext.hasAggregates()) {
GroupAndAggregateSemantics.validate(selectAnalysis.outputSymbols(), groupBy);
}
boolean isDistinct = node.getSelect().isDistinct();
Symbol where = expressionAnalyzer.generateQuerySymbol(node.getWhere(), expressionAnalysisContext);
WhereClauseValidator.validate(where);
var normalizer = EvaluatingNormalizer.functionOnlyNormalizer(nodeCtx, f -> expressionAnalysisContext.isEagerNormalizationAllowed() && f.isDeterministic());
QueriedSelectRelation relation = new QueriedSelectRelation(isDistinct, List.copyOf(context.sources().values()), context.joinPairs(), selectAnalysis.outputSymbols(), where, groupBy, analyzeHaving(node.getHaving(), groupBy, expressionAnalyzer, context.expressionAnalysisContext()), analyzeOrderBy(selectAnalysis, node.getOrderBy(), expressionAnalyzer, expressionAnalysisContext, expressionAnalysisContext.hasAggregates() || !groupBy.isEmpty(), isDistinct), longSymbolOrNull(node.getLimit(), expressionAnalyzer, expressionAnalysisContext, normalizer, coordinatorTxnCtx), longSymbolOrNull(node.getOffset(), expressionAnalyzer, expressionAnalysisContext, normalizer, coordinatorTxnCtx));
statementContext.endRelation();
return relation;
}
use of io.crate.analyze.QueriedSelectRelation in project crate by crate.
the class WhereClauseAnalyzerTest method analyzeSelectWhere.
private WhereClause analyzeSelectWhere(String stmt) {
AnalyzedRelation rel = e.analyze(stmt);
if (rel instanceof QueriedSelectRelation) {
QueriedSelectRelation queriedRelation = ((QueriedSelectRelation) rel);
if (queriedRelation.from().get(0) instanceof DocTableRelation) {
DocTableRelation docTableRelation = (DocTableRelation) queriedRelation.from().get(0);
WhereClauseOptimizer.DetailedQuery detailedQuery = WhereClauseOptimizer.optimize(new EvaluatingNormalizer(e.nodeCtx, RowGranularity.CLUSTER, null, docTableRelation), queriedRelation.where(), docTableRelation.tableInfo(), coordinatorTxnCtx, e.nodeCtx);
return detailedQuery.toBoundWhereClause(docTableRelation.tableInfo(), Row.EMPTY, SubQueryResults.EMPTY, coordinatorTxnCtx, e.nodeCtx);
}
return new WhereClause(queriedRelation.where());
} else {
return WhereClause.MATCH_ALL;
}
}
use of io.crate.analyze.QueriedSelectRelation in project crate by crate.
the class SplitPointsTest method test_split_points_creation_with_filter_in_aggregate_expression.
@Test
public void test_split_points_creation_with_filter_in_aggregate_expression() {
QueriedSelectRelation relation = e.analyze("select sum(i) filter (where x > 1) from t1");
SplitPoints splitPoints = SplitPointsBuilder.create(relation);
assertThat(splitPoints.toCollect(), contains(isReference("i"), isFunction("op_>", isReference("x"), isLiteral(1))));
assertThat(splitPoints.aggregates(), contains(isFunction("sum")));
}
Aggregations