24#ifndef SQL_JOIN_OPTIMIZER_INTERESTING_ORDERS_H 
   25#define SQL_JOIN_OPTIMIZER_INTERESTING_ORDERS_H 
  362                               used_at_end, homogenize_tables);
 
  373    return !
m_orderings[ordering_idx].ordering.GetElements().empty() &&
 
  447      fd_set.set(new_fd_idx - 1);
 
  463    if (ordering_idx == 0) {
 
  469    return m_dfsm_states[state_idx].follows_interesting_order.test(
 
  500      std::bitset<kMaxSupportedOrderings> ignored_orderings)
 const {
 
  502    std::bitset<kMaxSupportedOrderings> a =
 
  503        m_dfsm_states[a_idx].follows_interesting_order & ~ignored_orderings;
 
  504    std::bitset<kMaxSupportedOrderings> b =
 
  505        m_dfsm_states[b_idx].follows_interesting_order & ~ignored_orderings;
 
  506    std::bitset<kMaxSupportedOrderings> future_a =
 
  507        m_dfsm_states[a_idx].can_reach_interesting_order & ~ignored_orderings;
 
  508    std::bitset<kMaxSupportedOrderings> future_b =
 
  509        m_dfsm_states[b_idx].can_reach_interesting_order & ~ignored_orderings;
 
  510    return (a & b) != a || (future_a & future_b) != future_a;
 
  519  class OrderWithElementInserted;
 
  586  friend bool operator==(
const NFSMEdge &a, 
const NFSMEdge &b);
 
  587  friend bool operator!=(
const NFSMEdge &a, 
const NFSMEdge &b);
 
  733                          bool used_at_end, 
table_map homogenize_tables);
 
  772      THD *thd, 
const Ordering &reduced_ordering, 
bool used_at_end,
 
  797                                    int *generation, 
int extra_allowed_fd_idx);
 
  811  void AddEdge(
THD *thd, 
int state_idx, 
int required_fd_idx,
 
  821                                   int *start_point) 
const;
 
Element_type * data()
Definition: sql_array.h:117
 
static Bounds_checked_array Alloc(MEM_ROOT *mem_root, size_t size)
Definition: sql_array.h:71
 
const_iterator cbegin() const
Returns a pointer to the first element in the array.
Definition: sql_array.h:145
 
size_t size() const
Definition: sql_array.h:155
 
Bounds_checked_array Clone(MEM_ROOT *mem_root) const
Make a copy of '*this'. Allocate memory for m_array on 'mem_root'.
Definition: sql_array.h:76
 
const_iterator cend() const
Returns a pointer to the past-the-end element in the array.
Definition: sql_array.h:147
 
Base class that is used to represent any kind of expression in a relational query.
Definition: item.h:928
 
Definition: interesting_orders.h:316
 
void SetHeadForAggregates(Bounds_checked_array< ItemHandle > head)
Definition: interesting_orders.h:394
 
void AddRollupFromOrder(THD *thd, int state_idx, const Ordering &ordering)
Definition: interesting_orders.cc:1724
 
void AddEdge(THD *thd, int state_idx, int required_fd_idx, const Ordering &ordering)
Definition: interesting_orders.cc:1320
 
bool ItemHandleBeforeInGroup(ItemHandle a, ItemHandle b) const
Definition: interesting_orders.h:720
 
void CreateOrderingsFromGroupings(THD *thd)
We don't currently have any operators that only group and do not sort (e.g.
Definition: interesting_orders.cc:935
 
void AddFDsFromComputedItems(THD *thd)
Try to add new FDs from items that are not base items; e.g., if we have an item (a + 1),...
Definition: interesting_orders.cc:626
 
Mem_root_array< OrderingWithInfo > m_orderings
Definition: interesting_orders.h:686
 
bool ImpliedByEarlierElements(ItemHandle item, Ordering::Elements prefix, bool all_fds) const
Checks whether the given item is redundant given previous elements in the ordering; ie....
Definition: interesting_orders.cc:828
 
Bounds_checked_array< int > m_optimized_fd_mapping
Definition: interesting_orders.h:705
 
