File: opt_rewrite_remove_casefold.cc

package info (click to toggle)
mariadb 1%3A11.8.3-1
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 772,520 kB
  • sloc: ansic: 2,414,714; cpp: 1,791,394; asm: 381,336; perl: 62,905; sh: 49,647; pascal: 40,897; java: 39,363; python: 20,791; yacc: 20,432; sql: 17,907; xml: 12,344; ruby: 8,544; cs: 6,542; makefile: 6,145; ada: 1,879; lex: 1,193; javascript: 996; objc: 80; tcl: 73; awk: 46; php: 22
file content (159 lines) | stat: -rw-r--r-- 4,286 bytes parent folder | download | duplicates (2)
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
149
150
151
152
153
154
155
156
157
158
159
/* Copyright (c) 2023, MariaDB plc

   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 St, Fifth Floor, Boston, MA 02110-1335  USA */

#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;
}