Search in sources :

Example 21 with OrderBy

use of io.crate.analyze.OrderBy 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 22 with OrderBy

use of io.crate.analyze.OrderBy 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)

Example 23 with OrderBy

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

the class QueriedDocTableFetchPushDownTest method testPushDownWithNestedOrderInOutput.

@Test
public void testPushDownWithNestedOrderInOutput() 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(funcOfI), 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']), INPUT(1) 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) 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 24 with OrderBy

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

the class SystemCollectSourceTest method testOrderBySymbolsDoNotAppearTwiceInRows.

@Test
public void testOrderBySymbolsDoNotAppearTwiceInRows() throws Exception {
    SystemCollectSource systemCollectSource = internalCluster().getInstance(SystemCollectSource.class);
    Reference shardId = new Reference(new ReferenceIdent(new TableIdent("sys", "shards"), "id"), RowGranularity.SHARD, DataTypes.INTEGER);
    RoutedCollectPhase collectPhase = new RoutedCollectPhase(UUID.randomUUID(), 1, "collect", new Routing(ImmutableMap.of()), RowGranularity.SHARD, Collections.singletonList(shardId), ImmutableList.of(), WhereClause.MATCH_ALL, DistributionInfo.DEFAULT_BROADCAST);
    collectPhase.orderBy(new OrderBy(Collections.singletonList(shardId), new boolean[] { false }, new Boolean[] { null }));
    Iterable<? extends Row> rows = systemCollectSource.toRowsIterableTransformation(collectPhase, false).apply(Collections.singletonList(new UnassignedShard(new ShardId("foo", 1), mock(ClusterService.class), true, ShardRoutingState.UNASSIGNED)));
    Row next = rows.iterator().next();
    assertThat(next.numColumns(), is(1));
}
Also used : OrderBy(io.crate.analyze.OrderBy) UnassignedShard(io.crate.metadata.shard.unassigned.UnassignedShard) ShardId(org.elasticsearch.index.shard.ShardId) ClusterService(org.elasticsearch.cluster.ClusterService) Row(io.crate.data.Row) RoutedCollectPhase(io.crate.planner.node.dql.RoutedCollectPhase) Test(org.junit.Test) SQLTransportIntegrationTest(io.crate.integrationtests.SQLTransportIntegrationTest)

Example 25 with OrderBy

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

the class LuceneOrderedDocCollectorTest method nextPageQuery.

private Long[] nextPageQuery(IndexReader reader, FieldDoc lastCollected, boolean reverseFlag, @Nullable Boolean nullFirst) throws IOException {
    OrderBy orderBy = new OrderBy(ImmutableList.<Symbol>of(REFERENCE), new boolean[] { reverseFlag }, new Boolean[] { nullFirst });
    SortField sortField = new SortedNumericSortField("value", SortField.Type.LONG, reverseFlag);
    Long missingValue = (Long) LuceneMissingValue.missingValue(orderBy, 0);
    sortField.setMissingValue(missingValue);
    Sort sort = new Sort(sortField);
    Query nextPageQuery = LuceneOrderedDocCollector.nextPageQuery(lastCollected, orderBy, new Object[] { missingValue }, name -> valueFieldType);
    TopFieldDocs result = search(reader, nextPageQuery, sort);
    Long[] results = new Long[result.scoreDocs.length];
    for (int i = 0; i < result.scoreDocs.length; i++) {
        Long value = (Long) ((FieldDoc) result.scoreDocs[i]).fields[0];
        results[i] = value.equals(missingValue) ? null : value;
    }
    return results;
}
Also used : OrderBy(io.crate.analyze.OrderBy)

Aggregations

OrderBy (io.crate.analyze.OrderBy)27 Test (org.junit.Test)14 QuerySpec (io.crate.analyze.QuerySpec)13 QueriedDocTable (io.crate.analyze.relations.QueriedDocTable)8 Symbol (io.crate.analyze.symbol.Symbol)8 CrateUnitTest (io.crate.test.integration.CrateUnitTest)5 InputFactory (io.crate.operation.InputFactory)4 WhereClause (io.crate.analyze.WhereClause)3 Row (io.crate.data.Row)3 Reference (io.crate.metadata.Reference)3 RoutedCollectPhase (io.crate.planner.node.dql.RoutedCollectPhase)3 ArrayList (java.util.ArrayList)3 List (java.util.List)3 ShardId (org.elasticsearch.index.shard.ShardId)3 Function (io.crate.analyze.symbol.Function)2 AbsFunction (io.crate.operation.scalar.arithmetic.AbsFunction)2 Map (java.util.Map)2 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)2 StandardAnalyzer (org.apache.lucene.analysis.standard.StandardAnalyzer)2 Document (org.apache.lucene.document.Document)2