private RelNode rewriteUsingViews(RelOptPlanner planner, RelNode basePlan,
    RelMetadataProvider mdProvider, RexExecutor executorProvider, Collection<? extends RelOptMaterialization> materializations) {
      final PerfLogger perfLogger = SessionState.getPerfLogger();
      perfLogger.perfLogBegin(this.getClass().getName(), PerfLogger.OPTIMIZER);
      final RelOptCluster optCluster = basePlan.getCluster();

      // We need to expand IN/BETWEEN expressions when materialized view rewriting
      // is triggered since otherwise this may prevent some rewritings from happening
      HepProgramBuilder program = new HepProgramBuilder();
      generatePartialProgram(program, false, HepMatchOrder.DEPTH_FIRST,
              HiveInBetweenExpandRule.FILTER_INSTANCE,
              HiveInBetweenExpandRule.JOIN_INSTANCE,
              HiveInBetweenExpandRule.PROJECT_INSTANCE);
      basePlan = executeProgram(basePlan, program.build(), mdProvider, executorProvider);

      // Pre-processing to being able to trigger additional rewritings
      basePlan = HiveMaterializedViewBoxing.boxPlan(basePlan);

      // If this is not a rebuild, we use Volcano planner as the decision
      // on whether to use MVs or not and which MVs to use should be cost-based
      optCluster.invalidateMetadataQuery();
      RelMetadataQuery.THREAD_PROVIDERS.set(HiveMaterializationRelMetadataProvider.DEFAULT);

      // Add materializations to planner
      for (RelOptMaterialization materialization : materializations) {
        if (LOG.isDebugEnabled()) {
          LOG.debug("Adding materialization {} to the planner; the plan is:\n{}", materialization.qualifiedTableName,
              RelOptUtil.toString(materialization.queryRel));
        }
        planner.addMaterialization(materialization);
      }
      // Add rule to split aggregate with grouping sets (if any)
      planner.addRule(HiveAggregateSplitRule.INSTANCE);
      // Add view-based rewriting rules to planner
      for (RelOptRule rule : HiveMaterializedViewRule.MATERIALIZED_VIEW_REWRITING_RULES) {
        planner.addRule(rule);
      }
      // Unboxing rule
      planner.addRule(HiveMaterializedViewBoxing.INSTANCE_UNBOXING);
      // Partition pruner rule
      planner.addRule(HiveFilterProjectTSTransposeRule.INSTANCE);
      planner.addRule(new HivePartitionPruneRule(conf));

      // Optimize plan
      final String ruleExclusionRegex = conf.get(ConfVars.HIVE_CBO_RULE_EXCLUSION_REGEX.varname, "");
      if (!ruleExclusionRegex.isEmpty()) {
        LOG.info("The CBO rules matching the following regex are excluded from planning: {}",
            ruleExclusionRegex);
        planner.setRuleDescExclusionFilter(Pattern.compile(ruleExclusionRegex));
      }
      planner.setRoot(basePlan);
      basePlan = planner.findBestExp();
      // Remove view-based rewriting rules from planner
      planner.clear();

      // Restore default cost model
      optCluster.invalidateMetadataQuery();
      RelMetadataQuery.THREAD_PROVIDERS.set(JaninoRelMetadataProvider.of(mdProvider));

      perfLogger.perfLogEnd(this.getClass().getName(), PerfLogger.OPTIMIZER, "Calcite: View-based rewriting");
      return basePlan;
    }
Returns true if MV is being loaded, constructed or being rebuilt.
ql/src/java/org/apache/hadoop/hive/ql/parse/CalcitePlanner.java