github-actions[bot] commented on code in PR #62628: URL: https://github.com/apache/doris/pull/62628#discussion_r3115198204
########## regression-test/suites/nereids_p0/hash_shuffle_key_prune/join_key_prune.groovy: ########## @@ -0,0 +1,211 @@ +// 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 07: Pure Join Shuffle Key Optimization — JK01-JK08 +// =========================================================================================== +// +// Purpose +// Validate pure Join shuffle-key optimization outside Agg scenarios. +// When two tables use Shuffle Hash Join and the join key contains multiple columns, +// pruning can reduce the number of columns participating in hash computation and lower shuffle cost. +// +// Note +// Join shuffle-key pruning is cost-based, but we keep the environment deterministic with fixed statistics +// and be_number_for_test=1, then use hard assertions for validation. +// +// Case List +// JK01: All join keys on both sides have high NDV + no skew -> can be pruned (positive) +// JK02: All join keys are skewed, but step 2 still removes the string key when numeric combined NDV is high -> pruning works (positive) +// JK03: Numeric columns are skewed while varchar is balanced -> single-key selection can still prune on varchar (positive) +// JK04: Left table lacks statistics -> conservative -> no pruning (negative) +// JK05: Left table has hot_values=null -> not sampled -> conservative -> no pruning (negative) +// JK06: Verify the switch works -> OFF keeps the full join shuffle key, ON can prune +// JK07: join [shuffle[skew(...)] ] hint -> explicit skew guard -> no pruning (negative) +// JK08: All keys are skewed and numeric combined NDV is too low for step 2 -> no pruning (negative) +// +// Validation +// Fix join reorder and turn off runtime filters to keep the plan stable. +// Compare OFF/ON plan changes and result consistency. +// =========================================================================================== +suite("join_key_prune", "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 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 extractKeyCounts = { String e -> + def c=[]; def m=(e=~/orderedShuffledColumns=\[([0-9,\s-]*)\]/) + while(m.find()){ + def r=m.group(1).trim() + def p=r.length()==0 ? [] : r.split(",").collect{it.trim()}.findAll{it.length()>0} + c<<p.size() + } + c + } + def norm = { rows -> rows.collect{r->r.collect{v->v==null?"NULL":v.toString()}.join("||")}.sort() } + + // Join shuffle-key prune test helper + def runJoin = { String id,String q,boolean expChg -> Review Comment: `runJoin()` only checks that the overall plan text changed. For positive cases that means any join-side rewrite will pass, even if the wrong key subset is chosen. The FE unit test already extracts the left/right `DistributionSpecHash` under `PhysicalHashJoin`; this suite should mirror that and assert the actual ordered keys for both sides instead of only `eOff != eOn`. ########## regression-test/suites/nereids_p0/hash_shuffle_key_prune/window_distinct_regression.groovy: ########## @@ -0,0 +1,286 @@ +// 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;""" + + 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 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 findAggShuffleCtx = { String e, String c -> + def lines = e.readLines() + int rootIndex = -1 + for (int i = 0; i < lines.size(); i++) { + if (lines[i].contains("PhysicalHashAggregate") && lines[i].contains("groupByExpr=[") + && lines[i].contains("${c}#")) { + 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 + } + def m = (line =~ /orderedShuffledColumns=\[([0-9,\s-]*)\]/) + if (line.contains("PhysicalDistribute") && m.find()) { + def r = m.group(1).trim() + ctxs << (r.length() == 0 ? [] : r.split(",").collect { it.trim() }.findAll { it.length() > 0 }.collect { Integer.parseInt(it) }) + } + } + [subtree: subtreeText(lines, rootIndex), ids: ctxs] + } + def assertDistinctShufflePrunedToSingle = { String id, String eOff, String eOn, String c -> + def offCtx = findAggShuffleCtx(eOff, c) + def onCtx = findAggShuffleCtx(eOn, c) + assertTrue(offCtx != null && onCtx != null, "${id}: missing agg subtree for ${c}") + def offIds = offCtx.ids + def onIds = onCtx.ids + def target = findScopedExprId(onCtx.subtree, c) + assertTrue(target != null, "${id}: missing exprId for ${c}") + def offSingleCount = offIds.count { it.size() == 1 && it[0] == target } + def onSingleCount = onIds.count { it.size() == 1 && it[0] == target } + def offMultiTarget = offIds.findAll { it.size() > 1 && it.contains(target) } + def onMultiTarget = onIds.findAll { it.size() > 1 && it.contains(target) } + assertTrue(!offMultiTarget.isEmpty(), + "${id}: expected at least one multi-key shuffle containing ${c}/${target} before pruning, got ${offIds}") + assertTrue(onSingleCount > offSingleCount, Review Comment: This helper is still not anchored to the exact `DISTINCT_GLOBAL` / target agg child exchange. It counts every `PhysicalDistribute` under the selected agg subtree, so a lower-phase rewrite can satisfy these assertions while one of the target distribute nodes stays unchanged. Please inspect the specific child `PhysicalDistribute` of the target agg, like `ShuffleKeyPrunerTest.extractDistinctGlobalChildDistributeKeySize()`, instead of using aggregate counts across the whole subtree. -- 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]
