Skip to content

Commit 14d2d0b

Browse files
committed
fix
1 parent 148250e commit 14d2d0b

3 files changed

Lines changed: 178 additions & 1 deletion

File tree

iotdb-core/datanode/src/main/java/org/apache/iotdb/db/queryengine/plan/execution/config/metadata/relational/CountDBTask.java

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,8 +59,12 @@ public static void buildTSBlock(
5959
final Map<String, ?> databaseInfoMap,
6060
final SettableFuture<ConfigTaskResult> future,
6161
final Predicate<String> canSeenDB) {
62+
// information_schema is synthesized in table model rather than returned from ConfigNode.
6263
final long databaseCount =
63-
databaseInfoMap.keySet().stream().filter(canSeenDB::test).count()
64+
databaseInfoMap.keySet().stream()
65+
.filter(databaseName -> !INFORMATION_DATABASE.equals(databaseName))
66+
.filter(canSeenDB::test)
67+
.count()
6468
+ (canSeenDB.test(INFORMATION_DATABASE) ? 1 : 0);
6569

6670
final TsBlockBuilder builder = new TsBlockBuilder(Collections.singletonList(TSDataType.INT32));
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,107 @@
1+
/*
2+
* Licensed to the Apache Software Foundation (ASF) under one
3+
* or more contributor license agreements. See the NOTICE file
4+
* distributed with this work for additional information
5+
* regarding copyright ownership. The ASF licenses this file
6+
* to you under the Apache License, Version 2.0 (the
7+
* "License"); you may not use this file except in compliance
8+
* with the License. You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing,
13+
* software distributed under the License is distributed on an
14+
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15+
* KIND, either express or implied. See the License for the
16+
* specific language governing permissions and limitations
17+
* under the License.
18+
*/
19+
20+
package org.apache.iotdb.db.queryengine.plan.execution.config.metadata.relational;
21+
22+
import org.apache.iotdb.commons.conf.IoTDBConstant;
23+
import org.apache.iotdb.commons.queryengine.plan.relational.sql.ast.NodeLocation;
24+
import org.apache.iotdb.db.queryengine.plan.execution.config.ConfigTaskResult;
25+
import org.apache.iotdb.db.queryengine.plan.execution.config.executor.IConfigTaskExecutor;
26+
import org.apache.iotdb.db.queryengine.plan.relational.sql.ast.CountDB;
27+
import org.apache.iotdb.rpc.TSStatusCode;
28+
29+
import com.google.common.util.concurrent.SettableFuture;
30+
import org.apache.tsfile.read.common.block.TsBlock;
31+
import org.junit.Test;
32+
import org.mockito.Mockito;
33+
34+
import java.util.Collections;
35+
import java.util.HashMap;
36+
import java.util.Map;
37+
import java.util.function.Predicate;
38+
39+
import static org.apache.iotdb.commons.schema.table.InformationSchema.INFORMATION_DATABASE;
40+
import static org.junit.Assert.assertEquals;
41+
import static org.junit.Assert.assertSame;
42+
import static org.mockito.ArgumentMatchers.same;
43+
import static org.mockito.Mockito.verify;
44+
import static org.mockito.Mockito.when;
45+
46+
public class CountDBTaskTest {
47+
48+
@Test
49+
public void testBuildTSBlockCountsVisibleDatabases() throws Exception {
50+
final Map<String, Object> databaseInfoMap = new HashMap<>();
51+
databaseInfoMap.put("db1", new Object());
52+
databaseInfoMap.put("db2", new Object());
53+
54+
final SettableFuture<ConfigTaskResult> future = SettableFuture.create();
55+
CountDBTask.buildTSBlock(databaseInfoMap, future, databaseName -> !"db2".equals(databaseName));
56+
57+
final ConfigTaskResult result = future.get();
58+
final TsBlock resultSet = result.getResultSet();
59+
60+
assertEquals(TSStatusCode.SUCCESS_STATUS, result.getStatusCode());
61+
assertEquals(
62+
Collections.singletonList(IoTDBConstant.COLUMN_COUNT),
63+
result.getResultSetHeader().getRespColumns());
64+
assertEquals(1, resultSet.getPositionCount());
65+
assertEquals(2, resultSet.getColumn(0).getInt(0));
66+
}
67+
68+
@Test
69+
public void testBuildTSBlockCanHideInformationSchema() throws Exception {
70+
final Map<String, Object> databaseInfoMap = new HashMap<>();
71+
databaseInfoMap.put("db1", new Object());
72+
databaseInfoMap.put("db2", new Object());
73+
74+
final SettableFuture<ConfigTaskResult> future = SettableFuture.create();
75+
CountDBTask.buildTSBlock(databaseInfoMap, future, databaseName -> "db1".equals(databaseName));
76+
77+
final ConfigTaskResult result = future.get();
78+
assertEquals(1, result.getResultSet().getColumn(0).getInt(0));
79+
}
80+
81+
@Test
82+
public void testBuildTSBlockDoesNotDoubleCountInformationSchema() throws Exception {
83+
final Map<String, Object> databaseInfoMap = new HashMap<>();
84+
databaseInfoMap.put("db1", new Object());
85+
databaseInfoMap.put(INFORMATION_DATABASE, new Object());
86+
87+
final SettableFuture<ConfigTaskResult> future = SettableFuture.create();
88+
CountDBTask.buildTSBlock(databaseInfoMap, future, databaseName -> true);
89+
90+
final ConfigTaskResult result = future.get();
91+
assertEquals(2, result.getResultSet().getColumn(0).getInt(0));
92+
}
93+
94+
@Test
95+
public void testExecuteDelegatesToExecutor() throws Exception {
96+
final CountDB node = new CountDB(new NodeLocation(1, 1));
97+
final Predicate<String> canSeenDB = databaseName -> true;
98+
final CountDBTask task = new CountDBTask(node, canSeenDB);
99+
final IConfigTaskExecutor executor = Mockito.mock(IConfigTaskExecutor.class);
100+
final SettableFuture<ConfigTaskResult> future = SettableFuture.create();
101+
102+
when(executor.countDatabases(same(node), same(canSeenDB))).thenReturn(future);
103+
104+
assertSame(future, task.execute(executor));
105+
verify(executor).countDatabases(same(node), same(canSeenDB));
106+
}
107+
}
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
/*
2+
* Licensed to the Apache Software Foundation (ASF) under one
3+
* or more contributor license agreements. See the NOTICE file
4+
* distributed with this work for additional information
5+
* regarding copyright ownership. The ASF licenses this file
6+
* to you under the Apache License, Version 2.0 (the
7+
* "License"); you may not use this file except in compliance
8+
* with the License. You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing,
13+
* software distributed under the License is distributed on an
14+
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15+
* KIND, either express or implied. See the License for the
16+
* specific language governing permissions and limitations
17+
* under the License.
18+
*/
19+
20+
package org.apache.iotdb.db.queryengine.plan.relational.sql.parser;
21+
22+
import org.apache.iotdb.commons.queryengine.plan.relational.sql.ast.Statement;
23+
import org.apache.iotdb.db.protocol.session.IClientSession;
24+
import org.apache.iotdb.db.protocol.session.InternalClientSession;
25+
import org.apache.iotdb.db.queryengine.plan.relational.sql.ast.CountDB;
26+
import org.apache.iotdb.db.queryengine.plan.relational.sql.util.DataNodeSqlFormatter;
27+
28+
import org.junit.Before;
29+
import org.junit.Test;
30+
31+
import java.time.ZoneId;
32+
33+
import static org.junit.Assert.assertEquals;
34+
import static org.junit.Assert.assertTrue;
35+
36+
public class CountDBStatementTest {
37+
38+
private SqlParser sqlParser;
39+
private IClientSession clientSession;
40+
41+
@Before
42+
public void setUp() {
43+
sqlParser = new SqlParser();
44+
clientSession = new InternalClientSession("testClient");
45+
}
46+
47+
@Test
48+
public void testCountDatabaseStatement() {
49+
final Statement statement =
50+
sqlParser.createStatement("count database", ZoneId.systemDefault(), clientSession);
51+
52+
assertTrue(statement instanceof CountDB);
53+
assertEquals("COUNT DATABASES", statement.toString());
54+
assertEquals("COUNT DATABASES", DataNodeSqlFormatter.formatDataNodeSql(statement));
55+
}
56+
57+
@Test
58+
public void testCountDatabasesStatement() {
59+
final Statement statement =
60+
sqlParser.createStatement("count databases", ZoneId.systemDefault(), clientSession);
61+
62+
assertTrue(statement instanceof CountDB);
63+
assertEquals("COUNT DATABASES", statement.toString());
64+
assertEquals("COUNT DATABASES", DataNodeSqlFormatter.formatDataNodeSql(statement));
65+
}
66+
}

0 commit comments

Comments
 (0)