Search in sources :

Example 91 with RelationName

use of io.crate.metadata.RelationName in project crate by crate.

the class AlterTableOperation method executeAlterTableRenameTable.

public CompletableFuture<Long> executeAlterTableRenameTable(AnalyzedAlterTableRename statement) {
    DocTableInfo sourceTableInfo = statement.sourceTableInfo();
    RelationName sourceRelationName = sourceTableInfo.ident();
    RelationName targetRelationName = statement.targetTableIdent();
    return renameTable(sourceRelationName, targetRelationName, sourceTableInfo.isPartitioned());
}
Also used : DocTableInfo(io.crate.metadata.doc.DocTableInfo) RelationName(io.crate.metadata.RelationName)

Example 92 with RelationName

use of io.crate.metadata.RelationName in project crate by crate.

the class AlterTableOperation method executeAlterTableAddColumn.

public CompletableFuture<Long> executeAlterTableAddColumn(final BoundAddColumn analysis) {
    FutureActionListener<AcknowledgedResponse, Long> result = new FutureActionListener<>(r -> -1L);
    if (analysis.newPrimaryKeys() || analysis.hasNewGeneratedColumns()) {
        RelationName ident = analysis.table().ident();
        String stmt = String.format(Locale.ENGLISH, "SELECT COUNT(*) FROM \"%s\".\"%s\"", ident.schema(), ident.name());
        try {
            session().quickExec(stmt, new ResultSetReceiver(analysis, result), Row.EMPTY);
        } catch (Throwable t) {
            result.completeExceptionally(t);
        }
    } else {
        return addColumnToTable(analysis, result);
    }
    return result;
}
Also used : AcknowledgedResponse(org.elasticsearch.action.support.master.AcknowledgedResponse) RelationName(io.crate.metadata.RelationName) FutureActionListener(io.crate.action.FutureActionListener)

Example 93 with RelationName

use of io.crate.metadata.RelationName 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 94 with RelationName

use of io.crate.metadata.RelationName in project crate by crate.

the class SwapRelationsOperation method applyDropRelations.

private UpdatedState applyDropRelations(ClusterState state, UpdatedState updatedState, List<RelationName> dropRelations) {
    ClusterState stateAfterRename = updatedState.newState;
    Metadata.Builder updatedMetadata = Metadata.builder(stateAfterRename.metadata());
    RoutingTable.Builder routingBuilder = RoutingTable.builder(stateAfterRename.routingTable());
    for (RelationName dropRelation : dropRelations) {
        for (Index index : indexNameResolver.concreteIndices(state, IndicesOptions.LENIENT_EXPAND_OPEN, dropRelation.indexNameOrAlias())) {
            String indexName = index.getName();
            updatedMetadata.remove(indexName);
            routingBuilder.remove(indexName);
            updatedState.newIndices.remove(indexName);
        }
    }
    ClusterState stateAfterDropRelations = ClusterState.builder(stateAfterRename).metadata(updatedMetadata).routingTable(routingBuilder.build()).build();
    return new UpdatedState(allocationService.reroute(applyDropTableClusterStateModifiers(stateAfterDropRelations, dropRelations), "indices drop after name switch"), updatedState.newIndices);
}
Also used : ClusterState(org.elasticsearch.cluster.ClusterState) RoutingTable(org.elasticsearch.cluster.routing.RoutingTable) IndexMetadata(org.elasticsearch.cluster.metadata.IndexMetadata) IndexTemplateMetadata(org.elasticsearch.cluster.metadata.IndexTemplateMetadata) Metadata(org.elasticsearch.cluster.metadata.Metadata) AliasMetadata(org.elasticsearch.cluster.metadata.AliasMetadata) RelationName(io.crate.metadata.RelationName) Index(org.elasticsearch.index.Index)

Example 95 with RelationName

use of io.crate.metadata.RelationName in project crate by crate.

the class JoinOperations method convertImplicitJoinConditionsToJoinPairs.

