diff --git a/fe/fe-core/src/main/java/com/starrocks/sql/analyzer/QueryAnalyzer.java b/fe/fe-core/src/main/java/com/starrocks/sql/analyzer/QueryAnalyzer.java index 0e55c7440c0b3..88ffc405ba7b5 100644 --- a/fe/fe-core/src/main/java/com/starrocks/sql/analyzer/QueryAnalyzer.java +++ b/fe/fe-core/src/main/java/com/starrocks/sql/analyzer/QueryAnalyzer.java @@ -55,8 +55,6 @@ import com.starrocks.common.Pair; import com.starrocks.common.profile.Timer; import com.starrocks.common.profile.Tracers; -import com.starrocks.common.util.concurrent.lock.LockType; -import com.starrocks.common.util.concurrent.lock.Locker; import com.starrocks.privilege.SecurityPolicyRewriteRule; import com.starrocks.qe.ConnectContext; import com.starrocks.server.GlobalStateMgr; @@ -1217,7 +1215,6 @@ public Table resolveTable(TableRelation tableRelation) { } MetaUtils.checkDbNullAndReport(db, dbName); - Locker locker = new Locker(); Table table = null; if (tableRelation.isSyncMVQuery()) { @@ -1228,17 +1225,11 @@ public Table resolveTable(TableRelation tableRelation) { Table mvTable = materializedIndex.first; Preconditions.checkState(mvTable != null); Preconditions.checkState(mvTable instanceof OlapTable); - try { - // Add read lock to avoid concurrent problems. - locker.lockDatabase(db, LockType.READ); - OlapTable mvOlapTable = new OlapTable(); - ((OlapTable) mvTable).copyOnlyForQuery(mvOlapTable); - // Copy the necessary olap table meta to avoid changing original meta; - mvOlapTable.setBaseIndexId(materializedIndex.second.getIndexId()); - table = mvOlapTable; - } finally { - locker.unLockDatabase(db, LockType.READ); - } + OlapTable mvOlapTable = new OlapTable(); + ((OlapTable) mvTable).copyOnlyForQuery(mvOlapTable); + // Copy the necessary olap table meta to avoid changing original meta; + mvOlapTable.setBaseIndexId(materializedIndex.second.getIndexId()); + table = mvOlapTable; } } } else { diff --git a/fe/fe-core/src/test/java/com/starrocks/planner/MaterializedViewManualTest.java b/fe/fe-core/src/test/java/com/starrocks/planner/MaterializedViewManualTest.java index a529af16f23b9..2bf55319e96d5 100644 --- a/fe/fe-core/src/test/java/com/starrocks/planner/MaterializedViewManualTest.java +++ b/fe/fe-core/src/test/java/com/starrocks/planner/MaterializedViewManualTest.java @@ -209,132 +209,6 @@ public void testNullableTestCase1() throws Exception { .match("join_null_mv_2"); } - @Test - public void testRewriteWithPushDownEquivalent1() throws Exception { - starRocksAssert.withTable("CREATE TABLE `tbl1` (\n" + - " `k1` date,\n" + - " `k2` decimal64(18, 2),\n" + - " `k3` varchar(255),\n" + - " `v1` bigint \n" + - ") ENGINE=OLAP \n" + - "DUPLICATE KEY(`k1`, `k2`, `k3`)\n" + - "DISTRIBUTED BY RANDOM\n" + - "PROPERTIES (\n" + - "\"replication_num\" = \"1\"\n" + - ");"); - starRocksAssert.withMaterializedView("CREATE MATERIALIZED VIEW `mv1` \n" + - "DISTRIBUTED BY RANDOM\n" + - "REFRESH ASYNC\n" + - "PROPERTIES (\n" + - "\"replication_num\" = \"1\"\n" + - ")\n" + - "AS SELECT k1, k2, k3, sum(v1) from tbl1 group by k1, k2, k3"); - { - String sql = "select t1.k1, " + - " sum(case when t1.k1 between date_add('2024-07-20', interval -1 month) and " + - " date_add('2024-07-20', interval 1 month) then t1.v1 else 0 end) " + - " from tbl1 t1 group by t1.k1"; - sql(sql).contains("mv1") - .contains(" 1:AGGREGATE (update serialize)\n" + - " | STREAMING\n" + - " | output: sum(if((7: k1 >= '2024-06-20') AND (7: k1 <= '2024-08-20'), 10: sum(v1), 0))\n" + - " | group by: 7: k1\n" + - " | \n" + - " 0:OlapScanNode\n" + - " TABLE: mv1"); - } - { - String sql = "select t1.k1, " + - " 2 * sum(case when t1.k1 between date_add('2024-07-20', interval -1 month) and " + - " date_add('2024-07-20', interval 1 month) then t1.v1 else 0 end) " + - " from tbl1 t1 group by t1.k1"; - sql(sql).contains("mv1") - .contains(" 1:AGGREGATE (update serialize)\n" + - " | STREAMING\n" + - " | output: sum(if((7: k1 >= '2024-06-20') AND (7: k1 <= '2024-08-20'), 10: sum(v1), 0))\n" + - " | group by: 7: k1\n" + - " | \n" + - " 0:OlapScanNode\n" + - " TABLE: mv1") - .contains(" 4:Project\n" + - " | : 8: k1\n" + - " | : 2 * 12: sum"); - } - starRocksAssert.dropMaterializedView("mv1"); - starRocksAssert.dropTable("tbl1"); - } - - @Test - public void testRewriteWithPushDownEquivalent2() throws Exception { - starRocksAssert.withTable("CREATE TABLE `tbl1` (\n" + - " `k1` date,\n" + - " `k2` decimal64(18, 2),\n" + - " `k3` varchar(255),\n" + - " `v1` bigint \n" + - ") ENGINE=OLAP \n" + - "DUPLICATE KEY(`k1`, `k2`, `k3`)\n" + - "DISTRIBUTED BY RANDOM\n" + - "PROPERTIES (\n" + - "\"replication_num\" = \"1\"\n" + - ");"); - starRocksAssert.withMaterializedView("CREATE MATERIALIZED VIEW `mv1` \n" + - "DISTRIBUTED BY RANDOM\n" + - "REFRESH ASYNC\n" + - "PROPERTIES (\n" + - "\"replication_num\" = \"1\"\n" + - ")\n" + - "AS SELECT k1, k2, k3, sum(2 * v1) from tbl1 group by k1, k2, k3"); - { - // mv's sum doesn't contain column ref which cannot be used for rewrite - String sql = "select t1.k1, " + - " sum(case when t1.k1 between date_add('2024-07-20', interval -1 month) and " + - " date_add('2024-07-20', interval 1 month) then 2 * t1.v1 else 0 end) " + - " from tbl1 t1 group by t1.k1"; - sql(sql).notContain("mv1"); - } - starRocksAssert.dropMaterializedView("mv1"); - starRocksAssert.dropTable("tbl1"); - } - - @Test - public void testRewriteWithPushDownEquivalent3() throws Exception { - starRocksAssert.withTable("CREATE TABLE `tbl1` (\n" + - " `k1` date,\n" + - " `k2` decimal64(18, 2),\n" + - " `k3` varchar(255),\n" + - " `v1` bigint \n" + - ") ENGINE=OLAP \n" + - "DUPLICATE KEY(`k1`, `k2`, `k3`)\n" + - "DISTRIBUTED BY RANDOM\n" + - "PROPERTIES (\n" + - "\"replication_num\" = \"1\"\n" + - ");"); - starRocksAssert.withMaterializedView("CREATE MATERIALIZED VIEW `mv1` \n" + - "DISTRIBUTED BY RANDOM\n" + - "REFRESH ASYNC\n" + - "PROPERTIES (\n" + - "\"replication_num\" = \"1\"\n" + - ")\n" + - "AS SELECT k1, k2, k3, sum(v1), max(v1) from tbl1 group by k1, k2, k3"); - { - String sql = "select t1.k1, 2 * min(v1 + 1) from tbl1 t1 group by t1.k1"; - sql(sql).notContain("mv1"); - } - { - String sql = "select t1.k1, 2 * sum(case when t1.v1 > 10 then t1.v1 else 0 end) " + - " from tbl1 t1 group by t1.k1"; - sql(sql).notContain("mv1"); - } - { - String sql = "select t1.k1, " + - " 2 * sum(v1 + cast(k3 as int)) " + - " from tbl1 t1 group by t1.k1"; - sql(sql).notContain("mv1"); - } - starRocksAssert.dropMaterializedView("mv1"); - starRocksAssert.dropTable("tbl1"); - } - @Test public void testNullableTestCase2() throws Exception { String mv = "create materialized view join_null_mv\n" + @@ -455,97 +329,7 @@ public void testDateTruncPartitionColumnExpr2() throws Exception { starRocksAssert.dropTable("test_partition_expr_tbl1"); } - @Test - public void testRewriteWithPushDownEquivalent4() throws Exception { - starRocksAssert.withTable("CREATE TABLE `tbl1` (\n" + - " `k1` date,\n" + - " `k2` decimal64(18, 2),\n" + - " `k3` varchar(255),\n" + - " `v1` bigint \n" + - ") ENGINE=OLAP \n" + - "DUPLICATE KEY(`k1`, `k2`, `k3`)\n" + - "DISTRIBUTED BY RANDOM\n" + - "PROPERTIES (\n" + - "\"replication_num\" = \"1\"\n" + - ");"); - starRocksAssert.withMaterializedView("CREATE MATERIALIZED VIEW `mv1` \n" + - "DISTRIBUTED BY RANDOM\n" + - "REFRESH ASYNC\n" + - "PROPERTIES (\n" + - "\"replication_num\" = \"1\"\n" + - ")\n" + - "AS SELECT k1, k3, sum(k2) from tbl1 group by k1, k3"); - // sum(decimal) - { - String sql = "select t1.k1, " + - " 2 * sum(case when t1.k1 between date_add('2024-07-20', interval -1 month) and " + - " date_add('2024-07-20', interval 1 month) then t1.k2 else 0 end) " + - " from tbl1 t1 group by t1.k1"; - String plan = getVerboseExplain(sql); - PlanTestBase.assertContains(plan, "mv1"); - PlanTestBase.assertContains(plan, " | aggregate: sum[(if[((8: k1 >= '2024-06-20') AND " + - "(8: k1 <= '2024-08-20'), [10: sum(k2), DECIMAL128(38,2), true], cast(0 as DECIMAL128(38,2))); " + - "args: BOOLEAN,DECIMAL128,DECIMAL128; result: DECIMAL128(38,2); args nullable: true; " + - "result nullable: true]); args: DECIMAL128; result: DECIMAL128(38,2); " + - "args nullable: true; result nullable: true]"); - } - { - String sql = "select t1.k1, " + - " 2 * sum(case when t1.k1 between date_add('2024-07-20', interval -1 month) and " + - " date_add('2024-07-20', interval 1 month) then 2 * t1.k2 else 0 end) " + - " from tbl1 t1 group by t1.k1"; - String plan = getCostExplain(sql); - PlanTestBase.assertNotContains(plan, "mv1"); - } - starRocksAssert.dropMaterializedView("mv1"); - starRocksAssert.dropTable("tbl1"); - } - - @Test - public void testRewriteWithPushDownEquivalent5() throws Exception { - starRocksAssert.withTable("CREATE TABLE `tbl1` (\n" + - " `k1` date,\n" + - " `k2` decimal64(18, 2),\n" + - " `k3` varchar(255),\n" + - " `v1` bigint \n" + - ") ENGINE=OLAP \n" + - "DUPLICATE KEY(`k1`, `k2`, `k3`)\n" + - "DISTRIBUTED BY RANDOM\n" + - "PROPERTIES (\n" + - "\"replication_num\" = \"1\"\n" + - ");"); - starRocksAssert.withMaterializedView("CREATE MATERIALIZED VIEW `mv1` \n" + - "DISTRIBUTED BY RANDOM\n" + - "REFRESH ASYNC\n" + - "PROPERTIES (\n" + - "\"replication_num\" = \"1\"\n" + - ")\n" + - "AS SELECT k1, k3, sum(k2) from tbl1 group by k1, k3"); - // sum(decimal) - { - String sql = "select t1.k1, " + - " 2 * sum(case when t1.k1 between date_add('2024-07-20', interval -1 month) and " + - " date_add('2024-07-20', interval 1 month) then t1.k2 when t1.k3 ='xxxx' then k2 else 0 end) " + - " from tbl1 t1 group by t1.k1"; - String plan = getVerboseExplain(sql); - PlanTestBase.assertContains(plan, "mv1"); - PlanTestBase.assertContains(plan, "| aggregate: sum[(CASE WHEN (8: k1 >= '2024-06-20') AND " + - "(8: k1 <= '2024-08-20') THEN 10: sum(k2) WHEN 9: k3 = 'xxxx' THEN 10: sum(k2) " + - "ELSE CAST(0 AS DECIMAL128(38,2)) END); args: DECIMAL128; result: DECIMAL128(38,2); " + - "args nullable: true; result nullable: true]"); - } - { - String sql = "select t1.k1, " + - " 2 * sum(case when t1.k1 between date_add('2024-07-20', interval -1 month) and " + - " date_add('2024-07-20', interval 1 month) then t1.k2 when t1.k3 ='xxxx' then k2 + 1 else 0 end) " + - " from tbl1 t1 group by t1.k1"; - String plan = getVerboseExplain(sql); - PlanTestBase.assertNotContains(plan, "mv1"); - } - starRocksAssert.dropMaterializedView("mv1"); - starRocksAssert.dropTable("tbl1"); - } @Test public void testMvRewriteForColumnReorder() throws Exception { @@ -675,24 +459,24 @@ public void testRewriteWithEliminateJoinsBasic1() { @Test public void testRewriteWithEliminateJoinsBasic2() throws Exception { - starRocksAssert.withTable("CREATE TABLE `tbl1` (\n" + - " `k1` date,\n" + - " `k2` decimal64(18, 2),\n" + - " `k3` varchar(255),\n" + - " `v1` varchar(255)\n" + - ") ENGINE=OLAP \n" + - "DUPLICATE KEY(`k1`, `k2`, `k3`)\n" + - "DISTRIBUTED BY RANDOM\n" + - "PROPERTIES (\n" + - "\"replication_num\" = \"1\"\n" + - ");"); - starRocksAssert.withMaterializedView("CREATE MATERIALIZED VIEW `mv1` \n" + - "DISTRIBUTED BY RANDOM\n" + - "REFRESH ASYNC\n" + - "PROPERTIES (\n" + - "\"replication_num\" = \"1\"\n" + - ")\n" + - "AS SELECT k1, k2, k3, sum(v1) from tbl1 group by k1, k2, k3"); + starRocksAssert.withTable("CREATE TABLE `tbl1` (\n" + + " `k1` date,\n" + + " `k2` decimal64(18, 2),\n" + + " `k3` varchar(255),\n" + + " `v1` varchar(255)\n" + + ") ENGINE=OLAP \n" + + "DUPLICATE KEY(`k1`, `k2`, `k3`)\n" + + "DISTRIBUTED BY RANDOM\n" + + "PROPERTIES (\n" + + "\"replication_num\" = \"1\"\n" + + ");"); + starRocksAssert.withMaterializedView("CREATE MATERIALIZED VIEW `mv1` \n" + + "DISTRIBUTED BY RANDOM\n" + + "REFRESH ASYNC\n" + + "PROPERTIES (\n" + + "\"replication_num\" = \"1\"\n" + + ")\n" + + "AS SELECT k1, k2, k3, sum(v1) from tbl1 group by k1, k2, k3"); { String sql = "with cte as(" + " select " + @@ -742,4 +526,353 @@ public void testRewriteWithEliminateJoinsBasic2() throws Exception { starRocksAssert.dropMaterializedView("mv1"); starRocksAssert.dropTable("tbl1"); } + + @Test + public void testRewriteWithPushDownEquivalent1() throws Exception { + starRocksAssert.withTable("CREATE TABLE `tbl1` (\n" + + " `k1` date,\n" + + " `k2` decimal64(18, 2),\n" + + " `k3` varchar(255),\n" + + " `v1` bigint \n" + + ") ENGINE=OLAP \n" + + "DUPLICATE KEY(`k1`, `k2`, `k3`)\n" + + "DISTRIBUTED BY RANDOM\n" + + "PROPERTIES (\n" + + "\"replication_num\" = \"1\"\n" + + ");"); + starRocksAssert.withMaterializedView("CREATE MATERIALIZED VIEW `mv1` \n" + + "DISTRIBUTED BY RANDOM\n" + + "REFRESH ASYNC\n" + + "PROPERTIES (\n" + + "\"replication_num\" = \"1\"\n" + + ")\n" + + "AS SELECT k1, k2, k3, sum(v1) from tbl1 group by k1, k2, k3"); + { + String sql = "select t1.k1, " + + " sum(case when t1.k1 between date_add('2024-07-20', interval -1 month) and " + + " date_add('2024-07-20', interval 1 month) then t1.v1 else 0 end) " + + " from tbl1 t1 group by t1.k1"; + sql(sql).contains("mv1") + .contains(" 1:AGGREGATE (update serialize)\n" + + " | STREAMING\n" + + " | output: sum(if((7: k1 >= '2024-06-20') AND (7: k1 <= '2024-08-20'), 10: sum(v1), 0))\n" + + " | group by: 7: k1\n" + + " | \n" + + " 0:OlapScanNode\n" + + " TABLE: mv1"); + } + { + String sql = "select t1.k1, " + + " 2 * sum(case when t1.k1 between date_add('2024-07-20', interval -1 month) and " + + " date_add('2024-07-20', interval 1 month) then t1.v1 else 0 end) " + + " from tbl1 t1 group by t1.k1"; + sql(sql).contains("mv1") + .contains(" 1:AGGREGATE (update serialize)\n" + + " | STREAMING\n" + + " | output: sum(if((7: k1 >= '2024-06-20') AND (7: k1 <= '2024-08-20'), 10: sum(v1), 0))\n" + + " | group by: 7: k1\n" + + " | \n" + + " 0:OlapScanNode\n" + + " TABLE: mv1") + .contains(" 4:Project\n" + + " | : 8: k1\n" + + " | : 2 * 12: sum"); + } + starRocksAssert.dropMaterializedView("mv1"); + starRocksAssert.dropTable("tbl1"); + } + + @Test + public void testRewriteWithPushDownEquivalent2() throws Exception { + starRocksAssert.withTable("CREATE TABLE `tbl1` (\n" + + " `k1` date,\n" + + " `k2` decimal64(18, 2),\n" + + " `k3` varchar(255),\n" + + " `v1` bigint \n" + + ") ENGINE=OLAP \n" + + "DUPLICATE KEY(`k1`, `k2`, `k3`)\n" + + "DISTRIBUTED BY RANDOM\n" + + "PROPERTIES (\n" + + "\"replication_num\" = \"1\"\n" + + ");"); + starRocksAssert.withMaterializedView("CREATE MATERIALIZED VIEW `mv1` \n" + + "DISTRIBUTED BY RANDOM\n" + + "REFRESH ASYNC\n" + + "PROPERTIES (\n" + + "\"replication_num\" = \"1\"\n" + + ")\n" + + "AS SELECT k1, k2, k3, sum(2 * v1) from tbl1 group by k1, k2, k3"); + { + // mv's sum doesn't contain column ref which cannot be used for rewrite + String sql = "select t1.k1, " + + " sum(case when t1.k1 between date_add('2024-07-20', interval -1 month) and " + + " date_add('2024-07-20', interval 1 month) then 2 * t1.v1 else 0 end) " + + " from tbl1 t1 group by t1.k1"; + sql(sql).notContain("mv1"); + } + starRocksAssert.dropMaterializedView("mv1"); + starRocksAssert.dropTable("tbl1"); + } + + @Test + public void testRewriteWithPushDownEquivalent3() throws Exception { + starRocksAssert.withTable("CREATE TABLE `tbl1` (\n" + + " `k1` date,\n" + + " `k2` decimal64(18, 2),\n" + + " `k3` varchar(255),\n" + + " `v1` bigint \n" + + ") ENGINE=OLAP \n" + + "DUPLICATE KEY(`k1`, `k2`, `k3`)\n" + + "DISTRIBUTED BY RANDOM\n" + + "PROPERTIES (\n" + + "\"replication_num\" = \"1\"\n" + + ");"); + starRocksAssert.withMaterializedView("CREATE MATERIALIZED VIEW `mv1` \n" + + "DISTRIBUTED BY RANDOM\n" + + "REFRESH ASYNC\n" + + "PROPERTIES (\n" + + "\"replication_num\" = \"1\"\n" + + ")\n" + + "AS SELECT k1, k2, k3, sum(v1), max(v1) from tbl1 group by k1, k2, k3"); + { + String sql = "select t1.k1, 2 * min(v1 + 1) from tbl1 t1 group by t1.k1"; + sql(sql).notContain("mv1"); + } + { + String sql = "select t1.k1, 2 * sum(case when t1.v1 > 10 then t1.v1 else 0 end) " + + " from tbl1 t1 group by t1.k1"; + sql(sql).notContain("mv1"); + } + { + String sql = "select t1.k1, " + + " 2 * sum(v1 + cast(k3 as int)) " + + " from tbl1 t1 group by t1.k1"; + sql(sql).notContain("mv1"); + } + starRocksAssert.dropMaterializedView("mv1"); + starRocksAssert.dropTable("tbl1"); + } + + @Test + public void testRewriteWithPushDownEquivalent4() throws Exception { + starRocksAssert.withTable("CREATE TABLE `tbl1` (\n" + + " `k1` date,\n" + + " `k2` decimal64(18, 2),\n" + + " `k3` varchar(255),\n" + + " `v1` bigint \n" + + ") ENGINE=OLAP \n" + + "DUPLICATE KEY(`k1`, `k2`, `k3`)\n" + + "DISTRIBUTED BY RANDOM\n" + + "PROPERTIES (\n" + + "\"replication_num\" = \"1\"\n" + + ");"); + starRocksAssert.withMaterializedView("CREATE MATERIALIZED VIEW `mv1` \n" + + "DISTRIBUTED BY RANDOM\n" + + "REFRESH ASYNC\n" + + "PROPERTIES (\n" + + "\"replication_num\" = \"1\"\n" + + ")\n" + + "AS SELECT k1, k3, sum(k2) from tbl1 group by k1, k3"); + // sum(decimal) + { + String sql = "select t1.k1, " + + " 2 * sum(case when t1.k1 between date_add('2024-07-20', interval -1 month) and " + + " date_add('2024-07-20', interval 1 month) then t1.k2 else 0 end) " + + " from tbl1 t1 group by t1.k1"; + String plan = getVerboseExplain(sql); + PlanTestBase.assertContains(plan, "mv1"); + PlanTestBase.assertContains(plan, " | aggregate: sum[(if[((8: k1 >= '2024-06-20') AND " + + "(8: k1 <= '2024-08-20'), [10: sum(k2), DECIMAL128(38,2), true], cast(0 as DECIMAL128(38,2))); " + + "args: BOOLEAN,DECIMAL128,DECIMAL128; result: DECIMAL128(38,2); args nullable: true; " + + "result nullable: true]); args: DECIMAL128; result: DECIMAL128(38,2); " + + "args nullable: true; result nullable: true]"); + } + { + String sql = "select t1.k1, " + + " 2 * sum(case when t1.k1 between date_add('2024-07-20', interval -1 month) and " + + " date_add('2024-07-20', interval 1 month) then 2 * t1.k2 else 0 end) " + + " from tbl1 t1 group by t1.k1"; + String plan = getCostExplain(sql); + PlanTestBase.assertNotContains(plan, "mv1"); + } + starRocksAssert.dropMaterializedView("mv1"); + starRocksAssert.dropTable("tbl1"); + } + + @Test + public void testRewriteWithPushDownEquivalent5() throws Exception { + starRocksAssert.withTable("CREATE TABLE `tbl1` (\n" + + " `k1` date,\n" + + " `k2` decimal64(18, 2),\n" + + " `k3` varchar(255),\n" + + " `v1` bigint \n" + + ") ENGINE=OLAP \n" + + "DUPLICATE KEY(`k1`, `k2`, `k3`)\n" + + "DISTRIBUTED BY RANDOM\n" + + "PROPERTIES (\n" + + "\"replication_num\" = \"1\"\n" + + ");"); + starRocksAssert.withMaterializedView("CREATE MATERIALIZED VIEW `mv1` \n" + + "DISTRIBUTED BY RANDOM\n" + + "REFRESH ASYNC\n" + + "PROPERTIES (\n" + + "\"replication_num\" = \"1\"\n" + + ")\n" + + "AS SELECT k1, k3, sum(k2) from tbl1 group by k1, k3"); + // sum(decimal) + { + String sql = "select t1.k1, " + + " 2 * sum(case when t1.k1 between date_add('2024-07-20', interval -1 month) and " + + " date_add('2024-07-20', interval 1 month) then t1.k2 when t1.k3 ='xxxx' then k2 else 0 end) " + + " from tbl1 t1 group by t1.k1"; + String plan = getVerboseExplain(sql); + PlanTestBase.assertContains(plan, "mv1"); + PlanTestBase.assertContains(plan, "| aggregate: sum[(CASE WHEN (8: k1 >= '2024-06-20') AND " + + "(8: k1 <= '2024-08-20') THEN 10: sum(k2) WHEN 9: k3 = 'xxxx' THEN 10: sum(k2) " + + "ELSE CAST(0 AS DECIMAL128(38,2)) END); args: DECIMAL128; result: DECIMAL128(38,2); " + + "args nullable: true; result nullable: true]"); + } + + { + String sql = "select t1.k1, " + + " 2 * sum(case when t1.k1 between date_add('2024-07-20', interval -1 month) and " + + " date_add('2024-07-20', interval 1 month) then t1.k2 when t1.k3 ='xxxx' then k2 + 1 else 0 end) " + + " from tbl1 t1 group by t1.k1"; + String plan = getVerboseExplain(sql); + PlanTestBase.assertNotContains(plan, "mv1"); + } + starRocksAssert.dropMaterializedView("mv1"); + starRocksAssert.dropTable("tbl1"); + } + + @Test + public void testRewriteWithCaseWhen1() throws Exception { + starRocksAssert.withTable("CREATE TABLE `tbl1` (\n" + + " `k1` date,\n" + + " `k2` decimal64(18, 2),\n" + + " `k3` varchar(255),\n" + + " `v1` bigint \n" + + ") ENGINE=OLAP \n" + + "DUPLICATE KEY(`k1`, `k2`, `k3`)\n" + + "DISTRIBUTED BY RANDOM\n" + + "PROPERTIES (\n" + + "\"replication_num\" = \"1\"\n" + + ");"); + starRocksAssert.withMaterializedView("CREATE MATERIALIZED VIEW `mv1` \n" + + "DISTRIBUTED BY RANDOM\n" + + "REFRESH ASYNC\n" + + "PROPERTIES (\n" + + "\"replication_num\" = \"1\"\n" + + ")\n" + + "AS SELECT k1, k3, " + + " sum(k2) as sum1," + + " sum(case when t1.k1 between date_add('2024-07-20', interval -1 month) " + + "and date_add('2024-07-20', interval 1 month) then t1.k2 + 1 else 0 end ) as sum2 \n" + + " from tbl1 t1 group by k1, k3"); + // sum(decimal) + { + String sql = "select t1.k1, " + + " sum(case when t1.k1 between date_add('2024-07-20', interval -1 month) and " + + " date_add('2024-07-20', interval 1 month) then t1.k2 + 1 else 0 end) " + + " from tbl1 t1 group by t1.k1"; + String plan = getVerboseExplain(sql); + PlanTestBase.assertContains(plan, "mv1"); + PlanTestBase.assertContains(plan, " 1:AGGREGATE (update serialize)\n" + + " | STREAMING\n" + + " | aggregate: sum[([10: sum2, DECIMAL128(38,2), true]); args: DECIMAL128; result: DECIMAL128(38,2); " + + "args nullable: true; result nullable: true]\n" + + " | group by: [7: k1, DATE, true]\n" + + " | cardinality: 1"); + } + starRocksAssert.dropMaterializedView("mv1"); + starRocksAssert.dropTable("tbl1"); + } + + @Test + public void testRewriteWithCaseWhen2() throws Exception { + starRocksAssert.withTable("CREATE TABLE `tbl1` (\n" + + " `k1` date,\n" + + " `k2` decimal64(18, 2),\n" + + " `k3` varchar(255),\n" + + " `v1` bigint \n" + + ") ENGINE=OLAP \n" + + "DUPLICATE KEY(`k1`, `k2`, `k3`)\n" + + "DISTRIBUTED BY RANDOM\n" + + "PROPERTIES (\n" + + "\"replication_num\" = \"1\"\n" + + ");"); + starRocksAssert.withMaterializedView("CREATE MATERIALIZED VIEW `mv1` \n" + + "DISTRIBUTED BY RANDOM\n" + + "REFRESH ASYNC\n" + + "PROPERTIES (\n" + + "\"replication_num\" = \"1\"\n" + + ")\n" + + "AS SELECT k1, k3, " + + " sum(k2) as sum1," + + " sum(case when t1.k1 between date_add('2024-07-20', interval -1 month) " + + "and date_add('2024-07-20', interval 1 month) then t1.k2 + 1 else 0 end ) as sum2 \n" + + " from tbl1 t1 group by k1, k3"); + // mv: case when case when cond1 then val1 else val2 + // query: if (cond1, val1, val2) + { + String sql = "select t1.k1, " + + " sum(if(t1.k1 between date_add('2024-07-20', interval -1 month) and " + + " date_add('2024-07-20', interval 1 month), t1.k2 + 1, 0)) " + + " from tbl1 t1 group by t1.k1"; + String plan = getVerboseExplain(sql); + PlanTestBase.assertContains(plan, "mv1"); + PlanTestBase.assertContains(plan, " 1:AGGREGATE (update serialize)\n" + + " | STREAMING\n" + + " | aggregate: sum[([10: sum2, DECIMAL128(38,2), true]); args: DECIMAL128; result: DECIMAL128(38,2); " + + "args nullable: true; result nullable: true]\n" + + " | group by: [7: k1, DATE, true]\n" + + " | cardinality: 1"); + } + starRocksAssert.dropMaterializedView("mv1"); + starRocksAssert.dropTable("tbl1"); + } + + @Test + public void testRewriteWithCaseWhen3() throws Exception { + starRocksAssert.withTable("CREATE TABLE `tbl1` (\n" + + " `k1` date,\n" + + " `k2` decimal64(18, 2),\n" + + " `k3` varchar(255),\n" + + " `v1` bigint \n" + + ") ENGINE=OLAP \n" + + "DUPLICATE KEY(`k1`, `k2`, `k3`)\n" + + "DISTRIBUTED BY RANDOM\n" + + "PROPERTIES (\n" + + "\"replication_num\" = \"1\"\n" + + ");"); + starRocksAssert.withMaterializedView("CREATE MATERIALIZED VIEW `mv1` \n" + + "DISTRIBUTED BY RANDOM\n" + + "REFRESH ASYNC\n" + + "PROPERTIES (\n" + + "\"replication_num\" = \"1\"\n" + + ")\n" + + "AS SELECT k1, k3, " + + " sum(k2) as sum1," + + " sum(if(t1.k1 between date_add('2024-07-20', interval -1 month) and " + + " date_add('2024-07-20', interval 1 month), t1.k2 + 1, 0)) as sum2" + + " from tbl1 t1 group by k1, k3"); + // mv: if (cond1, val1, val2) + // query: case when case when cond1 then val1 else val2 + { + String sql = "select t1.k1, " + + " sum(case when t1.k1 between date_add('2024-07-20', interval -1 month) " + + "and date_add('2024-07-20', interval 1 month) then t1.k2 + 1 else 0 end ) \n" + + " from tbl1 t1 group by t1.k1"; + String plan = getVerboseExplain(sql); + PlanTestBase.assertContains(plan, "mv1"); + PlanTestBase.assertContains(plan, " 1:AGGREGATE (update serialize)\n" + + " | STREAMING\n" + + " | aggregate: sum[([10: sum2, DECIMAL128(38,2), true]); args: DECIMAL128; result: DECIMAL128(38,2); " + + "args nullable: true; result nullable: true]\n" + + " | group by: [7: k1, DATE, true]\n" + + " | cardinality: 1"); + } + starRocksAssert.dropMaterializedView("mv1"); + starRocksAssert.dropTable("tbl1"); + } }