Search in sources :

Example 21 with NodeContext

use of io.crate.metadata.NodeContext in project crate by crate.

the class MoveOrderBeneathNestedLoop method apply.

@Override
public LogicalPlan apply(Order order, Captures captures, TableStats tableStats, TransactionContext txnCtx, NodeContext nodeCtx) {
    NestedLoopJoin nestedLoop = captures.get(nlCapture);
    Set<RelationName> relationsInOrderBy = Collections.newSetFromMap(new IdentityHashMap<>());
    Consumer<ScopedSymbol> gatherRelationsFromField = f -> relationsInOrderBy.add(f.relation());
    Consumer<Reference> gatherRelationsFromRef = r -> relationsInOrderBy.add(r.ident().tableIdent());
    OrderBy orderBy = order.orderBy();
    for (Symbol orderExpr : orderBy.orderBySymbols()) {
        FieldsVisitor.visitFields(orderExpr, gatherRelationsFromField);
        RefVisitor.visitRefs(orderExpr, gatherRelationsFromRef);
    }
    if (relationsInOrderBy.size() == 1) {
        RelationName relationInOrderBy = relationsInOrderBy.iterator().next();
        if (relationInOrderBy == nestedLoop.topMostLeftRelation().relationName()) {
            LogicalPlan lhs = nestedLoop.sources().get(0);
            LogicalPlan newLhs = order.replaceSources(List.of(lhs));
            return new NestedLoopJoin(newLhs, nestedLoop.sources().get(1), nestedLoop.joinType(), nestedLoop.joinCondition(), nestedLoop.isFiltered(), nestedLoop.topMostLeftRelation(), true, nestedLoop.isRewriteFilterOnOuterJoinToInnerJoinDone());
        }
    }
    return null;
}
Also used : TransactionContext(io.crate.metadata.TransactionContext) NodeContext(io.crate.metadata.NodeContext) LogicalPlan(io.crate.planner.operators.LogicalPlan) Captures(io.crate.planner.optimizer.matcher.Captures) IdentityHashMap(java.util.IdentityHashMap) RelationName(io.crate.metadata.RelationName) Reference(io.crate.metadata.Reference) Pattern(io.crate.planner.optimizer.matcher.Pattern) Set(java.util.Set) NestedLoopJoin(io.crate.planner.operators.NestedLoopJoin) Rule(io.crate.planner.optimizer.Rule) Consumer(java.util.function.Consumer) Order(io.crate.planner.operators.Order) RefVisitor(io.crate.expression.symbol.RefVisitor) List(java.util.List) OrderBy(io.crate.analyze.OrderBy) TableStats(io.crate.statistics.TableStats) Symbol(io.crate.expression.symbol.Symbol) Pattern.typeOf(io.crate.planner.optimizer.matcher.Pattern.typeOf) ScopedSymbol(io.crate.expression.symbol.ScopedSymbol) Patterns.source(io.crate.planner.optimizer.matcher.Patterns.source) Collections(java.util.Collections) FieldsVisitor(io.crate.expression.symbol.FieldsVisitor) Capture(io.crate.planner.optimizer.matcher.Capture) OrderBy(io.crate.analyze.OrderBy) Reference(io.crate.metadata.Reference) Symbol(io.crate.expression.symbol.Symbol) ScopedSymbol(io.crate.expression.symbol.ScopedSymbol) NestedLoopJoin(io.crate.planner.operators.NestedLoopJoin) RelationName(io.crate.metadata.RelationName) LogicalPlan(io.crate.planner.operators.LogicalPlan) ScopedSymbol(io.crate.expression.symbol.ScopedSymbol)

Example 22 with NodeContext

use of io.crate.metadata.NodeContext in project crate by crate.

the class IndexWriterProjectorTest method testIndexWriter.

