MySQL 8.4.2
Source Code Documentation
Query Optimizer

Enumerations

enum  Range_placement {
  RP_OUTSIDE_HIGH , RP_OUTSIDE_LOW , RP_ON_MIN , RP_ON_MAX ,
  RP_INSIDE , RP_INSIDE_TRUNCATED , RP_INSIDE_YEAR_HOLE , RP_ROUNDED_DOWN ,
  RP_ROUNDED_UP
}
 fold_condition uses analyze_field_constant to analyze constants in comparisons expressions with a view to fold the expression. More...
 

Functions

static bool round_fold_or_convert_dec (THD *thd, Item **const_val, Range_placement *place, Item_field *f, my_decimal *d, bool up, bool *discount_equal)
 Minion of analyze_int_field_constant. More...
 
static bool fold_or_convert_dec (THD *thd, Item **const_val, Range_placement *place, Item_field *f, my_decimal *d)
 Minion of analyze_int_field_constant. More...
 
static bool analyze_int_field_constant (THD *thd, Item_field *f, Item **const_val, Item_func::Functype ft, bool left_has_field, Range_placement *place, bool *discount_equal)
 Minion of analyze_field_constant for integer type fields. More...
 
static bool analyze_decimal_field_constant (THD *thd, const Item_field *f, Item **const_val, Item_func::Functype ft, Range_placement *place, bool *negative)
 Minion of analyze_field_constant for decimal type fields. More...
 
static bool analyze_real_field_constant (THD *thd, Item_field *f, Item **const_val, Range_placement *place)
 Minion of analyze_field_constant for real type fields. More...
 
static bool analyze_year_field_constant (THD *thd, Item **const_val, Range_placement *place)
 Minion of analyze_field_constant for YEAR type fields. More...
 
static bool analyze_timestamp_field_constant (THD *thd, const Item_field *f, Item **const_val, Range_placement *place)
 Minion of analyze_field_constant for TIMESTAMP, DATETIME, DATE fields. More...
 
static bool analyze_time_field_constant (THD *thd, Item **const_val)
 
static bool analyze_field_constant (THD *thd, Item_field *f, Item **const_val, Item_func *func, bool left_has_field, Range_placement *place, bool *discount_equal, bool *negative)
 Analyze a constant's placement within (or without) the type range of the field f. More...
 
static bool fold_or_simplify (THD *thd, Item *ref_or_field, Item_func::Functype ft, bool always_true, bool manifest_result, Item **retcond, Item::cond_result *cond_value)
 We have found that the constant's value makes the predicate always true or false (modulo field nullability), so simplify or remove the condition according to this table: More...
 
static bool fold_arguments (THD *thd, Item_func *func)
 
static bool fold_arguments (THD *thd, Item_cond *cond)
 Call fold_condition on all the conditions's arguments. More...
 
static Range_placement map_less_to_greater (Range_placement place)
 Switch places of low, high, max and min. More...
 
static bool adjust_cmp_op (THD *thd, Item_func *func, bool left_has_field, bool inverse, Item *ref_or_field, Item *c, Item **retcond)
 Possibly change comparison operator because we have rounded the constant in some direction. More...
 
static bool simplify_to_equal (THD *thd, Item *ref_or_field, Item *c, Item **retcond, Item::cond_result *cond_value)
 Replace condition in retcond with "=" (i.e. Item_func_eq) and set cond_value to Item::COND_OK. More...
 
bool fold_condition (THD *thd, Item *cond, Item **retcond, Item::cond_result *cond_value, bool manifest_result)
 Fold boolean condition {=, <>, >, >=, <, <=, <=>} involving constants and fields (or references to fields), possibly determining if the condition is always true or false based on type range of the field, or possibly simplifying >=, <= to = if constant lies on the range border. More...
 

Detailed Description

Enumeration Type Documentation

◆ Range_placement

fold_condition uses analyze_field_constant to analyze constants in comparisons expressions with a view to fold the expression.

