Skip to content

Commit 3ba0903

Browse files
authored
Allow time column specification in table creation & Enhance table IT's TTL for insertion & disable SCHEMA_REPLICATION_FACTOR / DATA_REPLICATION_FACTOR in table database definition
1 parent 8491391 commit 3ba0903

File tree

7 files changed

+83
-50
lines changed

7 files changed

+83
-50
lines changed

integration-test/src/test/java/org/apache/iotdb/relational/it/schema/IoTDBDatabaseIT.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -157,10 +157,9 @@ public void testManageDatabase() {
157157

158158
// Test create database with properties
159159
statement.execute(
160-
"create database test_prop with (ttl=300, schema_replication_factor=DEFAULT, data_replication_factor=3, time_partition_interval=100000)");
160+
"create database test_prop with (ttl=300, schema_region_group_num=DEFAULT, time_partition_interval=100000)");
161161
databaseNames = new String[] {"test_prop"};
162162
TTLs = new String[] {"300"};
163-
dataReplicaFactors = new int[] {3};
164163
timePartitionInterval = new int[] {100000};
165164

166165
// show

integration-test/src/test/java/org/apache/iotdb/relational/it/schema/IoTDBTableIT.java

Lines changed: 35 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ public void testManageTable() {
6868
final Statement statement = connection.createStatement()) {
6969

7070
statement.execute("create database test1");
71-
statement.execute("create database test2 with (ttl=300)");
71+
statement.execute("create database test2 with (ttl=3000000)");
7272

7373
// should specify database before create table
7474
try {
@@ -130,22 +130,22 @@ public void testManageTable() {
130130
}
131131

132132
// Alter table properties
133-
statement.execute("alter table test1.table1 set properties ttl=1");
134-
ttls = new String[] {"1"};
133+
statement.execute("alter table test1.table1 set properties ttl=1000000");
134+
ttls = new String[] {"1000000"};
135135

136136
// Alter non-exist table
137137
try {
138-
statement.execute("alter table test1.nonExist set properties ttl=1");
138+
statement.execute("alter table test1.nonExist set properties ttl=1000000");
139139
} catch (final SQLException e) {
140140
assertEquals("550: Table 'test1.nonexist' does not exist", e.getMessage());
141141
}
142142

143143
// If exists
144-
statement.execute("alter table if exists test1.nonExist set properties ttl=1");
144+
statement.execute("alter table if exists test1.nonExist set properties ttl=1000000");
145145

146146
// Alter non-supported properties
147147
try {
148-
statement.execute("alter table test1.table1 set properties nonSupport=1");
148+
statement.execute("alter table test1.table1 set properties nonSupport=1000000");
149149
} catch (final SQLException e) {
150150
assertEquals("701: Table property 'nonsupport' is currently not allowed.", e.getMessage());
151151
}
@@ -191,16 +191,6 @@ public void testManageTable() {
191191
statement.execute(
192192
"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)");
193193

194-
try {
195-
statement.execute(
196-
"create table table2(region_id STRING ID, plant_id STRING ID, device_id STRING ID, model STRING ATTRIBUTE, temperature FLOAT MEASUREMENT, humidity DOUBLE MEASUREMENT, time2 INT64 TIME) with (TTL=3600000)");
197-
fail();
198-
} catch (final SQLException e) {
199-
assertEquals(
200-
"701: Create table or add column statement shall not specify column category TIME",
201-
e.getMessage());
202-
}
203-
204194
try {
205195
statement.execute(
206196
"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)");
@@ -291,7 +281,7 @@ public void testManageTable() {
291281
statement.execute("create table table3()");
292282

293283
tableNames = new String[] {"table3", "table2"};
294-
ttls = new String[] {"300", "6600000"};
284+
ttls = new String[] {"3000000", "6600000"};
295285

296286
// show tables from current database
297287
try (final ResultSet resultSet = statement.executeQuery("SHOW tables")) {
@@ -310,10 +300,10 @@ public void testManageTable() {
310300
assertEquals(tableNames.length, cnt);
311301
}
312302

313-
statement.execute("alter table table3 set properties ttl=300");
303+
statement.execute("alter table table3 set properties ttl=1000000");
314304
statement.execute("alter table table3 set properties ttl=DEFAULT");
315305

316-
// The table3's ttl shall be also 300
306+
// The table3's ttl shall be also 3000000
317307
try (final ResultSet resultSet = statement.executeQuery("SHOW tables")) {
318308
int cnt = 0;
319309
ResultSetMetaData metaData = resultSet.getMetaData();
@@ -535,6 +525,32 @@ public void testManageTable() {
535525
assertEquals("500: Unknown database test1", e.getMessage());
536526
}
537527

528+
// Test time column
529+
statement.execute("create table test100 (time time)");
530+
statement.execute("create table test101 (time timestamp time)");
531+
532+
try {
533+
statement.execute("create table test102 (time timestamp id)");
534+
fail();
535+
} catch (final SQLException e) {
536+
assertEquals(
537+
"701: The time column category shall be bounded with column name 'time'.",
538+
e.getMessage());
539+
}
540+
541+
try {
542+
statement.execute("create table test102 (time id)");
543+
fail();
544+
} catch (final SQLException e) {
545+
assertEquals("701: The time column's type shall be 'timestamp'.", e.getMessage());
546+
}
547+
548+
try {
549+
statement.execute("create table test102 (time time, time time)");
550+
fail();
551+
} catch (final SQLException e) {
552+
assertEquals("701: Columns in table shall not share the same name time.", e.getMessage());
553+
}
538554
} catch (final SQLException e) {
539555
e.printStackTrace();
540556
fail(e.getMessage());

iotdb-core/datanode/src/main/java/org/apache/iotdb/db/queryengine/plan/execution/config/TableConfigTaskVisitor.java

Lines changed: 24 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -146,9 +146,7 @@
146146
import static org.apache.iotdb.commons.schema.table.TsTable.TABLE_ALLOWED_PROPERTIES;
147147
import static org.apache.iotdb.commons.schema.table.TsTable.TTL_PROPERTY;
148148
import static org.apache.iotdb.db.queryengine.plan.execution.config.metadata.relational.CreateDBTask.DATA_REGION_GROUP_NUM_KEY;
149-
import static org.apache.iotdb.db.queryengine.plan.execution.config.metadata.relational.CreateDBTask.DATA_REPLICATION_FACTOR_KEY;
150149
import static org.apache.iotdb.db.queryengine.plan.execution.config.metadata.relational.CreateDBTask.SCHEMA_REGION_GROUP_NUM_KEY;
151-
import static org.apache.iotdb.db.queryengine.plan.execution.config.metadata.relational.CreateDBTask.SCHEMA_REPLICATION_FACTOR_KEY;
152150
import static org.apache.iotdb.db.queryengine.plan.execution.config.metadata.relational.CreateDBTask.TIME_PARTITION_INTERVAL_KEY;
153151
import static org.apache.iotdb.db.queryengine.plan.execution.config.metadata.relational.CreateDBTask.TTL_KEY;
154152
import static org.apache.iotdb.db.queryengine.plan.relational.type.InternalTypeManager.getTSDataType;
@@ -193,8 +191,6 @@ protected IConfigTask visitCreateDB(final CreateDB node, final MPPQueryContext c
193191
if (property.isSetToDefault()) {
194192
switch (key) {
195193
case TTL_KEY:
196-
case SCHEMA_REPLICATION_FACTOR_KEY:
197-
case DATA_REPLICATION_FACTOR_KEY:
198194
case TIME_PARTITION_INTERVAL_KEY:
199195
case SCHEMA_REGION_GROUP_NUM_KEY:
200196
case DATA_REGION_GROUP_NUM_KEY:
@@ -219,13 +215,6 @@ protected IConfigTask visitCreateDB(final CreateDB node, final MPPQueryContext c
219215
}
220216
schema.setTTL(parseLongFromLiteral(value, TTL_KEY));
221217
break;
222-
case SCHEMA_REPLICATION_FACTOR_KEY:
223-
schema.setSchemaReplicationFactor(
224-
parseIntFromLiteral(value, SCHEMA_REPLICATION_FACTOR_KEY));
225-
break;
226-
case DATA_REPLICATION_FACTOR_KEY:
227-
schema.setDataReplicationFactor(parseIntFromLiteral(value, DATA_REPLICATION_FACTOR_KEY));
228-
break;
229218
case TIME_PARTITION_INTERVAL_KEY:
230219
schema.setTimePartitionInterval(parseLongFromLiteral(value, TIME_PARTITION_INTERVAL_KEY));
231220
break;
@@ -323,20 +312,43 @@ protected IConfigTask visitCreateTable(final CreateTable node, final MPPQueryCon
323312
table.setProps(convertPropertiesToMap(node.getProperties(), false));
324313

325314
// TODO: Place the check at statement analyzer
315+
boolean hasTimeColumn = false;
326316
for (final ColumnDefinition columnDefinition : node.getElements()) {
327317
final TsTableColumnCategory category = columnDefinition.getColumnCategory();
328318
final String columnName = columnDefinition.getName().getValue();
319+
final TSDataType dataType = getDataType(columnDefinition.getType());
320+
if (checkTimeColumnIdempotent(category, columnName, dataType) && !hasTimeColumn) {
321+
hasTimeColumn = true;
322+
continue;
323+
}
329324
if (table.getColumnSchema(columnName) != null) {
330325
throw new SemanticException(
331326
String.format("Columns in table shall not share the same name %s.", columnName));
332327
}
333-
final TSDataType dataType = getDataType(columnDefinition.getType());
334328
table.addColumnSchema(
335329
TableHeaderSchemaValidator.generateColumnSchema(category, columnName, dataType));
336330
}
337331
return new CreateTableTask(table, databaseTablePair.getLeft(), node.isIfNotExists());
338332
}
339333

334+
private boolean checkTimeColumnIdempotent(
335+
final TsTableColumnCategory category, final String columnName, final TSDataType dataType) {
336+
if (category == TsTableColumnCategory.TIME || columnName.equals(TsTable.TIME_COLUMN_NAME)) {
337+
if (category == TsTableColumnCategory.TIME
338+
&& columnName.equals(TsTable.TIME_COLUMN_NAME)
339+
&& dataType == TSDataType.TIMESTAMP) {
340+
return true;
341+
} else if (dataType == TSDataType.TIMESTAMP) {
342+
throw new SemanticException(
343+
"The time column category shall be bounded with column name 'time'.");
344+
} else {
345+
throw new SemanticException("The time column's type shall be 'timestamp'.");
346+
}
347+
}
348+
349+
return false;
350+
}
351+
340352
@Override
341353
protected IConfigTask visitRenameTable(final RenameTable node, final MPPQueryContext context) {
342354
context.setQueryType(QueryType.WRITE);

iotdb-core/datanode/src/main/java/org/apache/iotdb/db/queryengine/plan/execution/config/metadata/relational/CreateDBTask.java

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,12 +30,14 @@ public class CreateDBTask implements IConfigTask {
3030

3131
/////////////////////////////// Allowed properties ///////////////////////////////
3232
public static final String TTL_KEY = "ttl";
33-
public static final String SCHEMA_REPLICATION_FACTOR_KEY = "schema_replication_factor";
34-
public static final String DATA_REPLICATION_FACTOR_KEY = "data_replication_factor";
3533
public static final String TIME_PARTITION_INTERVAL_KEY = "time_partition_interval";
3634
public static final String SCHEMA_REGION_GROUP_NUM_KEY = "schema_region_group_num";
3735
public static final String DATA_REGION_GROUP_NUM_KEY = "data_region_group_num";
3836

37+
// Deprecated
38+
public static final String SCHEMA_REPLICATION_FACTOR_KEY = "schema_replication_factor";
39+
public static final String DATA_REPLICATION_FACTOR_KEY = "data_replication_factor";
40+
3941
/////////////////////////////// Fields ///////////////////////////////
4042

4143
private final TDatabaseSchema schema;

iotdb-core/datanode/src/main/java/org/apache/iotdb/db/queryengine/plan/relational/sql/ast/ColumnDefinition.java

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -62,10 +62,13 @@ public ColumnDefinition(
6262
super(requireNonNull(location, "location is null"));
6363
this.name = requireNonNull(name, "name is null");
6464
this.columnCategory = requireNonNull(columnCategory, "columnCategory is null");
65-
if ((columnCategory == TsTableColumnCategory.ID
66-
|| columnCategory == TsTableColumnCategory.ATTRIBUTE)
67-
&& (Objects.isNull(type))) {
68-
type = new GenericDataType(new Identifier("string"), new ArrayList<>());
65+
if (Objects.isNull(type)) {
66+
if ((columnCategory == TsTableColumnCategory.ID
67+
|| columnCategory == TsTableColumnCategory.ATTRIBUTE)) {
68+
type = new GenericDataType(new Identifier("string"), new ArrayList<>());
69+
} else if (columnCategory == TsTableColumnCategory.TIME) {
70+
type = new GenericDataType(new Identifier("timestamp"), new ArrayList<>());
71+
}
6972
}
7073
this.type = requireNonNull(type, "type is null");
7174
this.charsetName = charsetName;

iotdb-core/node-commons/src/main/java/org/apache/iotdb/commons/schema/table/column/TimeColumnSchema.java

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -28,11 +28,12 @@
2828
import java.util.Map;
2929

3030
public class TimeColumnSchema extends TsTableColumnSchema {
31-
public TimeColumnSchema(String columnName, TSDataType dataType) {
31+
public TimeColumnSchema(final String columnName, final TSDataType dataType) {
3232
super(columnName, dataType);
3333
}
3434

35-
public TimeColumnSchema(String columnName, TSDataType dataType, Map<String, String> props) {
35+
public TimeColumnSchema(
36+
final String columnName, final TSDataType dataType, final Map<String, String> props) {
3637
super(columnName, dataType, props);
3738
}
3839

@@ -41,17 +42,17 @@ public TsTableColumnCategory getColumnCategory() {
4142
return TsTableColumnCategory.TIME;
4243
}
4344

44-
static TimeColumnSchema deserialize(InputStream stream) throws IOException {
45-
String columnName = ReadWriteIOUtils.readString(stream);
46-
TSDataType dataType = ReadWriteIOUtils.readDataType(stream);
47-
Map<String, String> props = ReadWriteIOUtils.readMap(stream);
45+
static TimeColumnSchema deserialize(final InputStream stream) throws IOException {
46+
final String columnName = ReadWriteIOUtils.readString(stream);
47+
final TSDataType dataType = ReadWriteIOUtils.readDataType(stream);
48+
final Map<String, String> props = ReadWriteIOUtils.readMap(stream);
4849
return new TimeColumnSchema(columnName, dataType, props);
4950
}
5051

51-
static TimeColumnSchema deserialize(ByteBuffer buffer) {
52-
String columnName = ReadWriteIOUtils.readString(buffer);
53-
TSDataType dataType = ReadWriteIOUtils.readDataType(buffer);
54-
Map<String, String> props = ReadWriteIOUtils.readMap(buffer);
52+
static TimeColumnSchema deserialize(final ByteBuffer buffer) {
53+
final String columnName = ReadWriteIOUtils.readString(buffer);
54+
final TSDataType dataType = ReadWriteIOUtils.readDataType(buffer);
55+
final Map<String, String> props = ReadWriteIOUtils.readMap(buffer);
5556
return new TimeColumnSchema(columnName, dataType, props);
5657
}
5758
}

iotdb-core/relational-grammar/src/main/antlr4/org/apache/iotdb/db/relational/grammar/sql/RelationalSql.g4

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -156,7 +156,7 @@ charsetDesc
156156
;
157157

158158
columnDefinition
159-
: identifier columnCategory=(ID | ATTRIBUTE) charsetName?
159+
: identifier columnCategory=(ID | ATTRIBUTE | TIME) charsetName?
160160
| identifier type (columnCategory=(ID | ATTRIBUTE | TIME | MEASUREMENT))? charsetName?
161161
;
162162

0 commit comments

Comments
 (0)