github-actions[bot] commented on code in PR #62628: URL: https://github.com/apache/doris/pull/62628#discussion_r3121827535
########## regression-test/suites/nereids_p0/hash_shuffle_key_prune/distinct_mixed.groovy: ########## @@ -0,0 +1,260 @@ +// 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. + +// =========================================================================================== +// Category 08: Distinct Partition and Mixed Join — DA01-DA05, M01-M03 +// =========================================================================================== +// +// Purpose +// Validate two kinds of special scenarios: +// A. COUNT(DISTINCT) cases: DISTINCT introduces an extra partition operator, so pruning must optimize +// both the partition and agg shuffle keys +// B. Mixed Join: pruning behavior when Agg hot_values and Join probe-side hot_values=null are mixed +// +// DA Group Case List +// DA01: High NDV + COUNT(DISTINCT g) + agg_phase=2 -> pruning works (positive) +// DA02: Low NDV + COUNT(DISTINCT g) -> NDV is insufficient -> no pruning (negative) +// DA03: Mixed COUNT(DISTINCT g) + AVG(v) aggregation -> pruning still works (positive) +// DA04: agg_phase=4 (2+2 mode) -> pruning still works (positive) +// DA05: count(distinct [skew] g) -> explicit skew-distinct guard -> no pruning (negative) +// +// M Group Case List +// M01: t_08_m_agg (hot_values='') JOIN t_08_m_probe (hot_values=null) +// join on 6 columns = group by 6 columns -> conservative -> plan unchanged (negative) +// M02: join on 3 columns < group by 6 columns, but probe-side hot_values=null -> conservative -> plan unchanged (negative) +// M03: Probe side uses an agg table with hot_values=null -> conservative -> plan unchanged (negative) +// +// Validation +// Both DA and M groups use hard assertions. +// Every case verifies that OFF/ON results are identical. +// =========================================================================================== +suite("distinct_mixed", "agg_shuffle_prune_func") { + sql """set enable_nereids_planner=true;""" + sql """set enable_fallback_to_original_planner=false;""" + sql """set enable_sql_cache=false;""" + sql """set enable_query_cache=false;""" + sql """set agg_phase=0;""" + + def dbName = context.config.getDbNameByFile(context.file) + sql "create database if not exists ${dbName};" + sql "use ${dbName};" + + def tptn = 8 + sql "set parallel_fragment_exec_instance_num=1;" + sql "set parallel_pipeline_task_num=${tptn};" + sql "set be_number_for_test=1;" + + def qn = (tptn*512 + tptn*64).toString() + def R = "480000" + def lowNdv = "4" + def daQn = "60000" + def esc = { Object v -> v==null?"null":v.toString().replace("\\","\\\\").replace("'","\\'") } + def ss = { String t,String c,String n,String nn,String ds,String mi,String ma,String hv -> + def hvc = hv==null ? "" : ", 'hot_values'='${esc(hv)}'" + sql "alter table ${t} modify column ${c} set stats ('row_count'='${R}','ndv'='${esc(n)}','num_nulls'='${esc(nn)}','data_size'='${esc(ds)}','min_value'='${esc(mi)}','max_value'='${esc(ma)}'${hvc});" + } + def extractSigns = { String e -> + def s=[]; def m=(e=~/orderedShuffledColumns=\[([0-9,\s-]*)\]/); while(m.find()){s<<"["+m.group(1).replaceAll("\\s+","")+"]"}; s + } + def extractIdLists = { String e -> + def ids=[]; def m=(e=~/orderedShuffledColumns=\[([0-9,\s-]*)\]/) + while(m.find()){ + def r=m.group(1).trim() + ids << (r.length()==0 ? [] : r.split(",").collect{it.trim()}.findAll{it.length()>0}.collect{Integer.parseInt(it)}) + } + ids + } + def parseOrderedIds = { String line -> + def m=(line=~/orderedShuffledColumns=\[([0-9,\s-]*)\]/) + if (!m.find()) { + return null + } + def r=m.group(1).trim() + return (r.length()==0 ? [] : r.split(",").collect{it.trim()}.findAll{it.length()>0}.collect{Integer.parseInt(it)}) + } + def subtreeText = { List<String> lines, int rootIndex -> + int rootIndent = lines[rootIndex].indexOf("Physical") + def collected = [] + for (int i = rootIndex; i < lines.size(); i++) { + def line = lines[i] + int indent = line.indexOf("Physical") + if (i > rootIndex && indent >= 0 && indent <= rootIndent) { + break + } + collected << line + } + collected.join("\n") + } + def exprId = { String e,String c -> def m=(e=~("\\b"+java.util.regex.Pattern.quote(c)+"#(\\d+)\\b")); m.find()?Integer.parseInt(m.group(1)):null } + def findScopedExprId = { String e,String c -> def m=(e=~("\\b"+java.util.regex.Pattern.quote(c)+"#(\\d+)\\b")); m.find()?Integer.parseInt(m.group(1)):null } + def norm = { rows -> rows.collect{r->r.collect{v->v==null?"NULL":v.toString()}.join("||")}.sort() } + def explainText = { String q -> (sql "explain physical plan " + q).collect { it[0].toString() }.join("\n") } + def findDistinctAggShuffleCtx = { String e -> + def lines = e.readLines() + int rootIndex = -1 + for (int i = 0; i < lines.size(); i++) { + if (lines[i].contains("PhysicalHashAggregate") && lines[i].contains("aggPhase=DISTINCT_GLOBAL")) { + rootIndex = i + break + } + } + if (rootIndex < 0) { + for (int i = 0; i < lines.size(); i++) { + if (lines[i].contains("PhysicalHashAggregate") && lines[i].contains("aggPhase=GLOBAL")) { + rootIndex = i + break + } + } + } + if (rootIndex < 0) { + return null + } + int rootIndent = lines[rootIndex].indexOf("PhysicalHashAggregate") + def ctxs = [] + for (int i = rootIndex + 1; i < lines.size(); i++) { + def line = lines[i] + int indent = line.indexOf("Physical") + if (indent < 0) { + continue + } + if (indent <= rootIndent) { + break + } + if (line.contains("PhysicalDistribute") && line.contains("orderedShuffledColumns=")) { + ctxs << [ids: parseOrderedIds(line), subtree: subtreeText(lines, i)] + } + } + [subtree: subtreeText(lines, rootIndex), ctxs: ctxs] + } + + // DA-group test helper + def runDA = { String id,String q,boolean expChg,int ap -> + sql "set agg_phase=${ap};"; sql "set disable_nereids_rules='';" + sql "set enable_shuffle_key_prune=false;" + def eOff=explainText(q); def oS=extractSigns(eOff); def offCtx=findDistinctAggShuffleCtx(eOff) + sql "set enable_shuffle_key_prune=true;" + def eOn=explainText(q); def nS=extractSigns(eOn); def onCtx=findDistinctAggShuffleCtx(eOn) + def chg=(eOff!=eOn) + logger.info("${id}: oS=${oS}, nS=${nS}, offCtx=${offCtx}, onCtx=${onCtx}, changed=${chg}") + if(expChg){ + assertTrue(chg,"${id}: signs should change") + assertTrue(offCtx!=null && onCtx!=null, "${id}: missing DISTINCT/global agg subtree") + assertTrue(!offCtx.ctxs.isEmpty() && offCtx.ctxs.size()==onCtx.ctxs.size(), + "${id}: expected matching DISTINCT/global distribute lists, got off=${offCtx}, on=${onCtx}") + def target = findScopedExprId(onCtx.subtree, "a") + assertTrue(target!=null, "${id}: missing exprId for a") + assertTrue(offCtx.ctxs.any{it.ids.size()>1 && it.ids.contains(target)}, + "${id}: expected at least one multi-key DISTINCT/global shuffle containing a/${target} before pruning, got ${offCtx.ctxs}") + assertTrue(onCtx.ctxs.every{it.ids.size()==1 && it.ids[0]==target}, + "${id}: expected all DISTINCT/global shuffles to prune to [a]/${target}, got ${onCtx.ctxs}") + } else { + assertTrue(oS.toString()==nS.toString(),"${id}: should stay unchanged") + assertTrue(offCtx?.ctxs?.collect{it.ids} == onCtx?.ctxs?.collect{it.ids}, + "${id}: DISTINCT/global shuffle lists should stay unchanged, off=${offCtx}, on=${onCtx}") + } + sql "set enable_shuffle_key_prune=false;"; def rOff=sql q + sql "set enable_shuffle_key_prune=true;"; def rOn=sql q + assertTrue(norm(rOff)==norm(rOn),"${id}: results differ") + } + + // ===== DA group table setup ===== + sql "drop table if exists t_08_da;" + sql """create table t_08_da (id bigint,a bigint,b bigint,c bigint,d bigint,e bigint,f bigint,g bigint,v bigint) + duplicate key(id) distributed by hash(id) buckets 4 properties ("replication_num"="1");""" + sql """insert into t_08_da select number,number%5000,number%4800,number%4600, + number%4400,number%4200,number%4000,number%3800,number from numbers("number"="2000");""" + + def daBase = { String ndv -> + ss("t_08_da","id","60000","0","1600000","0","1999","") + ["a","b","c","d","e","f","g","v"].each{c-> ss("t_08_da",c,ndv,"0","1600000","0","9999","")} + } + + // DA01: high NDV -> prune + daBase(daQn) + runDA("DA01","select a,b,c,d,e,f,count(distinct g) from t_08_da group by a,b,c,d,e,f",true,2) + + // DA02: low NDV -> no pruning + daBase(lowNdv) + runDA("DA02","select a,b,c,d,e,f,count(distinct g) from t_08_da group by a,b,c,d,e,f",false,2) + + // DA03: distinct + avg mixed -> prune + daBase(daQn) + runDA("DA03","select a,b,c,d,e,f,count(distinct g),avg(v) from t_08_da group by a,b,c,d,e,f",true,2) + + // DA04: agg_phase=4 (2+2) + daBase(daQn) + runDA("DA04","select a,b,c,d,e,f,count(distinct g) from t_08_da group by a,b,c,d,e,f",true,4) + + // DA05: distinct [skew] hint → should skip shuffle key prune + daBase(daQn) + runDA("DA05","select a,count(distinct [skew] g) from t_08_da group by a",false,2) + + // Reset agg_phase so the M group stays on the suite default path. + sql "set agg_phase=0;" + + // ===== M group table setup ===== + sql "drop table if exists t_08_m_agg;" + sql "drop table if exists t_08_m_probe;" + sql """create table t_08_m_agg (x bigint,b bigint,c bigint,d bigint,e bigint,f bigint,g bigint,v bigint) + duplicate key(x) distributed by hash(x) buckets 4 properties ("replication_num"="1");""" + sql """create table t_08_m_probe (b bigint,c bigint,d bigint,e bigint,f bigint,g bigint,v bigint) + duplicate key(b) distributed by hash(c) buckets 4 properties ("replication_num"="1");""" + sql """insert into t_08_m_agg select number,number%50,number%800,number%700,number%600,number%500,number%400,number from numbers("number"="1000");""" + sql "insert into t_08_m_probe select b,c,d,e,f,g,v from t_08_m_agg;" + + // t_08_m_agg: sample semantics -> hot_values='' + ss("t_08_m_agg","x","1000","0","800000","0","999","") + ss("t_08_m_agg","b","50","0","800000","0","49","") + ss("t_08_m_agg","c","10000","0","800000","0","799","") + ss("t_08_m_agg","d","10000","0","800000","0","699","") + ss("t_08_m_agg","e","10000","0","800000","0","599","") + ss("t_08_m_agg","f","500","0","800000","0","499","") + ss("t_08_m_agg","g","400","0","800000","0","399","") + ss("t_08_m_agg","v","1000","0","800000","0","999","") + // t_08_m_probe: full semantics -> hot_values=null (conservative) + ss("t_08_m_probe","b","50","0","800000","0","49",null) + ss("t_08_m_probe","c","800","0","800000","0","799",null) + ss("t_08_m_probe","d","700","0","800000","0","699",null) + ss("t_08_m_probe","e","600","0","800000","0","599",null) + ss("t_08_m_probe","f","500","0","800000","0","499",null) + ss("t_08_m_probe","g","400","0","800000","0","399",null) + ss("t_08_m_probe","v","1000","0","800000","0","999",null) + + // M01: join on 6 = gby 6 (hot_values=null) -> conservative + sql "set enable_shuffle_key_prune=false;" + def offM01=(sql "explain physical plan select s.b,s.c,s.v,a.sv from t_08_m_probe s join (select b,c,d,e,f,g,sum(v) as sv from t_08_m_agg group by b,c,d,e,f,g) a on s.b=a.b and s.c=a.c and s.d=a.d and s.e=a.e and s.f=a.f and s.g=a.g").toString() Review Comment: These M01-M03 cases only compare the `EXPLAIN` text. Unlike the DA cases, they never execute the query with `enable_shuffle_key_prune` OFF/ON and compare normalized results, so a correctness regression in the mixed `hot_values=''` / `hot_values=null` path would still pass here. Since this PR is test-only and the suite header says every case verifies result equality, please run each query in both modes and assert the results match. ########## regression-test/suites/nereids_p0/hash_shuffle_key_prune/window_distinct_regression.groovy: ########## @@ -0,0 +1,296 @@ +// 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. + +// =========================================================================================== +// Category 09: Window / Distinct Regression — WD01-WD04 +// =========================================================================================== +// +// Purpose +// Add a more focused regression set for window / distinct patterns: +// - Window guard: ensure pruning is not triggered incorrectly under window wrappers +// - Distinct + Window: ensure DISTINCT aggregation keeps the correct prune / no-prune behavior when wrapped by a window +// +// Case List +// WD01: window over inner agg, child distribution already satisfied -> no pruning (negative) +// WD02: count(distinct) + window, high NDV -> pruning works (positive) +// WD03: count(distinct) + avg + window, high NDV -> pruning still works (positive) +// WD04: count(distinct) + window, low NDV -> no pruning (negative) +// =========================================================================================== +suite("window_distinct_regression", "agg_shuffle_prune_func") { + sql """set enable_nereids_planner=true;""" + sql """set enable_fallback_to_original_planner=false;""" + sql """set enable_sql_cache=false;""" + sql """set enable_query_cache=false;""" + sql """set agg_phase=0;""" + sql """set agg_shuffle_use_parent_key=false;""" + sql """set detail_shape_nodes='PhysicalHashAggregate,PhysicalDistribute,PhysicalWindow';""" + + def dbName = context.config.getDbNameByFile(context.file) + sql "create database if not exists ${dbName};" + sql "use ${dbName};" + + def tptn = 8 + sql "set parallel_fragment_exec_instance_num=1;" + sql "set parallel_pipeline_task_num=${tptn};" + sql "set be_number_for_test=1;" + + def highNdv = (tptn * 512 + tptn * 64).toString() + def lowNdv = "4" + def rowCount = "480000" + def esc = { Object v -> v == null ? "null" : v.toString().replace("\\", "\\\\").replace("'", "\\'") } + def ss = { String t, String c, String n, String nn, String ds, String mi, String ma, String hv -> + def hvc = hv == null ? "" : ", 'hot_values'='${esc(hv)}'" + sql "alter table ${t} modify column ${c} set stats ('row_count'='${rowCount}','ndv'='${esc(n)}','num_nulls'='${esc(nn)}','data_size'='${esc(ds)}','min_value'='${esc(mi)}','max_value'='${esc(ma)}'${hvc});" + } + def extractSigns = { String e -> + def s = [] + def m = (e =~ /orderedShuffledColumns=\[([0-9,\s-]*)\]/) + while (m.find()) { + s << "[" + m.group(1).replaceAll("\\s+", "") + "]" + } + s + } + def extractSingle = { String e -> + def ids = [] + def m = (e =~ /orderedShuffledColumns=\[([0-9,\s-]*)\]/) + while (m.find()) { + def r = m.group(1).trim() + if (r.length() == 0) continue + def p = r.split(",").collect { it.trim() }.findAll { it.length() > 0 } + if (p.size() == 1) ids << Integer.parseInt(p[0]) + } + ids + } + def extractIdLists = { String e -> + def ids = [] + def m = (e =~ /orderedShuffledColumns=\[([0-9,\s-]*)\]/) + while (m.find()) { + def r = m.group(1).trim() + ids << (r.length() == 0 ? [] : r.split(",").collect { it.trim() }.findAll { it.length() > 0 }.collect { Integer.parseInt(it) }) + } + ids + } + def exprId = { String e, String c -> + def m = (e =~ ("\\b" + java.util.regex.Pattern.quote(c) + "#(\\d+)\\b")) + m.find() ? Integer.parseInt(m.group(1)) : null + } + def subtreeText = { List<String> lines, int rootIndex -> + int rootIndent = lines[rootIndex].indexOf("Physical") + def collected = [] + for (int i = rootIndex; i < lines.size(); i++) { + def line = lines[i] + int indent = line.indexOf("Physical") + if (i > rootIndex && indent >= 0 && indent <= rootIndent) { + break + } + collected << line + } + collected.join("\n") + } + def parseOrderedIds = { String line -> + def m = (line =~ /orderedShuffledColumns=\[([0-9,\s-]*)\]/) + if (!m.find()) { + return null + } + def r = m.group(1).trim() + return (r.length() == 0 ? [] : r.split(",").collect { it.trim() }.findAll { it.length() > 0 }.collect { Integer.parseInt(it) }) + } + def findDistinctAggShuffleCtx = { String e -> + def lines = e.readLines() + int rootIndex = -1 + for (int i = 0; i < lines.size(); i++) { + if (lines[i].contains("PhysicalHashAggregate") && lines[i].contains("aggPhase=DISTINCT_GLOBAL")) { + rootIndex = i + break + } + } + if (rootIndex < 0) { + for (int i = 0; i < lines.size(); i++) { + if (lines[i].contains("PhysicalHashAggregate") && lines[i].contains("aggPhase=GLOBAL")) { + rootIndex = i + break + } + } + } + if (rootIndex < 0) { + return null + } + int rootIndent = lines[rootIndex].indexOf("PhysicalHashAggregate") + def ctxs = [] + for (int i = rootIndex + 1; i < lines.size(); i++) { + def line = lines[i] + int indent = line.indexOf("Physical") + if (indent < 0) { + continue + } + if (indent <= rootIndent) { + break + } + if (line.contains("PhysicalDistribute") && line.contains("orderedShuffledColumns=")) { + ctxs << [ids: parseOrderedIds(line), subtree: subtreeText(lines, i)] + } + } + [subtree: subtreeText(lines, rootIndex), ctxs: ctxs] + } + def findScopedExprId = { String e, String c -> + def m = (e =~ ("\\b" + java.util.regex.Pattern.quote(c) + "#(\\d+)\\b")) + m.find() ? Integer.parseInt(m.group(1)) : null + } + def assertDistinctShufflePrunedToSingle = { String id, String eOff, String eOn, String c -> + def offCtx = findDistinctAggShuffleCtx(eOff) + def onCtx = findDistinctAggShuffleCtx(eOn) + assertTrue(offCtx != null && onCtx != null, "${id}: missing DISTINCT/global agg subtree") + assertTrue(!offCtx.ctxs.isEmpty() && !onCtx.ctxs.isEmpty(), "${id}: missing DISTINCT/global distribute ids, off=${offCtx}, on=${onCtx}") + def offTarget = offCtx.ctxs.last().ids Review Comment: `findDistinctAggShuffleCtx()` collects every `PhysicalDistribute` under the DISTINCT/GLOBAL subtree, but this helper only validates `ctxs.last()`. In the distinct paths we already have multiple distribute nodes (`distinct_mixed.runDA()` checks all of them), so WD02/WD03 can still pass if an earlier distribute stops pruning under the window wrapper. Please validate the full `ctxs` list here instead of only the last entry. -- 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. To unsubscribe, e-mail: [email protected] For queries about this service, please contact Infrastructure at: [email protected] --------------------------------------------------------------------- To unsubscribe, e-mail: [email protected] For additional commands, e-mail: [email protected]
