/* Copyright (c) 2021, Oracle and/or its affiliates.

   This program is free software; you can redistribute it and/or modify
   it under the terms of the GNU General Public License, version 2.0,
   as published by the Free Software Foundation.

   This program is also distributed with certain software (including
   but not limited to OpenSSL) that is licensed under separate terms,
   as designated in a particular file or component or in included license
   documentation.  The authors of MySQL hereby grant you an additional
   permission to link the program and your derivative works with the
   separately licensed software that they have included with MySQL.

   This program is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   GNU General Public License, version 2.0, for more details.

   You should have received a copy of the GNU General Public License
   along with this program; if not, write to the Free Software
   Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301  USA */

#include "sql/join_optimizer/interesting_orders.h"

#include <algorithm>
#include <functional>
#include <type_traits>

#include "sql/item.h"
#include "sql/item_func.h"
#include "sql/item_sum.h"
#include "sql/join_optimizer/bit_utils.h"
#include "sql/join_optimizer/print_utils.h"
#include "sql/parse_tree_nodes.h"
#include "sql/sql_class.h"

using std::all_of;
using std::bind;
using std::distance;
using std::equal;
using std::fill;
using std::lower_bound;
using std::make_pair;
using std::max;
using std::move;
using std::none_of;
using std::pair;
using std::sort;
using std::string;
using std::swap;
using std::unique;
using std::upper_bound;

namespace {

// Set some maximum limits on the size of the FSMs, in order to prevent runaway
// computation on pathological queries. As rough reference: As of 8.0.26,
// there is a single query in the test suite hitting these limits (it wants 8821
// NFSM states and an estimated 2^50 DFSM states). Excluding that query, the
// test suite contains the following largest FSMs:
//
//  - Largest NFSM: 63 NFSM states => 2 DFSM states
//  - Largest DFSM: 37 NFSM states => 152 DFSM states
//
// And for DBT-3:
//
//  - Largest NFSM: 43 NFSM states => 3 DFSM states
//  - Largest DFSM: 8 NFSM states => 8 DFSM states
//
// We could make system variables out of these if needed, but they would
// probably have to be settable by superusers only, in order to prevent runaway
// unabortable queries from taking down the server. Having them as fixed limits
// is good enough for now.
constexpr int kMaxNFSMStates = 200;
constexpr int kMaxDFSMStates = 2000;

template <class T>
Bounds_checked_array<T> DuplicateArray(THD *thd,
                                       Bounds_checked_array<T> array) {
  static_assert(std::is_pod<T>::value);
  T *items = thd->mem_root->ArrayAlloc<T>(array.size());
  if (!array.empty()) {
    memcpy(items, &array[0], sizeof(*items) * array.size());
  }
  return {items, array.size()};
}

bool OrderingsAreEqual(Ordering a, Ordering b) {
  // Groupings are sorted by item, so this comparison works for both orderings
  // and groupings.
  return equal(a.begin(), a.end(), b.begin(), b.end());
}

}  // namespace

bool IsGrouping(Ordering ordering) {
  return !ordering.empty() && ordering[0].direction == ORDER_NOT_RELEVANT;
}

LogicalOrderings::LogicalOrderings(THD *thd)
    : m_items(thd->mem_root),
      m_orderings(thd->mem_root),
      m_fds(thd->mem_root),
      m_states(thd->mem_root),
      m_edges(thd->mem_root),
      m_dfsm_states(thd->mem_root),
      m_dfsm_edges(thd->mem_root) {
  GetHandle(nullptr);  // Always has the zero handle.

  // Add the empty ordering/grouping.
  m_orderings.push_back(OrderingWithInfo{Ordering{nullptr, 0},
                                         OrderingWithInfo::UNINTERESTING,
                                         /*used_at_end=*/true});

  FunctionalDependency decay_fd;
  decay_fd.type = FunctionalDependency::DECAY;
  decay_fd.tail = 0;
  decay_fd.always_active = true;
  m_fds.push_back(decay_fd);
}

int LogicalOrderings::AddOrderingInternal(THD *thd, Ordering order,
                                          OrderingWithInfo::Type type,
                                          bool used_at_end,
                                          table_map homogenize_tables) {
  assert(!m_built);

#ifndef NDEBUG
  if (IsGrouping(order)) {
    // Verify that the grouping is sorted and deduplicated.
    for (size_t i = 1; i < order.size(); ++i) {
      assert(order[i].item > order[i - 1].item);
      assert(order[i].direction == ORDER_NOT_RELEVANT);
    }

    // Verify that none of the items are of ROW_RESULT,
    // as RemoveDuplicatesIterator cannot handle them.
    // (They would theoretically be fine for orderings.)
    for (size_t i = 0; i < order.size(); ++i) {
      assert(m_items[order[i].item].item->result_type() != ROW_RESULT);
    }
  }
#endif

  if (type != OrderingWithInfo::UNINTERESTING) {
    for (OrderElement element : order) {
      if (element.direction == ORDER_ASC) {
        m_items[element.item].used_asc = true;
      }
      if (element.direction == ORDER_DESC) {
        m_items[element.item].used_desc = true;
      }
      if (element.direction == ORDER_NOT_RELEVANT) {
        m_items[element.item].used_in_grouping = true;
      }
    }
  }

  // Deduplicate against all the existing ones.
  for (size_t i = 0; i < m_orderings.size(); ++i) {
    if (OrderingsAreEqual(m_orderings[i].ordering, order)) {
      // Potentially promote the existing one.
      m_orderings[i].type = std::max(m_orderings[i].type, type);
      m_orderings[i].homogenize_tables |= homogenize_tables;
      return i;
    }
  }
  m_orderings.push_back(OrderingWithInfo{DuplicateArray(thd, order), type,
                                         used_at_end, homogenize_tables});
  m_longest_ordering = std::max<int>(m_longest_ordering, order.size());

  return m_orderings.size() - 1;
}

int LogicalOrderings::AddFunctionalDependency(THD *thd,
                                              FunctionalDependency fd) {
  assert(!m_built);

  // Deduplicate against all the existing ones.
  for (size_t i = 0; i < m_fds.size(); ++i) {
    if (m_fds[i].type != fd.type) {
      continue;
    }
    if (fd.type == FunctionalDependency::EQUIVALENCE) {
      // Equivalences are symmetric.
      if (m_fds[i].head[0] == fd.head[0] && m_fds[i].tail == fd.tail) {
        return i;
      }
      if (m_fds[i].tail == fd.head[0] && m_fds[i].head[0] == fd.tail) {
        return i;
      }
    } else {
      if (m_fds[i].tail == fd.tail &&
          equal(m_fds[i].head.begin(), m_fds[i].head.end(), fd.head.begin(),
                fd.head.end())) {
        return i;
      }
    }
  }
  fd.head = DuplicateArray(thd, fd.head);
  m_fds.push_back(fd);
  return m_fds.size() - 1;
}

void LogicalOrderings::Build(THD *thd, string *trace) {
  BuildEquivalenceClasses();
  RecanonicalizeGroupings();
  AddFDsFromComputedItems(thd);
  AddFDsFromConstItems(thd);
  AddFDsFromAggregateItems(thd);
  PreReduceOrderings(thd);
  CreateOrderingsFromGroupings(thd);
  CreateHomogenizedOrderings(thd);
  PruneFDs(thd);
  if (trace != nullptr) {
    PrintFunctionalDependencies(trace);
  }
  FindElementsThatCanBeAddedByFDs();
  PruneUninterestingOrders(thd);
  if (trace != nullptr) {
    PrintInterestingOrders(trace);
  }
  BuildNFSM(thd);
  if (trace != nullptr) {
    *trace += "NFSM for interesting orders, before pruning:\n";
    PrintNFSMDottyGraph(trace);
    if (m_states.size() >= kMaxNFSMStates) {
      *trace += "NOTE: NFSM is incomplete, because it became too big.\n";
    }
  }
  PruneNFSM(thd);
  if (trace != nullptr) {
    *trace += "\nNFSM for interesting orders, after pruning:\n";
    PrintNFSMDottyGraph(trace);
  }
  ConvertNFSMToDFSM(thd);
  if (trace != nullptr) {
    *trace += "\nDFSM for interesting orders:\n";
    PrintDFSMDottyGraph(trace);
    if (m_dfsm_states.size() >= kMaxDFSMStates) {
      *trace +=
          "NOTE: DFSM does not contain all NFSM states, because it became too "
          "big.\n";
    }
  }
  FindInitialStatesForOrdering();
  m_built = true;
}

LogicalOrderings::StateIndex LogicalOrderings::ApplyFDs(
    LogicalOrderings::StateIndex state_idx, FunctionalDependencySet fds) const {
  for (;;) {  // Termination condition within loop.
    FunctionalDependencySet relevant_fds =
        m_dfsm_states[state_idx].can_use_fd & fds;
    if (relevant_fds.none()) {
      return state_idx;
    }

    // Pick an arbitrary one and follow it. Note that this part assumes
    // kMaxSupportedFDs <= 64.
    static_assert(kMaxSupportedFDs <= sizeof(unsigned long long) * CHAR_BIT);
    int fd_idx = FindLowestBitSet(relevant_fds.to_ullong()) + 1;
    state_idx = m_dfsm_states[state_idx].next_state[fd_idx];

    // Now continue for as long as we have anything to follow;
    // we'll converge on the right answer eventually. Typically,
    // there will be one or two edges to follow, but in extreme cases,
    // there could be O(k²) in the number of FDs.
  }
}

