MySQL  8.0.18
Source Code Documentation
sql_select.h File Reference
#include <limits.h>
#include <stddef.h>
#include <sys/types.h>
#include <functional>
#include "my_alloc.h"
#include "my_base.h"
#include "my_bitmap.h"
#include "my_dbug.h"
#include "my_inttypes.h"
#include "my_sqlcommand.h"
#include "my_table_map.h"
#include "sql/field.h"
#include "sql/item.h"
#include "sql/item_cmpfunc.h"
#include "sql/opt_costmodel.h"
#include "sql/sql_bitmap.h"
#include "sql/sql_class.h"
#include "sql/sql_cmd_dml.h"
#include "sql/sql_const.h"
#include "sql/sql_executor.h"
#include "sql/sql_lex.h"
#include "sql/sql_opt_exec_shared.h"
#include "sql/system_variables.h"
#include "sql/table.h"

Go to the source code of this file.

Classes

class  List< T >
 
class  Sql_cmd_select
 
class  Key_use
 A Key_use represents an equality predicate of the form (table.column = val), where the column is indexed by keypart in key and val is either a constant, a column from other table, or an expression over column(s) from other table(s). More...
 
struct  POSITION
 A position of table within a join order. More...
 
class  JOIN_TAB
 Query optimization plan node. More...
 
class  Join_tab_compare_default
 "Less than" comparison function object used to compare two JOIN_TAB objects based on a number of factors in this order: More...
 
class  Join_tab_compare_straight
 "Less than" comparison function object used to compare two JOIN_TAB objects that are joined using STRAIGHT JOIN. More...
 
class  Join_tab_compare_embedded_first
 
class  store_key
 class to copying an field/item to a key struct More...
 
class  store_key_field
 
class  store_key_item
 
class  store_key_hash_item
 
class  store_key_json_item
 
class  store_key_const_item
 

Macros

#define LOWER_BITS(type, A)   ((type)(((type)1 << (A)) - 1))
 Returns a constant of type 'type' with the 'A' lowest-weight bits set. More...
 
#define KEY_OPTIMIZE_EXISTS   1
 
#define KEY_OPTIMIZE_REF_OR_NULL   2
 
#define FT_KEYPART   (MAX_REF_PARTS + 10)
 
#define SJ_OPT_NONE   0
 
#define SJ_OPT_DUPS_WEEDOUT   1
 
#define SJ_OPT_LOOSE_SCAN   2
 
#define SJ_OPT_FIRST_MATCH   3
 
#define SJ_OPT_MATERIALIZE_LOOKUP   4
 
#define SJ_OPT_MATERIALIZE_SCAN   5
 
#define ASSERT_BEST_REF_IN_JOIN_ORDER(join)
 Use this in a function which depends on best_ref listing tables in the final join order. More...
 

Typedefs

typedef ulonglong nested_join_map
 

Enumerations

enum  quick_type { QS_NONE, QS_RANGE, QS_DYNAMIC_RANGE }
 Bits describing quick select type. More...
 
enum  aggregate_evaluated { AGGR_COMPLETE, AGGR_REGULAR, AGGR_DELAYED, AGGR_EMPTY }
 

Functions

join_type calc_join_type (int quick_type)
 
bool sj_is_materialize_strategy (uint strategy)
 
void count_field_types (SELECT_LEX *select_lex, Temp_table_param *param, List< Item > &fields, bool reset_with_sum_func, bool save_sum_fields)
 Update TMP_TABLE_PARAM with count of the different type of fields. More...
 
uint find_shortest_key (TABLE *table, const Key_map *usable_keys)
 Find shortest key suitable for full table scan. More...
 
bool simple_pred (Item_func *func_item, Item **args, bool *inv_order)
 Test if the predicate compares a field with constants. More...
 
bool optimize_aggregated_query (THD *thd, SELECT_LEX *select, List< Item > &all_fields, Item *conds, aggregate_evaluated *decision)
 Substitute constants for some COUNT(), MIN() and MAX() functions in an aggregated (implicitly grouped) query. More...
 
int refpos_order_cmp (const void *arg, const void *a, const void *b)
 
bool error_if_full_join (JOIN *join)
 Give error if we some tables are done with a full join. More...
 
bool handle_query (THD *thd, LEX *lex, Query_result *result, ulonglong added_options, ulonglong removed_options)
 Handle data manipulation query which is not represented by Sql_cmd_dml class. More...
 
bool set_statement_timer (THD *thd)
 Set the time until the currently running statement is aborted. More...
 
void reset_statement_timer (THD *thd)
 Deactivate the timer associated with the statement that was executed. More...
 
