use of io.crate.metadata.CoordinatorTxnCtx in project crate by crate.
the class ExpressionAnalyzerTest method testAnalyzeArraySliceFunctionCall.
@Test
public void testAnalyzeArraySliceFunctionCall() {
ReferenceIdent arrayRefIdent = new ReferenceIdent(new RelationName("doc", "tarr"), "xs");
Reference arrayRef = new Reference(arrayRefIdent, RowGranularity.DOC, DataTypes.INTEGER_ARRAY, ColumnPolicy.DYNAMIC, Reference.IndexType.PLAIN, true, true, 1, null);
CoordinatorTxnCtx txnCtx = CoordinatorTxnCtx.systemTransactionContext();
ExpressionAnalysisContext localContext = new ExpressionAnalysisContext(txnCtx.sessionContext());
Symbol function = ExpressionAnalyzer.allocateFunction(ArraySliceFunction.NAME, List.of(arrayRef, Literal.of(1), Literal.of(3)), null, localContext, txnCtx, expressions.nodeCtx);
var result = executor.asSymbol("tarr.xs[1:3]");
assertThat(result, is(equalTo(function)));
}
use of io.crate.metadata.CoordinatorTxnCtx in project crate by crate.
the class ExpressionAnalyzerTest method testInSelfJoinCaseFunctionsThatLookTheSameMustNotReuseFunctionAllocation.
@Test
public void testInSelfJoinCaseFunctionsThatLookTheSameMustNotReuseFunctionAllocation() throws Exception {
TableInfo t1 = executor.resolveTableInfo("t1");
TableRelation relation = new TableRelation(t1);
RelationName a1 = new RelationName(null, "a1");
RelationName a2 = new RelationName(null, "a2");
Map<RelationName, AnalyzedRelation> sources = Map.of(a1, new AliasedAnalyzedRelation(relation, a1), a2, new AliasedAnalyzedRelation(relation, a2));
SessionContext sessionContext = SessionContext.systemSessionContext();
CoordinatorTxnCtx coordinatorTxnCtx = new CoordinatorTxnCtx(sessionContext);
ExpressionAnalyzer expressionAnalyzer = new ExpressionAnalyzer(coordinatorTxnCtx, expressions.nodeCtx, paramTypeHints, new FullQualifiedNameFieldProvider(sources, ParentRelations.NO_PARENTS, sessionContext.searchPath().currentSchema()), null);
Function andFunction = (Function) expressionAnalyzer.convert(SqlParser.createExpression("not a1.x = 1 and not a2.x = 1"), context);
ScopedSymbol t1Id = ((ScopedSymbol) ((Function) ((Function) andFunction.arguments().get(0)).arguments().get(0)).arguments().get(0));
ScopedSymbol t2Id = ((ScopedSymbol) ((Function) ((Function) andFunction.arguments().get(1)).arguments().get(0)).arguments().get(0));
assertThat(t1Id.relation(), is(not(t2Id.relation())));
}
use of io.crate.metadata.CoordinatorTxnCtx 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.CoordinatorTxnCtx 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.CoordinatorTxnCtx in project crate by crate.
the class LogicalPlanner method plan.
public LogicalPlan plan(AnalyzedRelation relation, PlannerContext plannerContext, SubqueryPlanner subqueryPlanner, boolean avoidTopLevelFetch) {
CoordinatorTxnCtx coordinatorTxnCtx = plannerContext.transactionContext();
var planBuilder = new PlanBuilder(subqueryPlanner, coordinatorTxnCtx, tableStats, plannerContext.params());
LogicalPlan logicalPlan = relation.accept(planBuilder, relation.outputs());
LogicalPlan optimizedPlan = optimizer.optimize(logicalPlan, tableStats, coordinatorTxnCtx);
assert logicalPlan.outputs().equals(optimizedPlan.outputs()) : "Optimized plan must have the same outputs as original plan";
LogicalPlan prunedPlan = optimizedPlan.pruneOutputsExcept(tableStats, relation.outputs());
assert logicalPlan.outputs().equals(optimizedPlan.outputs()) : "Pruned plan must have the same outputs as original plan";
LogicalPlan fetchOptimized = fetchOptimizer.optimize(prunedPlan, tableStats, coordinatorTxnCtx);
if (fetchOptimized != prunedPlan || avoidTopLevelFetch) {
return fetchOptimized;
}
assert logicalPlan.outputs().equals(fetchOptimized.outputs()) : "Fetch optimized plan must have the same outputs as original plan";
// even if there is no operator that reduces the number of records
return RewriteToQueryThenFetch.tryRewrite(relation, fetchOptimized, tableStats);
}
Aggregations