/**
  Try to get rid of uninteresting orders, possibly by discarding irrelevant
  suffixes and merging them with others. In a typical query, this removes a
  large amount of index-created orderings that will never get to something
  interesting, reducing the end FSM size (and thus, reducing the number of
  different access paths we have to keep around).

  This step is the only one that can move orderings around, and thus also
  populates m_optimized_ordering_mapping.
 */
void LogicalOrderings::PruneUninterestingOrders(THD *thd) {
  m_optimized_ordering_mapping =
      Bounds_checked_array<int>::Alloc(thd->mem_root, m_orderings.size());
  int new_length = 0;
  for (size_t ordering_idx = 0; ordering_idx < m_orderings.size();
       ++ordering_idx) {
    if (m_orderings[ordering_idx].type == OrderingWithInfo::UNINTERESTING) {
      Ordering &ordering = m_orderings[ordering_idx].ordering;

      // We are not prepared for uninteresting groupings yet.
      assert(!IsGrouping(ordering));

      // Find the longest prefix that contains only elements that are used in
      // interesting groupings. We will never shorten the uninteresting ordering
      // below this; it is overconservative in some cases, but it makes sure
      // we never miss a path to an interesting grouping.
      size_t minimum_prefix_len = 0;
      while (ordering.size() > minimum_prefix_len &&
             m_items[m_items[ordering[minimum_prefix_len].item].canonical_item]
                 .used_in_grouping) {
        ++minimum_prefix_len;
      }

      // Shorten this ordering one by one element, until it can (heuristically)
      // become an interesting ordering with the FDs we have. Note that it might
      // become the empty ordering, and if so, it will be deleted entirely
      // in the step below.
      while (ordering.size() > minimum_prefix_len &&
             !CouldBecomeInterestingOrdering(ordering)) {
        ordering.resize(ordering.size() - 1);
      }
    }

    // Since some orderings may have changed, we need to re-deduplicate.
    // Note that at this point, we no longer care about used_at_end;
    // it was only used for reducing orderings in homogenization.
    m_optimized_ordering_mapping[ordering_idx] = new_length;
    for (int i = 0; i < new_length; ++i) {
      if (OrderingsAreEqual(m_orderings[i].ordering,
                            m_orderings[ordering_idx].ordering)) {
        m_optimized_ordering_mapping[ordering_idx] = i;
        m_orderings[i].type =
            std::max(m_orderings[i].type, m_orderings[ordering_idx].type);
        break;
      }
    }
    if (m_optimized_ordering_mapping[ordering_idx] == new_length) {
      // Not a duplicate of anything earlier, so keep it.
      m_orderings[new_length++] = m_orderings[ordering_idx];
    }
  }
  m_orderings.resize(new_length);
}

void LogicalOrderings::PruneFDs(THD *thd) {
  // The definition of prunable FDs in the papers seems to be very abstract
  // and not practically realizable, so we use a simple heuristic instead:
  // A FD is useful iff it produces an item that is part of some ordering.
  // Discard all non-useful FDs. (Items not part of some ordering will cause
  // the new proposed ordering to immediately be pruned away, so this is
  // safe. See also the comment in the .h file about transitive dependencies.)
  //
  // Note that this will sometimes leave useless FDs; if we have e.g. a → b
  // and b is useful, we will mark the FD as useful even if nothing can
  // produce a. However, such FDs don't induce more NFSM states (which is
  // the main point of the pruning), it just slows the NFSM down slightly,
  // and by far the dominant FDs to prune in our cases are the ones
  // induced by keys, e.g. S → k where S is always the same and k
  // is non-useful. These are caught by this heuristic.

  m_optimized_fd_mapping =
      Bounds_checked_array<int>::Alloc(thd->mem_root, m_fds.size());
  size_t old_length = m_fds.size();

  // We always need to keep the decay FD, so start at 1.
  m_optimized_fd_mapping[0] = 0;
  int new_length = 1;

  for (size_t fd_idx = 1; fd_idx < old_length; ++fd_idx) {
    const FunctionalDependency &fd = m_fds[fd_idx];

    // See if we this FDs is useful, ie., can produce an item used in an
    // ordering.
    bool used_fd = false;
    ItemHandle tail = m_items[fd.tail].canonical_item;
    if (m_items[tail].used_asc || m_items[tail].used_desc ||
        m_items[tail].used_in_grouping) {
      used_fd = true;
    } else if (fd.type == FunctionalDependency::EQUIVALENCE) {
      ItemHandle head = m_items[fd.head[0]].canonical_item;
      if (m_items[head].used_asc || m_items[head].used_desc ||
          m_items[head].used_in_grouping) {
        used_fd = true;
      }
    }

    if (!used_fd) {
      m_optimized_fd_mapping[fd_idx] = -1;
      continue;
    }

    if (m_fds[fd_idx].always_active) {
      // Defer these for now, by moving them to the end. We will need to keep
      // them in the array so that we can apply them under FSM construction,
      // but they should not get a FD bitmap, and thus also not priority for
      // the lowest index. We could have used a separate array, but the m_fds
      // array probably already has the memory.
      m_optimized_fd_mapping[fd_idx] = -1;
      m_fds.push_back(m_fds[fd_idx]);
    } else {
      m_optimized_fd_mapping[fd_idx] = new_length;
      m_fds[new_length++] = m_fds[fd_idx];
    }
  }

  // Now include the always-on FDs we deferred earlier.
  for (size_t fd_idx = old_length; fd_idx < m_fds.size(); ++fd_idx) {
    m_fds[new_length++] = m_fds[fd_idx];
  }

  m_fds.resize(new_length);
}

void LogicalOrderings::BuildEquivalenceClasses() {
  for (size_t i = 0; i < m_items.size(); ++i) {
    m_items[i].canonical_item = i;
  }

  // In the worst case, for n items, all equal, m FDs ordered optimally bad,
  // this algorithm is O(nm) (all items shifted one step down each loop).
  // In practice, it should be much better.
  bool done_anything;
  do {
    done_anything = false;
    for (const FunctionalDependency &fd : m_fds) {
      if (fd.type != FunctionalDependency::EQUIVALENCE) {
        continue;
      }
      ItemHandle left_item = fd.head[0];
      ItemHandle right_item = fd.tail;

      if (m_items[left_item].canonical_item ==
          m_items[right_item].canonical_item) {
        // Already fully applied.
        continue;
      }

      // Merge the classes so that the lowest index always is the canonical one
      // of its equivalence class.
      ItemHandle canonical_item, duplicate_item;
      if (m_items[right_item].canonical_item <
          m_items[left_item].canonical_item) {
        canonical_item = m_items[right_item].canonical_item;
        duplicate_item = left_item;
      } else {
        canonical_item = m_items[left_item].canonical_item;
        duplicate_item = right_item;
      }
      m_items[duplicate_item].canonical_item = canonical_item;
      m_items[canonical_item].used_asc |= m_items[duplicate_item].used_asc;
      m_items[canonical_item].used_desc |= m_items[duplicate_item].used_desc;
      m_items[canonical_item].used_in_grouping |=
          m_items[duplicate_item].used_in_grouping;
      done_anything = true;
    }
  } while (done_anything);
}

// Put all groupings into a canonical form that we can compare them
// as orderings without further logic. (It needs to be on a form that
// does not change markedly after applying equivalences, and it needs
// to be deterministic, but apart from that, the order is pretty arbitrary.)
// We can only do this after BuildEquivalenceClasses().
void LogicalOrderings::RecanonicalizeGroupings() {
  for (OrderingWithInfo &ordering : m_orderings) {
    if (IsGrouping(ordering.ordering)) {
      sort(ordering.ordering.begin(), ordering.ordering.end(),
           [this](const OrderElement &a, const OrderElement &b) {
             return ItemBeforeInGroup(a, b);
           });
    }
  }
}

// Window functions depend on both the function argument and on the PARTITION BY
// clause, so we need to add both to the functional dependency's head.
// The order of elements is arbitrary.
Bounds_checked_array<ItemHandle>
LogicalOrderings::CollectHeadForStaticWindowFunction(THD *thd,
                                                     ItemHandle argument_item,
                                                     Window *window) {
  const PT_order_list *partition_by = window->effective_partition_by();
  int partition_len = 0;
  if (partition_by != nullptr) {
    for (ORDER *order = partition_by->value.first; order != nullptr;
         order = order->next) {
      ++partition_len;
    }
  }
  auto head =
      Bounds_checked_array<ItemHandle>::Alloc(thd->mem_root, partition_len + 1);
  if (partition_by != nullptr) {
    for (ORDER *order = partition_by->value.first; order != nullptr;
         order = order->next) {
      head[partition_len--] = GetHandle(*order->item);
    }
  }
  head[0] = argument_item;
  return head;
}

/**
  Try to add new FDs from items that are not base items; e.g., if we have
  an item (a + 1), we add {a} → (a + 1) (since addition is deterministic).
  This can help reducing orderings that are on such derived items.
  For simplicity, we only bother doing this for items that derive from a
  single base field; i.e., from (a + b), we don't add {a,b} → (a + b)
  even though we could. Also note that these are functional dependencies,
  not equivalences; even though ORDER BY (a + 1) could be satisfied by an
  ordering on (a) (barring overflow issues), this does not hold in general,
  e.g. ORDER BY (-a) is _not_ satisfied by an ordering on (a), not to mention
  ORDER BY (a*a). We do not have the framework in Item to understand which
  functions are monotonous, so we do not attempt to create equivalences.

  This is really the only the case where we can get transitive FDs that are not
  equivalences. Since our approach does not apply FDs transitively without
  adding the intermediate item (e.g., for {a} → b and {b} → c, we won't extend
  (a) to (ac), only to (abc)), we extend any existing FDs here when needed.
 */
