private static boolean checkSupport()

in core/src/main/java/org/apache/calcite/plan/RexImplicationChecker.java [307:375]


  private static boolean checkSupport(InputUsageFinder firstUsageFinder,
      InputUsageFinder secondUsageFinder) {
    final Map<RexInputRef, InputRefUsage<SqlOperator, @Nullable RexNode>> firstUsageMap =
        firstUsageFinder.usageMap;
    final Map<RexInputRef, InputRefUsage<SqlOperator, @Nullable RexNode>> secondUsageMap =
        secondUsageFinder.usageMap;

    for (Map.Entry<RexInputRef, InputRefUsage<SqlOperator, @Nullable RexNode>> entry
        : secondUsageMap.entrySet()) {
      final InputRefUsage<SqlOperator, @Nullable RexNode> secondUsage =
          entry.getValue();
      final PairList<SqlOperator, @Nullable RexNode> secondUsageList =
          secondUsage.usageList;
      final int secondLen = secondUsageList.size();

      if (secondUsage.usageCount != secondLen || secondLen > 2) {
        return false;
      }

      final InputRefUsage<SqlOperator, @Nullable RexNode> firstUsage =
          firstUsageMap.get(entry.getKey());

      if (firstUsage == null
          || firstUsage.usageList.size() != firstUsage.usageCount
          || firstUsage.usageCount > 2) {
        return false;
      }

      final PairList<SqlOperator, @Nullable RexNode> firstUsageList =
          firstUsage.usageList;
      final int firstLen = firstUsageList.size();

      final SqlKind fKind = firstUsageList.get(0).getKey().getKind();
      final SqlKind sKind = secondUsageList.get(0).getKey().getKind();
      final SqlKind fKind2 =
          firstLen == 2 ? firstUsageList.get(1).getKey().getKind() : null;
      final SqlKind sKind2 =
          secondLen == 2 ? secondUsageList.get(1).getKey().getKind() : null;

      // Note: arguments to isEquivalentOp are never null, however checker-framework's
      // dataflow is not strong enough, so the first parameter is marked as nullable
      //noinspection ConstantConditions
      if (firstLen == 2 && secondLen == 2
          && fKind2 != null && sKind2 != null
          && !(isEquivalentOp(fKind, sKind) && isEquivalentOp(fKind2, sKind2))
          && !(isEquivalentOp(fKind, sKind2) && isEquivalentOp(fKind2, sKind))) {
        return false;
      } else if (firstLen == 1 && secondLen == 1
          && fKind != SqlKind.EQUALS && !isSupportedUnaryOperators(sKind)
          && !isEquivalentOp(fKind, sKind)) {
        return false;
      } else if (firstLen == 1 && secondLen == 2 && fKind != SqlKind.EQUALS) {
        return false;
      } else if (firstLen == 2 && secondLen == 1) {
        // Allow only cases like
        // x < 30 and x < 40 implies x < 70
        // x > 30 and x < 40 implies x < 70
        // But disallow cases like
        // x > 30 and x > 40 implies x < 70
        //noinspection ConstantConditions
        if (fKind2 != null && !isOppositeOp(fKind, fKind2) && !isSupportedUnaryOperators(sKind)
            && !(isEquivalentOp(fKind, fKind2) && isEquivalentOp(fKind, sKind))) {
          return false;
        }
      }
    }

    return true;
  }