Search in sources :

Example 21 with EvaluatingNormalizer

use of io.crate.expression.eval.EvaluatingNormalizer in project crate by crate.

the class InsertAnalyzer method getUpdateAssignments.

private static Map<Reference, Symbol> getUpdateAssignments(DocTableRelation targetTable, List<Reference> targetCols, ExpressionAnalyzer exprAnalyzer, CoordinatorTxnCtx txnCtx, NodeContext nodeCtx, ParamTypeHints paramTypeHints, Insert.DuplicateKeyContext<Expression> duplicateKeyContext) {
    if (duplicateKeyContext.getAssignments().isEmpty()) {
        return Collections.emptyMap();
    }
    ExpressionAnalysisContext exprCtx = new ExpressionAnalysisContext(txnCtx.sessionContext());
    ValuesResolver valuesResolver = new ValuesResolver(targetCols);
    final FieldProvider<?> fieldProvider;
    if (duplicateKeyContext.getType() == Insert.DuplicateKeyContext.Type.ON_CONFLICT_DO_UPDATE_SET) {
        fieldProvider = new ExcludedFieldProvider(new NameFieldProvider(targetTable), valuesResolver);
    } else {
        fieldProvider = new NameFieldProvider(targetTable);
    }
    var expressionAnalyzer = new ExpressionAnalyzer(txnCtx, nodeCtx, paramTypeHints, fieldProvider, null);
    var normalizer = new EvaluatingNormalizer(nodeCtx, RowGranularity.CLUSTER, null, targetTable);
    Map<Reference, Symbol> updateAssignments = new HashMap<>(duplicateKeyContext.getAssignments().size());
    for (Assignment<Expression> assignment : duplicateKeyContext.getAssignments()) {
        Reference targetCol = (Reference) exprAnalyzer.convert(assignment.columnName(), exprCtx);
        Symbol valueSymbol = ValueNormalizer.normalizeInputForReference(normalizer.normalize(expressionAnalyzer.convert(assignment.expression(), exprCtx), txnCtx), targetCol, targetTable.tableInfo(), s -> normalizer.normalize(s, txnCtx));
        updateAssignments.put(targetCol, valueSymbol);
    }
    return updateAssignments;
}
Also used : ExpressionAnalysisContext(io.crate.analyze.expressions.ExpressionAnalysisContext) EvaluatingNormalizer(io.crate.expression.eval.EvaluatingNormalizer) HashMap(java.util.HashMap) QualifiedNameReference(io.crate.sql.tree.QualifiedNameReference) DynamicReference(io.crate.expression.symbol.DynamicReference) GeneratedReference(io.crate.metadata.GeneratedReference) Reference(io.crate.metadata.Reference) Symbol(io.crate.expression.symbol.Symbol) ExpressionAnalyzer(io.crate.analyze.expressions.ExpressionAnalyzer) FullQualifiedNameFieldProvider(io.crate.analyze.relations.FullQualifiedNameFieldProvider) NameFieldProvider(io.crate.analyze.relations.NameFieldProvider) Expression(io.crate.sql.tree.Expression) ExcludedFieldProvider(io.crate.analyze.relations.ExcludedFieldProvider)

Example 22 with EvaluatingNormalizer

use of io.crate.expression.eval.EvaluatingNormalizer in project crate by crate.

the class CopyAnalyzer method analyzeCopyTo.

