Search in sources :

Example 1 with WhereClauseAnalyzer

use of io.crate.analyze.where.WhereClauseAnalyzer in project crate by crate.

the class DeleteAnalyzer method analyze.

public AnalyzedStatement analyze(Delete node, Analysis analysis) {
    int numNested = 1;
    Function<ParameterExpression, Symbol> convertParamFunction = analysis.parameterContext();
    StatementAnalysisContext statementAnalysisContext = new StatementAnalysisContext(analysis.sessionContext(), convertParamFunction, Operation.DELETE, analysis.transactionContext());
    RelationAnalysisContext relationAnalysisContext = statementAnalysisContext.startRelation();
    AnalyzedRelation analyzedRelation = relationAnalyzer.analyze(node.getRelation(), statementAnalysisContext);
    assert analyzedRelation instanceof DocTableRelation : "analyzedRelation must be DocTableRelation";
    DocTableRelation docTableRelation = (DocTableRelation) analyzedRelation;
    EvaluatingNormalizer normalizer = new EvaluatingNormalizer(functions, RowGranularity.CLUSTER, ReplaceMode.MUTATE, null, docTableRelation);
    DeleteAnalyzedStatement deleteAnalyzedStatement = new DeleteAnalyzedStatement(docTableRelation);
    ExpressionAnalyzer expressionAnalyzer = new ExpressionAnalyzer(functions, analysis.sessionContext(), convertParamFunction, new FullQualifedNameFieldProvider(relationAnalysisContext.sources()), null);
    ExpressionAnalysisContext expressionAnalysisContext = new ExpressionAnalysisContext();
    WhereClauseAnalyzer whereClauseAnalyzer = new WhereClauseAnalyzer(functions, deleteAnalyzedStatement.analyzedRelation());
    if (analysis.parameterContext().hasBulkParams()) {
        numNested = analysis.parameterContext().numBulkParams();
    }
    TransactionContext transactionContext = analysis.transactionContext();
    for (int i = 0; i < numNested; i++) {
        analysis.parameterContext().setBulkIdx(i);
        Symbol query = expressionAnalyzer.generateQuerySymbol(node.getWhere(), expressionAnalysisContext);
        WhereClause whereClause = new WhereClause(normalizer.normalize(query, transactionContext));
        whereClause = validate(whereClauseAnalyzer.analyze(whereClause, transactionContext));
        deleteAnalyzedStatement.whereClauses.add(whereClause);
    }
    statementAnalysisContext.endRelation();
    return deleteAnalyzedStatement;
}
Also used : ExpressionAnalysisContext(io.crate.analyze.expressions.ExpressionAnalysisContext) Symbol(io.crate.analyze.symbol.Symbol) ExpressionAnalyzer(io.crate.analyze.expressions.ExpressionAnalyzer) TransactionContext(io.crate.metadata.TransactionContext) ParameterExpression(io.crate.sql.tree.ParameterExpression) WhereClauseAnalyzer(io.crate.analyze.where.WhereClauseAnalyzer)

Example 2 with WhereClauseAnalyzer

use of io.crate.analyze.where.WhereClauseAnalyzer in project crate by crate.

the class UpdateAnalyzer method analyze.

