Search in sources :

Example 1 with ExpressionAnalysisContext

use of io.crate.analyze.expressions.ExpressionAnalysisContext in project crate by crate.

the class DocIndexMetaData method initializeGeneratedExpressions.

private void initializeGeneratedExpressions() {
    if (generatedColumnReferences.isEmpty()) {
        return;
    }
    Collection<Reference> references = this.references.values();
    TableReferenceResolver tableReferenceResolver = new TableReferenceResolver(references);
    ExpressionAnalyzer expressionAnalyzer = new ExpressionAnalyzer(functions, SessionContext.SYSTEM_SESSION, ParamTypeHints.EMPTY, tableReferenceResolver, null);
    ExpressionAnalysisContext context = new ExpressionAnalysisContext();
    for (Reference reference : generatedColumnReferences) {
        GeneratedReference generatedReference = (GeneratedReference) reference;
        Expression expression = SqlParser.createExpression(generatedReference.formattedGeneratedExpression());
        generatedReference.generatedExpression(expressionAnalyzer.convert(expression, context));
        generatedReference.referencedReferences(ImmutableList.copyOf(tableReferenceResolver.references()));
        tableReferenceResolver.references().clear();
    }
}
Also used : ExpressionAnalysisContext(io.crate.analyze.expressions.ExpressionAnalysisContext) Expression(io.crate.sql.tree.Expression) ExpressionAnalyzer(io.crate.analyze.expressions.ExpressionAnalyzer) TableReferenceResolver(io.crate.analyze.expressions.TableReferenceResolver)

Example 2 with ExpressionAnalysisContext

use of io.crate.analyze.expressions.ExpressionAnalysisContext 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 3 with ExpressionAnalysisContext

use of io.crate.analyze.expressions.ExpressionAnalysisContext 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 4 with ExpressionAnalysisContext

use of io.crate.analyze.expressions.ExpressionAnalysisContext in project crate by crate.

the class InsertFromValuesAnalyzer method analyze.

public AnalyzedStatement analyze(InsertFromValues node, Analysis analysis) {
    DocTableInfo tableInfo = schemas.getWritableTable(TableIdent.of(node.table(), analysis.sessionContext().defaultSchema()));
    Operation.blockedRaiseException(tableInfo, Operation.INSERT);
    DocTableRelation tableRelation = new DocTableRelation(tableInfo);
    FieldProvider fieldProvider = new NameFieldProvider(tableRelation);
    Function<ParameterExpression, Symbol> convertParamFunction = analysis.parameterContext();
    ExpressionAnalyzer expressionAnalyzer = new ExpressionAnalyzer(functions, analysis.sessionContext(), convertParamFunction, fieldProvider, null);
    ExpressionAnalysisContext expressionAnalysisContext = new ExpressionAnalysisContext();
    expressionAnalyzer.setResolveFieldsOperation(Operation.INSERT);
    ValuesResolver valuesResolver = new ValuesResolver(tableRelation);
    ExpressionAnalyzer valuesAwareExpressionAnalyzer = new ValuesAwareExpressionAnalyzer(functions, analysis.sessionContext(), convertParamFunction, fieldProvider, valuesResolver);
    InsertFromValuesAnalyzedStatement statement = new InsertFromValuesAnalyzedStatement(tableInfo, analysis.parameterContext().numBulkParams());
    handleInsertColumns(node, node.maxValuesLength(), statement);
    Set<Reference> allReferencedReferences = new HashSet<>();
    for (GeneratedReference reference : tableInfo.generatedColumns()) {
        allReferencedReferences.addAll(reference.referencedReferences());
    }
    ReferenceToLiteralConverter.Context referenceToLiteralContext = new ReferenceToLiteralConverter.Context(statement.columns(), allReferencedReferences);
    ValueNormalizer valuesNormalizer = new ValueNormalizer(schemas);
    EvaluatingNormalizer normalizer = new EvaluatingNormalizer(functions, RowGranularity.CLUSTER, ReplaceMode.COPY, null, tableRelation);
    analyzeColumns(statement.tableInfo(), statement.columns());
    for (ValuesList valuesList : node.valuesLists()) {
        analyzeValues(tableRelation, valuesNormalizer, normalizer, expressionAnalyzer, expressionAnalysisContext, analysis.transactionContext(), valuesResolver, valuesAwareExpressionAnalyzer, valuesList, node.onDuplicateKeyAssignments(), statement, analysis.parameterContext(), referenceToLiteralContext);
    }
    return statement;
}
Also used : ExpressionAnalysisContext(io.crate.analyze.expressions.ExpressionAnalysisContext) DocTableInfo(io.crate.metadata.doc.DocTableInfo) ValueNormalizer(io.crate.analyze.expressions.ValueNormalizer) ExpressionAnalysisContext(io.crate.analyze.expressions.ExpressionAnalysisContext) Symbol(io.crate.analyze.symbol.Symbol) ExpressionAnalyzer(io.crate.analyze.expressions.ExpressionAnalyzer) FieldProvider(io.crate.analyze.relations.FieldProvider) NameFieldProvider(io.crate.analyze.relations.NameFieldProvider) NameFieldProvider(io.crate.analyze.relations.NameFieldProvider) DocTableRelation(io.crate.analyze.relations.DocTableRelation)

