Search in sources :

Example 41 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 42 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 43 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 44 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 45 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)64 Test (org.junit.Test)29 Symbol (io.crate.expression.symbol.Symbol)16 ArrayList (java.util.ArrayList)14 QuerySpec (io.crate.analyze.QuerySpec)13 List (java.util.List)13 Row (io.crate.data.Row)12 Reference (io.crate.metadata.Reference)10 Nullable (javax.annotation.Nullable)9 QueriedDocTable (io.crate.analyze.relations.QueriedDocTable)8 CrateDummyClusterServiceUnitTest (io.crate.test.integration.CrateDummyClusterServiceUnitTest)8 Map (java.util.Map)8 ShardId (org.elasticsearch.index.shard.ShardId)8 Symbol (io.crate.analyze.symbol.Symbol)7 InputFactory (io.crate.expression.InputFactory)7 RelationName (io.crate.metadata.RelationName)7 WindowDefinition (io.crate.analyze.WindowDefinition)6 TransactionContext (io.crate.metadata.TransactionContext)6 DataType (io.crate.types.DataType)6 Lists2 (io.crate.common.collections.Lists2)5