1717
1818package org .apache .doris .nereids .memo ;
1919
20- import org .apache .doris .catalog .MTMV ;
2120import org .apache .doris .common .IdGenerator ;
2221import org .apache .doris .common .Pair ;
22+ import org .apache .doris .nereids .StatementContext ;
2323import org .apache .doris .nereids .cost .Cost ;
2424import org .apache .doris .nereids .cost .CostCalculator ;
2525import org .apache .doris .nereids .metrics .EventChannel ;
5252import org .apache .logging .log4j .Logger ;
5353
5454import java .util .ArrayList ;
55- import java .util .BitSet ;
5655import java .util .HashMap ;
5756import java .util .HashSet ;
5857import java .util .LinkedHashMap ;
6160import java .util .Optional ;
6261import java .util .PriorityQueue ;
6362import java .util .Set ;
64- import java .util .concurrent .atomic .AtomicInteger ;
6563import java .util .stream .Collectors ;
6664import java .util .stream .Stream ;
6765import javax .annotation .Nullable ;
@@ -76,9 +74,7 @@ public class Memo {
7674 EventChannel .getDefaultChannel ().addConsumers (new LogConsumer (GroupMergeEvent .class , EventChannel .LOG )));
7775 private static long stateId = 0 ;
7876 private final ConnectContext connectContext ;
79- // The key is the query tableId, the value is the refresh version when last refresh, this is needed
80- // because struct info refresh base on target tableId.
81- private final Map <Integer , AtomicInteger > refreshVersion = new HashMap <>();
77+ private final StatementContext statementContext ;
8278 private final Map <Class <? extends AbstractMaterializedViewRule >, Set <Long >> materializationCheckSuccessMap =
8379 new LinkedHashMap <>();
8480 private final Map <Class <? extends AbstractMaterializedViewRule >, Set <Long >> materializationCheckFailMap =
@@ -93,11 +89,17 @@ public class Memo {
9389 public Memo () {
9490 this .root = null ;
9591 this .connectContext = null ;
92+ this .statementContext = null ;
9693 }
9794
9895 public Memo (ConnectContext connectContext , Plan plan ) {
96+ this (connectContext == null ? null : connectContext .getStatementContext (), plan );
97+ }
98+
99+ private Memo (StatementContext statementContext , Plan plan ) {
100+ this .statementContext = statementContext ;
101+ this .connectContext = statementContext == null ? null : statementContext .getConnectContext ();
99102 this .root = init (plan );
100- this .connectContext = connectContext ;
101103 }
102104
103105 public static long getStateId () {
@@ -132,30 +134,6 @@ public int getGroupExpressionsSize() {
132134 return groupExpressions .size ();
133135 }
134136
135- /** get the refresh version map*/
136- public Map <Integer , AtomicInteger > getRefreshVersion () {
137- return refreshVersion ;
138- }
139-
140- /** return the incremented refresh version for the given commonTableId*/
141- public long incrementAndGetRefreshVersion (int commonTableId ) {
142- return refreshVersion .compute (commonTableId , (k , v ) -> {
143- if (v == null ) {
144- return new AtomicInteger (1 );
145- }
146- v .incrementAndGet ();
147- return v ;
148- }).get ();
149- }
150-
151- /** return the incremented refresh version for the given relationId set*/
152- public void incrementAndGetRefreshVersion (BitSet commonTableIdSet ) {
153- for (int i = commonTableIdSet .nextSetBit (0 ); i >= 0 ;
154- i = commonTableIdSet .nextSetBit (i + 1 )) {
155- incrementAndGetRefreshVersion (i );
156- }
157- }
158-
159137 /**
160138 * Record materialization check result for performance
161139 */
@@ -379,6 +357,7 @@ public Plan copyOut(GroupExpression logicalExpression, boolean includeGroupExpre
379357 */
380358 private Group init (Plan plan ) {
381359 Preconditions .checkArgument (!(plan instanceof GroupPlan ), "Cannot init memo by a GroupPlan" );
360+ registerRelationIdentity (plan );
382361
383362 // initialize children recursively
384363 List <Group > childrenGroups = new ArrayList <>(plan .arity ());
@@ -481,15 +460,7 @@ private CopyInResult doCopyIn(Plan plan, @Nullable Group targetGroup, @Nullable
481460 plan .getLogicalProperties (), targetGroup .getLogicalProperties ());
482461 throw new IllegalStateException ("Insert a plan into targetGroup but differ in logicalproperties" );
483462 }
484- if (connectContext != null
485- && connectContext .getSessionVariable ().isEnableMaterializedViewNestRewrite ()
486- && plan instanceof LogicalCatalogRelation
487- && ((CatalogRelation ) plan ).getTable () instanceof MTMV
488- && !plan .getGroupExpression ().isPresent ()) {
489- TableId mvCommonTableId
490- = this .connectContext .getStatementContext ().getTableId (((CatalogRelation ) plan ).getTable ());
491- incrementAndGetRefreshVersion (mvCommonTableId .asInt ());
492- }
463+ registerRelationIdentity (plan );
493464 Optional <GroupExpression > groupExpr = plan .getGroupExpression ();
494465 if (groupExpr .isPresent ()) {
495466 Preconditions .checkState (groupExpressions .containsKey (groupExpr .get ()));
@@ -513,6 +484,24 @@ private CopyInResult doCopyIn(Plan plan, @Nullable Group targetGroup, @Nullable
513484 // TODO: need to derive logical property if generate new group. currently we not copy logical plan into
514485 }
515486
487+ private void registerRelationIdentity (Plan plan ) {
488+ if (statementContext == null ) {
489+ return ;
490+ }
491+ if (plan instanceof LogicalCatalogRelation ) {
492+ // StructInfoMap searches query alternatives by relation id, but each MV context starts from the
493+ // table ids in the MV definition. Register both original scans and nested MV scans copied into memo
494+ // so those table ids can be expanded back to the currently available relation ids.
495+ CatalogRelation catalogRelation = (CatalogRelation ) plan ;
496+ TableId tableId = statementContext .getTableId (catalogRelation .getTable ());
497+ boolean relationIdentityChanged = statementContext .getTableIdToRelationIds ()
498+ .put (tableId .asInt (), catalogRelation .getRelationId ().asInt ());
499+ if (relationIdentityChanged ) {
500+ groups .values ().forEach (group -> group .getStructInfoMap ().clearCandidateCache ());
501+ }
502+ }
503+ }
504+
516505 private List <Group > rewriteChildrenPlansToGroups (Plan plan , Group targetGroup ) {
517506 List <Group > childrenGroups = Lists .newArrayList ();
518507 for (int i = 0 ; i < plan .children ().size (); i ++) {
0 commit comments