24#ifndef SQL_JOIN_OPTIMIZER_INTERESTING_ORDERS_H
25#define SQL_JOIN_OPTIMIZER_INTERESTING_ORDERS_H
356 used_at_end, homogenize_tables);
367 return !
m_orderings[ordering_idx].ordering.GetElements().empty() &&
441 fd_set.set(new_fd_idx - 1);
457 if (ordering_idx == 0) {
463 return m_dfsm_states[state_idx].follows_interesting_order.test(
494 std::bitset<kMaxSupportedOrderings> ignored_orderings)
const {
496 std::bitset<kMaxSupportedOrderings> a =
497 m_dfsm_states[a_idx].follows_interesting_order & ~ignored_orderings;
498 std::bitset<kMaxSupportedOrderings> b =
499 m_dfsm_states[b_idx].follows_interesting_order & ~ignored_orderings;
500 std::bitset<kMaxSupportedOrderings> future_a =
501 m_dfsm_states[a_idx].can_reach_interesting_order & ~ignored_orderings;
502 std::bitset<kMaxSupportedOrderings> future_b =
503 m_dfsm_states[b_idx].can_reach_interesting_order & ~ignored_orderings;
504 return (a & b) != a || (future_a & future_b) != future_a;
513 class OrderWithElementInserted;
580 friend bool operator==(
const NFSMEdge &a,
const NFSMEdge &b);
581 friend bool operator!=(
const NFSMEdge &a,
const NFSMEdge &b);
727 bool used_at_end,
table_map homogenize_tables);
766 THD *thd,
const Ordering &reduced_ordering,
bool used_at_end,
791 int *generation,
int extra_allowed_fd_idx);
805 void AddEdge(
THD *thd,
int state_idx,
int required_fd_idx,
815 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:936
Definition: interesting_orders.h:315
void SetHeadForAggregates(Bounds_checked_array< ItemHandle > head)
Definition: interesting_orders.h:388
void AddRollupFromOrder(THD *thd, int state_idx, const Ordering &ordering)
Definition: interesting_orders.cc:1705
void AddEdge(THD *thd, int state_idx, int required_fd_idx, const Ordering &ordering)
Definition: interesting_orders.cc:1301
bool ItemHandleBeforeInGroup(ItemHandle a, ItemHandle b) const
Definition: interesting_orders.h:714
void CreateOrderingsFromGroupings(THD *thd)
We don't currently have any operators that only group and do not sort (e.g.
Definition: interesting_orders.cc:934
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:624
Mem_root_array< OrderingWithInfo > m_orderings
Definition: interesting_orders.h:680
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:827
Bounds_checked_array< int > m_optimized_fd_mapping
Definition: interesting_orders.h:699
StateIndex SetOrder(int ordering_idx) const
Definition: interesting_orders.h:428
int num_orderings() const
Definition: interesting_orders.h:360
bool DoesFollowOrder(StateIndex state_idx, int ordering_idx) const
Definition: interesting_orders.h:455
bool m_rollup
Definition: interesting_orders.h:559
int m_longest_ordering
Definition: interesting_orders.h:683
Mem_root_array< DFSMState > m_dfsm_states
Definition: interesting_orders.h:691
bool m_built
Definition: interesting_orders.h:515
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:1112
friend bool operator==(const NFSMEdge &a, const NFSMEdge &b)
Definition: interesting_orders.h:853
std::string PrintOrdering(const Ordering &ordering) const
Definition: interesting_orders.cc:2126
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:1095
void PrintFunctionalDependencies(std::ostream *trace)
Definition: interesting_orders.cc:2179
int RemapOrderingIndex(int ordering_idx) const
Definition: interesting_orders.h:421
void FinalizeDFSMState(THD *thd, int state_idx)
Definition: interesting_orders.cc:1901
void AddGroupingFromRollup(THD *thd, int state_idx, const Ordering &ordering)
Definition: interesting_orders.cc:1685
void PrintDFSMDottyGraph(std::ostream *trace) const
Definition: interesting_orders.cc:2277
friend bool operator!=(const NFSMEdge &a, const NFSMEdge &b)
Definition: interesting_orders.h:858
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:704
int num_items() const
Definition: interesting_orders.h:326
bool ordering_is_relevant_for_sortahead(int ordering_idx) const
Definition: interesting_orders.h:366
void PreReduceOrderings(THD *thd)
Do safe reduction on all orderings (some of them may get merged by PruneUninterestingOrders() later),...
Definition: interesting_orders.cc:894
void SetRollup(bool rollup)
Definition: interesting_orders.h:395
void CreateHomogenizedOrderings(THD *thd)
For each interesting ordering, see if we can homogenize it onto each table.
Definition: interesting_orders.cc:1022
Mem_root_array< NFSMState > m_states
Definition: interesting_orders.h:688
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:1178
int AddOrderingInternal(THD *thd, Ordering order, OrderingWithInfo::Type type, bool used_at_end, table_map homogenize_tables)
Definition: interesting_orders.cc:214
void AddFDsFromConstItems(THD *thd)
Try to add FDs from items that are constant by themselves, e.g.
Definition: interesting_orders.cc:738
int AddFunctionalDependency(THD *thd, FunctionalDependency fd)
Definition: interesting_orders.cc:269
bool FunctionalDependencyApplies(const FunctionalDependency &fd, const Ordering &ordering, int *start_point) const
Definition: interesting_orders.cc:1319
Mem_root_array< DFSMEdge > m_dfsm_edges
Definition: interesting_orders.h:692
void ReturnElements(Ordering::Elements elements)
Return an Ordering::Elements object with size()==m_longest_ordering to m_elements_pool.
Definition: interesting_orders.h:836
bool MoreOrderedThan(StateIndex a_idx, StateIndex b_idx, std::bitset< kMaxSupportedOrderings > ignored_orderings) const
Definition: interesting_orders.h:492
bool AlwaysActiveFD(int fd_idx)
Definition: interesting_orders.cc:1896
void BuildNFSM(THD *thd)
Definition: interesting_orders.cc:1478
void PrintInterestingOrders(std::ostream *trace)
Definition: interesting_orders.cc:2196
void BuildEquivalenceClasses()
Definition: interesting_orders.cc:521
int AddArtificialState(THD *thd, const Ordering &ordering)
Definition: interesting_orders.cc:1285
int StateIndex
Definition: interesting_orders.h:426
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:1765
Item * item(ItemHandle item) const
Definition: interesting_orders.h:325
void PrintNFSMDottyGraph(std::ostream *trace) const
Definition: interesting_orders.cc:2243
const Ordering & ordering(int ordering_idx) const
Definition: interesting_orders.h:362
void FindElementsThatCanBeAddedByFDs()
Definition: interesting_orders.cc:799
StateIndex ApplyFDs(StateIndex state_idx, FunctionalDependencySet fds) const
Definition: interesting_orders.cc:362
Bounds_checked_array< int > m_optimized_ordering_mapping
Definition: interesting_orders.h:696
int AddOrdering(THD *thd, Ordering order, bool interesting, bool used_at_end, table_map homogenize_tables)
Definition: interesting_orders.h:351
Ordering::Elements RetrieveElements(MEM_ROOT *mem_root)
Fetch an Ordering::Elements object with size()==m_longest_ordering.
Definition: interesting_orders.h:822
void ExpandThroughAlwaysActiveFDs(Mem_root_array< int > *nfsm_states, int *generation, int extra_allowed_fd_idx)
Definition: interesting_orders.cc:1918
void PruneFDs(THD *thd)
Definition: interesting_orders.cc:452
void PruneUninterestingOrders(THD *thd)
Try to get rid of uninteresting orders, possibly by discarding irrelevant suffixes and merging them w...
Definition: interesting_orders.cc:394
void AddFDsFromAggregateItems(THD *thd)
Definition: interesting_orders.cc:761
Bounds_checked_array< ItemHandle > CollectHeadForStaticWindowFunction(THD *thd, ItemHandle argument_item, Window *window)
Definition: interesting_orders.cc:583
Mem_root_array< ItemInfo > m_items
Definition: interesting_orders.h:550
ItemHandle GetHandle(Item *item)
Definition: interesting_orders.cc:1189
void FindInitialStatesForOrdering()
Definition: interesting_orders.cc:2114
void AddGroupingFromOrder(THD *thd, int state_idx, const Ordering &ordering)
Definition: interesting_orders.cc:1662
LogicalOrderings(THD *thd)
Definition: interesting_orders.cc:192
Mem_root_array< FunctionalDependency > m_fds
Definition: interesting_orders.h:685
void ConvertNFSMToDFSM(THD *thd)
From the NFSM, convert an equivalent DFSM.
Definition: interesting_orders.cc:1962
Bounds_checked_array< ItemHandle > m_aggregate_head
Definition: interesting_orders.h:554
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:437
void RecanonicalizeGroupings()
Definition: interesting_orders.cc:571
void Build(THD *thd)
Definition: interesting_orders.cc:300
std::string PrintFunctionalDependency(const FunctionalDependency &fd, bool html) const
Definition: interesting_orders.cc:2146
void CreateOrderingsFromRollups(THD *thd)
bool ItemBeforeInGroup(const OrderElement &a, const OrderElement &b) const
Definition: interesting_orders.h:720
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:1227
int num_fds() const
Definition: interesting_orders.h:377
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:68
Represents a (potentially interesting) ordering, rollup or (non-rollup) grouping.
Definition: interesting_orders.h:160
Elements & GetElements()
Definition: interesting_orders.h:222
size_t size() const
Definition: interesting_orders.h:227
Kind
The kind of ordering that an Ordering instance may represent.
Definition: interesting_orders.h:168
@ 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:199
Kind GetKind() const
Definition: interesting_orders.h:212
Ordering()
Definition: interesting_orders.h:186
Ordering(Elements elements, Kind kind)
Definition: interesting_orders.h:188
void Deduplicate()
Remove duplicate entries, in-place.
Definition: interesting_orders.cc:156
Bounds_checked_array< OrderElement > Elements
This type hold the individual elements of the ordering.
Definition: interesting_orders.h:165
Kind m_kind
The kind of this ordering.
Definition: interesting_orders.h:239
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:207
Elements m_elements
The ordering terms.
Definition: interesting_orders.h:236
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:246
Ordering(const Ordering &other)
Copy constructor. Only defined explicitly to check Valid().
Definition: interesting_orders.h:193
bool Valid() const
Definition: interesting_orders.cc:167
const Elements & GetElements() const
Definition: interesting_orders.h:217
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:3860
bool operator!=(const Ordering &a, const Ordering &b)
Definition: interesting_orders.h:254
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:246
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:258
ItemHandle tail
Definition: interesting_orders.h:290
enum FunctionalDependency::@109 type
bool always_active
Definition: interesting_orders.h:312
Bounds_checked_array< ItemHandle > head
Definition: interesting_orders.h:289
@ FD
Definition: interesting_orders.h:280
@ DECAY
Definition: interesting_orders.h:273
@ EQUIVALENCE
Definition: interesting_orders.h:286
Definition: interesting_orders.h:624
int state_idx
Definition: interesting_orders.h:626
int required_fd_idx
Definition: interesting_orders.h:625
const DFSMState * state(const LogicalOrderings *orderings) const
Definition: interesting_orders.h:632
const FunctionalDependency * required_fd(const LogicalOrderings *orderings) const
Definition: interesting_orders.h:628
Definition: interesting_orders.h:599
FunctionalDependencySet can_use_fd
Definition: interesting_orders.h:621
Mem_root_array< int > nfsm_states
Definition: interesting_orders.h:601
std::bitset< kMaxSupportedOrderings > follows_interesting_order
Definition: interesting_orders.h:609
Mem_root_array< int > outgoing_edges
Definition: interesting_orders.h:600
std::bitset< kMaxSupportedOrderings > can_reach_interesting_order
Definition: interesting_orders.h:616
Bounds_checked_array< int > next_state
Definition: interesting_orders.h:606
Definition: interesting_orders.h:517
bool used_desc
Definition: interesting_orders.h:545
bool used_in_grouping
Definition: interesting_orders.h:546
ItemHandle canonical_item
Definition: interesting_orders.h:531
bool can_be_added_by_fd
Definition: interesting_orders.h:537
bool used_asc
Definition: interesting_orders.h:544
Item * item
Definition: interesting_orders.h:519
Definition: interesting_orders.h:561
const NFSMState * state(const LogicalOrderings *orderings) const
Definition: interesting_orders.h:575
int required_fd_idx
Definition: interesting_orders.h:566
const FunctionalDependency * required_fd(const LogicalOrderings *orderings) const
Definition: interesting_orders.h:571
int state_idx
Definition: interesting_orders.h:569
Definition: interesting_orders.h:583
enum LogicalOrderings::NFSMState::@110 type
Mem_root_array< NFSMEdge > outgoing_edges
Definition: interesting_orders.h:585
std::bitset< kMaxSupportedOrderings > can_reach_interesting_order
Definition: interesting_orders.h:590
int satisfied_ordering_idx
Definition: interesting_orders.h:587
int seen
Definition: interesting_orders.h:597
Ordering satisfied_ordering
Definition: interesting_orders.h:586
@ ARTIFICIAL
Definition: interesting_orders.h:584
@ DELETED
Definition: interesting_orders.h:584
@ INTERESTING
Definition: interesting_orders.h:584
Definition: interesting_orders.h:637
StateIndex state_idx
Definition: interesting_orders.h:677
Ordering ordering
Definition: interesting_orders.h:638
bool used_at_end
Definition: interesting_orders.h:671
Type
Definition: interesting_orders.h:643
@ UNINTERESTING
Definition: interesting_orders.h:668
@ INTERESTING
Definition: interesting_orders.h:646
@ HOMOGENIZED
Definition: interesting_orders.h:661
enum LogicalOrderings::OrderingWithInfo::Type type
table_map homogenize_tables
Definition: interesting_orders.h:674
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