Search in sources :

Example 1 with RelationAnalysisContext

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);
}
Also used : RelationAnalysisContext(io.crate.analyze.relations.RelationAnalysisContext) ExpressionAnalysisContext(io.crate.analyze.expressions.ExpressionAnalysisContext) EvaluatingNormalizer(io.crate.expression.eval.EvaluatingNormalizer) Symbol(io.crate.expression.symbol.Symbol) ExpressionAnalyzer(io.crate.analyze.expressions.ExpressionAnalyzer) AnalyzedRelation(io.crate.analyze.relations.AnalyzedRelation) StatementAnalysisContext(io.crate.analyze.relations.StatementAnalysisContext) DocTableRelation(io.crate.analyze.relations.DocTableRelation) SubqueryAnalyzer(io.crate.analyze.expressions.SubqueryAnalyzer) FullQualifiedNameFieldProvider(io.crate.analyze.relations.FullQualifiedNameFieldProvider)

Example 2 with RelationAnalysisContext

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);
}
Also used : RelationAnalysisContext(io.crate.analyze.relations.RelationAnalysisContext) SelectAnalysis(io.crate.analyze.relations.select.SelectAnalysis) ExpressionAnalysisContext(io.crate.analyze.expressions.ExpressionAnalysisContext) EvaluatingNormalizer(io.crate.expression.eval.EvaluatingNormalizer) Reference(io.crate.metadata.Reference) Symbol(io.crate.expression.symbol.Symbol) ExpressionAnalyzer(io.crate.analyze.expressions.ExpressionAnalyzer) AnalyzedRelation(io.crate.analyze.relations.AnalyzedRelation) StatementAnalysisContext(io.crate.analyze.relations.StatementAnalysisContext) SubqueryAnalyzer(io.crate.analyze.expressions.SubqueryAnalyzer) AbstractTableRelation(io.crate.analyze.relations.AbstractTableRelation) FullQualifiedNameFieldProvider(io.crate.analyze.relations.FullQualifiedNameFieldProvider)

Aggregations

ExpressionAnalysisContext (io.crate.analyze.expressions.ExpressionAnalysisContext)2 ExpressionAnalyzer (io.crate.analyze.expressions.ExpressionAnalyzer)2 SubqueryAnalyzer (io.crate.analyze.expressions.SubqueryAnalyzer)2 AnalyzedRelation (io.crate.analyze.relations.AnalyzedRelation)2 FullQualifiedNameFieldProvider (io.crate.analyze.relations.FullQualifiedNameFieldProvider)2 RelationAnalysisContext (io.crate.analyze.relations.RelationAnalysisContext)2 StatementAnalysisContext (io.crate.analyze.relations.StatementAnalysisContext)2 EvaluatingNormalizer (io.crate.expression.eval.EvaluatingNormalizer)2 Symbol (io.crate.expression.symbol.Symbol)2 AbstractTableRelation (io.crate.analyze.relations.AbstractTableRelation)1 DocTableRelation (io.crate.analyze.relations.DocTableRelation)1 SelectAnalysis (io.crate.analyze.relations.select.SelectAnalysis)1 Reference (io.crate.metadata.Reference)1