[BugFix] Fix agg push down type cast bug (#63875)

Signed-off-by: shuming.li <ming.moriarty@gmail.com>
This commit is contained in:
shuming.li 2025-10-11 10:04:35 +08:00 committed by GitHub
parent f754c243b4
commit d0b7c2226f
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 48 additions and 5 deletions

View File

@ -477,8 +477,9 @@ public final class AggregatedMaterializedViewPushDownRewriter extends Materializ
remapping.put(aggColRef, newColRef);
ctx.aggColRefToPushDownAggMap.put(aggColRef, aggCall);
}
Map<ColumnRefOperator, CallOperator> newAggregations = Maps.newHashMap();
uniqueAggregations.forEach((k, v) -> newAggregations.put(v, k));
Map<ColumnRefOperator, CallOperator> newAggregations = uniqueAggregations.entrySet().stream()
.map(e -> Maps.immutableEntry(e.getValue(), e.getKey()))
.collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue));
LogicalAggregationOperator newAggOp = LogicalAggregationOperator.builder()
.setAggregations(newAggregations)
.setType(AggType.GLOBAL)

View File

@ -768,9 +768,15 @@ public final class AggregatedMaterializedViewRewriter extends MaterializedViewRe
// aggColRefToAggMap: oldCol1 -> coalesce(newCol1, 0)
// It will generate new projections as below:
// newProjections: oldCol1 -> coalesce(newCol1, 0)
ScalarOperator newProjectOp = mvRewriteContext.isInAggregatePushDown() ?
newAggColRef : genRollupProject(aggCall, newAggColRef, hasGroupByKeys);
aggColRefToAggMap.put(origColRef, newProjectOp);
if (mvRewriteContext.isInAggregatePushDown()) {
// it's safe to change origColRef's type here because it's copied in agg push down rule
// and origColRef will be removed after rewrite.
origColRef.setType(newAggColRef.getType());
aggColRefToAggMap.put(origColRef, newAggColRef);
} else {
ScalarOperator newProjectOp = genRollupProject(aggCall, newAggColRef, hasGroupByKeys);
aggColRefToAggMap.put(origColRef, newProjectOp);
}
}
}

View File

@ -24,7 +24,9 @@ import com.starrocks.common.FeConstants;
import com.starrocks.common.Pair;
import com.starrocks.sql.optimizer.OptExpression;
import com.starrocks.sql.optimizer.rule.transformation.materialization.AggregatedMaterializedViewPushDownRewriter;
import com.starrocks.sql.plan.PlanTestBase;
import com.starrocks.thrift.TExplainLevel;
import com.starrocks.utframe.StarRocksTestBase;
import mockit.Mock;
import mockit.MockUp;
import org.apache.commons.lang3.StringUtils;
@ -1198,4 +1200,38 @@ public class MaterializedViewAggPushDownRewriteTest extends MaterializedViewTest
starRocksAssert.dropMaterializedView("test_pt8_mv");
starRocksAssert.dropMaterializedView("test_pt9_mv");
}
@Test
public void testAggPushDownTypeCastBug() throws Exception {
starRocksAssert.withMaterializedView("CREATE MATERIALIZED VIEW `mv1` \n" +
"DISTRIBUTED BY RANDOM\n" +
"PROPERTIES (\n" +
"\"replication_num\" = \"1\"" +
")\n" +
"AS SELECT `l`.`LO_ORDERDATE`, " +
"sum(`l`.`LO_REVENUE` + 1), " +
"max(`l`.`LO_REVENUE` + 1), " +
"min(`l`.`LO_REVENUE` + 1), " +
"bitmap_agg(`l`.`LO_REVENUE` + 1), " +
"hll_union(hll_hash(`l`.`LO_REVENUE` + 1)), " +
"percentile_union(percentile_hash(`l`.`LO_REVENUE` + 1)), " +
"any_value(`l`.`LO_REVENUE` + 1) , " +
"array_agg(DISTINCT `l`.`LO_REVENUE` + 1)\n" +
"FROM `lineorder` AS `l`\n" +
"GROUP BY `l`.`LO_ORDERDATE`;");
String query = " select LO_ORDERDATE, sum(LO_REVENUE + 1), max(LO_REVENUE + 1), sum(LO_REVENUE + 1), max(LO_REVENUE + " +
"1) , sum(LO_REVENUE + 1), max(LO_REVENUE + 1), count(distinct LO_REVENUE + 1), count(distinct LO_REVENUE + 1) " +
"from lineorder l join dates d " +
"on l.LO_ORDERDATE = d.d_date group by LO_ORDERDATE order by LO_ORDERDATE;";
String plan = getQueryPlan(query, TExplainLevel.NORMAL);
System.out.println(plan);
PlanTestBase.assertNotContains(plan, ": count AS BIGINT)");
PlanTestBase.assertContains(plan, "mv1",
" 2:AGGREGATE (update serialize)\n" +
" | STREAMING\n" +
" | output: sum(40: sum(l.LO_REVENUE + 1)), max(41: max(l.LO_REVENUE + 1)), " +
"bitmap_union(43: bitmap_agg(l.LO_REVENUE + 1))\n" +
" | group by: 57: cast, 39: LO_ORDERDATE");
starRocksAssert.dropMaterializedView("mv1");
}
}