StateIndex SetOrder(int ordering_idx) const
Definition: interesting_orders.h:434
 
int num_orderings() const
Definition: interesting_orders.h:366
 
bool DoesFollowOrder(StateIndex state_idx, int ordering_idx) const
Definition: interesting_orders.h:461
 
bool m_rollup
Definition: interesting_orders.h:565
 
int m_longest_ordering
Definition: interesting_orders.h:689
 
Mem_root_array< DFSMState > m_dfsm_states
Definition: interesting_orders.h:697
 
bool m_built
Definition: interesting_orders.h:521
 
void AddHomogenizedOrderingIfPossible(THD *thd, const Ordering &reduced_ordering, bool used_at_end, int table_idx, Bounds_checked_array< std::pair< ItemHandle, ItemHandle > > reverse_canonical)
Helper function for CreateHomogenizedOrderings().
Definition: interesting_orders.cc:1119
 
friend bool operator==(const NFSMEdge &a, const NFSMEdge &b)
Definition: interesting_orders.h:859
 
std::string PrintOrdering(const Ordering &ordering) const
Definition: interesting_orders.cc:2134
 
Ordering ReduceOrdering(Ordering ordering, bool all_fds, Ordering::Elements tmp) const
Remove redundant elements using the functional dependencies that we have, to give a more canonical fo...
Definition: interesting_orders.cc:1102
 
void PrintFunctionalDependencies(std::ostream *trace)
Definition: interesting_orders.cc:2187
 
int RemapOrderingIndex(int ordering_idx) const
Definition: interesting_orders.h:427
 
void FinalizeDFSMState(THD *thd, int state_idx)
Definition: interesting_orders.cc:1915
 
void AddGroupingFromRollup(THD *thd, int state_idx, const Ordering &ordering)
Definition: interesting_orders.cc:1704
 
void PrintDFSMDottyGraph(std::ostream *trace) const
Definition: interesting_orders.cc:2285
 
friend bool operator!=(const NFSMEdge &a, const NFSMEdge &b)
Definition: interesting_orders.h:864
 
Mem_root_array< OrderElement * > m_elements_pool
We may potentially use a lot of Ordering::Elements objects, with short and non-overlapping life times...
Definition: interesting_orders.h:710
 
int num_items() const
Definition: interesting_orders.h:332
 
bool ordering_is_relevant_for_sortahead(int ordering_idx) const
Definition: interesting_orders.h:372
 
void PreReduceOrderings(THD *thd)
Do safe reduction on all orderings (some of them may get merged by PruneUninterestingOrders() later),...
Definition: interesting_orders.cc:895
 
void SetRollup(bool rollup)
Definition: interesting_orders.h:401
 
void CreateHomogenizedOrderings(THD *thd)
For each interesting ordering, see if we can homogenize it onto each table.
Definition: interesting_orders.cc:1023
 
Mem_root_array< NFSMState > m_states
Definition: interesting_orders.h:694
 
void SortElements(Ordering::Elements elements) const
Sort the elements so that a will appear before b if ItemBeforeInGroup(a,b)==true.
Definition: interesting_orders.cc:1185
 
int AddOrderingInternal(THD *thd, Ordering order, OrderingWithInfo::Type type, bool used_at_end, table_map homogenize_tables)
Definition: interesting_orders.cc:215
 
void AddFDsFromConstItems(THD *thd)
Try to add FDs from items that are constant by themselves, e.g.
Definition: interesting_orders.cc:739
 
int AddFunctionalDependency(THD *thd, FunctionalDependency fd)
Definition: interesting_orders.cc:270
 
bool FunctionalDependencyApplies(const FunctionalDependency &fd, const Ordering &ordering, int *start_point) const
Definition: interesting_orders.cc:1338
 
Mem_root_array< DFSMEdge > m_dfsm_edges
Definition: interesting_orders.h:698
 
void ReturnElements(Ordering::Elements elements)
Return an Ordering::Elements object with size()==m_longest_ordering to m_elements_pool.
Definition: interesting_orders.h:842
 