void LogicalOrderings::AddFDsFromComputedItems(THD *thd) {
  int num_original_items = m_items.size();
  int num_original_fds = m_fds.size();
  for (int item_idx = 0; item_idx < num_original_items; ++item_idx) {
    // We only care about items that are used in some ordering,
    // not any used as base in FDs or the likes.
    const ItemHandle canonical_idx = m_items[item_idx].canonical_item;
    if (!m_items[canonical_idx].used_asc && !m_items[canonical_idx].used_desc &&
        !m_items[canonical_idx].used_in_grouping) {
      continue;
    }

    // We only want to look at items that are not already Item_field
    // or aggregate functions (the latter are handled in
    // AddFDsFromAggregateItems()), and that are generated from a single field.
    // Some quick heuristics will eliminate most of these for us.
    Item *item = m_items[item_idx].item;
    const table_map used_tables = item->used_tables();
    if (item->type() == Item::FIELD_ITEM || item->has_aggregation() ||
        used_tables == 0 || Overlaps(used_tables, PSEUDO_TABLE_BITS) ||
        !IsSingleBitSet(used_tables)) {
      continue;
    }

    // Window functions have much more state than just the parameter,
    // so we cannot say that e.g. {a} → SUM(a) OVER (...), unless we
    // know that the function is over the entire frame (unbounded).
    //
    // TODO(sgunders): We could also add FDs for window functions
    // where could guarantee that the partition is only one row.
    bool is_static_wf = false;
    if (item->has_wf()) {
      if (item->m_is_window_function &&
          down_cast<Item_sum *>(item)->framing() &&
          down_cast<Item_sum *>(item)->window()->static_aggregates()) {
        is_static_wf = true;
      } else {
        continue;
      }
    }

    Item_field *base_field = nullptr;
    bool error =
        WalkItem(item, enum_walk::POSTFIX, [&base_field](Item *sub_item) {
          if (sub_item->type() == Item::FUNC_ITEM &&
              down_cast<Item_func *>(sub_item)->functype() ==
                  Item_func::ROLLUP_GROUP_ITEM_FUNC) {
            // Rollup items are nondeterministic, yet don't always set
            // RAND_TABLE_BIT.
            return true;
          }
          if (sub_item->type() == Item::FIELD_ITEM) {
            if (base_field != nullptr &&
                !base_field->eq(sub_item, /*binary_cmp=*/true)) {
              // More than one field in use.
              return true;
            }
            base_field = down_cast<Item_field *>(sub_item);
          }
          return false;
        });
    if (error || base_field == nullptr) {
      // More than one field in use, or no fields in use
      // (can happen even when used_tables is set, e.g. for
      // an Item_view_ref to a constant).
      continue;
    }

    if (!base_field->field->binary()) {
      // Fields with collations can have equality (with no tiebreaker)
      // even with fields that contain differing binary data.
      // Thus, functions do not always preserve equality; a == b
      // does not mean f(a) == f(b), and thus, the FD does not
      // hold either.
      continue;
    }

    ItemHandle head_item = GetHandle(base_field);
    FunctionalDependency fd;
    fd.type = FunctionalDependency::FD;
    if (is_static_wf) {
      fd.head = CollectHeadForStaticWindowFunction(
          thd, head_item, down_cast<Item_sum *>(item)->window());
    } else {
      fd.head = Bounds_checked_array<ItemHandle>(&head_item, 1);
    }
    fd.tail = item_idx;
    fd.always_active = true;
    AddFunctionalDependency(thd, fd);

    if (fd.head.size() == 1) {
      // Extend existing FDs transitively (see function comment).
      // E.g. if we have S → base, also add S → item.
      for (int fd_idx = 0; fd_idx < num_original_fds; ++fd_idx) {
        if (m_fds[fd_idx].type == FunctionalDependency::FD &&
            m_fds[fd_idx].tail == head_item && m_fds[fd_idx].always_active) {
          fd = m_fds[fd_idx];
          fd.tail = item_idx;
          AddFunctionalDependency(thd, fd);
        }
      }
    }
  }
}

/**
  Try to add FDs from items that are constant by themselves, e.g. if someone
  does ORDER BY 'x', add a new FD {} → 'x' so that the ORDER BY can be elided.

  TODO(sgunders): This can potentially remove subqueries or other functions
  that would throw errors if actually executed, potentially modifying
  semantics. See if that is illegal, and thus, if we need to test-execute them
  at least once somehow (ideally not during optimization).
 */
void LogicalOrderings::AddFDsFromConstItems(THD *thd) {
  int num_original_items = m_items.size();
  for (int item_idx = 0; item_idx < num_original_items; ++item_idx) {
    // We only care about items that are used in some ordering,
    // not any used as base in FDs or the likes.
    const ItemHandle canonical_idx = m_items[item_idx].canonical_item;
    if (!m_items[canonical_idx].used_asc && !m_items[canonical_idx].used_desc &&
        !m_items[canonical_idx].used_in_grouping) {
      continue;
    }

    if (m_items[item_idx].item->const_for_execution()) {
      // Add {} → item.
      FunctionalDependency fd;
      fd.type = FunctionalDependency::FD;
      fd.head = Bounds_checked_array<ItemHandle>();
      fd.tail = item_idx;
      fd.always_active = true;
      AddFunctionalDependency(thd, fd);
    }
  }
}

void LogicalOrderings::AddFDsFromAggregateItems(THD *thd) {
  // If ROLLUP is active, and we have nullable GROUP BY expressions, we could
  // get two different NULL groups with different aggregates; one for the actual
  // NULL value, and one for the rollup group. If so, these FDs no longer hold,
  // and we cannot add them.
  if (m_rollup) {
    for (ItemHandle item : m_aggregate_head) {
      if (m_items[item].item->is_nullable()) {
        return;
      }
    }
  }

  int num_original_items = m_items.size();
  for (int item_idx = 0; item_idx < num_original_items; ++item_idx) {
    // We only care about items that are used in some ordering,
    // not any used as base in FDs or the likes.
    const ItemHandle canonical_idx = m_items[item_idx].canonical_item;
    if (!m_items[canonical_idx].used_asc && !m_items[canonical_idx].used_desc &&
        !m_items[canonical_idx].used_in_grouping) {
      continue;
    }

    if (m_items[item_idx].item->has_aggregation() &&
        !m_items[item_idx].item->has_wf()) {
      // Add {all GROUP BY items} → item.
      // Note that the head might be empty, for implicit grouping,
      // which means all aggregate items are constant (there is only one row).
      FunctionalDependency fd;
      fd.type = FunctionalDependency::FD;
      fd.head = m_aggregate_head;
      fd.tail = item_idx;
      fd.always_active = true;
      AddFunctionalDependency(thd, fd);
    }
  }
}

void LogicalOrderings::FindElementsThatCanBeAddedByFDs() {
  for (const FunctionalDependency &fd : m_fds) {
    m_items[m_items[fd.tail].canonical_item].can_be_added_by_fd = true;
    if (fd.type == FunctionalDependency::EQUIVALENCE) {
      m_items[m_items[fd.head[0]].canonical_item].can_be_added_by_fd = true;
    }
  }
}

/**
  Does the element already exist in given ordering? Unlike
  ImpliedByEarlierElements, only counts literal item duplicates, not items that
  are redundant due to functional dependencies.
 */
static bool Contains(Ordering prefix, ItemHandle item) {
  for (OrderElement elem : prefix) {
    if (elem.item == item) {
      // ASC/DESC doesn't matter, the second item is redundant even on a
      // mismatch.
      return true;
    }
  }
  return false;
}

/**
  Checks whether the given item is redundant given previous elements in
  the ordering; ie., whether adding it will never change the ordering.
  This could either be because it's a duplicate, or because it is implied
  by functional dependencies. When this is applied to all elements in turn,
  it is called “reducing” the ordering. [Neu04] claims that this operation
  is not confluent, which is erroneous (their example is faulty, ignoring
  that Simmen reduces from the back). [Neu04b] has modified the claim to
  be that it is not confluent for _groupings_, which is correct.
  We make no attempt at optimality.

  If all_fds is true, we consider all functional dependencies, including those
  that may not always be active; e.g. a FD a=b may come from a join, and thus
  does not hold before the join is actually done, but we assume it holds anyway.
  This is OK from order homogenization, which is concerned with making orderings
  that will turn into the desired interesting ordering (e.g. for ORDER BY) only
  after all joins have been done. It would not be OK if we were to use it for
  merge joins somehow.
 */
bool LogicalOrderings::ImpliedByEarlierElements(ItemHandle item,
                                                Ordering prefix,
                                                bool all_fds) const {
  // First, search for straight-up duplicates (ignoring ASC/DESC).
  if (Contains(prefix, item)) {
    return true;
  }

  // Check if this item is implied by any of the functional dependencies.
  for (size_t fd_idx = 1; fd_idx < m_fds.size(); ++fd_idx) {
    const FunctionalDependency &fd = m_fds[fd_idx];
    if (!all_fds && !fd.always_active) {
      continue;
    }
    if (fd.type == FunctionalDependency::FD) {
      if (fd.tail != item) {
        continue;
      }

      // Check if we have all the required head items.
      bool all_found = true;
      for (ItemHandle other_item : fd.head) {
        if (!Contains(prefix, other_item)) {
          all_found = false;
          break;
        }
      }
      if (all_found) {
        return true;
      }
    } else {
      // a = b implies that a → b and b → a, so we check for both of those.
      assert(fd.type == FunctionalDependency::EQUIVALENCE);
      assert(fd.head.size() == 1);
      if (fd.tail == item && Contains(prefix, fd.head[0])) {
        return true;
      }
      if (fd.head[0] == item && Contains(prefix, fd.tail)) {
        return true;
      }
    }
  }
  return false;
}

