From f384ab16c4847066c959086518ce7beea45d2a20 Mon Sep 17 00:00:00 2001 From: lambxu Date: Fri, 14 Nov 2025 18:08:52 +0800 Subject: [PATCH 1/3] save --- .../apache/doris/planner/OlapScanNode.java | 6 +- ...rialized_view_common_expr_push_down.groovy | 91 +++++++++++++++++++ 2 files changed, 96 insertions(+), 1 deletion(-) create mode 100644 regression-test/suites/rollup_p0/test_materialized_view_common_expr_push_down.groovy diff --git a/fe/fe-core/src/main/java/org/apache/doris/planner/OlapScanNode.java b/fe/fe-core/src/main/java/org/apache/doris/planner/OlapScanNode.java index 8ccd7163640a49..1a2ca392972ae4 100644 --- a/fe/fe-core/src/main/java/org/apache/doris/planner/OlapScanNode.java +++ b/fe/fe-core/src/main/java/org/apache/doris/planner/OlapScanNode.java @@ -1167,7 +1167,11 @@ protected void toThrift(TPlanNode msg) { if (annSortLimit != -1) { msg.olap_scan_node.setAnnSortLimit(annSortLimit); } - msg.olap_scan_node.setKeyType(olapTable.getKeysType().toThrift()); + if (selectedIndexId != -1) { + msg.olap_scan_node.setKeyType(olapTable.getIndexMetaByIndexId(selectedIndexId).getKeysType().toThrift()); + } else { + msg.olap_scan_node.setKeyType(olapTable.getKeysType().toThrift()); + } String tableName = olapTable.getName(); if (selectedIndexId != -1) { tableName = tableName + "(" + getSelectedIndexName() + ")"; diff --git a/regression-test/suites/rollup_p0/test_materialized_view_common_expr_push_down.groovy b/regression-test/suites/rollup_p0/test_materialized_view_common_expr_push_down.groovy new file mode 100644 index 00000000000000..19f91a25a8f9c8 --- /dev/null +++ b/regression-test/suites/rollup_p0/test_materialized_view_common_expr_push_down.groovy @@ -0,0 +1,91 @@ +// 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. + +suite("test_materialized_view_common_expr_push_down") { + def baseTable = "test_base_tbl" + def mvTable = "test_mv" + + def getJobState = { tableName -> + def jobStateResult = sql """ SHOW ALTER TABLE MATERIALIZED VIEW WHERE TableName='${tableName}' ORDER BY CreateTime DESC LIMIT 1; """ + return jobStateResult[0][8] + } + + def query_mv_index_type = { indexName -> + def res = sql_return_maparray "SHOW TABLETS FROM ${dbName}.${tableName}" + def tabletId = res[0].TabletId + res = sql_return_maparray "SHOW TABLET ${tabletId}" + def dbId = res[0].DbId + def tableId = res[0].TableId + res = sql_return_maparray """ SHOW PROC "/dbs/${dbId}/${tableId}/indexes" """ + for (def record in res) { + if (record.KeyName == indexName) { + return record.IndexId + } + } + throw new Exception("index ${indexName} is not exists") + } + + sql """set enable_common_expr_pushdown = true""" + + sql "DROP TABLE IF EXISTS ${baseTable}" + sql """ + CREATE TABLE `${baseTable}` ( + `companyId` bigint NULL, + `jobId` bigint NULL, + `province` text NULL, + `vCallerId` bigint NULL DEFAULT "0", + `aCallerId` bigint NULL DEFAULT "-1" + ) ENGINE=OLAP + DUPLICATE KEY(`companyId`) + DISTRIBUTED BY HASH(`companyId`) BUCKETS 8 + PROPERTIES ( + "replication_allocation" = "tag.location.default: 1" + ); + """ + sql """ + CREATE materialized VIEW ${mvTable} AS SELECT + companyId as company_id, + jobId as job_id, + province as p, + vCallerId as v_caller_id, + aCallerId as a_caller_id + FROM ${baseTable} + GROUP BY company_id, job_id, p, v_caller_id, a_caller_id; + """ + int max_try_secs = 60 + while (max_try_secs--) { + String res = getJobState(baseTable) + if (res == "FINISHED" || res == "CANCELLED") { + assertEquals("FINISHED", res) + sleep(3000) + break + } else { + Thread.sleep(2000) + if (max_try_secs < 1) { + println "test timeout," + "state:" + res + assertEquals("FINISHED",res) + } + } + } + + sql "insert into ${baseTable} values(100,100,'北京',3,3)" + sql "insert into ${baseTable} values(100,100,'广东',3,3)" + + qt_sql """ select company_id from ${baseTable} index ${mvTable} where v_caller_id = 3 and if(`a_caller_id` is null,-1, `a_caller_id`) = 3 """ + + sql "DROP TABLE ${baseTable} FORCE;" +} From 504f1d4ea351b6cf442d0f4f816f7d4348fc6d3f Mon Sep 17 00:00:00 2001 From: lambxu Date: Fri, 14 Nov 2025 18:37:57 +0800 Subject: [PATCH 2/3] save --- .../test_materialized_view_common_expr_push_down.out | 5 +++++ 1 file changed, 5 insertions(+) create mode 100644 regression-test/data/rollup_p0/test_materialized_view_common_expr_push_down.out diff --git a/regression-test/data/rollup_p0/test_materialized_view_common_expr_push_down.out b/regression-test/data/rollup_p0/test_materialized_view_common_expr_push_down.out new file mode 100644 index 00000000000000..891d84e9ae60e9 --- /dev/null +++ b/regression-test/data/rollup_p0/test_materialized_view_common_expr_push_down.out @@ -0,0 +1,5 @@ +-- This file is automatically generated. You should know what you did if you want to edit this +-- !sql -- +100 +100 + From 16284569de4bd3944724124cca86d767544d2088 Mon Sep 17 00:00:00 2001 From: lambxu Date: Fri, 14 Nov 2025 19:15:50 +0800 Subject: [PATCH 3/3] save --- ...materialized_view_common_expr_push_down.groovy | 15 --------------- 1 file changed, 15 deletions(-) diff --git a/regression-test/suites/rollup_p0/test_materialized_view_common_expr_push_down.groovy b/regression-test/suites/rollup_p0/test_materialized_view_common_expr_push_down.groovy index 19f91a25a8f9c8..31359db5fdd903 100644 --- a/regression-test/suites/rollup_p0/test_materialized_view_common_expr_push_down.groovy +++ b/regression-test/suites/rollup_p0/test_materialized_view_common_expr_push_down.groovy @@ -24,21 +24,6 @@ suite("test_materialized_view_common_expr_push_down") { return jobStateResult[0][8] } - def query_mv_index_type = { indexName -> - def res = sql_return_maparray "SHOW TABLETS FROM ${dbName}.${tableName}" - def tabletId = res[0].TabletId - res = sql_return_maparray "SHOW TABLET ${tabletId}" - def dbId = res[0].DbId - def tableId = res[0].TableId - res = sql_return_maparray """ SHOW PROC "/dbs/${dbId}/${tableId}/indexes" """ - for (def record in res) { - if (record.KeyName == indexName) { - return record.IndexId - } - } - throw new Exception("index ${indexName} is not exists") - } - sql """set enable_common_expr_pushdown = true""" sql "DROP TABLE IF EXISTS ${baseTable}"