Search in sources :

Example 36 with OrderBy

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

the class ShardCollectSource method createMultiShardScoreDocCollector.

private CrateCollector createMultiShardScoreDocCollector(RoutedCollectPhase collectPhase, BatchConsumer consumer, JobCollectContext jobCollectContext, String localNodeId) {
    Map<String, Map<String, List<Integer>>> locations = collectPhase.routing().locations();
    SharedShardContexts sharedShardContexts = jobCollectContext.sharedShardContexts();
    Map<String, List<Integer>> indexShards = locations.get(localNodeId);
    List<OrderedDocCollector> orderedDocCollectors = new ArrayList<>();
    for (Map.Entry<String, List<Integer>> entry : indexShards.entrySet()) {
        String indexName = entry.getKey();
        for (Integer shardNum : entry.getValue()) {
            ShardId shardId = new ShardId(indexName, shardNum);
            SharedShardContext context = sharedShardContexts.getOrCreateContext(shardId);
            try {
                ShardCollectorProvider shardCollectorProvider = getCollectorProviderSafe(shardId);
                orderedDocCollectors.add(shardCollectorProvider.getOrderedCollector(collectPhase, context, jobCollectContext, consumer.requiresScroll()));
            } catch (ShardNotFoundException | IllegalIndexShardStateException e) {
                throw e;
            } catch (IndexNotFoundException e) {
                if (PartitionName.isPartition(indexName)) {
                    break;
                }
                throw e;
            } catch (Throwable t) {
                throw new UnhandledServerException(t);
            }
        }
    }
    OrderBy orderBy = collectPhase.orderBy();
    assert orderBy != null : "orderBy must not be null";
    return BatchIteratorCollectorBridge.newInstance(OrderedLuceneBatchIteratorFactory.newInstance(orderedDocCollectors, collectPhase.toCollect().size(), OrderingByPosition.rowOrdering(OrderByPositionVisitor.orderByPositions(orderBy.orderBySymbols(), collectPhase.toCollect()), orderBy.reverseFlags(), orderBy.nullsFirst()), executor, consumer.requiresScroll()), consumer);
}
Also used : OrderBy(io.crate.analyze.OrderBy) ArrayList(java.util.ArrayList) IllegalIndexShardStateException(org.elasticsearch.index.shard.IllegalIndexShardStateException) OrderedDocCollector(io.crate.operation.collect.collectors.OrderedDocCollector) ShardId(org.elasticsearch.index.shard.ShardId) SharedShardContexts(io.crate.action.job.SharedShardContexts) ShardNotFoundException(org.elasticsearch.index.shard.ShardNotFoundException) IndexNotFoundException(org.elasticsearch.index.IndexNotFoundException) UnhandledServerException(io.crate.exceptions.UnhandledServerException) List(java.util.List) ArrayList(java.util.ArrayList) Map(java.util.Map) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) SharedShardContext(io.crate.action.job.SharedShardContext)

Example 37 with OrderBy

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

the class RowsTransformer method toRowsIterable.

public static Iterable<Row> toRowsIterable(InputFactory inputFactory, ReferenceResolver<?> referenceResolver, RoutedCollectPhase collectPhase, Iterable<?> iterable) {
    WhereClause whereClause = collectPhase.whereClause();
    if (whereClause.noMatch()) {
        return Collections.emptyList();
    }
    InputFactory.Context ctx = inputFactory.ctxForRefs(referenceResolver);
    ctx.add(collectPhase.toCollect());
    OrderBy orderBy = collectPhase.orderBy();
    if (orderBy != null) {
        for (Symbol symbol : orderBy.orderBySymbols()) {
            ctx.add(symbol);
        }
    }
    Input<Boolean> condition;
    if (whereClause.hasQuery()) {
        assert DataTypes.BOOLEAN.equals(whereClause.query().valueType()) : "whereClause.query() must be of type " + DataTypes.BOOLEAN;
        //noinspection unchecked  whereClause().query() is a symbol of type boolean so it must become Input<Boolean>
        condition = (Input<Boolean>) ctx.add(whereClause.query());
    } else {
        condition = Literal.BOOLEAN_TRUE;
    }
    @SuppressWarnings("unchecked") Iterable<Row> rows = Iterables.filter(Iterables.transform(iterable, new ValueAndInputRow<>(ctx.topLevelInputs(), ctx.expressions())), InputCondition.asPredicate(condition));
    if (orderBy == null) {
        return rows;
    }
    return sortRows(Iterables.transform(rows, Row::materialize), collectPhase);
}
Also used : OrderBy(io.crate.analyze.OrderBy) InputFactory(io.crate.operation.InputFactory) Symbol(io.crate.analyze.symbol.Symbol) WhereClause(io.crate.analyze.WhereClause) Row(io.crate.data.Row)

Example 38 with OrderBy

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

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

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

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