Search in sources :

Example 16 with QuerySpec

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

the class QueriedDocTableFetchPushDownTest method testNoPushDownWithOrder.

@Test
public void testNoPushDownWithOrder() throws Exception {
    QuerySpec qs = new QuerySpec();
    qs.outputs(Lists.newArrayList(REF_I));
    qs.orderBy(new OrderBy(ImmutableList.of(REF_I), new boolean[] { true }, new Boolean[] { false }));
    QueriedDocTableFetchPushDown pd = new QueriedDocTableFetchPushDown(new QueriedDocTable(TABLE_REL, qs));
    assertNull(pd.pushDown());
    assertThat(qs, isSQL("SELECT s.t.i 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 17 with QuerySpec

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

the class QueriedDocTableFetchPushDownTest method testLimitIsPushedDown.

@Test
public void testLimitIsPushedDown() throws Exception {
    QuerySpec qs = new QuerySpec();
    qs.outputs(Lists.newArrayList(REF_I, REF_A));
    qs.limit(Optional.of(Literal.of(10)));
    qs.offset(Optional.of(Literal.of(100)));
    QueriedDocTableFetchPushDown pd = new QueriedDocTableFetchPushDown(new QueriedDocTable(TABLE_REL, qs));
    QueriedDocTable sub = pd.pushDown();
    assertThat(sub.querySpec().limit().get(), is(Literal.of(10)));
    assertThat(sub.querySpec().offset().get(), is(Literal.of(100)));
    assertThat(qs.limit().get(), is(Literal.of(10)));
    assertThat(qs.offset().get(), is(Literal.of(100)));
}
Also used : QueriedDocTable(io.crate.analyze.relations.QueriedDocTable) QuerySpec(io.crate.analyze.QuerySpec) Test(org.junit.Test)

Example 18 with QuerySpec

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

the class QueriedDocTableFetchPushDownTest method testWhereIsPushedDown.

@Test
public void testWhereIsPushedDown() throws Exception {
    QuerySpec qs = new QuerySpec();
    qs.outputs(Lists.newArrayList(REF_I, REF_A));
    qs.where(WhereClause.NO_MATCH);
    QueriedDocTableFetchPushDown pd = new QueriedDocTableFetchPushDown(new QueriedDocTable(TABLE_REL, qs));
    QueriedDocTable sub = pd.pushDown();
    assertThat(sub.querySpec().where(), is(WhereClause.NO_MATCH));
    assertThat(qs.where(), is(WhereClause.MATCH_ALL));
}
Also used : QueriedDocTable(io.crate.analyze.relations.QueriedDocTable) QuerySpec(io.crate.analyze.QuerySpec) Test(org.junit.Test)

Example 19 with QuerySpec

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

the class QueriedDocTableFetchPushDownTest method testPushDownWithoutOrder.

@Test
public void testPushDownWithoutOrder() throws Exception {
    QuerySpec qs = new QuerySpec();
    qs.outputs(Lists.newArrayList(REF_A, REF_I));
    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'])"));
    assertThat(sub.querySpec(), isSQL("SELECT s.t._fetchid"));
}
Also used : QueriedDocTable(io.crate.analyze.relations.QueriedDocTable) QuerySpec(io.crate.analyze.QuerySpec) Test(org.junit.Test)

Example 20 with QuerySpec

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

the class QueriedDocTableFetchPushDown method pushDown.

@Nullable
QueriedDocTable pushDown() {
    if (querySpec.groupBy().isPresent() || querySpec.having().isPresent() || querySpec.hasAggregates()) {
        return null;
    }
    Optional<OrderBy> orderBy = querySpec.orderBy();
    FetchRequiredVisitor.Context context;
    if (orderBy.isPresent()) {
        context = new FetchRequiredVisitor.Context(new LinkedHashSet<>(orderBy.get().orderBySymbols()));
    } else {
        context = new FetchRequiredVisitor.Context();
    }
    boolean fetchRequired = FetchRequiredVisitor.INSTANCE.process(querySpec.outputs(), context);
    if (!fetchRequired)
        return null;
    // build the subquery
    QuerySpec sub = new QuerySpec();
    Reference docIdReference = DocSysColumns.forTable(tableRelation.tableInfo().ident(), DocSysColumns.FETCHID);
    List<Symbol> outputs = new ArrayList<>();
    if (orderBy.isPresent()) {
        sub.orderBy(orderBy.get());
        outputs.add(docIdReference);
        outputs.addAll(context.querySymbols());
    } else {
        outputs.add(docIdReference);
    }
    for (Symbol symbol : querySpec.outputs()) {
        if (Symbols.containsColumn(symbol, DocSysColumns.SCORE) && !outputs.contains(symbol)) {
            outputs.add(symbol);
        }
    }
    sub.outputs(outputs);
    QueriedDocTable subRelation = new QueriedDocTable(tableRelation, sub);
    HashMap<Symbol, InputColumn> symbolMap = new HashMap<>(sub.outputs().size());
    int index = 0;
    for (Symbol symbol : sub.outputs()) {
        symbolMap.put(symbol, new InputColumn(index++, symbol.valueType()));
    }
    // push down the where clause
    sub.where(querySpec.where());
    querySpec.where(null);
    ToFetchReferenceVisitor toFetchReferenceVisitor = new ToFetchReferenceVisitor();
    toFetchReferenceVisitor.processInplace(querySpec.outputs(), symbolMap);
    if (orderBy.isPresent()) {
        // replace order by symbols with input columns, we need to copy the order by since it was pushed down to the
        // subquery before
        List<Symbol> newOrderBySymbols = MappingSymbolVisitor.copying().process(orderBy.get().orderBySymbols(), symbolMap);
        querySpec.orderBy(new OrderBy(newOrderBySymbols, orderBy.get().reverseFlags(), orderBy.get().nullsFirst()));
    }
    sub.limit(querySpec.limit());
    sub.offset(querySpec.offset());
    return subRelation;
}
Also used : OrderBy(io.crate.analyze.OrderBy) Reference(io.crate.metadata.Reference) QueriedDocTable(io.crate.analyze.relations.QueriedDocTable) QuerySpec(io.crate.analyze.QuerySpec) Nullable(javax.annotation.Nullable)

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