Search in sources :

Example 31 with DocTableRelation

use of io.crate.analyze.relations.DocTableRelation in project crate by crate.

the class InsertAnalyzer method analyze.

public AnalyzedInsertStatement analyze(Insert<Expression> insert, ParamTypeHints typeHints, CoordinatorTxnCtx txnCtx) {
    DocTableInfo tableInfo = (DocTableInfo) schemas.resolveTableInfo(insert.table().getName(), Operation.INSERT, txnCtx.sessionContext().sessionUser(), txnCtx.sessionContext().searchPath());
    List<Reference> targetColumns = new ArrayList<>(resolveTargetColumns(insert.columns(), tableInfo));
    AnalyzedRelation subQueryRelation = relationAnalyzer.analyze(insert.insertSource(), new StatementAnalysisContext(typeHints, Operation.READ, txnCtx, targetColumns));
    ensureClusteredByPresentOrNotRequired(targetColumns, tableInfo);
    checkSourceAndTargetColsForLengthAndTypesCompatibility(targetColumns, subQueryRelation.outputs());
    DocTableRelation tableRelation = new DocTableRelation(tableInfo);
    NameFieldProvider fieldProvider = new NameFieldProvider(tableRelation);
    ExpressionAnalyzer expressionAnalyzer = new ExpressionAnalyzer(txnCtx, nodeCtx, typeHints, fieldProvider, null, Operation.READ);
    verifyOnConflictTargets(txnCtx, expressionAnalyzer, tableInfo, insert.duplicateKeyContext());
    Map<Reference, Symbol> onDuplicateKeyAssignments = processUpdateAssignments(tableRelation, targetColumns, typeHints, txnCtx, nodeCtx, fieldProvider, insert.duplicateKeyContext());
    final boolean ignoreDuplicateKeys = insert.duplicateKeyContext().getType() == Insert.DuplicateKeyContext.Type.ON_CONFLICT_DO_NOTHING;
    List<Symbol> returnValues;
    if (insert.returningClause().isEmpty()) {
        returnValues = null;
    } else {
        var exprCtx = new ExpressionAnalysisContext(txnCtx.sessionContext());
        Map<RelationName, AnalyzedRelation> sources = Map.of(tableRelation.relationName(), tableRelation);
        var sourceExprAnalyzer = new ExpressionAnalyzer(txnCtx, nodeCtx, typeHints, new FullQualifiedNameFieldProvider(sources, ParentRelations.NO_PARENTS, txnCtx.sessionContext().searchPath().currentSchema()), null);
        var selectAnalysis = SelectAnalyzer.analyzeSelectItems(insert.returningClause(), sources, sourceExprAnalyzer, exprCtx);
        returnValues = selectAnalysis.outputSymbols();
    }
    return new AnalyzedInsertStatement(subQueryRelation, tableInfo, targetColumns, ignoreDuplicateKeys, onDuplicateKeyAssignments, returnValues);
}
Also used : DocTableInfo(io.crate.metadata.doc.DocTableInfo) ExpressionAnalysisContext(io.crate.analyze.expressions.ExpressionAnalysisContext) 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) ArrayList(java.util.ArrayList) AnalyzedRelation(io.crate.analyze.relations.AnalyzedRelation) StatementAnalysisContext(io.crate.analyze.relations.StatementAnalysisContext) FullQualifiedNameFieldProvider(io.crate.analyze.relations.FullQualifiedNameFieldProvider) NameFieldProvider(io.crate.analyze.relations.NameFieldProvider) DocTableRelation(io.crate.analyze.relations.DocTableRelation) RelationName(io.crate.metadata.RelationName) FullQualifiedNameFieldProvider(io.crate.analyze.relations.FullQualifiedNameFieldProvider)

Example 32 with DocTableRelation

use of io.crate.analyze.relations.DocTableRelation 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 33 with DocTableRelation

use of io.crate.analyze.relations.DocTableRelation in project crate by crate.

the class UpdatePlanner method plan.

