Search in sources :

Example 1 with ValueNormalizer

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

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

the class InsertFromSubQueryAnalyzer method processUpdateAssignments.

private Map<Reference, Symbol> processUpdateAssignments(DocTableRelation tableRelation, List<Reference> targetColumns, SessionContext sessionContext, ParameterContext parameterContext, TransactionContext transactionContext, FieldProvider fieldProvider, List<Assignment> assignments) {
    ExpressionAnalyzer expressionAnalyzer = new ExpressionAnalyzer(functions, sessionContext, parameterContext, fieldProvider, null);
    ExpressionAnalysisContext expressionAnalysisContext = new ExpressionAnalysisContext();
    EvaluatingNormalizer normalizer = new EvaluatingNormalizer(functions, RowGranularity.CLUSTER, ReplaceMode.COPY, null, tableRelation);
    ValueNormalizer valuesNormalizer = new ValueNormalizer(schemas);
    ValuesResolver valuesResolver = new ValuesResolver(tableRelation, targetColumns);
    ValuesAwareExpressionAnalyzer valuesAwareExpressionAnalyzer = new ValuesAwareExpressionAnalyzer(functions, sessionContext, parameterContext, fieldProvider, valuesResolver);
    Map<Reference, Symbol> updateAssignments = new HashMap<>(assignments.size());
    for (Assignment assignment : assignments) {
        Reference columnName = tableRelation.resolveField((Field) expressionAnalyzer.convert(assignment.columnName(), expressionAnalysisContext));
        assert columnName != null : "columnName must not be null";
        Symbol valueSymbol = normalizer.normalize(valuesAwareExpressionAnalyzer.convert(assignment.expression(), expressionAnalysisContext), transactionContext);
        Symbol assignmentExpression = valuesNormalizer.normalizeInputForReference(valueSymbol, columnName);
        updateAssignments.put(columnName, assignmentExpression);
    }
    return updateAssignments;
}
Also used : Assignment(io.crate.sql.tree.Assignment) ValueNormalizer(io.crate.analyze.expressions.ValueNormalizer) ExpressionAnalysisContext(io.crate.analyze.expressions.ExpressionAnalysisContext) DynamicReference(io.crate.analyze.symbol.DynamicReference) Symbol(io.crate.analyze.symbol.Symbol) ExpressionAnalyzer(io.crate.analyze.expressions.ExpressionAnalyzer)

Aggregations

ExpressionAnalysisContext (io.crate.analyze.expressions.ExpressionAnalysisContext)2 ExpressionAnalyzer (io.crate.analyze.expressions.ExpressionAnalyzer)2 ValueNormalizer (io.crate.analyze.expressions.ValueNormalizer)2 Symbol (io.crate.analyze.symbol.Symbol)2 DocTableRelation (io.crate.analyze.relations.DocTableRelation)1 FieldProvider (io.crate.analyze.relations.FieldProvider)1 NameFieldProvider (io.crate.analyze.relations.NameFieldProvider)1 DynamicReference (io.crate.analyze.symbol.DynamicReference)1 DocTableInfo (io.crate.metadata.doc.DocTableInfo)1 Assignment (io.crate.sql.tree.Assignment)1