Skip to content
Open
Original file line number Diff line number Diff line change
Expand Up @@ -717,7 +717,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) with (ttl='INF')");
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 @@ -1043,13 +1049,18 @@ public void testTreeViewTable() throws Exception {
}

try (final Connection connection =
EnvFactory.getEnv()
.getConnection("testUser", "testUser123456", BaseEnv.TABLE_SQL_DIALECT);
EnvFactory.getEnv().getConnection(BaseEnv.TABLE_SQL_DIALECT);
final Statement statement = connection.createStatement()) {
statement.execute("alter database tree_view_db set properties ttl=100");
statement.execute(
"create or replace view tree_view_db.view_table (tag1 tag, tag2 tag, s11 int32 field, s3 from s2) restrict with (ttl=100) as root.\"重庆\".\"1\".**");
"create or replace view tree_view_db.view_table (tag1 tag, tag2 tag, s11 int32 field, s3 from s2) restrict as root.\"重庆\".\"1\".**");

TestUtils.assertResultSetEqual(
statement.executeQuery("show tables from tree_view_db"),
"TableName,TTL(ms),",
Collections.singleton("view_table,100,"));
} catch (final SQLException e) {
fail();
fail(e.getMessage());
}

try (final Connection connection =
Expand All @@ -1066,14 +1077,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 @@ -91,18 +91,16 @@ protected void checkTableExistence(final ConfigNodeProcedureEnv env) {
} else {
oldView = oldTableAndStatus.get().getLeft();
oldStatus = oldTableAndStatus.get().getRight();
setNextState(CreateTableState.PRE_CREATE);
}
} else {
final TDatabaseSchema schema =
env.getConfigManager().getClusterSchemaManager().getDatabaseSchemaByName(database);
if (!table.getPropValue(TsTable.TTL_PROPERTY).isPresent()
&& schema.isSetTTL()
&& schema.getTTL() != Long.MAX_VALUE) {
table.addProp(TsTable.TTL_PROPERTY, String.valueOf(schema.getTTL()));
}
setNextState(CreateTableState.PRE_CREATE);
}
final TDatabaseSchema schema =
env.getConfigManager().getClusterSchemaManager().getDatabaseSchemaByName(database);
if (!table.getPropValue(TsTable.TTL_PROPERTY).isPresent()
&& schema.isSetTTL()
&& schema.getTTL() != Long.MAX_VALUE) {
table.addProp(TsTable.TTL_PROPERTY, String.valueOf(schema.getTTL()));
}
setNextState(CreateTableState.PRE_CREATE);
} catch (final MetadataException | DatabaseNotExistsException e) {
setFailure(new ProcedureException(e));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -252,7 +252,7 @@ private void doTransfer(final PipeDeleteDataNodeEvent pipeDeleteDataNodeEvent)
String.format(
"Transfer deletion %s error, result status %s.",
pipeDeleteDataNodeEvent.getDeleteDataNode().getType(), status),
pipeDeleteDataNodeEvent.getDeletionResource().toString(),
pipeDeleteDataNodeEvent.getDeleteDataNode().toString(),
true);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -442,8 +442,11 @@ protected void extractProgressReportEvent(final PipeRealtimeEvent event) {
}
pendingQueue.pollLast();
}
if (pendingQueue.peekLast() instanceof ProgressReportEvent) {
final ProgressReportEvent oldEvent = (ProgressReportEvent) pendingQueue.peekLast();
final Event last = pendingQueue.peekLast();
if (last instanceof PipeRealtimeEvent
&& ((PipeRealtimeEvent) last).getEvent() instanceof ProgressReportEvent) {
final ProgressReportEvent oldEvent =
(ProgressReportEvent) ((PipeRealtimeEvent) last).getEvent();
oldEvent.bindProgressIndex(
oldEvent
.getProgressIndex()
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,18 +129,20 @@ private static String getShowCreateTableSQL(final TsTable table) {
builder.append(",");
}

if (table.getColumnList().size() > 1) {
if (!table.getColumnList().isEmpty()) {
builder.deleteCharAt(builder.length() - 1);
}

builder.append(")");
if (table.getPropValue(TsTable.COMMENT_KEY).isPresent()) {
builder.append(" COMMENT ").append(getString(table.getPropValue(TsTable.COMMENT_KEY).get()));
}
builder
.append(" WITH (ttl=")
.append(table.getPropValue(TsTable.TTL_PROPERTY).orElse("'" + TTL_INFINITE + "'"))
.append(")");

String ttlString = table.getPropValue(TsTable.TTL_PROPERTY).orElse(TTL_INFINITE);
if (ttlString.equals(TTL_INFINITE)) {
ttlString = "'" + ttlString + "'";
}
builder.append(" WITH (ttl=").append(ttlString).append(")");

return builder.toString();
}
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 All @@ -136,10 +142,11 @@ public static String getShowCreateViewSQL(final TsTable table) {
builder.append(" RESTRICT");
}

builder
.append(" WITH (ttl=")
.append(table.getPropValue(TsTable.TTL_PROPERTY).orElse("'" + TTL_INFINITE + "'"))
.append(")");
String ttlString = table.getPropValue(TsTable.TTL_PROPERTY).orElse(TTL_INFINITE);
if (ttlString.equals(TTL_INFINITE)) {
ttlString = "'" + ttlString + "'";
}
builder.append(" WITH (ttl=").append(ttlString).append(")");

builder.append(" AS ");

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