You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@iotdb.apache.org by ja...@apache.org on 2024/08/16 04:16:26 UTC
(iotdb) branch master updated: Support alter table add column / set properties if exists & default string type for id / attribute columns
This is an automated email from the ASF dual-hosted git repository.
jackietien pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/iotdb.git
The following commit(s) were added to refs/heads/master by this push:
new b93348d3ccb Support alter table add column / set properties if exists & default string type for id / attribute columns
b93348d3ccb is described below
commit b93348d3ccbb0f44ab846285b33c6d7ea681c041
Author: Caideyipi <87...@users.noreply.github.com>
AuthorDate: Fri Aug 16 12:16:21 2024 +0800
Support alter table add column / set properties if exists & default string type for id / attribute columns
---
.../iotdb/relational/it/schema/IoTDBTableIT.java | 68 ++++-
.../java/org/apache/iotdb/rpc/TSStatusCode.java | 1 +
.../consensus/request/ConfigPhysicalPlan.java | 4 +
.../consensus/request/ConfigPhysicalPlanType.java | 1 +
.../write/table/SetTablePropertiesPlan.java | 79 +++++
.../iotdb/confignode/manager/ConfigManager.java | 2 +
.../iotdb/confignode/manager/ProcedureManager.java | 282 +++++++++---------
.../manager/schema/ClusterSchemaManager.java | 91 +++++-
.../persistence/executor/ConfigPlanExecutor.java | 3 +
.../persistence/schema/ClusterSchemaInfo.java | 30 +-
.../confignode/persistence/schema/ConfigMTree.java | 71 +++--
.../procedure/impl/schema/SchemaUtils.java | 70 +++++
.../impl/schema/table/AddTableColumnProcedure.java | 145 ++++-----
.../impl/schema/table/CreateTableProcedure.java | 130 +++------
.../schema/table/SetTablePropertiesProcedure.java | 325 +++++++++++++++++++++
.../state/schema/SetTablePropertiesState.java} | 17 +-
.../procedure/store/ProcedureFactory.java | 6 +
.../confignode/procedure/store/ProcedureType.java | 1 +
.../request/ConfigPhysicalPlanSerDeTest.java | 16 +
.../table/SetTablePropertiesProcedureTest.java | 63 ++++
.../table/TableAlreadyExistsException.java | 2 +-
.../metadata/table/TableNotExistsException.java | 2 +-
.../iotdb/db/queryengine/plan/Coordinator.java | 4 +
.../execution/config/TableConfigTaskVisitor.java | 119 ++++++--
.../config/executor/ClusterConfigTaskExecutor.java | 69 ++++-
.../config/executor/IConfigTaskExecutor.java | 31 +-
.../relational/AlterTableAddColumnTask.java | 70 +----
...eTask.java => AlterTableSetPropertiesTask.java} | 31 +-
.../metadata/relational/CreateTableTask.java | 4 +-
.../relational/analyzer/StatementAnalyzer.java | 30 +-
.../fetcher/TableHeaderSchemaValidator.java | 96 ++++--
.../plan/relational/sql/ast/AddColumn.java | 59 +++-
.../plan/relational/sql/ast/ColumnDefinition.java | 30 +-
.../plan/relational/sql/ast/CreateTable.java | 28 +-
.../sql/ast/DefaultTraversalVisitor.java | 10 +-
.../plan/relational/sql/ast/SetProperties.java | 29 +-
.../plan/relational/sql/parser/AstBuilder.java | 21 +-
.../plan/relational/sql/util/SqlFormatter.java | 24 +-
.../schema/table/AlterTableOperationType.java | 9 +-
.../apache/iotdb/commons/schema/table/TsTable.java | 14 +-
.../db/relational/grammar/sql/RelationalSql.g4 | 13 +-
41 files changed, 1492 insertions(+), 608 deletions(-)
diff --git a/integration-test/src/test/java/org/apache/iotdb/relational/it/schema/IoTDBTableIT.java b/integration-test/src/test/java/org/apache/iotdb/relational/it/schema/IoTDBTableIT.java
index dd930228612..982b056d981 100644
--- a/integration-test/src/test/java/org/apache/iotdb/relational/it/schema/IoTDBTableIT.java
+++ b/integration-test/src/test/java/org/apache/iotdb/relational/it/schema/IoTDBTableIT.java
@@ -88,8 +88,10 @@ public class IoTDBTableIT {
// or use full qualified table name
// test "TTL=INF"
+ // "MEASUREMENT" can be omitted when type is specified
+ // "STRING" can be omitted when id/attribute is specified
statement.execute(
- "create table test1.table1(region_id STRING ID, plant_id STRING ID, device_id STRING ID, model STRING ATTRIBUTE, temperature FLOAT MEASUREMENT, humidity DOUBLE MEASUREMENT) with (TTL='INF')");
+ "create table test1.table1(region_id STRING ID, plant_id STRING ID, device_id ID, model STRING ATTRIBUTE, temperature FLOAT MEASUREMENT, humidity DOUBLE) with (TTL='INF')");
try {
statement.execute(
@@ -123,6 +125,27 @@ public class IoTDBTableIT {
assertEquals(tableNames.length, cnt);
}
+ // Alter table properties
+ statement.execute("alter table test1.table1 set properties ttl=1");
+ ttls = new String[] {"1"};
+
+ // Alter non-exist table
+ try {
+ statement.execute("alter table test1.nonExist set properties ttl=1");
+ } catch (final SQLException e) {
+ assertEquals("550: Table 'test1.nonexist' does not exist", e.getMessage());
+ }
+
+ // If exists
+ statement.execute("alter table if exists test1.nonExist set properties ttl=1");
+
+ // Alter non-supported properties
+ try {
+ statement.execute("alter table test1.table1 set properties nonSupport=1");
+ } catch (final SQLException e) {
+ assertEquals("701: Table property 'nonsupport' is currently not allowed.", e.getMessage());
+ }
+
// using SHOW tables from
try (final ResultSet resultSet = statement.executeQuery("SHOW tables from test1")) {
int cnt = 0;
@@ -140,6 +163,27 @@ public class IoTDBTableIT {
assertEquals(tableNames.length, cnt);
}
+ // Set back to default
+ statement.execute("alter table test1.table1 set properties ttl=DEFAULT");
+ ttls = new String[] {"INF"};
+
+ try (final ResultSet resultSet = statement.executeQuery("SHOW tables from test1")) {
+ int cnt = 0;
+ final ResultSetMetaData metaData = resultSet.getMetaData();
+ assertEquals(showTablesColumnHeaders.size(), metaData.getColumnCount());
+ for (int i = 0; i < showTablesColumnHeaders.size(); i++) {
+ assertEquals(
+ showTablesColumnHeaders.get(i).getColumnName(), metaData.getColumnName(i + 1));
+ }
+ while (resultSet.next()) {
+ assertEquals(tableNames[cnt], resultSet.getString(1));
+ assertEquals(ttls[cnt], resultSet.getString(2));
+ cnt++;
+ }
+ assertEquals(tableNames.length, cnt);
+ }
+
+ // Create if not exist
statement.execute(
"create table if not exists test1.table1(region_id STRING ID, plant_id STRING ID, device_id STRING ID, model STRING ATTRIBUTE, temperature FLOAT MEASUREMENT, humidity DOUBLE MEASUREMENT)");
@@ -157,7 +201,7 @@ public class IoTDBTableIT {
"create table table2(region_id STRING ID, plant_id STRING ID, device_id STRING ID, model STRING ATTRIBUTE, temperature FLOAT MEASUREMENT, humidity DOUBLE MEASUREMENT) with (UNKNOWN=3600000)");
fail();
} catch (final SQLException e) {
- assertEquals("701: Table property unknown is currently not allowed.", e.getMessage());
+ assertEquals("701: Table property 'unknown' is currently not allowed.", e.getMessage());
}
try {
@@ -218,7 +262,25 @@ public class IoTDBTableIT {
}
statement.execute(
- "create table table2(region_id STRING ID, plant_id STRING ID, color STRING ATTRIBUTE, temperature FLOAT MEASUREMENT, speed DOUBLE MEASUREMENT) with (TTL=6600000)");
+ "create table table2(region_id STRING ID, plant_id STRING ID, color STRING ATTRIBUTE, temperature FLOAT MEASUREMENT) with (TTL=6600000)");
+
+ statement.execute("alter table table2 add column speed DOUBLE MEASUREMENT");
+
+ try {
+ statement.execute("alter table table2 add column speed DOUBLE MEASUREMENT");
+ } catch (final SQLException e) {
+ assertEquals("552: Column 'speed' already exist", e.getMessage());
+ }
+
+ statement.execute("alter table table2 add column if not exists speed DOUBLE MEASUREMENT");
+
+ try {
+ statement.execute("alter table table3 add column speed DOUBLE MEASUREMENT");
+ } catch (final SQLException e) {
+ assertEquals("550: Table 'test2.table3' does not exist", e.getMessage());
+ }
+
+ statement.execute("alter table if exists table3 add column speed DOUBLE MEASUREMENT");
tableNames = new String[] {"table2"};
ttls = new String[] {"6600000"};
diff --git a/iotdb-client/service-rpc/src/main/java/org/apache/iotdb/rpc/TSStatusCode.java b/iotdb-client/service-rpc/src/main/java/org/apache/iotdb/rpc/TSStatusCode.java
index d37513213b9..cb2e8220e90 100644
--- a/iotdb-client/service-rpc/src/main/java/org/apache/iotdb/rpc/TSStatusCode.java
+++ b/iotdb-client/service-rpc/src/main/java/org/apache/iotdb/rpc/TSStatusCode.java
@@ -89,6 +89,7 @@ public enum TSStatusCode {
TABLE_NOT_EXISTS(550),
TABLE_ALREADY_EXISTS(551),
+ COLUMN_ALREADY_EXISTS(552),
ONLY_LOGICAL_VIEW(560),
// Storage Engine
diff --git a/iotdb-core/confignode/src/main/java/org/apache/iotdb/confignode/consensus/request/ConfigPhysicalPlan.java b/iotdb-core/confignode/src/main/java/org/apache/iotdb/confignode/consensus/request/ConfigPhysicalPlan.java
index 7ac66ac2219..00e7345f650 100644
--- a/iotdb-core/confignode/src/main/java/org/apache/iotdb/confignode/consensus/request/ConfigPhysicalPlan.java
+++ b/iotdb-core/confignode/src/main/java/org/apache/iotdb/confignode/consensus/request/ConfigPhysicalPlan.java
@@ -119,6 +119,7 @@ import org.apache.iotdb.confignode.consensus.request.write.table.AddTableColumnP
import org.apache.iotdb.confignode.consensus.request.write.table.CommitCreateTablePlan;
import org.apache.iotdb.confignode.consensus.request.write.table.PreCreateTablePlan;
import org.apache.iotdb.confignode.consensus.request.write.table.RollbackCreateTablePlan;
+import org.apache.iotdb.confignode.consensus.request.write.table.SetTablePropertiesPlan;
import org.apache.iotdb.confignode.consensus.request.write.template.CommitSetSchemaTemplatePlan;
import org.apache.iotdb.confignode.consensus.request.write.template.CreateSchemaTemplatePlan;
import org.apache.iotdb.confignode.consensus.request.write.template.DropSchemaTemplatePlan;
@@ -412,6 +413,9 @@ public abstract class ConfigPhysicalPlan implements IConsensusRequest {
case AddTableColumn:
plan = new AddTableColumnPlan();
break;
+ case SetTableProperties:
+ plan = new SetTablePropertiesPlan();
+ break;
case GetNodePathsPartition:
plan = new GetNodePathsPartitionPlan();
break;
diff --git a/iotdb-core/confignode/src/main/java/org/apache/iotdb/confignode/consensus/request/ConfigPhysicalPlanType.java b/iotdb-core/confignode/src/main/java/org/apache/iotdb/confignode/consensus/request/ConfigPhysicalPlanType.java
index dd91aef20c2..a77a21007de 100644
--- a/iotdb-core/confignode/src/main/java/org/apache/iotdb/confignode/consensus/request/ConfigPhysicalPlanType.java
+++ b/iotdb-core/confignode/src/main/java/org/apache/iotdb/confignode/consensus/request/ConfigPhysicalPlanType.java
@@ -153,6 +153,7 @@ public enum ConfigPhysicalPlanType {
RollbackCreateTable((short) 851),
CommitCreateTable((short) 852),
AddTableColumn((short) 853),
+ SetTableProperties((short) 854),
/** Deprecated types for sync, restored them for upgrade. */
@Deprecated
diff --git a/iotdb-core/confignode/src/main/java/org/apache/iotdb/confignode/consensus/request/write/table/SetTablePropertiesPlan.java b/iotdb-core/confignode/src/main/java/org/apache/iotdb/confignode/consensus/request/write/table/SetTablePropertiesPlan.java
new file mode 100644
index 00000000000..88f3eb89871
--- /dev/null
+++ b/iotdb-core/confignode/src/main/java/org/apache/iotdb/confignode/consensus/request/write/table/SetTablePropertiesPlan.java
@@ -0,0 +1,79 @@
+/*
+ * 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.ConfigPhysicalPlan;
+import org.apache.iotdb.confignode.consensus.request.ConfigPhysicalPlanType;
+
+import org.apache.tsfile.utils.ReadWriteIOUtils;
+
+import java.io.DataOutputStream;
+import java.io.IOException;
+import java.nio.ByteBuffer;
+import java.util.Map;
+
+public class SetTablePropertiesPlan extends ConfigPhysicalPlan {
+
+ private String database;
+
+ private String tableName;
+
+ private Map<String, String> properties;
+
+ public SetTablePropertiesPlan() {
+ super(ConfigPhysicalPlanType.SetTableProperties);
+ }
+
+ public SetTablePropertiesPlan(
+ final String database, final String tableName, final Map<String, String> properties) {
+ super(ConfigPhysicalPlanType.SetTableProperties);
+ this.database = database;
+ this.tableName = tableName;
+ this.properties = properties;
+ }
+
+ public String getDatabase() {
+ return database;
+ }
+
+ public String getTableName() {
+ return tableName;
+ }
+
+ public Map<String, String> getProperties() {
+ return properties;
+ }
+
+ @Override
+ protected void serializeImpl(final DataOutputStream stream) throws IOException {
+ stream.writeShort(getType().getPlanType());
+
+ ReadWriteIOUtils.write(database, stream);
+ ReadWriteIOUtils.write(tableName, stream);
+ ReadWriteIOUtils.write(properties, stream);
+ }
+
+ @Override
+ protected void deserializeImpl(final ByteBuffer buffer) throws IOException {
+ this.database = ReadWriteIOUtils.readString(buffer);
+ this.tableName = ReadWriteIOUtils.readString(buffer);
+ this.properties = ReadWriteIOUtils.readMap(buffer);
+ }
+}
diff --git a/iotdb-core/confignode/src/main/java/org/apache/iotdb/confignode/manager/ConfigManager.java b/iotdb-core/confignode/src/main/java/org/apache/iotdb/confignode/manager/ConfigManager.java
index 270fca3ce12..524d3f4cc4f 100644
--- a/iotdb-core/confignode/src/main/java/org/apache/iotdb/confignode/manager/ConfigManager.java
+++ b/iotdb-core/confignode/src/main/java/org/apache/iotdb/confignode/manager/ConfigManager.java
@@ -2438,6 +2438,8 @@ public class ConfigManager implements IManager {
switch (AlterTableOperationType.getType(req.operationType)) {
case ADD_COLUMN:
return procedureManager.alterTableAddColumn(req);
+ case SET_PROPERTIES:
+ return procedureManager.alterTableSetProperties(req);
default:
throw new IllegalArgumentException();
}
diff --git a/iotdb-core/confignode/src/main/java/org/apache/iotdb/confignode/manager/ProcedureManager.java b/iotdb-core/confignode/src/main/java/org/apache/iotdb/confignode/manager/ProcedureManager.java
index 71f363facc0..7d0dea92186 100644
--- a/iotdb-core/confignode/src/main/java/org/apache/iotdb/confignode/manager/ProcedureManager.java
+++ b/iotdb-core/confignode/src/main/java/org/apache/iotdb/confignode/manager/ProcedureManager.java
@@ -82,6 +82,7 @@ import org.apache.iotdb.confignode.procedure.impl.schema.SetTemplateProcedure;
import org.apache.iotdb.confignode.procedure.impl.schema.UnsetTemplateProcedure;
import org.apache.iotdb.confignode.procedure.impl.schema.table.AddTableColumnProcedure;
import org.apache.iotdb.confignode.procedure.impl.schema.table.CreateTableProcedure;
+import org.apache.iotdb.confignode.procedure.impl.schema.table.SetTablePropertiesProcedure;
import org.apache.iotdb.confignode.procedure.impl.subscription.consumer.CreateConsumerProcedure;
import org.apache.iotdb.confignode.procedure.impl.subscription.consumer.DropConsumerProcedure;
import org.apache.iotdb.confignode.procedure.impl.subscription.consumer.runtime.ConsumerGroupMetaSyncProcedure;
@@ -124,6 +125,7 @@ import org.apache.iotdb.rpc.TSStatusCode;
import org.apache.tsfile.utils.Binary;
import org.apache.tsfile.utils.Pair;
+import org.apache.tsfile.utils.ReadWriteIOUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
@@ -138,6 +140,7 @@ import java.util.Map;
import java.util.Objects;
import java.util.Optional;
import java.util.concurrent.ScheduledExecutorService;
+import java.util.concurrent.locks.ReentrantLock;
import java.util.stream.Collectors;
import static org.apache.iotdb.confignode.conf.ConfigNodeConstant.REGION_MIGRATE_PROCESS;
@@ -163,6 +166,8 @@ public class ProcedureManager {
private final long planSizeLimit;
private ProcedureMetrics procedureMetrics;
+ private final ReentrantLock tableLock = new ReentrantLock();
+
public ProcedureManager(ConfigManager configManager, ProcedureInfo procedureInfo) {
this.configManager = configManager;
this.scheduler = new SimpleProcedureScheduler();
@@ -222,39 +227,15 @@ public class ProcedureManager {
synchronized (this) {
while (executor.isRunning()
&& System.currentTimeMillis() - startCheckTimeForProcedures < PROCEDURE_WAIT_TIME_OUT) {
- ProcedureType type;
- for (final Procedure<?> procedure : executor.getProcedures().values()) {
- type = ProcedureFactory.getProcedureType(procedure);
- if (type == null) {
- continue;
- }
- // A table shall not be concurrently operated or else the dataNode cache
- // may record fake values
- switch (type) {
- case CREATE_TABLE_PROCEDURE:
- final CreateTableProcedure createTableProcedure = (CreateTableProcedure) procedure;
- if (databaseSchema.getName().equals(createTableProcedure.getDatabase())) {
- hasOverlappedTask = true;
- break;
- }
- break;
- case ADD_TABLE_COLUMN_PROCEDURE:
- final AddTableColumnProcedure addTableColumnProcedure =
- (AddTableColumnProcedure) procedure;
- if (database.equals(addTableColumnProcedure.getDatabase())) {
- hasOverlappedTask = true;
- break;
- }
- break;
- default:
- break;
- }
- }
- if (!hasOverlappedTask) {
+ final Pair<Long, Boolean> procedureIdDuplicatePair =
+ awaitDuplicateTableTask(
+ database, null, null, null, ProcedureType.CREATE_TABLE_PROCEDURE);
+ hasOverlappedTask = procedureIdDuplicatePair.getRight();
+
+ if (Boolean.FALSE.equals(procedureIdDuplicatePair.getRight())) {
final DeleteDatabaseProcedure deleteDatabaseProcedure =
new DeleteDatabaseProcedure(databaseSchema, isGeneratedByPipe);
- final long procedureId = this.executor.submitProcedure(deleteDatabaseProcedure);
- procedureIds.add(procedureId);
+ procedureIds.add(this.executor.submitProcedure(deleteDatabaseProcedure));
break;
}
try {
@@ -263,13 +244,13 @@ public class ProcedureManager {
Thread.currentThread().interrupt();
}
}
- }
- if (hasOverlappedTask) {
- return RpcUtils.getStatus(
- TSStatusCode.OVERLAP_WITH_EXISTING_TASK,
- String.format(
- "Some other task is operating table under the database %s, please retry after the procedure finishes.",
- database));
+ if (hasOverlappedTask) {
+ return RpcUtils.getStatus(
+ TSStatusCode.OVERLAP_WITH_EXISTING_TASK,
+ String.format(
+ "Some other task is operating table under the database %s, please retry after the procedure finishes.",
+ database));
+ }
}
}
final List<TSStatus> procedureStatus = new ArrayList<>();
@@ -1308,64 +1289,21 @@ public class ProcedureManager {
}
public TSStatus createTable(final String database, final TsTable table) {
- long procedureId = -1;
- synchronized (this) {
- boolean hasOverlappedTask = false;
- ProcedureType type;
- for (final Procedure<?> procedure : executor.getProcedures().values()) {
- type = ProcedureFactory.getProcedureType(procedure);
- if (type == null) {
- continue;
- }
- // A table shall not be concurrently operated or else the dataNode cache
- // may record fake values
- switch (type) {
- case CREATE_TABLE_PROCEDURE:
- final CreateTableProcedure createTableProcedure = (CreateTableProcedure) procedure;
- if (database.equals(createTableProcedure.getDatabase())
- && table.equals(createTableProcedure.getTable())) {
- procedureId = createTableProcedure.getProcId();
- break;
- }
- if (database.equals(createTableProcedure.getDatabase())
- && table.getTableName().equals(createTableProcedure.getTable().getTableName())) {
- hasOverlappedTask = true;
- break;
- }
- break;
- case ADD_TABLE_COLUMN_PROCEDURE:
- final AddTableColumnProcedure addTableColumnProcedure =
- (AddTableColumnProcedure) procedure;
- if (database.equals(addTableColumnProcedure.getDatabase())
- && table.getTableName().equals(addTableColumnProcedure.getTableName())) {
- hasOverlappedTask = true;
- break;
- }
- break;
- case DELETE_DATABASE_PROCEDURE:
- final DeleteDatabaseProcedure deleteDatabaseProcedure =
- (DeleteDatabaseProcedure) procedure;
- if (database.equals(deleteDatabaseProcedure.getDatabase())) {
- hasOverlappedTask = true;
- break;
- }
- break;
- default:
- break;
- }
- }
+ final Pair<Long, Boolean> procedureIdDuplicatePair =
+ awaitDuplicateTableTask(
+ database, table, table.getTableName(), null, ProcedureType.CREATE_TABLE_PROCEDURE);
+ long procedureId = procedureIdDuplicatePair.getLeft();
- if (procedureId == -1) {
- if (hasOverlappedTask) {
- return RpcUtils.getStatus(
- TSStatusCode.OVERLAP_WITH_EXISTING_TASK,
- "Some other task is operating table with same name.");
- }
- procedureId = this.executor.submitProcedure(new CreateTableProcedure(database, table));
+ if (procedureId == -1) {
+ if (Boolean.TRUE.equals(procedureIdDuplicatePair.getRight())) {
+ return RpcUtils.getStatus(
+ TSStatusCode.OVERLAP_WITH_EXISTING_TASK,
+ "Some other task is operating table with same name.");
}
+ procedureId = this.executor.submitProcedure(new CreateTableProcedure(database, table));
}
final List<TSStatus> procedureStatus = new ArrayList<>();
- boolean isSucceed =
+ final boolean isSucceed =
waitingProcedureFinished(Collections.singletonList(procedureId), procedureStatus);
if (isSucceed) {
return StatusUtils.OK;
@@ -1381,65 +1319,54 @@ public class ProcedureManager {
final List<TsTableColumnSchema> columnSchemaList =
TsTableColumnSchemaUtil.deserializeColumnSchemaList(req.updateInfo);
- long procedureId = -1;
- synchronized (this) {
- boolean hasOverlappedTask = false;
- ProcedureType type;
- for (final Procedure<?> procedure : executor.getProcedures().values()) {
- type = ProcedureFactory.getProcedureType(procedure);
- if (type == null) {
- continue;
- }
- // A table shall not be concurrently operated or else the dataNode cache
- // may record fake values
- switch (type) {
- case CREATE_TABLE_PROCEDURE:
- final CreateTableProcedure createTableProcedure = (CreateTableProcedure) procedure;
- if (database.equals(createTableProcedure.getDatabase())
- && tableName.equals(createTableProcedure.getTable().getTableName())) {
- hasOverlappedTask = true;
- break;
- }
- break;
- case ADD_TABLE_COLUMN_PROCEDURE:
- final AddTableColumnProcedure addTableColumnProcedure =
- (AddTableColumnProcedure) procedure;
- if (queryId.equals(addTableColumnProcedure.getQueryId())) {
- procedureId = addTableColumnProcedure.getProcId();
- break;
- }
- if (database.equals(addTableColumnProcedure.getDatabase())
- && tableName.equals(addTableColumnProcedure.getTableName())) {
- hasOverlappedTask = true;
- break;
- }
- break;
- case DELETE_DATABASE_PROCEDURE:
- final DeleteDatabaseProcedure deleteDatabaseProcedure =
- (DeleteDatabaseProcedure) procedure;
- if (database.equals(deleteDatabaseProcedure.getDatabase())) {
- hasOverlappedTask = true;
- break;
- }
- break;
- default:
- break;
- }
+ final Pair<Long, Boolean> procedureIdDuplicatePair =
+ awaitDuplicateTableTask(
+ database, null, tableName, queryId, ProcedureType.ADD_TABLE_COLUMN_PROCEDURE);
+ long procedureId = procedureIdDuplicatePair.getLeft();
+
+ if (procedureId == -1) {
+ if (Boolean.TRUE.equals(procedureIdDuplicatePair.getRight())) {
+ return RpcUtils.getStatus(
+ TSStatusCode.OVERLAP_WITH_EXISTING_TASK,
+ "Some other task is operating table with same name.");
}
+ procedureId =
+ this.executor.submitProcedure(
+ new AddTableColumnProcedure(database, tableName, queryId, columnSchemaList));
+ }
+ final List<TSStatus> procedureStatus = new ArrayList<>();
+ final boolean isSucceed =
+ waitingProcedureFinished(Collections.singletonList(procedureId), procedureStatus);
+ if (isSucceed) {
+ return StatusUtils.OK;
+ } else {
+ return procedureStatus.get(0);
+ }
+ }
- if (procedureId == -1) {
- if (hasOverlappedTask) {
- return RpcUtils.getStatus(
- TSStatusCode.OVERLAP_WITH_EXISTING_TASK,
- "Some other task dropping table with same name.");
- }
- procedureId =
- this.executor.submitProcedure(
- new AddTableColumnProcedure(database, tableName, queryId, columnSchemaList));
+ public TSStatus alterTableSetProperties(final TAlterTableReq req) {
+ final String database = req.database;
+ final String tableName = req.tableName;
+ final String queryId = req.queryId;
+ final Map<String, String> properties = ReadWriteIOUtils.readMap(req.updateInfo);
+
+ final Pair<Long, Boolean> procedureIdDuplicatePair =
+ awaitDuplicateTableTask(
+ database, null, tableName, queryId, ProcedureType.SET_TABLE_PROPERTIES_PROCEDURE);
+ long procedureId = procedureIdDuplicatePair.getLeft();
+
+ if (procedureId == -1) {
+ if (Boolean.TRUE.equals(procedureIdDuplicatePair.getRight())) {
+ return RpcUtils.getStatus(
+ TSStatusCode.OVERLAP_WITH_EXISTING_TASK,
+ "Some other task is operating table with same name.");
}
+ procedureId =
+ this.executor.submitProcedure(
+ new SetTablePropertiesProcedure(database, tableName, queryId, properties));
}
- List<TSStatus> procedureStatus = new ArrayList<>();
- boolean isSucceed =
+ final List<TSStatus> procedureStatus = new ArrayList<>();
+ final boolean isSucceed =
waitingProcedureFinished(Collections.singletonList(procedureId), procedureStatus);
if (isSucceed) {
return StatusUtils.OK;
@@ -1448,6 +1375,67 @@ public class ProcedureManager {
}
}
+ private synchronized Pair<Long, Boolean> awaitDuplicateTableTask(
+ final String database,
+ final TsTable table,
+ final String tableName,
+ final String queryId,
+ final ProcedureType thisType) {
+ ProcedureType type;
+ for (final Procedure<?> procedure : executor.getProcedures().values()) {
+ type = ProcedureFactory.getProcedureType(procedure);
+ if (type == null) {
+ continue;
+ }
+ // A table shall not be concurrently operated or else the dataNode cache
+ // may record fake values
+ switch (type) {
+ case CREATE_TABLE_PROCEDURE:
+ final CreateTableProcedure createTableProcedure = (CreateTableProcedure) procedure;
+ if (type == thisType && Objects.equals(table, createTableProcedure.getTable())) {
+ return new Pair<>(procedure.getProcId(), false);
+ }
+ if (database.equals(createTableProcedure.getDatabase())
+ && Objects.equals(tableName, createTableProcedure.getTable().getTableName())) {
+ return new Pair<>(-1L, true);
+ }
+ break;
+ case ADD_TABLE_COLUMN_PROCEDURE:
+ final AddTableColumnProcedure addTableColumnProcedure =
+ (AddTableColumnProcedure) procedure;
+ if (type == thisType && queryId.equals(addTableColumnProcedure.getQueryId())) {
+ return new Pair<>(procedure.getProcId(), false);
+ }
+ if (database.equals(addTableColumnProcedure.getDatabase())
+ && Objects.equals(tableName, addTableColumnProcedure.getTableName())) {
+ return new Pair<>(-1L, true);
+ }
+ break;
+ case SET_TABLE_PROPERTIES_PROCEDURE:
+ final SetTablePropertiesProcedure setTablePropertiesProcedure =
+ (SetTablePropertiesProcedure) procedure;
+ if (type == thisType && queryId.equals(setTablePropertiesProcedure.getQueryId())) {
+ return new Pair<>(procedure.getProcId(), false);
+ }
+ if (database.equals(setTablePropertiesProcedure.getDatabase())
+ && Objects.equals(tableName, setTablePropertiesProcedure.getTableName())) {
+ return new Pair<>(-1L, true);
+ }
+ break;
+ case DELETE_DATABASE_PROCEDURE:
+ final DeleteDatabaseProcedure deleteDatabaseProcedure =
+ (DeleteDatabaseProcedure) procedure;
+ if (database.equals(deleteDatabaseProcedure.getDatabase())) {
+ return new Pair<>(-1L, true);
+ }
+ break;
+ default:
+ break;
+ }
+ }
+ return new Pair<>(-1L, false);
+ }
+
// ======================================================
/*
GET-SET Region
diff --git a/iotdb-core/confignode/src/main/java/org/apache/iotdb/confignode/manager/schema/ClusterSchemaManager.java b/iotdb-core/confignode/src/main/java/org/apache/iotdb/confignode/manager/schema/ClusterSchemaManager.java
index 871f148873c..49f1b92f434 100644
--- a/iotdb-core/confignode/src/main/java/org/apache/iotdb/confignode/manager/schema/ClusterSchemaManager.java
+++ b/iotdb-core/confignode/src/main/java/org/apache/iotdb/confignode/manager/schema/ClusterSchemaManager.java
@@ -52,6 +52,7 @@ import org.apache.iotdb.confignode.consensus.request.write.database.SetSchemaRep
import org.apache.iotdb.confignode.consensus.request.write.database.SetTimePartitionIntervalPlan;
import org.apache.iotdb.confignode.consensus.request.write.pipe.payload.PipeEnrichedPlan;
import org.apache.iotdb.confignode.consensus.request.write.table.AddTableColumnPlan;
+import org.apache.iotdb.confignode.consensus.request.write.table.SetTablePropertiesPlan;
import org.apache.iotdb.confignode.consensus.request.write.template.CreateSchemaTemplatePlan;
import org.apache.iotdb.confignode.consensus.request.write.template.DropSchemaTemplatePlan;
import org.apache.iotdb.confignode.consensus.request.write.template.ExtendSchemaTemplatePlan;
@@ -104,6 +105,8 @@ import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.locks.ReentrantLock;
import java.util.stream.Collectors;
+import static org.apache.iotdb.commons.schema.SchemaConstant.ROOT;
+
/** The ClusterSchemaManager Manages cluster schemaengine read and write requests. */
public class ClusterSchemaManager {
@@ -1097,30 +1100,34 @@ public class ClusterSchemaManager {
schemaQuotaStatistics.setSeriesThreshold(seriesThreshold);
}
- public Optional<TsTable> getTable(final String database, final String tableName) {
- return clusterSchemaInfo.getTsTable(database, tableName);
+ public Optional<TsTable> getTableIfExists(final String database, final String tableName) {
+ return clusterSchemaInfo.getTsTableIfExists(database, tableName);
}
public synchronized Pair<TSStatus, TsTable> tableColumnCheckForColumnExtension(
final String database,
final String tableName,
final List<TsTableColumnSchema> columnSchemaList) {
- final TsTable originalTable =
- clusterSchemaInfo.getAllUsingTables().get(database).stream()
- .filter(tsTable -> tsTable.getTableName().equals(tableName))
- .findAny()
- .orElse(null);
+ final TsTable originalTable = getTableIfExists(database, tableName).orElse(null);
if (Objects.isNull(originalTable)) {
return new Pair<>(
RpcUtils.getStatus(
TSStatusCode.TABLE_NOT_EXISTS,
- String.format("Table %s.%s not exist", database, tableName)),
+ String.format(
+ "Table '%s.%s' does not exist",
+ database.substring(ROOT.length() + 1), tableName)),
null);
}
final TsTable expandedTable = TsTable.deserialize(ByteBuffer.wrap(originalTable.serialize()));
+ final String errorMsg =
+ String.format(
+ "Column '%s' already exist",
+ columnSchemaList.stream()
+ .map(TsTableColumnSchema::getColumnName)
+ .collect(Collectors.joining(", ")));
columnSchemaList.removeIf(
columnSchema -> {
if (Objects.isNull(originalTable.getColumnSchema(columnSchema.getColumnName()))) {
@@ -1130,33 +1137,89 @@ public class ClusterSchemaManager {
return true;
});
+ if (columnSchemaList.isEmpty()) {
+ return new Pair<>(RpcUtils.getStatus(TSStatusCode.COLUMN_ALREADY_EXISTS, errorMsg), null);
+ }
return new Pair<>(RpcUtils.SUCCESS_STATUS, expandedTable);
}
public synchronized TSStatus addTableColumn(
- String database, String tableName, List<TsTableColumnSchema> columnSchemaList) {
- AddTableColumnPlan addTableColumnPlan =
+ final String database,
+ final String tableName,
+ final List<TsTableColumnSchema> columnSchemaList) {
+ final AddTableColumnPlan addTableColumnPlan =
new AddTableColumnPlan(database, tableName, columnSchemaList, false);
try {
return getConsensusManager().write(addTableColumnPlan);
- } catch (ConsensusException e) {
+ } catch (final ConsensusException e) {
LOGGER.warn(e.getMessage(), e);
return RpcUtils.getStatus(TSStatusCode.INTERNAL_SERVER_ERROR, e.getMessage());
}
}
public synchronized TSStatus rollbackAddTableColumn(
- String database, String tableName, List<TsTableColumnSchema> columnSchemaList) {
- AddTableColumnPlan addTableColumnPlan =
+ final String database,
+ final String tableName,
+ final List<TsTableColumnSchema> columnSchemaList) {
+ final AddTableColumnPlan addTableColumnPlan =
new AddTableColumnPlan(database, tableName, columnSchemaList, true);
try {
return getConsensusManager().write(addTableColumnPlan);
- } catch (ConsensusException e) {
+ } catch (final ConsensusException e) {
LOGGER.warn(e.getMessage(), e);
return RpcUtils.getStatus(TSStatusCode.INTERNAL_SERVER_ERROR, e.getMessage());
}
}
+ public synchronized TSStatus setTableProperties(
+ final String database, final String tableName, final Map<String, String> properties) {
+ final SetTablePropertiesPlan setTablePropertiesPlan =
+ new SetTablePropertiesPlan(database, tableName, properties);
+ try {
+ return getConsensusManager().write(setTablePropertiesPlan);
+ } catch (final ConsensusException e) {
+ LOGGER.warn(e.getMessage(), e);
+ return RpcUtils.getStatus(TSStatusCode.INTERNAL_SERVER_ERROR, e.getMessage());
+ }
+ }
+
+ public synchronized Pair<TSStatus, TsTable> updateTableProperties(
+ final String database,
+ final String tableName,
+ final Map<String, String> originalProperties,
+ final Map<String, String> updatedProperties) {
+ final TsTable originalTable = getTableIfExists(database, tableName).orElse(null);
+
+ if (Objects.isNull(originalTable)) {
+ return new Pair<>(
+ RpcUtils.getStatus(
+ TSStatusCode.TABLE_NOT_EXISTS,
+ String.format(
+ "Table '%s.%s' does not exist",
+ database.substring(ROOT.length() + 1), tableName)),
+ null);
+ }
+
+ updatedProperties
+ .keySet()
+ .removeIf(
+ key ->
+ Objects.equals(
+ updatedProperties.get(key), originalTable.getPropValue(key).orElse(null)));
+ if (updatedProperties.isEmpty()) {
+ return new Pair<>(RpcUtils.SUCCESS_STATUS, null);
+ }
+
+ final TsTable updatedTable = TsTable.deserialize(ByteBuffer.wrap(originalTable.serialize()));
+ updatedProperties.forEach(
+ (k, v) -> {
+ originalProperties.put(k, originalTable.getPropValue(k).orElse(null));
+ updatedTable.addProp(k, v);
+ });
+
+ return new Pair<>(RpcUtils.SUCCESS_STATUS, updatedTable);
+ }
+
public void clearSchemaQuotaCache() {
schemaQuotaStatistics.clear();
}
diff --git a/iotdb-core/confignode/src/main/java/org/apache/iotdb/confignode/persistence/executor/ConfigPlanExecutor.java b/iotdb-core/confignode/src/main/java/org/apache/iotdb/confignode/persistence/executor/ConfigPlanExecutor.java
index a5a81c99905..3a6699ea2ba 100644
--- a/iotdb-core/confignode/src/main/java/org/apache/iotdb/confignode/persistence/executor/ConfigPlanExecutor.java
+++ b/iotdb-core/confignode/src/main/java/org/apache/iotdb/confignode/persistence/executor/ConfigPlanExecutor.java
@@ -103,6 +103,7 @@ import org.apache.iotdb.confignode.consensus.request.write.table.AddTableColumnP
import org.apache.iotdb.confignode.consensus.request.write.table.CommitCreateTablePlan;
import org.apache.iotdb.confignode.consensus.request.write.table.PreCreateTablePlan;
import org.apache.iotdb.confignode.consensus.request.write.table.RollbackCreateTablePlan;
+import org.apache.iotdb.confignode.consensus.request.write.table.SetTablePropertiesPlan;
import org.apache.iotdb.confignode.consensus.request.write.template.CommitSetSchemaTemplatePlan;
import org.apache.iotdb.confignode.consensus.request.write.template.CreateSchemaTemplatePlan;
import org.apache.iotdb.confignode.consensus.request.write.template.DropSchemaTemplatePlan;
@@ -471,6 +472,8 @@ public class ConfigPlanExecutor {
return clusterSchemaInfo.commitCreateTable((CommitCreateTablePlan) physicalPlan);
case AddTableColumn:
return clusterSchemaInfo.addTableColumn((AddTableColumnPlan) physicalPlan);
+ case SetTableProperties:
+ return clusterSchemaInfo.setTableProperties((SetTablePropertiesPlan) physicalPlan);
case CreatePipeV2:
return pipeInfo.createPipe((CreatePipePlanV2) physicalPlan);
case SetPipeStatusV2:
diff --git a/iotdb-core/confignode/src/main/java/org/apache/iotdb/confignode/persistence/schema/ClusterSchemaInfo.java b/iotdb-core/confignode/src/main/java/org/apache/iotdb/confignode/persistence/schema/ClusterSchemaInfo.java
index e565ac48f35..2d57c1731b5 100644
--- a/iotdb-core/confignode/src/main/java/org/apache/iotdb/confignode/persistence/schema/ClusterSchemaInfo.java
+++ b/iotdb-core/confignode/src/main/java/org/apache/iotdb/confignode/persistence/schema/ClusterSchemaInfo.java
@@ -49,6 +49,7 @@ import org.apache.iotdb.confignode.consensus.request.write.table.AddTableColumnP
import org.apache.iotdb.confignode.consensus.request.write.table.CommitCreateTablePlan;
import org.apache.iotdb.confignode.consensus.request.write.table.PreCreateTablePlan;
import org.apache.iotdb.confignode.consensus.request.write.table.RollbackCreateTablePlan;
+import org.apache.iotdb.confignode.consensus.request.write.table.SetTablePropertiesPlan;
import org.apache.iotdb.confignode.consensus.request.write.template.CommitSetSchemaTemplatePlan;
import org.apache.iotdb.confignode.consensus.request.write.template.CreateSchemaTemplatePlan;
import org.apache.iotdb.confignode.consensus.request.write.template.DropSchemaTemplatePlan;
@@ -1111,7 +1112,7 @@ public class ClusterSchemaInfo implements SnapshotProcessor {
databaseReadWriteLock.readLock().lock();
try {
return mTree.getAllPreCreateTables();
- } catch (MetadataException e) {
+ } catch (final MetadataException e) {
LOGGER.warn(e.getMessage(), e);
throw new RuntimeException(e);
} finally {
@@ -1119,11 +1120,11 @@ public class ClusterSchemaInfo implements SnapshotProcessor {
}
}
- public Optional<TsTable> getTsTable(String database, String tableName) {
+ public Optional<TsTable> getTsTableIfExists(final String database, final String tableName) {
databaseReadWriteLock.readLock().lock();
try {
- return mTree.getTable(new PartialPath(database), tableName);
- } catch (MetadataException e) {
+ return mTree.getTableIfExists(new PartialPath(database), tableName);
+ } catch (final MetadataException e) {
LOGGER.warn(e.getMessage(), e);
throw new RuntimeException(e);
} finally {
@@ -1131,8 +1132,8 @@ public class ClusterSchemaInfo implements SnapshotProcessor {
}
}
- public TSStatus addTableColumn(AddTableColumnPlan plan) {
- String databaseName = PathUtils.qualifyDatabaseName(plan.getDatabase());
+ public TSStatus addTableColumn(final AddTableColumnPlan plan) {
+ final String databaseName = PathUtils.qualifyDatabaseName(plan.getDatabase());
databaseReadWriteLock.writeLock().lock();
try {
if (plan.isRollback()) {
@@ -1143,7 +1144,22 @@ public class ClusterSchemaInfo implements SnapshotProcessor {
new PartialPath(databaseName), plan.getTableName(), plan.getColumnSchemaList());
}
return RpcUtils.SUCCESS_STATUS;
- } catch (MetadataException e) {
+ } catch (final MetadataException e) {
+ LOGGER.warn(e.getMessage(), e);
+ return RpcUtils.getStatus(e.getErrorCode(), e.getMessage());
+ } finally {
+ databaseReadWriteLock.writeLock().unlock();
+ }
+ }
+
+ public TSStatus setTableProperties(final SetTablePropertiesPlan plan) {
+ final String databaseName = PathUtils.qualifyDatabaseName(plan.getDatabase());
+ databaseReadWriteLock.writeLock().lock();
+ try {
+ mTree.setTableProperties(
+ new PartialPath(databaseName), plan.getTableName(), plan.getProperties());
+ return RpcUtils.SUCCESS_STATUS;
+ } catch (final MetadataException e) {
LOGGER.warn(e.getMessage(), e);
return RpcUtils.getStatus(e.getErrorCode(), e.getMessage());
} finally {
diff --git a/iotdb-core/confignode/src/main/java/org/apache/iotdb/confignode/persistence/schema/ConfigMTree.java b/iotdb-core/confignode/src/main/java/org/apache/iotdb/confignode/persistence/schema/ConfigMTree.java
index 56e06157ccf..c7a2af3a27f 100644
--- a/iotdb-core/confignode/src/main/java/org/apache/iotdb/confignode/persistence/schema/ConfigMTree.java
+++ b/iotdb-core/confignode/src/main/java/org/apache/iotdb/confignode/persistence/schema/ConfigMTree.java
@@ -64,6 +64,7 @@ import java.util.HashSet;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
+import java.util.Objects;
import java.util.Optional;
import java.util.Set;
import java.util.Stack;
@@ -701,14 +702,14 @@ public class ConfigMTree {
}
public Map<String, List<TsTable>> getAllPreCreateTables() throws MetadataException {
- Map<String, List<TsTable>> result = new HashMap<>();
- List<PartialPath> databaseList = getAllDatabasePaths();
+ final Map<String, List<TsTable>> result = new HashMap<>();
+ final List<PartialPath> databaseList = getAllDatabasePaths();
for (PartialPath databasePath : databaseList) {
- String database = databasePath.getFullPath().substring(ROOT.length() + 1);
- IConfigMNode databaseNode = getDatabaseNodeByDatabasePath(databasePath).getAsMNode();
- for (IConfigMNode child : databaseNode.getChildren().values()) {
+ final String database = databasePath.getFullPath().substring(ROOT.length() + 1);
+ final IConfigMNode databaseNode = getDatabaseNodeByDatabasePath(databasePath).getAsMNode();
+ for (final IConfigMNode child : databaseNode.getChildren().values()) {
if (child instanceof ConfigTableNode) {
- ConfigTableNode tableNode = (ConfigTableNode) child;
+ final ConfigTableNode tableNode = (ConfigTableNode) child;
if (!tableNode.getStatus().equals(TableNodeStatus.PRE_CREATE)) {
continue;
}
@@ -719,40 +720,56 @@ public class ConfigMTree {
return result;
}
- public Optional<TsTable> getTable(final PartialPath database, final String tableName)
+ public void addTableColumn(
+ final PartialPath database,
+ final String tableName,
+ final List<TsTableColumnSchema> columnSchemaList)
throws MetadataException {
- final IConfigMNode databaseNode = getDatabaseNodeByDatabasePath(database).getAsMNode();
- if (!databaseNode.hasChild(tableName)) {
- return Optional.empty();
- }
- final ConfigTableNode tableNode = (ConfigTableNode) databaseNode.getChild(tableName);
- return Optional.of(tableNode.getTable());
+ final TsTable table = getTable(database, tableName);
+ columnSchemaList.forEach(table::addColumnSchema);
}
- public void addTableColumn(
- PartialPath database, String tableName, List<TsTableColumnSchema> columnSchemaList)
+ public void rollbackAddTableColumn(
+ final PartialPath database,
+ final String tableName,
+ final List<TsTableColumnSchema> columnSchemaList)
+ throws MetadataException {
+ final TsTable table = getTable(database, tableName);
+ columnSchemaList.forEach(o -> table.removeColumnSchema(o.getColumnName()));
+ }
+
+ public void setTableProperties(
+ final PartialPath database, final String tableName, final Map<String, String> tableProperties)
throws MetadataException {
- IConfigMNode databaseNode = getDatabaseNodeByDatabasePath(database).getAsMNode();
+ final TsTable table = getTable(database, tableName);
+ tableProperties.forEach(
+ (k, v) -> {
+ if (Objects.nonNull(v)) {
+ table.addProp(k, v);
+ } else {
+ table.removeProp(k);
+ }
+ });
+ }
+
+ private TsTable getTable(final PartialPath database, final String tableName)
+ throws MetadataException {
+ final IConfigMNode databaseNode = getDatabaseNodeByDatabasePath(database).getAsMNode();
if (!databaseNode.hasChild(tableName)) {
throw new TableNotExistsException(
database.getFullPath().substring(ROOT.length() + 1), tableName);
}
- ConfigTableNode tableNode = (ConfigTableNode) databaseNode.getChild(tableName);
- TsTable table = tableNode.getTable();
- columnSchemaList.forEach(table::addColumnSchema);
+ return ((ConfigTableNode) databaseNode.getChild(tableName)).getTable();
}
- public void rollbackAddTableColumn(
- PartialPath database, String tableName, List<TsTableColumnSchema> columnSchemaList)
+ public Optional<TsTable> getTableIfExists(final PartialPath database, final String tableName)
throws MetadataException {
- IConfigMNode databaseNode = getDatabaseNodeByDatabasePath(database).getAsMNode();
+ final IConfigMNode databaseNode = getDatabaseNodeByDatabasePath(database).getAsMNode();
if (!databaseNode.hasChild(tableName)) {
- throw new TableNotExistsException(
- database.getFullPath().substring(ROOT.length() + 1), tableName);
+ return Optional.empty();
}
- ConfigTableNode tableNode = (ConfigTableNode) databaseNode.getChild(tableName);
- TsTable table = tableNode.getTable();
- columnSchemaList.forEach(o -> table.removeColumnSchema(o.getColumnName()));
+ final ConfigTableNode tableNode = (ConfigTableNode) databaseNode.getChild(tableName);
+ return Optional.of(tableNode.getTable());
}
// endregion
diff --git a/iotdb-core/confignode/src/main/java/org/apache/iotdb/confignode/procedure/impl/schema/SchemaUtils.java b/iotdb-core/confignode/src/main/java/org/apache/iotdb/confignode/procedure/impl/schema/SchemaUtils.java
index 1df6ff508fd..83bc5dcd8e5 100644
--- a/iotdb-core/confignode/src/main/java/org/apache/iotdb/confignode/procedure/impl/schema/SchemaUtils.java
+++ b/iotdb-core/confignode/src/main/java/org/apache/iotdb/confignode/procedure/impl/schema/SchemaUtils.java
@@ -26,7 +26,12 @@ import org.apache.iotdb.common.rpc.thrift.TSStatus;
import org.apache.iotdb.commons.exception.MetadataException;
import org.apache.iotdb.commons.path.PartialPath;
import org.apache.iotdb.commons.path.PathPatternTree;
+import org.apache.iotdb.commons.schema.table.TsTable;
+import org.apache.iotdb.commons.schema.table.TsTableInternalRPCType;
+import org.apache.iotdb.commons.schema.table.TsTableInternalRPCUtil;
import org.apache.iotdb.confignode.client.CnToDnRequestType;
+import org.apache.iotdb.confignode.client.async.CnToDnInternalServiceAsyncRequestManager;
+import org.apache.iotdb.confignode.client.async.handlers.DataNodeAsyncRequestContext;
import org.apache.iotdb.confignode.manager.ConfigManager;
import org.apache.iotdb.db.exception.metadata.PathNotExistException;
import org.apache.iotdb.db.schemaengine.template.Template;
@@ -34,8 +39,11 @@ import org.apache.iotdb.mpp.rpc.thrift.TCheckSchemaRegionUsingTemplateReq;
import org.apache.iotdb.mpp.rpc.thrift.TCheckSchemaRegionUsingTemplateResp;
import org.apache.iotdb.mpp.rpc.thrift.TCountPathsUsingTemplateReq;
import org.apache.iotdb.mpp.rpc.thrift.TCountPathsUsingTemplateResp;
+import org.apache.iotdb.mpp.rpc.thrift.TUpdateTableReq;
import org.apache.iotdb.rpc.TSStatusCode;
+import org.apache.tsfile.utils.ReadWriteIOUtils;
+
import java.io.ByteArrayOutputStream;
import java.io.DataOutputStream;
import java.io.IOException;
@@ -209,4 +217,66 @@ public class SchemaUtils {
}
}
}
+
+ public static Map<Integer, TSStatus> preReleaseTable(
+ final String database, final TsTable table, final ConfigManager configManager) {
+ final TUpdateTableReq req = new TUpdateTableReq();
+ req.setType(TsTableInternalRPCType.PRE_CREATE_OR_ADD_COLUMN.getOperationType());
+ req.setTableInfo(TsTableInternalRPCUtil.serializeSingleTsTable(database, table));
+
+ final Map<Integer, TDataNodeLocation> dataNodeLocationMap =
+ configManager.getNodeManager().getRegisteredDataNodeLocations();
+ final DataNodeAsyncRequestContext<TUpdateTableReq, TSStatus> clientHandler =
+ new DataNodeAsyncRequestContext<>(CnToDnRequestType.UPDATE_TABLE, req, dataNodeLocationMap);
+ CnToDnInternalServiceAsyncRequestManager.getInstance().sendAsyncRequestWithRetry(clientHandler);
+ return clientHandler.getResponseMap().entrySet().stream()
+ .filter(entry -> entry.getValue().getCode() != TSStatusCode.SUCCESS_STATUS.getStatusCode())
+ .collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue));
+ }
+
+ public static Map<Integer, TSStatus> commitReleaseTable(
+ final String database, final String tableName, final ConfigManager configManager) {
+ final TUpdateTableReq req = new TUpdateTableReq();
+ req.setType(TsTableInternalRPCType.COMMIT_CREATE_OR_ADD_COLUMN.getOperationType());
+ final ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
+ try {
+ ReadWriteIOUtils.write(database, outputStream);
+ ReadWriteIOUtils.write(tableName, outputStream);
+ } catch (final IOException ignored) {
+ // ByteArrayOutputStream will not throw IOException
+ }
+ req.setTableInfo(outputStream.toByteArray());
+
+ final Map<Integer, TDataNodeLocation> dataNodeLocationMap =
+ configManager.getNodeManager().getRegisteredDataNodeLocations();
+ final DataNodeAsyncRequestContext<TUpdateTableReq, TSStatus> clientHandler =
+ new DataNodeAsyncRequestContext<>(CnToDnRequestType.UPDATE_TABLE, req, dataNodeLocationMap);
+ CnToDnInternalServiceAsyncRequestManager.getInstance().sendAsyncRequestWithRetry(clientHandler);
+ return clientHandler.getResponseMap().entrySet().stream()
+ .filter(entry -> entry.getValue().getCode() != TSStatusCode.SUCCESS_STATUS.getStatusCode())
+ .collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue));
+ }
+
+ public static Map<Integer, TSStatus> rollbackPreRelease(
+ final String database, final String tableName, final ConfigManager configManager) {
+ final TUpdateTableReq req = new TUpdateTableReq();
+ req.setType(TsTableInternalRPCType.ROLLBACK_CREATE_OR_ADD_COLUMN.getOperationType());
+ final ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
+ try {
+ ReadWriteIOUtils.write(database, outputStream);
+ ReadWriteIOUtils.write(tableName, outputStream);
+ } catch (final IOException ignore) {
+ // ByteArrayOutputStream will not throw IOException
+ }
+ req.setTableInfo(outputStream.toByteArray());
+
+ final Map<Integer, TDataNodeLocation> dataNodeLocationMap =
+ configManager.getNodeManager().getRegisteredDataNodeLocations();
+ final DataNodeAsyncRequestContext<TUpdateTableReq, TSStatus> clientHandler =
+ new DataNodeAsyncRequestContext<>(CnToDnRequestType.UPDATE_TABLE, req, dataNodeLocationMap);
+ CnToDnInternalServiceAsyncRequestManager.getInstance().sendAsyncRequestWithRetry(clientHandler);
+ return clientHandler.getResponseMap().entrySet().stream()
+ .filter(entry -> entry.getValue().getCode() != TSStatusCode.SUCCESS_STATUS.getStatusCode())
+ .collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue));
+ }
}
diff --git a/iotdb-core/confignode/src/main/java/org/apache/iotdb/confignode/procedure/impl/schema/table/AddTableColumnProcedure.java b/iotdb-core/confignode/src/main/java/org/apache/iotdb/confignode/procedure/impl/schema/table/AddTableColumnProcedure.java
index ab4e4693276..3e77b6dad8b 100644
--- a/iotdb-core/confignode/src/main/java/org/apache/iotdb/confignode/procedure/impl/schema/table/AddTableColumnProcedure.java
+++ b/iotdb-core/confignode/src/main/java/org/apache/iotdb/confignode/procedure/impl/schema/table/AddTableColumnProcedure.java
@@ -19,26 +19,20 @@
package org.apache.iotdb.confignode.procedure.impl.schema.table;
-import org.apache.iotdb.common.rpc.thrift.TDataNodeLocation;
import org.apache.iotdb.common.rpc.thrift.TSStatus;
import org.apache.iotdb.commons.exception.IoTDBException;
import org.apache.iotdb.commons.exception.MetadataException;
import org.apache.iotdb.commons.schema.table.TsTable;
-import org.apache.iotdb.commons.schema.table.TsTableInternalRPCType;
-import org.apache.iotdb.commons.schema.table.TsTableInternalRPCUtil;
import org.apache.iotdb.commons.schema.table.column.TsTableColumnSchema;
import org.apache.iotdb.commons.schema.table.column.TsTableColumnSchemaUtil;
-import org.apache.iotdb.confignode.client.CnToDnRequestType;
-import org.apache.iotdb.confignode.client.async.CnToDnInternalServiceAsyncRequestManager;
-import org.apache.iotdb.confignode.client.async.handlers.DataNodeAsyncRequestContext;
import org.apache.iotdb.confignode.procedure.env.ConfigNodeProcedureEnv;
import org.apache.iotdb.confignode.procedure.exception.ProcedureException;
import org.apache.iotdb.confignode.procedure.exception.ProcedureSuspendedException;
import org.apache.iotdb.confignode.procedure.exception.ProcedureYieldException;
import org.apache.iotdb.confignode.procedure.impl.StateMachineProcedure;
+import org.apache.iotdb.confignode.procedure.impl.schema.SchemaUtils;
import org.apache.iotdb.confignode.procedure.state.schema.AddTableColumnState;
import org.apache.iotdb.confignode.procedure.store.ProcedureType;
-import org.apache.iotdb.mpp.rpc.thrift.TUpdateTableReq;
import org.apache.iotdb.rpc.TSStatusCode;
import org.apache.tsfile.utils.Pair;
@@ -46,7 +40,6 @@ import org.apache.tsfile.utils.ReadWriteIOUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
-import java.io.ByteArrayOutputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.nio.ByteBuffer;
@@ -134,65 +127,25 @@ public class AddTableColumnProcedure
}
private void preRelease(final ConfigNodeProcedureEnv env) {
- final TUpdateTableReq req = new TUpdateTableReq();
- req.setType(TsTableInternalRPCType.PRE_CREATE_OR_ADD_COLUMN.getOperationType());
- req.setTableInfo(TsTableInternalRPCUtil.serializeSingleTsTable(database, table));
-
- final Map<Integer, TDataNodeLocation> dataNodeLocationMap =
- env.getConfigManager().getNodeManager().getRegisteredDataNodeLocations();
- final DataNodeAsyncRequestContext<TUpdateTableReq, TSStatus> clientHandler =
- new DataNodeAsyncRequestContext<>(CnToDnRequestType.UPDATE_TABLE, req, dataNodeLocationMap);
- CnToDnInternalServiceAsyncRequestManager.getInstance().sendAsyncRequestWithRetry(clientHandler);
- final Map<Integer, TSStatus> statusMap = clientHandler.getResponseMap();
- for (final Map.Entry<Integer, TSStatus> entry : statusMap.entrySet()) {
- if (entry.getValue().getCode() != TSStatusCode.SUCCESS_STATUS.getStatusCode()) {
- // All dataNodes must clear the related schema cache
- LOGGER.warn(
- "Failed to pre-release column extension info of table {}.{} to DataNode {}",
- database,
- table.getTableName(),
- dataNodeLocationMap.get(entry.getKey()));
- setFailure(
- new ProcedureException(
- new MetadataException("Pre-release table column extension info failed")));
- return;
- }
+ final Map<Integer, TSStatus> failedResults =
+ SchemaUtils.preReleaseTable(database, table, env.getConfigManager());
+
+ if (!failedResults.isEmpty()) {
+ // All dataNodes must clear the related schema cache
+ LOGGER.warn(
+ "Failed to pre-release column extension info of table {}.{} to DataNode, failure results: {}",
+ database,
+ table.getTableName(),
+ failedResults);
+ setFailure(
+ new ProcedureException(
+ new MetadataException("Pre-release table column extension info failed")));
+ return;
}
setNextState(AddTableColumnState.ADD_COLUMN);
}
- private void commitRelease(final ConfigNodeProcedureEnv env) {
- final TUpdateTableReq req = new TUpdateTableReq();
- req.setType(TsTableInternalRPCType.COMMIT_CREATE_OR_ADD_COLUMN.getOperationType());
- final ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
- try {
- ReadWriteIOUtils.write(database, outputStream);
- ReadWriteIOUtils.write(table.getTableName(), outputStream);
- } catch (final IOException ignored) {
- // ByteArrayOutputStream will not throw IOException
- }
- req.setTableInfo(outputStream.toByteArray());
-
- final Map<Integer, TDataNodeLocation> dataNodeLocationMap =
- env.getConfigManager().getNodeManager().getRegisteredDataNodeLocations();
- final DataNodeAsyncRequestContext<TUpdateTableReq, TSStatus> clientHandler =
- new DataNodeAsyncRequestContext<>(CnToDnRequestType.UPDATE_TABLE, req, dataNodeLocationMap);
- CnToDnInternalServiceAsyncRequestManager.getInstance().sendAsyncRequestWithRetry(clientHandler);
- final Map<Integer, TSStatus> statusMap = clientHandler.getResponseMap();
- for (final Map.Entry<Integer, TSStatus> entry : statusMap.entrySet()) {
- if (entry.getValue().getCode() != TSStatusCode.SUCCESS_STATUS.getStatusCode()) {
- LOGGER.warn(
- "Failed to commit column extension info of table {}.{} to DataNode {}",
- database,
- table.getTableName(),
- dataNodeLocationMap.get(entry.getKey()));
- // TODO: Handle commit failure
- return;
- }
- }
- }
-
private void addColumn(final ConfigNodeProcedureEnv env) {
final TSStatus status =
env.getConfigManager()
@@ -205,6 +158,19 @@ public class AddTableColumnProcedure
}
}
+ private void commitRelease(final ConfigNodeProcedureEnv env) {
+ final Map<Integer, TSStatus> failedResults =
+ SchemaUtils.commitReleaseTable(database, table.getTableName(), env.getConfigManager());
+ if (!failedResults.isEmpty()) {
+ LOGGER.warn(
+ "Failed to commit column extension info of table {}.{} to DataNode, failure results: {}",
+ database,
+ table.getTableName(),
+ failedResults);
+ // TODO: Handle commit failure
+ }
+ }
+
@Override
protected void rollbackState(final ConfigNodeProcedureEnv env, final AddTableColumnState state)
throws IOException, InterruptedException, ProcedureException {
@@ -212,9 +178,14 @@ public class AddTableColumnProcedure
try {
switch (state) {
case ADD_COLUMN:
+ LOGGER.info(
+ "Start rollback pre release info of table {}.{} when adding column",
+ database,
+ table.getTableName());
rollbackAddColumn(env);
break;
case PRE_RELEASE:
+ LOGGER.info("Start rollback Add column to table {}.{}", database, table.getTableName());
rollbackPreRelease(env);
break;
}
@@ -238,35 +209,19 @@ public class AddTableColumnProcedure
}
private void rollbackPreRelease(final ConfigNodeProcedureEnv env) {
- final TUpdateTableReq req = new TUpdateTableReq();
- req.setType(TsTableInternalRPCType.ROLLBACK_CREATE_OR_ADD_COLUMN.getOperationType());
- final ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
- try {
- ReadWriteIOUtils.write(database, outputStream);
- ReadWriteIOUtils.write(table.getTableName(), outputStream);
- } catch (final IOException ignore) {
- // ByteArrayOutputStream will not throw IOException
- }
- req.setTableInfo(outputStream.toByteArray());
-
- final Map<Integer, TDataNodeLocation> dataNodeLocationMap =
- env.getConfigManager().getNodeManager().getRegisteredDataNodeLocations();
- final DataNodeAsyncRequestContext<TUpdateTableReq, TSStatus> clientHandler =
- new DataNodeAsyncRequestContext<>(CnToDnRequestType.UPDATE_TABLE, req, dataNodeLocationMap);
- CnToDnInternalServiceAsyncRequestManager.getInstance().sendAsyncRequestWithRetry(clientHandler);
- final Map<Integer, TSStatus> statusMap = clientHandler.getResponseMap();
- for (final Map.Entry<Integer, TSStatus> entry : statusMap.entrySet()) {
- if (entry.getValue().getCode() != TSStatusCode.SUCCESS_STATUS.getStatusCode()) {
- LOGGER.warn(
- "Failed to rollback pre-release column extension info of table {}.{} rollback-create info to DataNode {}",
- database,
- table.getTableName(),
- dataNodeLocationMap.get(entry.getKey()));
- setFailure(
- new ProcedureException(
- new MetadataException("Rollback pre-release table column extension info failed")));
- return;
- }
+ final Map<Integer, TSStatus> failedResults =
+ SchemaUtils.rollbackPreRelease(database, table.getTableName(), env.getConfigManager());
+
+ if (!failedResults.isEmpty()) {
+ // All dataNodes must clear the related schema cache
+ LOGGER.warn(
+ "Failed to rollback pre-release column extension info of table {}.{} info to DataNode, failure results: {}",
+ database,
+ table.getTableName(),
+ failedResults);
+ setFailure(
+ new ProcedureException(
+ new MetadataException("Rollback pre-release table column extension info failed")));
}
}
@@ -330,8 +285,12 @@ public class AddTableColumnProcedure
@Override
public boolean equals(final Object o) {
- if (this == o) return true;
- if (!(o instanceof AddTableColumnProcedure)) return false;
+ if (this == o) {
+ return true;
+ }
+ if (!(o instanceof AddTableColumnProcedure)) {
+ return false;
+ }
final AddTableColumnProcedure that = (AddTableColumnProcedure) o;
return Objects.equals(database, that.database)
&& Objects.equals(tableName, that.tableName)
diff --git a/iotdb-core/confignode/src/main/java/org/apache/iotdb/confignode/procedure/impl/schema/table/CreateTableProcedure.java b/iotdb-core/confignode/src/main/java/org/apache/iotdb/confignode/procedure/impl/schema/table/CreateTableProcedure.java
index 3d5546b476b..f077e251ea6 100644
--- a/iotdb-core/confignode/src/main/java/org/apache/iotdb/confignode/procedure/impl/schema/table/CreateTableProcedure.java
+++ b/iotdb-core/confignode/src/main/java/org/apache/iotdb/confignode/procedure/impl/schema/table/CreateTableProcedure.java
@@ -28,11 +28,7 @@ import org.apache.iotdb.commons.exception.MetadataException;
import org.apache.iotdb.commons.path.PartialPath;
import org.apache.iotdb.commons.path.PathPatternTree;
import org.apache.iotdb.commons.schema.table.TsTable;
-import org.apache.iotdb.commons.schema.table.TsTableInternalRPCType;
-import org.apache.iotdb.commons.schema.table.TsTableInternalRPCUtil;
import org.apache.iotdb.confignode.client.CnToDnRequestType;
-import org.apache.iotdb.confignode.client.async.CnToDnInternalServiceAsyncRequestManager;
-import org.apache.iotdb.confignode.client.async.handlers.DataNodeAsyncRequestContext;
import org.apache.iotdb.confignode.consensus.request.write.table.CommitCreateTablePlan;
import org.apache.iotdb.confignode.consensus.request.write.table.PreCreateTablePlan;
import org.apache.iotdb.confignode.consensus.request.write.table.RollbackCreateTablePlan;
@@ -42,12 +38,12 @@ import org.apache.iotdb.confignode.procedure.exception.ProcedureSuspendedExcepti
import org.apache.iotdb.confignode.procedure.exception.ProcedureYieldException;
import org.apache.iotdb.confignode.procedure.impl.StateMachineProcedure;
import org.apache.iotdb.confignode.procedure.impl.schema.DataNodeRegionTaskExecutor;
+import org.apache.iotdb.confignode.procedure.impl.schema.SchemaUtils;
import org.apache.iotdb.confignode.procedure.state.schema.CreateTableState;
import org.apache.iotdb.confignode.procedure.store.ProcedureType;
import org.apache.iotdb.consensus.exception.ConsensusException;
import org.apache.iotdb.mpp.rpc.thrift.TCheckTimeSeriesExistenceReq;
import org.apache.iotdb.mpp.rpc.thrift.TCheckTimeSeriesExistenceResp;
-import org.apache.iotdb.mpp.rpc.thrift.TUpdateTableReq;
import org.apache.iotdb.rpc.TSStatusCode;
import org.apache.tsfile.utils.ReadWriteIOUtils;
@@ -135,7 +131,7 @@ public class CreateTableProcedure
private void checkTableExistence(final ConfigNodeProcedureEnv env) {
if (env.getConfigManager()
.getClusterSchemaManager()
- .getTable(database, table.getTableName())
+ .getTableIfExists(database, table.getTableName())
.isPresent()) {
setFailure(
new ProcedureException(
@@ -167,29 +163,20 @@ public class CreateTableProcedure
}
private void preReleaseTable(final ConfigNodeProcedureEnv env) {
- final TUpdateTableReq req = new TUpdateTableReq();
- req.setType(TsTableInternalRPCType.PRE_CREATE_OR_ADD_COLUMN.getOperationType());
- req.setTableInfo(TsTableInternalRPCUtil.serializeSingleTsTable(database, table));
-
- final Map<Integer, TDataNodeLocation> dataNodeLocationMap =
- env.getConfigManager().getNodeManager().getRegisteredDataNodeLocations();
- final DataNodeAsyncRequestContext<TUpdateTableReq, TSStatus> clientHandler =
- new DataNodeAsyncRequestContext<>(CnToDnRequestType.UPDATE_TABLE, req, dataNodeLocationMap);
- CnToDnInternalServiceAsyncRequestManager.getInstance().sendAsyncRequestWithRetry(clientHandler);
- final Map<Integer, TSStatus> statusMap = clientHandler.getResponseMap();
- for (final Map.Entry<Integer, TSStatus> entry : statusMap.entrySet()) {
-
- if (entry.getValue().getCode() != TSStatusCode.SUCCESS_STATUS.getStatusCode()) {
- // All dataNodes must clear the related schema cache
- LOGGER.warn(
- "Failed to sync table {}.{} pre-create info to DataNode {}",
- database,
- table.getTableName(),
- dataNodeLocationMap.get(entry.getKey()));
- setFailure(new ProcedureException(new MetadataException("Pre create table failed")));
- return;
- }
+ final Map<Integer, TSStatus> failedResults =
+ SchemaUtils.preReleaseTable(database, table, env.getConfigManager());
+
+ if (!failedResults.isEmpty()) {
+ // All dataNodes must clear the related schema cache
+ LOGGER.warn(
+ "Failed to sync table {}.{} pre-create info to DataNode, failure results: {}",
+ database,
+ table.getTableName(),
+ failedResults);
+ setFailure(new ProcedureException(new MetadataException("Pre create table failed")));
+ return;
}
+
setNextState(CreateTableState.VALIDATE_TIMESERIES_EXISTENCE);
}
@@ -301,33 +288,16 @@ public class CreateTableProcedure
}
private void commitReleaseTable(final ConfigNodeProcedureEnv env) {
- final TUpdateTableReq req = new TUpdateTableReq();
- req.setType(TsTableInternalRPCType.COMMIT_CREATE_OR_ADD_COLUMN.getOperationType());
- final ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
- try {
- ReadWriteIOUtils.write(database, outputStream);
- ReadWriteIOUtils.write(table.getTableName(), outputStream);
- } catch (final IOException ignored) {
- // ByteArrayOutputStream will not throw IOException
- }
- req.setTableInfo(outputStream.toByteArray());
-
- final Map<Integer, TDataNodeLocation> dataNodeLocationMap =
- env.getConfigManager().getNodeManager().getRegisteredDataNodeLocations();
- final DataNodeAsyncRequestContext<TUpdateTableReq, TSStatus> clientHandler =
- new DataNodeAsyncRequestContext<>(CnToDnRequestType.UPDATE_TABLE, req, dataNodeLocationMap);
- CnToDnInternalServiceAsyncRequestManager.getInstance().sendAsyncRequestWithRetry(clientHandler);
- final Map<Integer, TSStatus> statusMap = clientHandler.getResponseMap();
- for (final Map.Entry<Integer, TSStatus> entry : statusMap.entrySet()) {
- if (entry.getValue().getCode() != TSStatusCode.SUCCESS_STATUS.getStatusCode()) {
- LOGGER.warn(
- "Failed to sync table {}.{} commit-create info to DataNode {}",
- database,
- table.getTableName(),
- dataNodeLocationMap.get(entry.getKey()));
- // TODO: Handle commit failure
- return;
- }
+ final Map<Integer, TSStatus> failedResults =
+ SchemaUtils.commitReleaseTable(database, table.getTableName(), env.getConfigManager());
+
+ if (!failedResults.isEmpty()) {
+ LOGGER.warn(
+ "Failed to sync table {}.{} commit-create info to DataNode {}, failure results: ",
+ database,
+ table.getTableName(),
+ failedResults);
+ // TODO: Handle commit failure
}
}
@@ -370,33 +340,17 @@ public class CreateTableProcedure
}
private void rollbackPreRelease(final ConfigNodeProcedureEnv env) {
- final TUpdateTableReq req = new TUpdateTableReq();
- req.setType(TsTableInternalRPCType.ROLLBACK_CREATE_OR_ADD_COLUMN.getOperationType());
- final ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
- try {
- ReadWriteIOUtils.write(database, outputStream);
- ReadWriteIOUtils.write(table.getTableName(), outputStream);
- } catch (final IOException ignore) {
- // ByteArrayOutputStream will not throw IOException
- }
- req.setTableInfo(outputStream.toByteArray());
-
- final Map<Integer, TDataNodeLocation> dataNodeLocationMap =
- env.getConfigManager().getNodeManager().getRegisteredDataNodeLocations();
- final DataNodeAsyncRequestContext<TUpdateTableReq, TSStatus> clientHandler =
- new DataNodeAsyncRequestContext<>(CnToDnRequestType.UPDATE_TABLE, req, dataNodeLocationMap);
- CnToDnInternalServiceAsyncRequestManager.getInstance().sendAsyncRequestWithRetry(clientHandler);
- final Map<Integer, TSStatus> statusMap = clientHandler.getResponseMap();
- for (final Map.Entry<Integer, TSStatus> entry : statusMap.entrySet()) {
- if (entry.getValue().getCode() != TSStatusCode.SUCCESS_STATUS.getStatusCode()) {
- LOGGER.warn(
- "Failed to sync table {}.{} rollback-create info to DataNode {}",
- database,
- table.getTableName(),
- dataNodeLocationMap.get(entry.getKey()));
- setFailure(new ProcedureException(new MetadataException("Rollback create table failed")));
- return;
- }
+ final Map<Integer, TSStatus> failedResults =
+ SchemaUtils.rollbackPreRelease(database, table.getTableName(), env.getConfigManager());
+
+ if (!failedResults.isEmpty()) {
+ // All dataNodes must clear the related schema cache
+ LOGGER.warn(
+ "Failed to sync table {}.{} rollback-create info to DataNode {}, failure results: ",
+ database,
+ table.getTableName(),
+ failedResults);
+ setFailure(new ProcedureException(new MetadataException("Rollback create table failed")));
}
}
@@ -406,8 +360,8 @@ public class CreateTableProcedure
}
@Override
- protected int getStateId(final CreateTableState createTableState) {
- return createTableState.ordinal();
+ protected int getStateId(final CreateTableState state) {
+ return state.ordinal();
}
@Override
@@ -440,8 +394,12 @@ public class CreateTableProcedure
@Override
public boolean equals(final Object o) {
- if (this == o) return true;
- if (!(o instanceof CreateTableProcedure)) return false;
+ if (this == o) {
+ return true;
+ }
+ if (!(o instanceof CreateTableProcedure)) {
+ return false;
+ }
final CreateTableProcedure that = (CreateTableProcedure) o;
return Objects.equals(database, that.database) && Objects.equals(table, that.table);
}
diff --git a/iotdb-core/confignode/src/main/java/org/apache/iotdb/confignode/procedure/impl/schema/table/SetTablePropertiesProcedure.java b/iotdb-core/confignode/src/main/java/org/apache/iotdb/confignode/procedure/impl/schema/table/SetTablePropertiesProcedure.java
new file mode 100644
index 00000000000..10c6d976012
--- /dev/null
+++ b/iotdb-core/confignode/src/main/java/org/apache/iotdb/confignode/procedure/impl/schema/table/SetTablePropertiesProcedure.java
@@ -0,0 +1,325 @@
+/*
+ * 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.procedure.impl.schema.table;
+
+import org.apache.iotdb.common.rpc.thrift.TSStatus;
+import org.apache.iotdb.commons.exception.IoTDBException;
+import org.apache.iotdb.commons.exception.MetadataException;
+import org.apache.iotdb.commons.schema.table.TsTable;
+import org.apache.iotdb.confignode.procedure.env.ConfigNodeProcedureEnv;
+import org.apache.iotdb.confignode.procedure.exception.ProcedureException;
+import org.apache.iotdb.confignode.procedure.exception.ProcedureSuspendedException;
+import org.apache.iotdb.confignode.procedure.exception.ProcedureYieldException;
+import org.apache.iotdb.confignode.procedure.impl.StateMachineProcedure;
+import org.apache.iotdb.confignode.procedure.impl.schema.SchemaUtils;
+import org.apache.iotdb.confignode.procedure.state.schema.SetTablePropertiesState;
+import org.apache.iotdb.confignode.procedure.store.ProcedureType;
+import org.apache.iotdb.rpc.TSStatusCode;
+
+import org.apache.tsfile.utils.Pair;
+import org.apache.tsfile.utils.ReadWriteIOUtils;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import java.io.DataOutputStream;
+import java.io.IOException;
+import java.nio.ByteBuffer;
+import java.util.HashMap;
+import java.util.Map;
+import java.util.Objects;
+
+import static org.apache.iotdb.confignode.procedure.state.schema.SetTablePropertiesState.COMMIT_RELEASE;
+import static org.apache.iotdb.confignode.procedure.state.schema.SetTablePropertiesState.PRE_RELEASE;
+import static org.apache.iotdb.confignode.procedure.state.schema.SetTablePropertiesState.SET_PROPERTIES;
+import static org.apache.iotdb.confignode.procedure.state.schema.SetTablePropertiesState.VALIDATE_TABLE;
+
+public class SetTablePropertiesProcedure
+ extends StateMachineProcedure<ConfigNodeProcedureEnv, SetTablePropertiesState> {
+
+ private static final Logger LOGGER = LoggerFactory.getLogger(SetTablePropertiesProcedure.class);
+
+ private String database;
+
+ private String tableName;
+
+ private String queryId;
+
+ private Map<String, String> originalProperties = new HashMap<>();
+ private Map<String, String> updatedProperties;
+ private TsTable table;
+
+ public SetTablePropertiesProcedure() {
+ super();
+ }
+
+ public SetTablePropertiesProcedure(
+ final String database,
+ final String tableName,
+ final String queryId,
+ final Map<String, String> properties) {
+ this.database = database;
+ this.tableName = tableName;
+ this.queryId = queryId;
+ this.updatedProperties = properties;
+ }
+
+ @Override
+ protected Flow executeFromState(
+ final ConfigNodeProcedureEnv env, final SetTablePropertiesState state)
+ throws ProcedureSuspendedException, ProcedureYieldException, InterruptedException {
+ final long startTime = System.currentTimeMillis();
+ try {
+ switch (state) {
+ case VALIDATE_TABLE:
+ validateTable(env);
+ LOGGER.info(
+ "Validate table for table {}.{} when setting properties", database, tableName);
+ if (!isFailed() && Objects.isNull(table)) {
+ LOGGER.info(
+ "The updated table has the same properties with the original one. Skip the procedure.");
+ return Flow.NO_MORE_STATE;
+ }
+ break;
+ case PRE_RELEASE:
+ preRelease(env);
+ LOGGER.info(
+ "Pre release info for table {}.{} when setting properties", database, tableName);
+ break;
+ case SET_PROPERTIES:
+ setProperties(env);
+ LOGGER.info("Set properties to table {}.{}", database, tableName);
+ break;
+ case COMMIT_RELEASE:
+ commitRelease(env);
+ LOGGER.info(
+ "Commit release info of table {}.{} when setting properties", database, tableName);
+ return Flow.NO_MORE_STATE;
+ default:
+ setFailure(new ProcedureException("Unrecognized AddTableColumnState " + state));
+ return Flow.NO_MORE_STATE;
+ }
+ return Flow.HAS_MORE_STATE;
+ } finally {
+ LOGGER.info(
+ "SetTableProperties-{}.{}-{} costs {}ms",
+ database,
+ tableName,
+ state,
+ (System.currentTimeMillis() - startTime));
+ }
+ }
+
+ private void validateTable(final ConfigNodeProcedureEnv env) {
+ final Pair<TSStatus, TsTable> result =
+ env.getConfigManager()
+ .getClusterSchemaManager()
+ .updateTableProperties(database, tableName, originalProperties, updatedProperties);
+ final TSStatus status = result.getLeft();
+ if (status.getCode() != TSStatusCode.SUCCESS_STATUS.getStatusCode()) {
+ setFailure(new ProcedureException(new IoTDBException(status.getMessage(), status.getCode())));
+ return;
+ }
+ table = result.getRight();
+ setNextState(PRE_RELEASE);
+ }
+
+ private void preRelease(final ConfigNodeProcedureEnv env) {
+ final Map<Integer, TSStatus> failedResults =
+ SchemaUtils.preReleaseTable(database, table, env.getConfigManager());
+
+ if (!failedResults.isEmpty()) {
+ // All dataNodes must clear the related schema cache
+ LOGGER.warn(
+ "Failed to pre-release properties info of table {}.{} to DataNode, failure results: {}",
+ database,
+ table.getTableName(),
+ failedResults);
+ setFailure(
+ new ProcedureException(
+ new MetadataException("Pre-release table properties info failed")));
+ return;
+ }
+
+ setNextState(SET_PROPERTIES);
+ }
+
+ private void setProperties(final ConfigNodeProcedureEnv env) {
+ final TSStatus status =
+ env.getConfigManager()
+ .getClusterSchemaManager()
+ .setTableProperties(database, tableName, updatedProperties);
+ if (status.getCode() != TSStatusCode.SUCCESS_STATUS.getStatusCode()) {
+ setFailure(new ProcedureException(new IoTDBException(status.getMessage(), status.getCode())));
+ } else {
+ setNextState(COMMIT_RELEASE);
+ }
+ }
+
+ private void commitRelease(final ConfigNodeProcedureEnv env) {
+ final Map<Integer, TSStatus> failedResults =
+ SchemaUtils.commitReleaseTable(database, table.getTableName(), env.getConfigManager());
+ if (!failedResults.isEmpty()) {
+ LOGGER.warn(
+ "Failed to commit properties info of table {}.{} to DataNode, failure results: {}",
+ database,
+ table.getTableName(),
+ failedResults);
+ // TODO: Handle commit failure
+ }
+ }
+
+ @Override
+ protected void rollbackState(
+ final ConfigNodeProcedureEnv env, final SetTablePropertiesState state)
+ throws IOException, InterruptedException, ProcedureException {
+ final long startTime = System.currentTimeMillis();
+ try {
+ switch (state) {
+ case PRE_RELEASE:
+ LOGGER.info(
+ "Start rollback pre release info for table {}.{} when setting properties",
+ database,
+ table.getTableName());
+ rollbackPreRelease(env);
+ break;
+ case SET_PROPERTIES:
+ LOGGER.info(
+ "Start rollback set properties to table {}.{}", database, table.getTableName());
+ rollbackSetProperties(env);
+ break;
+ }
+ } finally {
+ LOGGER.info(
+ "Rollback SetTableProperties-{} costs {}ms.",
+ state,
+ (System.currentTimeMillis() - startTime));
+ }
+ }
+
+ private void rollbackPreRelease(final ConfigNodeProcedureEnv env) {
+ final Map<Integer, TSStatus> failedResults =
+ SchemaUtils.rollbackPreRelease(database, table.getTableName(), env.getConfigManager());
+
+ if (!failedResults.isEmpty()) {
+ // All dataNodes must clear the related schema cache
+ LOGGER.warn(
+ "Failed to rollback properties info of table {}.{} info to DataNode, failure results: {}",
+ database,
+ table.getTableName(),
+ failedResults);
+ setFailure(
+ new ProcedureException(
+ new MetadataException("Rollback pre-release table column extension info failed")));
+ }
+ }
+
+ private void rollbackSetProperties(final ConfigNodeProcedureEnv env) {
+ if (table == null) {
+ return;
+ }
+ final TSStatus status =
+ env.getConfigManager()
+ .getClusterSchemaManager()
+ .setTableProperties(database, tableName, originalProperties);
+ if (status.getCode() != TSStatusCode.SUCCESS_STATUS.getStatusCode()) {
+ setFailure(new ProcedureException(new IoTDBException(status.getMessage(), status.getCode())));
+ }
+ }
+
+ @Override
+ protected SetTablePropertiesState getState(final int stateId) {
+ return SetTablePropertiesState.values()[stateId];
+ }
+
+ @Override
+ protected int getStateId(final SetTablePropertiesState state) {
+ return state.ordinal();
+ }
+
+ @Override
+ protected SetTablePropertiesState getInitialState() {
+ return VALIDATE_TABLE;
+ }
+
+ public String getDatabase() {
+ return database;
+ }
+
+ public String getTableName() {
+ return tableName;
+ }
+
+ public String getQueryId() {
+ return queryId;
+ }
+
+ @Override
+ public void serialize(final DataOutputStream stream) throws IOException {
+ stream.writeShort(ProcedureType.SET_TABLE_PROPERTIES_PROCEDURE.getTypeCode());
+ super.serialize(stream);
+
+ ReadWriteIOUtils.write(database, stream);
+ ReadWriteIOUtils.write(tableName, stream);
+ ReadWriteIOUtils.write(queryId, stream);
+
+ ReadWriteIOUtils.write(originalProperties, stream);
+ ReadWriteIOUtils.write(updatedProperties, stream);
+ if (Objects.nonNull(table)) {
+ ReadWriteIOUtils.write(true, stream);
+ table.serialize(stream);
+ } else {
+ ReadWriteIOUtils.write(false, stream);
+ }
+ }
+
+ @Override
+ public void deserialize(final ByteBuffer byteBuffer) {
+ super.deserialize(byteBuffer);
+ this.database = ReadWriteIOUtils.readString(byteBuffer);
+ this.tableName = ReadWriteIOUtils.readString(byteBuffer);
+ this.queryId = ReadWriteIOUtils.readString(byteBuffer);
+
+ this.originalProperties = ReadWriteIOUtils.readMap(byteBuffer);
+ this.updatedProperties = ReadWriteIOUtils.readMap(byteBuffer);
+ if (ReadWriteIOUtils.readBool(byteBuffer)) {
+ this.table = TsTable.deserialize(byteBuffer);
+ }
+ }
+
+ @Override
+ public boolean equals(final Object o) {
+ if (this == o) {
+ return true;
+ }
+ if (!(o instanceof SetTablePropertiesProcedure)) {
+ return false;
+ }
+ final SetTablePropertiesProcedure that = (SetTablePropertiesProcedure) o;
+ return Objects.equals(database, that.database)
+ && Objects.equals(tableName, that.tableName)
+ && Objects.equals(updatedProperties, that.updatedProperties)
+ && Objects.equals(queryId, that.queryId);
+ }
+
+ @Override
+ public int hashCode() {
+ return Objects.hash(database, tableName, updatedProperties, queryId);
+ }
+}
diff --git a/iotdb-core/datanode/src/main/java/org/apache/iotdb/db/exception/metadata/table/TableNotExistsException.java b/iotdb-core/confignode/src/main/java/org/apache/iotdb/confignode/procedure/state/schema/SetTablePropertiesState.java
similarity index 64%
copy from iotdb-core/datanode/src/main/java/org/apache/iotdb/db/exception/metadata/table/TableNotExistsException.java
copy to iotdb-core/confignode/src/main/java/org/apache/iotdb/confignode/procedure/state/schema/SetTablePropertiesState.java
index 1bc2db0123c..45a1b8b654a 100644
--- a/iotdb-core/datanode/src/main/java/org/apache/iotdb/db/exception/metadata/table/TableNotExistsException.java
+++ b/iotdb-core/confignode/src/main/java/org/apache/iotdb/confignode/procedure/state/schema/SetTablePropertiesState.java
@@ -17,16 +17,11 @@
* under the License.
*/
-package org.apache.iotdb.db.exception.metadata.table;
+package org.apache.iotdb.confignode.procedure.state.schema;
-import org.apache.iotdb.commons.exception.MetadataException;
-import org.apache.iotdb.rpc.TSStatusCode;
-
-public class TableNotExistsException extends MetadataException {
-
- public TableNotExistsException(String database, String tableName) {
- super(
- String.format("Table '%s.%s' does not exist.", database, tableName),
- TSStatusCode.TABLE_NOT_EXISTS.getStatusCode());
- }
+public enum SetTablePropertiesState {
+ VALIDATE_TABLE,
+ PRE_RELEASE,
+ SET_PROPERTIES,
+ COMMIT_RELEASE
}
diff --git a/iotdb-core/confignode/src/main/java/org/apache/iotdb/confignode/procedure/store/ProcedureFactory.java b/iotdb-core/confignode/src/main/java/org/apache/iotdb/confignode/procedure/store/ProcedureFactory.java
index b1946264182..d9ca53e436e 100644
--- a/iotdb-core/confignode/src/main/java/org/apache/iotdb/confignode/procedure/store/ProcedureFactory.java
+++ b/iotdb-core/confignode/src/main/java/org/apache/iotdb/confignode/procedure/store/ProcedureFactory.java
@@ -49,6 +49,7 @@ import org.apache.iotdb.confignode.procedure.impl.schema.SetTemplateProcedure;
import org.apache.iotdb.confignode.procedure.impl.schema.UnsetTemplateProcedure;
import org.apache.iotdb.confignode.procedure.impl.schema.table.AddTableColumnProcedure;
import org.apache.iotdb.confignode.procedure.impl.schema.table.CreateTableProcedure;
+import org.apache.iotdb.confignode.procedure.impl.schema.table.SetTablePropertiesProcedure;
import org.apache.iotdb.confignode.procedure.impl.subscription.consumer.AlterConsumerGroupProcedure;
import org.apache.iotdb.confignode.procedure.impl.subscription.consumer.CreateConsumerProcedure;
import org.apache.iotdb.confignode.procedure.impl.subscription.consumer.DropConsumerProcedure;
@@ -190,6 +191,9 @@ public class ProcedureFactory implements IProcedureFactory {
case ADD_TABLE_COLUMN_PROCEDURE:
procedure = new AddTableColumnProcedure();
break;
+ case SET_TABLE_PROPERTIES_PROCEDURE:
+ procedure = new SetTablePropertiesProcedure();
+ break;
case CREATE_PIPE_PLUGIN_PROCEDURE:
procedure = new CreatePipePluginProcedure();
break;
@@ -331,6 +335,8 @@ public class ProcedureFactory implements IProcedureFactory {
return ProcedureType.CREATE_TABLE_PROCEDURE;
} else if (procedure instanceof AddTableColumnProcedure) {
return ProcedureType.ADD_TABLE_COLUMN_PROCEDURE;
+ } else if (procedure instanceof SetTablePropertiesProcedure) {
+ return ProcedureType.SET_TABLE_PROPERTIES_PROCEDURE;
} else if (procedure instanceof CreatePipePluginProcedure) {
return ProcedureType.CREATE_PIPE_PLUGIN_PROCEDURE;
} else if (procedure instanceof DropPipePluginProcedure) {
diff --git a/iotdb-core/confignode/src/main/java/org/apache/iotdb/confignode/procedure/store/ProcedureType.java b/iotdb-core/confignode/src/main/java/org/apache/iotdb/confignode/procedure/store/ProcedureType.java
index aaa36ddfc60..487ff288895 100644
--- a/iotdb-core/confignode/src/main/java/org/apache/iotdb/confignode/procedure/store/ProcedureType.java
+++ b/iotdb-core/confignode/src/main/java/org/apache/iotdb/confignode/procedure/store/ProcedureType.java
@@ -65,6 +65,7 @@ public enum ProcedureType {
CREATE_TABLE_PROCEDURE((short) 750),
DROP_TABLE_PROCEDURE((short) 751),
ADD_TABLE_COLUMN_PROCEDURE((short) 752),
+ SET_TABLE_PROPERTIES_PROCEDURE((short) 753),
// ProcedureId 800-899 is used by IoTDB-Ml
diff --git a/iotdb-core/confignode/src/test/java/org/apache/iotdb/confignode/consensus/request/ConfigPhysicalPlanSerDeTest.java b/iotdb-core/confignode/src/test/java/org/apache/iotdb/confignode/consensus/request/ConfigPhysicalPlanSerDeTest.java
index 2a6d53c61ba..86fde2710a0 100644
--- a/iotdb-core/confignode/src/test/java/org/apache/iotdb/confignode/consensus/request/ConfigPhysicalPlanSerDeTest.java
+++ b/iotdb-core/confignode/src/test/java/org/apache/iotdb/confignode/consensus/request/ConfigPhysicalPlanSerDeTest.java
@@ -150,6 +150,7 @@ import org.apache.iotdb.confignode.consensus.request.write.table.AddTableColumnP
import org.apache.iotdb.confignode.consensus.request.write.table.CommitCreateTablePlan;
import org.apache.iotdb.confignode.consensus.request.write.table.PreCreateTablePlan;
import org.apache.iotdb.confignode.consensus.request.write.table.RollbackCreateTablePlan;
+import org.apache.iotdb.confignode.consensus.request.write.table.SetTablePropertiesPlan;
import org.apache.iotdb.confignode.consensus.request.write.template.CreateSchemaTemplatePlan;
import org.apache.iotdb.confignode.consensus.request.write.template.DropSchemaTemplatePlan;
import org.apache.iotdb.confignode.consensus.request.write.template.ExtendSchemaTemplatePlan;
@@ -1523,6 +1524,21 @@ public class ConfigPhysicalPlanSerDeTest {
rollbackCreateTablePlan0.getTableName(), rollbackCreateTablePlan1.getTableName());
}
+ @Test
+ public void SetTablePropertiesPlanTest() throws IOException {
+ final SetTablePropertiesPlan setTablePropertiesPlan0 =
+ new SetTablePropertiesPlan("root.database1", "table1", Collections.singletonMap("a", null));
+ final SetTablePropertiesPlan setTablePropertiesPlan1 =
+ (SetTablePropertiesPlan)
+ ConfigPhysicalPlan.Factory.create(setTablePropertiesPlan0.serializeToByteBuffer());
+ Assert.assertEquals(
+ setTablePropertiesPlan0.getDatabase(), setTablePropertiesPlan1.getDatabase());
+ Assert.assertEquals(
+ setTablePropertiesPlan0.getTableName(), setTablePropertiesPlan1.getTableName());
+ Assert.assertEquals(
+ setTablePropertiesPlan0.getProperties(), setTablePropertiesPlan1.getProperties());
+ }
+
@Test
public void GetTriggerTablePlanTest() throws IOException {
GetTriggerTablePlan getTriggerTablePlan0 = new GetTriggerTablePlan(true);
diff --git a/iotdb-core/confignode/src/test/java/org/apache/iotdb/confignode/procedure/impl/schema/table/SetTablePropertiesProcedureTest.java b/iotdb-core/confignode/src/test/java/org/apache/iotdb/confignode/procedure/impl/schema/table/SetTablePropertiesProcedureTest.java
new file mode 100644
index 00000000000..16180027b18
--- /dev/null
+++ b/iotdb-core/confignode/src/test/java/org/apache/iotdb/confignode/procedure/impl/schema/table/SetTablePropertiesProcedureTest.java
@@ -0,0 +1,63 @@
+/*
+ * 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.procedure.impl.schema.table;
+
+import org.apache.iotdb.commons.exception.IllegalPathException;
+import org.apache.iotdb.confignode.procedure.store.ProcedureType;
+
+import org.junit.Assert;
+import org.junit.Test;
+
+import java.io.ByteArrayOutputStream;
+import java.io.DataOutputStream;
+import java.io.IOException;
+import java.nio.ByteBuffer;
+import java.util.HashMap;
+
+public class SetTablePropertiesProcedureTest {
+ @Test
+ public void serializeDeserializeTest() throws IllegalPathException, IOException {
+ final SetTablePropertiesProcedure setTablePropertiesProcedure =
+ new SetTablePropertiesProcedure(
+ "root.database1",
+ "table1",
+ "0",
+ new HashMap<String, String>() {
+ {
+ put("prop1", "value1");
+ put("ttl", null);
+ }
+ });
+
+ final ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
+ final DataOutputStream dataOutputStream = new DataOutputStream(byteArrayOutputStream);
+ setTablePropertiesProcedure.serialize(dataOutputStream);
+
+ final ByteBuffer byteBuffer = ByteBuffer.wrap(byteArrayOutputStream.toByteArray());
+
+ Assert.assertEquals(
+ ProcedureType.SET_TABLE_PROPERTIES_PROCEDURE.getTypeCode(), byteBuffer.getShort());
+
+ final SetTablePropertiesProcedure deserializedProcedure = new SetTablePropertiesProcedure();
+ deserializedProcedure.deserialize(byteBuffer);
+
+ Assert.assertEquals(setTablePropertiesProcedure, deserializedProcedure);
+ }
+}
diff --git a/iotdb-core/datanode/src/main/java/org/apache/iotdb/db/exception/metadata/table/TableAlreadyExistsException.java b/iotdb-core/datanode/src/main/java/org/apache/iotdb/db/exception/metadata/table/TableAlreadyExistsException.java
index 31f91d98cf0..d28c04e9e7e 100644
--- a/iotdb-core/datanode/src/main/java/org/apache/iotdb/db/exception/metadata/table/TableAlreadyExistsException.java
+++ b/iotdb-core/datanode/src/main/java/org/apache/iotdb/db/exception/metadata/table/TableAlreadyExistsException.java
@@ -24,7 +24,7 @@ import org.apache.iotdb.rpc.TSStatusCode;
public class TableAlreadyExistsException extends MetadataException {
- public TableAlreadyExistsException(String database, String tableName) {
+ public TableAlreadyExistsException(final String database, final String tableName) {
super(
String.format("Table '%s.%s' already exists.", database, tableName),
TSStatusCode.TABLE_ALREADY_EXISTS.getStatusCode());
diff --git a/iotdb-core/datanode/src/main/java/org/apache/iotdb/db/exception/metadata/table/TableNotExistsException.java b/iotdb-core/datanode/src/main/java/org/apache/iotdb/db/exception/metadata/table/TableNotExistsException.java
index 1bc2db0123c..616a20d49bc 100644
--- a/iotdb-core/datanode/src/main/java/org/apache/iotdb/db/exception/metadata/table/TableNotExistsException.java
+++ b/iotdb-core/datanode/src/main/java/org/apache/iotdb/db/exception/metadata/table/TableNotExistsException.java
@@ -24,7 +24,7 @@ import org.apache.iotdb.rpc.TSStatusCode;
public class TableNotExistsException extends MetadataException {
- public TableNotExistsException(String database, String tableName) {
+ public TableNotExistsException(final String database, final String tableName) {
super(
String.format("Table '%s.%s' does not exist.", database, tableName),
TSStatusCode.TABLE_NOT_EXISTS.getStatusCode());
diff --git a/iotdb-core/datanode/src/main/java/org/apache/iotdb/db/queryengine/plan/Coordinator.java b/iotdb-core/datanode/src/main/java/org/apache/iotdb/db/queryengine/plan/Coordinator.java
index e8481dbd962..fff9085efa6 100644
--- a/iotdb-core/datanode/src/main/java/org/apache/iotdb/db/queryengine/plan/Coordinator.java
+++ b/iotdb-core/datanode/src/main/java/org/apache/iotdb/db/queryengine/plan/Coordinator.java
@@ -47,6 +47,7 @@ import org.apache.iotdb.db.queryengine.plan.execution.config.TreeConfigTaskVisit
import org.apache.iotdb.db.queryengine.plan.planner.TreeModelPlanner;
import org.apache.iotdb.db.queryengine.plan.relational.metadata.Metadata;
import org.apache.iotdb.db.queryengine.plan.relational.planner.RelationalModelPlanner;
+import org.apache.iotdb.db.queryengine.plan.relational.sql.ast.AddColumn;
import org.apache.iotdb.db.queryengine.plan.relational.sql.ast.CreateDB;
import org.apache.iotdb.db.queryengine.plan.relational.sql.ast.CreateTable;
import org.apache.iotdb.db.queryengine.plan.relational.sql.ast.DescribeTable;
@@ -54,6 +55,7 @@ import org.apache.iotdb.db.queryengine.plan.relational.sql.ast.DropDB;
import org.apache.iotdb.db.queryengine.plan.relational.sql.ast.DropTable;
import org.apache.iotdb.db.queryengine.plan.relational.sql.ast.Flush;
import org.apache.iotdb.db.queryengine.plan.relational.sql.ast.SetConfiguration;
+import org.apache.iotdb.db.queryengine.plan.relational.sql.ast.SetProperties;
import org.apache.iotdb.db.queryengine.plan.relational.sql.ast.ShowCluster;
import org.apache.iotdb.db.queryengine.plan.relational.sql.ast.ShowConfigNodes;
import org.apache.iotdb.db.queryengine.plan.relational.sql.ast.ShowDB;
@@ -320,6 +322,8 @@ public class Coordinator {
|| statement instanceof CreateTable
|| statement instanceof DescribeTable
|| statement instanceof ShowTables
+ || statement instanceof AddColumn
+ || statement instanceof SetProperties
|| statement instanceof DropTable
|| statement instanceof ShowCluster
|| statement instanceof ShowRegions
diff --git a/iotdb-core/datanode/src/main/java/org/apache/iotdb/db/queryengine/plan/execution/config/TableConfigTaskVisitor.java b/iotdb-core/datanode/src/main/java/org/apache/iotdb/db/queryengine/plan/execution/config/TableConfigTaskVisitor.java
index c3ef4c4d42d..672ccc5fe2d 100644
--- a/iotdb-core/datanode/src/main/java/org/apache/iotdb/db/queryengine/plan/execution/config/TableConfigTaskVisitor.java
+++ b/iotdb-core/datanode/src/main/java/org/apache/iotdb/db/queryengine/plan/execution/config/TableConfigTaskVisitor.java
@@ -27,6 +27,8 @@ import org.apache.iotdb.db.queryengine.common.MPPQueryContext;
import org.apache.iotdb.db.queryengine.plan.analyze.QueryType;
import org.apache.iotdb.db.queryengine.plan.execution.config.metadata.ShowClusterTask;
import org.apache.iotdb.db.queryengine.plan.execution.config.metadata.ShowRegionTask;
+import org.apache.iotdb.db.queryengine.plan.execution.config.metadata.relational.AlterTableAddColumnTask;
+import org.apache.iotdb.db.queryengine.plan.execution.config.metadata.relational.AlterTableSetPropertiesTask;
import org.apache.iotdb.db.queryengine.plan.execution.config.metadata.relational.CreateDBTask;
import org.apache.iotdb.db.queryengine.plan.execution.config.metadata.relational.CreateTableTask;
import org.apache.iotdb.db.queryengine.plan.execution.config.metadata.relational.DescribeTableTask;
@@ -40,6 +42,7 @@ import org.apache.iotdb.db.queryengine.plan.execution.config.sys.FlushTask;
import org.apache.iotdb.db.queryengine.plan.execution.config.sys.SetConfigurationTask;
import org.apache.iotdb.db.queryengine.plan.relational.metadata.Metadata;
import org.apache.iotdb.db.queryengine.plan.relational.metadata.fetcher.TableHeaderSchemaValidator;
+import org.apache.iotdb.db.queryengine.plan.relational.sql.ast.AddColumn;
import org.apache.iotdb.db.queryengine.plan.relational.sql.ast.AstVisitor;
import org.apache.iotdb.db.queryengine.plan.relational.sql.ast.ColumnDefinition;
import org.apache.iotdb.db.queryengine.plan.relational.sql.ast.CreateDB;
@@ -55,7 +58,9 @@ import org.apache.iotdb.db.queryengine.plan.relational.sql.ast.Literal;
import org.apache.iotdb.db.queryengine.plan.relational.sql.ast.LongLiteral;
import org.apache.iotdb.db.queryengine.plan.relational.sql.ast.Node;
import org.apache.iotdb.db.queryengine.plan.relational.sql.ast.Property;
+import org.apache.iotdb.db.queryengine.plan.relational.sql.ast.QualifiedName;
import org.apache.iotdb.db.queryengine.plan.relational.sql.ast.SetConfiguration;
+import org.apache.iotdb.db.queryengine.plan.relational.sql.ast.SetProperties;
import org.apache.iotdb.db.queryengine.plan.relational.sql.ast.ShowCluster;
import org.apache.iotdb.db.queryengine.plan.relational.sql.ast.ShowConfigNodes;
import org.apache.iotdb.db.queryengine.plan.relational.sql.ast.ShowDB;
@@ -70,8 +75,11 @@ import org.apache.iotdb.db.queryengine.plan.statement.sys.FlushStatement;
import org.apache.iotdb.db.queryengine.plan.statement.sys.SetConfigurationStatement;
import org.apache.tsfile.enums.TSDataType;
+import org.apache.tsfile.utils.Pair;
+import java.util.Collections;
import java.util.HashMap;
+import java.util.List;
import java.util.Locale;
import java.util.Map;
import java.util.Objects;
@@ -88,43 +96,44 @@ public class TableConfigTaskVisitor extends AstVisitor<IConfigTask, MPPQueryCont
private final Metadata metadata;
- public TableConfigTaskVisitor(IClientSession clientSession, Metadata metadata) {
+ public TableConfigTaskVisitor(final IClientSession clientSession, final Metadata metadata) {
this.clientSession = clientSession;
this.metadata = metadata;
}
@Override
- protected IConfigTask visitNode(Node node, MPPQueryContext context) {
+ protected IConfigTask visitNode(final Node node, final MPPQueryContext context) {
throw new UnsupportedOperationException(
"Unsupported statement type: " + node.getClass().getName());
}
@Override
- protected IConfigTask visitCreateDB(CreateDB node, MPPQueryContext context) {
+ protected IConfigTask visitCreateDB(final CreateDB node, final MPPQueryContext context) {
context.setQueryType(QueryType.WRITE);
return new CreateDBTask(node);
}
@Override
- protected IConfigTask visitUse(Use node, MPPQueryContext context) {
+ protected IConfigTask visitUse(final Use node, final MPPQueryContext context) {
context.setQueryType(QueryType.WRITE);
return new UseDBTask(node, clientSession);
}
@Override
- protected IConfigTask visitDropDB(DropDB node, MPPQueryContext context) {
+ protected IConfigTask visitDropDB(final DropDB node, final MPPQueryContext context) {
context.setQueryType(QueryType.WRITE);
return new DropDBTask(node);
}
@Override
- protected IConfigTask visitShowDB(ShowDB node, MPPQueryContext context) {
+ protected IConfigTask visitShowDB(final ShowDB node, final MPPQueryContext context) {
context.setQueryType(QueryType.READ);
return new ShowDBTask(node);
}
@Override
- protected IConfigTask visitShowCluster(ShowCluster showCluster, MPPQueryContext context) {
+ protected IConfigTask visitShowCluster(
+ final ShowCluster showCluster, final MPPQueryContext context) {
context.setQueryType(QueryType.READ);
// As the implementation is identical, we'll simply translate to the
// corresponding tree-model variant and execute that.
@@ -134,7 +143,8 @@ public class TableConfigTaskVisitor extends AstVisitor<IConfigTask, MPPQueryCont
}
@Override
- protected IConfigTask visitShowRegions(ShowRegions showRegions, MPPQueryContext context) {
+ protected IConfigTask visitShowRegions(
+ final ShowRegions showRegions, final MPPQueryContext context) {
context.setQueryType(QueryType.READ);
// As the implementation is identical, we'll simply translate to the
// corresponding tree-model variant and execute that.
@@ -147,13 +157,13 @@ public class TableConfigTaskVisitor extends AstVisitor<IConfigTask, MPPQueryCont
@Override
protected IConfigTask visitShowDataNodes(
- ShowDataNodes showDataNodesStatement, MPPQueryContext context) {
+ final ShowDataNodes showDataNodesStatement, final MPPQueryContext context) {
context.setQueryType(QueryType.READ);
return new ShowDataNodesTask(showDataNodesStatement);
}
protected IConfigTask visitShowConfigNodes(
- ShowConfigNodes showConfigNodesStatement, MPPQueryContext context) {
+ final ShowConfigNodes showConfigNodesStatement, final MPPQueryContext context) {
context.setQueryType(QueryType.READ);
return new ShowConfigNodesTask(showConfigNodesStatement);
}
@@ -161,16 +171,75 @@ public class TableConfigTaskVisitor extends AstVisitor<IConfigTask, MPPQueryCont
@Override
protected IConfigTask visitCreateTable(final CreateTable node, final MPPQueryContext context) {
context.setQueryType(QueryType.WRITE);
+ final Pair<String, String> databaseTablePair = splitQualifiedName(node.getName());
+
+ final TsTable table = new TsTable(databaseTablePair.getRight());
+
+ table.setProps(convertPropertiesToMap(node.getProperties(), false));
+
+ // TODO: Place the check at statement analyzer
+ for (final ColumnDefinition columnDefinition : node.getElements()) {
+ final TsTableColumnCategory category = columnDefinition.getColumnCategory();
+ final String columnName = columnDefinition.getName().getValue();
+ if (table.getColumnSchema(columnName) != null) {
+ throw new SemanticException(
+ String.format("Columns in table shall not share the same name %s.", columnName));
+ }
+ final TSDataType dataType = getDataType(columnDefinition.getType());
+ table.addColumnSchema(
+ TableHeaderSchemaValidator.generateColumnSchema(category, columnName, dataType));
+ }
+ return new CreateTableTask(table, databaseTablePair.getLeft(), node.isIfNotExists());
+ }
+
+ @Override
+ protected IConfigTask visitAddColumn(final AddColumn node, final MPPQueryContext context) {
+ context.setQueryType(QueryType.WRITE);
+ final Pair<String, String> databaseTablePair = splitQualifiedName(node.getTableName());
+
+ final ColumnDefinition definition = node.getColumn();
+ return new AlterTableAddColumnTask(
+ databaseTablePair.getLeft(),
+ databaseTablePair.getRight(),
+ Collections.singletonList(
+ TableHeaderSchemaValidator.generateColumnSchema(
+ definition.getColumnCategory(),
+ definition.getName().getValue(),
+ getDataType(definition.getType()))),
+ context.getQueryId().getId(),
+ node.tableIfExists(),
+ node.columnIfNotExists());
+ }
+
+ @Override
+ protected IConfigTask visitSetProperties(
+ final SetProperties node, final MPPQueryContext context) {
+ context.setQueryType(QueryType.WRITE);
+ final Pair<String, String> databaseTablePair = splitQualifiedName(node.getName());
+
+ return new AlterTableSetPropertiesTask(
+ databaseTablePair.getLeft(),
+ databaseTablePair.getRight(),
+ convertPropertiesToMap(node.getProperties(), true),
+ context.getQueryId().getId(),
+ node.ifExists());
+ }
+
+ public Pair<String, String> splitQualifiedName(final QualifiedName name) {
String database = clientSession.getDatabaseName();
- if (node.getName().getPrefix().isPresent()) {
- database = node.getName().getPrefix().get().toString();
+ if (name.getPrefix().isPresent()) {
+ database = name.getPrefix().get().toString();
}
if (database == null) {
throw new SemanticException(DATABASE_NOT_SPECIFIED);
}
- final TsTable table = new TsTable(node.getName().getSuffix());
+ return new Pair<>(database, name.getSuffix());
+ }
+
+ private Map<String, String> convertPropertiesToMap(
+ final List<Property> propertyList, final boolean serializeDefault) {
final Map<String, String> map = new HashMap<>();
- for (final Property property : node.getProperties()) {
+ for (final Property property : propertyList) {
final String key = property.getName().getValue().toLowerCase(Locale.ENGLISH);
if (TABLE_ALLOWED_PROPERTIES_2_DEFAULT_VALUE_MAP.containsKey(key)) {
if (!property.isSetToDefault()) {
@@ -196,30 +265,20 @@ public class TableConfigTaskVisitor extends AstVisitor<IConfigTask, MPPQueryCont
"TTL' value must be equal to or greater than 0, but now is: " + value);
}
map.put(key, String.valueOf(parsedValue));
+ } else if (serializeDefault) {
+ map.put(key, null);
}
} else {
- throw new SemanticException("Table property " + key + " is currently not allowed.");
- }
- }
- table.setProps(map);
-
- for (final ColumnDefinition columnDefinition : node.getElements()) {
- final TsTableColumnCategory category = columnDefinition.getColumnCategory();
- final String columnName = columnDefinition.getName().getValue();
- if (table.getColumnSchema(columnName) != null) {
- throw new SemanticException(
- String.format("Columns in table shall not share the same name %s.", columnName));
+ throw new SemanticException("Table property '" + key + "' is currently not allowed.");
}
- final TSDataType dataType = getDataType(columnDefinition.getType());
- TableHeaderSchemaValidator.generateColumnSchema(table, category, columnName, dataType);
}
- return new CreateTableTask(table, database, node.isIfNotExists());
+ return map;
}
- private TSDataType getDataType(DataType dataType) {
+ private TSDataType getDataType(final DataType dataType) {
try {
return getTSDataType(metadata.getType(toTypeSignature(dataType)));
- } catch (TypeNotFoundException e) {
+ } catch (final TypeNotFoundException e) {
throw new SemanticException(String.format("Unknown type: %s", dataType));
}
}
diff --git a/iotdb-core/datanode/src/main/java/org/apache/iotdb/db/queryengine/plan/execution/config/executor/ClusterConfigTaskExecutor.java b/iotdb-core/datanode/src/main/java/org/apache/iotdb/db/queryengine/plan/execution/config/executor/ClusterConfigTaskExecutor.java
index fcd7fe48de6..37d18c03e5d 100644
--- a/iotdb-core/datanode/src/main/java/org/apache/iotdb/db/queryengine/plan/execution/config/executor/ClusterConfigTaskExecutor.java
+++ b/iotdb-core/datanode/src/main/java/org/apache/iotdb/db/queryengine/plan/execution/config/executor/ClusterConfigTaskExecutor.java
@@ -3124,7 +3124,9 @@ public class ClusterConfigTaskExecutor implements IConfigTaskExecutor {
final String database,
final String tableName,
final List<TsTableColumnSchema> columnSchemaList,
- final String queryId) {
+ final String queryId,
+ final boolean tableIfExists,
+ final boolean columnIfExists) {
final SettableFuture<ConfigTaskResult> future = SettableFuture.create();
try (final ConfigNodeClient client =
CLUSTER_DELETION_CONFIG_NODE_CLIENT_MANAGER.borrowClient(ConfigNodeInfo.CONFIG_REGION_ID)) {
@@ -3151,12 +3153,73 @@ public class ClusterConfigTaskExecutor implements IConfigTaskExecutor {
// keep waiting until task ends
} while (TSStatusCode.OVERLAP_WITH_EXISTING_TASK.getStatusCode() == tsStatus.getCode());
- if (TSStatusCode.SUCCESS_STATUS.getStatusCode() != tsStatus.getCode()) {
+ if (TSStatusCode.SUCCESS_STATUS.getStatusCode() == tsStatus.getCode()
+ || (TSStatusCode.TABLE_NOT_EXISTS.getStatusCode() == tsStatus.getCode() && tableIfExists)
+ || (TSStatusCode.COLUMN_ALREADY_EXISTS.getStatusCode() == tsStatus.getCode()
+ && columnIfExists)) {
+ future.set(new ConfigTaskResult(TSStatusCode.SUCCESS_STATUS));
+ } else {
LOGGER.warn(
"Failed to add column to table {}.{}, status is {}.", database, tableName, tsStatus);
future.setException(new IoTDBException(tsStatus.getMessage(), tsStatus.getCode()));
- } else {
+ }
+ } catch (final ClientManagerException | TException e) {
+ future.setException(e);
+ }
+ return future;
+ }
+
+ @Override
+ public SettableFuture<ConfigTaskResult> alterTableSetProperties(
+ final String database,
+ final String tableName,
+ final Map<String, String> properties,
+ final String queryId,
+ final boolean ifExists) {
+ final SettableFuture<ConfigTaskResult> future = SettableFuture.create();
+ try (final ConfigNodeClient client =
+ CLUSTER_DELETION_CONFIG_NODE_CLIENT_MANAGER.borrowClient(ConfigNodeInfo.CONFIG_REGION_ID)) {
+ TSStatus tsStatus;
+ final TAlterTableReq req = new TAlterTableReq();
+ req.setDatabase(database);
+ req.setTableName(tableName);
+ req.setQueryId(queryId);
+ req.setOperationType(AlterTableOperationType.SET_PROPERTIES.getTypeValue());
+
+ final ByteArrayOutputStream stream = new ByteArrayOutputStream();
+ try {
+ ReadWriteIOUtils.write(properties, stream);
+ } catch (IOException ignored) {
+ // ByteArrayOutputStream won't throw IOException
+ }
+ req.setUpdateInfo(stream.toByteArray());
+
+ do {
+ try {
+ tsStatus = client.alterTable(req);
+ } catch (final TTransportException e) {
+ if (e.getType() == TTransportException.TIMED_OUT
+ || e.getCause() instanceof SocketTimeoutException) {
+ // time out mainly caused by slow execution, wait until
+ tsStatus = RpcUtils.getStatus(TSStatusCode.OVERLAP_WITH_EXISTING_TASK);
+ } else {
+ throw e;
+ }
+ }
+ // keep waiting until task ends
+ } while (TSStatusCode.OVERLAP_WITH_EXISTING_TASK.getStatusCode() == tsStatus.getCode());
+
+ if (TSStatusCode.SUCCESS_STATUS.getStatusCode() == tsStatus.getCode()
+ || (TSStatusCode.TABLE_NOT_EXISTS.getStatusCode() == tsStatus.getCode() && ifExists)) {
future.set(new ConfigTaskResult(TSStatusCode.SUCCESS_STATUS));
+ } else {
+ LOGGER.warn(
+ "Failed to set properties {} to table {}.{}, status is {}.",
+ properties,
+ database,
+ tableName,
+ tsStatus);
+ future.setException(new IoTDBException(tsStatus.getMessage(), tsStatus.getCode()));
}
} catch (final ClientManagerException | TException e) {
future.setException(e);
diff --git a/iotdb-core/datanode/src/main/java/org/apache/iotdb/db/queryengine/plan/execution/config/executor/IConfigTaskExecutor.java b/iotdb-core/datanode/src/main/java/org/apache/iotdb/db/queryengine/plan/execution/config/executor/IConfigTaskExecutor.java
index 90a407f99aa..3fb6b769be6 100644
--- a/iotdb-core/datanode/src/main/java/org/apache/iotdb/db/queryengine/plan/execution/config/executor/IConfigTaskExecutor.java
+++ b/iotdb-core/datanode/src/main/java/org/apache/iotdb/db/queryengine/plan/execution/config/executor/IConfigTaskExecutor.java
@@ -92,6 +92,7 @@ import org.apache.iotdb.service.rpc.thrift.TPipeTransferResp;
import com.google.common.util.concurrent.SettableFuture;
import java.util.List;
+import java.util.Map;
public interface IConfigTaskExecutor {
@@ -271,21 +272,31 @@ public interface IConfigTaskExecutor {
SettableFuture<ConfigTaskResult> showConfigNodes(ShowConfigNodes showConfigNodes);
- SettableFuture<ConfigTaskResult> useDatabase(Use useDB, IClientSession clientSession);
+ SettableFuture<ConfigTaskResult> useDatabase(final Use useDB, final IClientSession clientSession);
- SettableFuture<ConfigTaskResult> dropDatabase(DropDB dropDB);
+ SettableFuture<ConfigTaskResult> dropDatabase(final DropDB dropDB);
- SettableFuture<ConfigTaskResult> createDatabase(CreateDB createDB);
+ SettableFuture<ConfigTaskResult> createDatabase(final CreateDB createDB);
- SettableFuture<ConfigTaskResult> createTable(TsTable table, String database, boolean ifNotExists);
+ SettableFuture<ConfigTaskResult> createTable(
+ final TsTable table, final String database, final boolean ifNotExists);
- SettableFuture<ConfigTaskResult> describeTable(String database, String tableName);
+ SettableFuture<ConfigTaskResult> describeTable(final String database, final String tableName);
- SettableFuture<ConfigTaskResult> showTables(String database);
+ SettableFuture<ConfigTaskResult> showTables(final String database);
SettableFuture<ConfigTaskResult> alterTableAddColumn(
- String database,
- String tableName,
- List<TsTableColumnSchema> columnSchemaList,
- String queryId);
+ final String database,
+ final String tableName,
+ final List<TsTableColumnSchema> columnSchemaList,
+ final String queryId,
+ final boolean tableIfExists,
+ final boolean columnIfExists);
+
+ SettableFuture<ConfigTaskResult> alterTableSetProperties(
+ final String database,
+ final String tableName,
+ final Map<String, String> properties,
+ final String queryId,
+ final boolean ifExists);
}
diff --git a/iotdb-core/datanode/src/main/java/org/apache/iotdb/db/queryengine/plan/execution/config/metadata/relational/AlterTableAddColumnTask.java b/iotdb-core/datanode/src/main/java/org/apache/iotdb/db/queryengine/plan/execution/config/metadata/relational/AlterTableAddColumnTask.java
index 4a531514650..a6e2c7ab21a 100644
--- a/iotdb-core/datanode/src/main/java/org/apache/iotdb/db/queryengine/plan/execution/config/metadata/relational/AlterTableAddColumnTask.java
+++ b/iotdb-core/datanode/src/main/java/org/apache/iotdb/db/queryengine/plan/execution/config/metadata/relational/AlterTableAddColumnTask.java
@@ -19,28 +19,16 @@
package org.apache.iotdb.db.queryengine.plan.execution.config.metadata.relational;
-import org.apache.iotdb.commons.schema.table.column.AttributeColumnSchema;
-import org.apache.iotdb.commons.schema.table.column.IdColumnSchema;
-import org.apache.iotdb.commons.schema.table.column.MeasurementColumnSchema;
import org.apache.iotdb.commons.schema.table.column.TsTableColumnSchema;
import org.apache.iotdb.commons.utils.PathUtils;
-import org.apache.iotdb.db.exception.sql.SemanticException;
import org.apache.iotdb.db.queryengine.plan.execution.config.ConfigTaskResult;
import org.apache.iotdb.db.queryengine.plan.execution.config.IConfigTask;
import org.apache.iotdb.db.queryengine.plan.execution.config.executor.IConfigTaskExecutor;
-import org.apache.iotdb.db.queryengine.plan.relational.metadata.ColumnSchema;
-import org.apache.iotdb.db.queryengine.plan.relational.type.InternalTypeManager;
import com.google.common.util.concurrent.ListenableFuture;
-import org.apache.tsfile.common.conf.TSFileDescriptor;
-import org.apache.tsfile.enums.TSDataType;
-import org.apache.tsfile.read.common.type.StringType;
-import java.util.ArrayList;
import java.util.List;
-import static org.apache.iotdb.db.utils.EncodingInferenceUtils.getDefaultEncoding;
-
public class AlterTableAddColumnTask implements IConfigTask {
private final String database;
@@ -51,56 +39,30 @@ public class AlterTableAddColumnTask implements IConfigTask {
private final String queryId;
+ private final boolean tableIfExists;
+
+ private final boolean columnIfExists;
+
public AlterTableAddColumnTask(
- String database, String tableName, List<ColumnSchema> columnList, String queryId) {
+ String database,
+ final String tableName,
+ final List<TsTableColumnSchema> columnList,
+ final String queryId,
+ final boolean tableIfExists,
+ final boolean columnIfExists) {
database = PathUtils.qualifyDatabaseName(database);
this.database = database;
this.tableName = tableName;
- this.columnList = parseInputColumnSchema(columnList);
+ this.columnList = columnList;
this.queryId = queryId;
+ this.tableIfExists = tableIfExists;
+ this.columnIfExists = columnIfExists;
}
@Override
- public ListenableFuture<ConfigTaskResult> execute(IConfigTaskExecutor configTaskExecutor)
+ public ListenableFuture<ConfigTaskResult> execute(final IConfigTaskExecutor configTaskExecutor)
throws InterruptedException {
- return configTaskExecutor.alterTableAddColumn(database, tableName, columnList, queryId);
- }
-
- private List<TsTableColumnSchema> parseInputColumnSchema(List<ColumnSchema> inputColumnList) {
- List<TsTableColumnSchema> columnSchemaList = new ArrayList<>(inputColumnList.size());
- for (ColumnSchema inputColumn : inputColumnList) {
- switch (inputColumn.getColumnCategory()) {
- case ID:
- if (!inputColumn.getType().equals(StringType.STRING)) {
- throw new SemanticException("Id column only support data type STRING.");
- }
- columnSchemaList.add(new IdColumnSchema(inputColumn.getName(), TSDataType.STRING));
- break;
- case ATTRIBUTE:
- if (!inputColumn.getType().equals(StringType.STRING)) {
- throw new SemanticException("Attribute column only support data type STRING.");
- }
- columnSchemaList.add(new AttributeColumnSchema(inputColumn.getName(), TSDataType.STRING));
- break;
- case MEASUREMENT:
- TSDataType dataType = InternalTypeManager.getTSDataType(inputColumn.getType());
- columnSchemaList.add(
- new MeasurementColumnSchema(
- inputColumn.getName(),
- dataType,
- getDefaultEncoding(dataType),
- TSFileDescriptor.getInstance().getConfig().getCompressor()));
- break;
- case TIME:
- throw new SemanticException(
- "Adding column for column category "
- + inputColumn.getColumnCategory()
- + " is not supported");
- default:
- throw new IllegalStateException(
- "Unknown ColumnCategory for adding column: " + inputColumn.getColumnCategory());
- }
- }
- return columnSchemaList;
+ return configTaskExecutor.alterTableAddColumn(
+ database, tableName, columnList, queryId, tableIfExists, columnIfExists);
}
}
diff --git a/iotdb-core/datanode/src/main/java/org/apache/iotdb/db/queryengine/plan/execution/config/metadata/relational/CreateTableTask.java b/iotdb-core/datanode/src/main/java/org/apache/iotdb/db/queryengine/plan/execution/config/metadata/relational/AlterTableSetPropertiesTask.java
similarity index 64%
copy from iotdb-core/datanode/src/main/java/org/apache/iotdb/db/queryengine/plan/execution/config/metadata/relational/CreateTableTask.java
copy to iotdb-core/datanode/src/main/java/org/apache/iotdb/db/queryengine/plan/execution/config/metadata/relational/AlterTableSetPropertiesTask.java
index 51a7015525d..4d650c3a53c 100644
--- a/iotdb-core/datanode/src/main/java/org/apache/iotdb/db/queryengine/plan/execution/config/metadata/relational/CreateTableTask.java
+++ b/iotdb-core/datanode/src/main/java/org/apache/iotdb/db/queryengine/plan/execution/config/metadata/relational/AlterTableSetPropertiesTask.java
@@ -19,7 +19,6 @@
package org.apache.iotdb.db.queryengine.plan.execution.config.metadata.relational;
-import org.apache.iotdb.commons.schema.table.TsTable;
import org.apache.iotdb.commons.utils.PathUtils;
import org.apache.iotdb.db.queryengine.plan.execution.config.ConfigTaskResult;
import org.apache.iotdb.db.queryengine.plan.execution.config.IConfigTask;
@@ -27,24 +26,38 @@ import org.apache.iotdb.db.queryengine.plan.execution.config.executor.IConfigTas
import com.google.common.util.concurrent.ListenableFuture;
-public class CreateTableTask implements IConfigTask {
+import java.util.Map;
- private final TsTable table;
+public class AlterTableSetPropertiesTask implements IConfigTask {
private final String database;
- private final boolean ifNotExists;
+ private final String tableName;
- public CreateTableTask(TsTable table, String database, boolean ifNotExists) {
+ private final Map<String, String> properties;
+
+ private final String queryId;
+
+ private final boolean ifExists;
+
+ public AlterTableSetPropertiesTask(
+ String database,
+ final String tableName,
+ final Map<String, String> properties,
+ final String queryId,
+ final boolean ifExists) {
database = PathUtils.qualifyDatabaseName(database);
- this.table = table;
this.database = database;
- this.ifNotExists = ifNotExists;
+ this.tableName = tableName;
+ this.properties = properties;
+ this.queryId = queryId;
+ this.ifExists = ifExists;
}
@Override
- public ListenableFuture<ConfigTaskResult> execute(IConfigTaskExecutor configTaskExecutor)
+ public ListenableFuture<ConfigTaskResult> execute(final IConfigTaskExecutor configTaskExecutor)
throws InterruptedException {
- return configTaskExecutor.createTable(table, database, ifNotExists);
+ return configTaskExecutor.alterTableSetProperties(
+ database, tableName, properties, queryId, ifExists);
}
}
diff --git a/iotdb-core/datanode/src/main/java/org/apache/iotdb/db/queryengine/plan/execution/config/metadata/relational/CreateTableTask.java b/iotdb-core/datanode/src/main/java/org/apache/iotdb/db/queryengine/plan/execution/config/metadata/relational/CreateTableTask.java
index 51a7015525d..96344033046 100644
--- a/iotdb-core/datanode/src/main/java/org/apache/iotdb/db/queryengine/plan/execution/config/metadata/relational/CreateTableTask.java
+++ b/iotdb-core/datanode/src/main/java/org/apache/iotdb/db/queryengine/plan/execution/config/metadata/relational/CreateTableTask.java
@@ -35,7 +35,7 @@ public class CreateTableTask implements IConfigTask {
private final boolean ifNotExists;
- public CreateTableTask(TsTable table, String database, boolean ifNotExists) {
+ public CreateTableTask(final TsTable table, String database, final boolean ifNotExists) {
database = PathUtils.qualifyDatabaseName(database);
this.table = table;
this.database = database;
@@ -43,7 +43,7 @@ public class CreateTableTask implements IConfigTask {
}
@Override
- public ListenableFuture<ConfigTaskResult> execute(IConfigTaskExecutor configTaskExecutor)
+ public ListenableFuture<ConfigTaskResult> execute(final IConfigTaskExecutor configTaskExecutor)
throws InterruptedException {
return configTaskExecutor.createTable(table, database, ifNotExists);
}
diff --git a/iotdb-core/datanode/src/main/java/org/apache/iotdb/db/queryengine/plan/relational/analyzer/StatementAnalyzer.java b/iotdb-core/datanode/src/main/java/org/apache/iotdb/db/queryengine/plan/relational/analyzer/StatementAnalyzer.java
index 7093cbf08dd..8ca82c91eb8 100644
--- a/iotdb-core/datanode/src/main/java/org/apache/iotdb/db/queryengine/plan/relational/analyzer/StatementAnalyzer.java
+++ b/iotdb-core/datanode/src/main/java/org/apache/iotdb/db/queryengine/plan/relational/analyzer/StatementAnalyzer.java
@@ -134,6 +134,7 @@ import java.util.Collection;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
+import java.util.Locale;
import java.util.NoSuchElementException;
import java.util.Objects;
import java.util.Optional;
@@ -154,6 +155,7 @@ import static java.lang.Math.toIntExact;
import static java.util.Collections.emptyList;
import static java.util.Locale.ENGLISH;
import static java.util.Objects.requireNonNull;
+import static org.apache.iotdb.commons.schema.table.TsTable.TABLE_ALLOWED_PROPERTIES_2_DEFAULT_VALUE_MAP;
import static org.apache.iotdb.db.queryengine.execution.warnings.StandardWarningCode.REDUNDANT_ORDER_BY;
import static org.apache.iotdb.db.queryengine.plan.relational.analyzer.AggregationAnalyzer.verifyOrderByAggregations;
import static org.apache.iotdb.db.queryengine.plan.relational.analyzer.AggregationAnalyzer.verifySourceAggregations;
@@ -298,13 +300,13 @@ public class StatementAnalyzer {
}
@Override
- protected Scope visitCreateTable(CreateTable node, Optional<Scope> context) {
+ protected Scope visitCreateTable(final CreateTable node, final Optional<Scope> context) {
validateProperties(node.getProperties(), context);
return createAndAssignScope(node, context);
}
@Override
- protected Scope visitDropTable(DropTable node, Optional<Scope> context) {
+ protected Scope visitDropTable(final DropTable node, final Optional<Scope> context) {
return createAndAssignScope(node, context);
}
@@ -324,7 +326,8 @@ public class StatementAnalyzer {
}
@Override
- protected Scope visitSetProperties(SetProperties node, Optional<Scope> context) {
+ protected Scope visitSetProperties(final SetProperties node, final Optional<Scope> context) {
+ validateProperties(node.getProperties(), context);
return createAndAssignScope(node, context);
}
@@ -2399,15 +2402,26 @@ public class StatementAnalyzer {
return aliases.build();
}
- private void validateProperties(List<Property> properties, Optional<Scope> scope) {
- Set<String> propertyNames = new HashSet<>();
- for (Property property : properties) {
- if (!propertyNames.add(property.getName().getValue())) {
+ private void validateProperties(final List<Property> properties, final Optional<Scope> scope) {
+ final Set<String> propertyNames = new HashSet<>();
+ for (final Property property : properties) {
+ final String key = property.getName().getValue().toLowerCase(Locale.ENGLISH);
+ if (!TABLE_ALLOWED_PROPERTIES_2_DEFAULT_VALUE_MAP.containsKey(key)) {
+ throw new SemanticException("Table property " + key + " is currently not allowed.");
+ }
+ if (!propertyNames.add(key)) {
throw new SemanticException(
String.format("Duplicate property: %s", property.getName().getValue()));
}
+ if (!property.isSetToDefault()) {
+ final Expression value = property.getNonDefaultValue();
+ if (!(value instanceof LongLiteral)) {
+ throw new SemanticException(
+ "TTL' value must be a LongLiteral, but now is: " + value.toString());
+ }
+ }
}
- for (Property property : properties) {
+ for (final Property property : properties) {
process(property, scope);
}
}
diff --git a/iotdb-core/datanode/src/main/java/org/apache/iotdb/db/queryengine/plan/relational/metadata/fetcher/TableHeaderSchemaValidator.java b/iotdb-core/datanode/src/main/java/org/apache/iotdb/db/queryengine/plan/relational/metadata/fetcher/TableHeaderSchemaValidator.java
index c52d870e126..a6103521040 100644
--- a/iotdb-core/datanode/src/main/java/org/apache/iotdb/db/queryengine/plan/relational/metadata/fetcher/TableHeaderSchemaValidator.java
+++ b/iotdb-core/datanode/src/main/java/org/apache/iotdb/db/queryengine/plan/relational/metadata/fetcher/TableHeaderSchemaValidator.java
@@ -38,12 +38,14 @@ import org.apache.iotdb.db.queryengine.plan.execution.config.metadata.relational
import org.apache.iotdb.db.queryengine.plan.execution.config.metadata.relational.CreateTableTask;
import org.apache.iotdb.db.queryengine.plan.relational.metadata.ColumnSchema;
import org.apache.iotdb.db.queryengine.plan.relational.metadata.TableSchema;
+import org.apache.iotdb.db.queryengine.plan.relational.type.InternalTypeManager;
import org.apache.iotdb.db.schemaengine.table.DataNodeTableCache;
import org.apache.iotdb.rpc.TSStatusCode;
import com.google.common.util.concurrent.ListenableFuture;
import org.apache.tsfile.common.conf.TSFileDescriptor;
import org.apache.tsfile.enums.TSDataType;
+import org.apache.tsfile.read.common.type.StringType;
import org.apache.tsfile.read.common.type.TypeFactory;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
@@ -182,71 +184,72 @@ public class TableHeaderSchemaValidator {
}
}
- private void addColumnSchema(List<ColumnSchema> columnSchemas, TsTable tsTable) {
- for (ColumnSchema columnSchema : columnSchemas) {
+ private void addColumnSchema(final List<ColumnSchema> columnSchemas, final TsTable tsTable) {
+ for (final ColumnSchema columnSchema : columnSchemas) {
TsTableColumnCategory category = columnSchema.getColumnCategory();
if (category == null) {
throw new ColumnCreationFailException(
"Cannot create column " + columnSchema.getName() + " category is not provided");
}
- String columnName = columnSchema.getName();
+ final String columnName = columnSchema.getName();
if (tsTable.getColumnSchema(columnName) != null) {
throw new SemanticException(
String.format("Columns in table shall not share the same name %s.", columnName));
}
- TSDataType dataType = getTSDataType(columnSchema.getType());
+ final TSDataType dataType = getTSDataType(columnSchema.getType());
if (dataType == null) {
throw new ColumnCreationFailException(
"Cannot create column " + columnSchema.getName() + " datatype is not provided");
}
- generateColumnSchema(tsTable, category, columnName, dataType);
+ tsTable.addColumnSchema(generateColumnSchema(category, columnName, dataType));
}
}
- public static void generateColumnSchema(
- TsTable tsTable, TsTableColumnCategory category, String columnName, TSDataType dataType) {
+ public static TsTableColumnSchema generateColumnSchema(
+ final TsTableColumnCategory category, final String columnName, final TSDataType dataType) {
switch (category) {
case ID:
if (!TSDataType.STRING.equals(dataType)) {
throw new SemanticException(
"DataType of ID Column should only be STRING, current is " + dataType);
}
- tsTable.addColumnSchema(new IdColumnSchema(columnName, dataType));
- break;
+ return new IdColumnSchema(columnName, dataType);
case ATTRIBUTE:
if (!TSDataType.STRING.equals(dataType)) {
throw new SemanticException(
"DataType of ATTRIBUTE Column should only be STRING, current is " + dataType);
}
- tsTable.addColumnSchema(new AttributeColumnSchema(columnName, dataType));
- break;
+ return new AttributeColumnSchema(columnName, dataType);
case TIME:
throw new SemanticException(
"Create table statement shall not specify column category TIME");
case MEASUREMENT:
- tsTable.addColumnSchema(
- new MeasurementColumnSchema(
- columnName,
- dataType,
- getDefaultEncoding(dataType),
- TSFileDescriptor.getInstance().getConfig().getCompressor()));
- break;
+ return new MeasurementColumnSchema(
+ columnName,
+ dataType,
+ getDefaultEncoding(dataType),
+ TSFileDescriptor.getInstance().getConfig().getCompressor());
default:
throw new IllegalArgumentException();
}
}
private void autoCreateColumn(
- String database,
- String tableName,
- List<ColumnSchema> inputColumnList,
- MPPQueryContext context) {
- AlterTableAddColumnTask task =
+ final String database,
+ final String tableName,
+ final List<ColumnSchema> inputColumnList,
+ final MPPQueryContext context) {
+ final AlterTableAddColumnTask task =
new AlterTableAddColumnTask(
- database, tableName, inputColumnList, context.getQueryId().getId());
+ database,
+ tableName,
+ parseInputColumnSchema(inputColumnList),
+ context.getQueryId().getId(),
+ true,
+ true);
try {
- ListenableFuture<ConfigTaskResult> future = task.execute(configTaskExecutor);
- ConfigTaskResult result = future.get();
+ final ListenableFuture<ConfigTaskResult> future = task.execute(configTaskExecutor);
+ final ConfigTaskResult result = future.get();
if (result.getStatusCode().getStatusCode() != TSStatusCode.SUCCESS_STATUS.getStatusCode()) {
throw new RuntimeException(
new IoTDBException(
@@ -255,9 +258,48 @@ public class TableHeaderSchemaValidator {
database, tableName, inputColumnList),
result.getStatusCode().getStatusCode()));
}
- } catch (ExecutionException | InterruptedException e) {
+ } catch (final ExecutionException | InterruptedException e) {
LOGGER.warn("Auto add table column failed.", e);
throw new RuntimeException(e);
}
}
+
+ private List<TsTableColumnSchema> parseInputColumnSchema(
+ final List<ColumnSchema> inputColumnList) {
+ final List<TsTableColumnSchema> columnSchemaList = new ArrayList<>(inputColumnList.size());
+ for (final ColumnSchema inputColumn : inputColumnList) {
+ switch (inputColumn.getColumnCategory()) {
+ case ID:
+ if (!inputColumn.getType().equals(StringType.STRING)) {
+ throw new SemanticException("Id column only support data type STRING.");
+ }
+ columnSchemaList.add(new IdColumnSchema(inputColumn.getName(), TSDataType.STRING));
+ break;
+ case ATTRIBUTE:
+ if (!inputColumn.getType().equals(StringType.STRING)) {
+ throw new SemanticException("Attribute column only support data type STRING.");
+ }
+ columnSchemaList.add(new AttributeColumnSchema(inputColumn.getName(), TSDataType.STRING));
+ break;
+ case MEASUREMENT:
+ final TSDataType dataType = InternalTypeManager.getTSDataType(inputColumn.getType());
+ columnSchemaList.add(
+ new MeasurementColumnSchema(
+ inputColumn.getName(),
+ dataType,
+ getDefaultEncoding(dataType),
+ TSFileDescriptor.getInstance().getConfig().getCompressor()));
+ break;
+ case TIME:
+ throw new SemanticException(
+ "Adding column for column category "
+ + inputColumn.getColumnCategory()
+ + " is not supported");
+ default:
+ throw new IllegalStateException(
+ "Unknown ColumnCategory for adding column: " + inputColumn.getColumnCategory());
+ }
+ }
+ return columnSchemaList;
+ }
}
diff --git a/iotdb-core/datanode/src/main/java/org/apache/iotdb/db/queryengine/plan/relational/sql/ast/AddColumn.java b/iotdb-core/datanode/src/main/java/org/apache/iotdb/db/queryengine/plan/relational/sql/ast/AddColumn.java
index f29b57fd503..a9cae506bc0 100644
--- a/iotdb-core/datanode/src/main/java/org/apache/iotdb/db/queryengine/plan/relational/sql/ast/AddColumn.java
+++ b/iotdb-core/datanode/src/main/java/org/apache/iotdb/db/queryengine/plan/relational/sql/ast/AddColumn.java
@@ -19,8 +19,7 @@
package org.apache.iotdb.db.queryengine.plan.relational.sql.ast;
-import com.google.common.collect.ImmutableList;
-
+import java.util.Collections;
import java.util.List;
import java.util.Objects;
@@ -32,18 +31,34 @@ public class AddColumn extends Statement {
private final QualifiedName tableName;
private final ColumnDefinition column;
- public AddColumn(QualifiedName tableName, ColumnDefinition column) {
+ private final boolean tableIfExists;
+ private final boolean columnIfNotExists;
+
+ public AddColumn(
+ final QualifiedName tableName,
+ final ColumnDefinition column,
+ final boolean tableIfExists,
+ final boolean columnIfNotExists) {
super(null);
this.tableName = requireNonNull(tableName, "tableName is null");
this.column = requireNonNull(column, "column is null");
+ this.tableIfExists = tableIfExists;
+ this.columnIfNotExists = columnIfNotExists;
}
- public AddColumn(NodeLocation location, QualifiedName tableName, ColumnDefinition column) {
+ public AddColumn(
+ final NodeLocation location,
+ final QualifiedName tableName,
+ final ColumnDefinition column,
+ final boolean tableIfExists,
+ final boolean columnIfNotExists) {
super(requireNonNull(location, "location is null"));
this.tableName = requireNonNull(tableName, "tableName is null");
this.column = requireNonNull(column, "column is null");
+ this.tableIfExists = tableIfExists;
+ this.columnIfNotExists = columnIfNotExists;
}
public QualifiedName getTableName() {
@@ -54,35 +69,51 @@ public class AddColumn extends Statement {
return column;
}
+ public boolean tableIfExists() {
+ return tableIfExists;
+ }
+
+ public boolean columnIfNotExists() {
+ return columnIfNotExists;
+ }
+
@Override
- public <R, C> R accept(AstVisitor<R, C> visitor, C context) {
+ public <R, C> R accept(final AstVisitor<R, C> visitor, final C context) {
return visitor.visitAddColumn(this, context);
}
@Override
- public List<Node> getChildren() {
- return ImmutableList.of(column);
+ public List<? extends Node> getChildren() {
+ return Collections.singletonList(column);
}
@Override
public int hashCode() {
- return Objects.hash(tableName, column);
+ return Objects.hash(tableName, column, tableIfExists, columnIfNotExists);
}
@Override
- public boolean equals(Object obj) {
- if (this == obj) {
+ public boolean equals(final Object o) {
+ if (this == o) {
return true;
}
- if ((obj == null) || (getClass() != obj.getClass())) {
+ if (o == null || getClass() != o.getClass()) {
return false;
}
- AddColumn o = (AddColumn) obj;
- return Objects.equals(tableName, o.tableName) && Objects.equals(column, o.column);
+ final AddColumn that = (AddColumn) o;
+ return tableIfExists == that.tableIfExists
+ && columnIfNotExists == that.columnIfNotExists
+ && Objects.equals(tableName, that.tableName)
+ && Objects.equals(column, that.column);
}
@Override
public String toString() {
- return toStringHelper(this).add("name", tableName).add("column", column).toString();
+ return toStringHelper(this)
+ .add("tableName", tableName)
+ .add("column", column)
+ .add("tableIfExists", tableIfExists)
+ .add("columnIfExists", columnIfNotExists)
+ .toString();
}
}
diff --git a/iotdb-core/datanode/src/main/java/org/apache/iotdb/db/queryengine/plan/relational/sql/ast/ColumnDefinition.java b/iotdb-core/datanode/src/main/java/org/apache/iotdb/db/queryengine/plan/relational/sql/ast/ColumnDefinition.java
index e9b5f61438e..47be606075d 100644
--- a/iotdb-core/datanode/src/main/java/org/apache/iotdb/db/queryengine/plan/relational/sql/ast/ColumnDefinition.java
+++ b/iotdb-core/datanode/src/main/java/org/apache/iotdb/db/queryengine/plan/relational/sql/ast/ColumnDefinition.java
@@ -25,6 +25,7 @@ import com.google.common.collect.ImmutableList;
import javax.annotation.Nullable;
+import java.util.ArrayList;
import java.util.List;
import java.util.Objects;
import java.util.Optional;
@@ -41,10 +42,10 @@ public final class ColumnDefinition extends Node {
@Nullable private final String charsetName;
public ColumnDefinition(
- Identifier name,
- DataType type,
- TsTableColumnCategory columnCategory,
- @Nullable String charsetName) {
+ final Identifier name,
+ final DataType type,
+ final TsTableColumnCategory columnCategory,
+ final @Nullable String charsetName) {
super(null);
this.name = requireNonNull(name, "name is null");
this.type = requireNonNull(type, "type is null");
@@ -53,15 +54,20 @@ public final class ColumnDefinition extends Node {
}
public ColumnDefinition(
- NodeLocation location,
- Identifier name,
+ final NodeLocation location,
+ final Identifier name,
DataType type,
- TsTableColumnCategory columnCategory,
- @Nullable String charsetName) {
+ final TsTableColumnCategory columnCategory,
+ final @Nullable String charsetName) {
super(requireNonNull(location, "location is null"));
this.name = requireNonNull(name, "name is null");
- this.type = requireNonNull(type, "type is null");
this.columnCategory = requireNonNull(columnCategory, "columnCategory is null");
+ if ((columnCategory == TsTableColumnCategory.ID
+ || columnCategory == TsTableColumnCategory.ATTRIBUTE)
+ && (Objects.isNull(type))) {
+ type = new GenericDataType(new Identifier("string"), new ArrayList<>());
+ }
+ this.type = requireNonNull(type, "type is null");
this.charsetName = charsetName;
}
@@ -82,7 +88,7 @@ public final class ColumnDefinition extends Node {
}
@Override
- public <R, C> R accept(AstVisitor<R, C> visitor, C context) {
+ public <R, C> R accept(final AstVisitor<R, C> visitor, C context) {
return visitor.visitColumnDefinition(this, context);
}
@@ -92,14 +98,14 @@ public final class ColumnDefinition extends Node {
}
@Override
- public boolean equals(Object o) {
+ public boolean equals(final Object o) {
if (this == o) {
return true;
}
if (o == null || getClass() != o.getClass()) {
return false;
}
- ColumnDefinition that = (ColumnDefinition) o;
+ final ColumnDefinition that = (ColumnDefinition) o;
return Objects.equals(name, that.name)
&& Objects.equals(type, that.type)
&& columnCategory == that.columnCategory
diff --git a/iotdb-core/datanode/src/main/java/org/apache/iotdb/db/queryengine/plan/relational/sql/ast/CreateTable.java b/iotdb-core/datanode/src/main/java/org/apache/iotdb/db/queryengine/plan/relational/sql/ast/CreateTable.java
index 6a434ac97f4..caca3ff5795 100644
--- a/iotdb-core/datanode/src/main/java/org/apache/iotdb/db/queryengine/plan/relational/sql/ast/CreateTable.java
+++ b/iotdb-core/datanode/src/main/java/org/apache/iotdb/db/queryengine/plan/relational/sql/ast/CreateTable.java
@@ -40,11 +40,11 @@ public class CreateTable extends Statement {
private final List<Property> properties;
public CreateTable(
- QualifiedName name,
- List<ColumnDefinition> elements,
- boolean ifNotExists,
- @Nullable String charsetName,
- List<Property> properties) {
+ final QualifiedName name,
+ final List<ColumnDefinition> elements,
+ final boolean ifNotExists,
+ final @Nullable String charsetName,
+ final List<Property> properties) {
super(null);
this.name = requireNonNull(name, "name is null");
this.elements = ImmutableList.copyOf(requireNonNull(elements, "elements is null"));
@@ -54,12 +54,12 @@ public class CreateTable extends Statement {
}
public CreateTable(
- NodeLocation location,
- QualifiedName name,
- List<ColumnDefinition> elements,
- boolean ifNotExists,
- @Nullable String charsetName,
- List<Property> properties) {
+ final NodeLocation location,
+ final QualifiedName name,
+ final List<ColumnDefinition> elements,
+ final boolean ifNotExists,
+ final @Nullable String charsetName,
+ final List<Property> properties) {
super(requireNonNull(location, "location is null"));
this.name = requireNonNull(name, "name is null");
this.elements = ImmutableList.copyOf(requireNonNull(elements, "elements is null"));
@@ -89,7 +89,7 @@ public class CreateTable extends Statement {
}
@Override
- public <R, C> R accept(AstVisitor<R, C> visitor, C context) {
+ public <R, C> R accept(final AstVisitor<R, C> visitor, final C context) {
return visitor.visitCreateTable(this, context);
}
@@ -99,14 +99,14 @@ public class CreateTable extends Statement {
}
@Override
- public boolean equals(Object o) {
+ public boolean equals(final Object o) {
if (this == o) {
return true;
}
if (o == null || getClass() != o.getClass()) {
return false;
}
- CreateTable that = (CreateTable) o;
+ final CreateTable that = (CreateTable) o;
return ifNotExists == that.ifNotExists
&& Objects.equals(name, that.name)
&& Objects.equals(elements, that.elements)
diff --git a/iotdb-core/datanode/src/main/java/org/apache/iotdb/db/queryengine/plan/relational/sql/ast/DefaultTraversalVisitor.java b/iotdb-core/datanode/src/main/java/org/apache/iotdb/db/queryengine/plan/relational/sql/ast/DefaultTraversalVisitor.java
index 75f5f186f60..ba8ccdd9fcf 100644
--- a/iotdb-core/datanode/src/main/java/org/apache/iotdb/db/queryengine/plan/relational/sql/ast/DefaultTraversalVisitor.java
+++ b/iotdb-core/datanode/src/main/java/org/apache/iotdb/db/queryengine/plan/relational/sql/ast/DefaultTraversalVisitor.java
@@ -388,8 +388,8 @@ public abstract class DefaultTraversalVisitor<C> extends AstVisitor<Void, C> {
}
@Override
- protected Void visitCreateTable(CreateTable node, C context) {
- for (Property property : node.getProperties()) {
+ protected Void visitCreateTable(final CreateTable node, final C context) {
+ for (final Property property : node.getProperties()) {
process(property, context);
}
@@ -406,8 +406,8 @@ public abstract class DefaultTraversalVisitor<C> extends AstVisitor<Void, C> {
}
@Override
- protected Void visitSetProperties(SetProperties node, C context) {
- for (Property property : node.getProperties()) {
+ protected Void visitSetProperties(final SetProperties node, final C context) {
+ for (final Property property : node.getProperties()) {
process(property, context);
}
@@ -415,7 +415,7 @@ public abstract class DefaultTraversalVisitor<C> extends AstVisitor<Void, C> {
}
@Override
- protected Void visitAddColumn(AddColumn node, C context) {
+ protected Void visitAddColumn(final AddColumn node, final C context) {
process(node.getColumn(), context);
return null;
diff --git a/iotdb-core/datanode/src/main/java/org/apache/iotdb/db/queryengine/plan/relational/sql/ast/SetProperties.java b/iotdb-core/datanode/src/main/java/org/apache/iotdb/db/queryengine/plan/relational/sql/ast/SetProperties.java
index 7a962aa0bcf..8d3e91f118f 100644
--- a/iotdb-core/datanode/src/main/java/org/apache/iotdb/db/queryengine/plan/relational/sql/ast/SetProperties.java
+++ b/iotdb-core/datanode/src/main/java/org/apache/iotdb/db/queryengine/plan/relational/sql/ast/SetProperties.java
@@ -39,20 +39,31 @@ public class SetProperties extends Statement {
private final Type type;
private final QualifiedName name;
private final List<Property> properties;
+ private final boolean ifExists;
- public SetProperties(Type type, QualifiedName name, List<Property> properties) {
+ public SetProperties(
+ final Type type,
+ final QualifiedName name,
+ final List<Property> properties,
+ final boolean ifExists) {
super(null);
this.type = requireNonNull(type, "type is null");
this.name = requireNonNull(name, "name is null");
this.properties = ImmutableList.copyOf(requireNonNull(properties, "properties is null"));
+ this.ifExists = ifExists;
}
public SetProperties(
- @Nonnull NodeLocation location, Type type, QualifiedName name, List<Property> properties) {
+ final @Nonnull NodeLocation location,
+ final Type type,
+ final QualifiedName name,
+ final List<Property> properties,
+ final boolean ifExists) {
super(requireNonNull(location, "location is null"));
this.type = requireNonNull(type, "type is null");
this.name = requireNonNull(name, "name is null");
this.properties = ImmutableList.copyOf(requireNonNull(properties, "properties is null"));
+ this.ifExists = ifExists;
}
public Type getType() {
@@ -67,8 +78,12 @@ public class SetProperties extends Statement {
return properties;
}
+ public boolean ifExists() {
+ return ifExists;
+ }
+
@Override
- public <R, C> R accept(AstVisitor<R, C> visitor, C context) {
+ public <R, C> R accept(final AstVisitor<R, C> visitor, final C context) {
return visitor.visitSetProperties(this, context);
}
@@ -79,19 +94,20 @@ public class SetProperties extends Statement {
@Override
public int hashCode() {
- return Objects.hash(type, name, properties);
+ return Objects.hash(type, name, properties, ifExists);
}
@Override
- public boolean equals(Object obj) {
+ public boolean equals(final Object obj) {
if (this == obj) {
return true;
}
if ((obj == null) || (getClass() != obj.getClass())) {
return false;
}
- SetProperties o = (SetProperties) obj;
+ final SetProperties o = (SetProperties) obj;
return type == o.type
+ && ifExists == o.ifExists
&& Objects.equals(name, o.name)
&& Objects.equals(properties, o.properties);
}
@@ -102,6 +118,7 @@ public class SetProperties extends Statement {
.add("type", type)
.add("name", name)
.add("properties", properties)
+ .add("ifExists", ifExists)
.toString();
}
}
diff --git a/iotdb-core/datanode/src/main/java/org/apache/iotdb/db/queryengine/plan/relational/sql/parser/AstBuilder.java b/iotdb-core/datanode/src/main/java/org/apache/iotdb/db/queryengine/plan/relational/sql/parser/AstBuilder.java
index 7926c9226ea..1780c5e0061 100644
--- a/iotdb-core/datanode/src/main/java/org/apache/iotdb/db/queryengine/plan/relational/sql/parser/AstBuilder.java
+++ b/iotdb-core/datanode/src/main/java/org/apache/iotdb/db/queryengine/plan/relational/sql/parser/AstBuilder.java
@@ -167,6 +167,7 @@ import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
+import java.util.Objects;
import java.util.Optional;
import java.util.function.Function;
@@ -254,7 +255,7 @@ public class AstBuilder extends RelationalSqlBaseVisitor<Node> {
}
@Override
- public Node visitCreateTableStatement(RelationalSqlParser.CreateTableStatementContext ctx) {
+ public Node visitCreateTableStatement(final RelationalSqlParser.CreateTableStatementContext ctx) {
List<Property> properties = ImmutableList.of();
if (ctx.properties() != null) {
properties = visit(ctx.properties().propertyAssignments().property(), Property.class);
@@ -271,11 +272,11 @@ public class AstBuilder extends RelationalSqlBaseVisitor<Node> {
}
@Override
- public Node visitColumnDefinition(RelationalSqlParser.ColumnDefinitionContext ctx) {
+ public Node visitColumnDefinition(final RelationalSqlParser.ColumnDefinitionContext ctx) {
return new ColumnDefinition(
getLocation(ctx),
lowerIdentifier((Identifier) visit(ctx.identifier())),
- (DataType) visit(ctx.type()),
+ Objects.nonNull(ctx.type()) ? (DataType) visit(ctx.type()) : null,
getColumnCategory(ctx.columnCategory),
ctx.charsetName() == null
? null
@@ -309,8 +310,13 @@ public class AstBuilder extends RelationalSqlBaseVisitor<Node> {
}
@Override
- public Node visitAddColumn(RelationalSqlParser.AddColumnContext ctx) {
- return new AddColumn(getQualifiedName(ctx.tableName), (ColumnDefinition) visit(ctx.column));
+ public Node visitAddColumn(final RelationalSqlParser.AddColumnContext ctx) {
+ return new AddColumn(
+ getLocation(ctx),
+ getQualifiedName(ctx.tableName),
+ (ColumnDefinition) visit(ctx.column),
+ ctx.EXISTS().size() == (Objects.nonNull(ctx.NOT()) ? 2 : 1),
+ Objects.nonNull(ctx.NOT()));
}
@Override
@@ -331,7 +337,7 @@ public class AstBuilder extends RelationalSqlBaseVisitor<Node> {
}
@Override
- public Node visitSetTableProperties(RelationalSqlParser.SetTablePropertiesContext ctx) {
+ public Node visitSetTableProperties(final RelationalSqlParser.SetTablePropertiesContext ctx) {
List<Property> properties = ImmutableList.of();
if (ctx.propertyAssignments() != null) {
properties = visit(ctx.propertyAssignments().property(), Property.class);
@@ -340,7 +346,8 @@ public class AstBuilder extends RelationalSqlBaseVisitor<Node> {
getLocation(ctx),
SetProperties.Type.TABLE,
getQualifiedName(ctx.qualifiedName()),
- properties);
+ properties,
+ Objects.nonNull(ctx.EXISTS()));
}
@Override
diff --git a/iotdb-core/datanode/src/main/java/org/apache/iotdb/db/queryengine/plan/relational/sql/util/SqlFormatter.java b/iotdb-core/datanode/src/main/java/org/apache/iotdb/db/queryengine/plan/relational/sql/util/SqlFormatter.java
index 97cb104384e..42361d09a25 100644
--- a/iotdb-core/datanode/src/main/java/org/apache/iotdb/db/queryengine/plan/relational/sql/util/SqlFormatter.java
+++ b/iotdb-core/datanode/src/main/java/org/apache/iotdb/db/queryengine/plan/relational/sql/util/SqlFormatter.java
@@ -508,16 +508,16 @@ public final class SqlFormatter {
}
@Override
- protected Void visitCreateTable(CreateTable node, Integer indent) {
+ protected Void visitCreateTable(final CreateTable node, final Integer indent) {
builder.append("CREATE TABLE ");
if (node.isIfNotExists()) {
builder.append("IF NOT EXISTS ");
}
- String tableName = formatName(node.getName());
+ final String tableName = formatName(node.getName());
builder.append(tableName).append(" (\n");
- String elementIndent = indentString(indent + 1);
- String columnList =
+ final String elementIndent = indentString(indent + 1);
+ final String columnList =
node.getElements().stream()
.map(
element -> {
@@ -602,8 +602,8 @@ public final class SqlFormatter {
}
@Override
- protected Void visitSetProperties(SetProperties node, Integer context) {
- SetProperties.Type type = node.getType();
+ protected Void visitSetProperties(final SetProperties node, final Integer context) {
+ final SetProperties.Type type = node.getType();
builder.append("ALTER ");
switch (type) {
case TABLE:
@@ -611,6 +611,9 @@ public final class SqlFormatter {
case MATERIALIZED_VIEW:
builder.append("MATERIALIZED VIEW ");
}
+ if (node.ifExists()) {
+ builder.append("IF EXISTS ");
+ }
builder
.append(formatName(node.getName()))
@@ -654,10 +657,17 @@ public final class SqlFormatter {
}
@Override
- protected Void visitAddColumn(AddColumn node, Integer indent) {
+ protected Void visitAddColumn(final AddColumn node, final Integer indent) {
builder.append("ALTER TABLE ");
+ if (node.tableIfExists()) {
+ builder.append("IF EXISTS ");
+ }
builder.append(formatName(node.getTableName())).append(" ADD COLUMN ");
+ if (node.columnIfNotExists()) {
+ builder.append("IF NOT EXISTS ");
+ }
+
builder.append(formatColumnDefinition(node.getColumn()));
return null;
diff --git a/iotdb-core/node-commons/src/main/java/org/apache/iotdb/commons/schema/table/AlterTableOperationType.java b/iotdb-core/node-commons/src/main/java/org/apache/iotdb/commons/schema/table/AlterTableOperationType.java
index fe9d8b7e4fa..33e4b3aa36d 100644
--- a/iotdb-core/node-commons/src/main/java/org/apache/iotdb/commons/schema/table/AlterTableOperationType.java
+++ b/iotdb-core/node-commons/src/main/java/org/apache/iotdb/commons/schema/table/AlterTableOperationType.java
@@ -20,11 +20,12 @@
package org.apache.iotdb.commons.schema.table;
public enum AlterTableOperationType {
- ADD_COLUMN((byte) 0);
+ ADD_COLUMN((byte) 0),
+ SET_PROPERTIES((byte) 1);
private final byte type;
- AlterTableOperationType(byte type) {
+ AlterTableOperationType(final byte type) {
this.type = type;
}
@@ -32,10 +33,12 @@ public enum AlterTableOperationType {
return type;
}
- public static AlterTableOperationType getType(byte value) {
+ public static AlterTableOperationType getType(final byte value) {
switch (value) {
case 0:
return ADD_COLUMN;
+ case 1:
+ return SET_PROPERTIES;
default:
throw new IllegalArgumentException();
}
diff --git a/iotdb-core/node-commons/src/main/java/org/apache/iotdb/commons/schema/table/TsTable.java b/iotdb-core/node-commons/src/main/java/org/apache/iotdb/commons/schema/table/TsTable.java
index f4360e5f807..5d7cc60ab2f 100644
--- a/iotdb-core/node-commons/src/main/java/org/apache/iotdb/commons/schema/table/TsTable.java
+++ b/iotdb-core/node-commons/src/main/java/org/apache/iotdb/commons/schema/table/TsTable.java
@@ -155,7 +155,7 @@ public class TsTable {
}
}
- public void addProp(String key, String value) {
+ public void addProp(final String key, final String value) {
readWriteLock.writeLock().lock();
try {
if (props == null) {
@@ -167,6 +167,18 @@ public class TsTable {
}
}
+ public void removeProp(final String key) {
+ readWriteLock.writeLock().lock();
+ try {
+ if (props == null) {
+ return;
+ }
+ props.remove(key);
+ } finally {
+ readWriteLock.writeLock().unlock();
+ }
+ }
+
public byte[] serialize() {
ByteArrayOutputStream stream = new ByteArrayOutputStream();
try {
diff --git a/iotdb-core/relational-grammar/src/main/antlr4/org/apache/iotdb/db/relational/grammar/sql/RelationalSql.g4 b/iotdb-core/relational-grammar/src/main/antlr4/org/apache/iotdb/db/relational/grammar/sql/RelationalSql.g4
index e0359b2bf9d..0e61741d4c8 100644
--- a/iotdb-core/relational-grammar/src/main/antlr4/org/apache/iotdb/db/relational/grammar/sql/RelationalSql.g4
+++ b/iotdb-core/relational-grammar/src/main/antlr4/org/apache/iotdb/db/relational/grammar/sql/RelationalSql.g4
@@ -140,7 +140,8 @@ charsetDesc
;
columnDefinition
- : identifier type (columnCategory=(ID | ATTRIBUTE | TIME | MEASUREMENT))? charsetName?
+ : identifier columnCategory=(ID | ATTRIBUTE) charsetName?
+ | identifier type (columnCategory=(ID | ATTRIBUTE | TIME | MEASUREMENT))? charsetName?
;
charsetName
@@ -163,12 +164,12 @@ descTableStatement
;
alterTableStatement
- : ALTER TABLE from=qualifiedName RENAME TO to=identifier #renameTable
- | ALTER TABLE tableName=qualifiedName ADD COLUMN column=columnDefinition #addColumn
- | ALTER TABLE tableName=qualifiedName RENAME COLUMN from=identifier TO to=identifier #renameColumn
- | ALTER TABLE tableName=qualifiedName DROP COLUMN column=identifier #dropColumn
+ : ALTER TABLE (IF EXISTS)? from=qualifiedName RENAME TO to=identifier #renameTable
+ | ALTER TABLE (IF EXISTS)? tableName=qualifiedName ADD COLUMN (IF NOT EXISTS)? column=columnDefinition #addColumn
+ | ALTER TABLE (IF EXISTS)? tableName=qualifiedName RENAME COLUMN (IF EXISTS)? from=identifier TO to=identifier #renameColumn
+ | ALTER TABLE (IF EXISTS)? tableName=qualifiedName DROP COLUMN (IF EXISTS)? column=identifier #dropColumn
// set TTL can use this
- | ALTER TABLE tableName=qualifiedName SET PROPERTIES propertyAssignments #setTableProperties
+ | ALTER TABLE (IF EXISTS)? tableName=qualifiedName SET PROPERTIES propertyAssignments #setTableProperties
;