Search in sources :

Example 21 with RelationName

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

the class AlterTableAnalyzer method analyze.

AnalyzedAlterTableRename analyze(AlterTableRename<Expression> node, SessionContext sessionContext) {
    if (!node.table().partitionProperties().isEmpty()) {
        throw new UnsupportedOperationException("Renaming a single partition is not supported");
    }
    // we do not support renaming to a different schema, thus the target table identifier must not include a schema
    // this is an artificial limitation, technically it can be done
    List<String> newIdentParts = node.newName().getParts();
    if (newIdentParts.size() > 1) {
        throw new IllegalArgumentException("Target table name must not include a schema");
    }
    RelationName relationName;
    if (node.blob()) {
        relationName = RelationName.fromBlobTable(node.table());
    } else {
        relationName = schemas.resolveRelation(node.table().getName(), sessionContext.searchPath());
    }
    DocTableInfo tableInfo = schemas.getTableInfo(relationName, Operation.ALTER_TABLE_RENAME);
    RelationName newRelationName = new RelationName(relationName.schema(), newIdentParts.get(0));
    newRelationName.ensureValidForRelationCreation();
    return new AnalyzedAlterTableRename(tableInfo, newRelationName);
}
Also used : DocTableInfo(io.crate.metadata.doc.DocTableInfo) RelationName(io.crate.metadata.RelationName)

Example 22 with RelationName

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

the class DocSchemaInfo method tableNames.

private Collection<String> tableNames() {
    Set<String> tables = new HashSet<>();
    extractRelationNamesForSchema(Stream.of(clusterService.state().metadata().getConcreteAllIndices()), schemaName, tables);
    // Search for partitioned table templates
    Iterator<String> templates = clusterService.state().metadata().getTemplates().keysIt();
    while (templates.hasNext()) {
        String templateName = templates.next();
        if (!IndexParts.isPartitioned(templateName)) {
            continue;
        }
        try {
            PartitionName partitionName = PartitionName.fromIndexOrTemplate(templateName);
            RelationName ti = partitionName.relationName();
            if (schemaName.equalsIgnoreCase(ti.schema())) {
                tables.add(ti.name());
            }
        } catch (IllegalArgumentException e) {
        // do nothing
        }
    }
    return tables;
}
Also used : PartitionName(io.crate.metadata.PartitionName) RelationName(io.crate.metadata.RelationName) HashSet(java.util.HashSet)

Example 23 with RelationName

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

the class RenameTableClusterStateExecutor method execute.

public ClusterState execute(ClusterState currentState, RenameTableRequest request) throws Exception {
    RelationName source = request.sourceTableIdent();
    RelationName target = request.targetTableIdent();
    boolean isPartitioned = request.isPartitioned();
    Metadata currentMetadata = currentState.getMetadata();
    Metadata.Builder newMetadata = Metadata.builder(currentMetadata);
    if (isPartitioned) {
        IndexTemplateMetadata indexTemplateMetadata = DDLClusterStateHelpers.templateMetadata(currentMetadata, source);
        if (indexTemplateMetadata == null) {
            throw new IndexTemplateMissingException("Template for partitioned table is missing");
        }
        renameTemplate(newMetadata, indexTemplateMetadata, target);
    }
    RoutingTable.Builder newRoutingTable = RoutingTable.builder(currentState.routingTable());
    ClusterBlocks.Builder blocksBuilder = ClusterBlocks.builder().blocks(currentState.blocks());
    logger.info("renaming table '{}' to '{}'", source.fqn(), target.fqn());
    try {
        Index[] sourceIndices = indexNameExpressionResolver.concreteIndices(currentState, STRICT_INDICES_OPTIONS, source.indexNameOrAlias());
        for (Index sourceIndex : sourceIndices) {
            IndexMetadata sourceIndexMetadata = currentMetadata.getIndexSafe(sourceIndex);
            String sourceIndexName = sourceIndex.getName();
            newMetadata.remove(sourceIndexName);
            newRoutingTable.remove(sourceIndexName);
            blocksBuilder.removeIndexBlocks(sourceIndexName);
            IndexMetadata targetMd;
            if (isPartitioned) {
                PartitionName partitionName = PartitionName.fromIndexOrTemplate(sourceIndexName);
                String targetIndexName = IndexParts.toIndexName(target, partitionName.ident());
                targetMd = IndexMetadata.builder(sourceIndexMetadata).removeAllAliases().putAlias(AliasMetadata.builder(target.indexNameOrAlias()).build()).index(targetIndexName).build();
            } else {
                targetMd = IndexMetadata.builder(sourceIndexMetadata).index(target.indexNameOrAlias()).build();
            }
            newMetadata.put(targetMd, true);
            newRoutingTable.addAsFromCloseToOpen(targetMd);
            blocksBuilder.addBlocks(targetMd);
        }
    } catch (IndexNotFoundException e) {
        if (isPartitioned == false) {
            throw e;
        }
    // empty partition case, no indices, just a template exists
    }
    ClusterState clusterStateAfterRename = ClusterState.builder(currentState).metadata(newMetadata).routingTable(newRoutingTable.build()).blocks(blocksBuilder).build();
    return allocationService.reroute(ddlClusterStateService.onRenameTable(clusterStateAfterRename, source, target, request.isPartitioned()), "rename-table");
}
Also used : ClusterState(org.elasticsearch.cluster.ClusterState) IndexTemplateMissingException(org.elasticsearch.indices.IndexTemplateMissingException) ClusterBlocks(org.elasticsearch.cluster.block.ClusterBlocks) IndexTemplateMetadata(org.elasticsearch.cluster.metadata.IndexTemplateMetadata) IndexMetadata(org.elasticsearch.cluster.metadata.IndexMetadata) IndexTemplateMetadata(org.elasticsearch.cluster.metadata.IndexTemplateMetadata) Metadata(org.elasticsearch.cluster.metadata.Metadata) AliasMetadata(org.elasticsearch.cluster.metadata.AliasMetadata) Index(org.elasticsearch.index.Index) PartitionName(io.crate.metadata.PartitionName) RoutingTable(org.elasticsearch.cluster.routing.RoutingTable) RelationName(io.crate.metadata.RelationName) IndexNotFoundException(org.elasticsearch.index.IndexNotFoundException) IndexMetadata(org.elasticsearch.cluster.metadata.IndexMetadata)

