use of io.crate.metadata.NodeContext in project crate by crate.
the class WindowProjector method createComputeEndFrameBoundary.
static ComputeFrameBoundary<Object[]> createComputeEndFrameBoundary(int numCellsInSourceRow, TransactionContext txnCtx, NodeContext nodeCtx, WindowDefinition windowDefinition, Comparator<Object[]> cmpOrderBy) {
var frameDefinition = windowDefinition.windowFrameDefinition();
var frameBoundEnd = frameDefinition.end();
var framingMode = frameDefinition.mode();
DataType offsetType = frameBoundEnd.value().valueType();
Object offsetValue = evaluateWithoutParams(txnCtx, nodeCtx, frameBoundEnd.value());
Object[] endProbeValues = new Object[numCellsInSourceRow];
BiFunction<Object[], Object[], Object[]> updateProbeValues;
if (offsetValue != null && framingMode == WindowFrame.Mode.RANGE) {
updateProbeValues = createUpdateProbeValueFunction(windowDefinition, ArithmeticOperatorsFactory::getAddFunction, offsetValue, offsetType);
} else {
updateProbeValues = (currentRow, x) -> x;
}
return (partitionStart, partitionEnd, currentIndex, sortedRows) -> frameBoundEnd.type().getEnd(framingMode, partitionStart, partitionEnd, currentIndex, offsetValue, updateProbeValues.apply(sortedRows.get(currentIndex), endProbeValues), cmpOrderBy, sortedRows);
}
use of io.crate.metadata.NodeContext in project crate by crate.
the class WindowProjector method createComputeStartFrameBoundary.
static ComputeFrameBoundary<Object[]> createComputeStartFrameBoundary(int numCellsInSourceRow, TransactionContext txnCtx, NodeContext nodeCtx, WindowDefinition windowDefinition, @Nullable Comparator<Object[]> cmpOrderBy) {
var frameDefinition = windowDefinition.windowFrameDefinition();
var frameBoundStart = frameDefinition.start();
var framingMode = frameDefinition.mode();
DataType offsetType = frameBoundStart.value().valueType();
Object offsetValue = evaluateWithoutParams(txnCtx, nodeCtx, frameBoundStart.value());
Object[] startProbeValues = new Object[numCellsInSourceRow];
BiFunction<Object[], Object[], Object[]> updateStartProbeValue;
if (offsetValue != null && framingMode == WindowFrame.Mode.RANGE) {
updateStartProbeValue = createUpdateProbeValueFunction(windowDefinition, ArithmeticOperatorsFactory::getSubtractFunction, offsetValue, offsetType);
} else {
updateStartProbeValue = (currentRow, x) -> x;
}
return (partitionStart, partitionEnd, currentIndex, sortedRows) -> frameBoundStart.type().getStart(framingMode, partitionStart, partitionEnd, currentIndex, offsetValue, updateStartProbeValue.apply(sortedRows.get(currentIndex), startProbeValues), cmpOrderBy, sortedRows);
}
use of io.crate.metadata.NodeContext in project crate by crate.
the class UserDefinedFunctionService method validateFunctionIsNotInUseByGeneratedColumn.
void validateFunctionIsNotInUseByGeneratedColumn(String schema, String functionName, UserDefinedFunctionsMetadata functionsMetadata, ClusterState currentState) {
// The iteration of schemas/tables must happen on the node context WITHOUT the UDF already removed.
// Otherwise the lazy table factories will already fail while evaluating generated functionsMetadata.
// To avoid that, a copy of the node context with the removed UDF function is used on concrete expression evaluation.
var nodeCtxWithRemovedFunction = new NodeContext(nodeCtx.functions().copyOf());
updateImplementations(schema, functionsMetadata.functionsMetadata().stream(), nodeCtxWithRemovedFunction);
var metadata = currentState.metadata();
var indices = Stream.of(metadata.getConcreteAllIndices()).filter(NO_BLOB_NOR_DANGLING).map(IndexParts::new).filter(indexParts -> !indexParts.isPartitioned()).collect(Collectors.toList());
var templates = metadata.getTemplates().keysIt();
while (templates.hasNext()) {
var indexParts = new IndexParts(templates.next());
if (indexParts.isPartitioned()) {
indices.add(indexParts);
}
}
var indexNameExpressionResolver = new IndexNameExpressionResolver();
for (var indexParts : indices) {
var tableInfo = new DocTableInfoBuilder(nodeCtx, indexParts.toRelationName(), currentState, indexNameExpressionResolver).build();
TableReferenceResolver tableReferenceResolver = new TableReferenceResolver(tableInfo.columns(), tableInfo.ident());
CoordinatorTxnCtx coordinatorTxnCtx = CoordinatorTxnCtx.systemTransactionContext();
ExpressionAnalyzer exprAnalyzer = new ExpressionAnalyzer(coordinatorTxnCtx, nodeCtxWithRemovedFunction, ParamTypeHints.EMPTY, tableReferenceResolver, null);
for (var ref : tableInfo.columns()) {
if (ref instanceof GeneratedReference) {
var genRef = (GeneratedReference) ref;
Expression expression = SqlParser.createExpression(genRef.formattedGeneratedExpression());
try {
exprAnalyzer.convert(expression, new ExpressionAnalysisContext(coordinatorTxnCtx.sessionContext()));
} catch (UnsupportedOperationException e) {
throw new IllegalArgumentException("Cannot drop function '" + functionName + "', it is still in use by '" + tableInfo + "." + genRef + "'");
}
}
}
}
}
use of io.crate.metadata.NodeContext in project crate by crate.
the class RestoreSnapshotPlan method bind.
@VisibleForTesting
public static BoundRestoreSnapshot bind(AnalyzedRestoreSnapshot restoreSnapshot, 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(restoreSnapshot.properties().map(eval), SnapshotSettings.SETTINGS);
HashSet<BoundRestoreSnapshot.RestoreTableInfo> restoreTables = new HashSet<>(restoreSnapshot.tables().size());
for (Table<Symbol> table : restoreSnapshot.tables()) {
var relationName = RelationName.of(table.getName(), txnCtx.sessionContext().searchPath().currentSchema());
try {
DocTableInfo docTableInfo = schemas.getTableInfo(relationName, Operation.RESTORE_SNAPSHOT);
if (table.partitionProperties().isEmpty()) {
throw new RelationAlreadyExists(relationName);
}
var partitionName = toPartitionName(docTableInfo, Lists2.map(table.partitionProperties(), x -> x.map(eval)));
if (docTableInfo.partitions().contains(partitionName)) {
throw new PartitionAlreadyExistsException(partitionName);
}
restoreTables.add(new BoundRestoreSnapshot.RestoreTableInfo(relationName, partitionName));
} catch (RelationUnknown | SchemaUnknownException e) {
if (table.partitionProperties().isEmpty()) {
restoreTables.add(new BoundRestoreSnapshot.RestoreTableInfo(relationName, null));
} else {
var partitionName = toPartitionName(relationName, Lists2.map(table.partitionProperties(), x -> x.map(eval)));
restoreTables.add(new BoundRestoreSnapshot.RestoreTableInfo(relationName, partitionName));
}
}
}
return new BoundRestoreSnapshot(restoreSnapshot.repository(), restoreSnapshot.snapshot(), restoreTables, restoreSnapshot.includeTables(), restoreSnapshot.includeCustomMetadata(), restoreSnapshot.customMetadataTypes(), restoreSnapshot.includeGlobalSettings(), restoreSnapshot.globalSettings(), settings);
}
use of io.crate.metadata.NodeContext in project crate by crate.
the class GroupByOptimizedIteratorTest method prepare.
@Before
public void prepare() throws Exception {
NodeContext nodeCtx = createNodeContext();
IndexWriter iw = new IndexWriter(new ByteBuffersDirectory(), new IndexWriterConfig(new StandardAnalyzer()));
columnName = "x";
expectedResult = new ArrayList<>(20);
for (long i = 0; i < 20; i++) {
Document doc = new Document();
String val = "val_" + i;
doc.add(new SortedSetDocValuesField(columnName, new BytesRef(val)));
iw.addDocument(doc);
expectedResult.add(new Object[] { val, 1L });
}
iw.commit();
indexSearcher = new IndexSearcher(DirectoryReader.open(iw));
inExpr = new InputCollectExpression(0);
CountAggregation aggregation = (CountAggregation) nodeCtx.functions().getQualified(CountAggregation.COUNT_STAR_SIGNATURE, Collections.emptyList(), CountAggregation.COUNT_STAR_SIGNATURE.getReturnType().createType());
aggregationContexts = List.of(new AggregationContext(aggregation, () -> true, List.of()));
}
Aggregations