/**
  Do safe reduction on all orderings (some of them may get merged by
  PruneUninterestingOrders() later), ie., remove all items that may be removed
  using only FDs that always are active.

  There's a problem in [Neu04] that is never adequately addressed; orderings are
  only ever expanded, and then eventually compared against interesting orders.
  But the interesting order itself is not necessarily extended, due to pruning.
  For instance, if an index could yield (x,y) and we have {} → x, there's no way
  we could get it to match the interesting order (y) even though they are
  logically equivalent. For an even trickier case, imagine an index (x,y) and
  an interesting order (y,z), with {} → x and y → z. For this to match, we'd
  need to have a “super-order” (x,y,z) and infer that from both orderings.

  Instead, we do a pre-step related to Simmen's “Test Ordering” procedure;
  we reduce the orderings. In the example above, both will be reduced to (y),
  and then match. This is mostly a band-aid around the problem; for instance,
  it cannot deal with FDs that are not always active, and it does not deal
  adequately with groupings (since reduction does not).

  Note that this could make the empty ordering interesting after merging.
 */
void LogicalOrderings::PreReduceOrderings(THD *thd) {
  OrderElement *tmpbuf =
      thd->mem_root->ArrayAlloc<OrderElement>(m_longest_ordering);
  for (OrderingWithInfo &ordering : m_orderings) {
    Ordering reduced_ordering = ReduceOrdering(ordering.ordering,
                                               /*all_fds=*/false, tmpbuf);
    if (reduced_ordering.size() < ordering.ordering.size()) {
      ordering.ordering = DuplicateArray(thd, reduced_ordering);
    }
  }
}

/**
  We don't currently have any operators that only group and do not sort
  (e.g. hash grouping), so we always implement grouping by sorting.
  This function makes that representation explicit -- for each grouping,
  it will make sure there is at least one ordering representing that
  grouping. This means we never need to “sort by a grouping”, which
  would destroy ordering information that could be useful later.

  As an example, take SELECT ... GROUP BY a, b ORDER BY a. This needs to
  group first by {a,b} (assume we're using filesort, not an index),
  then sort by (a). If we just represent the sort we're doing as going
  directly to {a,b}, we can't elide the sort on (a). Instead, we create
  a sort (a,b) (implicitly convertible to {a,b}), which makes the FSM
  understand that we're _both_ sorted on (a,b) and grouped on {a,b},
  and then also sorted on (a).

  Any given grouping would be satisfied by lots of different orderings:
  {a,b} could be (a,b), (b,a), (a DESC, b) etc.. We look through all
  interesting orders that are a subset of our grouping, and if they are,
  we extend them arbitrarily to complete the grouping. E.g., if our
  grouping is {a,b,c,d} and the ordering (c DESC, b) is interesting,
  we make a homogenized ordering (c DESC, b, a, d). This is roughly
  equivalent to Simmen's “Cover Order” procedure. If we cannot make
  such a cover, we simply make a new last-resort ordering (a,b,c,d).

  We don't consider equivalences here; perhaps we should, at least
  for at-end groupings.
 */
void LogicalOrderings::CreateOrderingsFromGroupings(THD *thd) {
  OrderElement *tmpbuf =
      thd->mem_root->ArrayAlloc<OrderElement>(m_longest_ordering);

  int num_original_orderings = m_orderings.size();
  for (int grouping_idx = 1; grouping_idx < num_original_orderings;
       ++grouping_idx) {
    Ordering grouping = m_orderings[grouping_idx].ordering;
    if (!IsGrouping(grouping) ||
        m_orderings[grouping_idx].type != OrderingWithInfo::INTERESTING) {
      continue;
    }

    bool has_cover = false;
    for (int ordering_idx = 1; ordering_idx < num_original_orderings;
         ++ordering_idx) {
      Ordering ordering = m_orderings[ordering_idx].ordering;
      if (IsGrouping(ordering) ||
          m_orderings[ordering_idx].type != OrderingWithInfo::INTERESTING ||
          ordering.size() > grouping.size()) {
        continue;
      }
      bool can_cover = all_of(ordering.begin(), ordering.end(),
                              [&grouping](const OrderElement &element) {
                                return Contains(grouping, element.item);
                              });
      if (!can_cover) {
        continue;
      }

      has_cover = true;

      // On a full match, just note that we have a cover, don't make a new
      // ordering. We assume both are free of duplicates.
      if (ordering.size() == grouping.size()) {
        continue;
      }

      for (size_t i = 0; i < ordering.size(); ++i) {
        tmpbuf[i] = ordering[i];
      }
      int len = ordering.size();
      for (const OrderElement &element : grouping) {
        if (!Contains(ordering, element.item)) {
          tmpbuf[len].item = element.item;
          tmpbuf[len].direction = ORDER_ASC;  // Arbitrary.
          ++len;
        }
      }
      assert(len == static_cast<int>(grouping.size()));

      AddOrderingInternal(thd, Ordering(tmpbuf, len),
                          OrderingWithInfo::HOMOGENIZED,
                          m_orderings[grouping_idx].used_at_end,
                          /*homogenize_tables=*/0);
    }

    // Make a fallback ordering if no cover was found.
    if (!has_cover) {
      for (size_t i = 0; i < grouping.size(); ++i) {
        tmpbuf[i].item = grouping[i].item;
        tmpbuf[i].direction = ORDER_ASC;  // Arbitrary.
      }

      AddOrderingInternal(thd, Ordering(tmpbuf, grouping.size()),
                          OrderingWithInfo::HOMOGENIZED,
                          m_orderings[grouping_idx].used_at_end,
                          /*homogenize_tables=*/0);
    }
  }
}

/**
  For each interesting ordering, see if we can homogenize it onto each table.
  A homogenized ordering is one that refers to fewer tables than the original
  one -- in our case, a single table. (If we wanted to, we could homogenize down
  to sets of tables instead of single tables only. However, that would open up
  for O(2^n) orderings, so we restrict to single-table.)

  The idea is to enable sort-ahead; find an ordering we can sort a single table
  in that, after later applying functional dependencies, eventually gives the
  desired ordering. This is just a heuristic (in particular, we only consider
  equivalences, not other functional dependencies), but in most cases will give
  us an ordering if any exist.

  Neumann et al do not talk much about this, so this comes from the Simmen
  paper, where it is called “Homogenize Order”.
 */
void LogicalOrderings::CreateHomogenizedOrderings(THD *thd) {
  // Collect all tables we have seen referred to in items. (Actually, we could
  // limit ourselves to the ones we've seen in functional dependencies, but this
  // is simpler.)
  table_map seen_tables = 0;
  for (const ItemInfo &item : m_items) {
    if (item.item != nullptr) {
      seen_tables |= item.item->used_tables();
    }
  }
  seen_tables &= ~PSEUDO_TABLE_BITS;

  // Build a reverse table of canonical items to items,
  // and sort it, so that we can fairly efficiently make lookups into it.
  auto reverse_canonical =
      Bounds_checked_array<pair<ItemHandle, ItemHandle>>::Alloc(thd->mem_root,
                                                                m_items.size());
  for (size_t item_idx = 0; item_idx < m_items.size(); ++item_idx) {
    reverse_canonical[item_idx].first = m_items[item_idx].canonical_item;
    reverse_canonical[item_idx].second = item_idx;
  }
  sort(reverse_canonical.begin(), reverse_canonical.end());

  OrderElement *tmpbuf =
      thd->mem_root->ArrayAlloc<OrderElement>(m_longest_ordering);
  OrderElement *tmpbuf2 =
      thd->mem_root->ArrayAlloc<OrderElement>(m_longest_ordering);

  // Now, for each table, try to see if we can rewrite an ordering
  // to something only referring to that table, by swapping out non-conforming
  // items for others.
  int num_original_orderings = m_orderings.size();
  for (int ordering_idx = 1; ordering_idx < num_original_orderings;
       ++ordering_idx) {
    if (m_orderings[ordering_idx].type == OrderingWithInfo::UNINTERESTING) {
      continue;
    }
    if (IsGrouping(m_orderings[ordering_idx].ordering)) {
      // We've already made orderings out of these, which will be
      // homogenized, so we don't need to homogenize the grouping itself,
      // too.
      continue;
    }
    Ordering reduced_ordering = ReduceOrdering(
        m_orderings[ordering_idx].ordering,
        /*all_fds=*/m_orderings[ordering_idx].used_at_end, tmpbuf);
    if (reduced_ordering.empty()) {
      continue;
    }

    // Now try to homogenize it onto all tables in turn.
    table_map homogenize_tables;
    if (m_orderings[ordering_idx].used_at_end) {
      // Try all tables.
      homogenize_tables = seen_tables;
    } else {
      // Try only the ones we were asked to (because it's not relevant
      // for later tables anyway).
      homogenize_tables = m_orderings[ordering_idx].homogenize_tables;
    }
    for (int table_idx : BitsSetIn(homogenize_tables)) {
      AddHomogenizedOrderingIfPossible(thd, reduced_ordering,
                                       m_orderings[ordering_idx].used_at_end,
                                       table_idx, reverse_canonical, tmpbuf2);
    }
  }
}

/**
  Remove redundant elements using the functional dependencies that we have,
  to give a more canonical form before homogenization. Note that we assume
  here that every functional dependency holds, so this is not applicable
  generally throughout the tree, only at the end (e.g. final ORDER BY).
  This is called “Reduce Order” in the Simmen paper.

  tmpbuf is used as the memory store for the new ordering.
 */