bool MoreOrderedThan(StateIndex a_idx, StateIndex b_idx, std::bitset< kMaxSupportedOrderings > ignored_orderings) const
Definition: interesting_orders.h:498
 
bool AlwaysActiveFD(int fd_idx)
Definition: interesting_orders.cc:1910
 
void BuildNFSM(THD *thd)
Definition: interesting_orders.cc:1497
 
void PrintInterestingOrders(std::ostream *trace)
Definition: interesting_orders.cc:2204
 
void BuildEquivalenceClasses()
Definition: interesting_orders.cc:523
 
int AddArtificialState(THD *thd, const Ordering &ordering)
Definition: interesting_orders.cc:1304
 
int StateIndex
Definition: interesting_orders.h:432
 
void PruneNFSM(THD *thd)
Try to prune away irrelevant nodes from the NFSM; it is worth spending some time on this,...
Definition: interesting_orders.cc:1784
 
Item * item(ItemHandle item) const
Definition: interesting_orders.h:331
 
void PrintNFSMDottyGraph(std::ostream *trace) const
Definition: interesting_orders.cc:2251
 
const Ordering & ordering(int ordering_idx) const
Definition: interesting_orders.h:368
 
void FindElementsThatCanBeAddedByFDs()
Definition: interesting_orders.cc:800
 
StateIndex ApplyFDs(StateIndex state_idx, FunctionalDependencySet fds) const
Definition: interesting_orders.cc:363
 
Bounds_checked_array< int > m_optimized_ordering_mapping
Definition: interesting_orders.h:702
 
int AddOrdering(THD *thd, Ordering order, bool interesting, bool used_at_end, table_map homogenize_tables)
Definition: interesting_orders.h:357
 
Ordering::Elements RetrieveElements(MEM_ROOT *mem_root)
Fetch an Ordering::Elements object with size()==m_longest_ordering.
Definition: interesting_orders.h:828
 
void ExpandThroughAlwaysActiveFDs(Mem_root_array< int > *nfsm_states, int *generation, int extra_allowed_fd_idx)
Definition: interesting_orders.cc:1932
 
void PruneFDs(THD *thd)
Definition: interesting_orders.cc:454
 
void PruneUninterestingOrders(THD *thd)
Try to get rid of uninteresting orders, possibly by discarding irrelevant suffixes and merging them w...
Definition: interesting_orders.cc:395
 
void AddFDsFromAggregateItems(THD *thd)
Definition: interesting_orders.cc:762
 
Bounds_checked_array< ItemHandle > CollectHeadForStaticWindowFunction(THD *thd, ItemHandle argument_item, Window *window)
Definition: interesting_orders.cc:585
 
Mem_root_array< ItemInfo > m_items
Definition: interesting_orders.h:556
 
ItemHandle GetHandle(Item *item)
Definition: interesting_orders.cc:1196
 
void FindInitialStatesForOrdering()
Definition: interesting_orders.cc:2122
 
void AddGroupingFromOrder(THD *thd, int state_idx, const Ordering &ordering)
Definition: interesting_orders.cc:1681
 
LogicalOrderings(THD *thd)
Definition: interesting_orders.cc:193
 
Mem_root_array< FunctionalDependency > m_fds
Definition: interesting_orders.h:691
 
void ConvertNFSMToDFSM(THD *thd)
From the NFSM, convert an equivalent DFSM.
Definition: interesting_orders.cc:1976
 
Bounds_checked_array< ItemHandle > m_aggregate_head
Definition: interesting_orders.h:560
 
void TryAddingOrderWithElementInserted(THD *thd, int state_idx, int fd_idx, Ordering old_ordering, size_t start_point, ItemHandle item_to_add, enum_order direction)
 
FunctionalDependencySet GetFDSet(int fd_idx) const
Definition: interesting_orders.h:443
 
void RecanonicalizeGroupings()
Definition: interesting_orders.cc:573
 
void Build(THD *thd)
Definition: interesting_orders.cc:301
 
std::string PrintFunctionalDependency(const FunctionalDependency &fd, bool html) const
Definition: interesting_orders.cc:2154
 
