morningman commented on a change in pull request #2821: The new materialized view selector URL: https://github.com/apache/incubator-doris/pull/2821#discussion_r383721342
########## File path: fe/src/test/java/org/apache/doris/planner/MaterializedViewFunctionTest.java ########## @@ -0,0 +1,562 @@ +// 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.planner; + +import org.apache.doris.utframe.DorisAssert; +import org.apache.doris.utframe.UtFrameUtils; + +import org.junit.After; +import org.junit.Before; +import org.junit.BeforeClass; +import org.junit.Test; + +import java.util.UUID; + +public class MaterializedViewFunctionTest { + private static String runningDir = "fe/mocked/DemoTest/" + UUID.randomUUID().toString() + "/"; + private static final String EMPS_TABLE_NAME = "emps"; + private static final String EMPS_MV_NAME = "emps_mv"; + private static final String HR_DB_NAME = "db1"; + private static final String QUERY_USE_EMPS_MV = "rollup: " + EMPS_MV_NAME; + private static final String QUERY_USE_EMPS = "rollup: " + EMPS_TABLE_NAME; + private static final String DEPTS_TABLE_NAME = "depts"; + private static final String DEPTS_MV_NAME = "depts_mv"; + private static final String QUERY_USE_DEPTS_MV = "rollup: " + DEPTS_MV_NAME; + private static final String QUERY_USE_DEPTS = "rollup: " + DEPTS_TABLE_NAME; + private static DorisAssert dorisAssert; + + @BeforeClass + public static void beforeClass() throws Exception { + UtFrameUtils.createMinDorisCluster(runningDir); + dorisAssert = new DorisAssert(); + dorisAssert.withEnableMV().withDatabase(HR_DB_NAME).useDatabase(HR_DB_NAME); + } + + @Before + public void beforeMethod() throws Exception { + String createTableSQL = "create table " + HR_DB_NAME + "." + EMPS_TABLE_NAME + " (empid int, name varchar, " + + "deptno int, salary int, commission int) " + "distributed by hash(empid) buckets 3 properties('replication_num' = '1');"; + dorisAssert.withTable(createTableSQL); + createTableSQL = "create table " + HR_DB_NAME + "." + DEPTS_TABLE_NAME + " (deptno int, name varchar, cost " + + "int) " + "distributed by hash(deptno) buckets 3 properties('replication_num' = '1');"; + dorisAssert.withTable(createTableSQL); + } + + @After + public void afterMethod() throws Exception { + dorisAssert.dropTable(EMPS_TABLE_NAME); + dorisAssert.dropTable(DEPTS_TABLE_NAME); + } + + @Test + public void testProjectionMV1() throws Exception { + String createMVSQL = "create materialized view " + EMPS_MV_NAME + " as select deptno, empid from " + + EMPS_TABLE_NAME + " order by deptno;"; + String query = "select empid, deptno from " + EMPS_TABLE_NAME + ";"; + dorisAssert.withMaterializedView(createMVSQL); + dorisAssert.query(query).explainContains(QUERY_USE_EMPS_MV); + } + + @Test + public void testProjectionMV2() throws Exception { + String createMVSQL = "create materialized view " + EMPS_MV_NAME + " as select deptno, empid from " + + EMPS_TABLE_NAME + " order by deptno;"; + String query1 = "select empid + 1 from " + EMPS_TABLE_NAME + " where deptno = 10;"; + dorisAssert.withMaterializedView(createMVSQL); + dorisAssert.query(query1).explainContains(QUERY_USE_EMPS_MV); + String query2 = "select name from " + EMPS_TABLE_NAME + " where deptno -10 = 0;"; + dorisAssert.query(query2).explainWithout(QUERY_USE_EMPS_MV); + + } + + @Test + public void testProjectionMV3() throws Exception { + String createMVSQL = "create materialized view " + EMPS_MV_NAME + " as select deptno, empid, name from " + + EMPS_TABLE_NAME + " order by deptno;"; + String query1 = "select empid +1, name from " + EMPS_TABLE_NAME + " where deptno = 10;"; + dorisAssert.withMaterializedView(createMVSQL); + dorisAssert.query(query1).explainContains(QUERY_USE_EMPS_MV); + String query2 = "select name from " + EMPS_TABLE_NAME + " where deptno - 10 = 0;"; + dorisAssert.query(query2).explainContains(QUERY_USE_EMPS_MV); + } + + @Test + public void testProjectionMV4() throws Exception { + String createMVSQL = "create materialized view " + EMPS_MV_NAME + " as select name, deptno, salary from " + + EMPS_TABLE_NAME + ";"; + String query1 = "select name from " + EMPS_TABLE_NAME + " where deptno > 30 and salary > 3000;"; + dorisAssert.withMaterializedView(createMVSQL); + dorisAssert.query(query1).explainContains(QUERY_USE_EMPS_MV); + String query2 = "select empid from " + EMPS_TABLE_NAME + " where deptno > 30 and empid > 10;"; + dorisAssert.query(query2).explainWithout(QUERY_USE_EMPS_MV); + } + + @Test + public void testUnionQueryOnProjectionMV() throws Exception { + String createMVSQL = "create materialized view " + EMPS_MV_NAME + " as select deptno, empid from " + + EMPS_TABLE_NAME + " order by deptno;"; + String union = "select empid from " + EMPS_TABLE_NAME + " where deptno > 300" + " union all select empid from" + + " " + EMPS_TABLE_NAME + " where deptno < 200"; + dorisAssert.withMaterializedView(createMVSQL).query(union).explainContains(QUERY_USE_EMPS_MV); + } + + @Test + public void testAggQueryOnAggMV1() throws Exception { + String createMVSQL = "create materialized view " + EMPS_MV_NAME + " as select deptno, sum(salary), max" + "" + + "(salary) from " + EMPS_TABLE_NAME + " group by deptno;"; + String query = "select sum(salary), deptno from " + EMPS_TABLE_NAME + " group by deptno;"; + dorisAssert.withMaterializedView(createMVSQL).query(query).explainContains(QUERY_USE_EMPS_MV); + } + + @Test + public void testAggQueryOnAggMV2() throws Exception { + String agg = "select deptno, sum(salary) from " + EMPS_TABLE_NAME + " group by deptno"; + String createMVSQL = "create materialized view " + EMPS_MV_NAME + " as " + agg + ";"; + String query = "select * from (select deptno, sum(salary) as sum_salary from " + EMPS_TABLE_NAME + " group " + + "by" + " deptno) a where (sum_salary * 2) > 3;"; + dorisAssert.withMaterializedView(createMVSQL).query(query).explainContains(QUERY_USE_EMPS_MV); + } + + /* + TODO + The deduplicate materialized view is not yet supported + @Test + public void testAggQueryOnDeduplicatedMV() throws Exception { + String deduplicateSQL = "select deptno, empid, name, salary, commission from " + EMPS_TABLE_NAME + " group " + + "by" + " deptno, empid, name, salary, commission"; + String createMVSQL = "create materialized view " + EMPS_MV_NAME + " as " + deduplicateSQL + ";"; + String query1 = "select deptno, sum(salary) from (" + deduplicateSQL + ") A group by deptno;"; + dorisAssert.withMaterializedView(createMVSQL); + dorisAssert.query(query1).explainContains(QUERY_USE_EMPS_MV); + String query2 = "select deptno, empid from " + EMPS_TABLE_NAME + ";"; + dorisAssert.query(query2).explainWithout(QUERY_USE_EMPS_MV); + } + */ + + @Test + public void testAggQueryOnAggMV3() throws Exception { + String createMVSQL = "create materialized view " + EMPS_MV_NAME + " as select deptno, commission, sum(salary)" + + " from " + EMPS_TABLE_NAME + " group by deptno, commission;"; + String query = "select commission, sum(salary) from " + EMPS_TABLE_NAME + " where commission * (deptno + " + + "commission) = 100 group by commission;"; + dorisAssert.withMaterializedView(createMVSQL); + dorisAssert.query(query).explainContains(QUERY_USE_EMPS_MV); + } + + /** + * Matching failed because the filtering condition under Aggregate + * references columns for aggregation. + */ + @Test + public void testAggQueryOnAggMV4() throws Exception { + String createMVSQL = "create materialized view " + EMPS_MV_NAME + " as select deptno, commission, sum(salary)" + + " from " + EMPS_TABLE_NAME + " group by deptno, commission;"; + String query = "select deptno, sum(salary) from " + EMPS_TABLE_NAME + " where salary>1000 group by deptno;"; + dorisAssert.withMaterializedView(createMVSQL); + dorisAssert.query(query).explainWithout(QUERY_USE_EMPS_MV); + } + + /** + * There will be a compensating Project added after matching of the Aggregate. + */ + @Test + public void testAggQuqeryOnAggMV5() throws Exception { + String createMVSQL = "create materialized view " + EMPS_MV_NAME + " as select deptno, commission, sum(salary)" + + " from " + EMPS_TABLE_NAME + " group by deptno, commission;"; + String query = "select * from (select deptno, sum(salary) as sum_salary from " + EMPS_TABLE_NAME + " group " + + "by" + " deptno) a where sum_salary>10;"; + dorisAssert.withMaterializedView(createMVSQL); + dorisAssert.query(query).explainContains(QUERY_USE_EMPS_MV); + } + + /** + * There will be a compensating Project + Filter added after matching of the Aggregate. + * + * @throws Exception + */ + @Test + public void testAggQuqeryOnAggMV6() throws Exception { + String createMVSQL = "create materialized view " + EMPS_MV_NAME + " as select deptno, commission, sum(salary)" + "" + "" + "" + "" + "" + "" + "" + "" + "" + "" + "" + "" + "" + "" + " from " + EMPS_TABLE_NAME + " " + "group by" + " deptno, commission;"; Review comment: Why there are lots of `""` in sql? ---------------------------------------------------------------- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. For queries about this service, please contact Infrastructure at: [email protected] With regards, Apache Git Services --------------------------------------------------------------------- To unsubscribe, e-mail: [email protected] For additional commands, e-mail: [email protected]
