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, boolean nullFirst) throws IOException {
OrderBy orderBy = new OrderBy(List.of(REFERENCE), new boolean[] { reverseFlag }, new boolean[] { nullFirst });
SortField sortField = new SortedNumericSortField("value", SortField.Type.LONG, reverseFlag);
Long missingValue = (Long) NullSentinelValues.nullSentinelForScoreDoc(orderBy, 0);
sortField.setMissingValue(missingValue);
Sort sort = new Sort(sortField);
OptimizeQueryForSearchAfter queryForSearchAfter = new OptimizeQueryForSearchAfter(orderBy, mock(QueryShardContext.class), name -> valueFieldType);
Query nextPageQuery = queryForSearchAfter.apply(lastCollected);
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;
}
use of io.crate.analyze.OrderBy in project crate by crate.
the class OrderedLuceneBatchIteratorFactoryTest method prepareSearchers.
@Before
public void prepareSearchers() throws Exception {
IndexWriter iw1 = new IndexWriter(new ByteBuffersDirectory(), new IndexWriterConfig(new StandardAnalyzer()));
IndexWriter iw2 = new IndexWriter(new ByteBuffersDirectory(), new IndexWriterConfig(new StandardAnalyzer()));
expectedResult = LongStream.range(0, 20).mapToObj(i -> new Object[] { i }).collect(Collectors.toList());
// expect descending order to differentiate between insert order
expectedResult.sort(Comparator.comparingLong((Object[] o) -> ((long) o[0])).reversed());
for (int i = 0; i < 20; i++) {
Document doc = new Document();
doc.add(new NumericDocValuesField(columnName, i));
if (i % 2 == 0) {
iw1.addDocument(doc);
} else {
iw2.addDocument(doc);
}
}
iw1.commit();
iw2.commit();
searcher1 = new IndexSearcher(DirectoryReader.open(iw1));
searcher2 = new IndexSearcher(DirectoryReader.open(iw2));
orderBy = new OrderBy(Collections.singletonList(reference), reverseFlags, nullsFirst);
}
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().getDataNodeInstance(SystemCollectSource.class);
Reference shardId = new Reference(new ReferenceIdent(new RelationName("sys", "shards"), "id"), RowGranularity.SHARD, DataTypes.INTEGER, 0, null);
RoutedCollectPhase collectPhase = new RoutedCollectPhase(UUID.randomUUID(), 1, "collect", new Routing(Map.of()), RowGranularity.SHARD, Collections.singletonList(shardId), List.of(), WhereClause.MATCH_ALL.queryOrFallback(), DistributionInfo.DEFAULT_BROADCAST);
collectPhase.orderBy(new OrderBy(Collections.singletonList(shardId), new boolean[] { false }, new boolean[] { false }));
Iterable<? extends Row> rows = systemCollectSource.toRowsIterableTransformation(collectPhase, CoordinatorTxnCtx.systemTransactionContext(), unassignedShardRefResolver(), false).apply(Collections.singletonList(new UnassignedShard(1, "foo", mock(ClusterService.class), true, ShardRoutingState.UNASSIGNED)));
Row next = rows.iterator().next();
assertThat(next.numColumns(), is(1));
}
use of io.crate.analyze.OrderBy in project crate by crate.
the class OrderyByAnalyzer method analyzeSortItems.
@Nullable
public static OrderBy analyzeSortItems(List<SortItem> sortItems, Function<Expression, Symbol> expressionToSymbolFunction) {
int size = sortItems.size();
if (size == 0) {
return null;
}
List<Symbol> symbols = new ArrayList<>(size);
boolean[] reverseFlags = new boolean[size];
boolean[] nullsFirst = new boolean[size];
for (int i = 0; i < size; i++) {
SortItem sortItem = sortItems.get(i);
Expression sortKey = sortItem.getSortKey();
Symbol symbol = expressionToSymbolFunction.apply(sortKey);
symbols.add(symbol);
switch(sortItem.getNullOrdering()) {
case FIRST:
nullsFirst[i] = true;
break;
case LAST:
nullsFirst[i] = false;
break;
case UNDEFINED:
nullsFirst[i] = sortItem.getOrdering() == SortItem.Ordering.DESCENDING;
break;
default:
}
reverseFlags[i] = sortItem.getOrdering() == SortItem.Ordering.DESCENDING;
}
return new OrderBy(symbols, reverseFlags, nullsFirst);
}
use of io.crate.analyze.OrderBy in project crate by crate.
the class ShardCollectSource method getIterator.
@Override
public CompletableFuture<BatchIterator<Row>> getIterator(TransactionContext txnCtx, CollectPhase phase, CollectTask collectTask, boolean supportMoveToStart) {
RoutedCollectPhase collectPhase = (RoutedCollectPhase) phase;
String localNodeId = clusterService.localNode().getId();
Projectors projectors = new Projectors(collectPhase.projections(), collectPhase.jobId(), collectTask.txnCtx(), collectTask.getRamAccounting(), collectTask.memoryManager(), sharedProjectorFactory);
boolean requireMoveToStartSupport = supportMoveToStart && !projectors.providesIndependentScroll();
if (collectPhase.maxRowGranularity() == RowGranularity.SHARD) {
return CompletableFuture.completedFuture(projectors.wrap(InMemoryBatchIterator.of(getShardsIterator(collectTask.txnCtx(), collectPhase, localNodeId), SentinelRow.SENTINEL, true)));
}
OrderBy orderBy = collectPhase.orderBy();
if (collectPhase.maxRowGranularity() == RowGranularity.DOC && orderBy != null) {
return createMultiShardScoreDocCollector(collectPhase, requireMoveToStartSupport, collectTask, localNodeId).thenApply(projectors::wrap);
}
boolean hasShardProjections = Projections.hasAnyShardProjections(collectPhase.projections());
Map<String, IntIndexedContainer> indexShards = collectPhase.routing().locations().get(localNodeId);
List<CompletableFuture<BatchIterator<Row>>> iterators = indexShards == null ? Collections.emptyList() : getIterators(collectTask, collectPhase, requireMoveToStartSupport, indexShards);
final CompletableFuture<BatchIterator<Row>> result;
switch(iterators.size()) {
case 0:
result = CompletableFuture.completedFuture(InMemoryBatchIterator.empty(SentinelRow.SENTINEL));
break;
case 1:
result = iterators.get(0);
break;
default:
if (hasShardProjections) {
// use AsyncCompositeBatchIterator for multi-threaded loadNextBatch
// in order to process shard-based projections concurrently
// noinspection unchecked
result = CompletableFutures.allAsList(iterators).thenApply(its -> CompositeBatchIterator.asyncComposite(executor, availableThreads, its.toArray(new BatchIterator[0])));
} else {
// noinspection unchecked
result = CompletableFutures.allAsList(iterators).thenApply(its -> CompositeBatchIterator.seqComposite(its.toArray(new BatchIterator[0])));
}
}
return result.thenApply(it -> projectors.wrap(it));
}
Aggregations