Ordering LogicalOrderings::ReduceOrdering(Ordering ordering, bool all_fds,
                                          OrderElement *tmpbuf) const {
  size_t reduced_length = 0;
  for (size_t part_idx = 0; part_idx < ordering.size(); ++part_idx) {
    if (ImpliedByEarlierElements(ordering[part_idx].item,
                                 ordering.prefix(part_idx), all_fds)) {
      // Delete this element.
    } else {
      tmpbuf[reduced_length++] = ordering[part_idx];
    }
  }
  return {tmpbuf, reduced_length};
}

/// Helper function for CreateHomogenizedOrderings().
void LogicalOrderings::AddHomogenizedOrderingIfPossible(
    THD *thd, Ordering reduced_ordering, bool used_at_end, int table_idx,
    Bounds_checked_array<pair<ItemHandle, ItemHandle>> reverse_canonical,
    OrderElement *tmpbuf) {
  const table_map available_tables = table_map{1} << table_idx;
  int length = 0;

  for (OrderElement element : reduced_ordering) {
    if (IsSubset(m_items[element.item].item->used_tables(), available_tables)) {
      // Already OK.
      if (!ImpliedByEarlierElements(element.item, Ordering(tmpbuf, length),
                                    /*all_fds=*/used_at_end)) {
        tmpbuf[length++] = element;
      }
      continue;
    }

    // Find all equivalent items.
    ItemHandle canonical_item = m_items[element.item].canonical_item;
    auto first = lower_bound(reverse_canonical.begin(), reverse_canonical.end(),
                             canonical_item,
                             [](const pair<ItemHandle, ItemHandle> &a,
                                ItemHandle b) { return a.first < b; });
    auto last =
        upper_bound(first, reverse_canonical.end(), canonical_item,
                    [](ItemHandle a, const pair<ItemHandle, ItemHandle> &b) {
                      return a < b.first;
                    });
    assert(last - first >= 1);

    bool found = false;
    for (auto it = first; it != last; ++it) {
      if (IsSubset(m_items[it->second].item->used_tables(), available_tables)) {
        if (ImpliedByEarlierElements(it->second, Ordering(tmpbuf, length),
                                     /*all_fds=*/used_at_end)) {
          // Unneeded in the new order, so delete it.
          // Similar to the reduction process above.
        } else {
          tmpbuf[length].item = it->second;
          tmpbuf[length].direction = element.direction;
          ++length;
        }
        found = true;
        break;
      }
    }
    if (!found) {
      // Not possible to homogenize this ordering.
      return;
    }
  }

  if (IsGrouping(reduced_ordering)) {
    // We've replaced some items, so we need to re-sort.
    sort(tmpbuf, tmpbuf + length,
         [this](const OrderElement &a, const OrderElement &b) {
           return ItemBeforeInGroup(a, b);
         });
  }

  AddOrderingInternal(thd, Ordering(tmpbuf, length),
                      OrderingWithInfo::HOMOGENIZED, used_at_end,
                      /*homogenize_tables=*/0);
}

ItemHandle LogicalOrderings::GetHandle(Item *item) {
  for (size_t i = 1; i < m_items.size(); ++i) {
    if (item == m_items[i].item ||
        item->eq(m_items[i].item, /*binary_cmp=*/true)) {
      return i;
    }
  }
  m_items.push_back(ItemInfo{item, /*canonical_item=*/0});
  return m_items.size() - 1;
}

/**
  For a given ordering, check whether it ever has the hope of becoming an
  interesting ordering. In its base form, this is a prefix check; if we
  have an ordering (a,b) and an interesting order (a,b,c), it passes.
  However, we add some slightly more lax heuristics in order to make the
  graph a bit wider at build time (and thus require fewer FD applications at
  runtime); namely, if there's a prefix mismatch but the item could be added
  by some FD later (without the ordering becoming too long), we let it slide
  and just skip that item.

  E.g.: If we have an ordering (a,b) and an interesting order (a,x,b),
  we first match a. x does not match b, but we check whether x is ever on the
  right side of any FD (for instance because there might be an FD a → x).
  If it is, we skip it and match b with b. There's an example of this in the
  DoesNotStrictlyPruneOnPrefixes unit test.

  Obviously, this leads to false positives, but that is fine; this is just
  to prune down the amount of states in the NFSM. [Neu04] points out that
  such pruning is pretty much essential for performance, and our experience
  is the same.

  There is one extra quirk; the prefix check needs to take equivalences into
  account, or we would prune away orderings that could become interesting
  after equivalences. We solve this by always mapping to an equivalence class
  when doing the prefix comparison. There's an example of this in the
  TwoEquivalences unit test.
 */
bool LogicalOrderings::CouldBecomeInterestingOrdering(Ordering ordering) const {
  for (OrderingWithInfo other_ordering : m_orderings) {
    const Ordering interesting_ordering = other_ordering.ordering;
    if (other_ordering.type != OrderingWithInfo::INTERESTING ||
        interesting_ordering.size() < ordering.size()) {
      continue;
    }

    // Groupings can never become orderings. Orderings can become groupings,
    // but for simplicity, we require them to immediately become groupings then,
    // or else be pruned away.
    if (IsGrouping(ordering) != IsGrouping(interesting_ordering)) {
      continue;
    }

    // Since groupings are ordered by item (actually canonical item;
    // see RecanonicalizeGroupings(), ItemBeforeInGroup() and
    // the GroupReordering unit test), we can use the same comparison
    // for ordering-ordering and grouping-grouping comparisons.
    bool match = true;
    for (size_t i = 0, j = 0;
         i < ordering.size() || j < interesting_ordering.size();) {
      if (ordering.size() - i > interesting_ordering.size() - j) {
        // We have excess items at the end, so give up.
        match = false;
        break;
      }

      const ItemHandle needed_item =
          m_items[interesting_ordering[j].item].canonical_item;
      if (i < ordering.size() &&
          m_items[ordering[i].item].canonical_item == needed_item &&
          ordering[i].direction == interesting_ordering[j].direction) {
        // We have a matching item, so move both iterators along.
        ++i, ++j;
        continue;
      }

      if (m_items[needed_item].can_be_added_by_fd) {
        // We don't have this item, but it could be generated, so skip it.
        ++j;
        continue;
      }

      // We don't have this item, and it can not be added later, so give up.
      match = false;
      break;
    }
    if (match) {
      return true;
    }
  }
  return false;
}

int LogicalOrderings::AddArtificialState(THD *thd, Ordering ordering) {
  for (size_t i = 0; i < m_states.size(); ++i) {
    if (OrderingsAreEqual(m_states[i].satisfied_ordering, ordering)) {
      return i;
    }
  }

  NFSMState state;
  state.satisfied_ordering = DuplicateArray(thd, ordering);
  state.satisfied_ordering_idx = -1;  // Irrelevant, but placate the compiler.
  state.outgoing_edges.init(thd->mem_root);
  state.type = NFSMState::ARTIFICIAL;
  m_states.push_back(move(state));
  return m_states.size() - 1;
}

void LogicalOrderings::AddEdge(THD *thd, int state_idx, int required_fd_idx,
                               Ordering ordering) {
  NFSMEdge edge;
  edge.required_fd_idx = required_fd_idx;
  edge.state_idx = AddArtificialState(thd, ordering);

  if (edge.state_idx == state_idx) {
    // Don't add self-edges; they are already implicit.
    return;
  }

  m_edges.push_back(edge);
  m_states[state_idx].outgoing_edges.push_back(m_edges.size() - 1);
}

bool LogicalOrderings::FunctionalDependencyApplies(
    const FunctionalDependency &fd, const Ordering ordering,
    int *start_point) const {
  assert(fd.type != FunctionalDependency::DECAY);
  *start_point = -1;
  for (ItemHandle head_item : fd.head) {
    bool matched = false;
    for (size_t i = 0; i < ordering.size(); ++i) {
      if (ordering[i].item == head_item ||
          (fd.type == FunctionalDependency::EQUIVALENCE &&
           ordering[i].item == fd.tail)) {
        *start_point = max<int>(*start_point, i);
        matched = true;
        break;
      }
    }
    if (!matched) {
      return false;
    }
  }
  return true;
}

/**
  Remove duplicate entries from an ordering, in-place.
 */
static void DeduplicateOrdering(Ordering *ordering) {
  size_t length = 0;
  for (size_t i = 0; i < ordering->size(); ++i) {
    if (!Contains(ordering->prefix(length), (*ordering)[i].item)) {
      (*ordering)[length++] = (*ordering)[i];
    }
  }
  ordering->resize(length);
}

