Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -502,6 +502,12 @@ private Optional<String> checkNdvValidation(OlapScan olapScan, double rowCount)
return Optional.empty();
}

private boolean isRegisteredRowCount(OlapScan olapScan) {
AnalysisManager analysisManager = Env.getCurrentEnv().getAnalysisManager();
TableStatsMeta tableMeta = analysisManager.findTableStatsStatus(olapScan.getTable().getId());
return tableMeta != null && tableMeta.userInjected;
}

private Statistics computeOlapScan(OlapScan olapScan) {
OlapTable olapTable = olapScan.getTable();
double tableRowCount = getOlapTableRowCount(olapScan);
Expand All @@ -520,6 +526,12 @@ private Statistics computeOlapScan(OlapScan olapScan) {
if (selectedPartitionsRowCount == -1) {
selectedPartitionsRowCount = tableRowCount;
}
if (isRegisteredRowCount(olapScan)) {
// If a row count is injected for the materialized view, use it to fix the issue where
// the materialized view cannot be selected by cbo stable due to selectedPartitionsRowCount being 0,
// which is caused by delayed statistics reporting.
selectedPartitionsRowCount = tableRowCount;
}
// if estimated mv rowCount is more than actual row count, fall back to base table stats
if (selectedPartitionsRowCount >= optStats.get().getRowCount()) {
Statistics derivedStats = optStats.get();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,8 @@ suite ("multiple_no_where") {
qt_select_star "select * from lineorder_flat order by 1,2, P_MFGR;"

sql """analyze table lineorder_flat with sync;"""
sql """alter table lineorder_flat modify column C_CITY set stats ('row_count'='7');"""
sql """alter table lineorder_flat modify column mv_LO_ORDERKEY set stats ('row_count'='1');"""
sql """set enable_stats=false;"""

mv_rewrite_success("""SELECT SUM(LO_EXTENDEDPRICE * LO_DISCOUNT) AS revenue
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -154,6 +154,10 @@ suite ("multiple_ssb") {

sql """analyze table lineorder_flat with sync;"""
sql """alter table lineorder_flat modify column LO_ORDERDATE set stats ('row_count'='8');"""
sql """alter table lineorder_flat modify column a1 set stats ('row_count'='1');"""
sql """alter table lineorder_flat modify column a4 set stats ('row_count'='1');"""
sql """alter table lineorder_flat modify column a6 set stats ('row_count'='1');"""
sql """alter table lineorder_flat modify column x2 set stats ('row_count'='1');"""
sql """set enable_stats=false;"""

mv_rewrite_success("""SELECT SUM(LO_EXTENDEDPRICE * LO_DISCOUNT) AS revenue
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -153,7 +153,10 @@ suite ("multiple_ssb_between") {
sql """set enable_stats=true;"""

sql """alter table lineorder_flat modify column LO_ORDERDATE set stats ('row_count'='8');"""

sql """alter table lineorder_flat modify column mv_LO_ORDERKEY set stats ('row_count'='1');"""
sql """alter table lineorder_flat modify column mv_P_BRAND set stats ('row_count'='1');"""
sql """alter table lineorder_flat modify column mv_C_NATION set stats ('row_count'='1');"""

mv_rewrite_success("""SELECT SUM(LO_EXTENDEDPRICE * LO_DISCOUNT) AS revenue
FROM lineorder_flat
WHERE
Expand Down
1 change: 1 addition & 0 deletions regression-test/suites/mv_p0/ssb/q_1_1/q_1_1.groovy
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,7 @@ suite ("mv_ssb_q_1_1") {

sql "analyze table lineorder_flat with sync;"
sql """alter table lineorder_flat modify column C_CITY set stats ('row_count'='6');"""
sql """alter table lineorder_flat modify column mv_LO_ORDERKEY set stats ('row_count'='1');"""
mv_rewrite_success("""SELECT SUM(LO_EXTENDEDPRICE * LO_DISCOUNT) AS revenue
FROM lineorder_flat
WHERE
Expand Down
1 change: 1 addition & 0 deletions regression-test/suites/mv_p0/ssb/q_2_1/q_2_1.groovy
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,7 @@ suite ("mv_ssb_q_2_1") {
qt_select_star "select * from lineorder_flat order by 1,2,P_MFGR;"

sql """alter table lineorder_flat modify column LO_ORDERDATE set stats ('row_count'='2');"""
sql """alter table lineorder_flat modify column mv_P_BRAND set stats ('row_count'='1');"""

mv_rewrite_success("""SELECT
SUM(LO_REVENUE), (LO_ORDERDATE DIV 10000) AS YEAR,
Expand Down
1 change: 1 addition & 0 deletions regression-test/suites/mv_p0/ssb/q_3_1/q_3_1.groovy
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,7 @@ suite ("mv_ssb_q_3_1") {
sql """analyze table lineorder_flat with sync;"""

sql """alter table lineorder_flat modify column LO_ORDERDATE set stats ('row_count'='2');"""
sql """alter table lineorder_flat modify column mv_C_NATION set stats ('row_count'='1');"""

mv_rewrite_success("""SELECT
C_NATION,
Expand Down
1 change: 1 addition & 0 deletions regression-test/suites/mv_p0/ssb/q_4_1/q_4_1.groovy
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,7 @@ suite ("mv_ssb_q_4_1") {
sql """analyze table lineorder_flat with sync;"""

sql """alter table lineorder_flat modify column LO_ORDERDATE set stats ('row_count'='2');"""
sql """alter table lineorder_flat modify column mv_C_NATION set stats ('row_count'='1');"""

mv_rewrite_success("""SELECT (LO_ORDERDATE DIV 10000) AS YEAR,
C_NATION,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,7 @@ suite ("q_4_1_r1") {

sql """analyze table lineorder_flat with sync;"""
sql """alter table lineorder_flat modify column LO_ORDERDATE set stats ('row_count'='8');"""
sql """alter table lineorder_flat modify column mv_C_NATION set stats ('row_count'='1');"""
sql """set enable_stats=false;"""

mv_rewrite_success("""SELECT (LO_ORDERDATE DIV 10000) AS YEAR,
Expand Down
Loading