The analysis will determine the range characteristic of the constant, as represented by one value of this set. In the simple case, if the constant is within the range of the field to which it is being compared, the value used is RP_INSIDE.

Enumerator
RP_OUTSIDE_HIGH 

The constant is larger than the highest value in the range.

RP_OUTSIDE_LOW 

The constant is lower than the lowest value in the range.

RP_ON_MIN 

The constant has the value of the lowest value in the range.

Only used for integer types and the YEAR type.

RP_ON_MAX 

The constant has the value of the highest value in the range.

Only used for integer types and the YEAR type.

RP_INSIDE 

The constant has a value within the range, but that is not on one of the borders if applicable for the type, cf.

RP_ON_MIN and RP_ON_MAX.

RP_INSIDE_TRUNCATED 

The constant has a value within the range, but was truncated.

Used for DECIMAL if the constant has more precision in the fraction than the field and for floating point underflow.

RP_INSIDE_YEAR_HOLE 

The YEAR type has a discontinuous range {0} U [1901, 2155].

If the constant is larger than 0, but less than 1901, we represent that with this value.

RP_ROUNDED_DOWN 

The constant has been rounded down, i.e. to the left on the number line, due to restriction on field length (FLOAT or DOUBLE) or because we have a FLOAT type and the constant given has a mantissa with more significant digits than allowed for FLOAT.

RP_ROUNDED_UP 

The constant has been rounded up, i.e. to the right on the number line, due to restriction on field length (FLOAT or DOUBLE) or because we have a FLOAT type and the constant given has a mantissa with more significant digits than allowed for FLOAT.

Function Documentation

◆ adjust_cmp_op()

static bool adjust_cmp_op ( THD thd,
Item_func func,
bool  left_has_field,
bool  inverse,
Item ref_or_field,
Item c,
Item **  retcond 
)
static

Possibly change comparison operator because we have rounded the constant in some direction.

Depending on the case, we change:

       <      to        <=
       >      to        >=
       <=     to        <
       >=     to        >
Parameters
[in]thdsession thread
[in]functhe comparison operator
[in]left_has_fieldtrue if <field> <cmp> <const>, false if inverse
[in]inverseinvert the logic
[in]ref_or_fieldthe field or a ref to it
[in]cthe constant
[in,out]retcondthe resulting condition
Returns
true on error

◆ analyze_decimal_field_constant()

static bool analyze_decimal_field_constant ( THD thd,
const Item_field f,
Item **  const_val,
Item_func::Functype  ft,
Range_placement place,
bool *  negative 
)
static

Minion of analyze_field_constant for decimal type fields.

Analyze a constant's placement within (or without) the type range of the field f. Also normalize the given constant to the type of the field if applicable.

Parameters
thdthe session context
fthe decimal typed field
[out]const_vala pointer to an item tree pointer containing the constant (at execution time). May be modified if we replace or fold the constant.
ftthe function type of the comparison
[out]placethe placement of the const_val relative to the range of f
[out]negativetrue if the constant is has a (minus) sign
Returns
true on error

◆ analyze_field_constant()

static bool analyze_field_constant ( THD thd,
Item_field f,
Item **  const_val,
Item_func func,
bool  left_has_field,
Range_placement place,
bool *  discount_equal,
bool *  negative 
)
static

Analyze a constant's placement within (or without) the type range of the field f.

Also normalize the given constant to the type of the field if applicable.

Parameters
thdthe session context
fthe field
[out]const_vala pointer to an item tree pointer containing the constant (at execution time). May be modified if we replace or fold the constant.
functhe function of the comparison
left_has_fieldthe field is the left operand
[out]placethe placement of the const_val relative to the range of f
[out]discount_equalset to true: caller should replace GE with GT or LE with LT.
[out]negativetrue if the constant (decimal) is has a (minus) sign
Returns
true on error

◆ analyze_int_field_constant()

