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

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -69,12 +69,12 @@ public void testManageTable() {
// should specify database before create table
try {
statement.execute(
"create table table1(region_id STRING ID, plant_id STRING ID, device_id STRING ID, model STRING ATTRIBUTE, temperature FLOAT MEASUREMENT, humidity DOUBLE MEASUREMENT) with (TTL=3600000)");
"create table table1(region_id STRING ID, plant_id STRING ID, device_id STRING ID, model STRING ATTRIBUTE, temperature FLOAT MEASUREMENT, humidity DOUBLE MEASUREMENT)");
} catch (final SQLException e) {
assertEquals("701: database is not specified", e.getMessage());
}

// Show tables shall succeed in a newly created database
// Show tables shall succeed in a newly created database with no tables
try (final ResultSet resultSet = statement.executeQuery("SHOW tables from test1")) {
ResultSetMetaData metaData = resultSet.getMetaData();
assertEquals(showTablesColumnHeaders.size(), metaData.getColumnCount());
Expand All @@ -87,10 +87,59 @@ public void testManageTable() {

// or use full qualified table name
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=3600000)");
"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)");

try {
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)");
} catch (final SQLException e) {
assertEquals("551: Table 'test1.table1' already exists.", e.getMessage());
}

String[] tableNames = new String[] {"table1"};
String[] ttls = new String[] {"INF"};

statement.execute("use test2");

// show tables by specifying another database
// Check duplicate create table won't affect table state
// using SHOW tables in
try (final ResultSet resultSet = statement.executeQuery("SHOW tables in test1")) {
int cnt = 0;
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);
}

// using SHOW tables from
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);
}

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)");

try {
statement.execute(
"create table table2(region_id TEXT ID, plant_id STRING ID, device_id STRING ID, model STRING ATTRIBUTE, temperature FLOAT MEASUREMENT, humidity DOUBLE MEASUREMENT) with (TTL=3600000)");
Expand Down Expand Up @@ -128,8 +177,8 @@ public void testManageTable() {
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)");

String[] tableNames = new String[] {"table2"};
String[] ttls = new String[] {"6600000"};
tableNames = new String[] {"table2"};
ttls = new String[] {"6600000"};

// show tables from current database
try (final ResultSet resultSet = statement.executeQuery("SHOW tables")) {
Expand All @@ -148,44 +197,6 @@ public void testManageTable() {
assertEquals(tableNames.length, cnt);
}

// show tables by specifying another database
tableNames = new String[] {"table1"};
ttls = new String[] {"3600000"};

// using SHOW tables in
try (final ResultSet resultSet = statement.executeQuery("SHOW tables in test1")) {
int cnt = 0;
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);
}

// using SHOW tables from
try (final ResultSet resultSet = statement.executeQuery("SHOW tables from test1")) {
int cnt = 0;
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);
}

// show tables from a non-exist database
try {
statement.executeQuery("SHOW tables from test3");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,7 @@
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.Optional;
import java.util.Set;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.locks.ReentrantLock;
Expand Down Expand Up @@ -1106,7 +1107,7 @@ public void updateSchemaQuotaConfiguration(long seriesThreshold, long deviceThre
schemaQuotaStatistics.setSeriesThreshold(seriesThreshold);
}

public TsTable getTable(String database, String tableName) {
public Optional<TsTable> getTable(final String database, final String tableName) {
return clusterSchemaInfo.getTsTable(database, tableName);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,7 @@
import java.util.List;
import java.util.Locale;
import java.util.Map;
import java.util.Optional;
import java.util.Set;
import java.util.UUID;
import java.util.concurrent.locks.ReentrantReadWriteLock;
Expand Down Expand Up @@ -1118,7 +1119,7 @@ public Map<String, List<TsTable>> getAllPreCreateTables() {
}
}

public TsTable getTsTable(String database, String tableName) {
public Optional<TsTable> getTsTable(String database, String tableName) {
databaseReadWriteLock.readLock().lock();
try {
return mTree.getTable(new PartialPath(database), tableName);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.Set;
import java.util.Stack;
import java.util.TreeSet;
Expand Down Expand Up @@ -718,14 +719,14 @@ public Map<String, List<TsTable>> getAllPreCreateTables() throws MetadataExcepti
return result;
}

public TsTable getTable(PartialPath database, String tableName) throws MetadataException {
IConfigMNode databaseNode = getDatabaseNodeByDatabasePath(database).getAsMNode();
public Optional<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);
return Optional.empty();
}
ConfigTableNode tableNode = (ConfigTableNode) databaseNode.getChild(tableName);
return tableNode.getTable();
final ConfigTableNode tableNode = (ConfigTableNode) databaseNode.getChild(tableName);
return Optional.of(tableNode.getTable());
}

public void addTableColumn(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@

import static org.apache.iotdb.commons.conf.IoTDBConstant.MULTI_LEVEL_PATH_WILDCARD;
import static org.apache.iotdb.commons.schema.SchemaConstant.ROOT;
import static org.apache.iotdb.rpc.TSStatusCode.TABLE_ALREADY_EXISTS;

public class CreateTableProcedure
extends StateMachineProcedure<ConfigNodeProcedureEnv, CreateTableState> {
Expand All @@ -91,6 +92,10 @@ protected Flow executeFromState(final ConfigNodeProcedureEnv env, final CreateTa
final long startTime = System.currentTimeMillis();
try {
switch (state) {
case CHECK_TABLE_EXISTENCE:
LOGGER.info("Check the existence of table {}.{}", database, table.getTableName());
checkTableExistence(env);
break;
case PRE_CREATE:
LOGGER.info("Pre create table {}.{}", database, table.getTableName());
preCreateTable(env);
Expand Down Expand Up @@ -127,6 +132,23 @@ protected Flow executeFromState(final ConfigNodeProcedureEnv env, final CreateTa
}
}

private void checkTableExistence(final ConfigNodeProcedureEnv env) {
if (env.getConfigManager()
.getClusterSchemaManager()
.getTable(database, table.getTableName())
.isPresent()) {
setFailure(
new ProcedureException(
new IoTDBException(
String.format(
"Table '%s.%s' already exists.",
database.substring(ROOT.length() + 1), table.getTableName()),
TABLE_ALREADY_EXISTS.getStatusCode())));
} else {
setNextState(CreateTableState.PRE_CREATE);
}
}

private void preCreateTable(final ConfigNodeProcedureEnv env) {
final PreCreateTablePlan plan = new PreCreateTablePlan(database, table);
TSStatus status;
Expand Down Expand Up @@ -390,7 +412,7 @@ protected int getStateId(final CreateTableState createTableState) {

@Override
protected CreateTableState getInitialState() {
return CreateTableState.PRE_CREATE;
return CreateTableState.CHECK_TABLE_EXISTENCE;
}

public String getDatabase() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
package org.apache.iotdb.confignode.procedure.state.schema;

public enum CreateTableState {
CHECK_TABLE_EXISTENCE,
PRE_CREATE,
PRE_RELEASE,
VALIDATE_TIMESERIES_EXISTENCE,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ public class TableAlreadyExistsException extends MetadataException {

public TableAlreadyExistsException(String database, String tableName) {
super(
String.format("Table %s.%s already exists.", database, tableName),
String.format("Table '%s.%s' already exists.", database, tableName),
TSStatusCode.TABLE_ALREADY_EXISTS.getStatusCode());
}
}