void CreateOrderingsFromRollups(THD *thd)
 
bool ItemBeforeInGroup(const OrderElement &a, const OrderElement &b) const
Definition: interesting_orders.h:726
 
bool CouldBecomeInterestingOrdering(const Ordering &ordering) const
For a given ordering, check whether it ever has the hope of becoming an interesting ordering.
Definition: interesting_orders.cc:1246
 
int num_fds() const
Definition: interesting_orders.h:383
 
A typesafe replacement for DYNAMIC_ARRAY.
Definition: mem_root_array.h:432
 
A scope-guard class for allocating an Ordering::Elements instance which is automatically returned to ...
Definition: interesting_orders.cc:69
 
Represents a (potentially interesting) ordering, rollup or (non-rollup) grouping.
Definition: interesting_orders.h:161
 
Elements & GetElements()
Definition: interesting_orders.h:223
 
size_t size() const
Definition: interesting_orders.h:228
 
Kind
The kind of ordering that an Ordering instance may represent.
Definition: interesting_orders.h:169
 
@ kOrder
Specific sequence of m_elements, and specific direction of each element.
 
@ kGroup
Elements may appear in any sequence and may be ordered in any direction.
 
@ kRollup
Specific sequence of m_elements, but each element may be ordered in any direction.
 
@ kEmpty
An ordering with no elements.
 
Ordering & operator=(const Ordering &other)
Assignment operator. Only defined explicitly to check Valid().
Definition: interesting_orders.h:200
 
Kind GetKind() const
Definition: interesting_orders.h:213
 
Ordering()
Definition: interesting_orders.h:187
 
Ordering(Elements elements, Kind kind)
Definition: interesting_orders.h:189
 
void Deduplicate()
Remove duplicate entries, in-place.
Definition: interesting_orders.cc:157
 
Bounds_checked_array< OrderElement > Elements
This type hold the individual elements of the ordering.
Definition: interesting_orders.h:166
 
Kind m_kind
The kind of this ordering.
Definition: interesting_orders.h:240
 
Ordering Clone(MEM_ROOT *mem_root) const
Make a copy of *this. Allocate new memory for m_elements from mem_root.
Definition: interesting_orders.h:208
 
Elements m_elements
The ordering terms.
Definition: interesting_orders.h:237
 
friend bool operator==(const Ordering &a, const Ordering &b)
Check if 'a' and 'b' has the same kind and contains the same elements.
Definition: interesting_orders.h:247
 
Ordering(const Ordering &other)
Copy constructor. Only defined explicitly to check Valid().
Definition: interesting_orders.h:194
 
bool Valid() const
Definition: interesting_orders.cc:168
 
const Elements & GetElements() const
Definition: interesting_orders.h:218
 
For each client connection we create a separate thread with THD serving as a thread/connection descri...
Definition: sql_lexer_thd.h:36
 
