Search in sources :

Example 1 with RelationAlreadyExists

use of io.crate.exceptions.RelationAlreadyExists in project crate by crate.

the class CreateViewPlan method executeOrFail.

@Override
public void executeOrFail(DependencyCarrier dependencies, PlannerContext plannerContext, RowConsumer consumer, Row params, SubQueryResults subQueryResults) {
    User owner = createViewStmt.owner();
    String formattedQuery = SqlFormatter.formatSql(createViewStmt.query(), makeExpressions(params));
    ensureFormattedQueryCanStillBeAnalyzed(createViewStmt.name(), dependencies.nodeContext(), dependencies.schemas(), plannerContext.transactionContext(), formattedQuery, createViewStmt.replaceExisting());
    CreateViewRequest request = new CreateViewRequest(createViewStmt.name(), formattedQuery, createViewStmt.replaceExisting(), owner == null ? null : owner.name());
    dependencies.createViewAction().execute(request, new OneRowActionListener<>(consumer, resp -> {
        if (resp.alreadyExistsFailure()) {
            throw new RelationAlreadyExists(createViewStmt.name());
        }
        return new Row1(1L);
    }));
}
Also used : ParamTypeHints(io.crate.analyze.ParamTypeHints) Literal(io.crate.sql.tree.Literal) RelationName(io.crate.metadata.RelationName) SearchPath(io.crate.metadata.SearchPath) DefaultTraversalVisitor(io.crate.sql.tree.DefaultTraversalVisitor) ArrayList(java.util.ArrayList) CreateViewStmt(io.crate.analyze.CreateViewStmt) SqlParser(io.crate.sql.parser.SqlParser) OneRowActionListener(io.crate.execution.support.OneRowActionListener) Query(io.crate.sql.tree.Query) Nullable(javax.annotation.Nullable) NodeContext(io.crate.metadata.NodeContext) ParameterExpression(io.crate.sql.tree.ParameterExpression) User(io.crate.user.User) CreateViewRequest(io.crate.execution.ddl.views.CreateViewRequest) SqlFormatter(io.crate.sql.SqlFormatter) Table(io.crate.sql.tree.Table) RowConsumer(io.crate.data.RowConsumer) List(java.util.List) Row(io.crate.data.Row) RelationAlreadyExists(io.crate.exceptions.RelationAlreadyExists) Symbol(io.crate.expression.symbol.Symbol) RelationAnalyzer(io.crate.analyze.relations.RelationAnalyzer) SubQueryResults(io.crate.planner.operators.SubQueryResults) Schemas(io.crate.metadata.Schemas) Expression(io.crate.sql.tree.Expression) Row1(io.crate.data.Row1) CoordinatorTxnCtx(io.crate.metadata.CoordinatorTxnCtx) Row1(io.crate.data.Row1) User(io.crate.user.User) RelationAlreadyExists(io.crate.exceptions.RelationAlreadyExists) CreateViewRequest(io.crate.execution.ddl.views.CreateViewRequest)

Example 2 with RelationAlreadyExists

use of io.crate.exceptions.RelationAlreadyExists in project crate by crate.

the class TransportCreateTableAction method masterOperation.