@Test
public void testIndexWriter() throws Throwable {
    execute("create table bulk_import (id int primary key, name string) with (number_of_replicas=0)");
    ensureGreen();
    InputCollectExpression sourceInput = new InputCollectExpression(1);
    List<CollectExpression<Row, ?>> collectExpressions = Collections.<CollectExpression<Row, ?>>singletonList(sourceInput);
    RelationName bulkImportIdent = new RelationName(sqlExecutor.getCurrentSchema(), "bulk_import");
    ClusterState state = clusterService().state();
    Settings tableSettings = TableSettingsResolver.get(state.getMetadata(), bulkImportIdent, false);
    ThreadPool threadPool = internalCluster().getInstance(ThreadPool.class);
    IndexWriterProjector writerProjector = new IndexWriterProjector(clusterService(), new NodeLimits(new ClusterSettings(Settings.EMPTY, ClusterSettings.BUILT_IN_CLUSTER_SETTINGS)), new NoopCircuitBreaker("dummy"), RamAccounting.NO_ACCOUNTING, threadPool.scheduler(), threadPool.executor(ThreadPool.Names.SEARCH), CoordinatorTxnCtx.systemTransactionContext(), new NodeContext(internalCluster().getInstance(Functions.class)), Settings.EMPTY, IndexMetadata.INDEX_NUMBER_OF_SHARDS_SETTING.get(tableSettings), NumberOfReplicas.fromSettings(tableSettings, state.getNodes().getSize()), internalCluster().getInstance(TransportCreatePartitionsAction.class), internalCluster().getInstance(TransportShardUpsertAction.class)::execute, IndexNameResolver.forTable(bulkImportIdent), new Reference(new ReferenceIdent(bulkImportIdent, DocSysColumns.RAW), RowGranularity.DOC, DataTypes.STRING, 0, null), Collections.singletonList(ID_IDENT), Collections.<Symbol>singletonList(new InputColumn(0)), null, null, sourceInput, collectExpressions, 20, null, null, false, false, UUID.randomUUID(), UpsertResultContext.forRowCount(), false);
    BatchIterator rowsIterator = InMemoryBatchIterator.of(IntStream.range(0, 100).mapToObj(i -> new RowN(new Object[] { i, "{\"id\": " + i + ", \"name\": \"Arthur\"}" })).collect(Collectors.toList()), SENTINEL, true);
    TestingRowConsumer consumer = new TestingRowConsumer();
    consumer.accept(writerProjector.apply(rowsIterator), null);
    Bucket objects = consumer.getBucket();
    assertThat(objects, contains(isRow(100L)));
    execute("refresh table bulk_import");
    execute("select count(*) from bulk_import");
    assertThat(response.rowCount(), is(1L));
    assertThat(response.rows()[0][0], is(100L));
}
Also used : TransportCreatePartitionsAction(org.elasticsearch.action.admin.indices.create.TransportCreatePartitionsAction) ClusterState(org.elasticsearch.cluster.ClusterState) ClusterSettings(org.elasticsearch.common.settings.ClusterSettings) NodeContext(io.crate.metadata.NodeContext) Reference(io.crate.metadata.Reference) ThreadPool(org.elasticsearch.threadpool.ThreadPool) BatchIterator(io.crate.data.BatchIterator) InMemoryBatchIterator(io.crate.data.InMemoryBatchIterator) CollectExpression(io.crate.execution.engine.collect.CollectExpression) InputCollectExpression(io.crate.execution.engine.collect.InputCollectExpression) ReferenceIdent(io.crate.metadata.ReferenceIdent) RowN(io.crate.data.RowN) InputCollectExpression(io.crate.execution.engine.collect.InputCollectExpression) Bucket(io.crate.data.Bucket) InputColumn(io.crate.expression.symbol.InputColumn) NodeLimits(io.crate.execution.jobs.NodeLimits) RelationName(io.crate.metadata.RelationName) NoopCircuitBreaker(org.elasticsearch.common.breaker.NoopCircuitBreaker) Settings(org.elasticsearch.common.settings.Settings) ClusterSettings(org.elasticsearch.common.settings.ClusterSettings) TestingRowConsumer(io.crate.testing.TestingRowConsumer) Test(org.junit.Test)

Example 23 with NodeContext

use of io.crate.metadata.NodeContext in project crate by crate.

the class TruncFunction method createTruncWithMode.

private static Scalar<Number, Number> createTruncWithMode(Signature signature, Signature boundSignature) {
    return new Scalar<>() {

        @Override
        public Signature signature() {
            return signature;
        }

        @Override
        public Signature boundSignature() {
            return boundSignature;
        }

        @Override
        public Number evaluate(TransactionContext txnCtx, NodeContext nodeCtx, Input<Number>... args) {
            Number n = args[0].value();
            Number nd = args[1].value();
            if (null == n || null == nd) {
                return null;
            }
            double val = n.doubleValue();
            int numDecimals = nd.intValue();
            RoundingMode mode = val >= 0 ? RoundingMode.FLOOR : RoundingMode.CEILING;
            return BigDecimal.valueOf(val).setScale(numDecimals, mode).doubleValue();
        }
    };
}
Also used : Input(io.crate.data.Input) NodeContext(io.crate.metadata.NodeContext) TransactionContext(io.crate.metadata.TransactionContext) RoundingMode(java.math.RoundingMode) UnaryScalar(io.crate.expression.scalar.UnaryScalar) Scalar(io.crate.metadata.Scalar)