void free_underlaid_joins (THD *thd, SELECT_LEX *select)
 Free joins of subselect of this select. More...
 
void calc_used_field_length (TABLE *table, bool needs_rowid, uint *p_used_fields, uint *p_used_fieldlength, uint *p_used_blobs, bool *p_used_null_fields, bool *p_used_uneven_bit_fields)
 Find how much space the prevous read not const tables takes in cache. More...
 
ORDERsimple_remove_const (ORDER *order, Item *where)
 Filter out ORDER items those are equal to constants in WHERE. More...
 
bool const_expression_in_where (Item *cond, Item *comp_item, Field *comp_field=NULL, Item **const_item=NULL)
 Test if a field or an item is equal to a constant value in WHERE. More...
 
bool test_if_subpart (ORDER *a, ORDER *b)
 Return 1 if second is a subpart of first argument. More...
 
void calc_group_buffer (JOIN *join, ORDER *group)
 calc how big buffer we need for comparing group entries. More...
 
bool make_join_readinfo (JOIN *join, uint no_jbuf_after)
 Plan refinement stage: do various setup things for the executor. More...
 
bool create_ref_for_key (JOIN *join, JOIN_TAB *j, Key_use *org_keyuse, table_map used_tables)
 Setup a ref access for looking up rows via an index (a key). More...
 
bool types_allow_materialization (Item *outer, Item *inner)
 Check if two items are compatible wrt. More...
 
bool and_conditions (Item **e1, Item *e2)
 Extend e1 by AND'ing e2 to the condition e1 points to. More...
 
static Itemand_items (Item *cond, Item *item)
 Create a AND item of two existing items. More...
 
static Item_bool_funcand_items (Item *cond, Item_bool_func *item)
 A variant of the above, guaranteed to return Item_bool_func. More...
 
uint actual_key_parts (const KEY *key_info)
 Returns number of key parts depending on OPTIMIZER_SWITCH_USE_INDEX_EXTENSIONS flag. More...
 
uint get_index_for_order (ORDER_with_src *order, QEP_TAB *tab, ha_rows limit, bool *need_sort, bool *reverse)
 Find a key to apply single table UPDATE/DELETE by a given ORDER. More...
 
int test_if_order_by_key (ORDER_with_src *order, TABLE *table, uint idx, uint *used_key_parts, bool *skip_quick)
 Test if one can use the key to resolve ordering. More...
 
bool test_if_cheaper_ordering (const JOIN_TAB *tab, ORDER_with_src *order, TABLE *table, Key_map usable_keys, int key, ha_rows select_limit, int *new_key, int *new_key_direction, ha_rows *new_select_limit, uint *new_used_key_parts=NULL, uint *saved_best_key_parts=NULL)
 Find a cheaper access key than a given key. More...
 
void calc_length_and_keyparts (Key_use *keyuse, JOIN_TAB *tab, const uint key, table_map used_tables, Key_use **chosen_keyuses, uint *length_out, uint *keyparts_out, table_map *dep_map, bool *maybe_null)
 Calculate properties of ref key: key length, number of used key parts, dependency map, possibility of null. More...
 
SJ_TMP_TABLEcreate_sj_tmp_table (THD *thd, JOIN *join, SJ_TMP_TABLE::TAB *first_tab, SJ_TMP_TABLE::TAB *last_tab)
 Set up the support structures (NULL bits, row offsets, etc.) for a semijoin duplicate weedout table. More...
 

Macro Definition Documentation

◆ ASSERT_BEST_REF_IN_JOIN_ORDER

#define ASSERT_BEST_REF_IN_JOIN_ORDER (   join)
Value:
do { \
DBUG_ASSERT(join->tables == 0 || (join->best_ref && !join->join_tab)); \
} while (0)
std::string join(Container cont, const std::string &delim)
join elements of an container into a string seperated by a delimiter.
Definition: string.h:144

Use this in a function which depends on best_ref listing tables in the final join order.

If 'tables==0', one is not expected to consult best_ref cells, and best_ref may not even have been allocated.

◆ FT_KEYPART

#define FT_KEYPART   (MAX_REF_PARTS + 10)

◆ KEY_OPTIMIZE_EXISTS

#define KEY_OPTIMIZE_EXISTS   1

◆ KEY_OPTIMIZE_REF_OR_NULL

#define KEY_OPTIMIZE_REF_OR_NULL   2

◆ LOWER_BITS

#define LOWER_BITS (   type,
 
)    ((type)(((type)1 << (A)) - 1))

Returns a constant of type 'type' with the 'A' lowest-weight bits set.

