|
drizzle
C++ MySQL-protocol-compatible database server
|
#include <quick_group_min_max_select.h>
Public Member Functions | |
| QuickGroupMinMaxSelect (Table *table, Join *join, bool have_min, bool have_max, KeyPartInfo *min_max_arg_part, uint32_t group_prefix_len, uint32_t group_key_parts, uint32_t used_key_parts, KeyInfo *index_info, uint use_index, double read_cost, ha_rows records, uint key_infix_len, unsigned char *key_infix, memory::Root *parent_alloc) | |
| bool | add_range (SEL_ARG *sel_range) |
| void | update_key_stat () |
| void | adjust_prefix_ranges () |
| bool | alloc_buffers () |
| int | init () |
| int | reset () |
| int | get_next () |
| bool | reverse_sorted () const |
| bool | unique_key_range () const |
| int | get_type () const |
| void | add_keys_and_lengths (std::string *key_names, std::string *used_lengths) |
| Public Member Functions inherited from drizzled::optimizer::QuickSelectInterface | |
| virtual void | range_end () |
| virtual int | init_ror_merged_scan (bool) |
| virtual void | save_last_pos () |
| virtual void | add_info_string (std::string *) |
| virtual bool | is_keys_used (const boost::dynamic_bitset<> &fields) |
Public Attributes | |
| memory::Root | alloc |
| QuickRangeSelect * | quick_prefix_select |
| Public Attributes inherited from drizzled::optimizer::QuickSelectInterface | |
| bool | sorted |
| ha_rows | records |
| double | read_time |
| Table * | head |
| uint32_t | index |
| uint32_t | max_used_key_length |
| uint32_t | used_key_parts |
| unsigned char * | last_rowid |
| unsigned char * | record |
Private Member Functions | |
| int | next_prefix () |
| int | next_min_in_range () |
| int | next_max_in_range () |
| int | next_min () |
| int | next_max () |
| void | update_min_result () |
| void | update_max_result () |
Private Attributes | |
| Cursor * | cursor |
| Join * | join |
| KeyInfo * | index_info |
| unsigned char * | record |
| unsigned char * | tmp_record |
| unsigned char * | group_prefix |
| uint32_t | group_prefix_len |
| uint32_t | group_key_parts |
| unsigned char * | last_prefix |
| bool | have_min |
| bool | have_max |
| bool | seen_first_key |
| KeyPartInfo * | min_max_arg_part |
| uint32_t | min_max_arg_len |
| unsigned char * | key_infix |
| uint32_t | key_infix_len |
| std::vector< QuickRange * > | min_max_ranges |
| uint32_t | real_prefix_len |
| uint32_t | real_key_parts |
| List< Item_sum > * | min_functions |
| List< Item_sum > * | max_functions |
| List< Item_sum >::iterator * | min_functions_it |
| List< Item_sum >::iterator * | max_functions_it |
Additional Inherited Members | |
| Public Types inherited from drizzled::optimizer::QuickSelectInterface | |
| enum | { QS_TYPE_RANGE = 0 , QS_TYPE_INDEX_MERGE = 1 , QS_TYPE_RANGE_DESC = 2 , QS_TYPE_ROR_INTERSECT = 4 , QS_TYPE_ROR_UNION = 5 , QS_TYPE_GROUP_MIN_MAX = 6 } |
Index scan for GROUP-BY queries with MIN/MAX aggregate functions.
This class provides a specialized index access method for GROUP-BY queries of the forms:
SELECT A_1,...,A_k, [B_1,...,B_m], [MIN(C)], [MAX(C)] FROM T WHERE [RNG(A_1,...,A_p ; where p <= k)] [AND EQ(B_1,...,B_m)] [AND PC(C)] [AND PA(A_i1,...,A_iq)] GROUP BY A_1,...,A_k;
or
SELECT DISTINCT A_i1,...,A_ik FROM T WHERE [RNG(A_1,...,A_p ; where p <= k)] [AND PA(A_i1,...,A_iq)];
where all selected fields are parts of the same index. The class of queries that can be processed by this quick select is fully specified in the description of get_best_trp_group_min_max() in optimizer/range.cc.
The get_next() method directly produces result tuples, thus obviating the need to call end_send_group() because all grouping is already done inside get_next().
Since one of the requirements is that all select fields are part of the same index, this class produces only index keys, and not complete records.
Definition at line 66 of file quick_group_min_max_select.h.
| drizzled::optimizer::QuickGroupMinMaxSelect::QuickGroupMinMaxSelect | ( | Table * | table, |
| Join * | join, | ||
| bool | have_min, | ||
| bool | have_max, | ||
| KeyPartInfo * | min_max_arg_part, | ||
| uint32_t | group_prefix_len, | ||
| uint32_t | group_key_parts, | ||
| uint32_t | used_key_parts, | ||
| KeyInfo * | index_info, | ||
| uint | use_index, | ||
| double | read_cost, | ||
| ha_rows | records, | ||
| uint | key_infix_len, | ||
| unsigned char * | key_infix, | ||
| memory::Root * | parent_alloc ) |
Definition at line 43 of file quick_group_min_max_select.cc.
| drizzled::optimizer::QuickGroupMinMaxSelect::~QuickGroupMinMaxSelect | ( | ) |
Definition at line 144 of file quick_group_min_max_select.cc.
|
virtual |
Append comma-separated list of keys this quick select uses to key_names; append comma-separated list of corresponding used lengths to used_lengths.
SYNOPSIS QuickGroupMinMaxSelect::add_keys_and_lengths()
| [out] | key_names | Names of used indexes |
| [out] | used_lengths | Corresponding lengths of the index names |
DESCRIPTION This method is used by select_describe to extract the names of the indexes used by a quick select.
Implements drizzled::optimizer::QuickSelectInterface.
Definition at line 696 of file quick_group_min_max_select.cc.
| bool drizzled::optimizer::QuickGroupMinMaxSelect::add_range | ( | SEL_ARG * | sel_range | ) |
Eventually create and add a new quick range object.
SYNOPSIS QuickGroupMinMaxSelect::add_range()
| [in] | sel_range | Range object from which a new object is created |
NOTES Construct a new QuickRange object from a SEL_ARG object, and add it to the array min_max_ranges. If sel_arg is an infinite range, e.g. (x < 5 or x > 4), then skip it and do not construct a quick range.
RETURN
| false | on success |
| true | otherwise |
Definition at line 164 of file quick_group_min_max_select.cc.
| void drizzled::optimizer::QuickGroupMinMaxSelect::adjust_prefix_ranges | ( | ) |
Opens the ranges if there are more conditions in quick_prefix_select than the ones used for jumping through the prefixes.
SYNOPSIS QuickGroupMinMaxSelect::adjust_prefix_ranges()
NOTES quick_prefix_select is made over the conditions on the whole key. It defines a number of ranges of length x. However when jumping through the prefixes we use only the the first few most significant keyparts in the range key. However if there are more keyparts to follow the ones we are using we must make the condition on the key inclusive (because x < "ab" means x[0] < 'a' OR (x[0] == 'a' AND x[1] < 'b'). To achive the above we must turn off the NEAR_MIN/NEAR_MAX
Definition at line 197 of file quick_group_min_max_select.cc.
|
virtual |
Get the next key containing the MIN and/or MAX key for the next group.
SYNOPSIS QuickGroupMinMaxSelect::get_next()
DESCRIPTION The method finds the next subsequent group of records that satisfies the query conditions and finds the keys that contain the MIN/MAX values for the key part referenced by the MIN/MAX function(s). Once a group and its MIN/MAX values are found, store these values in the Item_sum objects for the MIN/MAX functions. The rest of the values in the result row are stored in the Item_field::result_field of each select field. If the query does not contain MIN and/or MAX functions, then the function only finds the group prefix, which is a query answer itself.
NOTES If both MIN and MAX are computed, then we use the fact that if there is no MIN key, there can't be a MAX key as well, so we can skip looking for a MAX key in this case.
RETURN
| 0 | on success |
| HA_ERR_END_OF_FILE | if returned all keys |
| other | if some error occurred |
Implements drizzled::optimizer::QuickSelectInterface.
Definition at line 272 of file quick_group_min_max_select.cc.
|
inlinevirtual |
Returns the type of this quick select - one of the QS_TYPE_* values
Implements drizzled::optimizer::QuickSelectInterface.
Definition at line 436 of file quick_group_min_max_select.h.
|
virtual |
Do post-constructor initialization.
SYNOPSIS QuickGroupMinMaxSelect::init()
DESCRIPTION The method performs initialization that cannot be done in the constructor such as memory allocations that may fail. It allocates memory for the group prefix and inifix buffers, and for the lists of MIN/MAX item to be updated during execution.
RETURN
| 0 | OK |
| other | Error code |
Implements drizzled::optimizer::QuickSelectInterface.
Definition at line 100 of file quick_group_min_max_select.cc.
|
private |
Retrieve the maximal key in the next group.
SYNOPSIS QuickGroupMinMaxSelect::next_max()
DESCRIPTION Lookup the maximal key of the group, and store it into this->record.
RETURN
| 0 | on success |
| HA_ERR_KEY_NOT_FOUND | if no MAX key was found that fulfills all conditions. |
| HA_ERR_END_OF_FILE | - "" - |
| other | if some error occurred |
Definition at line 417 of file quick_group_min_max_select.cc.
|
private |
Find the maximal key in a group that satisfies some range conditions for the min/max argument field.
SYNOPSIS QuickGroupMinMaxSelect::next_max_in_range()
DESCRIPTION Given the sequence of ranges min_max_ranges, find the maximal key that is in the right-most possible range. If there is no such key, then the current group does not have a MAX key that satisfies the WHERE clause. If a key is found, its value is stored in this->record.
RETURN
| 0 | on success |
| HA_ERR_KEY_NOT_FOUND | if there is no key with the given prefix in any of the ranges |
| HA_ERR_END_OF_FILE | - "" - |
| other | if some error |
Definition at line 590 of file quick_group_min_max_select.cc.
|
private |
Retrieve the minimal key in the next group.
SYNOPSIS QuickGroupMinMaxSelect::next_min()
DESCRIPTION Find the minimal key within this group such that the key satisfies the query conditions and NULL semantics. The found key is loaded into this->record.
IMPLEMENTATION Depending on the values of min_max_ranges.elements, key_infix_len, and whether there is a NULL in the MIN field, this function may directly return without any data access. In this case we use the key loaded into this->record by the call to this->next_prefix() just before this call.
RETURN
| 0 | on success |
| HA_ERR_KEY_NOT_FOUND | if no MIN key was found that fulfills all conditions. |
| HA_ERR_END_OF_FILE | - "" - |
| other | if some error occurred |
Definition at line 352 of file quick_group_min_max_select.cc.
|
private |
Find the minimal key in a group that satisfies some range conditions for the min/max argument field.
SYNOPSIS QuickGroupMinMaxSelect::next_min_in_range()
DESCRIPTION Given the sequence of ranges min_max_ranges, find the minimal key that is in the left-most possible range. If there is no such key, then the current group does not have a MIN key that satisfies the WHERE clause. If a key is found, its value is stored in this->record.
RETURN
| 0 | on success |
| HA_ERR_KEY_NOT_FOUND | if there is no key with the given prefix in any of the ranges |
| HA_ERR_END_OF_FILE | - "" - |
| other | if some error |
Definition at line 472 of file quick_group_min_max_select.cc.
|
private |
Determine the prefix of the next group.
SYNOPSIS QuickGroupMinMaxSelect::next_prefix()
DESCRIPTION Determine the prefix of the next group that satisfies the query conditions. If there is a range condition referencing the group attributes, use a QuickRangeSelect object to retrieve the first key that satisfies the condition. If there is a key infix of constants, append this infix immediately after the group attributes. The possibly extended prefix is stored in this->group_prefix. The first key of the found group is stored in this->record, on which relies this->next_min().
RETURN
| 0 | on success |
| HA_ERR_KEY_NOT_FOUND | if there is no key with the formed prefix |
| HA_ERR_END_OF_FILE | if there are no more keys |
| other | if some error occurred |
Definition at line 426 of file quick_group_min_max_select.cc.
|
virtual |
Initialize a quick group min/max select for key retrieval.
SYNOPSIS QuickGroupMinMaxSelect::reset()
DESCRIPTION Initialize the index chosen for access and find and store the prefix of the last group. The method is expensive since it performs disk access.
RETURN
| 0 | OK |
| other | Error code |
Implements drizzled::optimizer::QuickSelectInterface.
Definition at line 253 of file quick_group_min_max_select.cc.
|
inlinevirtual |
Implements drizzled::optimizer::QuickSelectInterface.
Definition at line 426 of file quick_group_min_max_select.h.
|
inlinevirtual |
Reimplemented from drizzled::optimizer::QuickSelectInterface.
Definition at line 431 of file quick_group_min_max_select.h.
| void drizzled::optimizer::QuickGroupMinMaxSelect::update_key_stat | ( | ) |
Determine the total number and length of the keys that will be used for index lookup.
SYNOPSIS QuickGroupMinMaxSelect::update_key_stat()
DESCRIPTION The total length of the keys used for index lookup depends on whether there are any predicates referencing the min/max argument, and/or if the min/max argument field can be NULL. This function does an optimistic analysis whether the search key might be extended by a constant for the min/max keypart. It is 'optimistic' because during actual execution it may happen that a particular range is skipped, and then a shorter key will be used. However this is data dependent and can't be easily estimated here.
RETURN None
Definition at line 209 of file quick_group_min_max_select.cc.
|
private |
Update all MAX function results with the newly found value.
SYNOPSIS QuickGroupMinMaxSelect::update_max_result()
DESCRIPTION The method iterates through all MAX functions and updates the result value of each function by calling Item_sum::reset(), which in turn picks the new result value from this->head->getInsertRecord(), previously updated by next_max(). The updated value is stored in a member variable of each of the Item_sum objects, depending on the value type.
IMPLEMENTATION The update must be done separately for MIN and MAX, immediately after next_max() was called, because both MIN and MAX take their result value from the same buffer this->head->getInsertRecord() (i.e. this->record).
RETURN None
Definition at line 688 of file quick_group_min_max_select.cc.
|
private |
Update all MIN function results with the newly found value.
SYNOPSIS QuickGroupMinMaxSelect::update_min_result()
DESCRIPTION The method iterates through all MIN functions and updates the result value of each function by calling Item_sum::reset(), which in turn picks the new result value from this->head->getInsertRecord(), previously updated by next_min(). The updated value is stored in a member variable of each of the Item_sum objects, depending on the value type.
IMPLEMENTATION The update must be done separately for MIN and MAX, immediately after next_min() was called and before next_max() is called, because both MIN and MAX take their result value from the same buffer this->head->getInsertRecord() (i.e. this->record).
RETURN None
Definition at line 680 of file quick_group_min_max_select.cc.
| memory::Root drizzled::optimizer::QuickGroupMinMaxSelect::alloc |
Memory pool for this and quick_prefix_select data.
Definition at line 101 of file quick_group_min_max_select.h.
|
private |
The Cursor used to get data.
Definition at line 71 of file quick_group_min_max_select.h.
|
private |
A number of keyparts in the group prefix
Definition at line 78 of file quick_group_min_max_select.h.
|
private |
Key prefix consisting of the GROUP fields.
Definition at line 76 of file quick_group_min_max_select.h.
|
private |
Length of the group prefix.
Definition at line 77 of file quick_group_min_max_select.h.
|
private |
a MIN, a MAX, or both.
Definition at line 81 of file quick_group_min_max_select.h.
|
private |
Specify whether we are computing
Definition at line 80 of file quick_group_min_max_select.h.
|
private |
The index chosen for data access
Definition at line 73 of file quick_group_min_max_select.h.
|
private |
Descriptor of the current query
Definition at line 72 of file quick_group_min_max_select.h.
|
private |
Infix of constants from equality predicates.
Definition at line 85 of file quick_group_min_max_select.h.
|
private |
Definition at line 86 of file quick_group_min_max_select.h.
|
private |
Prefix of the last group for detecting EOF.
Definition at line 79 of file quick_group_min_max_select.h.
Definition at line 91 of file quick_group_min_max_select.h.
|
private |
Definition at line 93 of file quick_group_min_max_select.h.
Definition at line 90 of file quick_group_min_max_select.h.
|
private |
Definition at line 92 of file quick_group_min_max_select.h.
|
private |
The keypart of the only argument field of all MIN/MAX functions. The length of the MIN/MAX argument field
Definition at line 84 of file quick_group_min_max_select.h.
|
private |
Definition at line 83 of file quick_group_min_max_select.h.
|
private |
Array of range ptrs for the MIN/MAX field.
Definition at line 87 of file quick_group_min_max_select.h.
| QuickRangeSelect* drizzled::optimizer::QuickGroupMinMaxSelect::quick_prefix_select |
For retrieval of group prefixes.
Definition at line 102 of file quick_group_min_max_select.h.
|
private |
A number of keyparts in the above value.
Definition at line 89 of file quick_group_min_max_select.h.
|
private |
Length of key prefix extended with key_infix.
Definition at line 88 of file quick_group_min_max_select.h.
|
private |
Buffer where the next record is returned.
Definition at line 74 of file quick_group_min_max_select.h.
|
private |
Denotes whether the first key was retrieved.
Definition at line 82 of file quick_group_min_max_select.h.
|
private |
Temporary storage for next_min(), next_max().
Definition at line 75 of file quick_group_min_max_select.h.