Search in sources :

Example 36 with RoutedCollectPhase

use of io.crate.execution.dsl.phases.RoutedCollectPhase in project crate by crate.

the class GroupByPlannerTest method testGroupByOnNodeLevel.

@Test
public void testGroupByOnNodeLevel() throws Exception {
    var e = SQLExecutor.builder(clusterService, 2, RandomizedTest.getRandom(), List.of()).build();
    Collect collect = e.plan("select count(*), name from sys.nodes group by name");
    assertThat("number of nodeIds must be 1, otherwise there must be a merge", collect.resultDescription().nodeIds().size(), is(1));
    RoutedCollectPhase collectPhase = ((RoutedCollectPhase) collect.collectPhase());
    assertThat(collectPhase.projections(), contains(instanceOf(GroupProjection.class), instanceOf(EvalProjection.class)));
    GroupProjection groupProjection = (GroupProjection) collectPhase.projections().get(0);
    assertThat(Symbols.typeView(groupProjection.outputs()), contains(is(DataTypes.STRING), is(DataTypes.LONG)));
    assertThat(Symbols.typeView(collectPhase.projections().get(1).outputs()), contains(is(DataTypes.LONG), is(DataTypes.STRING)));
}
Also used : Collect(io.crate.planner.node.dql.Collect) GroupProjection(io.crate.execution.dsl.projection.GroupProjection) RoutedCollectPhase(io.crate.execution.dsl.phases.RoutedCollectPhase) CrateDummyClusterServiceUnitTest(io.crate.test.integration.CrateDummyClusterServiceUnitTest) Test(org.junit.Test) RandomizedTest(com.carrotsearch.randomizedtesting.RandomizedTest)

Example 37 with RoutedCollectPhase

use of io.crate.execution.dsl.phases.RoutedCollectPhase in project crate by crate.

the class CollectTest method test.

@Test
public void test() throws Exception {
    var e = SQLExecutor.builder(clusterService).addTable("CREATE TABLE t (x int)").build();
    TableStats tableStats = new TableStats();
    PlannerContext plannerCtx = e.getPlannerContext(clusterService.state());
    ProjectionBuilder projectionBuilder = new ProjectionBuilder(e.nodeCtx);
    QueriedSelectRelation analyzedRelation = e.analyze("SELECT 123 AS alias, 456 AS alias2 FROM t ORDER BY alias, 2");
    LogicalPlanner logicalPlanner = new LogicalPlanner(e.nodeCtx, tableStats, () -> clusterService.state().nodes().getMinNodeVersion());
    LogicalPlan operator = logicalPlanner.plan(analyzedRelation, plannerCtx);
    ExecutionPlan build = operator.build(plannerCtx, Set.of(), projectionBuilder, -1, 0, null, null, Row.EMPTY, SubQueryResults.EMPTY);
    assertThat((((RoutedCollectPhase) ((io.crate.planner.node.dql.Collect) build).collectPhase())).orderBy(), is(nullValue()));
}
Also used : PlannerContext(io.crate.planner.PlannerContext) ExecutionPlan(io.crate.planner.ExecutionPlan) QueriedSelectRelation(io.crate.analyze.QueriedSelectRelation) TableStats(io.crate.statistics.TableStats) ProjectionBuilder(io.crate.execution.dsl.projection.builder.ProjectionBuilder) RoutedCollectPhase(io.crate.execution.dsl.phases.RoutedCollectPhase) Test(org.junit.Test) CrateDummyClusterServiceUnitTest(io.crate.test.integration.CrateDummyClusterServiceUnitTest)

Example 38 with RoutedCollectPhase

use of io.crate.execution.dsl.phases.RoutedCollectPhase 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)

Example 39 with RoutedCollectPhase

use of io.crate.execution.dsl.phases.RoutedCollectPhase in project crate by crate.

the class RemoteCollectorFactory method createCollector.

/**
 * create a RemoteCollector
 * The RemoteCollector will collect data from another node using a wormhole as if it was collecting on this node.
 * <p>
 * This should only be used if a shard is not available on the current node due to a relocation
 */
