docs: private static Index findBestMatchIndexForMatviewMinOrMax(MaterializedViewInfo matviewinfo,
              Table srcTable, List<AbstractExpression> groupbyExprs, AbstractExpression minMaxAggExpr) {
          CatalogMap<Index> allIndexes = srcTable.getIndexes();
          StmtTableScan tableScan = new StmtTargetTableScan(srcTable);
  
          // Candidate index. If we can find an index covering both group-by columns and aggExpr (optimal) then we will
          // return immediately.
          // If the index found covers only group-by columns (sub-optimal), we will first cache it here.
          Index candidate = null;
          for (Index index : allIndexes) {
              // indexOptimalForMinMax == true if the index covered both the group-by columns and the min/max aggExpr.
              boolean indexOptimalForMinMax = false;
              // If minMaxAggExpr is not null, the diff can be zero or one.
              // Otherwise, for a usable index, its number of columns must agree with that of the group-by columns.
              final int diffAllowance = minMaxAggExpr == null ? 0 : 1;
  
              // Get all indexed exprs if there is any.
              String expressionjson = index.getExpressionsjson();
              List<AbstractExpression> indexedExprs = null;
              if ( ! expressionjson.isEmpty() ) {
                  try {
                      indexedExprs = AbstractExpression.fromJSONArrayString(expressionjson, tableScan);
                  } catch (JSONException e) {
                      e.printStackTrace();
                      assert(false);
                      return null;
                  }
              }
              // Get source table columns.
              List<Column> srcColumnArray = CatalogUtil.getSortedCatalogItems(srcTable.getColumns(), "index");
              MatViewIndexMatchingGroupby matchingCase = null;
  
              if (groupbyExprs == null) {
                  // This means group-by columns are all simple columns.
                  // It also means we can only access the group-by columns by colref.
                  List<ColumnRef> groupbyColRefs =
                      CatalogUtil.getSortedCatalogItems(matviewinfo.getGroupbycols(), "index");
                  if (indexedExprs == null) {
                      matchingCase = MatViewIndexMatchingGroupby.GB_COL_IDX_COL;
  
                      // All the columns in the index are also simple columns, EASY! colref vs. colref
                      List<ColumnRef> indexedColRefs =
                          CatalogUtil.getSortedCatalogItems(index.getColumns(), "index");
                      // The number of columns in index can never be less than that in the group-by column list.
                      // If minMaxAggExpr == null, they must be equal (diffAllowance == 0)
                      // Otherwise they may be equal (sub-optimal) or
                      // indexedColRefs.size() == groupbyColRefs.size() + 1 (optimal, diffAllowance == 1)
                      if (isInvalidIndexCandidate(indexedColRefs.size(), groupbyColRefs.size(), diffAllowance)) {
                          continue;
                      }
  
                      if (! isGroupbyMatchingIndex(matchingCase, groupbyColRefs, null, indexedColRefs, null, null)) {
                          continue;
                      }
                      if (isValidIndexCandidateForMinMax(indexedColRefs.size(), groupbyColRefs.size(), diffAllowance)) {
                          if(! isIndexOptimalForMinMax(matchingCase, minMaxAggExpr, indexedColRefs, null, srcColumnArray)) {
                              continue;
                          }
                          indexOptimalForMinMax = true;
                      }
                  }
                  else {
                      matchingCase = MatViewIndexMatchingGroupby.GB_COL_IDX_EXP;
                      // In this branch, group-by columns are simple columns, but the index contains complex columns.
                      // So it's only safe to access the index columns from indexedExprs.
                      // You can still get something from indexedColRefs, but they will be inaccurate.
                      // e.g.: ONE index column (a+b) will get you TWO separate entries {a, b} in indexedColRefs.
                      // In order to compare columns: for group-by columns: convert colref => col
                      //                              for    index columns: convert    tve => col
                      if (isInvalidIndexCandidate(indexedExprs.size(), groupbyColRefs.size(), diffAllowance)) {
                          continue;
                      }
  
                      if (! isGroupbyMatchingIndex(matchingCase, groupbyColRefs, null, null, indexedExprs, srcColumnArray)) {
                          continue;
                      }
                      if (isValidIndexCandidateForMinMax(indexedExprs.size(), groupbyColRefs.size(), diffAllowance)) {
                          if(! isIndexOptimalForMinMax(matchingCase, minMaxAggExpr, null, indexedExprs, null)) {
                              continue;
                          }
                          indexOptimalForMinMax = true;
                      }
                  }
              }
              else {
                  matchingCase = MatViewIndexMatchingGroupby.GB_EXP_IDX_EXP;
                  // This means group-by columns have complex columns.
                  // It's only safe to access the group-by columns from groupbyExprs.
                  // AND, indexedExprs must not be null in this case. (yeah!)
                  if ( indexedExprs == null ) {
                      continue;
                  }
                  if (isInvalidIndexCandidate(indexedExprs.size(), groupbyExprs.size(), diffAllowance)) {
                      continue;
                  }
  
                  if (! isGroupbyMatchingIndex(matchingCase, null, groupbyExprs, null, indexedExprs, null)) {
                      continue;
                  }
  
                  if (isValidIndexCandidateForMinMax(indexedExprs.size(), groupbyExprs.size(), diffAllowance)) {
                      if (! isIndexOptimalForMinMax(matchingCase, minMaxAggExpr, null, indexedExprs, null)) {
                          continue;
                      }
                      indexOptimalForMinMax = true;
                  }
              }
  
              // NOW index at least covered all group-by columns (sub-optimal candidate)
              if (!index.getPredicatejson().isEmpty()) {
                  // Additional check for partial indexes to make sure matview WHERE clause
                  // covers the partial index predicate
                  List<AbstractExpression> coveringExprs = new ArrayList<>();
                  List<AbstractExpression> exactMatchCoveringExprs = new ArrayList<>();
                  try {
                      String encodedPredicate = matviewinfo.getPredicate();
                      if (!encodedPredicate.isEmpty()) {
                          String predicate = Encoder.hexDecodeToString(encodedPredicate);
                          AbstractExpression matViewPredicate = AbstractExpression.fromJSONString(predicate, tableScan);
                          coveringExprs.addAll(ExpressionUtil.uncombineAny(matViewPredicate));
                      }
                  } catch (JSONException e) {
                      e.printStackTrace();
                      assert(false);
                      return null;
                  }
                  String predicatejson = index.getPredicatejson();
                  if ( ! predicatejson.isEmpty() &&
                          ! SubPlanAssembler.isPartialIndexPredicateCovered(
                                  tableScan, coveringExprs,
                                  predicatejson, exactMatchCoveringExprs)) {
                      // the partial index predicate does not match the MatView's
                      // where clause -- give up on this index
                      continue;
                  }
              }
              // if the index already covered group by columns and the aggCol/aggExpr,
              // it is already the best index we can get, return immediately.
              if (indexOptimalForMinMax) {
                  return index;
              }
              // otherwise wait to see if we can find something better!
              candidate = index;
          }
          return candidate;
      }