24#ifndef SQL_JOIN_OPTIMIZER_INTERESTING_ORDERS_H
25#define SQL_JOIN_OPTIMIZER_INTERESTING_ORDERS_H
354 used_at_end, homogenize_tables);
365 return !
m_orderings[ordering_idx].ordering.GetElements().empty() &&
409 void Build(
THD *thd, std::string *trace);
442 fd_set.set(new_fd_idx - 1);
458 if (ordering_idx == 0) {
464 return m_dfsm_states[state_idx].follows_interesting_order.test(
495 std::bitset<kMaxSupportedOrderings> ignored_orderings)
const {
497 std::bitset<kMaxSupportedOrderings> a =
498 m_dfsm_states[a_idx].follows_interesting_order & ~ignored_orderings;
499 std::bitset<kMaxSupportedOrderings> b =
500 m_dfsm_states[b_idx].follows_interesting_order & ~ignored_orderings;
501 std::bitset<kMaxSupportedOrderings> future_a =
502 m_dfsm_states[a_idx].can_reach_interesting_order & ~ignored_orderings;
503 std::bitset<kMaxSupportedOrderings> future_b =
504 m_dfsm_states[b_idx].can_reach_interesting_order & ~ignored_orderings;
505 return (a & b) != a || (future_a & future_b) != future_a;
514 class OrderWithElementInserted;
581 friend bool operator==(
const NFSMEdge &a,
const NFSMEdge &b);
582 friend bool operator!=(
const NFSMEdge &a,
const NFSMEdge &b);
728 bool used_at_end,
table_map homogenize_tables);
767 THD *thd,
const Ordering &reduced_ordering,
bool used_at_end,
792 int *generation,
int extra_allowed_fd_idx);
806 void AddEdge(
THD *thd,
int state_idx,
int required_fd_idx,
816 int *start_point)
const;
Element_type * data()
Definition: sql_array.h:116
static Bounds_checked_array Alloc(MEM_ROOT *mem_root, size_t size)
Definition: sql_array.h:70
const_iterator cbegin() const
Returns a pointer to the first element in the array.
Definition: sql_array.h:144
size_t size() const
Definition: sql_array.h:154
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:75
const_iterator cend() const
Returns a pointer to the past-the-end element in the array.
Definition: sql_array.h:146
Base class that is used to represent any kind of expression in a relational query.
Definition: item.h:853
Definition: interesting_orders.h:314
void SetHeadForAggregates(Bounds_checked_array< ItemHandle > head)
Definition: interesting_orders.h:386
void AddRollupFromOrder(THD *thd, int state_idx, const Ordering &ordering)
Definition: interesting_orders.cc:1703
void AddEdge(THD *thd, int state_idx, int required_fd_idx, const Ordering &ordering)
Definition: interesting_orders.cc:1299
bool ItemHandleBeforeInGroup(ItemHandle a, ItemHandle b) const
Definition: interesting_orders.h:715
void CreateOrderingsFromGroupings(THD *thd)
We don't currently have any operators that only group and do not sort (e.g.
Definition: interesting_orders.cc:932
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:622
Mem_root_array< OrderingWithInfo > m_orderings
Definition: interesting_orders.h:681
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:825
Bounds_checked_array< int > m_optimized_fd_mapping
Definition: interesting_orders.h:700
StateIndex SetOrder(int ordering_idx) const
Definition: interesting_orders.h:429
int num_orderings() const
Definition: interesting_orders.h:358
bool DoesFollowOrder(StateIndex state_idx, int ordering_idx) const
Definition: interesting_orders.h:456
bool m_rollup
Definition: interesting_orders.h:560
int m_longest_ordering
Definition: interesting_orders.h:684
Mem_root_array< DFSMState > m_dfsm_states
Definition: interesting_orders.h:692
bool m_built
Definition: interesting_orders.h:516
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:1110
friend bool operator==(const NFSMEdge &a, const NFSMEdge &b)
Definition: interesting_orders.h:854
std::string PrintOrdering(const Ordering &ordering) const
Definition: interesting_orders.cc:2124
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:1093
int RemapOrderingIndex(int ordering_idx) const
Definition: interesting_orders.h:422
void FinalizeDFSMState(THD *thd, int state_idx)
Definition: interesting_orders.cc:1899
void AddGroupingFromRollup(THD *thd, int state_idx, const Ordering &ordering)
Definition: interesting_orders.cc:1683
friend bool operator!=(const NFSMEdge &a, const NFSMEdge &b)
Definition: interesting_orders.h:859
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:705
void PrintDFSMDottyGraph(std::string *trace) const
Definition: interesting_orders.cc:2275
bool ordering_is_relevant_for_sortahead(int ordering_idx) const
Definition: interesting_orders.h:364
void PreReduceOrderings(THD *thd)
Do safe reduction on all orderings (some of them may get merged by PruneUninterestingOrders() later),...
Definition: interesting_orders.cc:892
void SetRollup(bool rollup)
Definition: interesting_orders.h:393
void PrintFunctionalDependencies(std::string *trace)
Definition: interesting_orders.cc:2177
void CreateHomogenizedOrderings(THD *thd)
For each interesting ordering, see if we can homogenize it onto each table.
Definition: interesting_orders.cc:1020
Mem_root_array< NFSMState > m_states
Definition: interesting_orders.h:689
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:1176
int AddOrderingInternal(THD *thd, Ordering order, OrderingWithInfo::Type type, bool used_at_end, table_map homogenize_tables)
Definition: interesting_orders.cc:212
void AddFDsFromConstItems(THD *thd)
Try to add FDs from items that are constant by themselves, e.g.
Definition: interesting_orders.cc:736
int AddFunctionalDependency(THD *thd, FunctionalDependency fd)
Definition: interesting_orders.cc:267
void PrintNFSMDottyGraph(std::string *trace) const
Definition: interesting_orders.cc:2241
bool FunctionalDependencyApplies(const FunctionalDependency &fd, const Ordering &ordering, int *start_point) const
Definition: interesting_orders.cc:1317
Mem_root_array< DFSMEdge > m_dfsm_edges
Definition: interesting_orders.h:693
void ReturnElements(Ordering::Elements elements)
Return an Ordering::Elements object with size()==m_longest_ordering to m_elements_pool.
Definition: interesting_orders.h:837
bool MoreOrderedThan(StateIndex a_idx, StateIndex b_idx, std::bitset< kMaxSupportedOrderings > ignored_orderings) const
Definition: interesting_orders.h:493
bool AlwaysActiveFD(int fd_idx)
Definition: interesting_orders.cc:1894
void BuildNFSM(THD *thd)
Definition: interesting_orders.cc:1476
void BuildEquivalenceClasses()
Definition: interesting_orders.cc:519
int AddArtificialState(THD *thd, const Ordering &ordering)
Definition: interesting_orders.cc:1283
int StateIndex
Definition: interesting_orders.h:427
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:1763
Item * item(ItemHandle item) const
Definition: interesting_orders.h:324
const Ordering & ordering(int ordering_idx) const
Definition: interesting_orders.h:360
void FindElementsThatCanBeAddedByFDs()
Definition: interesting_orders.cc:797
void PrintInterestingOrders(std::string *trace)
Definition: interesting_orders.cc:2194
StateIndex ApplyFDs(StateIndex state_idx, FunctionalDependencySet fds) const
Definition: interesting_orders.cc:360
Bounds_checked_array< int > m_optimized_ordering_mapping
Definition: interesting_orders.h:697
int AddOrdering(THD *thd, Ordering order, bool interesting, bool used_at_end, table_map homogenize_tables)
Definition: interesting_orders.h:349
Ordering::Elements RetrieveElements(MEM_ROOT *mem_root)
Fetch an Ordering::Elements object with size()==m_longest_ordering.
Definition: interesting_orders.h:823
void ExpandThroughAlwaysActiveFDs(Mem_root_array< int > *nfsm_states, int *generation, int extra_allowed_fd_idx)
Definition: interesting_orders.cc:1916
void PruneFDs(THD *thd)
Definition: interesting_orders.cc:450
void PruneUninterestingOrders(THD *thd)
Try to get rid of uninteresting orders, possibly by discarding irrelevant suffixes and merging them w...
Definition: interesting_orders.cc:392
void AddFDsFromAggregateItems(THD *thd)
Definition: interesting_orders.cc:759
Bounds_checked_array< ItemHandle > CollectHeadForStaticWindowFunction(THD *thd, ItemHandle argument_item, Window *window)
Definition: interesting_orders.cc:581
Mem_root_array< ItemInfo > m_items
Definition: interesting_orders.h:551
ItemHandle GetHandle(Item *item)
Definition: interesting_orders.cc:1187
void FindInitialStatesForOrdering()
Definition: interesting_orders.cc:2112
void AddGroupingFromOrder(THD *thd, int state_idx, const Ordering &ordering)
Definition: interesting_orders.cc:1660
LogicalOrderings(THD *thd)
Definition: interesting_orders.cc:190
Mem_root_array< FunctionalDependency > m_fds
Definition: interesting_orders.h:686
void ConvertNFSMToDFSM(THD *thd)
From the NFSM, convert an equivalent DFSM.
Definition: interesting_orders.cc:1960
Bounds_checked_array< ItemHandle > m_aggregate_head
Definition: interesting_orders.h:555
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:438
void RecanonicalizeGroupings()
Definition: interesting_orders.cc:569
std::string PrintFunctionalDependency(const FunctionalDependency &fd, bool html) const
Definition: interesting_orders.cc:2144
void CreateOrderingsFromRollups(THD *thd)
bool ItemBeforeInGroup(const OrderElement &a, const OrderElement &b) const
Definition: interesting_orders.h:721
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:1225
int num_fds() const
Definition: interesting_orders.h:375
void Build(THD *thd, std::string *trace)
Definition: interesting_orders.cc:298
A typesafe replacement for DYNAMIC_ARRAY.
Definition: mem_root_array.h:426
A scope-guard class for allocating an Ordering::Elements instance which is automatically returned to ...
Definition: interesting_orders.cc:66
Represents a (potentially interesting) ordering, rollup or (non-rollup) grouping.
Definition: interesting_orders.h:159
Elements & GetElements()
Definition: interesting_orders.h:221
size_t size() const
Definition: interesting_orders.h:226
Kind
The kind of ordering that an Ordering instance may represent.
Definition: interesting_orders.h:167
@ 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:198
Kind GetKind() const
Definition: interesting_orders.h:211
Ordering()
Definition: interesting_orders.h:185
Ordering(Elements elements, Kind kind)
Definition: interesting_orders.h:187
void Deduplicate()
Remove duplicate entries, in-place.
Definition: interesting_orders.cc:154
Bounds_checked_array< OrderElement > Elements
This type hold the individual elements of the ordering.
Definition: interesting_orders.h:164
Kind m_kind
The kind of this ordering.
Definition: interesting_orders.h:238
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:206
Elements m_elements
The ordering terms.
Definition: interesting_orders.h:235
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:245
Ordering(const Ordering &other)
Copy constructor. Only defined explicitly to check Valid().
Definition: interesting_orders.h:192
bool Valid() const
Definition: interesting_orders.cc:165
const Elements & GetElements() const
Definition: interesting_orders.h:216
For each client connection we create a separate thread with THD serving as a thread/connection descri...
Definition: sql_lexer_thd.h:34
Represents the (explicit) window of a SQL 2003 section 7.11 <window clause>, or the implicit (inlined...
Definition: window.h:105
static MEM_ROOT mem_root
Definition: client_plugin.cc:110
static bool equal(const Item *i1, const Item *i2, const Field *f2)
Definition: sql_select.cc:3813
bool operator!=(const Ordering &a, const Ordering &b)
Definition: interesting_orders.h:253
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:245
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:420
required string type
Definition: replication_group_member_actions.proto:34
Definition: interesting_orders.h:257
ItemHandle tail
Definition: interesting_orders.h:289
@ FD
Definition: interesting_orders.h:279
@ DECAY
Definition: interesting_orders.h:272
@ EQUIVALENCE
Definition: interesting_orders.h:285
bool always_active
Definition: interesting_orders.h:311
Bounds_checked_array< ItemHandle > head
Definition: interesting_orders.h:288
enum FunctionalDependency::@101 type
Definition: interesting_orders.h:625
int state_idx
Definition: interesting_orders.h:627
int required_fd_idx
Definition: interesting_orders.h:626
const DFSMState * state(const LogicalOrderings *orderings) const
Definition: interesting_orders.h:633
const FunctionalDependency * required_fd(const LogicalOrderings *orderings) const
Definition: interesting_orders.h:629
Definition: interesting_orders.h:600
FunctionalDependencySet can_use_fd
Definition: interesting_orders.h:622
Mem_root_array< int > nfsm_states
Definition: interesting_orders.h:602
std::bitset< kMaxSupportedOrderings > follows_interesting_order
Definition: interesting_orders.h:610
Mem_root_array< int > outgoing_edges
Definition: interesting_orders.h:601
std::bitset< kMaxSupportedOrderings > can_reach_interesting_order
Definition: interesting_orders.h:617
Bounds_checked_array< int > next_state
Definition: interesting_orders.h:607
Definition: interesting_orders.h:518
bool used_desc
Definition: interesting_orders.h:546
bool used_in_grouping
Definition: interesting_orders.h:547
ItemHandle canonical_item
Definition: interesting_orders.h:532
bool can_be_added_by_fd
Definition: interesting_orders.h:538
bool used_asc
Definition: interesting_orders.h:545
Item * item
Definition: interesting_orders.h:520
Definition: interesting_orders.h:562
const NFSMState * state(const LogicalOrderings *orderings) const
Definition: interesting_orders.h:576
int required_fd_idx
Definition: interesting_orders.h:567
const FunctionalDependency * required_fd(const LogicalOrderings *orderings) const
Definition: interesting_orders.h:572
int state_idx
Definition: interesting_orders.h:570
Definition: interesting_orders.h:584
@ ARTIFICIAL
Definition: interesting_orders.h:585
@ DELETED
Definition: interesting_orders.h:585
@ INTERESTING
Definition: interesting_orders.h:585
Mem_root_array< NFSMEdge > outgoing_edges
Definition: interesting_orders.h:586
std::bitset< kMaxSupportedOrderings > can_reach_interesting_order
Definition: interesting_orders.h:591
int satisfied_ordering_idx
Definition: interesting_orders.h:588
enum LogicalOrderings::NFSMState::@102 type
int seen
Definition: interesting_orders.h:598
Ordering satisfied_ordering
Definition: interesting_orders.h:587
Definition: interesting_orders.h:638
StateIndex state_idx
Definition: interesting_orders.h:678
Ordering ordering
Definition: interesting_orders.h:639
bool used_at_end
Definition: interesting_orders.h:672
Type
Definition: interesting_orders.h:644
@ UNINTERESTING
Definition: interesting_orders.h:669
@ INTERESTING
Definition: interesting_orders.h:647
@ HOMOGENIZED
Definition: interesting_orders.h:662
enum LogicalOrderings::OrderingWithInfo::Type type
table_map homogenize_tables
Definition: interesting_orders.h:675
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