diff --git a/integration-test/src/test/java/org/apache/iotdb/relational/it/schema/IoTDBDatabaseIT.java b/integration-test/src/test/java/org/apache/iotdb/relational/it/schema/IoTDBDatabaseIT.java index 94c215a52ae21..195237275478c 100644 --- a/integration-test/src/test/java/org/apache/iotdb/relational/it/schema/IoTDBDatabaseIT.java +++ b/integration-test/src/test/java/org/apache/iotdb/relational/it/schema/IoTDBDatabaseIT.java @@ -716,7 +716,7 @@ public void testInformationSchema() throws SQLException { statement.executeQuery("select * from views"), "database,table_name,view_definition,", Collections.singleton( - "test,view_table,CREATE VIEW \"view_table\" (\"tag1\" STRING TAG,\"tag2\" STRING TAG,\"s11\" INT32 FIELD,\"s3\" INT32 FIELD FROM \"s2\") RESTRICT WITH (ttl=100) AS root.\"a\".**,")); + "test,view_table,CREATE VIEW \"view_table\" (\"time\" TIMESTAMP TIME,\"tag1\" STRING TAG,\"tag2\" STRING TAG,\"s11\" INT32 FIELD,\"s3\" INT32 FIELD FROM \"s2\") RESTRICT WITH (ttl=100) AS root.\"a\".**,")); TestUtils.assertResultSetEqual( statement.executeQuery( 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 6663591975501..56cdb19f12f13 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 @@ -306,7 +306,7 @@ public void testManageTable() { } statement.execute( - "create table table2(region_id STRING TAG, plant_id STRING TAG, color STRING ATTRIBUTE, temperature FLOAT FIELD) with (TTL=6600000)"); + "create table table2(t1 TIMESTAMP TIME, region_id STRING TAG, plant_id STRING TAG, color STRING ATTRIBUTE, temperature FLOAT FIELD) with (TTL=6600000)"); statement.execute("alter table table2 add column speed DOUBLE FIELD COMMENT 'fast'"); @@ -314,7 +314,7 @@ public void testManageTable() { statement.executeQuery("show create table table2"), "Table,Create Table,", Collections.singleton( - "table2,CREATE TABLE \"table2\" (\"region_id\" STRING TAG,\"plant_id\" STRING TAG,\"color\" STRING ATTRIBUTE,\"temperature\" FLOAT FIELD,\"speed\" DOUBLE FIELD COMMENT 'fast') WITH (ttl=6600000),")); + "table2,CREATE TABLE \"table2\" (\"t1\" TIMESTAMP TIME,\"region_id\" STRING TAG,\"plant_id\" STRING TAG,\"color\" STRING ATTRIBUTE,\"temperature\" FLOAT FIELD,\"speed\" DOUBLE FIELD COMMENT 'fast') WITH (ttl=6600000),")); try { statement.execute("alter table table2 add column speed DOUBLE FIELD"); @@ -422,7 +422,7 @@ public void testManageTable() { assertEquals(columnNames.length, cnt); } - columnNames = new String[] {"time", "region_id", "plant_id", "color", "temperature", "speed"}; + columnNames = new String[] {"t1", "region_id", "plant_id", "color", "temperature", "speed"}; dataTypes = new String[] {"TIMESTAMP", "STRING", "STRING", "STRING", "FLOAT", "DOUBLE"}; categories = new String[] {"TIME", "TAG", "TAG", "ATTRIBUTE", "FIELD", "FIELD"}; @@ -451,7 +451,7 @@ public void testManageTable() { // Test comment // Before - columnNames = new String[] {"time", "region_id", "plant_id", "temperature", "speed"}; + columnNames = new String[] {"t1", "region_id", "plant_id", "temperature", "speed"}; dataTypes = new String[] {"TIMESTAMP", "STRING", "STRING", "FLOAT", "DOUBLE"}; categories = new String[] {"TIME", "TAG", "TAG", "FIELD", "FIELD"}; statuses = new String[] {"USING", "USING", "USING", "USING", "USING"}; @@ -480,7 +480,7 @@ public void testManageTable() { // After statement.execute("COMMENT ON COLUMN table2.region_id IS '重庆'"); statement.execute("COMMENT ON COLUMN table2.region_id IS NULL"); - statement.execute("COMMENT ON COLUMN test2.table2.time IS 'recent'"); + statement.execute("COMMENT ON COLUMN test2.table2.t1 IS 'recent'"); statement.execute("COMMENT ON COLUMN test2.table2.region_id IS ''"); comments = new String[] {"recent", "", null, null, "fast"}; @@ -527,7 +527,7 @@ public void testManageTable() { } try { - statement.execute("alter table table2 drop column time"); + statement.execute("alter table table2 drop column t1"); } catch (final SQLException e) { assertEquals("701: Dropping tag or time column is not supported.", e.getMessage()); } @@ -619,8 +619,14 @@ public void testManageTable() { // Test time column // More time column tests are included in other IT - statement.execute("create table test100 (time time)"); - statement.execute("create table test101 (time timestamp time)"); + statement.execute("create table test100 (t1 time)"); + statement.execute("create table test101 (t1 timestamp time)"); + + TestUtils.assertResultSetEqual( + statement.executeQuery("show create table test100"), + "Table,Create Table,", + Collections.singleton( + "test100,CREATE TABLE \"test100\" (\"t1\" TIMESTAMP TIME) WITH (ttl='INF'),")); } catch (final SQLException e) { e.printStackTrace(); fail(e.getMessage()); @@ -1066,14 +1072,14 @@ public void testTreeViewTable() throws Exception { statement.executeQuery("show create view view_table"), "View,Create View,", Collections.singleton( - "view_table,CREATE VIEW \"view_table\" (\"tag1\" STRING TAG,\"tag2\" STRING TAG,\"s11\" INT32 FIELD,\"s3\" STRING FIELD FROM \"s2\") RESTRICT WITH (ttl=100) AS root.\"重庆\".\"1\".**,")); + "view_table,CREATE VIEW \"view_table\" (\"time\" TIMESTAMP TIME,\"tag1\" STRING TAG,\"tag2\" STRING TAG,\"s11\" INT32 FIELD,\"s3\" STRING FIELD FROM \"s2\") RESTRICT WITH (ttl=100) AS root.\"重庆\".\"1\".**,")); // Can also use "show create table" TestUtils.assertResultSetEqual( statement.executeQuery("show create table view_table"), "View,Create View,", Collections.singleton( - "view_table,CREATE VIEW \"view_table\" (\"tag1\" STRING TAG,\"tag2\" STRING TAG,\"s11\" INT32 FIELD,\"s3\" STRING FIELD FROM \"s2\") RESTRICT WITH (ttl=100) AS root.\"重庆\".\"1\".**,")); + "view_table,CREATE VIEW \"view_table\" (\"time\" TIMESTAMP TIME,\"tag1\" STRING TAG,\"tag2\" STRING TAG,\"s11\" INT32 FIELD,\"s3\" STRING FIELD FROM \"s2\") RESTRICT WITH (ttl=100) AS root.\"重庆\".\"1\".**,")); statement.execute("create table a ()"); try { diff --git a/iotdb-core/datanode/src/main/java/org/apache/iotdb/db/queryengine/plan/execution/config/metadata/relational/ShowCreateTableTask.java b/iotdb-core/datanode/src/main/java/org/apache/iotdb/db/queryengine/plan/execution/config/metadata/relational/ShowCreateTableTask.java index 64581425f01b6..1d0ede7e03db8 100644 --- a/iotdb-core/datanode/src/main/java/org/apache/iotdb/db/queryengine/plan/execution/config/metadata/relational/ShowCreateTableTask.java +++ b/iotdb-core/datanode/src/main/java/org/apache/iotdb/db/queryengine/plan/execution/config/metadata/relational/ShowCreateTableTask.java @@ -96,7 +96,13 @@ private static String getShowCreateTableSQL(final TsTable table) { .append("TAG"); break; case TIME: - continue; + builder + .append(getIdentifier(schema.getColumnName())) + .append(" ") + .append(schema.getDataType()) + .append(" ") + .append("TIME"); + break; case FIELD: builder .append(getIdentifier(schema.getColumnName())) @@ -123,7 +129,7 @@ private static String getShowCreateTableSQL(final TsTable table) { builder.append(","); } - if (table.getColumnList().size() > 1) { + if (!table.getColumnList().isEmpty()) { builder.deleteCharAt(builder.length() - 1); } diff --git a/iotdb-core/datanode/src/main/java/org/apache/iotdb/db/queryengine/plan/execution/config/metadata/relational/ShowCreateViewTask.java b/iotdb-core/datanode/src/main/java/org/apache/iotdb/db/queryengine/plan/execution/config/metadata/relational/ShowCreateViewTask.java index f4a8578c1e7e2..3c1f8bc73eeaf 100644 --- a/iotdb-core/datanode/src/main/java/org/apache/iotdb/db/queryengine/plan/execution/config/metadata/relational/ShowCreateViewTask.java +++ b/iotdb-core/datanode/src/main/java/org/apache/iotdb/db/queryengine/plan/execution/config/metadata/relational/ShowCreateViewTask.java @@ -99,7 +99,13 @@ public static String getShowCreateViewSQL(final TsTable table) { .append("TAG"); break; case TIME: - continue; + builder + .append(getIdentifier(schema.getColumnName())) + .append(" ") + .append(schema.getDataType()) + .append(" ") + .append("TIME"); + break; case FIELD: builder .append(getIdentifier(schema.getColumnName())) @@ -122,7 +128,7 @@ public static String getShowCreateViewSQL(final TsTable table) { builder.append(","); } - if (table.getColumnList().size() > 1) { + if (!table.getColumnList().isEmpty()) { builder.deleteCharAt(builder.length() - 1); } 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 48cd81b39e142..4a82bdd70a0b9 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 @@ -22,9 +22,7 @@ import org.apache.iotdb.commons.conf.CommonDescriptor; import org.apache.iotdb.commons.exception.MetadataException; import org.apache.iotdb.commons.exception.runtime.SchemaExecutionException; -import org.apache.iotdb.commons.schema.table.column.AttributeColumnSchema; -import org.apache.iotdb.commons.schema.table.column.FieldColumnSchema; -import org.apache.iotdb.commons.schema.table.column.TagColumnSchema; +import org.apache.iotdb.commons.schema.table.column.TimeColumnSchema; import org.apache.iotdb.commons.schema.table.column.TsTableColumnCategory; import org.apache.iotdb.commons.schema.table.column.TsTableColumnSchema; import org.apache.iotdb.commons.schema.table.column.TsTableColumnSchemaUtil; @@ -93,6 +91,9 @@ public class TsTable { private transient int tagNums = 0; private transient int fieldNum = 0; + // Initiated during creation and never changed the reference + private transient TsTableColumnSchema timeColumnSchema; + public TsTable(final String tableName) { this.tableName = tableName; } @@ -101,7 +102,12 @@ public TsTable(final String tableName) { public TsTable(String tableName, ImmutableList columnSchemas) { this.tableName = tableName; columnSchemas.forEach( - columnSchema -> columnSchemaMap.put(columnSchema.getColumnName(), columnSchema)); + columnSchema -> { + columnSchemaMap.put(columnSchema.getColumnName(), columnSchema); + if (columnSchema instanceof TimeColumnSchema) { + timeColumnSchema = columnSchema; + } + }); } public TsTable(TsTable origin) { @@ -141,6 +147,19 @@ public TsTableColumnSchema getColumnSchema(final String columnName) { } } + // No need to acquire lock, because the time column is fixed after table creation + // And the inner name is protected by the volatile keyword + public TsTableColumnSchema getTimeColumnSchema() { + if (Objects.isNull(timeColumnSchema)) { + timeColumnSchema = + columnSchemaMap.values().stream() + .filter(column -> column instanceof TimeColumnSchema) + .findFirst() + .orElse(null); + } + return timeColumnSchema; + } + /** * Execute a write operation with optimistic lock support. This method handles the write flag and * version increment automatically. @@ -215,33 +234,10 @@ public void renameColumnSchema(final String oldName, final String newName) { () -> { // Ensures idempotency if (columnSchemaMap.containsKey(oldName)) { - final TsTableColumnSchema schema = columnSchemaMap.remove(oldName); + final TsTableColumnSchema schema = columnSchemaMap.get(oldName); final Map oldProps = schema.getProps(); oldProps.computeIfAbsent(TreeViewSchema.ORIGINAL_NAME, k -> schema.getColumnName()); - switch (schema.getColumnCategory()) { - case TAG: - columnSchemaMap.put( - newName, new TagColumnSchema(newName, schema.getDataType(), oldProps)); - break; - case FIELD: - columnSchemaMap.put( - newName, - new FieldColumnSchema( - newName, - schema.getDataType(), - ((FieldColumnSchema) schema).getEncoding(), - ((FieldColumnSchema) schema).getCompressor(), - oldProps)); - break; - case ATTRIBUTE: - columnSchemaMap.put( - newName, new AttributeColumnSchema(newName, schema.getDataType(), oldProps)); - break; - case TIME: - default: - // Do nothing - columnSchemaMap.put(oldName, schema); - } + schema.setColumnName(newName); } }); } diff --git a/iotdb-core/node-commons/src/main/java/org/apache/iotdb/commons/schema/table/column/TsTableColumnSchema.java b/iotdb-core/node-commons/src/main/java/org/apache/iotdb/commons/schema/table/column/TsTableColumnSchema.java index 8f773f19be497..8342c08365fd3 100644 --- a/iotdb-core/node-commons/src/main/java/org/apache/iotdb/commons/schema/table/column/TsTableColumnSchema.java +++ b/iotdb-core/node-commons/src/main/java/org/apache/iotdb/commons/schema/table/column/TsTableColumnSchema.java @@ -34,7 +34,7 @@ public abstract class TsTableColumnSchema { - protected String columnName; + protected volatile String columnName; protected TSDataType dataType; @@ -52,6 +52,12 @@ public abstract class TsTableColumnSchema { this.props = props; } + // Only used for column renaming + public TsTableColumnSchema setColumnName(String columnName) { + this.columnName = columnName; + return this; + } + public String getColumnName() { return columnName; }