This is an automated email from the ASF dual-hosted git repository. haonan pushed a commit to branch rc/1.3.1 in repository https://gitbox.apache.org/repos/asf/iotdb.git
commit 71c2857687436e6a6f1a63c91cbc5dd982df6b99 Author: 张宇欣 <[email protected]> AuthorDate: Sat Mar 2 12:52:40 2024 +0800 [IOTDB-6305] Add show current_timestamp statement (#12106) --- .../iotdb/db/it/IoTDBSimpleQueryStandaloneIT.java | 75 ++++++++++++++++++++++ .../org/apache/iotdb/db/qp/sql/IdentifierParser.g4 | 1 + .../org/apache/iotdb/db/qp/sql/IoTDBSqlParser.g4 | 10 ++- .../antlr4/org/apache/iotdb/db/qp/sql/SqlLexer.g4 | 4 ++ .../common/header/ColumnHeaderConstant.java | 6 ++ .../common/header/DatasetHeaderFactory.java | 4 ++ .../queryengine/plan/analyze/AnalyzeVisitor.java | 11 ++++ .../memory/StatementMemorySourceVisitor.java | 16 +++++ .../db/queryengine/plan/parser/ASTVisitor.java | 6 ++ .../plan/statement/StatementVisitor.java | 6 ++ .../metadata/ShowCurrentTimestampStatement.java | 29 +++++++++ 11 files changed, 166 insertions(+), 2 deletions(-) diff --git a/integration-test/src/test/java/org/apache/iotdb/db/it/IoTDBSimpleQueryStandaloneIT.java b/integration-test/src/test/java/org/apache/iotdb/db/it/IoTDBSimpleQueryStandaloneIT.java new file mode 100644 index 00000000000..3167cc00e23 --- /dev/null +++ b/integration-test/src/test/java/org/apache/iotdb/db/it/IoTDBSimpleQueryStandaloneIT.java @@ -0,0 +1,75 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +package org.apache.iotdb.db.it; + +import org.apache.iotdb.it.env.EnvFactory; +import org.apache.iotdb.it.framework.IoTDBTestRunner; +import org.apache.iotdb.itbase.category.LocalStandaloneIT; + +import org.junit.After; +import org.junit.Before; +import org.junit.Test; +import org.junit.experimental.categories.Category; +import org.junit.runner.RunWith; + +import java.sql.Connection; +import java.sql.ResultSet; +import java.sql.SQLException; +import java.sql.Statement; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertTrue; +import static org.junit.Assert.fail; + +@RunWith(IoTDBTestRunner.class) +@Category({LocalStandaloneIT.class}) +public class IoTDBSimpleQueryStandaloneIT { + + @Before + public void setUp() throws Exception { + EnvFactory.getEnv().initClusterEnvironment(); + } + + @After + public void tearDown() throws Exception { + EnvFactory.getEnv().cleanClusterEnvironment(); + } + + @Test + public void showCurrentTimestamp() { + try (Connection connection = EnvFactory.getEnv().getConnection(); + Statement statement = connection.createStatement()) { + long startTime = System.currentTimeMillis(); + long rowCount = 0; + try (ResultSet r1 = statement.executeQuery("show current_timestamp")) { + long currentTime = 0; + while (r1.next()) { + rowCount++; + currentTime = r1.getLong(1); + assertTrue(currentTime >= startTime); + } + assertEquals(1, r1.getMetaData().getColumnCount()); + assertEquals(1, rowCount); + } + } catch (SQLException e) { + fail(); + } + } +} diff --git a/iotdb-core/antlr/src/main/antlr4/org/apache/iotdb/db/qp/sql/IdentifierParser.g4 b/iotdb-core/antlr/src/main/antlr4/org/apache/iotdb/db/qp/sql/IdentifierParser.g4 index 13fb5bc9dfc..203c357576e 100644 --- a/iotdb-core/antlr/src/main/antlr4/org/apache/iotdb/db/qp/sql/IdentifierParser.g4 +++ b/iotdb-core/antlr/src/main/antlr4/org/apache/iotdb/db/qp/sql/IdentifierParser.g4 @@ -255,4 +255,5 @@ keyWords | WRITE | AUDIT | OPTION + | CURRENT_TIMESTAMP ; \ No newline at end of file diff --git a/iotdb-core/antlr/src/main/antlr4/org/apache/iotdb/db/qp/sql/IoTDBSqlParser.g4 b/iotdb-core/antlr/src/main/antlr4/org/apache/iotdb/db/qp/sql/IoTDBSqlParser.g4 index 68101e433bd..7c4ae5ebe0c 100644 --- a/iotdb-core/antlr/src/main/antlr4/org/apache/iotdb/db/qp/sql/IoTDBSqlParser.g4 +++ b/iotdb-core/antlr/src/main/antlr4/org/apache/iotdb/db/qp/sql/IoTDBSqlParser.g4 @@ -80,8 +80,9 @@ dclStatement utilityStatement : merge | fullMerge | flush | clearCache | settle | startRepairData | stopRepairData | explain | setSystemStatus | showVersion | showFlushInfo | showLockInfo | showQueryResource - | showQueries | killQuery | grantWatermarkEmbedding | revokeWatermarkEmbedding - | loadConfiguration | loadTimeseries | loadFile | removeFile | unloadFile + | showQueries | showCurrentTimestamp | killQuery | grantWatermarkEmbedding + | revokeWatermarkEmbedding | loadConfiguration | loadTimeseries | loadFile + | removeFile | unloadFile ; /** @@ -987,6 +988,11 @@ showQueries rowPaginationClause? ; +// Show Current Timestamp +showCurrentTimestamp + : SHOW CURRENT_TIMESTAMP + ; + // Kill Query killQuery : KILL (QUERY queryId=STRING_LITERAL | ALL QUERIES) diff --git a/iotdb-core/antlr/src/main/antlr4/org/apache/iotdb/db/qp/sql/SqlLexer.g4 b/iotdb-core/antlr/src/main/antlr4/org/apache/iotdb/db/qp/sql/SqlLexer.g4 index 5c79f778818..da294b5daf4 100644 --- a/iotdb-core/antlr/src/main/antlr4/org/apache/iotdb/db/qp/sql/SqlLexer.g4 +++ b/iotdb-core/antlr/src/main/antlr4/org/apache/iotdb/db/qp/sql/SqlLexer.g4 @@ -1013,6 +1013,10 @@ DATA_REGION_GROUP_NUM : D A T A '_' R E G I O N '_' G R O U P '_' N U M ; +CURRENT_TIMESTAMP + : C U R R E N T '_' T I M E S T A M P + ; + /** * 3. Operators */ diff --git a/iotdb-core/datanode/src/main/java/org/apache/iotdb/db/queryengine/common/header/ColumnHeaderConstant.java b/iotdb-core/datanode/src/main/java/org/apache/iotdb/db/queryengine/common/header/ColumnHeaderConstant.java index ae56f014a8a..4192821860b 100644 --- a/iotdb-core/datanode/src/main/java/org/apache/iotdb/db/queryengine/common/header/ColumnHeaderConstant.java +++ b/iotdb-core/datanode/src/main/java/org/apache/iotdb/db/queryengine/common/header/ColumnHeaderConstant.java @@ -188,6 +188,9 @@ public class ColumnHeaderConstant { public static final String VIEW_TYPE = "ViewType"; public static final String SOURCE = "Source"; + // column names for show current timestamp + public static final String CURRENT_TIMESTAMP = "CurrentTimestamp"; + public static final List<ColumnHeader> lastQueryColumnHeaders = ImmutableList.of( new ColumnHeader(TIMESERIES, TSDataType.TEXT), @@ -463,4 +466,7 @@ public class ColumnHeaderConstant { new ColumnHeader(ATTRIBUTES, TSDataType.TEXT), new ColumnHeader(VIEW_TYPE, TSDataType.TEXT), new ColumnHeader(SOURCE, TSDataType.TEXT)); + + public static final List<ColumnHeader> showCurrentTimestampColumnHeaders = + ImmutableList.of(new ColumnHeader(CURRENT_TIMESTAMP, TSDataType.INT64)); } diff --git a/iotdb-core/datanode/src/main/java/org/apache/iotdb/db/queryengine/common/header/DatasetHeaderFactory.java b/iotdb-core/datanode/src/main/java/org/apache/iotdb/db/queryengine/common/header/DatasetHeaderFactory.java index 9d46407218e..90b235fce46 100644 --- a/iotdb-core/datanode/src/main/java/org/apache/iotdb/db/queryengine/common/header/DatasetHeaderFactory.java +++ b/iotdb-core/datanode/src/main/java/org/apache/iotdb/db/queryengine/common/header/DatasetHeaderFactory.java @@ -192,4 +192,8 @@ public class DatasetHeaderFactory { public static DatasetHeader getShowLogicalViewHeader() { return new DatasetHeader(ColumnHeaderConstant.showLogicalViewColumnHeaders, true); } + + public static DatasetHeader getShowCurrentTimestampHeader() { + return new DatasetHeader(ColumnHeaderConstant.showCurrentTimestampColumnHeaders, true); + } } diff --git a/iotdb-core/datanode/src/main/java/org/apache/iotdb/db/queryengine/plan/analyze/AnalyzeVisitor.java b/iotdb-core/datanode/src/main/java/org/apache/iotdb/db/queryengine/plan/analyze/AnalyzeVisitor.java index d82b348bd98..dfac79c0fc5 100644 --- a/iotdb-core/datanode/src/main/java/org/apache/iotdb/db/queryengine/plan/analyze/AnalyzeVisitor.java +++ b/iotdb-core/datanode/src/main/java/org/apache/iotdb/db/queryengine/plan/analyze/AnalyzeVisitor.java @@ -117,6 +117,7 @@ import org.apache.iotdb.db.queryengine.plan.statement.metadata.CreateTimeSeriesS import org.apache.iotdb.db.queryengine.plan.statement.metadata.ShowChildNodesStatement; import org.apache.iotdb.db.queryengine.plan.statement.metadata.ShowChildPathsStatement; import org.apache.iotdb.db.queryengine.plan.statement.metadata.ShowClusterStatement; +import org.apache.iotdb.db.queryengine.plan.statement.metadata.ShowCurrentTimestampStatement; import org.apache.iotdb.db.queryengine.plan.statement.metadata.ShowDatabaseStatement; import org.apache.iotdb.db.queryengine.plan.statement.metadata.ShowDevicesStatement; import org.apache.iotdb.db.queryengine.plan.statement.metadata.ShowTTLStatement; @@ -3524,4 +3525,14 @@ public class AnalyzeVisitor extends StatementVisitor<Analysis, MPPQueryContext> return analysis; } // endregion view + + @Override + public Analysis visitShowCurrentTimestamp( + ShowCurrentTimestampStatement showCurrentTimestampStatement, MPPQueryContext context) { + Analysis analysis = new Analysis(); + analysis.setStatement(showCurrentTimestampStatement); + analysis.setFinishQueryAfterAnalyze(true); + analysis.setRespDatasetHeader(DatasetHeaderFactory.getShowCurrentTimestampHeader()); + return analysis; + } } diff --git a/iotdb-core/datanode/src/main/java/org/apache/iotdb/db/queryengine/plan/execution/memory/StatementMemorySourceVisitor.java b/iotdb-core/datanode/src/main/java/org/apache/iotdb/db/queryengine/plan/execution/memory/StatementMemorySourceVisitor.java index 4b590b04bf4..2012aa1209b 100644 --- a/iotdb-core/datanode/src/main/java/org/apache/iotdb/db/queryengine/plan/execution/memory/StatementMemorySourceVisitor.java +++ b/iotdb-core/datanode/src/main/java/org/apache/iotdb/db/queryengine/plan/execution/memory/StatementMemorySourceVisitor.java @@ -39,6 +39,7 @@ import org.apache.iotdb.db.queryengine.plan.statement.metadata.CountNodesStateme import org.apache.iotdb.db.queryengine.plan.statement.metadata.CountTimeSeriesStatement; import org.apache.iotdb.db.queryengine.plan.statement.metadata.ShowChildNodesStatement; import org.apache.iotdb.db.queryengine.plan.statement.metadata.ShowChildPathsStatement; +import org.apache.iotdb.db.queryengine.plan.statement.metadata.ShowCurrentTimestampStatement; import org.apache.iotdb.db.queryengine.plan.statement.metadata.template.ShowPathsUsingTemplateStatement; import org.apache.iotdb.db.queryengine.plan.statement.sys.ExplainStatement; import org.apache.iotdb.db.queryengine.plan.statement.sys.ShowVersionStatement; @@ -259,4 +260,19 @@ public class StatementMemorySourceVisitor return new StatementMemorySource( tsBlockBuilder.build(), context.getAnalysis().getRespDatasetHeader()); } + + public StatementMemorySource visitShowCurrentTimestamp( + ShowCurrentTimestampStatement showCurrentTimestampStatement, + StatementMemorySourceContext context) { + List<TSDataType> outputDataTypes = + ColumnHeaderConstant.showCurrentTimestampColumnHeaders.stream() + .map(ColumnHeader::getColumnType) + .collect(Collectors.toList()); + TsBlockBuilder tsBlockBuilder = new TsBlockBuilder(outputDataTypes); + tsBlockBuilder.getTimeColumnBuilder().writeLong(0L); + tsBlockBuilder.getColumnBuilder(0).writeLong(System.currentTimeMillis()); + tsBlockBuilder.declarePosition(); + return new StatementMemorySource( + tsBlockBuilder.build(), context.getAnalysis().getRespDatasetHeader()); + } } diff --git a/iotdb-core/datanode/src/main/java/org/apache/iotdb/db/queryengine/plan/parser/ASTVisitor.java b/iotdb-core/datanode/src/main/java/org/apache/iotdb/db/queryengine/plan/parser/ASTVisitor.java index 65832f3e571..fff5c53ef66 100644 --- a/iotdb-core/datanode/src/main/java/org/apache/iotdb/db/queryengine/plan/parser/ASTVisitor.java +++ b/iotdb-core/datanode/src/main/java/org/apache/iotdb/db/queryengine/plan/parser/ASTVisitor.java @@ -145,6 +145,7 @@ import org.apache.iotdb.db.queryengine.plan.statement.metadata.ShowClusterIdStat import org.apache.iotdb.db.queryengine.plan.statement.metadata.ShowClusterStatement; import org.apache.iotdb.db.queryengine.plan.statement.metadata.ShowConfigNodesStatement; import org.apache.iotdb.db.queryengine.plan.statement.metadata.ShowContinuousQueriesStatement; +import org.apache.iotdb.db.queryengine.plan.statement.metadata.ShowCurrentTimestampStatement; import org.apache.iotdb.db.queryengine.plan.statement.metadata.ShowDataNodesStatement; import org.apache.iotdb.db.queryengine.plan.statement.metadata.ShowDatabaseStatement; import org.apache.iotdb.db.queryengine.plan.statement.metadata.ShowDevicesStatement; @@ -4094,4 +4095,9 @@ public class ASTVisitor extends IoTDBSqlParserBaseVisitor<Statement> { } return showSpaceQuotaStatement; } + + @Override + public Statement visitShowCurrentTimestamp(IoTDBSqlParser.ShowCurrentTimestampContext ctx) { + return new ShowCurrentTimestampStatement(); + } } diff --git a/iotdb-core/datanode/src/main/java/org/apache/iotdb/db/queryengine/plan/statement/StatementVisitor.java b/iotdb-core/datanode/src/main/java/org/apache/iotdb/db/queryengine/plan/statement/StatementVisitor.java index bbbb2a93821..0ffad55900a 100644 --- a/iotdb-core/datanode/src/main/java/org/apache/iotdb/db/queryengine/plan/statement/StatementVisitor.java +++ b/iotdb-core/datanode/src/main/java/org/apache/iotdb/db/queryengine/plan/statement/StatementVisitor.java @@ -62,6 +62,7 @@ import org.apache.iotdb.db.queryengine.plan.statement.metadata.ShowClusterIdStat import org.apache.iotdb.db.queryengine.plan.statement.metadata.ShowClusterStatement; import org.apache.iotdb.db.queryengine.plan.statement.metadata.ShowConfigNodesStatement; import org.apache.iotdb.db.queryengine.plan.statement.metadata.ShowContinuousQueriesStatement; +import org.apache.iotdb.db.queryengine.plan.statement.metadata.ShowCurrentTimestampStatement; import org.apache.iotdb.db.queryengine.plan.statement.metadata.ShowDataNodesStatement; import org.apache.iotdb.db.queryengine.plan.statement.metadata.ShowDatabaseStatement; import org.apache.iotdb.db.queryengine.plan.statement.metadata.ShowDevicesStatement; @@ -563,4 +564,9 @@ public abstract class StatementVisitor<R, C> { ShowThrottleQuotaStatement showThrottleQuotaStatement, C context) { return visitStatement(showThrottleQuotaStatement, context); } + + public R visitShowCurrentTimestamp( + ShowCurrentTimestampStatement showCurrentTimestampStatement, C context) { + return visitStatement(showCurrentTimestampStatement, context); + } } diff --git a/iotdb-core/datanode/src/main/java/org/apache/iotdb/db/queryengine/plan/statement/metadata/ShowCurrentTimestampStatement.java b/iotdb-core/datanode/src/main/java/org/apache/iotdb/db/queryengine/plan/statement/metadata/ShowCurrentTimestampStatement.java new file mode 100644 index 00000000000..5764160c01e --- /dev/null +++ b/iotdb-core/datanode/src/main/java/org/apache/iotdb/db/queryengine/plan/statement/metadata/ShowCurrentTimestampStatement.java @@ -0,0 +1,29 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +package org.apache.iotdb.db.queryengine.plan.statement.metadata; + +import org.apache.iotdb.db.queryengine.plan.statement.StatementVisitor; + +public class ShowCurrentTimestampStatement extends ShowStatement { + @Override + public <R, C> R accept(StatementVisitor<R, C> visitor, C context) { + return visitor.visitShowCurrentTimestamp(this, context); + } +}
