public RelOptCost getCost(RelNode rel, RelMetadataQuery mq) {
    assert rel != null : "pre-condition: rel != null";
    if (rel instanceof RelSubset) {
      // Get cost of the subset, best rel may have been chosen or not
      RelSubset subset = (RelSubset) rel;
      if (subset.getBest() != null) {
        return getCost(subset.getBest(), mq);
      }
      return costFactory.makeInfiniteCost();
    }
    if (rel.getTraitSet().getTrait(ConventionTraitDef.INSTANCE)
        == Convention.NONE) {
      return costFactory.makeInfiniteCost();
    }
    // We get the cost of the operator
    RelOptCost cost = mq.getNonCumulativeCost(rel);
    if (!costFactory.makeZeroCost().isLt(cost)) {
      // cost must be positive, so nudge it
      cost = costFactory.makeTinyCost();
    }
    // If this operator has a materialized view below,
    // we make its cost tiny and adjust the cost of its
    // inputs
    boolean usesMaterializedViews = false;
    Multimap<Class<? extends RelNode>, RelNode> nodeTypes =
        mq.getNodeTypes(rel);
    for (RelNode scan : nodeTypes.get(TableScan.class)) {
      if (scan.getTable() instanceof RelOptHiveTable) {
        RelOptHiveTable relOptHiveTable = (RelOptHiveTable) scan.getTable();
        if (relOptHiveTable.getHiveTableMD().isMaterializedView()) {
          usesMaterializedViews = true;
          break;
        }
      }
    }
    if (isHeuristic && usesMaterializedViews) {
      // If a child of this expression uses a materialized view,
      // then we decrease its cost by a certain factor. This is
      // useful for e.g. partial rewritings, where a part of plan
      // does not use the materialization, but we still want to
      // decrease its cost so it is chosen instead of the original
      // plan
      cost = cost.multiplyBy(RelOptUtil.EPSILON);
      if (!costFactory.makeZeroCost().isLt(cost)) {
        // cost must be positive, so nudge it
        cost = costFactory.makeTinyCost();
      }
      for (RelNode input : rel.getInputs()) {
        cost = cost.plus(getCost(input, mq));
      }
    } else {
      // No materialized view or not heuristic approach, normal costing
      for (RelNode input : rel.getInputs()) {
        cost = cost.plus(getCost(input, mq));
      }
    }
    return cost;
  }
The method extends the logic of the super method to decrease
the cost of the plan if it contains materialized views
(heuristic).
ql/src/java/org/apache/hadoop/hive/ql/optimizer/calcite/cost/HiveVolcanoPlanner.java