Search in sources :

Example 1 with QueriedSelectRelation

use of io.crate.analyze.QueriedSelectRelation in project crate by crate.

the class RelationAnalyzer method visitQuery.

@Override
protected AnalyzedRelation visitQuery(Query node, StatementAnalysisContext statementContext) {
    AnalyzedRelation childRelation = node.getQueryBody().accept(this, statementContext);
    if (node.getOrderBy().isEmpty() && node.getLimit().isEmpty() && node.getOffset().isEmpty()) {
        return childRelation;
    }
    // In case of Set Operation (UNION, INTERSECT EXCEPT) or VALUES clause,
    // the `node` contains the ORDER BY and/or LIMIT and/or OFFSET and wraps the
    // actual operation (eg: UNION) which is parsed into the `queryBody` of the `node`.
    // Use child relation to process expressions of the "root" Query node
    statementContext.startRelation();
    RelationAnalysisContext relationAnalysisContext = statementContext.currentRelationContext();
    relationAnalysisContext.addSourceRelation(childRelation);
    statementContext.endRelation();
    List<Symbol> childRelationFields = childRelation.outputs();
    var coordinatorTxnCtx = statementContext.transactionContext();
    ExpressionAnalyzer expressionAnalyzer = new ExpressionAnalyzer(coordinatorTxnCtx, nodeCtx, statementContext.paramTyeHints(), new FullQualifiedNameFieldProvider(relationAnalysisContext.sources(), relationAnalysisContext.parentSources(), coordinatorTxnCtx.sessionContext().searchPath().currentSchema()), new SubqueryAnalyzer(this, statementContext));
    ExpressionAnalysisContext expressionAnalysisContext = relationAnalysisContext.expressionAnalysisContext();
    SelectAnalysis selectAnalysis = new SelectAnalysis(childRelationFields.size(), relationAnalysisContext.sources(), expressionAnalyzer, expressionAnalysisContext);
    for (Symbol field : childRelationFields) {
        selectAnalysis.add(Symbols.pathFromSymbol(field), field);
    }
    var normalizer = EvaluatingNormalizer.functionOnlyNormalizer(nodeCtx, f -> expressionAnalysisContext.isEagerNormalizationAllowed() && f.isDeterministic());
    return new QueriedSelectRelation(false, List.of(childRelation), List.of(), selectAnalysis.outputSymbols(), Literal.BOOLEAN_TRUE, List.of(), null, analyzeOrderBy(selectAnalysis, node.getOrderBy(), expressionAnalyzer, expressionAnalysisContext, false, false), longSymbolOrNull(node.getLimit(), expressionAnalyzer, expressionAnalysisContext, normalizer, coordinatorTxnCtx), longSymbolOrNull(node.getOffset(), expressionAnalyzer, expressionAnalysisContext, normalizer, coordinatorTxnCtx));
}
Also used : SelectAnalysis(io.crate.analyze.relations.select.SelectAnalysis) ExpressionAnalysisContext(io.crate.analyze.expressions.ExpressionAnalysisContext) Symbol(io.crate.expression.symbol.Symbol) ExpressionAnalyzer(io.crate.analyze.expressions.ExpressionAnalyzer) QueriedSelectRelation(io.crate.analyze.QueriedSelectRelation) SubqueryAnalyzer(io.crate.analyze.expressions.SubqueryAnalyzer)

Example 2 with QueriedSelectRelation

use of io.crate.analyze.QueriedSelectRelation in project crate by crate.

the class LogicalPlanner method tryOptimizeForInSubquery.

// In case the subselect is inside an IN() or = ANY() apply a "natural" OrderBy to optimize
// the building of TermInSetQuery which does a sort on the collection of values.
// See issue https://github.com/crate/crate/issues/6755
// If the output values are already sorted (even in desc order) no optimization is needed
private LogicalPlan tryOptimizeForInSubquery(SelectSymbol selectSymbol, AnalyzedRelation relation, LogicalPlan planBuilder) {
    if (selectSymbol.getResultType() == SelectSymbol.ResultType.SINGLE_COLUMN_MULTIPLE_VALUES && relation instanceof QueriedSelectRelation) {
        QueriedSelectRelation queriedRelation = (QueriedSelectRelation) relation;
        OrderBy relationOrderBy = queriedRelation.orderBy();
        Symbol firstOutput = queriedRelation.outputs().get(0);
        if ((relationOrderBy == null || relationOrderBy.orderBySymbols().get(0).equals(firstOutput) == false) && DataTypes.isPrimitive(firstOutput.valueType())) {
            return Order.create(planBuilder, new OrderBy(Collections.singletonList(firstOutput)));
        }
    }
    return planBuilder;
}
Also used : OrderBy(io.crate.analyze.OrderBy) SelectSymbol(io.crate.expression.symbol.SelectSymbol) ScopedSymbol(io.crate.expression.symbol.ScopedSymbol) Symbol(io.crate.expression.symbol.Symbol) QueriedSelectRelation(io.crate.analyze.QueriedSelectRelation)

