use of io.crate.analyze.relations.RelationAnalysisContext in project crate by crate.
the class DeleteAnalyzer method analyze.
public AnalyzedDeleteStatement analyze(Delete delete, ParamTypeHints typeHints, CoordinatorTxnCtx txnContext) {
StatementAnalysisContext stmtCtx = new StatementAnalysisContext(typeHints, Operation.DELETE, txnContext);
final RelationAnalysisContext relationCtx = stmtCtx.startRelation();
AnalyzedRelation relation = relationAnalyzer.analyze(delete.getRelation(), stmtCtx);
stmtCtx.endRelation();
MaybeAliasedStatement maybeAliasedStatement = MaybeAliasedStatement.analyze(relation);
relation = maybeAliasedStatement.nonAliasedRelation();
if (!(relation instanceof DocTableRelation)) {
throw new UnsupportedOperationException("Cannot delete from relations other than base tables");
}
DocTableRelation table = (DocTableRelation) relation;
EvaluatingNormalizer normalizer = new EvaluatingNormalizer(nodeCtx, RowGranularity.CLUSTER, null, table);
ExpressionAnalyzer expressionAnalyzer = new ExpressionAnalyzer(txnContext, nodeCtx, typeHints, new FullQualifiedNameFieldProvider(relationCtx.sources(), relationCtx.parentSources(), txnContext.sessionContext().searchPath().currentSchema()), new SubqueryAnalyzer(relationAnalyzer, new StatementAnalysisContext(typeHints, Operation.READ, txnContext)));
Symbol query = Objects.requireNonNullElse(expressionAnalyzer.generateQuerySymbol(delete.getWhere(), new ExpressionAnalysisContext(txnContext.sessionContext())), Literal.BOOLEAN_TRUE);
query = maybeAliasedStatement.maybeMapFields(query);
Symbol normalizedQuery = normalizer.normalize(query, txnContext);
return new AnalyzedDeleteStatement(table, normalizedQuery);
}
use of io.crate.analyze.relations.RelationAnalysisContext in project crate by crate.
the class UpdateAnalyzer method analyze.
public AnalyzedUpdateStatement analyze(Update update, ParamTypeHints typeHints, CoordinatorTxnCtx txnCtx) {
/* UPDATE t1 SET col1 = ?, col2 = ? WHERE id = ?`
* ^^^^^^^^^^^^^^^^^^ ^^^^^^
* assignments whereClause
*
* col1 = ?
* | |
* | source
* columnName/target
*/
StatementAnalysisContext stmtCtx = new StatementAnalysisContext(typeHints, Operation.UPDATE, txnCtx);
final RelationAnalysisContext relCtx = stmtCtx.startRelation();
AnalyzedRelation relation = relationAnalyzer.analyze(update.relation(), stmtCtx);
stmtCtx.endRelation();
MaybeAliasedStatement maybeAliasedStatement = MaybeAliasedStatement.analyze(relation);
relation = maybeAliasedStatement.nonAliasedRelation();
if (!(relation instanceof AbstractTableRelation)) {
throw new UnsupportedOperationException("UPDATE is only supported on base-tables");
}
AbstractTableRelation<?> table = (AbstractTableRelation<?>) relation;
EvaluatingNormalizer normalizer = new EvaluatingNormalizer(nodeCtx, RowGranularity.CLUSTER, null, table);
SubqueryAnalyzer subqueryAnalyzer = new SubqueryAnalyzer(relationAnalyzer, new StatementAnalysisContext(typeHints, Operation.READ, txnCtx));
ExpressionAnalyzer sourceExprAnalyzer = new ExpressionAnalyzer(txnCtx, nodeCtx, typeHints, new FullQualifiedNameFieldProvider(relCtx.sources(), relCtx.parentSources(), txnCtx.sessionContext().searchPath().currentSchema()), subqueryAnalyzer);
ExpressionAnalysisContext exprCtx = new ExpressionAnalysisContext(txnCtx.sessionContext());
Map<Reference, Symbol> assignmentByTargetCol = getAssignments(update.assignments(), typeHints, txnCtx, table, normalizer, subqueryAnalyzer, sourceExprAnalyzer, exprCtx);
Symbol query = Objects.requireNonNullElse(sourceExprAnalyzer.generateQuerySymbol(update.whereClause(), exprCtx), Literal.BOOLEAN_TRUE);
query = maybeAliasedStatement.maybeMapFields(query);
Symbol normalizedQuery = normalizer.normalize(query, txnCtx);
SelectAnalysis selectAnalysis = SelectAnalyzer.analyzeSelectItems(update.returningClause(), relCtx.sources(), sourceExprAnalyzer, exprCtx);
List<Symbol> outputSymbol = Lists2.map(selectAnalysis.outputSymbols(), x -> normalizer.normalize(x, txnCtx));
return new AnalyzedUpdateStatement(table, assignmentByTargetCol, normalizedQuery, outputSymbol.isEmpty() ? null : outputSymbol);
}
Aggregations