@@ -51,33 +51,44 @@ public enum Mode {
5151 FULL
5252 }
5353
54+ /**
55+ * How the rewrite result is consumed. {@link #DRY_RUN} only applies to incremental
56+ * refresh; {@link #EXPLAIN} applies to both incremental and complete refresh and only
57+ * produces a plan (no execution and no MV data read).
58+ */
59+ public enum ExecutionKind {
60+ /** Real refresh: the rewrite result is executed and written back. */
61+ EXECUTE ,
62+ /** Dry run: execute the delta query and return its rows, but do not write the MV. */
63+ DRY_RUN ,
64+ /** Explain: only generate the plan for display. */
65+ EXPLAIN
66+ }
67+
5468 private final Mode mode ;
5569 private final MTMV mtmv ;
5670 // The MTMV object does not exist yet during CREATE, so keep its name separately for diagnostics.
5771 private final String createMtmvName ;
72+ // Only used by EXPLAIN REFRESH ... ALL (kind == EXPLAIN); false for every other kind.
5873 private final boolean includeExhaustedStreams ;
59- private final boolean dryRun ;
74+ private final ExecutionKind executionKind ;
75+ // Present only for DRY_RUN (incremental refresh); empty otherwise.
6076 private final Optional <IvmDryRunLimit > dryRunLimit ;
6177 private final Map <BaseTableInfo , Set <Long >> fullRefreshResetPartitionIds ;
6278 private final Optional <StreamReadMode > fullRefreshNonPctReadMode ;
6379 // Set by MTMVPlanUtil before normalization: true means the MV unique keys include identity key columns.
6480 // Null when the rewrite context is created outside the analyzeQuery flow.
6581 private Boolean useFullKeys ;
6682
67- public IvmRewriteContext (Mode mode , MTMV mtmv , boolean includeExhaustedStreams ) {
68- this (mode , mtmv , null , includeExhaustedStreams , false , Optional .empty (),
69- Collections .emptyMap (), Optional .empty ());
70- }
71-
7283 private IvmRewriteContext (Mode mode , MTMV mtmv , String createMtmvName , boolean includeExhaustedStreams ,
73- boolean dryRun , Optional <IvmDryRunLimit > dryRunLimit ,
84+ ExecutionKind executionKind , Optional <IvmDryRunLimit > dryRunLimit ,
7485 Map <BaseTableInfo , Set <Long >> fullRefreshResetPartitionIds ,
7586 Optional <StreamReadMode > fullRefreshNonPctReadMode ) {
7687 this .mode = Objects .requireNonNull (mode , "mode can not be null" );
7788 this .mtmv = mode == Mode .CREATE ? mtmv : Objects .requireNonNull (mtmv , "mtmv can not be null" );
7889 this .createMtmvName = createMtmvName ;
7990 this .includeExhaustedStreams = includeExhaustedStreams ;
80- this .dryRun = dryRun ;
91+ this .executionKind = Objects . requireNonNull ( executionKind , "executionKind can not be null" ) ;
8192 this .dryRunLimit = Objects .requireNonNull (dryRunLimit , "dryRunLimit can not be null" );
8293 Map <BaseTableInfo , Set <Long >> resetPartitionIds = new HashMap <>();
8394 Objects .requireNonNull (fullRefreshResetPartitionIds , "fullRefreshResetPartitionIds can not be null" )
@@ -91,30 +102,47 @@ private IvmRewriteContext(Mode mode, MTMV mtmv, String createMtmvName, boolean i
91102 public static IvmRewriteContext create (String mtmvName ) {
92103 return new IvmRewriteContext (Mode .CREATE , null ,
93104 Objects .requireNonNull (mtmvName , "mtmvName can not be null" ), false ,
94- false , Optional .empty (), Collections .emptyMap (), Optional .empty ());
105+ ExecutionKind . EXECUTE , Optional .empty (), Collections .emptyMap (), Optional .empty ());
95106 }
96107
97108 public static IvmRewriteContext normalize (MTMV mtmv ) {
98- return new IvmRewriteContext (Mode .NORMALIZE , Objects .requireNonNull (mtmv , "mtmv can not be null" ), false );
109+ return new IvmRewriteContext (Mode .NORMALIZE , Objects .requireNonNull (mtmv , "mtmv can not be null" ),
110+ null , false , ExecutionKind .EXECUTE , Optional .empty (), Collections .emptyMap (), Optional .empty ());
111+ }
112+
113+ public static IvmRewriteContext incremental (MTMV mtmv ) {
114+ return new IvmRewriteContext (Mode .INCREMENTAL , Objects .requireNonNull (mtmv , "mtmv can not be null" ),
115+ null , false , ExecutionKind .EXECUTE , Optional .empty (), Collections .emptyMap (), Optional .empty ());
99116 }
100117
101- public static IvmRewriteContext incremental (MTMV mtmv , boolean includeExhaustedStreams ) {
102- return new IvmRewriteContext (Mode .INCREMENTAL , mtmv , includeExhaustedStreams );
118+ /** EXPLAIN REFRESH INCREMENTAL [ALL]: only a plan is produced. */
119+ public static IvmRewriteContext incrementalExplain (MTMV mtmv , boolean includeExhaustedStreams ) {
120+ return new IvmRewriteContext (Mode .INCREMENTAL , Objects .requireNonNull (mtmv , "mtmv can not be null" ),
121+ null , includeExhaustedStreams , ExecutionKind .EXPLAIN ,
122+ Optional .empty (), Collections .emptyMap (), Optional .empty ());
103123 }
104124
105125 public static IvmRewriteContext incrementalDryRun (MTMV mtmv , Optional <IvmDryRunLimit > dryRunLimit ) {
106126 return new IvmRewriteContext (Mode .INCREMENTAL , mtmv , null , false ,
107- true , dryRunLimit , Collections .emptyMap (), Optional .empty ());
127+ ExecutionKind .DRY_RUN , dryRunLimit , Collections .emptyMap (), Optional .empty ());
128+ }
129+
130+ /** EXPLAIN REFRESH COMPLETE: only a plan is produced. */
131+ public static IvmRewriteContext fullExplain (MTMV mtmv ) {
132+ return new IvmRewriteContext (Mode .FULL , Objects .requireNonNull (mtmv , "mtmv can not be null" ),
133+ null , false , ExecutionKind .EXPLAIN , Optional .empty (), Collections .emptyMap (), Optional .empty ());
108134 }
109135
110136 public static IvmRewriteContext full (MTMV mtmv ) {
111- return new IvmRewriteContext (Mode .FULL , mtmv , false );
137+ return new IvmRewriteContext (Mode .FULL , Objects .requireNonNull (mtmv , "mtmv can not be null" ),
138+ null , false , ExecutionKind .EXECUTE , Optional .empty (), Collections .emptyMap (), Optional .empty ());
112139 }
113140
114141 public static IvmRewriteContext full (MTMV mtmv ,
115142 Map <BaseTableInfo , Set <Long >> resetPartitionIds ,
116143 StreamReadMode nonPctReadMode ) {
117- return new IvmRewriteContext (Mode .FULL , mtmv , null , false , false , Optional .empty (), resetPartitionIds ,
144+ return new IvmRewriteContext (Mode .FULL , mtmv , null , false , ExecutionKind .EXECUTE , Optional .empty (),
145+ resetPartitionIds ,
118146 Optional .of (Objects .requireNonNull (nonPctReadMode , "nonPctReadMode can not be null" )));
119147 }
120148
@@ -138,12 +166,17 @@ public boolean isIncludeExhaustedStreams() {
138166 return includeExhaustedStreams ;
139167 }
140168
141- // True for REFRESH ... INCREMENTAL WITH DRY RUN: the root plan must be a LogicalResultSink.
169+ /** True for REFRESH ... INCREMENTAL WITH DRY RUN: the root plan must be a LogicalResultSink. */
142170 public boolean isDryRun () {
143- return dryRun ;
171+ return executionKind == ExecutionKind .DRY_RUN ;
172+ }
173+
174+ /** True for EXPLAIN REFRESH (incremental or complete): only the plan is produced. */
175+ public boolean isExplain () {
176+ return executionKind == ExecutionKind .EXPLAIN ;
144177 }
145178
146- // Present only for REFRESH ... INCREMENTAL WITH DRY RUN ; empty otherwise.
179+ // Present only for DRY_RUN (incremental refresh) ; empty otherwise.
147180 public Optional <IvmDryRunLimit > getDryRunLimit () {
148181 return dryRunLimit ;
149182 }
0 commit comments