github-actions[bot] commented on code in PR #62628:
URL: https://github.com/apache/doris/pull/62628#discussion_r3109138003
##########
regression-test/suites/nereids_p0/hash_shuffle_key_prune/degrade_fallback.groovy:
##########
@@ -0,0 +1,176 @@
+//
===========================================================================================
+// Category 04: Fallback and Conservative Logic — R01-R02, SK01, N01-N02,
E01-E02
+//
===========================================================================================
+//
+// Purpose
+// Validate fallback (no optimization) behavior of Shuffle Key Pruning in
special scenarios:
+// 1. ROLLUP/GROUPING SETS -> the Repeat operator produces multiple group-by
key sets -> no pruning
+// 2. All candidate columns are skewed -> no safe column can be chosen -> no
pruning
+// 3. Missing/partial statistics -> conservative fallback -> no pruning
+// 4. String fallback: when step 1 cannot pick a single key, step 2 removes
string-typed columns
+//
+// Case List
+// R01: GROUP BY ROLLUP(b,c,d,e,f) -> Repeat operator -> no pruning
(negative)
+// R02: GROUP BY GROUPING SETS -> same behavior -> no pruning (negative)
+// SK01: k1~k5 all have hot_values=0.8 -> all skewed -> no pruning (negative)
+// N01: Some partitions of a partitioned table have no stats -> conservative
-> no pruning (negative)
+// N02: Only column b is analyzed -> other columns miss stats -> no pruning
(negative)
+// E01: All non-string columns are skewed / low-NDV -> step 2 removes
strings and keeps a 3-column composite key (positive)
+// E02: Only string columns remain -> no usable key after removing strings
-> no pruning (negative)
+//
+// Validation
+// Check whether the plan changes and verify result consistency.
+//
===========================================================================================
+suite("degrade_fallback", "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 R = "480000"
+ def nullSkew = "48000"
+ 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();
if(r.length()==0){ids<<[];continue}
+
ids<<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 norm = { rows ->
rows.collect{r->r.collect{v->v==null?"NULL":v.toString()}.join("||")}.sort() }
+
+ def runEx = { String id,String q,int ap,boolean single,boolean
reqShuffle,Boolean expChg,String sel,def exc ->
+ sql "set agg_phase=${ap};"; sql "set disable_nereids_rules='';"
+ sql "set enable_shuffle_key_prune=false;"
+ def eOff=(sql "explain physical plan "+q).toString(); def
oS=extractSigns(eOff)
+ sql "set enable_shuffle_key_prune=true;"
+ def eOn=(sql "explain physical plan "+q).toString(); def
nS=extractSigns(eOn)
+ def si=[]; extractIdLists(eOn).findAll{it.size()==1}.each{si<<it[0]}
+ def shouldChg = (expChg==null ? single : expChg)
+ if(shouldChg){assertTrue(oS.toString()!=nS.toString(),"${id}: signs
should change"); if(single)assertTrue(si.size()>0,"${id}: expect single")}
+ else{assertTrue(oS.toString()==nS.toString(),"${id}: should stay
unchanged"); if(reqShuffle)assertTrue(nS.size()>0,"${id}: need shuffle")}
+ 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")
+ logger.info("${id}: oS=${oS}, nS=${nS}")
+ }
+
+ def runAdvanced = { String id,String q,boolean chg,List<String>
selCols,List<String> exc,Integer keyCount ->
+ sql "set agg_phase=0;"; sql "set disable_nereids_rules='';"
+ sql "set enable_shuffle_key_prune=false;"
+ def eOff=(sql "explain physical plan "+q).toString(); def
oS=extractSigns(eOff)
+ sql "set enable_shuffle_key_prune=true;"
+ def eOn=(sql "explain physical plan "+q).toString(); def
nS=extractSigns(eOn); def idLists=extractIdLists(eOn)
+ if(chg){assertTrue(oS.toString()!=nS.toString(),"${id}: signs should
change")}
+ else{assertTrue(oS.toString()==nS.toString(),"${id}: should stay
unchanged")}
+ if(keyCount!=null){def counts=idLists.collect{it.size()};
assertTrue(counts.any{it==keyCount},"${id}: expect keyCount ${keyCount}")}
+ 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")
+ logger.info("${id}: oS=${oS}, nS=${nS}")
+ }
+
+ // ===== R group: repeat -> no optimization =====
+ sql "drop table if exists t_04_repeat;"
+ sql """create table t_04_repeat (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 """insert into t_04_repeat select
+ (bitand(murmur_hash3_32(concat(cast(number as
string),'_x')),2147483647)%4500)*100000+1,
+ (bitand(murmur_hash3_32(concat(cast(number as
string),'_b')),2147483647)%220)*100000+2,
+ (bitand(murmur_hash3_32(concat(cast(number as
string),'_c')),2147483647)%3900)*100000+3,
+ (bitand(murmur_hash3_32(concat(cast(number as
string),'_d')),2147483647)%3600)*100000+4,
+ (bitand(murmur_hash3_32(concat(cast(number as
string),'_e')),2147483647)%4000)*100000+5,
+ (bitand(murmur_hash3_32(concat(cast(number as
string),'_f')),2147483647)%3400)*100000+6,
+ (bitand(murmur_hash3_32(concat(cast(number as
string),'_g')),2147483647)%120)*100000+7,
+ number from numbers("number"="2000");"""
+ sql "analyze table t_04_repeat with sync;"
Review Comment:
The negative cases that rely on `ANALYZE` here are not isolating the
intended guard. With only ~2000 analyzed rows,
`StatsCalculator.estimateGroupByRowCount()` is capped by child row count, so
the combined group-by NDV can never exceed the 4096 prune threshold. That means
`R01`/`R02` already cannot prune even without the Repeat guard, and the same
issue also applies later to `N01`/`N02` after analyzing just 1500 rows. These
cases need injected row_count/ndv above the threshold, otherwise they do not
actually validate the fallback reason they describe.
##########
regression-test/suites/nereids_p0/hash_shuffle_key_prune/hotvalue_boundary.groovy:
##########
@@ -0,0 +1,230 @@
+//
===========================================================================================
+// Category 01: Hot-Value Thresholds and Boundary Cases — A01, S01, S02, FC1,
B01-B04
+//
===========================================================================================
+//
+// Purpose
+// Validate how Agg Shuffle Key Pruning handles hot_values:
+// 1. hot_values='' (empty string) -> means "sampled but no hot values" ->
isBalanced()=true -> can be chosen as a shuffle key
+// 2. hot_values=null -> means "not sampled" -> conservative fallback, no
pruning
+// 3. hot_values='value :ratio' -> ratio >= 5% -> isBalanced()=false -> skip
the column (treat it as skewed)
+// 4. null ratio >= 5% -> isBalanced()=false -> skip the column (treat it as
skewed)
+// 5. max(null_ratio, hot_ratio) >= 5% -> overall considered skewed
+//
+// Test Case List
+// A01: hot_key with 30% skew + good_key without hot values -> good_key is
selected (positive)
+// S01: hot_values='' empty string -> treated as sampled without hot values
-> pruning works (positive)
+// S02: hot_values=null -> treated as not sampled -> conservative fallback,
no pruning (negative)
+// FC1: full analyze produces hot_values=null -> no pruning (negative)
+// B01: hot_ratio=4.9% (just below the 5% threshold) -> column is kept and
selected as the shuffle key
+// B02: hot_ratio=5.0% (equal to the threshold) -> column is skipped
+// B03: null ratio 10% -> treated as skewed -> column is skipped
+// B04: max(null ratio 10%, hot_ratio 4.9%) = 10% -> treated as skewed ->
column is skipped
+//
+// Validation
+// For each case, compare the orderedShuffledColumns change in explain
physical plan between OFF and ON,
+// and verify that OFF/ON execution results are identical.
+//
===========================================================================================
+suite("hotvalue_boundary", "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;"""
+
+ def dbName = context.config.getDbNameByFile(context.file)
+ sql "create database if not exists ${dbName};"
+ sql "use ${dbName};"
+
+ // Fix the number of pipeline tasks to 8 so NDV-threshold calculation
stays deterministic
+ 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;"
+
+ // ======== Shared helper functions ========
+
+ // R: manually injected row_count (480k rows, far above the NDV threshold,
so the optimizer sees shuffle as necessary)
+ def R = "480000"
+
+ // esc: escape backslashes and single quotes in strings for SQL
construction
+ def esc = { Object v ->
v==null?"null":v.toString().replace("\\","\\\\").replace("'","\\'") }
+
+ // ss: manually inject statistics for one column (alter table ... set
stats)
+ // t: table name, c: column name, n: NDV, nn: null count, ds: data_size
+ // mi/ma: min/max value, hv: hot_values string
+ // when hv=null, the hot_values field is not injected (to simulate full
analyze)
+ // when hv="", inject hot_values='' (meaning "sampled but no hot values")
+ // when hv="val :ratio", inject the specified hot value
+ 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});"
+ }
+
+ // extractSigns: extract all orderedShuffledColumns lists from explain
output
+ // returns values such as ["[1,2,3]", "[4]"] to compare OFF/ON plan
changes
+ def extractSigns = { String e ->
+ def s=[]; def m=(e=~/orderedShuffledColumns=\[([0-9,\s-]*)\]/);
while(m.find()){s<<"["+m.group(1).replaceAll("\\s+","")+"]"}; s
+ }
+
+ // extractSingle: extract orderedShuffledColumns entries that contain only
one column
+ // returns exprId lists, e.g. [4] means only the column with exprId=4
was selected as the single shuffle key
+ 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
+ }
+
+ // exprId: find the exprId that corresponds to a column name in explain
output (e.g. "good_key#7" -> 7)
+ 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 }
+
+ // norm: normalize query results into comparable string lists (handle
nulls and compare after sorting)
+ def norm = { rows ->
rows.collect{r->r.collect{v->v==null?"NULL":v.toString()}.join("||")}.sort() }
+
+ // showRow: get the show column stats row for a column
+ def showRow = { String t,String c -> def r=sql("show column stats
${t}(${c})"); assertEquals(1,r.size()); r[0] }
+
+ // run: shared test execution helper
+ // id: case id (such as "A01")
+ // q: SQL under test
+ // chg: whether the plan is expected to change (true=positive/pruning
works, false=negative/no pruning)
+ // sel: expected column name selected as the shuffle key (null means do
not check)
+ // exc: list of columns expected to be excluded
+ // single: whether pruning is expected to reduce to a single column
+ def run = { String id,String q,boolean chg,String sel,List<String>
exc,boolean single ->
+ sql "set agg_phase=0;"; sql "set disable_nereids_rules='';"
+ // First disable pruning to get the original plan
+ sql "set enable_shuffle_key_prune=false;"
+ def eOff=(sql "explain physical plan "+q).toString(); def
oS=extractSigns(eOff)
+ // Then enable pruning to get the optimized plan
+ sql "set enable_shuffle_key_prune=true;"
+ def eOn=(sql "explain physical plan "+q).toString(); def
nS=extractSigns(eOn); def si=extractSingle(eOn)
+ // Validate whether the plan changes or not
+ if(chg){assertTrue(oS.toString()!=nS.toString(),"${id}: signs should
change"); if(single)assertTrue(si.size()>0,"${id}: expect single")}
+ else{assertTrue(oS.toString()==nS.toString(),"${id}: should stay
unchanged")}
+ // Validate that the selected shuffle key is correct
+ if(sel!=null){def eid=exprId(eOn,sel);
assertTrue(eid!=null&&si.contains(eid),"${id}: ${sel} not selected")}
+ // Validate that excluded columns do not appear in the shuffle key
+ exc.each{c->def eid=exprId(eOn,c);
if(eid!=null)assertTrue(!si.contains(eid),"${id}: ${c} excluded")}
+ // Validate that OFF/ON query results are identical (functional
correctness)
+ 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")
+ logger.info("${id}: oS=${oS}, nS=${nS}, si=${si}")
+ }
+
+ // ===== Table setup =====
+ // t_01_simple: simple table used for A01/S01/S02
+ // hot_key: the column that will be injected with skew
+ // good_key: a safe column with no hot values, expected to be chosen as
the shuffle key
+ sql "drop table if exists t_01_simple;"
+ sql """create table t_01_simple (x bigint, hot_key int, good_key bigint, v
bigint)
+ duplicate key(x) distributed by hash(x) buckets 4 properties
("replication_num"="1");"""
+ sql """insert into t_01_simple select number*100000+1,
if(number<600,1,cast(number%2000 as int)+2),
+ (number%6000)*100000+7, number from numbers("number"="2000");"""
+
+ // t_01_boundary: boundary-condition table used for B01-B04
+ // hot_below_key: hot_ratio=4.9% (just below the 5% threshold)
+ // hot_boundary_key: hot_ratio=5.0% (equal to the 5% threshold)
+ // hot_above_key: hot_ratio=5.1% (just above the 5% threshold)
+ // null10_key: null ratio=10% (above the 5% threshold)
+ // maxmix_key: null ratio 10% + hot_ratio 4.9%, max=10% (above the
5% threshold)
+ // bi_good: safe bigint column
+ // vc16_good: safe varchar(16) column
+ sql "drop table if exists t_01_boundary;"
+ sql """create table t_01_boundary (x bigint, hot_below_key int,
hot_boundary_key int, hot_above_key int,
+ null10_key int, maxmix_key int, bi_good bigint, vc16_good varchar(16),
v bigint)
+ duplicate key(x) distributed by hash(x) buckets 4 properties
("replication_num"="1");"""
+ sql """insert into t_01_boundary select number*100000+1,
+ cast(number%2000 as int)+2, cast(number%2000 as int)+2,
cast(number%2000 as int)+2,
+ cast(number%2000 as int)+2, cast(number%2000 as int)+2,
+ (number%6000)*100000+7, concat('vc_',lpad(cast(number%7000 as
string),4,'0')),
+ number from numbers("number"="2000");"""
+
+ // ===== A01: hot_key has 30% skew, good_key has no hot values -> good_key
is selected =====
+ // hot_key: NDV=7000, hot_values='1 :0.30' -> value 1 takes 30%,
isBalanced()=false -> skipped
+ // good_key: NDV=9000, hot_values='' -> no hot values, isBalanced()=true
-> selected
+ ss("t_01_simple","x","60000","0","1600000","1","199900001","")
+ ss("t_01_simple","hot_key","7000","0","400000","1","7000","1 :0.30")
+ ss("t_01_simple","good_key","9000","0","800000","7","599900007","")
+ ss("t_01_simple","v","60000","0","1600000","0","1999","")
+
+ run("A01","""select count(hot_key),count(good_key),count(sum_v) from (
+ select hot_key,good_key,sum(v) as sum_v from t_01_simple group by
hot_key,good_key) t""",
+ true,"good_key",["hot_key"],true)
+
+ // ===== S01: hot_values='' -> means "sampled but no hot values" ->
pruning works =====
+ // hot_key: hot='1 :0.10' -> exactly at the threshold -> skewed -> skipped
+ // good_key: hot='' -> sampled without hot values -> balanced -> selected
+ ss("t_01_simple","hot_key","7000","0","400000","1","7000","1 :0.10")
+ ss("t_01_simple","good_key","9000","0","400000","1","9000","")
+ run("S01","""select count(hot_key),count(good_key),count(sum_v) from (
+ select hot_key,good_key,sum(v) as sum_v from t_01_simple group by
hot_key,good_key) t""",
+ true,"good_key",["hot_key"],true)
+
+ // ===== S02: hot_values=null -> means "not sampled" -> conservative
fallback, no pruning =====
+ // good_key receives hot_values=null (do not inject the hot_values field)
-> isBalanced() returns false
+ // so the optimizer cannot confirm that good_key is safe -> no optimization
+ ss("t_01_simple","hot_key","7000","0","400000","1","7000","1 :0.10")
+ ss("t_01_simple","good_key","9000","0","400000","1","9000",null)
+ run("S02","""select count(hot_key),count(good_key),count(sum_v) from (
+ select hot_key,good_key,sum(v) as sum_v from t_01_simple group by
hot_key,good_key) t""",
+ false,null,[],false)
+
+ // ===== FC1: full analyze produces hot_values=null -> no pruning =====
+ // Full analyze does not generate hot_values, so the hot_values field in
show column stats is null
+ // This is the real reproduction of the "S02 scenario": a table after full
analyze cannot be pruned
+ sql "drop table if exists t_01_full;"
+ sql """create table t_01_full (x bigint, skew_key int, good_key int, v
bigint)
+ duplicate key(x) distributed by hash(x) buckets 4 properties
("replication_num"="1");"""
+ sql """insert into t_01_full select number*100000+1,
if(number<200,1,cast(number%2000 as int)+2),
+ cast(number%9000 as int)+1, number from numbers("number"="2000");"""
+ sql "analyze table t_01_full with sync;"
Review Comment:
`FC1` does not actually isolate the `hot_values=null` behavior. After
`ANALYZE`, this table only has 2000 rows, so `good_key` can have NDV at most
2000, which is already below the 4096 balance threshold used by `isBalanced()`.
In other words, pruning is impossible here even if hot values were collected,
so the case cannot prove the claimed "full analyze -> hot_values=null -> no
pruning" regression. This needs either injected stats above the threshold after
analyze, or a much larger analyzed dataset.
--
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]