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