Skip to content

Commit 32a07b7

Browse files
committed
[fix](fe) Normalize stream scans before MV pre-rewrite recording
A stream scan inside a reused CTE could still be recorded as LogicalOlapTableStreamScan in tmpPlanForMvRewrite because the MV pre-rewrite CTE job list missed NormalizeOlapTableStreamScan. That recorded temp plan could then be optimized as a plain olap scan in pre-MV rewrite. Key changes: - add NormalizeOlapTableStreamScan to the MV pre-rewrite CTE child rewrite jobs - add a FE UT that inspects tmpPlanForMvRewrite and asserts stream scans are normalized - drop the attempted regression case because MV chosen/output cannot stably prove this internal temp-plan state Unit Test: - run-fe-ut.sh --run org.apache.doris.nereids.trees.plans.ExplainTableStreamPlanTest
1 parent 66e7fe8 commit 32a07b7

2 files changed

Lines changed: 45 additions & 0 deletions

File tree

‎fe/fe-core/src/main/java/org/apache/doris/nereids/jobs/executor/Rewriter.java‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -407,6 +407,7 @@ public class Rewriter extends AbstractBatchJobExecutor {
407407
topDown(
408408
new PruneOlapScanPartition(),
409409
new PruneEmptyPartition(),
410+
new NormalizeOlapTableStreamScan(),
410411
new PruneFileScanPartition(),
411412
new PushDownFilterIntoSchemaScan()
412413
)

‎fe/fe-core/src/test/java/org/apache/doris/nereids/trees/plans/ExplainTableStreamPlanTest.java‎

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@
4848
import org.apache.doris.nereids.trees.expressions.StatementScopeIdGenerator;
4949
import org.apache.doris.nereids.trees.expressions.literal.VarcharLiteral;
5050
import org.apache.doris.nereids.trees.plans.commands.ExplainCommand;
51+
import org.apache.doris.nereids.trees.plans.logical.LogicalOlapTableStreamScan;
5152
import org.apache.doris.nereids.trees.plans.logical.LogicalPlan;
5253
import org.apache.doris.nereids.trees.plans.logical.LogicalProject;
5354
import org.apache.doris.nereids.trees.plans.physical.PhysicalPlan;
@@ -499,6 +500,37 @@ public void testAppendOnlyAliasedStreamResetCanBePlanned() throws Exception {
499500
assertStreamScanCanBePlanned(ctx, "explain select s.* from test_stream.s_dup@reset() as s");
500501
}
501502

503+
@Test
504+
public void testRecordPlanForMvPreRewriteNormalizesStreamScanInsideCte() throws Exception {
505+
ConnectContext ctx = createDefaultCtx();
506+
ctx.setDatabase("test_stream");
507+
ctx.getSessionVariable().showHiddenColumns = true;
508+
ctx.getSessionVariable().enableMaterializedViewRewrite = true;
509+
510+
String sql = "explain with cte as ("
511+
+ "select k1, __DORIS_STREAM_CHANGE_TYPE_COL__ as change_type from test_stream.s2"
512+
+ ") "
513+
+ "select t1.k1 "
514+
+ "from (select k1, change_type from cte where change_type in ('APPEND', 'UPDATE_AFTER')) t1 "
515+
+ "join (select k1, change_type from cte where change_type in ('DELETE', 'UPDATE_BEFORE')) t2 "
516+
+ "on t1.k1 = t2.k1";
517+
518+
StatementScopeIdGenerator.clear();
519+
StatementContext statementContext = MemoTestUtils.createStatementContext(ctx, sql);
520+
statementContext.setForceRecordTmpPlan(true);
521+
NereidsPlanner planner = new NereidsPlanner(statementContext);
522+
LogicalPlan logicalPlan = (LogicalPlan) ((Explainable) (((ExplainCommand) parser.parseSingle(sql))
523+
.getLogicalPlan())).getExplainPlan(ctx);
524+
planner.planWithLock(logicalPlan, PhysicalProperties.ANY);
525+
526+
List<Plan> tmpPlans = planner.getCascadesContext().getStatementContext().getTmpPlanForMvRewrite();
527+
Assertions.assertFalse(tmpPlans.isEmpty());
528+
for (Plan tmpPlan : tmpPlans) {
529+
Assertions.assertFalse(containsLogicalStreamScan(tmpPlan),
530+
"tmp plan for mv pre rewrite should have normalized stream scans inside cte children");
531+
}
532+
}
533+
502534
@Test
503535
public void testMowTimeTravelQualifiedColumnCanBind() {
504536
// MOW time-travel goes through a union whose outputs are rebuilt with empty qualifiers.
@@ -545,6 +577,18 @@ private LogicalProject<?> findFirstLogicalProject(Plan plan) {
545577
return null;
546578
}
547579

580+
private boolean containsLogicalStreamScan(Plan plan) {
581+
if (plan instanceof LogicalOlapTableStreamScan) {
582+
return true;
583+
}
584+
for (Plan child : plan.children()) {
585+
if (containsLogicalStreamScan(child)) {
586+
return true;
587+
}
588+
}
589+
return false;
590+
}
591+
548592
private void assertStreamScanCanBePlanned(ConnectContext ctx, String sql) {
549593
PlanFragment fragment = Assertions.assertDoesNotThrow(() -> getFragment(ctx, sql));
550594

0 commit comments

Comments
 (0)