This is an automated email from the ASF dual-hosted git repository. qiaojialin pushed a commit to branch master in repository https://gitbox.apache.org/repos/asf/iotdb.git
The following commit(s) were added to refs/heads/master by this push: new 2b066d817f7 [IOTDB-6305] Add show current_timestamp statement (#12106) 2b066d817f7 is described below commit 2b066d817f72cdfe41052110380d7d655c1fea5c Author: 张宇欣 <545952...@qq.com> 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 7b1ca692ed1..2b3a3ebca0f 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 @@ -256,4 +256,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 8396889efb2..47e798b80c2 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 : 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 ; /** @@ -997,6 +998,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 81b995393d2..94906afb3fc 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 @@ -1017,6 +1017,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 d6da8a7b4a2..6ccdcedc7bc 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 @@ -116,6 +116,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; @@ -3554,4 +3555,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 31c9cc0022b..d1a64947094 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 4634134bc4d..bae44158f2b 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 @@ -148,6 +148,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; @@ -4130,4 +4131,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 1af9a7f1ad3..37c3ef391fc 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; @@ -568,4 +569,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); + } +}