summaryrefslogtreecommitdiffstats
path: root/sql/opt_rewrite_remove_casefold.cc
blob: bf45e1f00fea49674eddb23e3a006488e2a6ebca (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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
#ifdef USE_PRAGMA_IMPLEMENTATION
#pragma implementation				// gcc: Class implementation
#endif

#include "mariadb.h"
#include "sql_priv.h"
#include <m_ctype.h>
#include "sql_partition.h"
#include "sql_select.h"

#include "opt_trace.h"

/*
  @brief
    Check if passed item is "UCASE(table.colX)" where colX is either covered
    by some index or is a part of partition expression.

  @return
     Argument of the UCASE if passed item matches
     NULL otherwise.
*/

static Item* is_upper_key_col(Item *item)
{
  Item_func_ucase *item_func;
  if ((item_func= dynamic_cast<Item_func_ucase*>(item)))
  {
    Item *arg= item_func->arguments()[0];
    Item *arg_real= arg->real_item();
    if (arg_real->type() == Item::FIELD_ITEM)
    {
      if (dynamic_cast<const Type_handler_longstr*>(arg_real->type_handler()))
      {
        Field *field= ((Item_field*)arg_real)->field;
        bool appl= (field->flags & PART_KEY_FLAG);

#ifdef WITH_PARTITION_STORAGE_ENGINE
        partition_info *part_info;
        if (!appl && ((part_info= field->table->part_info)))
        {
          appl= bitmap_is_set(&part_info->full_part_field_set,
                              field->field_index);
        }
#endif
        if (appl)
        {
          /*
            Make sure COERCIBILITY(UPPER(col))=COERCIBILITY(col)
          */
          DBUG_ASSERT(arg->collation.derivation ==
                      item_func->collation.derivation);

          /* Return arg, not arg_real. Do not walk into Item_ref objects */
          return arg;
        }
      }
    }
  }
  return nullptr;
}


static void trace_upper_removal_rewrite(THD *thd, Item *old_item, Item *new_item)
{
  Json_writer_object trace_wrapper(thd);
  Json_writer_object obj(thd, "sargable_casefold_removal");
  obj.add("before", old_item)
     .add("after", new_item);
}


/*
  @brief
    Rewrite UPPER(key_varchar_col) = expr into key_varchar_col=expr

  @detail
    UPPER() may occur on both sides of the equality.
    UCASE() is a synonym of UPPER() so we handle it, too.
*/

Item* Item_func_eq::varchar_upper_cmp_transformer(THD *thd, uchar *arg)
{
  if (cmp.compare_type() == STRING_RESULT &&
      cmp.compare_collation()->state & MY_CS_UPPER_EQUAL_AS_EQUAL)
  {
    Item *arg0= arguments()[0];
    Item *arg1= arguments()[1];
    bool do_rewrite= false;
    Item *tmp;

    // Try rewriting the left argument
    if ((tmp= is_upper_key_col(arguments()[0])))
    {
      arg0= tmp;
      do_rewrite= true;
    }

    // Try rewriting the right argument
    if ((tmp= is_upper_key_col(arguments()[1])))
    {
      arg1=tmp;
      do_rewrite= true;
    }

    if (do_rewrite)
    {
      Item *res= new (thd->mem_root) Item_func_eq(thd, arg0, arg1);
      if (res && !res->fix_fields(thd, &res))
      {
        trace_upper_removal_rewrite(thd, this, res);
        return res;
      }
    }
  }
  return this;
}


/*
  @brief
    Rewrite "UPPER(key_col) IN (const-list)" into "key_col IN (const-list)"
*/

Item* Item_func_in::varchar_upper_cmp_transformer(THD *thd, uchar *arg)
{
  if (arg_types_compatible &&
      m_comparator.cmp_type() == STRING_RESULT &&
      cmp_collation.collation->state & MY_CS_UPPER_EQUAL_AS_EQUAL &&
      all_items_are_consts(args + 1, arg_count - 1))
  {
    Item *arg0= arguments()[0];
    Item *tmp;
    if ((tmp= is_upper_key_col(arg0)))
    {
      Item_func_in *cl= (Item_func_in*)build_clone(thd);
      Item *res;
      cl->arguments()[0]= tmp;
      cl->walk(&Item::cleanup_excluding_const_fields_processor, 0, 0);
      res= cl;
      if (res->fix_fields(thd, &res))
        return this;
      trace_upper_removal_rewrite(thd, this, res);
      return res;
    }
  }
  return this;
}