Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,7 @@
import org.apache.iotdb.confignode.consensus.request.write.table.RenameTableColumnPlan;
import org.apache.iotdb.confignode.consensus.request.write.table.RenameTablePlan;
import org.apache.iotdb.confignode.consensus.request.write.table.RollbackCreateTablePlan;
import org.apache.iotdb.confignode.consensus.request.write.table.RollbackPreAlterColumnDataTypePlan;
import org.apache.iotdb.confignode.consensus.request.write.table.RollbackPreDeleteTablePlan;
import org.apache.iotdb.confignode.consensus.request.write.table.SetTableColumnCommentPlan;
import org.apache.iotdb.confignode.consensus.request.write.table.SetTableCommentPlan;
Expand Down Expand Up @@ -463,6 +464,9 @@ public static ConfigPhysicalPlan create(final ByteBuffer buffer) throws IOExcept
case PreAlterColumnDataType:
plan = new PreAlterColumnDataTypePlan();
break;
case RollbackPreAlterColumnDataType:
plan = new RollbackPreAlterColumnDataTypePlan();
break;
case AlterColumnDataType:
plan = new AlterColumnDataTypePlan();
break;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -233,6 +233,7 @@ public enum ConfigPhysicalPlanType {
AlterColumnDataType((short) 878),
PreAlterColumnDataType((short) 879),
RollbackPreDeleteTable((short) 880),
RollbackPreAlterColumnDataType((short) 881),

/** Deprecated types for sync, restored them for upgrade. */
@Deprecated
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/

package org.apache.iotdb.confignode.consensus.request.write.table;

import org.apache.iotdb.confignode.consensus.request.ConfigPhysicalPlanType;

import org.apache.tsfile.enums.TSDataType;

import java.io.DataOutputStream;
import java.io.IOException;
import java.nio.ByteBuffer;

public class RollbackPreAlterColumnDataTypePlan extends AbstractTableColumnPlan {
private TSDataType newType;

public RollbackPreAlterColumnDataTypePlan() {
super(ConfigPhysicalPlanType.RollbackPreAlterColumnDataType);
}

public RollbackPreAlterColumnDataTypePlan(
final String database,
final String tableName,
final String columnName,
final TSDataType newType) {
super(ConfigPhysicalPlanType.RollbackPreAlterColumnDataType, database, tableName, columnName);
this.newType = newType;
}

@Override
protected void serializeImpl(final DataOutputStream stream) throws IOException {
super.serializeImpl(stream);
stream.write(newType.serialize());
}

@Override
protected void deserializeImpl(final ByteBuffer buffer) throws IOException {
super.deserializeImpl(buffer);
newType = TSDataType.deserializeFrom(buffer);
}

public TSDataType getNewType() {
return newType;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
import org.apache.iotdb.common.rpc.thrift.TSStatus;
import org.apache.iotdb.commons.conf.CommonDescriptor;
import org.apache.iotdb.commons.exception.MetadataException;
import org.apache.iotdb.commons.exception.table.TableInDeletionException;
import org.apache.iotdb.commons.path.PartialPath;
import org.apache.iotdb.commons.path.PathPatternTree;
import org.apache.iotdb.commons.schema.SchemaConstant;
Expand Down Expand Up @@ -1540,13 +1541,34 @@ public Optional<Pair<TsTable, TableNodeStatus>> getTableAndStatusIfExists(
return clusterSchemaInfo.getTsTableIfExists(database, tableName);
}

public boolean isColumnAlterCommitted(
final String database,
final String tableName,
final String columnName,
final TSDataType dataType)
throws MetadataException {
return clusterSchemaInfo.isColumnAlterCommitted(database, tableName, columnName, dataType);
}

public Optional<TSDataType> getPreAlteredColumnType(
final String database, final String tableName, final String columnName)
throws MetadataException {
return clusterSchemaInfo.getPreAlteredColumnType(database, tableName, columnName);
}

public synchronized Pair<TSStatus, TsTable> tableColumnCheckForColumnExtension(
final String database,
final String tableName,
final List<TsTableColumnSchema> columnSchemaList,
final boolean isTableView)
throws MetadataException {
final TsTable originalTable = getTableIfExists(database, tableName).orElse(null);
final TsTable originalTable =
clusterSchemaInfo.getTableForModification(
database,
tableName,
columnSchemaList.stream()
.map(TsTableColumnSchema::getColumnName)
.toArray(String[]::new));

if (Objects.isNull(originalTable)) {
return new Pair<>(
Expand Down Expand Up @@ -1601,7 +1623,7 @@ public synchronized Pair<TSStatus, TsTable> tableColumnCheckForColumnAltering(
final TSDataType dataType,
final boolean isGeneratedByPipe)
throws MetadataException {
final TsTable originalTable = getTableIfExists(database, tableName).orElse(null);
final TsTable originalTable = clusterSchemaInfo.getTableForModification(database, tableName);

if (Objects.isNull(originalTable)) {
return new Pair<>(
Expand Down Expand Up @@ -1638,7 +1660,8 @@ public synchronized Pair<TSStatus, TsTable> tableColumnCheckForColumnRenaming(
final String newName,
final boolean isTableView)
throws MetadataException {
final TsTable originalTable = getTableIfExists(database, tableName).orElse(null);
final TsTable originalTable =
clusterSchemaInfo.getTableForModification(database, tableName, oldName, newName);

if (Objects.isNull(originalTable)) {
return new Pair<>(
Expand Down Expand Up @@ -1691,7 +1714,7 @@ public synchronized Pair<TSStatus, TsTable> tableCheckForRenaming(
final String newName,
final boolean isTableView)
throws MetadataException {
final TsTable originalTable = getTableIfExists(database, tableName).orElse(null);
final TsTable originalTable = clusterSchemaInfo.getTableForModification(database, tableName);

if (Objects.isNull(originalTable)) {
return new Pair<>(
Expand All @@ -1707,7 +1730,12 @@ public synchronized Pair<TSStatus, TsTable> tableCheckForRenaming(
return result.get();
}

if (getTableIfExists(database, newName).isPresent()) {
final Optional<Pair<TsTable, TableNodeStatus>> targetTable =
getTableAndStatusIfExists(database, newName);
if (targetTable.isPresent() && targetTable.get().getRight() == TableNodeStatus.PRE_DELETE) {
throw new TableInDeletionException(database, newName);
}
if (targetTable.isPresent()) {
return new Pair<>(
RpcUtils.getStatus(
TSStatusCode.TABLE_ALREADY_EXISTS,
Expand Down Expand Up @@ -1776,7 +1804,7 @@ public synchronized Pair<TSStatus, TsTable> updateTableProperties(
final Map<String, String> updatedProperties,
final boolean isTableView)
throws MetadataException {
final TsTable originalTable = getTableIfExists(database, tableName).orElse(null);
final TsTable originalTable = clusterSchemaInfo.getTableForModification(database, tableName);

if (Objects.isNull(originalTable)) {
return new Pair<>(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,7 @@
import org.apache.iotdb.confignode.consensus.request.write.table.RenameTableColumnPlan;
import org.apache.iotdb.confignode.consensus.request.write.table.RenameTablePlan;
import org.apache.iotdb.confignode.consensus.request.write.table.RollbackCreateTablePlan;
import org.apache.iotdb.confignode.consensus.request.write.table.RollbackPreAlterColumnDataTypePlan;
import org.apache.iotdb.confignode.consensus.request.write.table.RollbackPreDeleteTablePlan;
import org.apache.iotdb.confignode.consensus.request.write.table.SetTableColumnCommentPlan;
import org.apache.iotdb.confignode.consensus.request.write.table.SetTableCommentPlan;
Expand Down Expand Up @@ -615,6 +616,9 @@ public TSStatus executeNonQueryPlan(ConfigPhysicalPlan physicalPlan)
return clusterSchemaInfo.dropTable((CommitDeleteTablePlan) physicalPlan);
case PreAlterColumnDataType:
return clusterSchemaInfo.preAlterColumnDataType((PreAlterColumnDataTypePlan) physicalPlan);
case RollbackPreAlterColumnDataType:
return clusterSchemaInfo.rollbackPreAlterColumnDataType(
(RollbackPreAlterColumnDataTypePlan) physicalPlan);
case AlterColumnDataType:
return clusterSchemaInfo.commitAlterColumnDataType(
((AlterColumnDataTypePlan) physicalPlan));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,13 +27,17 @@
import org.apache.iotdb.commons.exception.IllegalPathException;
import org.apache.iotdb.commons.exception.MetadataException;
import org.apache.iotdb.commons.exception.SemanticException;
import org.apache.iotdb.commons.exception.table.ColumnInAlterException;
import org.apache.iotdb.commons.exception.table.ColumnInDeletionException;
import org.apache.iotdb.commons.exception.table.TableInDeletionException;
import org.apache.iotdb.commons.path.PartialPath;
import org.apache.iotdb.commons.path.PathPatternTree;
import org.apache.iotdb.commons.schema.table.TableNodeStatus;
import org.apache.iotdb.commons.schema.table.TableType;
import org.apache.iotdb.commons.schema.table.TreeViewSchema;
import org.apache.iotdb.commons.schema.table.TsTable;
import org.apache.iotdb.commons.schema.table.TsTableInternalRPCUtil;
import org.apache.iotdb.commons.schema.table.column.TsTableColumnSchema;
import org.apache.iotdb.commons.schema.template.Template;
import org.apache.iotdb.commons.snapshot.SnapshotProcessor;
import org.apache.iotdb.commons.utils.PathUtils;
Expand Down Expand Up @@ -67,6 +71,7 @@
import org.apache.iotdb.confignode.consensus.request.write.table.RenameTableColumnPlan;
import org.apache.iotdb.confignode.consensus.request.write.table.RenameTablePlan;
import org.apache.iotdb.confignode.consensus.request.write.table.RollbackCreateTablePlan;
import org.apache.iotdb.confignode.consensus.request.write.table.RollbackPreAlterColumnDataTypePlan;
import org.apache.iotdb.confignode.consensus.request.write.table.RollbackPreDeleteTablePlan;
import org.apache.iotdb.confignode.consensus.request.write.table.SetTableColumnCommentPlan;
import org.apache.iotdb.confignode.consensus.request.write.table.SetTableCommentPlan;
Expand Down Expand Up @@ -109,6 +114,7 @@
import org.apache.iotdb.rpc.TSStatusCode;

import org.apache.tsfile.annotations.TableModel;
import org.apache.tsfile.enums.TSDataType;
import org.apache.tsfile.utils.Pair;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
Expand Down Expand Up @@ -1352,9 +1358,11 @@ public ShowTableResp showTables(final ShowTablePlan plan) {
})
.collect(Collectors.toList())
: tableModelMTree
.getAllUsingTablesUnderSpecificDatabase(
.getAllTablesUnderSpecificDatabase(
getQualifiedDatabasePartialPath(plan.getDatabase()))
.stream()
.filter(pair -> pair.getRight() != TableNodeStatus.PRE_CREATE)
.map(Pair::getLeft)
.map(
tsTable ->
new TTableInfo(
Expand Down Expand Up @@ -1447,7 +1455,7 @@ public DescTableResp descTable(final DescTablePlan plan) {
}
return new DescTableResp(
StatusUtils.OK,
tableModelMTree.getUsingTableSchema(databasePath, plan.getTableName()),
tableModelMTree.getTableSchemaForDesc(databasePath, plan.getTableName()),
null,
null);
} catch (final MetadataException e) {
Expand Down Expand Up @@ -1557,6 +1565,80 @@ public Optional<Pair<TsTable, TableNodeStatus>> getTsTableIfExists(
}
}

public TsTable getTableForModification(
final String database, final String tableName, final String... columnNames)
throws MetadataException {
databaseReadWriteLock.readLock().lock();
try {
final PartialPath databasePath = getQualifiedDatabasePartialPath(database);
final Optional<Pair<TsTable, TableNodeStatus>> tableAndStatus =
tableModelMTree.getTableAndStatusIfExists(databasePath, tableName);
if (!tableAndStatus.isPresent()) {
return null;
}
if (tableAndStatus.get().getRight() == TableNodeStatus.PRE_DELETE) {
throw new TableInDeletionException(database, tableName);
}
final TableSchemaDetails details =
tableModelMTree.getTableSchemaDetails(databasePath, tableName);
for (final String columnName : columnNames) {
if (details.preDeletedColumns.contains(columnName)) {
throw new ColumnInDeletionException(database, tableName, columnName);
}
if (details.preAlteredColumns.containsKey(columnName)) {
throw new ColumnInAlterException(database, tableName, columnName);
}
}
return tableModelMTree.getTableSchemaForDataNode(databasePath, tableName);
} finally {
databaseReadWriteLock.readLock().unlock();
}
}

public boolean isColumnAlterCommitted(
final String database,
final String tableName,
final String columnName,
final TSDataType dataType)
throws MetadataException {
databaseReadWriteLock.readLock().lock();
try {
final PartialPath databasePath = getQualifiedDatabasePartialPath(database);
final Optional<Pair<TsTable, TableNodeStatus>> tableAndStatus =
tableModelMTree.getTableAndStatusIfExists(databasePath, tableName);
if (!tableAndStatus.isPresent()) {
return false;
}
final TableSchemaDetails details =
tableModelMTree.getTableSchemaDetails(databasePath, tableName);
final TsTableColumnSchema columnSchema = details.table.getColumnSchema(columnName);
return !details.preAlteredColumns.containsKey(columnName)
&& columnSchema != null
&& columnSchema.getDataType() == dataType;
} finally {
databaseReadWriteLock.readLock().unlock();
}
}

public Optional<TSDataType> getPreAlteredColumnType(
final String database, final String tableName, final String columnName)
throws MetadataException {
databaseReadWriteLock.readLock().lock();
try {
final PartialPath databasePath = getQualifiedDatabasePartialPath(database);
if (!tableModelMTree.getTableAndStatusIfExists(databasePath, tableName).isPresent()) {
return Optional.empty();
}
return Optional.ofNullable(
tableModelMTree
.getTableSchemaDetails(databasePath, tableName)
.preAlteredColumns
.get(columnName));
} finally {
databaseReadWriteLock.readLock().unlock();
}
}

public TSStatus addTableColumn(final AddTableColumnPlan plan) {
return executeWithLock(
() -> {
Expand Down Expand Up @@ -1644,6 +1726,23 @@ public TSStatus preAlterColumnDataType(final PreAlterColumnDataTypePlan plan) {
}
}

public TSStatus rollbackPreAlterColumnDataType(final RollbackPreAlterColumnDataTypePlan plan) {
databaseReadWriteLock.writeLock().lock();
try {
tableModelMTree.rollbackPreAlterColumnDataType(
getQualifiedDatabasePartialPath(plan.getDatabase()),
plan.getTableName(),
plan.getColumnName(),
plan.getNewType());
return RpcUtils.SUCCESS_STATUS;
} catch (final MetadataException e) {
LOGGER.warn(e.getMessage(), e);
return RpcUtils.getStatus(e.getErrorCode(), e.getMessage());
} finally {
databaseReadWriteLock.writeLock().unlock();
}
}

public TSStatus commitAlterColumnDataType(AlterColumnDataTypePlan plan) {
databaseReadWriteLock.writeLock().lock();
try {
Expand Down
Loading
Loading