public static Plan plan(AnalyzedUpdateStatement update, PlannerContext plannerCtx, SubqueryPlanner subqueryPlanner) {
    if (update.outputs() != null && !plannerCtx.clusterState().getNodes().getMinNodeVersion().onOrAfter(Version.V_4_2_0)) {
        throw new UnsupportedFeatureException(RETURNING_VERSION_ERROR_MSG);
    }
    AbstractTableRelation<?> table = update.table();
    Plan plan;
    if (table instanceof DocTableRelation) {
        DocTableRelation docTable = (DocTableRelation) table;
        plan = plan(docTable, update.assignmentByTargetCol(), update.query(), plannerCtx, update.outputs());
    } else {
        plan = new Update((plannerContext, params, subQueryValues) -> sysUpdate(plannerContext, (TableRelation) table, update.assignmentByTargetCol(), update.query(), params, subQueryValues, update.outputs()));
    }
    Map<LogicalPlan, SelectSymbol> subQueries = subqueryPlanner.planSubQueries(update);
    return MultiPhasePlan.createIfNeeded(plan, subQueries);
}
Also used : UnsupportedFeatureException(io.crate.exceptions.UnsupportedFeatureException) UpdateProjection(io.crate.execution.dsl.projection.UpdateProjection) SessionContext(io.crate.action.sql.SessionContext) SysUpdateProjection(io.crate.execution.dsl.projection.SysUpdateProjection) EvaluatingNormalizer(io.crate.expression.eval.EvaluatingNormalizer) Collections.singletonList(java.util.Collections.singletonList) DependencyCarrier(io.crate.planner.DependencyCarrier) Map(java.util.Map) SelectSymbol(io.crate.expression.symbol.SelectSymbol) DistributionInfo(io.crate.planner.distribution.DistributionInfo) DocSysColumns(io.crate.metadata.doc.DocSysColumns) DocTableInfo(io.crate.metadata.doc.DocTableInfo) TableInfo(io.crate.metadata.table.TableInfo) LogicalPlan(io.crate.planner.operators.LogicalPlan) RoutedCollectPhase(io.crate.execution.dsl.phases.RoutedCollectPhase) ExecutionPlan(io.crate.planner.ExecutionPlan) List(java.util.List) Version(org.elasticsearch.Version) Row(io.crate.data.Row) Projection(io.crate.execution.dsl.projection.Projection) Symbol(io.crate.expression.symbol.Symbol) DataTypes(io.crate.types.DataTypes) SubQueryResults(io.crate.planner.operators.SubQueryResults) Assignments(io.crate.expression.symbol.Assignments) VisibleForTesting(io.crate.common.annotations.VisibleForTesting) InputColumn(io.crate.expression.symbol.InputColumn) SubqueryPlanner(io.crate.planner.SubqueryPlanner) WhereClauseOptimizer(io.crate.planner.WhereClauseOptimizer) CompletableFuture(java.util.concurrent.CompletableFuture) AnalyzedUpdateStatement(io.crate.analyze.AnalyzedUpdateStatement) ArrayList(java.util.ArrayList) Routing(io.crate.metadata.Routing) Objects.requireNonNull(java.util.Objects.requireNonNull) UpdateById(io.crate.planner.node.dml.UpdateById) VersioningValidationException(io.crate.exceptions.VersioningValidationException) TopN(io.crate.execution.engine.pipeline.TopN) Optimizer(io.crate.planner.optimizer.symbol.Optimizer) Nullable(javax.annotation.Nullable) MergeCountProjection(io.crate.execution.dsl.projection.MergeCountProjection) TableRelation(io.crate.analyze.relations.TableRelation) WhereClause(io.crate.analyze.WhereClause) NodeOperationTree(io.crate.execution.dsl.phases.NodeOperationTree) Reference(io.crate.metadata.Reference) NodeOperationTreeGenerator(io.crate.execution.engine.NodeOperationTreeGenerator) SubQueryAndParamBinder(io.crate.planner.operators.SubQueryAndParamBinder) Merge(io.crate.planner.Merge) RoutingProvider(io.crate.metadata.RoutingProvider) RowConsumer(io.crate.data.RowConsumer) DocTableRelation(io.crate.analyze.relations.DocTableRelation) AbstractTableRelation(io.crate.analyze.relations.AbstractTableRelation) PlannerContext(io.crate.planner.PlannerContext) Plan(io.crate.planner.Plan) MultiPhasePlan(io.crate.planner.MultiPhasePlan) Collect(io.crate.planner.node.dql.Collect) SelectSymbol(io.crate.expression.symbol.SelectSymbol) UnsupportedFeatureException(io.crate.exceptions.UnsupportedFeatureException) DocTableRelation(io.crate.analyze.relations.DocTableRelation) LogicalPlan(io.crate.planner.operators.LogicalPlan) LogicalPlan(io.crate.planner.operators.LogicalPlan) ExecutionPlan(io.crate.planner.ExecutionPlan) Plan(io.crate.planner.Plan) MultiPhasePlan(io.crate.planner.MultiPhasePlan)

