Search in sources :

Example 51 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, 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;
}
Also used : OrderBy(io.crate.analyze.OrderBy) SortedNumericSortField(org.apache.lucene.search.SortedNumericSortField) Query(org.apache.lucene.search.Query) FuzzyQuery(org.apache.lucene.search.FuzzyQuery) MatchAllDocsQuery(org.apache.lucene.search.MatchAllDocsQuery) ConstantScoreQuery(org.apache.lucene.search.ConstantScoreQuery) TermQuery(org.apache.lucene.search.TermQuery) FieldDoc(org.apache.lucene.search.FieldDoc) Sort(org.apache.lucene.search.Sort) QueryShardContext(org.elasticsearch.index.query.QueryShardContext) TopFieldDocs(org.apache.lucene.search.TopFieldDocs) SortField(org.apache.lucene.search.SortField) SortedNumericSortField(org.apache.lucene.search.SortedNumericSortField)

Example 52 with OrderBy

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);
}
Also used : IndexSearcher(org.apache.lucene.search.IndexSearcher) OrderBy(io.crate.analyze.OrderBy) NumericDocValuesField(org.apache.lucene.document.NumericDocValuesField) IndexWriter(org.apache.lucene.index.IndexWriter) ByteBuffersDirectory(org.apache.lucene.store.ByteBuffersDirectory) StandardAnalyzer(org.apache.lucene.analysis.standard.StandardAnalyzer) Document(org.apache.lucene.document.Document) IndexWriterConfig(org.apache.lucene.index.IndexWriterConfig) Before(org.junit.Before)

Example 53 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().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));
}
Also used : OrderBy(io.crate.analyze.OrderBy) ClusterService(org.elasticsearch.cluster.service.ClusterService) Reference(io.crate.metadata.Reference) RelationName(io.crate.metadata.RelationName) Routing(io.crate.metadata.Routing) Row(io.crate.data.Row) UnassignedShard(io.crate.metadata.shard.unassigned.UnassignedShard) ReferenceIdent(io.crate.metadata.ReferenceIdent) RoutedCollectPhase(io.crate.execution.dsl.phases.RoutedCollectPhase) Test(org.junit.Test)

Example 54 with OrderBy

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);
}
Also used : OrderBy(io.crate.analyze.OrderBy) SortItem(io.crate.sql.tree.SortItem) Expression(io.crate.sql.tree.Expression) Symbol(io.crate.expression.symbol.Symbol) ArrayList(java.util.ArrayList) Nullable(javax.annotation.Nullable)

