summaryrefslogtreecommitdiff
path: root/sql/opt_rewrite_date_cmp.h
blob: 9f6e1d74ffabb4fa8262f482f4d9a5856745309d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
/*
   Copyright (c) 2023, MariaDB

   This program is free software; you can redistribute it and/or modify
   it under the terms of the GNU General Public License as published by
   the Free Software Foundation; version 2 of the License.

   This program is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   GNU General Public License for more details.

   You should have received a copy of the GNU General Public License
   along with this program; if not, write to the Free Software
   Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1335  USA */

#ifndef OPT_REWRITE_DATE_CMP_INCLUDED
#define OPT_REWRITE_DATE_CMP_INCLUDED

class Item_func_eq;
class Item_func_ge;
class Item_func_gt;
class Item_func_le;
class Item_func_lt;
class Item_bool_rowready_func2;

/*
  @brief Class responsible for rewriting datetime comparison condition.
         It rewrites non-sargable conditions into sargable.

  @detail
  The intent of this class is to do equivalent rewrites as follows:

    YEAR(col) <= val  ->  col <= year_end(val)
    YEAR(col) <  val  ->  col <  year_start(val)
    YEAR(col) >= val  ->  col >= year_start(val)
    YEAR(col) >  val  ->  col >  year_end(val)
    YEAR(col) =  val  ->  col >= year_start(val) AND col<=year_end(val)

  Also the same is done for comparisons with DATE(col):

    DATE(col) <= val  ->  col <= day_end(val)

  if col has a DATE type (not DATETIME), then the rewrite becomes:

    DATE(col) <= val  ->  col <= val

  @usage
    Date_cmp_func_rewriter rwr(thd, item_func);
    Item *new_item= rwr.get_rewrite_result();

    Returned new_item points to an item that item_func was rewritten to.
    new_item already has fixed fields (fix_fields() was called).
    If no rewrite happened, new_item points to the initial item_func parameter

  @todo
    Also handle conditions in form "YEAR(date_col) BETWEEN 2014 AND 2017"
    and "YEAR(col) = c1 AND MONTH(col) = c2"
*/
class Date_cmp_func_rewriter
{
public:
  Date_cmp_func_rewriter(THD* thd, Item_func_eq *item_func);

  Date_cmp_func_rewriter(THD* thd, Item_func_ge *item_func);

  Date_cmp_func_rewriter(THD* thd, Item_func_gt *item_func);

  Date_cmp_func_rewriter(THD* thd, Item_func_le *item_func);

  Date_cmp_func_rewriter(THD* thd, Item_func_lt *item_func);

  Item* get_rewrite_result() const { return result; }

  Date_cmp_func_rewriter() = delete;
  Date_cmp_func_rewriter(const Date_cmp_func_rewriter&) = delete;
  Date_cmp_func_rewriter(Date_cmp_func_rewriter&&) = delete;

private:
  bool check_cond_match_and_prepare(Item_bool_rowready_func2 *item_func);
  Item_field *is_date_rounded_field(Item* item,
                                    const Type_handler *comparison_type,
                                    Item_func::Functype *out_func_type) const;
  void rewrite_le_gt_lt_ge();
  Item *create_start_bound();
  Item *create_end_bound();
  Item *create_cmp_func(Item_func::Functype func_type, Item *arg1, Item *arg2);

  THD *thd= nullptr;
  Item *const_arg_value= nullptr;
  Item_func::Functype rewrite_func_type= Item_func::UNKNOWN_FUNC;
  Item_func::Functype argument_func_type= Item_func::UNKNOWN_FUNC;
  Item_field *field_ref= nullptr;
  Item *result= nullptr;
};


void trace_date_item_rewrite(THD *thd,Item *new_item, Item *old_item);

template<typename T>
Item* do_date_conds_transformation(THD *thd, T *item)
{
  Date_cmp_func_rewriter rwr(thd, item);
  /* If the rewrite failed for some reason, we get the original item */
  Item *new_item= rwr.get_rewrite_result();
  trace_date_item_rewrite(thd, new_item, item);
  return new_item;
}


#endif