Example 34 with DocTableRelation

use of io.crate.analyze.relations.DocTableRelation 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)

Example 35 with DocTableRelation

use of io.crate.analyze.relations.DocTableRelation in project crate by crate.

the class T3 method sources.

public static Map<RelationName, AnalyzedRelation> sources(Iterable<RelationName> relations, ClusterService clusterService) {
    SQLExecutor.Builder executorBuilder = SQLExecutor.builder(clusterService);
    relations.forEach(rn -> {
        String tableDefinition = RELATION_DEFINITIONS.get(rn);
        if (tableDefinition == null) {
            throw new RuntimeException("Unknown relation " + rn);
        }
        try {
            executorBuilder.addTable(tableDefinition);
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    });
    SQLExecutor executor = executorBuilder.build();
    Schemas schemas = executor.schemas();
    MapBuilder<RelationName, AnalyzedRelation> builder = MapBuilder.newMapBuilder();
    for (RelationName relationName : relations) {
        builder.put(relationName, new DocTableRelation(schemas.getTableInfo(relationName)));
    }
    return builder.immutableMap();
}
Also used : RelationName(io.crate.metadata.RelationName) DocTableRelation(io.crate.analyze.relations.DocTableRelation) IOException(java.io.IOException) Schemas(io.crate.metadata.Schemas) AnalyzedRelation(io.crate.analyze.relations.AnalyzedRelation)

Aggregations

DocTableRelation (io.crate.analyze.relations.DocTableRelation)36 DocTableInfo (io.crate.metadata.doc.DocTableInfo)17 RelationName (io.crate.metadata.RelationName)13 AnalyzedRelation (io.crate.analyze.relations.AnalyzedRelation)10 Symbol (io.crate.expression.symbol.Symbol)10 CrateDummyClusterServiceUnitTest (io.crate.test.integration.CrateDummyClusterServiceUnitTest)10 SqlExpressions (io.crate.testing.SqlExpressions)10 Test (org.junit.Test)10 Before (org.junit.Before)9 ExpressionAnalysisContext (io.crate.analyze.expressions.ExpressionAnalysisContext)7 ExpressionAnalyzer (io.crate.analyze.expressions.ExpressionAnalyzer)7 EvaluatingNormalizer (io.crate.expression.eval.EvaluatingNormalizer)7 Reference (io.crate.metadata.Reference)6 SQLExecutor (io.crate.testing.SQLExecutor)6 WhereClause (io.crate.analyze.WhereClause)5 TableInfo (io.crate.metadata.table.TableInfo)4 QueriedSelectRelation (io.crate.analyze.QueriedSelectRelation)3 FullQualifiedNameFieldProvider (io.crate.analyze.relations.FullQualifiedNameFieldProvider)3 NameFieldProvider (io.crate.analyze.relations.NameFieldProvider)3 StatementAnalysisContext (io.crate.analyze.relations.StatementAnalysisContext)3