MySQL 9.0.0
Source Code Documentation
sql_const_folding.cc File Reference

Fold constant query expressions. More...

#include "sql/sql_const_folding.h"
#include <assert.h>
#include <float.h>
#include <stdint.h>
#include <sys/types.h>
#include <utility>
#include "decimal.h"
#include "field_types.h"
#include "my_inttypes.h"
#include "my_time.h"
#include "mysql/strings/dtoa.h"
#include "mysql/udf_registration_types.h"
#include "mysql_time.h"
#include "sql-common/my_decimal.h"
#include "sql/field.h"
#include "sql/item.h"
#include "sql/item_cmpfunc.h"
#include "sql/item_func.h"
#include "sql/item_timefunc.h"
#include "sql/sql_class.h"
#include "sql/sql_list.h"
#include "sql/sql_time.h"
#include "sql/tztime.h"
#include "sql_string.h"
#include "template_utils.h"

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

Fold constant query expressions.

Currently, this is only done at optimize time, but should eventually be done as much as possible at prepare time. For dynamic parameters, we'd still need to do it at optimize time.