Example 24 with RelationName

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

the class ShardReferenceResolver method createPartitionColumnResolver.

private static ReferenceResolver<NestableInput<?>> createPartitionColumnResolver(Index index, Schemas schemas) {
    PartitionName partitionName;
    try {
        partitionName = PartitionName.fromIndexOrTemplate(index.getName());
    } catch (IllegalArgumentException e) {
        throw new UnhandledServerException(String.format(Locale.ENGLISH, "Unable to load PARTITIONED BY columns from partition %s", index.getName()), e);
    }
    RelationName relationName = partitionName.relationName();
    MapBuilder<ColumnIdent, NestableInput> builder = MapBuilder.newMapBuilder();
    try {
        DocTableInfo info = schemas.getTableInfo(relationName);
        assert info.isPartitioned() : "table must be partitioned";
        int i = 0;
        int numPartitionedColumns = info.partitionedByColumns().size();
        List<String> partitionValue = partitionName.values();
        assert partitionValue.size() == numPartitionedColumns : "invalid number of partitioned columns";
        for (Reference partitionedInfo : info.partitionedByColumns()) {
            builder.put(partitionedInfo.column(), constant(partitionedInfo.valueType().implicitCast(partitionValue.get(i))));
            i++;
        }
    } catch (Exception e) {
        if (e instanceof ResourceUnknownException) {
            LOGGER.error("Orphaned partition '{}' with missing table '{}' found", index, relationName.fqn());
        } else {
            throw e;
        }
    }
    return new MapBackedRefResolver(builder.immutableMap());
}
Also used : NestableInput(io.crate.expression.NestableInput) DocTableInfo(io.crate.metadata.doc.DocTableInfo) Reference(io.crate.metadata.Reference) ResourceUnknownException(io.crate.exceptions.ResourceUnknownException) ResourceUnknownException(io.crate.exceptions.ResourceUnknownException) UnhandledServerException(io.crate.exceptions.UnhandledServerException) PartitionName(io.crate.metadata.PartitionName) ColumnIdent(io.crate.metadata.ColumnIdent) MapBackedRefResolver(io.crate.metadata.MapBackedRefResolver) UnhandledServerException(io.crate.exceptions.UnhandledServerException) RelationName(io.crate.metadata.RelationName)

Example 25 with RelationName

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

the class ViewsMetadata method remove.

public RemoveResult remove(List<RelationName> names) {
    HashMap<String, ViewMetadata> updatedQueryByName = new HashMap<>(this.viewByName);
    ArrayList<RelationName> missing = new ArrayList<>(names.size());
    for (RelationName name : names) {
        ViewMetadata removed = updatedQueryByName.remove(name.fqn());
        if (removed == null) {
            missing.add(name);
        }
    }
    return new RemoveResult(new ViewsMetadata(updatedQueryByName), missing);
}
Also used : HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) RelationName(io.crate.metadata.RelationName)

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