public AnalyzedStatement analyze(Update node, Analysis analysis) {
    StatementAnalysisContext statementAnalysisContext = new StatementAnalysisContext(analysis.sessionContext(), analysis.parameterContext(), Operation.UPDATE, analysis.transactionContext());
    RelationAnalysisContext currentRelationContext = statementAnalysisContext.startRelation();
    AnalyzedRelation analyzedRelation = relationAnalyzer.analyze(node.relation(), statementAnalysisContext);
    FieldResolver fieldResolver = (FieldResolver) analyzedRelation;
    EvaluatingNormalizer normalizer = new EvaluatingNormalizer(functions, RowGranularity.CLUSTER, ReplaceMode.MUTATE, null, fieldResolver);
    FieldProvider columnFieldProvider = new NameFieldProvider(analyzedRelation);
    ExpressionAnalyzer columnExpressionAnalyzer = new ExpressionAnalyzer(functions, analysis.sessionContext(), analysis.parameterContext(), columnFieldProvider, null);
    columnExpressionAnalyzer.setResolveFieldsOperation(Operation.UPDATE);
    assert Iterables.getOnlyElement(currentRelationContext.sources().values()) == analyzedRelation : "currentRelationContext.sources().values() must have one element and equal to analyzedRelation";
    ExpressionAnalyzer expressionAnalyzer = new ExpressionAnalyzer(functions, analysis.sessionContext(), analysis.parameterContext(), new FullQualifedNameFieldProvider(currentRelationContext.sources()), null);
    ExpressionAnalysisContext expressionAnalysisContext = new ExpressionAnalysisContext();
    int numNested = 1;
    if (analysis.parameterContext().numBulkParams() > 0) {
        numNested = analysis.parameterContext().numBulkParams();
    }
    WhereClauseAnalyzer whereClauseAnalyzer = null;
    if (analyzedRelation instanceof DocTableRelation) {
        whereClauseAnalyzer = new WhereClauseAnalyzer(functions, ((DocTableRelation) analyzedRelation));
    }
    TableInfo tableInfo = ((AbstractTableRelation) analyzedRelation).tableInfo();
    List<UpdateAnalyzedStatement.NestedAnalyzedStatement> nestedAnalyzedStatements = new ArrayList<>(numNested);
    for (int i = 0; i < numNested; i++) {
        analysis.parameterContext().setBulkIdx(i);
        Symbol querySymbol = expressionAnalyzer.generateQuerySymbol(node.whereClause(), expressionAnalysisContext);
        WhereClause whereClause = new WhereClause(normalizer.normalize(querySymbol, analysis.transactionContext()));
        if (whereClauseAnalyzer != null) {
            whereClause = whereClauseAnalyzer.analyze(whereClause, analysis.transactionContext());
        }
        if (!whereClause.docKeys().isPresent() && Symbols.containsColumn(whereClause.query(), DocSysColumns.VERSION)) {
            throw VERSION_SEARCH_EX;
        }
        UpdateAnalyzedStatement.NestedAnalyzedStatement nestedAnalyzedStatement = new UpdateAnalyzedStatement.NestedAnalyzedStatement(whereClause);
        for (Assignment assignment : node.assignements()) {
            analyzeAssignment(assignment, nestedAnalyzedStatement, tableInfo, normalizer, expressionAnalyzer, columnExpressionAnalyzer, expressionAnalysisContext, analysis.transactionContext());
        }
        nestedAnalyzedStatements.add(nestedAnalyzedStatement);
    }
    statementAnalysisContext.endRelation();
    return new UpdateAnalyzedStatement(analyzedRelation, nestedAnalyzedStatements);
}
Also used : ExpressionAnalysisContext(io.crate.analyze.expressions.ExpressionAnalysisContext) Symbol(io.crate.analyze.symbol.Symbol) ExpressionAnalyzer(io.crate.analyze.expressions.ExpressionAnalyzer) ArrayList(java.util.ArrayList) Assignment(io.crate.sql.tree.Assignment) WhereClauseAnalyzer(io.crate.analyze.where.WhereClauseAnalyzer) TableInfo(io.crate.metadata.table.TableInfo)

Example 3 with WhereClauseAnalyzer

use of io.crate.analyze.where.WhereClauseAnalyzer in project crate by crate.

the class QueriedDocTable method analyzeWhereClause.

void analyzeWhereClause(Functions functions, TransactionContext transactionContext) {
    WhereClauseAnalyzer whereClauseAnalyzer = new WhereClauseAnalyzer(functions, tableRelation());
    querySpec().where(whereClauseAnalyzer.analyze(querySpec().where(), transactionContext));
}
Also used : WhereClauseAnalyzer(io.crate.analyze.where.WhereClauseAnalyzer)

Example 4 with WhereClauseAnalyzer

use of io.crate.analyze.where.WhereClauseAnalyzer in project crate by crate.

the class CopyAnalyzer method createWhereClause.

private WhereClause createWhereClause(Optional<Expression> where, DocTableRelation tableRelation, List<String> partitions, EvaluatingNormalizer normalizer, ExpressionAnalyzer expressionAnalyzer, ExpressionAnalysisContext expressionAnalysisContext, TransactionContext transactionContext) {
    WhereClause whereClause = null;
    if (where.isPresent()) {
        WhereClauseAnalyzer whereClauseAnalyzer = new WhereClauseAnalyzer(functions, tableRelation);
        Symbol query = expressionAnalyzer.convert(where.get(), expressionAnalysisContext);
        whereClause = new WhereClause(normalizer.normalize(query, transactionContext));
        whereClause = whereClauseAnalyzer.analyze(whereClause, transactionContext);
    }
    if (whereClause == null) {
        return new WhereClause(null, null, partitions);
    } else if (whereClause.noMatch()) {
        return whereClause;
    } else {
        if (!whereClause.partitions().isEmpty() && !partitions.isEmpty() && !whereClause.partitions().equals(partitions)) {
            throw new IllegalArgumentException("Given partition ident does not match partition evaluated from where clause");
        }
        return new WhereClause(whereClause.query(), whereClause.docKeys().orElse(null), partitions.isEmpty() ? whereClause.partitions() : partitions);
    }
}
Also used : Symbol(io.crate.analyze.symbol.Symbol) WhereClauseAnalyzer(io.crate.analyze.where.WhereClauseAnalyzer)

Aggregations

WhereClauseAnalyzer (io.crate.analyze.where.WhereClauseAnalyzer)4 Symbol (io.crate.analyze.symbol.Symbol)3 ExpressionAnalysisContext (io.crate.analyze.expressions.ExpressionAnalysisContext)2 ExpressionAnalyzer (io.crate.analyze.expressions.ExpressionAnalyzer)2 TransactionContext (io.crate.metadata.TransactionContext)1 TableInfo (io.crate.metadata.table.TableInfo)1 Assignment (io.crate.sql.tree.Assignment)1 ParameterExpression (io.crate.sql.tree.ParameterExpression)1 ArrayList (java.util.ArrayList)1