presto-raptor-legacy/src/main/java/io/prestosql/plugin/raptor/legacy/storage/organization/ShardOrganizationManager.java/ShardOrganizationManager/getOverlappingOrganizationSets
class ShardOrganizationManager:    private static Set<OrganizationSet> getOverlappingOrganizationSets(Table tableInfo, Collection<ShardIndexInfo> shards)
    {
        if (shards.size() <= 1) {
            return ImmutableSet.of();
        }

        // Sort by low marker for the range
        List<ShardIndexInfo> sortedShards = shards.stream()
                .sorted((o1, o2) -> {
                    ShardRange sortRange1 = o1.getSortRange().get();
                    ShardRange sortRange2 = o2.getSortRange().get();
                    return ComparisonChain.start()
                            .compare(sortRange1.getMinTuple(), sortRange2.getMinTuple())
                            .compare(sortRange2.getMaxTuple(), sortRange1.getMaxTuple())
                            .result();
                })
                .collect(toList());

        Set<OrganizationSet> organizationSets = new HashSet<>();
        ImmutableSet.Builder<ShardIndexInfo> builder = ImmutableSet.builder();

        builder.add(sortedShards.get(0));
        int previousRange = 0;
        int nextRange = previousRange + 1;
        while (nextRange < sortedShards.size()) {
            ShardRange sortRange1 = sortedShards.get(previousRange).getSortRange().get();
            ShardRange sortRange2 = sortedShards.get(nextRange).getSortRange().get();

            if (sortRange1.overlaps(sortRange2) && !sortRange1.adjacent(sortRange2)) {
                builder.add(sortedShards.get(nextRange));
                if (!sortRange1.encloses(sortRange2)) {
                    previousRange = nextRange;
                }
            }
            else {
                Set<ShardIndexInfo> indexInfos = builder.build();
                if (indexInfos.size() > 1) {
                    organizationSets.add(createOrganizationSet(tableInfo.getTableId(), indexInfos));
                }
                builder = ImmutableSet.builder();
                previousRange = nextRange;
                builder.add(sortedShards.get(previousRange));
            }
            nextRange++;
        }

        Set<ShardIndexInfo> indexInfos = builder.build();
        if (indexInfos.size() > 1) {
            organizationSets.add(createOrganizationSet(tableInfo.getTableId(), indexInfos));
        }
        return organizationSets;
    }