static bool analyze_int_field_constant ( THD thd,
Item_field f,
Item **  const_val,
Item_func::Functype  ft,
bool  left_has_field,
Range_placement place,
bool *  discount_equal 
)
static

Minion of analyze_field_constant for integer type fields.

Analyze a constant's placement within (or without) the type range of the field f. Also normalize the given constant to the type of the field if applicable.

Parameters
thdthe session context
fthe integer typed field
[out]const_vala pointer to an item tree pointer containing the constant (at execution time). May be modified if we replace or fold the constant.
ftthe function type of the comparison
left_has_fieldthe field is the left operand
[out]placethe placement of the const_val relative to the range of f
[out]discount_equalset to true: caller should replace GE with GT or LE with LT.
Returns
true on error

◆ analyze_real_field_constant()

static bool analyze_real_field_constant ( THD thd,
Item_field f,
Item **  const_val,
Range_placement place 
)
static

Minion of analyze_field_constant for real type fields.

Analyze a constant's placement within (or without) the type range of the field f. Also normalize the given constant to the type of the field if applicable.

Parameters
thdthe session context
fthe real (FLOAT, DOUBLE) typed field
[out]const_vala pointer to an item tree pointer containing the constant (at execution time). May be modified if we replace or fold the constant.
[out]placethe placement of the const_val relative to
Returns
true on error

◆ analyze_time_field_constant()

static bool analyze_time_field_constant ( THD thd,
Item **  const_val 
)
static

◆ analyze_timestamp_field_constant()

static bool analyze_timestamp_field_constant ( THD thd,
const Item_field f,
Item **  const_val,
Range_placement place 
)
static

Minion of analyze_field_constant for TIMESTAMP, DATETIME, DATE fields.

Analyze a constant's placement within (or without) the type range of the field f. Also normalize the given constant to the type of the field if applicable.

Parameters
thdthe session context
fthe field
[out]const_vala pointer to an item tree pointer containing the constant (at execution time). May be modified if we replace or fold the constant.
[out]placethe placement of the const_val relative to
Returns
true on error

◆ analyze_year_field_constant()

static bool analyze_year_field_constant ( THD thd,
Item **  const_val,
Range_placement place 
)
static

Minion of analyze_field_constant for YEAR type fields.

Analyze a constant's placement within (or without) the type range of the field f. Also normalize the given constant to the type of the field if applicable.

Parameters
thdthe session context
[out]const_vala pointer to an item tree pointer containing the constant (at execution time). May be modified if we replace or fold the constant.
[out]placethe placement of the const_val relative to
Returns
true on error

◆ fold_arguments() [1/2]

static bool fold_arguments ( THD thd,
Item_cond cond 
)
static

Call fold_condition on all the conditions's arguments.

Parameters
thdthe session state
condthe condition
Returns
true on error

◆ fold_arguments() [2/2]

static bool fold_arguments ( THD thd,
Item_func func 
)
static

◆ fold_condition()

bool fold_condition ( THD thd,
Item cond,
Item **  retcond,
Item::cond_result cond_value,
bool  manifest_result = false 
)

Fold boolean condition {=, <>, >, >=, <, <=, <=>} involving constants and fields (or references to fields), possibly determining if the condition is always true or false based on type range of the field, or possibly simplifying >=, <= to = if constant lies on the range border.

Also fold such conditions if they are present as arguments of other functions in the same way, except there we replace the arguments with manifest FALSE or field <> field to exclude NULLs if the field is nullable, cf. ignore_unknown().

The constants are always converted to constants of the type of the field, no widening of the field type in therefore necessary after this folding for comparison to occur. When converting constants to decimal (when the field is a decimal), the constant will receive the same number of fractional decimals as the field. If decimal constant fraction truncation occurs, the comparison {GT,GE,LT,LE} logic is adjusted to preserve semantics without widening the field's type.