void LogicalOrderings::BuildNFSM(THD *thd) {
  // Add a state for each producable ordering.
  for (size_t i = 0; i < m_orderings.size(); ++i) {
    NFSMState state;
    state.satisfied_ordering = m_orderings[i].ordering;
    state.satisfied_ordering_idx = i;
    state.outgoing_edges.init(thd->mem_root);
    state.type = m_orderings[i].type == OrderingWithInfo::INTERESTING
                     ? NFSMState::INTERESTING
                     : NFSMState::ARTIFICIAL;
    m_states.push_back(move(state));
  }

  // Add an edge from the initial state to each producable ordering/grouping.
  for (size_t i = 1; i < m_orderings.size(); ++i) {
    if (IsGrouping(m_orderings[i].ordering)) {
      // Not directly producable, but we've made an ordering out of it earlier.
      continue;
    }
    NFSMEdge edge;
    edge.required_fd_idx = INT_MIN + i;
    edge.state_idx = i;
    m_edges.push_back(edge);
    m_states[0].outgoing_edges.push_back(m_edges.size() - 1);
  }

  // Add edges from functional dependencies, in a breadth-first search
  // (the array of m_states will expand as we go).
  OrderElement *tmpbuf =
      thd->mem_root->ArrayAlloc<OrderElement>(m_longest_ordering);
  OrderElement *tmpbuf2 =
      thd->mem_root->ArrayAlloc<OrderElement>(m_longest_ordering);
  for (size_t state_idx = 0; state_idx < m_states.size(); ++state_idx) {
    // Refuse to apply FDs for nondeterministic orderings other than possibly
    // ordering -> grouping; ie., (a) can _not_ be satisfied by (a, rand()).
    // This is to avoid evaluating such a nondeterministic function unexpectedly
    // early, e.g. in GROUP BY when the user didn't expect it to be used in
    // ORDER BY. (We still allow it on exact matches, though.See also comments
    // on RAND_TABLE_BIT in SortAheadOrdering.)
    Ordering old_ordering = m_states[state_idx].satisfied_ordering;
    const bool deterministic = none_of(
        old_ordering.begin(), old_ordering.end(), [this](OrderElement element) {
          return Overlaps(m_items[element.item].item->used_tables(),
                          RAND_TABLE_BIT);
        });

    // Apply the special decay FD; first to convert it into a grouping
    // (which we always allow, even for nondeterministic items),
    // then to shorten the ordering.
    if (!IsGrouping(old_ordering) && !old_ordering.empty()) {
      AddGroupingFromOrdering(thd, state_idx, old_ordering, tmpbuf);
    }
    if (!deterministic) {
      continue;
    }
    if (!IsGrouping(old_ordering) && old_ordering.size() > 1) {
      AddEdge(thd, state_idx, /*required_fd_idx=*/0,
              old_ordering.without_back());
    }

    if (m_states.size() >= kMaxNFSMStates) {
      // Stop expanding new functional dependencies, causing us to end fairly
      // soon. We won't necessarily find the optimal query, but we'll keep all
      // essential information, and not throw away any of the information we
      // have already gathered (unless the DFSM gets too large, too;
      // see ConvertNFSMToDFSM()).
      continue;
    }

    for (size_t fd_idx = 1; fd_idx < m_fds.size(); ++fd_idx) {
      const FunctionalDependency &fd = m_fds[fd_idx];

      int start_point;
      if (!FunctionalDependencyApplies(fd, old_ordering, &start_point)) {
        continue;
      }

      ItemHandle item_to_add = fd.tail;

      // On a = b, try to replace a with b or b with a.
      Ordering base_ordering;
      if (fd.type == FunctionalDependency::EQUIVALENCE) {
        Ordering new_ordering{tmpbuf, old_ordering.size()};
        memcpy(tmpbuf, &old_ordering[0],
               sizeof(old_ordering[0]) * old_ordering.size());
        ItemHandle other_item = fd.head[0];
        if (new_ordering[start_point].item == item_to_add) {
          // b already existed, so it's a we must add.
          swap(item_to_add, other_item);
        }
        new_ordering[start_point].item = item_to_add;  // Keep the direction.
        DeduplicateOrdering(&new_ordering);
        if (CouldBecomeInterestingOrdering(new_ordering)) {
          AddEdge(thd, state_idx, fd_idx, new_ordering);
        }

        // Now we can add back the item we just replaced,
        // at any point after this. E.g., if we had an order abc
        // and applied b=d to get adc, we can add back b to get
        // adbc or adcb. Also, we'll fall through afterwards
        // to _not_ replacing but just adding d, e.g. abdc and abcd.
        // So fall through.
        base_ordering = new_ordering;
        item_to_add = other_item;
      } else {
        base_ordering = old_ordering;
      }

      // On S -> b, try to add b everywhere after the last element of S.
      if (IsGrouping(base_ordering)) {
        if (m_items[m_items[item_to_add].canonical_item].used_in_grouping) {
          TryAddingOrderWithElementInserted(
              thd, state_idx, fd_idx, base_ordering, /*start_point=*/0,
              item_to_add, ORDER_NOT_RELEVANT, tmpbuf2);
        }
      } else {
        // NOTE: We could have neither add_asc nor add_desc, if the item is used
        // only in groupings. If so, we don't add it at all, before we convert
        // it to a grouping.
        bool add_asc = m_items[m_items[item_to_add].canonical_item].used_asc;
        bool add_desc = m_items[m_items[item_to_add].canonical_item].used_desc;
        if (add_asc) {
          TryAddingOrderWithElementInserted(thd, state_idx, fd_idx,
                                            base_ordering, start_point + 1,
                                            item_to_add, ORDER_ASC, tmpbuf2);
        }
        if (add_desc) {
          TryAddingOrderWithElementInserted(thd, state_idx, fd_idx,
                                            base_ordering, start_point + 1,
                                            item_to_add, ORDER_DESC, tmpbuf2);
        }
      }
    }
  }
}

void LogicalOrderings::AddGroupingFromOrdering(THD *thd, int state_idx,
                                               Ordering ordering,
                                               OrderElement *tmpbuf) {
  memcpy(tmpbuf, &ordering[0], sizeof(*tmpbuf) * ordering.size());
  for (size_t i = 0; i < ordering.size(); ++i) {
    tmpbuf[i].direction = ORDER_NOT_RELEVANT;
    if (!m_items[m_items[tmpbuf[i].item].canonical_item].used_in_grouping) {
      // Pruned away.
      return;
    }
  }
  sort(tmpbuf, tmpbuf + ordering.size(),
       [this](const OrderElement &a, const OrderElement &b) {
         return ItemBeforeInGroup(a, b);
       });
  AddEdge(thd, state_idx, /*required_fd_idx=*/0,
          Ordering(tmpbuf, ordering.size()));
}

void LogicalOrderings::TryAddingOrderWithElementInserted(
    THD *thd, int state_idx, int fd_idx, Ordering old_ordering,
    size_t start_point, ItemHandle item_to_add, enum_order direction,
    OrderElement *tmpbuf) {
  if (static_cast<int>(old_ordering.size()) >= m_longest_ordering) {
    return;
  }

  for (size_t add_pos = start_point; add_pos <= old_ordering.size();
       ++add_pos) {
    if (direction == ORDER_NOT_RELEVANT) {
      // For groupings, only insert in the sorted sequence.
      // (If we have found the right insertion spot, we immediately
      // exit after this at the end of the loop.)
      if (add_pos < old_ordering.size() &&
          ItemHandleBeforeInGroup(old_ordering[add_pos].item, item_to_add)) {
        continue;
      }

      // For groupings, we just deduplicate right away.
      // TODO(sgunders): When we get C++20, use operator<=> so that we
      // can use a == b here instead of !(a < b) && !(b < a) as we do now.
      if (add_pos < old_ordering.size() &&
          !ItemHandleBeforeInGroup(item_to_add, old_ordering[add_pos].item)) {
        break;
      }
    }

    if (add_pos > 0) {
      memcpy(tmpbuf, &old_ordering[0], sizeof(*tmpbuf) * (add_pos));
    }
    tmpbuf[add_pos].item = item_to_add;
    tmpbuf[add_pos].direction = direction;
    if (old_ordering.size() > add_pos) {
      memcpy(tmpbuf + add_pos + 1, &old_ordering[add_pos],
             sizeof(*tmpbuf) * (old_ordering.size() - add_pos));
    }
    Ordering new_ordering{tmpbuf, old_ordering.size() + 1};
    DeduplicateOrdering(&new_ordering);

    if (CouldBecomeInterestingOrdering(new_ordering)) {
      AddEdge(thd, state_idx, fd_idx, new_ordering);
    }

    if (direction == ORDER_NOT_RELEVANT) {
      break;
    }
  }
}

/**
  Try to prune away irrelevant nodes from the NFSM; it is worth spending some
  time on this, since the number of NFSM states can explode the size of the
  DFSM. Like with PruneFDs(), we don't do any of the pruning described in
  [Neu04]; it is unclear exactly what is meant, but it would seem the state
  removal/merging there is either underdefined or simply does not do anything
  except remove trivially bad nodes (those that cannot reach anything).

  This also sets the can_reach_interesting_order bitmap on each NFSM node.
 */
