Search in sources :

Example 21 with QuerySpec

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

the class RelationSplitterTest method testNoSplitOnOuterJoinRelation.

@Test
public void testNoSplitOnOuterJoinRelation() throws Exception {
    QuerySpec querySpec = fromQuery("t2.y < 10");
    JoinPair joinPair = new JoinPair(T3.T1, T3.T2, JoinType.LEFT, asSymbol("t1.a = t2.b"));
    RelationSplitter splitter = split(querySpec, Arrays.asList(joinPair));
    assertThat(querySpec, isSQL("SELECT true WHERE (doc.t2.y < 10)"));
    assertThat(splitter.getSpec(T3.TR_1), isSQL("SELECT doc.t1.a"));
    assertThat(splitter.getSpec(T3.TR_2), isSQL("SELECT doc.t2.y, doc.t2.b"));
}
Also used : QuerySpec(io.crate.analyze.QuerySpec) Test(org.junit.Test) CrateUnitTest(io.crate.test.integration.CrateUnitTest)

Example 22 with QuerySpec

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

the class RelationSplitterTest method testSplitOrderByThatIsPartiallyConsumed.

@Test
public void testSplitOrderByThatIsPartiallyConsumed() throws Exception {
    // select a from t1, t2 order by a, b + x desc
    List<Symbol> orderBySymbols = Arrays.asList(asSymbol("a"), asSymbol("x + y"));
    OrderBy orderBy = new OrderBy(orderBySymbols, new boolean[] { true, false }, new Boolean[] { null, null });
    QuerySpec querySpec = new QuerySpec().outputs(singleTrue()).orderBy(orderBy);
    RelationSplitter splitter = split(querySpec);
    assertThat(querySpec, isSQL("SELECT true ORDER BY doc.t1.a DESC, add(doc.t1.x, doc.t2.y)"));
    assertThat(splitter.remainingOrderBy().isPresent(), is(true));
    assertThat(splitter.remainingOrderBy().get().orderBy(), isSQL("doc.t1.a DESC, add(doc.t1.x, doc.t2.y)"));
    assertThat(splitter.requiredForQuery(), isSQL("doc.t1.a, doc.t1.x, doc.t2.y, add(doc.t1.x, doc.t2.y)"));
    assertThat(splitter.getSpec(T3.TR_1), isSQL("SELECT doc.t1.a, doc.t1.x"));
    assertThat(splitter.getSpec(T3.TR_2), isSQL("SELECT doc.t2.y"));
    assertThat(splitter.canBeFetched(), empty());
}
Also used : OrderBy(io.crate.analyze.OrderBy) Symbol(io.crate.analyze.symbol.Symbol) QuerySpec(io.crate.analyze.QuerySpec) Test(org.junit.Test) CrateUnitTest(io.crate.test.integration.CrateUnitTest)

Example 23 with QuerySpec

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

the class RelationSplitterTest method testSplitOrderByCombiningColumnsFrom3Relations.

@Test
public void testSplitOrderByCombiningColumnsFrom3Relations() throws Exception {
    // select a, b from t1, t2, t3 order by x, x - y + z, y, x + y
    QuerySpec querySpec = new QuerySpec().outputs(Arrays.asList(asSymbol("a"), asSymbol("b")));
    List<Symbol> orderBySymbols = Arrays.asList(asSymbol("x"), asSymbol("x - y + z"), asSymbol("y"), asSymbol("x+y"));
    OrderBy orderBy = new OrderBy(orderBySymbols, new boolean[] { false, false, false, false }, new Boolean[] { null, null, null, null });
    querySpec.orderBy(orderBy);
    RelationSplitter splitter = split(querySpec);
    assertThat(querySpec, isSQL("SELECT doc.t1.a, doc.t2.b " + "ORDER BY doc.t1.x, add(subtract(doc.t1.x, doc.t2.y), doc.t3.z), " + "doc.t2.y, add(doc.t1.x, doc.t2.y)"));
    assertThat(splitter.getSpec(T3.TR_1), isSQL("SELECT doc.t1.x, doc.t1.a"));
    assertThat(splitter.getSpec(T3.TR_2), isSQL("SELECT doc.t2.y, doc.t2.b"));
    assertThat(splitter.getSpec(T3.TR_3), isSQL("SELECT doc.t3.z"));
    assertThat(splitter.remainingOrderBy().isPresent(), is(true));
    assertThat(splitter.remainingOrderBy().get().orderBy(), isSQL("doc.t1.x, add(subtract(doc.t1.x, doc.t2.y), doc.t3.z), doc.t2.y, add(doc.t1.x, doc.t2.y)"));
}
Also used : OrderBy(io.crate.analyze.OrderBy) Symbol(io.crate.analyze.symbol.Symbol) QuerySpec(io.crate.analyze.QuerySpec) Test(org.junit.Test) CrateUnitTest(io.crate.test.integration.CrateUnitTest)

