@Override
  public int execute() throws HiveException {
    Table mv = context.getDb().getTable(desc.getMaterializedViewName());
    if (mv.isRewriteEnabled() == desc.isRewriteEnable()) {
      // This is a noop, return successfully
      return 0;
    }
    Table newMV = mv.copy(); // Do not mess with Table instance

    if (desc.isRewriteEnable()) {
      try {
        QueryState qs = new QueryState.Builder().withHiveConf(context.getConf()).build();
        CalcitePlanner planner = new CalcitePlanner(qs);
        Context ctx = new Context(context.getConf());
        ctx.setIsLoadingMaterializedView(true);
        planner.initCtx(ctx);
        planner.init(false);

        RelNode plan = planner.genLogicalPlan(ParseUtils.parse(newMV.getViewExpandedText(), ctx));
        if (plan == null) {
          String msg = "Cannot enable automatic rewriting for materialized view.";
          if (ctx.getCboInfo() != null) {
            msg += " " + ctx.getCboInfo();
          }
          throw new HiveException(msg);
        }
        MaterializationValidationResult validationResult = planner.getMaterializationValidationResult();
        String validationErrorMessage = validationResult.getErrorMessage();
        if (validationResult.getSupportedRewriteAlgorithms().isEmpty()) {
          throw new HiveException(validationErrorMessage);
        } else if (isNotBlank(validationErrorMessage)) {
          console.printError(validationErrorMessage);
        }
      } catch (Exception e) {
        throw new HiveException(e);
      }
    }

    newMV.setRewriteEnabled(desc.isRewriteEnable());
    EnvironmentContext environmentContext = new EnvironmentContext();
    environmentContext.putToProperties(StatsSetupConst.DO_NOT_UPDATE_STATS, StatsSetupConst.TRUE);
    context.getDb().alterTable(newMV, false, environmentContext, true);

    return 0;
  }
Operation process of enabling/disabling materialized view rewrite.
ql/src/java/org/apache/hadoop/hive/ql/ddl/view/materialized/alter/rewrite/AlterMaterializedViewRewriteOperation.java