Example 5 with ExpressionAnalysisContext

use of io.crate.analyze.expressions.ExpressionAnalysisContext in project crate by crate.

the class AnalyzedTableElements method validateGeneratedColumns.

private void validateGeneratedColumns(TableIdent tableIdent, Collection<? extends Reference> existingColumns, Functions functions, ParameterContext parameterContext, SessionContext sessionContext) {
    List<Reference> tableReferences = new ArrayList<>();
    for (AnalyzedColumnDefinition columnDefinition : columns) {
        buildReference(tableIdent, columnDefinition, tableReferences);
    }
    tableReferences.addAll(existingColumns);
    TableReferenceResolver tableReferenceResolver = new TableReferenceResolver(tableReferences);
    ExpressionAnalyzer expressionAnalyzer = new ExpressionAnalyzer(functions, sessionContext, parameterContext, tableReferenceResolver, null);
    SymbolPrinter printer = new SymbolPrinter(functions);
    ExpressionAnalysisContext expressionAnalysisContext = new ExpressionAnalysisContext();
    for (AnalyzedColumnDefinition columnDefinition : columns) {
        if (columnDefinition.generatedExpression() != null) {
            processGeneratedExpression(expressionAnalyzer, printer, columnDefinition, expressionAnalysisContext);
        }
    }
}
Also used : SymbolPrinter(io.crate.analyze.symbol.format.SymbolPrinter) ExpressionAnalysisContext(io.crate.analyze.expressions.ExpressionAnalysisContext) ExpressionAnalyzer(io.crate.analyze.expressions.ExpressionAnalyzer) TableReferenceResolver(io.crate.analyze.expressions.TableReferenceResolver)

Aggregations

ExpressionAnalysisContext (io.crate.analyze.expressions.ExpressionAnalysisContext)9 ExpressionAnalyzer (io.crate.analyze.expressions.ExpressionAnalyzer)9 Symbol (io.crate.analyze.symbol.Symbol)7 DocTableRelation (io.crate.analyze.relations.DocTableRelation)3 DocTableInfo (io.crate.metadata.doc.DocTableInfo)3 TableReferenceResolver (io.crate.analyze.expressions.TableReferenceResolver)2 ValueNormalizer (io.crate.analyze.expressions.ValueNormalizer)2 WhereClauseAnalyzer (io.crate.analyze.where.WhereClauseAnalyzer)2 TableInfo (io.crate.metadata.table.TableInfo)2 Assignment (io.crate.sql.tree.Assignment)2 Settings (org.elasticsearch.common.settings.Settings)2 SubqueryAnalyzer (io.crate.analyze.expressions.SubqueryAnalyzer)1 FieldProvider (io.crate.analyze.relations.FieldProvider)1 NameFieldProvider (io.crate.analyze.relations.NameFieldProvider)1 QueriedDocTable (io.crate.analyze.relations.QueriedDocTable)1 SelectAnalysis (io.crate.analyze.relations.select.SelectAnalysis)1 DynamicReference (io.crate.analyze.symbol.DynamicReference)1 SymbolPrinter (io.crate.analyze.symbol.format.SymbolPrinter)1 UnsupportedFeatureException (io.crate.exceptions.UnsupportedFeatureException)1 TransactionContext (io.crate.metadata.TransactionContext)1