Example 24 with NodeContext

use of io.crate.metadata.NodeContext in project crate by crate.

the class CreateAnalyzerPlan method createRequest.

@VisibleForTesting
public static ClusterUpdateSettingsRequest createRequest(AnalyzedCreateAnalyzer createAnalyzer, CoordinatorTxnCtx txnCtx, NodeContext nodeCtx, Row parameters, SubQueryResults subQueryResults, FulltextAnalyzerResolver ftResolver) {
    Function<? super Symbol, Object> eval = x -> SymbolEvaluator.evaluate(txnCtx, nodeCtx, x, parameters, subQueryResults);
    var analyzerIdent = createAnalyzer.ident();
    var tokenizer = bindTokenizer(createAnalyzer.tokenizer(), analyzerIdent, eval, ftResolver);
    var tokenFilters = bindTokenFilters(createAnalyzer.tokenFilters(), analyzerIdent, eval, ftResolver);
    var charFilters = bindCharFilters(createAnalyzer.charFilters(), analyzerIdent, eval, ftResolver);
    var genericAnalyzerSettings = bindGenericAnalyzerProperties(createAnalyzer.genericAnalyzerProperties(), analyzerIdent, eval);
    var analyzerSettings = buildAnalyzerSettings(analyzerIdent, createAnalyzer.extendedAnalyzerName(), tokenizer, tokenFilters, charFilters, genericAnalyzerSettings, ftResolver);
    Settings.Builder encodedSettingsBuilder = Settings.builder();
    encodedSettingsBuilder.put(ANALYZER.buildSettingName(analyzerIdent), encodeSettings(analyzerSettings).utf8ToString());
    if (tokenizer != null && !tokenizer.v2().isEmpty()) {
        encodedSettingsBuilder.put(TOKENIZER.buildSettingName(tokenizer.v1()), encodeSettings(tokenizer.v2()).utf8ToString());
    }
    for (Map.Entry<String, Settings> tokenFilter : tokenFilters.entrySet()) {
        if (!tokenFilter.getValue().isEmpty()) {
            encodedSettingsBuilder.put(TOKEN_FILTER.buildSettingName(tokenFilter.getKey()), encodeSettings(tokenFilter.getValue()).utf8ToString());
        }
    }
    for (Map.Entry<String, Settings> charFilter : charFilters.entrySet()) {
        if (!charFilter.getValue().isEmpty()) {
            encodedSettingsBuilder.put(CHAR_FILTER.buildSettingName(charFilter.getKey()), encodeSettings(charFilter.getValue()).utf8ToString());
        }
    }
    return new ClusterUpdateSettingsRequest().persistentSettings(encodedSettingsBuilder.build());
}
Also used : TOKEN_FILTER(io.crate.metadata.FulltextAnalyzerResolver.CustomType.TOKEN_FILTER) Tuple(io.crate.common.collections.Tuple) HashMap(java.util.HashMap) GenericProperties(io.crate.sql.tree.GenericProperties) Function(java.util.function.Function) GenericProperty(io.crate.sql.tree.GenericProperty) DependencyCarrier(io.crate.planner.DependencyCarrier) SymbolEvaluator(io.crate.analyze.SymbolEvaluator) ClusterUpdateSettingsRequest(org.elasticsearch.action.admin.cluster.settings.ClusterUpdateSettingsRequest) Settings(org.elasticsearch.common.settings.Settings) Locale(java.util.Locale) Map(java.util.Map) GenericPropertiesConverter(io.crate.analyze.GenericPropertiesConverter) CHAR_FILTER(io.crate.metadata.FulltextAnalyzerResolver.CustomType.CHAR_FILTER) OneRowActionListener(io.crate.execution.support.OneRowActionListener) FulltextAnalyzerResolver(io.crate.metadata.FulltextAnalyzerResolver) Nullable(javax.annotation.Nullable) NodeContext(io.crate.metadata.NodeContext) AnalyzedCreateAnalyzer(io.crate.analyze.AnalyzedCreateAnalyzer) FulltextAnalyzerResolver.encodeSettings(io.crate.metadata.FulltextAnalyzerResolver.encodeSettings) TOKENIZER(io.crate.metadata.FulltextAnalyzerResolver.CustomType.TOKENIZER) RowConsumer(io.crate.data.RowConsumer) List(java.util.List) Row(io.crate.data.Row) Literal(io.crate.expression.symbol.Literal) Symbol(io.crate.expression.symbol.Symbol) ANALYZER(io.crate.metadata.FulltextAnalyzerResolver.CustomType.ANALYZER) PlannerContext(io.crate.planner.PlannerContext) Plan(io.crate.planner.Plan) SubQueryResults(io.crate.planner.operators.SubQueryResults) VisibleForTesting(io.crate.common.annotations.VisibleForTesting) Row1(io.crate.data.Row1) CoordinatorTxnCtx(io.crate.metadata.CoordinatorTxnCtx) HashMap(java.util.HashMap) Map(java.util.Map) Settings(org.elasticsearch.common.settings.Settings) FulltextAnalyzerResolver.encodeSettings(io.crate.metadata.FulltextAnalyzerResolver.encodeSettings) ClusterUpdateSettingsRequest(org.elasticsearch.action.admin.cluster.settings.ClusterUpdateSettingsRequest) VisibleForTesting(io.crate.common.annotations.VisibleForTesting)

