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);
}
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;
}
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");
}
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());
}
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);
}
Aggregations