@Override
protected void masterOperation(final CreateTableRequest request, final ClusterState state, final ActionListener<CreateTableResponse> listener) {
    final RelationName relationName = request.getTableName();
    if (viewsExists(relationName, state)) {
        listener.onFailure(new RelationAlreadyExists(relationName));
        return;
    }
    if (request.getCreateIndexRequest() != null) {
        CreateIndexRequest createIndexRequest = request.getCreateIndexRequest();
        ActionListener<CreateIndexResponse> wrappedListener = ActionListener.wrap(response -> listener.onResponse(new CreateTableResponse(response.isShardsAcknowledged())), listener::onFailure);
        transportCreateIndexAction.masterOperation(createIndexRequest, state, wrappedListener);
    } else if (request.getPutIndexTemplateRequest() != null) {
        PutIndexTemplateRequest putIndexTemplateRequest = request.getPutIndexTemplateRequest();
        ActionListener<AcknowledgedResponse> wrappedListener = ActionListener.wrap(response -> listener.onResponse(new CreateTableResponse(response.isAcknowledged())), listener::onFailure);
        transportPutIndexTemplateAction.masterOperation(putIndexTemplateRequest, state, wrappedListener);
    } else {
        throw new IllegalStateException("Unknown table request");
    }
}
Also used : RelationName(io.crate.metadata.RelationName) TransportPutIndexTemplateAction(org.elasticsearch.action.admin.indices.template.put.TransportPutIndexTemplateAction) ClusterService(org.elasticsearch.cluster.service.ClusterService) ViewsMetadata(io.crate.metadata.view.ViewsMetadata) IOException(java.io.IOException) AcknowledgedResponse(org.elasticsearch.action.support.master.AcknowledgedResponse) CreateIndexResponse(org.elasticsearch.action.admin.indices.create.CreateIndexResponse) Inject(org.elasticsearch.common.inject.Inject) TransportMasterNodeAction(org.elasticsearch.action.support.master.TransportMasterNodeAction) ClusterState(org.elasticsearch.cluster.ClusterState) RelationAlreadyExists(io.crate.exceptions.RelationAlreadyExists) PutIndexTemplateRequest(org.elasticsearch.action.admin.indices.template.put.PutIndexTemplateRequest) ClusterBlockException(org.elasticsearch.cluster.block.ClusterBlockException) CreateIndexRequest(org.elasticsearch.action.admin.indices.create.CreateIndexRequest) TransportCreateIndexAction(org.elasticsearch.action.admin.indices.create.TransportCreateIndexAction) StreamInput(org.elasticsearch.common.io.stream.StreamInput) ThreadPool(org.elasticsearch.threadpool.ThreadPool) TransportService(org.elasticsearch.transport.TransportService) IndexNameExpressionResolver(org.elasticsearch.cluster.metadata.IndexNameExpressionResolver) ActionListener(org.elasticsearch.action.ActionListener) ActionListener(org.elasticsearch.action.ActionListener) RelationName(io.crate.metadata.RelationName) PutIndexTemplateRequest(org.elasticsearch.action.admin.indices.template.put.PutIndexTemplateRequest) RelationAlreadyExists(io.crate.exceptions.RelationAlreadyExists) CreateIndexRequest(org.elasticsearch.action.admin.indices.create.CreateIndexRequest) CreateIndexResponse(org.elasticsearch.action.admin.indices.create.CreateIndexResponse)

Example 3 with RelationAlreadyExists

use of io.crate.exceptions.RelationAlreadyExists in project crate by crate.

the class RestoreSnapshotPlan method bind.