Example 25 with NodeContext

use of io.crate.metadata.NodeContext in project crate by crate.

the class CreateSnapshotPlan method createRequest.

@VisibleForTesting
public static CreateSnapshotRequest createRequest(AnalyzedCreateSnapshot createSnapshot, CoordinatorTxnCtx txnCtx, NodeContext nodeCtx, Row parameters, SubQueryResults subQueryResults, Schemas schemas) {
    Function<? super Symbol, Object> eval = x -> SymbolEvaluator.evaluate(txnCtx, nodeCtx, x, parameters, subQueryResults);
    Settings settings = GenericPropertiesConverter.genericPropertiesToSettings(createSnapshot.properties().map(eval), SnapshotSettings.SETTINGS);
    boolean ignoreUnavailable = IGNORE_UNAVAILABLE.get(settings);
    final HashSet<String> snapshotIndices;
    final HashSet<String> templates = new HashSet<>();
    if (createSnapshot.tables().isEmpty()) {
        for (SchemaInfo schemaInfo : schemas) {
            for (TableInfo tableInfo : schemaInfo.getTables()) {
                // only check for user generated tables
                if (tableInfo instanceof DocTableInfo) {
                    Operation.blockedRaiseException(tableInfo, Operation.READ);
                }
            }
        }
        snapshotIndices = new HashSet<>(AnalyzedCreateSnapshot.ALL_INDICES);
    } else {
        snapshotIndices = new HashSet<>(createSnapshot.tables().size());
        for (Table<Symbol> table : createSnapshot.tables()) {
            DocTableInfo docTableInfo;
            try {
                docTableInfo = (DocTableInfo) schemas.resolveTableInfo(table.getName(), Operation.CREATE_SNAPSHOT, txnCtx.sessionContext().sessionUser(), txnCtx.sessionContext().searchPath());
            } catch (Exception e) {
                if (ignoreUnavailable && e instanceof ResourceUnknownException) {
                    LOGGER.info("Ignore unknown relation '{}' for the '{}' snapshot'", table.getName(), createSnapshot.snapshot());
                    continue;
                } else {
                    throw e;
                }
            }
            if (docTableInfo.isPartitioned()) {
                templates.add(PartitionName.templateName(docTableInfo.ident().schema(), docTableInfo.ident().name()));
            }
            if (table.partitionProperties().isEmpty()) {
                snapshotIndices.addAll(Arrays.asList(docTableInfo.concreteIndices()));
            } else {
                var partitionName = toPartitionName(docTableInfo, Lists2.map(table.partitionProperties(), x -> x.map(eval)));
                if (!docTableInfo.partitions().contains(partitionName)) {
                    if (!ignoreUnavailable) {
                        throw new PartitionUnknownException(partitionName);
                    } else {
                        LOGGER.info("ignoring unknown partition of table '{}' with ident '{}'", partitionName.relationName(), partitionName.ident());
                    }
                } else {
                    snapshotIndices.add(partitionName.asIndexName());
                }
            }
        }
    }
    return new CreateSnapshotRequest(createSnapshot.snapshot().getRepository(), createSnapshot.snapshot().getSnapshotId().getName()).includeGlobalState(createSnapshot.tables().isEmpty()).waitForCompletion(WAIT_FOR_COMPLETION.get(settings)).indices(snapshotIndices.toArray(new String[0])).indicesOptions(IndicesOptions.fromOptions(ignoreUnavailable, true, true, false, IndicesOptions.lenientExpandOpen())).templates(templates.stream().toList()).settings(settings);
}
Also used : Arrays(java.util.Arrays) Operation(io.crate.metadata.table.Operation) SnapshotInfo(org.elasticsearch.snapshots.SnapshotInfo) Function(java.util.function.Function) PartitionName(io.crate.metadata.PartitionName) DependencyCarrier(io.crate.planner.DependencyCarrier) HashSet(java.util.HashSet) WAIT_FOR_COMPLETION(io.crate.analyze.SnapshotSettings.WAIT_FOR_COMPLETION) SymbolEvaluator(io.crate.analyze.SymbolEvaluator) Settings(org.elasticsearch.common.settings.Settings) AnalyzedCreateSnapshot(io.crate.analyze.AnalyzedCreateSnapshot) IndicesOptions(org.elasticsearch.action.support.IndicesOptions) GenericPropertiesConverter(io.crate.analyze.GenericPropertiesConverter) SnapshotState(org.elasticsearch.snapshots.SnapshotState) OneRowActionListener(io.crate.execution.support.OneRowActionListener) PartitionUnknownException(io.crate.exceptions.PartitionUnknownException) PartitionPropertiesAnalyzer.toPartitionName(io.crate.analyze.PartitionPropertiesAnalyzer.toPartitionName) SchemaInfo(io.crate.metadata.table.SchemaInfo) DocTableInfo(io.crate.metadata.doc.DocTableInfo) TableInfo(io.crate.metadata.table.TableInfo) NodeContext(io.crate.metadata.NodeContext) CreateSnapshotException(io.crate.exceptions.CreateSnapshotException) Table(io.crate.sql.tree.Table) ResourceUnknownException(io.crate.exceptions.ResourceUnknownException) CreateSnapshotRequest(org.elasticsearch.action.admin.cluster.snapshots.create.CreateSnapshotRequest) Lists2(io.crate.common.collections.Lists2) SnapshotSettings(io.crate.analyze.SnapshotSettings) RowConsumer(io.crate.data.RowConsumer) Logger(org.apache.logging.log4j.Logger) Row(io.crate.data.Row) Symbol(io.crate.expression.symbol.Symbol) PlannerContext(io.crate.planner.PlannerContext) IGNORE_UNAVAILABLE(io.crate.analyze.SnapshotSettings.IGNORE_UNAVAILABLE) Plan(io.crate.planner.Plan) SubQueryResults(io.crate.planner.operators.SubQueryResults) Schemas(io.crate.metadata.Schemas) VisibleForTesting(io.crate.common.annotations.VisibleForTesting) LogManager(org.apache.logging.log4j.LogManager) Row1(io.crate.data.Row1) CoordinatorTxnCtx(io.crate.metadata.CoordinatorTxnCtx) DocTableInfo(io.crate.metadata.doc.DocTableInfo) PartitionUnknownException(io.crate.exceptions.PartitionUnknownException) Symbol(io.crate.expression.symbol.Symbol) ResourceUnknownException(io.crate.exceptions.ResourceUnknownException) PartitionUnknownException(io.crate.exceptions.PartitionUnknownException) CreateSnapshotException(io.crate.exceptions.CreateSnapshotException) ResourceUnknownException(io.crate.exceptions.ResourceUnknownException) CreateSnapshotRequest(org.elasticsearch.action.admin.cluster.snapshots.create.CreateSnapshotRequest) DocTableInfo(io.crate.metadata.doc.DocTableInfo) TableInfo(io.crate.metadata.table.TableInfo) Settings(org.elasticsearch.common.settings.Settings) SnapshotSettings(io.crate.analyze.SnapshotSettings) HashSet(java.util.HashSet) SchemaInfo(io.crate.metadata.table.SchemaInfo) VisibleForTesting(io.crate.common.annotations.VisibleForTesting)

Aggregations

NodeContext (io.crate.metadata.NodeContext)29 Symbol (io.crate.expression.symbol.Symbol)16 Row (io.crate.data.Row)14 CoordinatorTxnCtx (io.crate.metadata.CoordinatorTxnCtx)12 List (java.util.List)12 SymbolEvaluator (io.crate.analyze.SymbolEvaluator)10 VisibleForTesting (io.crate.common.annotations.VisibleForTesting)10 Plan (io.crate.planner.Plan)10 PlannerContext (io.crate.planner.PlannerContext)10 SubQueryResults (io.crate.planner.operators.SubQueryResults)10 Function (java.util.function.Function)10 Nullable (javax.annotation.Nullable)10 RowConsumer (io.crate.data.RowConsumer)9 TransactionContext (io.crate.metadata.TransactionContext)9 DependencyCarrier (io.crate.planner.DependencyCarrier)9 ArrayList (java.util.ArrayList)9 Settings (org.elasticsearch.common.settings.Settings)9 Row1 (io.crate.data.Row1)8 OneRowActionListener (io.crate.execution.support.OneRowActionListener)8 Map (java.util.Map)7