Example: LOWER_BITS(uint, 3) == 7. Requirement: A < sizeof(type) * 8.

◆ SJ_OPT_DUPS_WEEDOUT

#define SJ_OPT_DUPS_WEEDOUT   1

◆ SJ_OPT_FIRST_MATCH

#define SJ_OPT_FIRST_MATCH   3

◆ SJ_OPT_LOOSE_SCAN

#define SJ_OPT_LOOSE_SCAN   2

◆ SJ_OPT_MATERIALIZE_LOOKUP

#define SJ_OPT_MATERIALIZE_LOOKUP   4

◆ SJ_OPT_MATERIALIZE_SCAN

#define SJ_OPT_MATERIALIZE_SCAN   5

◆ SJ_OPT_NONE

#define SJ_OPT_NONE   0

Typedef Documentation

◆ nested_join_map

Enumeration Type Documentation

◆ aggregate_evaluated

Enumerator
AGGR_COMPLETE 
AGGR_REGULAR 
AGGR_DELAYED 
AGGR_EMPTY 

◆ quick_type

enum quick_type

Bits describing quick select type.

Enumerator
QS_NONE 
QS_RANGE 
QS_DYNAMIC_RANGE 

Function Documentation

◆ and_items() [1/2]

static Item* and_items ( Item cond,
Item item 
)
inlinestatic

Create a AND item of two existing items.

A new Item_cond_and item is created with the two supplied items as arguments.

Note
About handling of null pointers as arguments: if the first argument is a null pointer, then the item given as second argument is returned (no new Item_cond_and item is created). The second argument must not be a null pointer.
Parameters
condthe first argument to the new AND condition
itemthe second argument to the new AND condtion
Returns
the new AND item

◆ and_items() [2/2]

static Item_bool_func* and_items ( Item cond,
Item_bool_func item 
)
inlinestatic

A variant of the above, guaranteed to return Item_bool_func.

◆ optimize_aggregated_query()

bool optimize_aggregated_query ( THD thd,
SELECT_LEX select,
List< Item > &  all_fields,
Item conds,
aggregate_evaluated decision 
)

Substitute constants for some COUNT(), MIN() and MAX() functions in an aggregated (implicitly grouped) query.

Parameters
[in]thdthread handler
[in]selectquery block
[in]all_fieldsAll fields to be returned
[in]condsWHERE clause
[out]decisionoutcome for successful execution = AGGR_REGULAR regular execution required = AGGR_COMPLETE values available = AGGR_DELAYED execution with ha_records() required = AGGR_EMPTY source tables empty, aggregates are NULL or zero (for COUNT)
Returns
false if success, true if error

This function is called for queries with aggregate functions and no GROUP BY, thus the result set will contain a single row only.

First, the function will analyze the source tables and WHERE clause to see whether the query qualifies for optimization. If not, the decision AGGR_REGULAR is returned.

Second, the function walks over all expressions in the SELECT list. If the expression can be optimized with a storage engine operation that is O(1) (MIN or MAX) or O(0) (instant COUNT), the value is looked up and inserted in the value buffer, and the corresponding Item is marked as being const. If the expression is a COUNT operation that can be evaluated efficiently by the storage manager (but still O(N)), indicated with HA_COUNT_ROWS_INSTANT, it will be marked as such.

When all SELECT list expressions have been processed, there are four possible outcomes:

  • An empty result from the source tables is indicated, and the output state is AGGR_EMPTY. Notice that the result of aggregation is still one row, filled with zero for COUNT operations and NULL values for all other expressions.
  • All expressions have been given values, indicated with output state AGGR_COMPLETE.
  • All expressions have been given values, except for one or more COUNT operations that will be evaluated in execution. This is indicated with AGGR_DELAYED.
  • Some expressions must be evaluated as part of a regular execution, indicated with AGGR_REGULAR. Notice that some of the expressions may have been given values and are marked as const, but no expressions will be candidates for delayed execution.

◆ refpos_order_cmp()

int refpos_order_cmp ( const void *  arg,
const void *  a,
const void *  b 
)

◆ simple_pred()

bool simple_pred ( Item_func func_item,
Item **  args,
bool inv_order 
)

Test if the predicate compares a field with constants.

Parameters
func_itemPredicate item
[out]argsHere we store the field followed by constants
[out]inv_orderIs set to 1 if the predicate is of the form 'const op field'
Return values
0func_item is a simple predicate: a field is compared with constants
1Otherwise

◆ sj_is_materialize_strategy()

bool sj_is_materialize_strategy ( uint  strategy)
inline