@VisibleForTesting
public static BoundRestoreSnapshot bind(AnalyzedRestoreSnapshot restoreSnapshot, CoordinatorTxnCtx txnCtx, NodeContext nodeCtx, Row parameters, SubQueryResults subQueryResults, Schemas schemas) {
    Function<? super Symbol, Object> eval = x -> SymbolEvaluator.evaluate(txnCtx, nodeCtx, x, parameters, subQueryResults);
    Settings settings = GenericPropertiesConverter.genericPropertiesToSettings(restoreSnapshot.properties().map(eval), SnapshotSettings.SETTINGS);
    HashSet<BoundRestoreSnapshot.RestoreTableInfo> restoreTables = new HashSet<>(restoreSnapshot.tables().size());
    for (Table<Symbol> table : restoreSnapshot.tables()) {
        var relationName = RelationName.of(table.getName(), txnCtx.sessionContext().searchPath().currentSchema());
        try {
            DocTableInfo docTableInfo = schemas.getTableInfo(relationName, Operation.RESTORE_SNAPSHOT);
            if (table.partitionProperties().isEmpty()) {
                throw new RelationAlreadyExists(relationName);
            }
            var partitionName = toPartitionName(docTableInfo, Lists2.map(table.partitionProperties(), x -> x.map(eval)));
            if (docTableInfo.partitions().contains(partitionName)) {
                throw new PartitionAlreadyExistsException(partitionName);
            }
            restoreTables.add(new BoundRestoreSnapshot.RestoreTableInfo(relationName, partitionName));
        } catch (RelationUnknown | SchemaUnknownException e) {
            if (table.partitionProperties().isEmpty()) {
                restoreTables.add(new BoundRestoreSnapshot.RestoreTableInfo(relationName, null));
            } else {
                var partitionName = toPartitionName(relationName, Lists2.map(table.partitionProperties(), x -> x.map(eval)));
                restoreTables.add(new BoundRestoreSnapshot.RestoreTableInfo(relationName, partitionName));
            }
        }
    }
    return new BoundRestoreSnapshot(restoreSnapshot.repository(), restoreSnapshot.snapshot(), restoreTables, restoreSnapshot.includeTables(), restoreSnapshot.includeCustomMetadata(), restoreSnapshot.customMetadataTypes(), restoreSnapshot.includeGlobalSettings(), restoreSnapshot.globalSettings(), settings);
}
Also used : PartitionAlreadyExistsException(io.crate.exceptions.PartitionAlreadyExistsException) IndexParts(io.crate.metadata.IndexParts) RelationName(io.crate.metadata.RelationName) CompletableFuture(java.util.concurrent.CompletableFuture) Operation(io.crate.metadata.table.Operation) GetSnapshotsRequest(org.elasticsearch.action.admin.cluster.snapshots.get.GetSnapshotsRequest) SnapshotInfo(org.elasticsearch.snapshots.SnapshotInfo) Function(java.util.function.Function) PartitionName(io.crate.metadata.PartitionName) ArrayList(java.util.ArrayList) DependencyCarrier(io.crate.planner.DependencyCarrier) HashSet(java.util.HashSet) WAIT_FOR_COMPLETION(io.crate.analyze.SnapshotSettings.WAIT_FOR_COMPLETION) SymbolEvaluator(io.crate.analyze.SymbolEvaluator) Settings(org.elasticsearch.common.settings.Settings) RestoreSnapshotRequest(org.elasticsearch.action.admin.cluster.snapshots.restore.RestoreSnapshotRequest) SchemaUnknownException(io.crate.exceptions.SchemaUnknownException) BoundRestoreSnapshot(io.crate.analyze.BoundRestoreSnapshot) IndicesOptions(org.elasticsearch.action.support.IndicesOptions) GenericPropertiesConverter(io.crate.analyze.GenericPropertiesConverter) OneRowActionListener(io.crate.execution.support.OneRowActionListener) FutureActionListener(io.crate.action.FutureActionListener) PartitionPropertiesAnalyzer.toPartitionName(io.crate.analyze.PartitionPropertiesAnalyzer.toPartitionName) GetSnapshotsResponse(org.elasticsearch.action.admin.cluster.snapshots.get.GetSnapshotsResponse) DocTableInfo(io.crate.metadata.doc.DocTableInfo) AnalyzedRestoreSnapshot(io.crate.analyze.AnalyzedRestoreSnapshot) NodeContext(io.crate.metadata.NodeContext) Table(io.crate.sql.tree.Table) Set(java.util.Set) Lists2(io.crate.common.collections.Lists2) SnapshotSettings(io.crate.analyze.SnapshotSettings) RowConsumer(io.crate.data.RowConsumer) List(java.util.List) Row(io.crate.data.Row) RelationAlreadyExists(io.crate.exceptions.RelationAlreadyExists) Symbol(io.crate.expression.symbol.Symbol) PlannerContext(io.crate.planner.PlannerContext) IGNORE_UNAVAILABLE(io.crate.analyze.SnapshotSettings.IGNORE_UNAVAILABLE) Plan(io.crate.planner.Plan) SubQueryResults(io.crate.planner.operators.SubQueryResults) Schemas(io.crate.metadata.Schemas) VisibleForTesting(io.crate.common.annotations.VisibleForTesting) RelationUnknown(io.crate.exceptions.RelationUnknown) TransportGetSnapshotsAction(org.elasticsearch.action.admin.cluster.snapshots.get.TransportGetSnapshotsAction) Row1(io.crate.data.Row1) CoordinatorTxnCtx(io.crate.metadata.CoordinatorTxnCtx) BoundRestoreSnapshot(io.crate.analyze.BoundRestoreSnapshot) DocTableInfo(io.crate.metadata.doc.DocTableInfo) RelationUnknown(io.crate.exceptions.RelationUnknown) Symbol(io.crate.expression.symbol.Symbol) SchemaUnknownException(io.crate.exceptions.SchemaUnknownException) RelationAlreadyExists(io.crate.exceptions.RelationAlreadyExists) Settings(org.elasticsearch.common.settings.Settings) SnapshotSettings(io.crate.analyze.SnapshotSettings) PartitionAlreadyExistsException(io.crate.exceptions.PartitionAlreadyExistsException) HashSet(java.util.HashSet) VisibleForTesting(io.crate.common.annotations.VisibleForTesting)

