Search in sources :

Example 16 with QueriedSelectRelation

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;
}
Also used : SelectAnalysis(io.crate.analyze.relations.select.SelectAnalysis) CoordinatorTxnCtx(io.crate.metadata.CoordinatorTxnCtx) ExpressionAnalysisContext(io.crate.analyze.expressions.ExpressionAnalysisContext) Symbol(io.crate.expression.symbol.Symbol) ExpressionAnalyzer(io.crate.analyze.expressions.ExpressionAnalyzer) Relation(io.crate.sql.tree.Relation) QueriedSelectRelation(io.crate.analyze.QueriedSelectRelation) AliasedRelation(io.crate.sql.tree.AliasedRelation) QueriedSelectRelation(io.crate.analyze.QueriedSelectRelation) RelationName(io.crate.metadata.RelationName) SubqueryAnalyzer(io.crate.analyze.expressions.SubqueryAnalyzer) Map(java.util.Map)

Example 17 with QueriedSelectRelation

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;
    }
}
Also used : EvaluatingNormalizer(io.crate.expression.eval.EvaluatingNormalizer) QueriedSelectRelation(io.crate.analyze.QueriedSelectRelation) WhereClause(io.crate.analyze.WhereClause) DocTableRelation(io.crate.analyze.relations.DocTableRelation) WhereClauseOptimizer(io.crate.planner.WhereClauseOptimizer) AnalyzedRelation(io.crate.analyze.relations.AnalyzedRelation)

Example 18 with QueriedSelectRelation

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")));
}
Also used : QueriedSelectRelation(io.crate.analyze.QueriedSelectRelation) Test(org.junit.Test) CrateDummyClusterServiceUnitTest(io.crate.test.integration.CrateDummyClusterServiceUnitTest)

Aggregations

QueriedSelectRelation (io.crate.analyze.QueriedSelectRelation)18 Test (org.junit.Test)12 CrateDummyClusterServiceUnitTest (io.crate.test.integration.CrateDummyClusterServiceUnitTest)10 Symbol (io.crate.expression.symbol.Symbol)4 WhereClause (io.crate.analyze.WhereClause)3 DocTableRelation (io.crate.analyze.relations.DocTableRelation)3 OrderBy (io.crate.analyze.OrderBy)2 ExpressionAnalysisContext (io.crate.analyze.expressions.ExpressionAnalysisContext)2 ExpressionAnalyzer (io.crate.analyze.expressions.ExpressionAnalyzer)2 SubqueryAnalyzer (io.crate.analyze.expressions.SubqueryAnalyzer)2 SelectAnalysis (io.crate.analyze.relations.select.SelectAnalysis)2 ProjectionBuilder (io.crate.execution.dsl.projection.builder.ProjectionBuilder)2 EvaluatingNormalizer (io.crate.expression.eval.EvaluatingNormalizer)2 PlannerContext (io.crate.planner.PlannerContext)2 TableStats (io.crate.statistics.TableStats)2 CrateUnitTest (io.crate.test.integration.CrateUnitTest)2 SQLExecutor (io.crate.testing.SQLExecutor)2 Map (java.util.Map)2 RandomizedTest (com.carrotsearch.randomizedtesting.RandomizedTest)1 AbstractTableRelation (io.crate.analyze.relations.AbstractTableRelation)1