|
| 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 | +} |
0 commit comments