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);
}
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);
}
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());
}
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)"));
}
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"));
}
Aggregations