Parameters
thdsession state
condthe condition to handle
[out]retcondcondition after const removal
[out]cond_valueresulting value of the condition
  • COND_OK: condition must be evaluated (e.g int == 3)
  • COND_TRUE: always true (e.g signed tinyint < 128)
  • COND_FALSE: always false (e.g unsigned tinyint < 0)
manifest_resultFor IS NOT NULL on a not nullable item, if true, return item TRUE (1), else remove condition and return COND_TRUE. For cmp items, this is determined by Item_bool_func2::ignore_unknown.
Returns
false if success, true if error

◆ fold_or_convert_dec()

static bool fold_or_convert_dec ( THD thd,
Item **  const_val,
Range_placement place,
Item_field f,
my_decimal d 
)
static

Minion of analyze_int_field_constant.

Analyze decimal d. Convert decimal constant d to int if d has a zero fraction part and is within integer range. If it can't be converted to int, it is out of range, and we set place accordingly. If it has a non-zero fraction part, we fold. Used for = and <>.

Parameters
[in]thdthe session context
[in,out]const_valthe constant we are folding
[in,out]placethe range placement of the constant as analyzed
[in]fthe field the constant is being compared to
[in]dthe decimal value of the constant
Returns
true on error

◆ fold_or_simplify()

static bool fold_or_simplify ( THD thd,
Item ref_or_field,
Item_func::Functype  ft,
bool  always_true,
bool  manifest_result,
Item **  retcond,
Item::cond_result cond_value 
)
static

We have found that the constant's value makes the predicate always true or false (modulo field nullability), so simplify or remove the condition according to this table:

always_true == true

                 !top_level_item        top_level_item
              ------------------------------------------
   nullable   |  IF(NULL, NULL, 1)  |   IS NOT NULL    |
  !nullable   |  TRUE (1)           |   COND_TRUE      |
              ------------------------------------------

always_true == false

                 !top_level_item        top_level_item
              ------------------------------------------
   nullable   |  field <> field [*] |   COND_FALSE     |
  !nullable   |  FALSE (0)          |   COND_FALSE     |
              ------------------------------------------

[*] for the "<=>" operator, we fold to FALSE (0) in this case.
Parameters
thdcurrent session context
ref_or_fielda field (that is being being compared to a constant) or a ref to such a field.
ftfunction type
always_truetrue if the predicate is found to be always true
manifest_resulttrue if we should create an item to represent the truth value (and replace the condition with it) instead of returning nullptr in *cond_value and {Item::COND_TRUE, Item::COND_FALSE} in *cond_value
[out]retconda pointer to the condition altered by this method
[out]cond_valuethe resulting condition value
Returns
true on error

◆ map_less_to_greater()

static Range_placement map_less_to_greater ( Range_placement  place)
static

Switch places of low, high, max and min.

◆ round_fold_or_convert_dec()

static bool round_fold_or_convert_dec ( THD thd,
Item **  const_val,
Range_placement place,
Item_field f,
my_decimal d,
bool  up,
bool *  discount_equal 
)
static

Minion of analyze_int_field_constant.

Round up or down decimal d, then convert constant to int if possible. If d has a fraction part, set discount_equal. If it can't be converted to int, it is out of range, and we fold by setting place accordingly.

Parameters
[in]thdthe session context
[in,out]const_valthe constant we are folding
[in,out]placethe range placement of the constant as analyzed
[in]fthe field the constant is being compared to
[in]dthe decimal value of the constant
[in]uptrue if we are to round up, else we round down
[out]discount_equalset to true of d has a non-zero fraction part
Returns
true on error

◆ simplify_to_equal()

static bool simplify_to_equal ( THD thd,
Item ref_or_field,
Item c,
Item **  retcond,
Item::cond_result cond_value 
)
static

Replace condition in retcond with "=" (i.e. Item_func_eq) and set cond_value to Item::COND_OK.

Parameters
thdsession context
ref_or_fieldthe field being compared to a constant in the retcond
cthe constant being compared to a field in the retcond
[in,out]retcondthe comparison condition
[out]cond_valuethe resulting condition value
Returns
true on error