Example 55 with OrderBy

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));
}
Also used : OrderBy(io.crate.analyze.OrderBy) ShardId(org.elasticsearch.index.shard.ShardId) IndexParts(io.crate.metadata.IndexParts) TransactionContext(io.crate.metadata.TransactionContext) Projections(io.crate.execution.dsl.projection.Projections) IntCursor(com.carrotsearch.hppc.cursors.IntCursor) IndexMetadata(org.elasticsearch.cluster.metadata.IndexMetadata) OrderedDocCollector(io.crate.execution.engine.collect.collectors.OrderedDocCollector) EvaluatingNormalizer(io.crate.expression.eval.EvaluatingNormalizer) NodeLimits(io.crate.execution.jobs.NodeLimits) ShardNotFoundException(org.elasticsearch.index.shard.ShardNotFoundException) ProjectorFactory(io.crate.execution.engine.pipeline.ProjectorFactory) Settings(org.elasticsearch.common.settings.Settings) IndexNotFoundException(org.elasticsearch.index.IndexNotFoundException) Map(java.util.Map) SharedShardContexts(io.crate.execution.jobs.SharedShardContexts) ThreadPool(org.elasticsearch.threadpool.ThreadPool) ThreadPools.numIdleThreads(io.crate.execution.support.ThreadPools.numIdleThreads) OrderingByPosition(io.crate.execution.engine.sort.OrderingByPosition) ShardRowContext(io.crate.expression.reference.sys.shard.ShardRowContext) DocSysColumns(io.crate.metadata.doc.DocSysColumns) IntIndexedContainer(com.carrotsearch.hppc.IntIndexedContainer) NodeContext(io.crate.metadata.NodeContext) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) InMemoryBatchIterator(io.crate.data.InMemoryBatchIterator) OrderedLuceneBatchIteratorFactory(io.crate.execution.engine.collect.collectors.OrderedLuceneBatchIteratorFactory) StaticTableReferenceResolver(io.crate.expression.reference.StaticTableReferenceResolver) RoutedCollectPhase(io.crate.execution.dsl.phases.RoutedCollectPhase) CompletableFutures(io.crate.concurrent.CompletableFutures) RowsTransformer(io.crate.execution.engine.collect.RowsTransformer) IllegalIndexShardStateException(org.elasticsearch.index.shard.IllegalIndexShardStateException) Iterables(io.crate.common.collections.Iterables) CollectTask(io.crate.execution.engine.collect.CollectTask) SysShardsTableInfo(io.crate.metadata.sys.SysShardsTableInfo) OrderByPositionVisitor(io.crate.planner.consumer.OrderByPositionVisitor) List(java.util.List) Exceptions(io.crate.exceptions.Exceptions) Logger(org.apache.logging.log4j.Logger) OrderBy(io.crate.analyze.OrderBy) Row(io.crate.data.Row) Singleton(org.elasticsearch.common.inject.Singleton) Projectors(io.crate.execution.engine.pipeline.Projectors) SharedShardContext(io.crate.execution.jobs.SharedShardContext) SentinelRow(io.crate.data.SentinelRow) MapBackedRefResolver(io.crate.metadata.MapBackedRefResolver) CompositeBatchIterator(io.crate.data.CompositeBatchIterator) RemoteCollectorFactory(io.crate.execution.engine.collect.RemoteCollectorFactory) ThreadPoolExecutor(java.util.concurrent.ThreadPoolExecutor) ClusterService(org.elasticsearch.cluster.service.ClusterService) RowAccountingWithEstimators(io.crate.breaker.RowAccountingWithEstimators) CompletableFuture(java.util.concurrent.CompletableFuture) BatchIterator(io.crate.data.BatchIterator) Index(org.elasticsearch.index.Index) ShardRoutingState(org.elasticsearch.cluster.routing.ShardRoutingState) Supplier(java.util.function.Supplier) ArrayList(java.util.ArrayList) Inject(org.elasticsearch.common.inject.Inject) CircuitBreakerService(org.elasticsearch.indices.breaker.CircuitBreakerService) Metadata(org.elasticsearch.cluster.metadata.Metadata) UnassignedShard(io.crate.metadata.shard.unassigned.UnassignedShard) Symbols(io.crate.expression.symbol.Symbols) CollectPhase(io.crate.execution.dsl.phases.CollectPhase) IndicesService(org.elasticsearch.indices.IndicesService) IntSupplier(java.util.function.IntSupplier) Nullable(javax.annotation.Nullable) EsExecutors(org.elasticsearch.common.util.concurrent.EsExecutors) IndexEventListener(org.elasticsearch.index.shard.IndexEventListener) Executor(java.util.concurrent.Executor) IndexService(org.elasticsearch.index.IndexService) IndexShard(org.elasticsearch.index.shard.IndexShard) DataType(io.crate.types.DataType) ProjectionToProjectorVisitor(io.crate.execution.engine.pipeline.ProjectionToProjectorVisitor) TransportActionProvider(io.crate.execution.TransportActionProvider) RowGranularity(io.crate.metadata.RowGranularity) ShardCollectorProvider(io.crate.execution.engine.collect.ShardCollectorProvider) Suppliers(io.crate.common.Suppliers) InputFactory(io.crate.expression.InputFactory) Collections(java.util.Collections) LogManager(org.apache.logging.log4j.LogManager) IntIndexedContainer(com.carrotsearch.hppc.IntIndexedContainer) InMemoryBatchIterator(io.crate.data.InMemoryBatchIterator) CompositeBatchIterator(io.crate.data.CompositeBatchIterator) BatchIterator(io.crate.data.BatchIterator) Projectors(io.crate.execution.engine.pipeline.Projectors) CompletableFuture(java.util.concurrent.CompletableFuture) Row(io.crate.data.Row) SentinelRow(io.crate.data.SentinelRow) RoutedCollectPhase(io.crate.execution.dsl.phases.RoutedCollectPhase)

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