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

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -306,15 +306,15 @@ 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'");

TestUtils.assertResultSetEqual(
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");
Expand Down Expand Up @@ -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"};

Expand Down Expand Up @@ -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"};
Expand Down Expand Up @@ -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"};
Expand Down Expand Up @@ -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());
}
Expand Down Expand Up @@ -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());
Expand Down Expand Up @@ -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 {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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()))
Expand All @@ -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);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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()))
Expand All @@ -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);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -93,6 +91,9 @@
private transient int tagNums = 0;
private transient int fieldNum = 0;

// Initiated during creation and never changed the reference
private transient TsTableColumnSchema timeColumnSchema;

Check warning on line 95 in iotdb-core/node-commons/src/main/java/org/apache/iotdb/commons/schema/table/TsTable.java

View check run for this annotation

SonarQubeCloud / SonarCloud Code Analysis

Remove the "transient" modifier from this field.

See more on https://sonarcloud.io/project/issues?id=apache_iotdb&issues=AZ127kp7K60kTv2Xh_um&open=AZ127kp7K60kTv2Xh_um&pullRequest=17457

public TsTable(final String tableName) {
this.tableName = tableName;
}
Expand All @@ -101,7 +102,12 @@
public TsTable(String tableName, ImmutableList<TsTableColumnSchema> 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) {
Expand Down Expand Up @@ -141,6 +147,19 @@
}
}

// 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)

Check warning on line 156 in iotdb-core/node-commons/src/main/java/org/apache/iotdb/commons/schema/table/TsTable.java

View check run for this annotation

SonarQubeCloud / SonarCloud Code Analysis

Replace this lambda with method reference 'TimeColumnSchema.class::isInstance'.

See more on https://sonarcloud.io/project/issues?id=apache_iotdb&issues=AZ127kp7K60kTv2Xh_un&open=AZ127kp7K60kTv2Xh_un&pullRequest=17457
.findFirst()
.orElse(null);
}
return timeColumnSchema;
}

/**
* Execute a write operation with optimistic lock support. This method handles the write flag and
* version increment automatically.
Expand Down Expand Up @@ -215,33 +234,10 @@
() -> {
// Ensures idempotency
if (columnSchemaMap.containsKey(oldName)) {
final TsTableColumnSchema schema = columnSchemaMap.remove(oldName);
final TsTableColumnSchema schema = columnSchemaMap.get(oldName);
final Map<String, String> 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);
}
});
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@

public abstract class TsTableColumnSchema {

protected String columnName;
protected volatile String columnName;

protected TSDataType dataType;

Expand All @@ -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;
}
Expand Down
Loading