Example 24 with QuerySpec

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

the class ConsumingPlanner method plan.

@Nullable
public Plan plan(AnalyzedRelation relation, ConsumerContext consumerContext) {
    for (Consumer consumer : consumers) {
        Plan plan = consumer.consume(relation, consumerContext);
        if (plan != null) {
            if (relation instanceof QueriedRelation) {
                QuerySpec qs = ((QueriedRelation) relation).querySpec();
                SubqueryPlanner subqueryPlanner = new SubqueryPlanner(consumerContext.plannerContext());
                Map<Plan, SelectSymbol> subQueries = subqueryPlanner.planSubQueries(qs);
                return MultiPhasePlan.createIfNeeded(plan, subQueries);
            }
            return plan;
        }
    }
    ValidationException validationException = consumerContext.validationException();
    if (validationException != null) {
        throw validationException;
    }
    return null;
}
Also used : SelectSymbol(io.crate.analyze.symbol.SelectSymbol) ValidationException(io.crate.exceptions.ValidationException) QueriedRelation(io.crate.analyze.relations.QueriedRelation) QuerySpec(io.crate.analyze.QuerySpec) Nullable(javax.annotation.Nullable)

Example 25 with QuerySpec

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

the class ESGetStatementPlanner method convert.

public static Plan convert(QueriedDocTable table, Planner.Context context) {
    QuerySpec querySpec = table.querySpec();
    Optional<DocKeys> optKeys = querySpec.where().docKeys();
    assert !querySpec.hasAggregates() : "Can't create ESGet plan for queries with aggregates";
    assert !querySpec.groupBy().isPresent() : "Can't create ESGet plan for queries with group by";
    assert optKeys.isPresent() : "Can't create ESGet without docKeys";
    DocTableInfo tableInfo = table.tableRelation().tableInfo();
    DocKeys docKeys = optKeys.get();
    if (docKeys.withVersions()) {
        throw new VersionInvalidException();
    }
    Limits limits = context.getLimits(querySpec);
    if (limits.hasLimit() && limits.finalLimit() == 0) {
        return new NoopPlan(context.jobId());
    }
    table.tableRelation().validateOrderBy(querySpec.orderBy());
    return new ESGet(context.nextExecutionPhaseId(), tableInfo, querySpec.outputs(), optKeys.get(), querySpec.orderBy(), limits.finalLimit(), limits.offset(), context.jobId());
}
Also used : DocTableInfo(io.crate.metadata.doc.DocTableInfo) NoopPlan(io.crate.planner.NoopPlan) Limits(io.crate.planner.Limits) DocKeys(io.crate.analyze.where.DocKeys) ESGet(io.crate.planner.node.dql.ESGet) QuerySpec(io.crate.analyze.QuerySpec) VersionInvalidException(io.crate.exceptions.VersionInvalidException)

Aggregations

QuerySpec (io.crate.analyze.QuerySpec)31 Test (org.junit.Test)25 CrateUnitTest (io.crate.test.integration.CrateUnitTest)15 OrderBy (io.crate.analyze.OrderBy)13 QueriedDocTable (io.crate.analyze.relations.QueriedDocTable)11 Symbol (io.crate.analyze.symbol.Symbol)4 Function (io.crate.analyze.symbol.Function)3 AbsFunction (io.crate.operation.scalar.arithmetic.AbsFunction)2 Nullable (javax.annotation.Nullable)2 SessionContext (io.crate.action.sql.SessionContext)1 EvaluatingNormalizer (io.crate.analyze.EvaluatingNormalizer)1 RelationSource (io.crate.analyze.RelationSource)1 QueriedRelation (io.crate.analyze.relations.QueriedRelation)1 Field (io.crate.analyze.symbol.Field)1 SelectSymbol (io.crate.analyze.symbol.SelectSymbol)1 DocKeys (io.crate.analyze.where.DocKeys)1 ValidationException (io.crate.exceptions.ValidationException)1 VersionInvalidException (io.crate.exceptions.VersionInvalidException)1 Reference (io.crate.metadata.Reference)1 DocTableInfo (io.crate.metadata.doc.DocTableInfo)1