Search in sources :

Example 26 with QuerySpec

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

the class MultiSourceAggregationConsumer method removeAggregationsAndLimitsFromMSS.

private static void removeAggregationsAndLimitsFromMSS(MultiSourceSelect mss, SplitPoints splitPoints) {
    QuerySpec querySpec = mss.querySpec();
    List<Symbol> outputs = Lists2.concatUnique(splitPoints.toCollect(), extractFieldsFromJoinConditions(mss));
    querySpec.outputs(outputs);
    querySpec.hasAggregates(false);
    // Limit & offset must be applied after the aggregation, so remove it from mss and sources.
    // OrderBy can be ignored because it's also applied after aggregation but there is always only 1 row so it
    // wouldn't have any effect.
    removeLimitOffsetAndOrder(querySpec);
    for (RelationSource relationSource : mss.sources().values()) {
        removeLimitOffsetAndOrder(relationSource.querySpec());
    }
    // need to change the types on the fields of the MSS to match the new outputs
    ListIterator<Field> fieldsIt = mss.fields().listIterator();
    Iterator<Function> outputsIt = splitPoints.aggregates().iterator();
    while (fieldsIt.hasNext()) {
        Field field = fieldsIt.next();
        Symbol output = outputsIt.next();
        fieldsIt.set(new Field(field.relation(), field.path(), output.valueType()));
    }
}
Also used : Field(io.crate.analyze.symbol.Field) Function(io.crate.analyze.symbol.Function) RelationSource(io.crate.analyze.RelationSource) Symbol(io.crate.analyze.symbol.Symbol) QuerySpec(io.crate.analyze.QuerySpec)

Example 27 with QuerySpec

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

the class RelationSplitterTest method testQuerySpecSplit.

@Test
public void testQuerySpecSplit() throws Exception {
    QuerySpec querySpec = fromQuery("x = 1 and y = 3 and x + y = 4");
    RelationSplitter splitter = split(querySpec);
    assertThat(querySpec, isSQL("SELECT true WHERE (true AND (add(doc.t1.x, doc.t2.y) = 4))"));
    assertThat(splitter.getSpec(T3.TR_1), isSQL("SELECT doc.t1.x WHERE (doc.t1.x = 1)"));
    assertThat(splitter.getSpec(T3.TR_2), isSQL("SELECT doc.t2.y WHERE (true AND (doc.t2.y = 3))"));
}
Also used : QuerySpec(io.crate.analyze.QuerySpec) Test(org.junit.Test) CrateUnitTest(io.crate.test.integration.CrateUnitTest)

Example 28 with QuerySpec

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

the class QueriedDocTableFetchPushDownTest method testScoreGetsPushedDown.

@Test
public void testScoreGetsPushedDown() throws Exception {
    QuerySpec qs = new QuerySpec();
    qs.outputs(Lists.newArrayList(REF_A, REF_I, REF_SCORE));
    qs.orderBy(new OrderBy(Lists.newArrayList(REF_I), new boolean[] { true }, new Boolean[] { false }));
    QueriedDocTableFetchPushDown pd = new QueriedDocTableFetchPushDown(new QueriedDocTable(TABLE_REL, qs));
    QueriedDocTable sub = pd.pushDown();
    assertThat(qs, isSQL("SELECT FETCH(INPUT(0), s.t._doc['a']), INPUT(1), INPUT(2) ORDER BY INPUT(1) DESC NULLS LAST"));
    assertThat(sub.querySpec(), isSQL("SELECT s.t._fetchid, s.t.i, s.t._score ORDER BY s.t.i DESC NULLS LAST"));
}
Also used : OrderBy(io.crate.analyze.OrderBy) QueriedDocTable(io.crate.analyze.relations.QueriedDocTable) QuerySpec(io.crate.analyze.QuerySpec) Test(org.junit.Test)

Example 29 with QuerySpec

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

the class QueriedDocTableFetchPushDownTest method testPushDownOrderRefUsedInFunction.

@Test
public void testPushDownOrderRefUsedInFunction() throws Exception {
    QuerySpec qs = new QuerySpec();
    Function funcOfI = abs(REF_I);
    qs.outputs(Lists.newArrayList(REF_A, REF_I, funcOfI));
    qs.orderBy(new OrderBy(Lists.newArrayList(REF_I), new boolean[] { true }, new Boolean[] { false }));
    QueriedDocTableFetchPushDown pd = new QueriedDocTableFetchPushDown(new QueriedDocTable(TABLE_REL, qs));
    QueriedDocTable sub = pd.pushDown();
    assertThat(qs, isSQL("SELECT FETCH(INPUT(0), s.t._doc['a']), INPUT(1), abs(INPUT(1)) ORDER BY INPUT(1) DESC NULLS LAST"));
    assertThat(sub.querySpec(), isSQL("SELECT s.t._fetchid, s.t.i ORDER BY s.t.i DESC NULLS LAST"));
}
Also used : OrderBy(io.crate.analyze.OrderBy) Function(io.crate.analyze.symbol.Function) AbsFunction(io.crate.operation.scalar.arithmetic.AbsFunction) QueriedDocTable(io.crate.analyze.relations.QueriedDocTable) QuerySpec(io.crate.analyze.QuerySpec) Test(org.junit.Test)

Example 30 with QuerySpec

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

the class QueriedDocTableFetchPushDownTest method testPushDownWithNestedOrder.

@Test
public void testPushDownWithNestedOrder() throws Exception {
    QuerySpec qs = new QuerySpec();
    qs.outputs(Lists.newArrayList(REF_A, REF_I));
    qs.orderBy(new OrderBy(Lists.newArrayList(abs(REF_I)), new boolean[] { true }, new Boolean[] { false }));
    QueriedDocTableFetchPushDown pd = new QueriedDocTableFetchPushDown(new QueriedDocTable(TABLE_REL, qs));
    QueriedDocTable sub = pd.pushDown();
    assertThat(qs, isSQL("SELECT FETCH(INPUT(0), s.t._doc['a']), FETCH(INPUT(0), s.t._doc['i']) ORDER BY INPUT(1) DESC NULLS LAST"));
    assertThat(sub.querySpec(), isSQL("SELECT s.t._fetchid, abs(s.t.i) ORDER BY abs(s.t.i) DESC NULLS LAST"));
}
Also used : OrderBy(io.crate.analyze.OrderBy) QueriedDocTable(io.crate.analyze.relations.QueriedDocTable) QuerySpec(io.crate.analyze.QuerySpec) Test(org.junit.Test)

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