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 @@ -815,6 +815,12 @@ public void testMixedDatabase() throws SQLException {
try (final Connection connection =
EnvFactory.getEnv().getConnection(BaseEnv.TABLE_SQL_DIALECT);
final Statement statement = connection.createStatement()) {
TestUtils.assertResultSetEqual(
statement.executeQuery("count databases"), "count,", Collections.singleton("3,"));
TestUtils.assertResultSetEqual(
statement.executeQuery("select count(*) from information_schema.databases"),
"_col0,",
Collections.singleton("3,"));
statement.execute("drop database test");
}

Expand All @@ -825,6 +831,23 @@ public void testMixedDatabase() throws SQLException {
}
}

@Test
public void testCountDatabases() throws SQLException {
try (final Connection connection =
EnvFactory.getEnv().getConnection(BaseEnv.TABLE_SQL_DIALECT);
final Statement statement = connection.createStatement()) {
statement.execute("create database db1");
statement.execute("create database db2");

TestUtils.assertResultSetEqual(
statement.executeQuery("count databases"), "count,", Collections.singleton("3,"));
TestUtils.assertResultSetEqual(
statement.executeQuery("select count(*) from information_schema.databases"),
"_col0,",
Collections.singleton("3,"));
}
}

@Test
public void testDBAuth() throws SQLException {
try (final Connection adminCon = EnvFactory.getEnv().getConnection(BaseEnv.TABLE_SQL_DIALECT);
Expand All @@ -838,6 +861,8 @@ public void testDBAuth() throws SQLException {
try (final Connection userCon =
EnvFactory.getEnv().getConnection("test", "password123456", BaseEnv.TABLE_SQL_DIALECT);
final Statement userStmt = userCon.createStatement()) {
TestUtils.assertResultSetEqual(
userStmt.executeQuery("count databases"), "count,", Collections.singleton("1,"));
TestUtils.assertResultSetEqual(
userStmt.executeQuery("show databases"),
"Database,TTL(ms),SchemaReplicationFactor,DataReplicationFactor,TimePartitionInterval,",
Expand Down Expand Up @@ -869,6 +894,8 @@ public void testDBAuth() throws SQLException {
try (final Connection userCon =
EnvFactory.getEnv().getConnection("test", "password123456", BaseEnv.TABLE_SQL_DIALECT);
final Statement userStmt = userCon.createStatement()) {
TestUtils.assertResultSetEqual(
userStmt.executeQuery("count databases"), "count,", Collections.singleton("2,"));
try (final ResultSet resultSet = userStmt.executeQuery("SHOW DATABASES")) {
final ResultSetMetaData metaData = resultSet.getMetaData();
assertEquals(showDBColumnHeaders.size(), metaData.getColumnCount());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,7 @@
import org.apache.iotdb.db.queryengine.plan.relational.sql.ast.AlterColumnDataType;
import org.apache.iotdb.db.queryengine.plan.relational.sql.ast.AlterDB;
import org.apache.iotdb.db.queryengine.plan.relational.sql.ast.ClearCache;
import org.apache.iotdb.db.queryengine.plan.relational.sql.ast.CountDB;
import org.apache.iotdb.db.queryengine.plan.relational.sql.ast.CreateDB;
import org.apache.iotdb.db.queryengine.plan.relational.sql.ast.CreateExternalService;
import org.apache.iotdb.db.queryengine.plan.relational.sql.ast.CreateFunction;
Expand Down Expand Up @@ -610,6 +611,7 @@ private IQueryExecution createQueryExecutionForTableModel(
queryContext.setTimeOut(timeOut);
queryContext.setStartTime(startTime);
if (statement instanceof DropDB
|| statement instanceof CountDB
|| statement instanceof ShowDB
|| statement instanceof CreateDB
|| statement instanceof AlterDB
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,7 @@
import org.apache.iotdb.db.queryengine.plan.execution.config.metadata.relational.AlterTableRenameTableTask;
import org.apache.iotdb.db.queryengine.plan.execution.config.metadata.relational.AlterTableSetPropertiesTask;
import org.apache.iotdb.db.queryengine.plan.execution.config.metadata.relational.ClearCacheTask;
import org.apache.iotdb.db.queryengine.plan.execution.config.metadata.relational.CountDBTask;
import org.apache.iotdb.db.queryengine.plan.execution.config.metadata.relational.CreateDBTask;
import org.apache.iotdb.db.queryengine.plan.execution.config.metadata.relational.CreateTableTask;
import org.apache.iotdb.db.queryengine.plan.execution.config.metadata.relational.CreateTableViewTask;
Expand Down Expand Up @@ -156,6 +157,7 @@
import org.apache.iotdb.db.queryengine.plan.relational.sql.ast.AstVisitor;
import org.apache.iotdb.db.queryengine.plan.relational.sql.ast.ClearCache;
import org.apache.iotdb.db.queryengine.plan.relational.sql.ast.ColumnDefinition;
import org.apache.iotdb.db.queryengine.plan.relational.sql.ast.CountDB;
import org.apache.iotdb.db.queryengine.plan.relational.sql.ast.CreateDB;
import org.apache.iotdb.db.queryengine.plan.relational.sql.ast.CreateExternalService;
import org.apache.iotdb.db.queryengine.plan.relational.sql.ast.CreateFunction;
Expand Down Expand Up @@ -424,6 +426,15 @@ public IConfigTask visitShowDB(final ShowDB node, final MPPQueryContext context)
canShowDB(accessControl, context.getSession().getUserName(), databaseName, context));
}

@Override
public IConfigTask visitCountDB(final CountDB node, final MPPQueryContext context) {
context.setQueryType(QueryType.READ);
return new CountDBTask(
node,
databaseName ->
canShowDB(accessControl, context.getSession().getUserName(), databaseName, context));
}

public static boolean canShowDB(
final AccessControl accessControl,
final String userName,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -229,6 +229,7 @@
import org.apache.iotdb.db.queryengine.plan.execution.config.metadata.region.MigrateRegionTask;
import org.apache.iotdb.db.queryengine.plan.execution.config.metadata.region.ReconstructRegionTask;
import org.apache.iotdb.db.queryengine.plan.execution.config.metadata.region.RemoveRegionTask;
import org.apache.iotdb.db.queryengine.plan.execution.config.metadata.relational.CountDBTask;
import org.apache.iotdb.db.queryengine.plan.execution.config.metadata.relational.DeleteDeviceTask;
import org.apache.iotdb.db.queryengine.plan.execution.config.metadata.relational.DescribeTableDetailsTask;
import org.apache.iotdb.db.queryengine.plan.execution.config.metadata.relational.DescribeTableTask;
Expand All @@ -255,6 +256,7 @@
import org.apache.iotdb.db.queryengine.plan.expression.Expression;
import org.apache.iotdb.db.queryengine.plan.expression.visitor.TransformToViewExpressionVisitor;
import org.apache.iotdb.db.queryengine.plan.planner.plan.node.metadata.write.view.AlterLogicalViewNode;
import org.apache.iotdb.db.queryengine.plan.relational.sql.ast.CountDB;
import org.apache.iotdb.db.queryengine.plan.relational.sql.ast.DeleteDevice;
import org.apache.iotdb.db.queryengine.plan.relational.sql.ast.DropDB;
import org.apache.iotdb.db.queryengine.plan.relational.sql.ast.ShowCluster;
Expand Down Expand Up @@ -2335,7 +2337,7 @@
}

@Override
public SettableFuture<ConfigTaskResult> alterPipe(final AlterPipeStatement alterPipeStatement) {

Check warning on line 2340 in iotdb-core/datanode/src/main/java/org/apache/iotdb/db/queryengine/plan/execution/config/executor/ClusterConfigTaskExecutor.java

View check run for this annotation

SonarQubeCloud / SonarCloud Code Analysis

A "Brain Method" was detected. Refactor it to reduce at least one of the following metrics: LOC from 178 to 64, Complexity from 21 to 14, Nesting Level from 4 to 2, Number of Variables from 22 to 6.

See more on https://sonarcloud.io/project/issues?id=apache_iotdb&issues=AZ452B7JhugVU3q9_7uA&open=AZ452B7JhugVU3q9_7uA&pullRequest=17705
final SettableFuture<ConfigTaskResult> future = SettableFuture.create();

// Validate pipe name
Expand Down Expand Up @@ -4140,6 +4142,24 @@
return future;
}

@Override
public SettableFuture<ConfigTaskResult> countDatabases(
final CountDB countDB, final Predicate<String> canSeenDB) {
final SettableFuture<ConfigTaskResult> future = SettableFuture.create();
final List<String> databasePathPattern = Arrays.asList(ALL_RESULT_NODES);
try (final ConfigNodeClient client =
CONFIG_NODE_CLIENT_MANAGER.borrowClient(ConfigNodeInfo.CONFIG_REGION_ID)) {
final TGetDatabaseReq req =
new TGetDatabaseReq(databasePathPattern, ALL_MATCH_SCOPE.serialize())
.setIsTableModel(true);
final TShowDatabaseResp resp = client.showDatabase(req);
CountDBTask.buildTSBlock(resp.getDatabaseInfoMap(), future, canSeenDB);
} catch (final IOException | ClientManagerException | TException e) {
future.setException(e);
}
return future;
}

@Override
public SettableFuture<ConfigTaskResult> showCluster(final ShowCluster showCluster) {
// As the implementation is identical, we'll simply translate to the
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@
import org.apache.iotdb.db.queryengine.plan.execution.config.metadata.region.ReconstructRegionTask;
import org.apache.iotdb.db.queryengine.plan.execution.config.metadata.region.RemoveRegionTask;
import org.apache.iotdb.db.queryengine.plan.planner.plan.node.metadata.write.view.AlterLogicalViewNode;
import org.apache.iotdb.db.queryengine.plan.relational.sql.ast.CountDB;
import org.apache.iotdb.db.queryengine.plan.relational.sql.ast.DeleteDevice;
import org.apache.iotdb.db.queryengine.plan.relational.sql.ast.DropDB;
import org.apache.iotdb.db.queryengine.plan.relational.sql.ast.ShowCluster;
Expand Down Expand Up @@ -333,6 +334,9 @@ SettableFuture<ConfigTaskResult> showThrottleQuota(
SettableFuture<ConfigTaskResult> showDatabases(
final ShowDB showDB, final Predicate<String> canSeenDB);

SettableFuture<ConfigTaskResult> countDatabases(
final CountDB countDB, final Predicate<String> canSeenDB);

SettableFuture<ConfigTaskResult> showCluster(ShowCluster showCluster);

SettableFuture<ConfigTaskResult> useDatabase(final Use useDB, final IClientSession clientSession);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
/*
* 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.execution.config.metadata.relational;

import org.apache.iotdb.commons.conf.IoTDBConstant;
import org.apache.iotdb.commons.schema.column.ColumnHeader;
import org.apache.iotdb.db.queryengine.common.header.DatasetHeader;
import org.apache.iotdb.db.queryengine.plan.execution.config.ConfigTaskResult;
import org.apache.iotdb.db.queryengine.plan.execution.config.IConfigTask;
import org.apache.iotdb.db.queryengine.plan.execution.config.executor.IConfigTaskExecutor;
import org.apache.iotdb.db.queryengine.plan.relational.sql.ast.CountDB;
import org.apache.iotdb.rpc.TSStatusCode;

import com.google.common.util.concurrent.ListenableFuture;
import com.google.common.util.concurrent.SettableFuture;
import org.apache.tsfile.enums.TSDataType;
import org.apache.tsfile.read.common.block.TsBlockBuilder;

import java.util.Collections;
import java.util.Map;
import java.util.function.Predicate;

import static org.apache.iotdb.commons.schema.table.InformationSchema.INFORMATION_DATABASE;

public class CountDBTask implements IConfigTask {

Check warning on line 42 in iotdb-core/datanode/src/main/java/org/apache/iotdb/db/queryengine/plan/execution/config/metadata/relational/CountDBTask.java

View check run for this annotation

SonarQubeCloud / SonarCloud Code Analysis

Abbreviation in name 'CountDBTask' must contain no more than '2' consecutive capital letters.

See more on https://sonarcloud.io/project/issues?id=apache_iotdb&issues=AZ452BxOhugVU3q9_7t-&open=AZ452BxOhugVU3q9_7t-&pullRequest=17705

private final CountDB node;
private final Predicate<String> canSeenDB;

public CountDBTask(final CountDB node, final Predicate<String> canSeenDB) {
this.node = node;
this.canSeenDB = canSeenDB;
}

@Override
public ListenableFuture<ConfigTaskResult> execute(final IConfigTaskExecutor configTaskExecutor)
throws InterruptedException {
return configTaskExecutor.countDatabases(node, canSeenDB);
}

public static void buildTSBlock(

Check warning on line 58 in iotdb-core/datanode/src/main/java/org/apache/iotdb/db/queryengine/plan/execution/config/metadata/relational/CountDBTask.java

View check run for this annotation

SonarQubeCloud / SonarCloud Code Analysis

Abbreviation in name 'buildTSBlock' must contain no more than '2' consecutive capital letters.

See more on https://sonarcloud.io/project/issues?id=apache_iotdb&issues=AZ452BxOhugVU3q9_7t_&open=AZ452BxOhugVU3q9_7t_&pullRequest=17705
final Map<String, ?> databaseInfoMap,
final SettableFuture<ConfigTaskResult> future,
final Predicate<String> canSeenDB) {
// information_schema is synthesized in table model rather than returned from ConfigNode.
final long databaseCount =
databaseInfoMap.keySet().stream()
.filter(databaseName -> !INFORMATION_DATABASE.equals(databaseName))
.filter(canSeenDB::test)
.count()
+ (canSeenDB.test(INFORMATION_DATABASE) ? 1 : 0);

final TsBlockBuilder builder = new TsBlockBuilder(Collections.singletonList(TSDataType.INT32));
builder.getTimeColumnBuilder().writeLong(0L);
builder.getColumnBuilder(0).writeInt((int) databaseCount);
builder.declarePosition();

final DatasetHeader datasetHeader =
new DatasetHeader(
Collections.singletonList(
new ColumnHeader(IoTDBConstant.COLUMN_COUNT, TSDataType.INT32)),
true);
future.set(new ConfigTaskResult(TSStatusCode.SUCCESS_STATUS, builder.build(), datasetHeader));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,10 @@ default R visitShowDB(final ShowDB node, final C context) {
return visitStatement(node, context);
}

default R visitCountDB(final CountDB node, final C context) {
return visitStatement(node, context);
}

default R visitCreateTable(final CreateTable node, final C context) {
return visitStatement(node, context);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
/*
* 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.relational.sql.ast;

import org.apache.iotdb.commons.queryengine.plan.relational.sql.ast.AstMemoryEstimationHelper;
import org.apache.iotdb.commons.queryengine.plan.relational.sql.ast.IAstVisitor;
import org.apache.iotdb.commons.queryengine.plan.relational.sql.ast.Node;
import org.apache.iotdb.commons.queryengine.plan.relational.sql.ast.NodeLocation;
import org.apache.iotdb.commons.queryengine.plan.relational.sql.ast.Statement;

import com.google.common.collect.ImmutableList;
import org.apache.tsfile.utils.RamUsageEstimator;

import java.util.List;

import static java.util.Objects.requireNonNull;

public class CountDB extends Statement {

private static final long INSTANCE_SIZE = RamUsageEstimator.shallowSizeOfInstance(CountDB.class);

public CountDB(final NodeLocation location) {
super(requireNonNull(location, "location is null"));
}

@Override
public <R, C> R accept(final IAstVisitor<R, C> visitor, final C context) {
return ((AstVisitor<R, C>) visitor).visitCountDB(this, context);
}

@Override
public List<Node> getChildren() {
return ImmutableList.of();
}

@Override
public int hashCode() {
return getClass().hashCode();
}

@Override
public boolean equals(final Object obj) {
if (this == obj) {
return true;
}
return (obj != null) && (getClass() == obj.getClass());
}

@Override
public String toString() {
return "COUNT DATABASES";
}

@Override
public long ramBytesUsed() {
long size = INSTANCE_SIZE;
size += AstMemoryEstimationHelper.getEstimatedSizeOfNodeLocation(getLocationInternal());
return size;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -162,6 +162,7 @@
import org.apache.iotdb.db.queryengine.plan.relational.sql.ast.ClearCache;
import org.apache.iotdb.db.queryengine.plan.relational.sql.ast.ColumnDefinition;
import org.apache.iotdb.db.queryengine.plan.relational.sql.ast.CopyTo;
import org.apache.iotdb.db.queryengine.plan.relational.sql.ast.CountDB;
import org.apache.iotdb.db.queryengine.plan.relational.sql.ast.CountDevice;
import org.apache.iotdb.db.queryengine.plan.relational.sql.ast.CountStatement;
import org.apache.iotdb.db.queryengine.plan.relational.sql.ast.CreateDB;
Expand Down Expand Up @@ -406,6 +407,12 @@
return new ShowDB(getLocation(ctx), Objects.nonNull(ctx.DETAILS()));
}

@Override
public Node visitCountDatabasesStatement(
final RelationalSqlParser.CountDatabasesStatementContext ctx) {
return new CountDB(getLocation(ctx));
}

@Override
public Node visitCreateDbStatement(final RelationalSqlParser.CreateDbStatementContext ctx) {
List<Property> properties = ImmutableList.of();
Expand Down Expand Up @@ -1993,7 +2000,7 @@
}

@Override
public Node visitGrantStatement(RelationalSqlParser.GrantStatementContext ctx) {

Check warning on line 2003 in iotdb-core/datanode/src/main/java/org/apache/iotdb/db/queryengine/plan/relational/sql/parser/AstBuilder.java

View check run for this annotation

SonarQubeCloud / SonarCloud Code Analysis

A "Brain Method" was detected. Refactor it to reduce at least one of the following metrics: LOC from 81 to 64, Complexity from 29 to 14, Nesting Level from 4 to 2, Number of Variables from 10 to 6.

See more on https://sonarcloud.io/project/issues?id=apache_iotdb&issues=AZ452B87hugVU3q9_7uB&open=AZ452B87hugVU3q9_7uB&pullRequest=17705
boolean toUser;
String name;
toUser = ctx.holderType().getText().equalsIgnoreCase("user");
Expand Down Expand Up @@ -2078,7 +2085,7 @@
throw new SemanticException(DataNodeQueryMessages.AUTHOR_STATEMENT_PARSER_ERROR);
}

public Node visitRevokeStatement(RelationalSqlParser.RevokeStatementContext ctx) {

Check warning on line 2088 in iotdb-core/datanode/src/main/java/org/apache/iotdb/db/queryengine/plan/relational/sql/parser/AstBuilder.java

View check run for this annotation

SonarQubeCloud / SonarCloud Code Analysis

A "Brain Method" was detected. Refactor it to reduce at least one of the following metrics: LOC from 89 to 64, Complexity from 27 to 14, Nesting Level from 4 to 2, Number of Variables from 9 to 6.

See more on https://sonarcloud.io/project/issues?id=apache_iotdb&issues=AZ452B87hugVU3q9_7uC&open=AZ452B87hugVU3q9_7uC&pullRequest=17705
boolean fromUser;
String name;
fromUser = ctx.holderType().getText().equalsIgnoreCase("user");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
import org.apache.iotdb.db.queryengine.plan.relational.sql.ast.AstVisitor;
import org.apache.iotdb.db.queryengine.plan.relational.sql.ast.ColumnDefinition;
import org.apache.iotdb.db.queryengine.plan.relational.sql.ast.CopyTo;
import org.apache.iotdb.db.queryengine.plan.relational.sql.ast.CountDB;
import org.apache.iotdb.db.queryengine.plan.relational.sql.ast.CreateDB;
import org.apache.iotdb.db.queryengine.plan.relational.sql.ast.CreateFunction;
import org.apache.iotdb.db.queryengine.plan.relational.sql.ast.CreatePipe;
Expand Down Expand Up @@ -134,7 +135,13 @@ public Void visitExplainAnalyze(ExplainAnalyze node, Integer indent) {

@Override
public Void visitShowDB(ShowDB node, Integer indent) {
builder.append("SHOW DATABASE");
builder.append("SHOW DATABASES");
return null;
}

@Override
public Void visitCountDB(CountDB node, Integer indent) {
builder.append("COUNT DATABASES");
return null;
}

Expand Down
Loading
Loading