AnalyzedCopyTo analyzeCopyTo(CopyTo<Expression> node, ParamTypeHints paramTypeHints, CoordinatorTxnCtx txnCtx) {
    if (!node.directoryUri()) {
        throw new UnsupportedOperationException("Using COPY TO without specifying a DIRECTORY is not supported");
    }
    TableInfo tableInfo = schemas.resolveTableInfo(node.table().getName(), Operation.COPY_TO, txnCtx.sessionContext().sessionUser(), txnCtx.sessionContext().searchPath());
    Operation.blockedRaiseException(tableInfo, Operation.READ);
    DocTableRelation tableRelation = new DocTableRelation((DocTableInfo) tableInfo);
    EvaluatingNormalizer normalizer = new EvaluatingNormalizer(nodeCtx, RowGranularity.CLUSTER, null, tableRelation);
    var exprCtx = new ExpressionAnalysisContext(txnCtx.sessionContext());
    var expressionAnalyzer = new ExpressionAnalyzer(txnCtx, nodeCtx, paramTypeHints, new NameFieldProvider(tableRelation), null);
    var exprAnalyzerWithFieldsAsString = new ExpressionAnalyzer(txnCtx, nodeCtx, paramTypeHints, FieldProvider.FIELDS_AS_LITERAL, null);
    var uri = expressionAnalyzer.convert(node.targetUri(), exprCtx);
    var table = node.table().map(x -> exprAnalyzerWithFieldsAsString.convert(x, exprCtx));
    var properties = node.properties().map(x -> expressionAnalyzer.convert(x, exprCtx));
    var columns = Lists2.map(node.columns(), c -> normalizer.normalize(expressionAnalyzer.convert(c, exprCtx), txnCtx));
    var whereClause = node.whereClause().map(w -> normalizer.normalize(expressionAnalyzer.convert(w, exprCtx), txnCtx)).orElse(null);
    return new AnalyzedCopyTo(tableInfo, table, normalizer.normalize(uri, txnCtx), properties, columns, whereClause);
}
Also used : ExpressionAnalyzer(io.crate.analyze.expressions.ExpressionAnalyzer) DocTableInfo(io.crate.metadata.doc.DocTableInfo) TableInfo(io.crate.metadata.table.TableInfo) NodeContext(io.crate.metadata.NodeContext) TableRelation(io.crate.analyze.relations.TableRelation) ExpressionAnalysisContext(io.crate.analyze.expressions.ExpressionAnalysisContext) FieldProvider(io.crate.analyze.relations.FieldProvider) CopyFrom(io.crate.sql.tree.CopyFrom) Table(io.crate.sql.tree.Table) EvaluatingNormalizer(io.crate.expression.eval.EvaluatingNormalizer) GenericProperties(io.crate.sql.tree.GenericProperties) Operation(io.crate.metadata.table.Operation) Lists2(io.crate.common.collections.Lists2) CopyTo(io.crate.sql.tree.CopyTo) RowGranularity(io.crate.metadata.RowGranularity) DocTableRelation(io.crate.analyze.relations.DocTableRelation) Symbol(io.crate.expression.symbol.Symbol) NameFieldProvider(io.crate.analyze.relations.NameFieldProvider) Schemas(io.crate.metadata.Schemas) Expression(io.crate.sql.tree.Expression) CoordinatorTxnCtx(io.crate.metadata.CoordinatorTxnCtx) ExpressionAnalysisContext(io.crate.analyze.expressions.ExpressionAnalysisContext) EvaluatingNormalizer(io.crate.expression.eval.EvaluatingNormalizer) NameFieldProvider(io.crate.analyze.relations.NameFieldProvider) ExpressionAnalyzer(io.crate.analyze.expressions.ExpressionAnalyzer) DocTableRelation(io.crate.analyze.relations.DocTableRelation) DocTableInfo(io.crate.metadata.doc.DocTableInfo) TableInfo(io.crate.metadata.table.TableInfo)

Example 23 with EvaluatingNormalizer

use of io.crate.expression.eval.EvaluatingNormalizer in project crate by crate.

the class UpdatePlanner method plan.