public CompletableFuture<BatchIterator<Row>> createCollector(ShardId shardId, RoutedCollectPhase collectPhase, CollectTask collectTask, ShardCollectorProviderFactory shardCollectorProviderFactory) {
    ShardStateObserver shardStateObserver = new ShardStateObserver(clusterService);
    CompletableFuture<ShardRouting> shardBecameActive = shardStateObserver.waitForActiveShard(shardId);
    Runnable onClose = () -> {
    };
    Consumer<Throwable> kill = killReason -> {
        shardBecameActive.cancel(true);
        shardBecameActive.completeExceptionally(killReason);
    };
    return shardBecameActive.thenApply(activePrimaryRouting -> CollectingBatchIterator.newInstance(onClose, kill, () -> retrieveRows(activePrimaryRouting, collectPhase, collectTask, shardCollectorProviderFactory), true));
}
Also used : ShardRouting(org.elasticsearch.cluster.routing.ShardRouting) ShardId(org.elasticsearch.index.shard.ShardId) Projections(io.crate.execution.dsl.projection.Projections) ClusterService(org.elasticsearch.cluster.service.ClusterService) Buckets(io.crate.data.Buckets) BatchIterator(io.crate.data.BatchIterator) CompletableFuture(java.util.concurrent.CompletableFuture) CollectingRowConsumer(io.crate.data.CollectingRowConsumer) Inject(org.elasticsearch.common.inject.Inject) ArrayList(java.util.ArrayList) Routing(io.crate.metadata.Routing) IntArrayList(com.carrotsearch.hppc.IntArrayList) RemoteCollector(io.crate.execution.engine.collect.collectors.RemoteCollector) Map(java.util.Map) ShardStateObserver(io.crate.execution.engine.collect.collectors.ShardStateObserver) ThreadPool(org.elasticsearch.threadpool.ThreadPool) DistributionInfo(io.crate.planner.distribution.DistributionInfo) IndicesService(org.elasticsearch.indices.IndicesService) Collector(java.util.stream.Collector) ShardCollectorProviderFactory(io.crate.execution.engine.collect.sources.ShardCollectorProviderFactory) Executor(java.util.concurrent.Executor) UUIDs(org.elasticsearch.common.UUIDs) RoutedCollectPhase(io.crate.execution.dsl.phases.RoutedCollectPhase) UUID(java.util.UUID) TransportActionProvider(io.crate.execution.TransportActionProvider) Collectors(java.util.stream.Collectors) Lists2(io.crate.common.collections.Lists2) CollectingBatchIterator(io.crate.data.CollectingBatchIterator) TasksService(io.crate.execution.jobs.TasksService) Consumer(java.util.function.Consumer) Exceptions(io.crate.exceptions.Exceptions) List(java.util.List) Row(io.crate.data.Row) Singleton(org.elasticsearch.common.inject.Singleton) ShardRouting(org.elasticsearch.cluster.routing.ShardRouting) ShardStateObserver(io.crate.execution.engine.collect.collectors.ShardStateObserver)

Example 40 with RoutedCollectPhase

use of io.crate.execution.dsl.phases.RoutedCollectPhase in project crate by crate.

the class SystemCollectSource method getIterator.