Represents the (explicit) window of a SQL 2003 section 7.11 <window clause>, or the implicit (inlined...
Definition: window.h:110
 
static MEM_ROOT mem_root
Definition: client_plugin.cc:114
 
static bool equal(const Item *i1, const Item *i2, const Field *f2)
Definition: sql_select.cc:3935
 
bool operator!=(const Ordering &a, const Ordering &b)
Definition: interesting_orders.h:255
 
bool operator==(const Ordering &a, const Ordering &b)
Check if 'a' and 'b' has the same kind and contains the same elements.
Definition: interesting_orders.h:247
 
std::bitset< kMaxSupportedFDs > FunctionalDependencySet
Definition: interesting_orders_defs.h:63
 
static constexpr int kMaxSupportedFDs
Definition: interesting_orders_defs.h:62
 
static constexpr int kMaxSupportedOrderings
Definition: interesting_orders_defs.h:65
 
int ItemHandle
Definition: interesting_orders_defs.h:39
 
enum_order
Definition: key_spec.h:65
 
void TRASH(void *ptr, size_t length)
Put bad content in memory to be sure it will segfault if dereferenced.
Definition: memory_debugging.h:71
 
uint64_t table_map
Definition: my_table_map.h:30
 
mutable_buffer buffer(void *p, size_t n) noexcept
Definition: buffer.h:418
 
required string type
Definition: replication_group_member_actions.proto:34
 
Definition: interesting_orders.h:259
 
ItemHandle tail
Definition: interesting_orders.h:291
 
@ FD
Definition: interesting_orders.h:281
 
@ DECAY
Definition: interesting_orders.h:274
 
@ EQUIVALENCE
Definition: interesting_orders.h:287
 
bool always_active
Definition: interesting_orders.h:313
 
Bounds_checked_array< ItemHandle > head
Definition: interesting_orders.h:290
 
enum FunctionalDependency::@114 type
 
Definition: interesting_orders.h:630
 
int state_idx
Definition: interesting_orders.h:632
 
int required_fd_idx
Definition: interesting_orders.h:631
 
const DFSMState * state(const LogicalOrderings *orderings) const
Definition: interesting_orders.h:638
 
const FunctionalDependency * required_fd(const LogicalOrderings *orderings) const
Definition: interesting_orders.h:634
 
Definition: interesting_orders.h:605
 
FunctionalDependencySet can_use_fd
Definition: interesting_orders.h:627
 
Mem_root_array< int > nfsm_states
Definition: interesting_orders.h:607
 
std::bitset< kMaxSupportedOrderings > follows_interesting_order
Definition: interesting_orders.h:615
 
Mem_root_array< int > outgoing_edges
Definition: interesting_orders.h:606
 
std::bitset< kMaxSupportedOrderings > can_reach_interesting_order
Definition: interesting_orders.h:622
 
Bounds_checked_array< int > next_state
Definition: interesting_orders.h:612
 
Definition: interesting_orders.h:523
 
bool used_desc
Definition: interesting_orders.h:551
 
bool used_in_grouping
Definition: interesting_orders.h:552
 
ItemHandle canonical_item
Definition: interesting_orders.h:537
 
bool can_be_added_by_fd
Definition: interesting_orders.h:543
 
bool used_asc
Definition: interesting_orders.h:550
 
Item * item
Definition: interesting_orders.h:525
 
Definition: interesting_orders.h:567
 
const NFSMState * state(const LogicalOrderings *orderings) const
Definition: interesting_orders.h:581
 
int required_fd_idx
Definition: interesting_orders.h:572
 
const FunctionalDependency * required_fd(const LogicalOrderings *orderings) const
Definition: interesting_orders.h:577
 
int state_idx
Definition: interesting_orders.h:575
 
Definition: interesting_orders.h:589
 
@ ARTIFICIAL
Definition: interesting_orders.h:590
 
@ DELETED
Definition: interesting_orders.h:590
 
@ INTERESTING
Definition: interesting_orders.h:590
 
Mem_root_array< NFSMEdge > outgoing_edges
Definition: interesting_orders.h:591
 
std::bitset< kMaxSupportedOrderings > can_reach_interesting_order
Definition: interesting_orders.h:596
 
enum LogicalOrderings::NFSMState::@115 type
 
int satisfied_ordering_idx
Definition: interesting_orders.h:593
 
int seen
Definition: interesting_orders.h:603
 
Ordering satisfied_ordering
Definition: interesting_orders.h:592
 
Definition: interesting_orders.h:643
 
StateIndex state_idx
Definition: interesting_orders.h:683
 
Ordering ordering
Definition: interesting_orders.h:644
 
bool used_at_end
Definition: interesting_orders.h:677
 
Type
Definition: interesting_orders.h:649
 
@ UNINTERESTING
Definition: interesting_orders.h:674
 
@ INTERESTING
Definition: interesting_orders.h:652
 
@ HOMOGENIZED
Definition: interesting_orders.h:667
 
enum LogicalOrderings::OrderingWithInfo::Type type
 
table_map homogenize_tables
Definition: interesting_orders.h:680
 
The MEM_ROOT is a simple arena, where allocations are carved out of larger blocks.
Definition: my_alloc.h:83
 
Definition: interesting_orders_defs.h:44
 
ItemHandle item
Definition: interesting_orders_defs.h:45