protected RelNode applyMaterializedViewRewriting(RelOptPlanner planner, RelNode basePlan,
        RelMetadataProvider mdProvider, RexExecutor executorProvider) {
      final RelOptCluster optCluster = basePlan.getCluster();

      final boolean useMaterializedViewsRegistry =
          !conf.get(HiveConf.ConfVars.HIVE_SERVER2_MATERIALIZED_VIEWS_REGISTRY_IMPL.varname).equals("DUMMY");
      final RelNode calcitePreMVRewritingPlan = basePlan;
      
      final Set<TableName> tablesUsedQuery = getTablesUsed(basePlan);
      if (tablesUsedQuery.isEmpty()) {
        return basePlan;
      }
      // Add views to planner
      List<HiveRelOptMaterialization> materializations = new ArrayList<>();
      try {
        // This is not a rebuild, we retrieve all the materializations.
        // In turn, we do not need to force the materialization contents to be up-to-date,
        // as this is not a rebuild, and we apply the user parameters
        // (HIVE_MATERIALIZED_VIEW_REWRITING_TIME_WINDOW) instead.
        if (useMaterializedViewsRegistry) {
          materializations.addAll(db.getPreprocessedMaterializedViewsFromRegistry(tablesUsedQuery,
              queryState::getValidTxnList, getTxnMgr()));
        } else {
          materializations.addAll(db.getPreprocessedMaterializedViews(tablesUsedQuery,
              queryState::getValidTxnList, getTxnMgr()));
        }
        // We need to use the current cluster for the scan operator on views,
        // otherwise the planner will throw an Exception (different planners)
        materializations = materializations.stream().
                map(materialization -> materialization.copyToNewCluster(optCluster)).
                collect(Collectors.toList());
      } catch (HiveException e) {
        LOG.warn("Exception loading materialized views", e);
      }

      if (materializations.isEmpty()) {
        // There are no materializations, we can return the original plan
        return calcitePreMVRewritingPlan;
      }

      basePlan = rewriteUsingViews(planner, basePlan, mdProvider, executorProvider, materializations);

      List<Table> materializedViewsUsedOriginalPlan = getMaterializedViewsUsed(calcitePreMVRewritingPlan);
      List<Table> materializedViewsUsedAfterRewrite = getMaterializedViewsUsed(basePlan);
      if (materializedViewsUsedOriginalPlan.size() == materializedViewsUsedAfterRewrite.size()) {
        // Materialized view-based rewriting did not happen, we can return the original plan
        return calcitePreMVRewritingPlan;
      }

      try {
        if (!HiveMaterializedViewUtils.checkPrivilegeForMaterializedViews(materializedViewsUsedAfterRewrite)) {
          // if materialized views do not have appropriate privileges, we shouldn't be using them
          return calcitePreMVRewritingPlan;
        }
      } catch (HiveException e) {
        LOG.warn("Exception checking privileges for materialized views", e);
        return calcitePreMVRewritingPlan;
      }
      // A rewriting was produced, we will check whether it was part of an incremental rebuild
      // to try to replace INSERT OVERWRITE by INSERT or MERGE
      if (useMaterializedViewsRegistry) {
        // Before proceeding we need to check whether materialized views used are up-to-date
        // wrt information in metastore
        try {
          if (!db.validateMaterializedViewsFromRegistry(materializedViewsUsedAfterRewrite, tablesUsedQuery,
              queryState::getValidTxnList, getTxnMgr())) {
            return calcitePreMVRewritingPlan;
          }
        } catch (HiveException e) {
          LOG.warn("Exception validating materialized views", e);
          return calcitePreMVRewritingPlan;
        }
      }
      // Now we trigger some needed optimization rules again
      return applyPreJoinOrderingTransforms(basePlan, mdProvider, executorProvider);
    }
Returns true if MV is being loaded, constructed or being rebuilt.
ql/src/java/org/apache/hadoop/hive/ql/parse/CalcitePlanner.java