Example 3 with QueriedSelectRelation

use of io.crate.analyze.QueriedSelectRelation in project crate by crate.

the class GeneratedColumnsTest method testSubscriptExpressionThatReturnsAnArray.

@Test
public void testSubscriptExpressionThatReturnsAnArray() throws Exception {
    SQLExecutor e = SQLExecutor.builder(clusterService).addTable("create table t (obj object as (arr array(integer)), arr as obj['arr'])").build();
    QueriedSelectRelation query = e.analyze("select obj, arr from t");
    DocTableInfo table = ((DocTableRelation) query.from().get(0)).tableInfo();
    GeneratedColumns<Doc> generatedColumns = new GeneratedColumns<>(new InputFactory(e.nodeCtx), CoordinatorTxnCtx.systemTransactionContext(), GeneratedColumns.Validation.NONE, new DocRefResolver(Collections.emptyList()), Collections.emptyList(), table.generatedColumns());
    BytesReference bytes = BytesReference.bytes(XContentFactory.jsonBuilder().startObject().startObject("obj").startArray("arr").value(10).value(20).endArray().endObject().endObject());
    generatedColumns.setNextRow(new Doc(1, table.concreteIndices()[0], "1", 1, 1, 1, XContentHelper.convertToMap(bytes, false, XContentType.JSON).v2(), bytes::utf8ToString));
    Map.Entry<Reference, Input<?>> generatedColumn = generatedColumns.generatedToInject().iterator().next();
    assertThat((List<Object>) generatedColumn.getValue().value(), contains(10, 20));
}
Also used : BytesReference(org.elasticsearch.common.bytes.BytesReference) InputFactory(io.crate.expression.InputFactory) DocTableInfo(io.crate.metadata.doc.DocTableInfo) Reference(io.crate.metadata.Reference) BytesReference(org.elasticsearch.common.bytes.BytesReference) DocRefResolver(io.crate.expression.reference.DocRefResolver) Input(io.crate.data.Input) SQLExecutor(io.crate.testing.SQLExecutor) QueriedSelectRelation(io.crate.analyze.QueriedSelectRelation) DocTableRelation(io.crate.analyze.relations.DocTableRelation) Doc(io.crate.expression.reference.Doc) Map(java.util.Map) Test(org.junit.Test) CrateDummyClusterServiceUnitTest(io.crate.test.integration.CrateDummyClusterServiceUnitTest)

Example 4 with QueriedSelectRelation

use of io.crate.analyze.QueriedSelectRelation in project crate by crate.

the class SplitPointsTest method testScalarIsNotCollectedEarly.

@Test
public void testScalarIsNotCollectedEarly() throws Exception {
    QueriedSelectRelation relation = e.analyze("select x + 1 from t1 group by x");
    SplitPoints splitPoints = SplitPointsBuilder.create(relation);
    assertThat(splitPoints.toCollect(), contains(isReference("x")));
    assertThat(splitPoints.aggregates(), Matchers.emptyIterable());
}
Also used : QueriedSelectRelation(io.crate.analyze.QueriedSelectRelation) Test(org.junit.Test) CrateDummyClusterServiceUnitTest(io.crate.test.integration.CrateDummyClusterServiceUnitTest)

Example 5 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_fo_window_function_call.

@Test
public void test_split_points_creation_with_filter_in_aggregate_fo_window_function_call() {
    QueriedSelectRelation relation = e.analyze("select sum(i) filter (where x > 1) over(order by i) from t1");
    SplitPoints splitPoints = SplitPointsBuilder.create(relation);
    assertThat(splitPoints.toCollect(), contains(isReference("i"), isFunction("op_>", isReference("x"), isLiteral(1))));
    assertThat(splitPoints.windowFunctions(), contains(isFunction("sum")));
    assertThat(splitPoints.aggregates(), is(empty()));
}
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