| // 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.doris.utframe; |
| |
| import org.apache.doris.alter.AlterJobV2; |
| import org.apache.doris.analysis.AlterTableStmt; |
| import org.apache.doris.analysis.CreateDbStmt; |
| import org.apache.doris.analysis.CreateTableStmt; |
| import org.apache.doris.catalog.Catalog; |
| import org.apache.doris.catalog.Database; |
| import org.apache.doris.catalog.MaterializedIndexMeta; |
| import org.apache.doris.catalog.OlapTable; |
| import org.apache.doris.common.DdlException; |
| import org.apache.doris.common.FeConstants; |
| import org.apache.doris.planner.OlapScanNode; |
| import org.apache.doris.planner.PlanFragment; |
| import org.apache.doris.planner.Planner; |
| import org.apache.doris.qe.ConnectContext; |
| import org.apache.doris.qe.StmtExecutor; |
| import org.apache.doris.utframe.MockedFrontend.EnvVarNotSetException; |
| import org.apache.doris.utframe.MockedFrontend.FeStartException; |
| import org.apache.doris.utframe.MockedFrontend.NotInitException; |
| |
| import org.junit.AfterClass; |
| import org.junit.Assert; |
| import org.junit.BeforeClass; |
| import org.junit.Test; |
| |
| import java.io.IOException; |
| import java.util.List; |
| import java.util.Map; |
| import java.util.UUID; |
| |
| /* |
| * This demo shows how to run unit test with mocked FE and BE. |
| * It will |
| * 1. start a mocked FE and a mocked BE. |
| * 2. Create a database and a tbl. |
| * 3. Make a schema change to tbl. |
| * 4. send a query and get query plan |
| */ |
| public class DemoTest { |
| |
| // use a unique dir so that it won't be conflict with other unit test which |
| // may also start a Mocked Frontend |
| private static String runningDirBase = "fe"; |
| private static String runningDir = runningDirBase + "/mocked/DemoTest/" + UUID.randomUUID().toString() + "/"; |
| |
| @BeforeClass |
| public static void beforeClass() throws EnvVarNotSetException, IOException, |
| FeStartException, NotInitException, DdlException, InterruptedException { |
| FeConstants.default_scheduler_interval_millisecond = 10; |
| UtFrameUtils.createMinDorisCluster(runningDir); |
| } |
| |
| |
| @AfterClass |
| public static void TearDown() { |
| UtFrameUtils.cleanDorisFeDir(runningDirBase); |
| } |
| |
| @Test |
| public void testCreateDbAndTable() throws Exception { |
| // 1. create connect context |
| ConnectContext ctx = UtFrameUtils.createDefaultCtx(); |
| // 2. create database db1 |
| String createDbStmtStr = "create database db1;"; |
| CreateDbStmt createDbStmt = (CreateDbStmt) UtFrameUtils.parseAndAnalyzeStmt(createDbStmtStr, ctx); |
| Catalog.getCurrentCatalog().createDb(createDbStmt); |
| System.out.println(Catalog.getCurrentCatalog().getDbNames()); |
| // 3. create table tbl1 |
| String createTblStmtStr = "create table db1.tbl1(k1 int) distributed by hash(k1) buckets 3 properties('replication_num' = '1');"; |
| CreateTableStmt createTableStmt = (CreateTableStmt) UtFrameUtils.parseAndAnalyzeStmt(createTblStmtStr, ctx); |
| Catalog.getCurrentCatalog().createTable(createTableStmt); |
| // 4. get and test the created db and table |
| Database db = Catalog.getCurrentCatalog().getDb("default_cluster:db1"); |
| Assert.assertNotNull(db); |
| OlapTable tbl = (OlapTable) db.getTable("tbl1"); |
| tbl.readLock(); |
| try { |
| Assert.assertNotNull(tbl); |
| System.out.println(tbl.getName()); |
| Assert.assertEquals("Doris", tbl.getEngine()); |
| Assert.assertEquals(1, tbl.getBaseSchema().size()); |
| } finally { |
| tbl.readUnlock(); |
| } |
| // 5. process a schema change job |
| String alterStmtStr = "alter table db1.tbl1 add column k2 int default '1'"; |
| AlterTableStmt alterTableStmt = (AlterTableStmt) UtFrameUtils.parseAndAnalyzeStmt(alterStmtStr, ctx); |
| Catalog.getCurrentCatalog().getAlterInstance().processAlterTable(alterTableStmt); |
| // 6. check alter job |
| Map<Long, AlterJobV2> alterJobs = Catalog.getCurrentCatalog().getSchemaChangeHandler().getAlterJobsV2(); |
| Assert.assertEquals(1, alterJobs.size()); |
| for (AlterJobV2 alterJobV2 : alterJobs.values()) { |
| while (!alterJobV2.getJobState().isFinalState()) { |
| System.out.println("alter job " + alterJobV2.getJobId() + " is running. state: " + alterJobV2.getJobState()); |
| Thread.sleep(1000); |
| } |
| System.out.println("alter job " + alterJobV2.getJobId() + " is done. state: " + alterJobV2.getJobState()); |
| Assert.assertEquals(AlterJobV2.JobState.FINISHED, alterJobV2.getJobState()); |
| } |
| |
| OlapTable tbl1 = (OlapTable) db.getTable("tbl1"); |
| tbl1.readLock(); |
| try { |
| Assert.assertEquals(2, tbl1.getBaseSchema().size()); |
| String baseIndexName = tbl1.getIndexNameById(tbl.getBaseIndexId()); |
| Assert.assertEquals(baseIndexName, tbl1.getName()); |
| MaterializedIndexMeta indexMeta = tbl1.getIndexMetaByIndexId(tbl1.getBaseIndexId()); |
| Assert.assertNotNull(indexMeta); |
| } finally { |
| tbl1.readUnlock(); |
| } |
| // 7. query |
| // TODO: we can not process real query for now. So it has to be a explain query |
| String queryStr = "explain select * from db1.tbl1"; |
| StmtExecutor stmtExecutor = new StmtExecutor(ctx, queryStr); |
| stmtExecutor.execute(); |
| Planner planner = stmtExecutor.planner(); |
| List<PlanFragment> fragments = planner.getFragments(); |
| Assert.assertEquals(1, fragments.size()); |
| PlanFragment fragment = fragments.get(0); |
| Assert.assertTrue(fragment.getPlanRoot() instanceof OlapScanNode); |
| Assert.assertEquals(0, fragment.getChildren().size()); |
| } |
| } |