Example 4 with RelationAlreadyExists

use of io.crate.exceptions.RelationAlreadyExists in project crate by crate.

the class CreateBlobTableAnalyzer method analyze.

public AnalyzedCreateBlobTable analyze(CreateBlobTable<Expression> node, ParamTypeHints paramTypeHints, CoordinatorTxnCtx txnCtx) {
    var exprAnalyzerWithoutFields = new ExpressionAnalyzer(txnCtx, nodeCtx, paramTypeHints, FieldProvider.UNSUPPORTED, null);
    var exprCtx = new ExpressionAnalysisContext(txnCtx.sessionContext());
    CreateBlobTable<Symbol> createBlobTable = node.map(x -> exprAnalyzerWithoutFields.convert(x, exprCtx));
    RelationName relationName = RelationName.fromBlobTable(createBlobTable.name());
    relationName.ensureValidForRelationCreation();
    if (schemas.tableExists(relationName)) {
        throw new RelationAlreadyExists(relationName);
    }
    return new AnalyzedCreateBlobTable(relationName, createBlobTable);
}
Also used : ExpressionAnalysisContext(io.crate.analyze.expressions.ExpressionAnalysisContext) Symbol(io.crate.expression.symbol.Symbol) ExpressionAnalyzer(io.crate.analyze.expressions.ExpressionAnalyzer) RelationName(io.crate.metadata.RelationName) RelationAlreadyExists(io.crate.exceptions.RelationAlreadyExists)

Aggregations

RelationAlreadyExists (io.crate.exceptions.RelationAlreadyExists)4 RelationName (io.crate.metadata.RelationName)4 Symbol (io.crate.expression.symbol.Symbol)3 Row (io.crate.data.Row)2 Row1 (io.crate.data.Row1)2 RowConsumer (io.crate.data.RowConsumer)2 OneRowActionListener (io.crate.execution.support.OneRowActionListener)2 CoordinatorTxnCtx (io.crate.metadata.CoordinatorTxnCtx)2 NodeContext (io.crate.metadata.NodeContext)2 Schemas (io.crate.metadata.Schemas)2 SubQueryResults (io.crate.planner.operators.SubQueryResults)2 Table (io.crate.sql.tree.Table)2 ArrayList (java.util.ArrayList)2 List (java.util.List)2 FutureActionListener (io.crate.action.FutureActionListener)1 AnalyzedRestoreSnapshot (io.crate.analyze.AnalyzedRestoreSnapshot)1 BoundRestoreSnapshot (io.crate.analyze.BoundRestoreSnapshot)1 CreateViewStmt (io.crate.analyze.CreateViewStmt)1 GenericPropertiesConverter (io.crate.analyze.GenericPropertiesConverter)1 ParamTypeHints (io.crate.analyze.ParamTypeHints)1