/**
 * Converts any implicit join conditions of the WHERE clause to explicit {@link JoinPair}.
 * Every join condition that gets to be converted is removed from the {@code splitQueries}
 *
 * @param explicitJoinPairs The explicitJoinPairs as originally written in the query
 * @param splitQueries      The remaining queries of the WHERE clause split by involved relations
 * @return the new list of {@link JoinPair}
 */
public static List<JoinPair> convertImplicitJoinConditionsToJoinPairs(List<JoinPair> explicitJoinPairs, Map<Set<RelationName>, Symbol> splitQueries) {
    Iterator<Map.Entry<Set<RelationName>, Symbol>> queryIterator = splitQueries.entrySet().iterator();
    ArrayList<JoinPair> newJoinPairs = new ArrayList<>(explicitJoinPairs.size() + splitQueries.size());
    newJoinPairs.addAll(explicitJoinPairs);
    while (queryIterator.hasNext()) {
        Map.Entry<Set<RelationName>, Symbol> queryEntry = queryIterator.next();
        Set<RelationName> relations = queryEntry.getKey();
        if (relations.size() == 2) {
            // If more than 2 relations are involved it cannot be converted to a JoinPair
            Symbol implicitJoinCondition = queryEntry.getValue();
            JoinPair newJoinPair = null;
            int existingJoinPairIdx = -1;
            for (int i = 0; i < explicitJoinPairs.size(); i++) {
                JoinPair joinPair = explicitJoinPairs.get(i);
                if (relations.contains(joinPair.left()) && relations.contains(joinPair.right())) {
                    existingJoinPairIdx = i;
                    // 
                    if (joinPair.joinType() == JoinType.INNER || joinPair.joinType() == JoinType.CROSS) {
                        newJoinPair = JoinPair.of(joinPair.left(), joinPair.right(), JoinType.INNER, mergeJoinConditions(joinPair.condition(), implicitJoinCondition));
                        queryIterator.remove();
                    } else {
                        newJoinPair = joinPair;
                    }
                }
            }
            if (newJoinPair == null) {
                Iterator<RelationName> namesIter = relations.iterator();
                newJoinPair = JoinPair.of(namesIter.next(), namesIter.next(), JoinType.INNER, implicitJoinCondition);
                queryIterator.remove();
                newJoinPairs.add(newJoinPair);
            } else {
                newJoinPairs.set(existingJoinPairIdx, newJoinPair);
            }
        }
    }
    return newJoinPairs;
}
Also used : Set(java.util.Set) HashSet(java.util.HashSet) Symbol(io.crate.expression.symbol.Symbol) ArrayList(java.util.ArrayList) JoinPair(io.crate.analyze.relations.JoinPair) RelationName(io.crate.metadata.RelationName) LinkedHashMap(java.util.LinkedHashMap) Map(java.util.Map)

Aggregations

RelationName (io.crate.metadata.RelationName)180 Test (org.junit.Test)100 CrateDummyClusterServiceUnitTest (io.crate.test.integration.CrateDummyClusterServiceUnitTest)55 PartitionName (io.crate.metadata.PartitionName)47 Symbol (io.crate.expression.symbol.Symbol)42 Reference (io.crate.metadata.Reference)37 ColumnIdent (io.crate.metadata.ColumnIdent)31 AnalyzedRelation (io.crate.analyze.relations.AnalyzedRelation)21 ReferenceIdent (io.crate.metadata.ReferenceIdent)21 DocTableInfo (io.crate.metadata.doc.DocTableInfo)21 Map (java.util.Map)20 HashMap (java.util.HashMap)19 SqlExpressions (io.crate.testing.SqlExpressions)18 ArrayList (java.util.ArrayList)18 List (java.util.List)17 Before (org.junit.Before)17 DocTableRelation (io.crate.analyze.relations.DocTableRelation)13 SQLExecutor (io.crate.testing.SQLExecutor)11 CoordinatorTxnCtx (io.crate.metadata.CoordinatorTxnCtx)10 IndexTemplateMetadata (org.elasticsearch.cluster.metadata.IndexTemplateMetadata)10