@Override
    protected IrRowPattern visitIrAlternation(IrAlternation node, Boolean inExclusion) {
      List<IrRowPattern> children =
          node.getPatterns().stream()
              .map(pattern -> process(pattern, inExclusion))
              .collect(toImmutableList());

      // flatten alternation
      children =
          children.stream()
              .flatMap(
                  child -> {
                    if (child instanceof IrAlternation) {
                      return ((IrAlternation) child).getPatterns().stream();
                    }
                    return Stream.of(child);
                  })
              .collect(toImmutableList());

      Optional<IrRowPattern> firstEmptyChild =
          children.stream().filter(IrEmpty.class::isInstance).findFirst();
      if (!firstEmptyChild.isPresent()) {
        return new IrAlternation(children);
      }

      // remove all empty children following the first empty child
      children =
          children.stream()
              .filter(child -> !(child instanceof IrEmpty) || child == firstEmptyChild.get())
              .collect(toImmutableList());

      // if there is only the empty child left, replace alternation with empty pattern
      if (children.size() == 1) {
        return new IrEmpty();
      }

      return new IrAlternation(children);
    }
Flatten the alternation and remove redundant empty branches. This method recursively inlines
sub-pattern lists from child alternations into the top-level alternation. The branch
preference order is reflected in the resulting order of the elements. Examples: A | (B | C)
-> A | B | C (A | B) | ((C | D) | E) -> A | B | C | D | E

<p>Also, redundant empty branches are removed from the resulting sub-pattern list: all empty
branches following the first empty branch are not achievable, as they are less preferred than
the first empty branch, so they are considered redundant. Examples: A | (() | B) -> A | () |
B (A | ()) | ((() | B) | ()) -> A | () | B (() | ()) | () -> ()

<p>Note: The logic of removing redundant empty branches could be extended to remove other
duplicate sub-patterns.

@return the flattened IrAlternation containing at most one empty branch, or IrEmpty in case
    when the alternation is reduced to a single empty branch
iotdb-core/datanode/src/main/java/org/apache/iotdb/db/queryengine/plan/relational/planner/rowpattern/IrRowPatternFlattener.java