void LogicalOrderings::PruneNFSM(THD *thd) {
  // Find the transitive closure of the NFSM; ie., whether state A can reach
  // state B, either directly or through some other state (possibly many).
  // We use the standard Floyd-Warshall algorithm, which is O(n³); if n gets
  // to be very large, we can flip the direction of all edges and use
  // Dijkstra from each interesting order instead (since we're only interested
  // in reachability to interesting orders, and our graph is quite sparse),
  // but Floyd-Warshall is simple and has a low constant factor.
  const int N = m_states.size();
  bool *reachable = thd->mem_root->ArrayAlloc<bool>(N * N);

  // We have multiple pruning techniques, all heuristic in nature.
  // If one removes something, it may help to run the others again,
  // so keep running until we've stabilized.
  bool pruned_anything;
  do {
    pruned_anything = false;
    memset(reachable, 0, sizeof(*reachable) * N * N);

    for (int i = 0; i < N; ++i) {
      if (m_states[i].type == NFSMState::DELETED) {
        continue;
      }

      // There's always an implicit self-edge.
      reachable[i * N + i] = true;

      for (size_t edge_idx : m_states[i].outgoing_edges) {
        reachable[i * N + m_edges[edge_idx].state_idx] = true;
      }
    }

    for (int k = 0; k < N; ++k) {
      for (int i = 0; i < N; ++i) {
        for (int j = 0; j < N; ++j) {
          // If there are edges i -> k -> j, add an edge i -> j.
          reachable[i * N + j] |= reachable[i * N + k] & reachable[k * N + j];
        }
      }
    }

    // Now prune away artificial m_states that cannot reach any
    // interesting orders, and m_states that are not reachable from
    // the initial node (the latter can only happen as the result
    // of other prunings).
    for (int i = 1; i < N; ++i) {
      if (m_states[i].type != NFSMState::ARTIFICIAL) {
        continue;
      }

      if (!reachable[0 * N + i]) {
        m_states[i].type = NFSMState::DELETED;
        pruned_anything = true;
        continue;
      }

      bool can_reach_interesting = false;
      for (int j = 1; j < static_cast<int>(m_orderings.size()); ++j) {
        if (reachable[i * N + j] &&
            m_states[j].type == NFSMState::INTERESTING) {
          can_reach_interesting = true;
          break;
        }
      }
      if (!can_reach_interesting) {
        m_states[i].type = NFSMState::DELETED;
        pruned_anything = true;
      }
    }

    // For each producing order, remove edges to m_states that cannot
    // reach any _other_ interesting orders. This often helps dislodging
    // such m_states from the graph as a whole, removing them in some later
    // step. This supersedes the same-destination merging step from [Neu04].
    for (size_t i = 1; i < m_orderings.size(); ++i) {
      NFSMState &state = m_states[i];
      for (size_t j = 0; j < state.outgoing_edges.size(); ++j) {
        const int next_state_idx = m_edges[state.outgoing_edges[j]].state_idx;
        bool can_reach_other_interesting = false;
        for (size_t k = 1; k < m_orderings.size(); ++k) {
          if (k != i && m_states[k].type == NFSMState::INTERESTING &&
              reachable[next_state_idx * N + k]) {
            can_reach_other_interesting = true;
            break;
          }
        }
        if (!can_reach_other_interesting) {
          // Remove this edge.
          state.outgoing_edges[j] =
              state.outgoing_edges[state.outgoing_edges.size() - 1];
          state.outgoing_edges.resize(state.outgoing_edges.size() - 1);
          pruned_anything = true;
        }
      }
    }

    // Remove any edges to deleted m_states.
    for (int i = 0; i < N; ++i) {
      NFSMState &state = m_states[i];
      if (state.type == NFSMState::DELETED) {
        continue;
      }
      int num_kept = 0;
      for (size_t j = 0; j < state.outgoing_edges.size(); ++j) {
        const NFSMEdge &edge = m_edges[state.outgoing_edges[j]];
        if (edge.state(this)->type != NFSMState::DELETED) {
          state.outgoing_edges[num_kept++] = state.outgoing_edges[j];
        }
      }
      state.outgoing_edges.resize(num_kept);
    }
  } while (pruned_anything);

  // Set the bitmask of what each node can reach.
  for (size_t order_idx = 0; order_idx < m_orderings.size(); ++order_idx) {
    if (m_orderings[order_idx].type != OrderingWithInfo::INTERESTING ||
        order_idx >= kMaxSupportedOrderings) {
      continue;
    }
    for (int i = 0; i < N; ++i) {
      if (m_states[i].type == NFSMState::DELETED) {
        continue;
      }
      if (reachable[i * N + order_idx]) {
        m_states[i].can_reach_interesting_order.set(order_idx);
      }
    }
  }
}

bool LogicalOrderings::AlwaysActiveFD(int fd_idx) {
  // Note: Includes ϵ-edges.
  return fd_idx >= 0 && m_fds[fd_idx].always_active;
}

void LogicalOrderings::FinalizeDFSMState(THD *thd, int state_idx) {
  LogicalOrderings::DFSMState &state = m_dfsm_states[state_idx];
  for (int nfsm_state_idx : state.nfsm_states) {
    int ordering_idx = m_states[nfsm_state_idx].satisfied_ordering_idx;
    if (m_states[nfsm_state_idx].type == NFSMState::INTERESTING &&
        ordering_idx < kMaxSupportedOrderings &&
        m_orderings[ordering_idx].type == OrderingWithInfo::INTERESTING) {
      state.follows_interesting_order.set(ordering_idx);
    }
    state.can_reach_interesting_order |=
        m_states[nfsm_state_idx].can_reach_interesting_order;
  }
  state.next_state =
      Bounds_checked_array<int>::Alloc(thd->mem_root, m_fds.size());
  fill(state.next_state.begin(), state.next_state.end(), state_idx);
}

void LogicalOrderings::ExpandThroughAlwaysActiveFDs(
    Mem_root_array<int> *nfsm_states, int *generation,
    int extra_allowed_fd_idx) {
  ++*generation;  // Effectively clear the “seen” flag in all NFSM states.
  for (size_t i = 0; i < nfsm_states->size(); ++i) {
    const NFSMState &state = m_states[(*nfsm_states)[i]];
    for (int outgoing_edge_idx : state.outgoing_edges) {
      const NFSMEdge &edge = m_edges[outgoing_edge_idx];
      if ((AlwaysActiveFD(edge.required_fd_idx) ||
           edge.required_fd_idx == extra_allowed_fd_idx) &&
          m_states[edge.state_idx].seen != *generation) {
        nfsm_states->push_back(edge.state_idx);
        m_states[edge.state_idx].seen = *generation;
      }
    }
  }
}

/**
  From the NFSM, convert an equivalent DFSM.

  This is by means of the so-called powerset conversion, which is more commonly
  used to convert NFAs to DFAs. (The only real difference is that FAs have
  accepting states, while our FSM instead needs to store information about
  constituent interesting order states.)

  The powerset algorithm works by creating DFSM states that represent sets of
  NFSM states we could be in. E.g., if we have a state (a) and an FD {} → x can
  lead to new states () (ϵ-edge), (a) (implicit self-edge), (x), (ax), (xa),
  then we create a single new DFSM state that represent all those five states,
  and an {} → x edge from {(a)} to that new state. When creating edges from
  such superstates, we need to follow that FD from _all_ of them, so the list
  of constituent states can be fairly large.

  In theory, we could get 2^n DFSM states from n NFSM states, but in practice,
  we get fewer since our orderings generally only increase, not decrease.
  We only generate DFSM states by following FDs from the initial NFSM state;
  we don't create states eagerly for all 2^n possibilities.

  When creating DFSM states, we always include states that can be reached by
  means of always-active FDs. The ϵ edge (drop the last element from the
  ordering) is always active, and the client can also mark others as such.
  This means we get fewer DFSM states and fewer FDs to follow. See
  FunctionalDependency::always_active.
 */
void LogicalOrderings::ConvertNFSMToDFSM(THD *thd) {
  // See NFSMState::seen.
  int generation = 0;

  // Create the initial DFSM state. It consists of everything in the initial
  // NFSM state, and everything reachable from it with only always-active FDs.
  DFSMState initial;
  initial.nfsm_states.init(thd->mem_root);
  initial.nfsm_states.push_back(0);
  ExpandThroughAlwaysActiveFDs(&initial.nfsm_states, &generation,
                               /*extra_allowed_fd_idx=*/0);
  m_dfsm_states.push_back(move(initial));
  FinalizeDFSMState(thd, /*state_idx=*/0);

  // Reachability information set by FinalizeDFSMState() will include those
  // that can be reached through SetOrder() nodes, so it's misleading.
  // Clear it; this isn't 100% active if interesting orderings can be reached
  // through FDs only, but it will ever cause too little pruning, not too much.
  m_dfsm_states[0].can_reach_interesting_order.reset();

  // Used in iteration below.
  Mem_root_array<int> nfsm_states(thd->mem_root);
  Mem_root_array<NFSMEdge> nfsm_edges(thd->mem_root);

  for (size_t dfsm_state_idx = 0; dfsm_state_idx < m_dfsm_states.size();
       ++dfsm_state_idx) {
    // Take the union of all outgoing edges from the constituent NFSM m_states,
    // ignoring ϵ-edges and always active FDs, since we have special handling of
    // them below.
    nfsm_edges.clear();
    for (int nfsm_state_idx : m_dfsm_states[dfsm_state_idx].nfsm_states) {
      for (const int edge_idx : m_states[nfsm_state_idx].outgoing_edges) {
        const NFSMEdge &edge = m_edges[edge_idx];
        if (!AlwaysActiveFD(edge.required_fd_idx)) {
          nfsm_edges.push_back(edge);
        }
      }
    }

    if (m_dfsm_states.size() >= kMaxDFSMStates) {
      // Stop creating new states, causing us to end fairly soon (same as the
      // cutoff in BuildNFSM()). Note that since the paths representing explicit
      // sorts are put first, they will never be lost unless kMaxDFSMStates is
      // set extremely low.
      continue;
    }

    {
      // Sort and deduplicate the edges. Note that we sort on FD first,
      // since we'll be grouping on that when creating new m_states.
      sort(nfsm_edges.begin(), nfsm_edges.end(),
           [](const NFSMEdge &a, const NFSMEdge &b) {
             return make_pair(a.required_fd_idx, a.state_idx) <
                    make_pair(b.required_fd_idx, b.state_idx);
           });
      auto new_end =
          unique(nfsm_edges.begin(), nfsm_edges.end(),
                 [](const NFSMEdge &a, const NFSMEdge &b) {
                   return make_pair(a.required_fd_idx, a.state_idx) ==
                          make_pair(b.required_fd_idx, b.state_idx);
                 });
      nfsm_edges.resize(distance(nfsm_edges.begin(), new_end));
    }

    // For each relevant FD, find out which set of m_states we could reach.
    m_dfsm_states[dfsm_state_idx].outgoing_edges.init(thd->mem_root);
    nfsm_states.clear();
    for (size_t edge_idx = 0; edge_idx < nfsm_edges.size(); ++edge_idx) {
      nfsm_states.push_back(nfsm_edges[edge_idx].state_idx);

      // Is this the last state in the group? If not, keep iterating.
      if (edge_idx != nfsm_edges.size() - 1 &&
          nfsm_edges[edge_idx].required_fd_idx ==
              nfsm_edges[edge_idx + 1].required_fd_idx) {
        continue;
      }

      // Add the implicit self-edges.
      for (int nfsm_state_idx : m_dfsm_states[dfsm_state_idx].nfsm_states) {
        if (nfsm_state_idx != 0) {
          nfsm_states.push_back(nfsm_state_idx);
        }
      }

      // Expand the set to contain any ϵ-edges and always active FDs,
      // in a breadth-first manner. Note that now, we might see new
      // edges for the same FD, so we should follow those as well.
      ExpandThroughAlwaysActiveFDs(&nfsm_states, &generation,
                                   nfsm_edges[edge_idx].required_fd_idx);

      // Canonicalize: Sort and deduplicate.
      sort(nfsm_states.begin(), nfsm_states.end());
      auto new_end = unique(nfsm_states.begin(), nfsm_states.end());
      nfsm_states.resize(distance(nfsm_states.begin(), new_end));

      // See if there is an existing DFSM state that matches the set of
      // NFSM m_states we've collected.
      int target_dfsm_state_idx = -1;
      for (size_t i = 0; i < m_dfsm_states.size(); ++i) {
        if (equal(nfsm_states.begin(), nfsm_states.end(),
                  m_dfsm_states[i].nfsm_states.begin(),
                  m_dfsm_states[i].nfsm_states.end())) {
          target_dfsm_state_idx = i;
          break;
        }
      }
      if (target_dfsm_state_idx == -1) {
        // There's none, so create a new one. The type doesn't really matter,
        // except for printing out the graph.
        DFSMState state;
        state.nfsm_states = move(nfsm_states);
        m_dfsm_states.push_back(move(state));
        FinalizeDFSMState(thd, m_dfsm_states.size() - 1);
        target_dfsm_state_idx = m_dfsm_states.size() - 1;
      }

      // Finally, add an edge in the DFSM. Ignore self-edges; they are implicit.
      if (static_cast<size_t>(target_dfsm_state_idx) != dfsm_state_idx) {
        DFSMEdge edge;
        edge.required_fd_idx = nfsm_edges[edge_idx].required_fd_idx;
        edge.state_idx = target_dfsm_state_idx;
        m_dfsm_edges.push_back(edge);

        DFSMState &dfsm_state = m_dfsm_states[dfsm_state_idx];
        dfsm_state.outgoing_edges.push_back(m_dfsm_edges.size() - 1);
        if (edge.required_fd_idx >= 0) {
          dfsm_state.next_state[edge.required_fd_idx] = target_dfsm_state_idx;
          if (edge.required_fd_idx >= 1 &&
              edge.required_fd_idx <= kMaxSupportedFDs) {
            dfsm_state.can_use_fd.set(edge.required_fd_idx - 1);
          }
        }
      }

      // Prepare for the next group.
      nfsm_states.clear();
    }
  }
}