private static Plan plan(DocTableRelation docTable, Map<Reference, Symbol> assignmentByTargetCol, Symbol query, PlannerContext plannerCtx, @Nullable List<Symbol> returnValues) {
    EvaluatingNormalizer normalizer = EvaluatingNormalizer.functionOnlyNormalizer(plannerCtx.nodeContext());
    DocTableInfo tableInfo = docTable.tableInfo();
    WhereClauseOptimizer.DetailedQuery detailedQuery = WhereClauseOptimizer.optimize(normalizer, query, tableInfo, plannerCtx.transactionContext(), plannerCtx.nodeContext());
    if (detailedQuery.docKeys().isPresent()) {
        return new UpdateById(tableInfo, assignmentByTargetCol, detailedQuery.docKeys().get(), returnValues, plannerCtx.nodeContext());
    }
    return new Update((plannerContext, params, subQueryValues) -> updateByQuery(plannerContext, docTable, assignmentByTargetCol, detailedQuery, params, subQueryValues, returnValues));
}
Also used : DocTableInfo(io.crate.metadata.doc.DocTableInfo) EvaluatingNormalizer(io.crate.expression.eval.EvaluatingNormalizer) WhereClauseOptimizer(io.crate.planner.WhereClauseOptimizer) UpdateById(io.crate.planner.node.dml.UpdateById)

Example 24 with EvaluatingNormalizer

use of io.crate.expression.eval.EvaluatingNormalizer in project crate by crate.

the class WhereClauseAnalyzerTest method analyzeSelectWhere.

private WhereClause analyzeSelectWhere(String stmt) {
    AnalyzedRelation rel = e.analyze(stmt);
    if (rel instanceof QueriedSelectRelation) {
        QueriedSelectRelation queriedRelation = ((QueriedSelectRelation) rel);
        if (queriedRelation.from().get(0) instanceof DocTableRelation) {
            DocTableRelation docTableRelation = (DocTableRelation) queriedRelation.from().get(0);
            WhereClauseOptimizer.DetailedQuery detailedQuery = WhereClauseOptimizer.optimize(new EvaluatingNormalizer(e.nodeCtx, RowGranularity.CLUSTER, null, docTableRelation), queriedRelation.where(), docTableRelation.tableInfo(), coordinatorTxnCtx, e.nodeCtx);
            return detailedQuery.toBoundWhereClause(docTableRelation.tableInfo(), Row.EMPTY, SubQueryResults.EMPTY, coordinatorTxnCtx, e.nodeCtx);
        }
        return new WhereClause(queriedRelation.where());
    } else {
        return WhereClause.MATCH_ALL;
    }
}
Also used : EvaluatingNormalizer(io.crate.expression.eval.EvaluatingNormalizer) QueriedSelectRelation(io.crate.analyze.QueriedSelectRelation) WhereClause(io.crate.analyze.WhereClause) DocTableRelation(io.crate.analyze.relations.DocTableRelation) WhereClauseOptimizer(io.crate.planner.WhereClauseOptimizer) AnalyzedRelation(io.crate.analyze.relations.AnalyzedRelation)

Aggregations

EvaluatingNormalizer (io.crate.expression.eval.EvaluatingNormalizer)24 Symbol (io.crate.expression.symbol.Symbol)16 DocTableRelation (io.crate.analyze.relations.DocTableRelation)9 CoordinatorTxnCtx (io.crate.metadata.CoordinatorTxnCtx)8 Test (org.junit.Test)7 RowGranularity (io.crate.metadata.RowGranularity)6 DocTableInfo (io.crate.metadata.doc.DocTableInfo)6 WhereClause (io.crate.analyze.WhereClause)5 ExpressionAnalysisContext (io.crate.analyze.expressions.ExpressionAnalysisContext)5 ExpressionAnalyzer (io.crate.analyze.expressions.ExpressionAnalyzer)5 Literal (io.crate.expression.symbol.Literal)5 NodeContext (io.crate.metadata.NodeContext)5 Routing (io.crate.metadata.Routing)5 ArrayList (java.util.ArrayList)5 Lists2 (io.crate.common.collections.Lists2)4 Nullable (javax.annotation.Nullable)4 AbstractTableRelation (io.crate.analyze.relations.AbstractTableRelation)3 AnalyzedRelation (io.crate.analyze.relations.AnalyzedRelation)3 FullQualifiedNameFieldProvider (io.crate.analyze.relations.FullQualifiedNameFieldProvider)3 RoutedCollectPhase (io.crate.execution.dsl.phases.RoutedCollectPhase)3