7979import org .apache .logging .log4j .Logger ;
8080
8181import java .util .ArrayList ;
82- import java .util .BitSet ;
8382import java .util .Collection ;
8483import java .util .HashMap ;
8584import java .util .HashSet ;
@@ -188,16 +187,15 @@ protected List<StructInfo> getValidQueryStructInfos(Plan queryPlan, CascadesCont
188187 MaterializationContext materializationContext ) {
189188 List <StructInfo > validStructInfos = new ArrayList <>();
190189 // For every materialized view we should trigger refreshing struct info map
191- List <StructInfo > uncheckedQueryStructInfos = MaterializedViewUtils .extractStructInfo (queryPlan , queryPlan ,
192- cascadesContext ,
193- materializationContext .getQueryRelationIdSetMvMatched (cascadesContext .getStatementContext ()));
190+ List <StructInfo > uncheckedQueryStructInfos = MaterializedViewUtils .extractStructInfoFuzzy (queryPlan , queryPlan ,
191+ cascadesContext , materializationContext .getCommonTableIdSet (cascadesContext .getStatementContext ()));
194192 uncheckedQueryStructInfos .forEach (queryStructInfo -> {
195193 boolean valid = checkQueryPattern (queryStructInfo , cascadesContext ) && queryStructInfo .isValid ();
196194 if (!valid ) {
197195 cascadesContext .getMaterializationContexts ().forEach (ctx ->
198196 ctx .recordFailReason (queryStructInfo , "Query struct info is invalid" ,
199197 () -> String .format ("query table bitmap is %s, plan is %s" ,
200- queryStructInfo .getTableBitSet (), queryPlan .treeString ())
198+ queryStructInfo .getRelations (), queryPlan .treeString ())
201199 ));
202200 } else {
203201 validStructInfos .add (queryStructInfo );
@@ -278,9 +276,7 @@ protected List<Plan> doRewrite(StructInfo queryStructInfo, CascadesContext casca
278276 // Try to rewrite compensate predicates by using mv scan
279277 List <Expression > rewriteCompensatePredicates = rewriteExpression (compensatePredicates .toList (),
280278 queryPlan , materializationContext .getShuttledExprToScanExprMapping (),
281- viewToQuerySlotMapping , queryStructInfo .getTableBitSet (),
282- compensatePredicates .getRangePredicateMap (),
283- cascadesContext );
279+ viewToQuerySlotMapping , compensatePredicates .getRangePredicateMap (), cascadesContext );
284280 if (rewriteCompensatePredicates .isEmpty ()) {
285281 materializationContext .recordFailReason (queryStructInfo ,
286282 "Rewrite compensate predicate by view fail" ,
@@ -321,7 +317,7 @@ protected List<Plan> doRewrite(StructInfo queryStructInfo, CascadesContext casca
321317 && sessionVariable .isEnableMaterializedViewUnionRewrite ()) {
322318 MTMV mtmv = ((AsyncMaterializationContext ) materializationContext ).getMtmv ();
323319 Map <List <String >, Set <String >> queryUsedPartitions = PartitionCompensator .getQueryUsedPartitions (
324- cascadesContext .getStatementContext (), queryStructInfo .getTableBitSet ());
320+ cascadesContext .getStatementContext (), queryStructInfo .getRelationBitSet ());
325321 Set <MTMVRelatedTableIf > pctTables = mtmv .getMvPartitionInfo ().getPctTables ();
326322 boolean relateTableUsedPartitionsAnyNull = false ;
327323 boolean relateTableUsedPartitionsAllEmpty = true ;
@@ -588,14 +584,14 @@ protected Plan rewriteQueryByView(MatchMode matchMode, StructInfo queryStructInf
588584 * then use the corresponding value of mapping to replace it
589585 */
590586 protected List <Expression > rewriteExpression (List <? extends Expression > sourceExpressionsToWrite , Plan sourcePlan ,
591- ExpressionMapping targetExpressionMapping , SlotMapping targetToSourceMapping , BitSet sourcePlanBitSet ,
587+ ExpressionMapping targetExpressionMapping , SlotMapping targetToSourceMapping ,
592588 Map <Expression , ExpressionInfo > queryExprToInfoMap , CascadesContext cascadesContext ) {
593589 // Firstly, rewrite the target expression using source with inverse mapping
594590 // then try to use the target expression to represent the query. if any of source expressions
595591 // could not be represented by target expressions, return null.
596592 // generate target to target replacement expression mapping, and change target expression to source based
597593 List <? extends Expression > sourceShuttledExpressions = ExpressionUtils .shuttleExpressionWithLineage (
598- sourceExpressionsToWrite , sourcePlan , sourcePlanBitSet );
594+ sourceExpressionsToWrite , sourcePlan );
599595 ExpressionMapping expressionMappingKeySourceBased = targetExpressionMapping .keyPermute (targetToSourceMapping );
600596 // target to target replacement expression mapping, because mv is 1:1 so get the first element
601597 List <Map <Expression , Expression >> flattenExpressionMap = expressionMappingKeySourceBased .flattenMap ();
@@ -917,15 +913,15 @@ private boolean containsNullRejectSlot(Set<Set<Slot>> requireNoNullableViewSlot,
917913 }
918914 // query slot need shuttle to use table slot, avoid alias influence
919915 Set <Expression > queryUsedNeedRejectNullSlotsViewBased = ExpressionUtils .shuttleExpressionWithLineage (
920- new ArrayList <>(queryNullRejectSlotSet ), queryStructInfo .getTopPlan (), new BitSet () ).stream ()
916+ new ArrayList <>(queryNullRejectSlotSet ), queryStructInfo .getTopPlan ()).stream ()
921917 .map (expr -> ExpressionUtils .replace (expr , queryToViewMapping .toSlotReferenceMap ()))
922918 .collect (Collectors .toSet ());
923919 // view slot need shuttle to use table slot, avoid alias influence
924920 Set <Set <Slot >> shuttledRequireNoNullableViewSlot = new HashSet <>();
925921 for (Set <Slot > requireNullableSlots : requireNoNullableViewSlot ) {
926922 shuttledRequireNoNullableViewSlot .add (
927923 ExpressionUtils .shuttleExpressionWithLineage (new ArrayList <>(requireNullableSlots ),
928- viewStructInfo .getTopPlan (), new BitSet () ).stream ().map (Slot .class ::cast )
924+ viewStructInfo .getTopPlan ()).stream ().map (Slot .class ::cast )
929925 .collect (Collectors .toSet ()));
930926 }
931927 // query pulledUp predicates should have null reject predicates and contains any require noNullable slot
@@ -1101,7 +1097,7 @@ protected Plan tryRewriteTopN(LogicalTopN<Plan> queryTopNode, LogicalTopN<Plan>
11011097 List <Expression > queryOrderKeysExpressions = queryOrderKeys .stream ()
11021098 .map (OrderKey ::getExpr ).collect (Collectors .toList ());
11031099 List <? extends Expression > queryOrderByExpressionsShuttled = ExpressionUtils .shuttleExpressionWithLineage (
1104- queryOrderKeysExpressions , queryStructInfo .getTopPlan (), queryStructInfo . getTableBitSet () );
1100+ queryOrderKeysExpressions , queryStructInfo .getTopPlan ());
11051101
11061102 List <OrderKey > queryShuttledOrderKeys = new ArrayList <>();
11071103 for (int i = 0 ; i < queryOrderKeys .size (); i ++) {
@@ -1112,7 +1108,7 @@ protected Plan tryRewriteTopN(LogicalTopN<Plan> queryTopNode, LogicalTopN<Plan>
11121108 List <OrderKey > viewShuttledOrderKeys = new ArrayList <>();
11131109 List <? extends Expression > viewOrderByExpressionsShuttled = ExpressionUtils .shuttleExpressionWithLineage (
11141110 viewOrderKeys .stream ().map (OrderKey ::getExpr ).collect (Collectors .toList ()),
1115- viewStructInfo .getTopPlan (), new BitSet () );
1111+ viewStructInfo .getTopPlan ());
11161112 List <Expression > viewOrderByExpressionsQueryBasedSet = ExpressionUtils .replace (
11171113 viewOrderByExpressionsShuttled .stream ().map (Expression .class ::cast ).collect (Collectors .toList ()),
11181114 viewToQuerySlotMapping .toSlotReferenceMap ());
@@ -1132,7 +1128,7 @@ protected Plan tryRewriteTopN(LogicalTopN<Plan> queryTopNode, LogicalTopN<Plan>
11321128 // try to rewrite the order by expressions using the mv scan slot
11331129 List <Expression > rewrittenExpressions = rewriteExpression (queryOrderKeysExpressions ,
11341130 queryStructInfo .getTopPlan (), materializationContext .shuttledExprToScanExprMapping ,
1135- viewToQuerySlotMapping , queryStructInfo . getTableBitSet (), ImmutableMap .of (), cascadesContext );
1131+ viewToQuerySlotMapping , ImmutableMap .of (), cascadesContext );
11361132 if (rewrittenExpressions .isEmpty ()) {
11371133 materializationContext .recordFailReason (queryStructInfo ,
11381134 "query topN order keys rewrite fail, query topN order keys is not consistent "
0 commit comments