void LogicalOrderings::FindInitialStatesForOrdering() {
  // Find all constructor edges from the initial state, and use them
  // to populate the table.
  for (int outgoing_edge_idx : m_dfsm_states[0].outgoing_edges) {
    const DFSMEdge &edge = m_dfsm_edges[outgoing_edge_idx];
    if (edge.required_fd_idx < 0) {
      const int ordering_idx = edge.required_fd_idx - INT_MIN;
      m_orderings[ordering_idx].state_idx = edge.state_idx;
    }
  }
}

string LogicalOrderings::PrintOrdering(Ordering ordering) const {
  const bool is_grouping = IsGrouping(ordering);
  string ret = is_grouping ? "{" : "(";
  for (size_t i = 0; i < ordering.size(); ++i) {
    if (i != 0) ret += ", ";
    ret += ItemToString(m_items[ordering[i].item].item);
    if (ordering[i].direction == ORDER_DESC) {
      ret += " DESC";
    }
  }
  ret += is_grouping ? '}' : ')';
  return ret;
}

string LogicalOrderings::PrintFunctionalDependency(
    const FunctionalDependency &fd, bool html) const {
  switch (fd.type) {
    case FunctionalDependency::DECAY:
      if (html) {
        return "&epsilon;";
      } else {
        return "eps";
      }
    case FunctionalDependency::EQUIVALENCE:
      return ItemToString(m_items[fd.head[0]].item) + "=" +
             ItemToString(m_items[fd.tail].item);
    case FunctionalDependency::FD: {
      string ret = "{";
      for (size_t i = 0; i < fd.head.size(); ++i) {
        if (i != 0) {
          ret += ", ";
        }
        ret += ItemToString(m_items[fd.head[i]].item);
      }
      if (html) {
        ret += "} &rarr; ";
      } else {
        ret += "} -> ";
      }
      ret += ItemToString(m_items[fd.tail].item);
      return ret;
    }
  }
  assert(false);
  return "";
}

void LogicalOrderings::PrintFunctionalDependencies(string *trace) {
  if (m_fds.size() <= 1) {
    *trace += "\nNo functional dependencies (after pruning).\n\n";
  } else {
    *trace += "\nFunctional dependencies (after pruning):\n";
    for (size_t fd_idx = 1; fd_idx < m_fds.size(); ++fd_idx) {
      *trace +=
          " - " + PrintFunctionalDependency(m_fds[fd_idx], /*html=*/false);
      if (m_fds[fd_idx].always_active) {
        *trace += " [always active]";
      }
      *trace += "\n";
    }
    *trace += "\n";
  }
}

void LogicalOrderings::PrintInterestingOrders(string *trace) {
  *trace += "Interesting orders:\n";
  for (size_t order_idx = 0; order_idx < m_orderings.size(); ++order_idx) {
    const OrderingWithInfo &ordering = m_orderings[order_idx];
    if (order_idx == 0 && ordering.type == OrderingWithInfo::UNINTERESTING) {
      continue;
    }

    *trace += StringPrintf(" - %zu: ", order_idx);
    bool first = true;
    if (IsGrouping(ordering.ordering)) {
      *trace += "group ";
    }
    for (OrderElement element : ordering.ordering) {
      if (!first) {
        *trace += ", ";
      }
      first = false;
      *trace += ItemToString(m_items[element.item].item);
      if (element.direction == ORDER_DESC) {
        *trace += " DESC";
      }
    }
    if (ordering.ordering.empty()) {
      *trace += "()";
    }
    if (ordering.type == OrderingWithInfo::HOMOGENIZED) {
      *trace += " [homogenized from other ordering]";
    } else if (ordering.type == OrderingWithInfo::UNINTERESTING) {
      *trace += " [support order]";
    }
    *trace += "\n";
  }
  *trace += "\n";
}

void LogicalOrderings::PrintNFSMDottyGraph(string *trace) const {
  *trace += "digraph G {\n";
  for (size_t state_idx = 0; state_idx < m_states.size(); ++state_idx) {
    const NFSMState &state = m_states[state_idx];
    if (state.type == NFSMState::DELETED) {
      continue;
    }

    // We're printing the NFSM.
    *trace += StringPrintf("  s%zu [label=\"%s\"", state_idx,
                           PrintOrdering(state.satisfied_ordering).c_str());
    if (state.type == NFSMState::INTERESTING) {
      *trace += ", peripheries=2";
    }
    *trace += "]\n";

    for (size_t edge_idx : state.outgoing_edges) {
      const NFSMEdge &edge = m_edges[edge_idx];
      if (edge.required_fd_idx < 0) {
        // Pseudo-edge without a FD (from initial state only).
        *trace +=
            StringPrintf("  s%zu -> s%d [label=\"ordering %d\"]\n", state_idx,
                         edge.state_idx, edge.required_fd_idx - INT_MIN);
      } else {
        const FunctionalDependency *fd = edge.required_fd(this);
        *trace += StringPrintf(
            "  s%zu -> s%d [label=\"%s\"]\n", state_idx, edge.state_idx,
            PrintFunctionalDependency(*fd, /*html=*/true).c_str());
      }
    }
  }

  *trace += "}\n";
}

void LogicalOrderings::PrintDFSMDottyGraph(string *trace) const {
  *trace += "digraph G {\n";
  for (size_t state_idx = 0; state_idx < m_dfsm_states.size(); ++state_idx) {
    const DFSMState &state = m_dfsm_states[state_idx];
    *trace += StringPrintf("  s%zu [label=< ", state_idx);

    bool any_interesting = false;
    for (size_t i = 0; i < state.nfsm_states.size(); ++i) {
      const NFSMState &nsfm_state = m_states[state.nfsm_states[i]];
      if (i != 0) {
        *trace += ", ";
      }
      if (nsfm_state.type == NFSMState::INTERESTING) {
        any_interesting = true;
        *trace += "<b>";
      }
      *trace += PrintOrdering(nsfm_state.satisfied_ordering);
      if (nsfm_state.type == NFSMState::INTERESTING) {
        *trace += "</b>";
      }
    }
    *trace += " >";
    if (any_interesting) {
      *trace += ", peripheries=2";
    }
    *trace += "]\n";

    for (size_t edge_idx : state.outgoing_edges) {
      const DFSMEdge &edge = m_dfsm_edges[edge_idx];
      if (edge.required_fd_idx < 0) {
        // Pseudo-edge without a FD (from initial state only).
        *trace +=
            StringPrintf("  s%zu -> s%d [label=\"ordering %d\"]\n", state_idx,
                         edge.state_idx, edge.required_fd_idx - INT_MIN);
      } else {
        const FunctionalDependency *fd = edge.required_fd(this);
        *trace += StringPrintf(
            "  s%zu -> s%d [label=\"%s\"]\n", state_idx, edge.state_idx,
            PrintFunctionalDependency(*fd, /*html=*/true).c_str());
      }
    }
  }

  *trace += "}\n";
}