@Override
public CompletableFuture<BatchIterator<Row>> getIterator(TransactionContext txnCtx, CollectPhase phase, CollectTask collectTask, boolean supportMoveToStart) {
    RoutedCollectPhase collectPhase = (RoutedCollectPhase) phase;
    Map<String, Map<String, IntIndexedContainer>> locations = collectPhase.routing().locations();
    String table = Iterables.getOnlyElement(locations.get(clusterService.localNode().getId()).keySet());
    RelationName relationName = RelationName.fromIndexName(table);
    StaticTableDefinition<?> tableDefinition = tableDefinition(relationName);
    User user = requireNonNull(userLookup.findUser(txnCtx.sessionSettings().userName()), "User who invoked a statement must exist");
    return CompletableFuture.completedFuture(CollectingBatchIterator.newInstance(() -> {
    }, // If data is already local, then `CollectingBatchIterator` takes care of kill handling.
    t -> {
    }, () -> tableDefinition.retrieveRecords(txnCtx, user).thenApply(records -> recordsToRows(collectPhase, collectTask.txnCtx(), tableDefinition.getReferenceResolver(), supportMoveToStart, records)), tableDefinition.involvesIO()));
}
Also used : UserLookup(io.crate.user.UserLookup) TransactionContext(io.crate.metadata.TransactionContext) InformationSchemaTableDefinitions(io.crate.metadata.information.InformationSchemaTableDefinitions) RelationName(io.crate.metadata.RelationName) ClusterService(org.elasticsearch.cluster.service.ClusterService) BatchIterator(io.crate.data.BatchIterator) ReferenceResolver(io.crate.expression.reference.ReferenceResolver) CompletableFuture(java.util.concurrent.CompletableFuture) Function(java.util.function.Function) PgCatalogSchemaInfo(io.crate.metadata.pgcatalog.PgCatalogSchemaInfo) SysRowUpdater(io.crate.expression.reference.sys.SysRowUpdater) Inject(org.elasticsearch.common.inject.Inject) ArrayList(java.util.ArrayList) SysNodeChecks(io.crate.expression.reference.sys.check.node.SysNodeChecks) SchemaUnknownException(io.crate.exceptions.SchemaUnknownException) SysSchemaInfo(io.crate.metadata.sys.SysSchemaInfo) Map(java.util.Map) Objects.requireNonNull(java.util.Objects.requireNonNull) CollectPhase(io.crate.execution.dsl.phases.CollectPhase) PgCatalogTableDefinitions(io.crate.metadata.pgcatalog.PgCatalogTableDefinitions) InformationSchemaInfo(io.crate.metadata.information.InformationSchemaInfo) IntIndexedContainer(com.carrotsearch.hppc.IntIndexedContainer) NodeContext(io.crate.metadata.NodeContext) User(io.crate.user.User) RoutedCollectPhase(io.crate.execution.dsl.phases.RoutedCollectPhase) RowsTransformer(io.crate.execution.engine.collect.RowsTransformer) StaticTableDefinition(io.crate.expression.reference.StaticTableDefinition) Iterables(io.crate.common.collections.Iterables) CollectingBatchIterator(io.crate.data.CollectingBatchIterator) CollectTask(io.crate.execution.engine.collect.CollectTask) SysNodeChecksTableInfo(io.crate.metadata.sys.SysNodeChecksTableInfo) Row(io.crate.data.Row) SysTableDefinitions(io.crate.metadata.sys.SysTableDefinitions) UserManager(io.crate.user.UserManager) InputFactory(io.crate.expression.InputFactory) RelationUnknown(io.crate.exceptions.RelationUnknown) User(io.crate.user.User) RelationName(io.crate.metadata.RelationName) Map(java.util.Map) RoutedCollectPhase(io.crate.execution.dsl.phases.RoutedCollectPhase)

Aggregations

RoutedCollectPhase (io.crate.execution.dsl.phases.RoutedCollectPhase)50 Test (org.junit.Test)39 Collect (io.crate.planner.node.dql.Collect)23 CrateDummyClusterServiceUnitTest (io.crate.test.integration.CrateDummyClusterServiceUnitTest)23 RandomizedTest (com.carrotsearch.randomizedtesting.RandomizedTest)18 Routing (io.crate.metadata.Routing)18 MergePhase (io.crate.execution.dsl.phases.MergePhase)14 Merge (io.crate.planner.Merge)10 Symbol (io.crate.expression.symbol.Symbol)9 GroupProjection (io.crate.execution.dsl.projection.GroupProjection)8 Reference (io.crate.metadata.Reference)8 Row (io.crate.data.Row)7 EvalProjection (io.crate.execution.dsl.projection.EvalProjection)7 ArrayList (java.util.ArrayList)6 Bucket (io.crate.data.Bucket)5 RelationName (io.crate.metadata.RelationName)5 UUID (java.util.UUID)4 ClusterService (org.elasticsearch.cluster.service.ClusterService)4 IntIndexedContainer (com.carrotsearch.hppc.IntIndexedContainer)3 OrderBy (io.crate.analyze.OrderBy)3