File: opt_sargable_left.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 (158 lines) | stat: -rw-r--r-- 4,916 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
/* Copyright (c) 2024, 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 */

/**
  @file

  @brief
  This file contains SargableLeft optimization
*/

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

/*
  SargableLeft
  ============

  This optimization makes conditions in forms like

    LEFT(key_col, N) = 'string_const'
    SUBSTRING(key_col, 1, N) = 'string_const'

  sargable. The conditions take the first N characters of key_col and
  compare them with a string constant.
  However, producing index lookup intervals for this collation is complex
  due to contractions.

  Contractions
  ------------
  A contraction is a property of collation where a sequence of multiple
  characters is compared as some other character(s).
  For example, in utfmb4_danish_ci, 'AA' is compared as one character 'Å'
  which sorts after 'Z':

  MariaDB [test]> select a from t1 order by col1;
  +------+
  | col1 |
  +------+
  | BA1  | (1)
  | BC   |
  | BZ   |
  | BAA2 | (2)
  +------+

  Now suppose we're producing lookup ranges for condition

  LEFT(col1, 2)='BA'

  In addition to looking near 'BA' (1), we need to look into the area right
  after 'BZ' (2), where we may find 'BAA'.

  Fortunately, this was already implemented for handling LIKE conditions in
  form 'key_col LIKE 'BA%'. Each collation provides like_range() call which
  produces lookup range in a collation-aware way.

  Differences between LIKE and LEFT=
  ----------------------------------
  So can one reduce or even rewrite conditions with LEFT() into LIKE? No, there
  are differences.

  First, LIKE does character-by-character comparison, ignoring the collation's
  contractions:

  MariaDB [test]> select col1, col1='AA', col1 LIKE 'AA' from t1;
  +------+-----------+----------------+
  | col1 | col1='AA' | col1 LIKE 'AA' |
  +------+-----------+----------------+
  | AA   |         1 |              1 |
  | Å    |         1 |              0 |
  +------+-----------+----------------+

  (However, index comparison function uses equality's comparison rules.
  my_like_range() will produce an index range 'AA' <= col1 <= 'AA'. Reading rows
  from it will return 'Å' as well)

  Second, LEFT imposes additional constraints on the length of both parts. For
  example:
  - LEFT(col,2)='string-longer-than-two-chars' - is false for any value of col.
  - LEFT(col,2)='A' is not equivalent to (col LIKE 'A%'), consider col='Ab'.

  Take-aways
  ----------
  - SargableLeft makes use of my_like_range() to produce index intervals.
  - LEFT(col, N)='foo'
  - We ignore the value of N when producing the lookup range (this may make the
    range to include rows for which the predicate is false)
    = For the SUBSTRING form, we only need to check that M=1 in the
      SUBSTRING(col, M, N)='foo'.
*/


/*
  @brief Check if this condition is sargable LEFT(key_col, N)='foo', or
         similar condition with SUBSTRING().

  @detail
    'foo' here can be any constant we can compute during optimization,
    Only equality conditions are supported.
    See SargableLeft above for detals.

  @param  field      The first argument of LEFT or SUBSTRING if sargable,
                     otherwise deferenced to NULL
  @param  value_idx  The index of argument that is the prefix string
                     if sargable, otherwise dereferenced to -1
*/

bool Item_bool_func::with_sargable_substr(Item_field **field, int *value_idx) const
{
  int func_idx, val_idx= -1;
  Item **func_args, *real= NULL;
  bool ret= false;
  enum Functype type;
  if (functype() != EQ_FUNC)
    goto done;
  if (args[0]->type() == FUNC_ITEM)
    func_idx= 0;
  else if (args[1]->type() == FUNC_ITEM)
    func_idx= 1;
  else
    goto done;
  type= ((Item_func *) args[func_idx])->functype();
  if (type != SUBSTR_FUNC && type != LEFT_FUNC)
    goto done;
  func_args= ((Item_func *) args[func_idx])->arguments();
  real= func_args[0]->real_item();
  val_idx= 1 - func_idx;
  if (real->type() == FIELD_ITEM &&
      args[val_idx]->can_eval_in_optimize() &&
      (type == LEFT_FUNC || func_args[1]->val_int() == 1))
  {
    ret= true;
    goto done;
  }
  real= NULL;
  val_idx= -1;
done:
  if (field != NULL)
    *field= (Item_field *) real;
  if (value_idx != NULL)
    *value_idx= val_idx;
  return ret;
}