summaryrefslogtreecommitdiffstats
path: root/mysql-test/main/cset_narrowing.result
blob: e048e85b594f106f14bf4543698a60beeb051e9c (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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
set
@tmp_csetn_os= @@optimizer_switch,
optimizer_switch='cset_narrowing=on';
set names utf8mb4;
create table t1 (
mb3name varchar(32),
mb3 varchar(32) collate utf8mb3_general_ci,
key(mb3)
);
insert into t1 select seq, seq from seq_1_to_10000;
insert into t1 values ('mb3-question-mark', '?');
insert into t1 values ('mb3-replacement-char', _utf8mb3 0xEFBFBD);
create table t10 (
pk int auto_increment primary key,
mb4name varchar(32),
mb4 varchar(32) character set utf8mb4 collate utf8mb4_general_ci
);
insert into t10 (mb4name, mb4) values
('mb4-question-mark','?'),
('mb4-replacement-char', _utf8mb4 0xEFBFBD),
('mb4-smiley', _utf8mb4 0xF09F988A),
('1', '1');
analyze table t1,t10 persistent for all;
Table	Op	Msg_type	Msg_text
test.t1	analyze	status	Engine-independent statistics collected
test.t1	analyze	status	OK
test.t10	analyze	status	Engine-independent statistics collected
test.t10	analyze	status	OK
#
# Check that constants are already handled: the following should use
#  ref/range, because constants are converted into utf8mb3.
#
select collation('abc');
collation('abc')
utf8mb4_general_ci
explain select * from t1 force index (mb3) where t1.mb3='abc';
id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
1	SIMPLE	t1	ref	mb3	mb3	99	const	1	Using index condition
explain select * from t1 force index (mb3) where t1.mb3 in ('abc','cde','xyz');
id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
1	SIMPLE	t1	range	mb3	mb3	99	NULL	3	Using index condition
explain select * from t1 force index (mb3) where t1.mb3 between 'abc' and 'acc';
id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
1	SIMPLE	t1	range	mb3	mb3	99	NULL	1	Using index condition
explain select * from t1 force index (mb3) where t1.mb3 <'000';
id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
1	SIMPLE	t1	range	mb3	mb3	99	NULL	1	Using index condition
# If a constant can't be represented in utf8mb3, an error is produced:
explain select * from t1 force index (mb3) where t1.mb3='😊';
ERROR HY000: Illegal mix of collations (utf8mb3_general_ci,IMPLICIT) and (utf8mb4_general_ci,COERCIBLE) for operation '='
#
# Check ref access on mb3_field=mb4_field
#
explain format=json
select * from t10,t1 where t10.mb4=t1.mb3;
EXPLAIN
{
  "query_block": {
    "select_id": 1,
    "nested_loop": [
      {
        "table": {
          "table_name": "t10",
          "access_type": "ALL",
          "rows": 4,
          "filtered": 100,
          "attached_condition": "t10.mb4 is not null"
        }
      },
      {
        "table": {
          "table_name": "t1",
          "access_type": "ref",
          "possible_keys": ["mb3"],
          "key": "mb3",
          "key_length": "99",
          "used_key_parts": ["mb3"],
          "ref": ["test.t10.mb4"],
          "rows": 1,
          "filtered": 100,
          "index_condition": "t1.mb3 = t10.mb4"
        }
      }
    ]
  }
}
select * from t10,t1 where t10.mb4=t1.mb3;
pk	mb4name	mb4	mb3name	mb3
1	mb4-question-mark	?	mb3-question-mark	?
2	mb4-replacement-char	�	mb3-replacement-char	�
3	mb4-smiley	😊	mb3-replacement-char	�
4	1	1	1	1
select * from t10,t1 use index() where t10.mb4=t1.mb3;
pk	mb4name	mb4	mb3name	mb3
4	1	1	1	1
1	mb4-question-mark	?	mb3-question-mark	?
2	mb4-replacement-char	�	mb3-replacement-char	�
3	mb4-smiley	😊	mb3-replacement-char	�
explain format=json
select * from t10,t1 where t10.mb4<=>t1.mb3;
EXPLAIN
{
  "query_block": {
    "select_id": 1,
    "nested_loop": [
      {
        "table": {
          "table_name": "t10",
          "access_type": "ALL",
          "rows": 4,
          "filtered": 100
        }
      },
      {
        "table": {
          "table_name": "t1",
          "access_type": "ref",
          "possible_keys": ["mb3"],
          "key": "mb3",
          "key_length": "99",
          "used_key_parts": ["mb3"],
          "ref": ["test.t10.mb4"],
          "rows": 1,
          "filtered": 100,
          "index_condition": "t10.mb4 <=> t1.mb3"
        }
      }
    ]
  }
}
select * from t10,t1 where t10.mb4<=>t1.mb3;
pk	mb4name	mb4	mb3name	mb3
1	mb4-question-mark	?	mb3-question-mark	?
2	mb4-replacement-char	�	mb3-replacement-char	�
3	mb4-smiley	😊	mb3-replacement-char	�
4	1	1	1	1
set statement optimizer_switch='cset_narrowing=off', join_cache_level=0 for
explain format=json
select * from t10,t1 where t10.mb4=t1.mb3;
EXPLAIN
{
  "query_block": {
    "select_id": 1,
    "nested_loop": [
      {
        "table": {
          "table_name": "t10",
          "access_type": "ALL",
          "rows": 4,
          "filtered": 100
        }
      },
      {
        "table": {
          "table_name": "t1",
          "access_type": "ALL",
          "rows": 10002,
          "filtered": 100,
          "attached_condition": "t10.mb4 = convert(t1.mb3 using utf8mb4)"
        }
      }
    ]
  }
}
#
# Check ref access on mb3_field=mb4_expr
#
explain format=json
select * from t10,t1 where t1.mb3=concat('',t10.mb4);
EXPLAIN
{
  "query_block": {
    "select_id": 1,
    "nested_loop": [
      {
        "table": {
          "table_name": "t10",
          "access_type": "ALL",
          "rows": 4,
          "filtered": 100
        }
      },
      {
        "table": {
          "table_name": "t1",
          "access_type": "ref",
          "possible_keys": ["mb3"],
          "key": "mb3",
          "key_length": "99",
          "used_key_parts": ["mb3"],
          "ref": ["func"],
          "rows": 1,
          "filtered": 100,
          "index_condition": "t1.mb3 = concat('',t10.mb4)"
        }
      }
    ]
  }
}
select * from t10,t1 where t1.mb3=concat('',t10.mb4);
pk	mb4name	mb4	mb3name	mb3
1	mb4-question-mark	?	mb3-question-mark	?
2	mb4-replacement-char	�	mb3-replacement-char	�
3	mb4-smiley	😊	mb3-replacement-char	�
4	1	1	1	1
select * from t10,t1 use index() where t1.mb3=concat('',t10.mb4);
pk	mb4name	mb4	mb3name	mb3
4	1	1	1	1
1	mb4-question-mark	?	mb3-question-mark	?
2	mb4-replacement-char	�	mb3-replacement-char	�
3	mb4-smiley	😊	mb3-replacement-char	�
# Check that ref optimizer gets the right constant.
#  We need a const table for that, because key=const is handled by
#  coercing the constant.
#
# So, we take the smiley:
select * from t10 where t10.pk=3;
pk	mb4name	mb4
3	mb4-smiley	😊
set optimizer_trace=1;
# And see that we've got the Replacement Character in the ranges:
explain
select * from t10, t1 where t10.mb4=t1.mb3 and t10.pk=3;
id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
1	SIMPLE	t10	const	PRIMARY	PRIMARY	4	const	1	
1	SIMPLE	t1	ref	mb3	mb3	99	const	3	Using index condition
select
json_detailed(json_extract(trace, '$**.range_scan_alternatives')) as JS
from
information_schema.optimizer_trace;
JS
[
    [
        {
            "index": "mb3",
            "ranges": 
            ["(�) <= (mb3) <= (�)"],
            "rowid_ordered": true,
            "using_mrr": false,
            "index_only": false,
            "rows": 3,
            "cost": 3.760377105,
            "chosen": true
        }
    ]
]
select * from t10, t1 where t10.mb4=t1.mb3 and t10.pk=3;
pk	mb4name	mb4	mb3name	mb3
3	mb4-smiley	😊	mb3-replacement-char	�
#
# Will range optimizer handle t1.mb3>t10.mb4? No...
#
explain format=json
select * from t10, t1 where (t1.mb3=t10.mb4 or t1.mb3='hello') and t10.pk=3;
EXPLAIN
{
  "query_block": {
    "select_id": 1,
    "nested_loop": [
      {
        "table": {
          "table_name": "t10",
          "access_type": "const",
          "possible_keys": ["PRIMARY"],
          "key": "PRIMARY",
          "key_length": "4",
          "used_key_parts": ["pk"],
          "ref": ["const"],
          "rows": 1,
          "filtered": 100
        }
      },
      {
        "table": {
          "table_name": "t1",
          "access_type": "range",
          "possible_keys": ["mb3"],
          "key": "mb3",
          "key_length": "99",
          "used_key_parts": ["mb3"],
          "rows": 4,
          "filtered": 100,
          "index_condition": "t1.mb3 = '????' or t1.mb3 = 'hello'"
        }
      }
    ]
  }
}
explain format=json
select * from t10, t1 where t1.mb3>t10.mb4 and t10.pk=3;
EXPLAIN
{
  "query_block": {
    "select_id": 1,
    "nested_loop": [
      {
        "table": {
          "table_name": "t10",
          "access_type": "const",
          "possible_keys": ["PRIMARY"],
          "key": "PRIMARY",
          "key_length": "4",
          "used_key_parts": ["pk"],
          "ref": ["const"],
          "rows": 1,
          "filtered": 100
        }
      },
      {
        "table": {
          "table_name": "t1",
          "access_type": "ALL",
          "rows": 10002,
          "filtered": 100,
          "attached_condition": "convert(t1.mb3 using utf8mb4) > '????'"
        }
      }
    ]
  }
}
# For comparison, it will handle it when collations match:
create table t2 (
mb4name varchar(32),
mb4 varchar(32) collate utf8mb4_general_ci,
key(mb4)
);
insert into t2 select * from t1;
explain format=json
select * from t10, t2 where t2.mb4>t10.mb4 and t10.pk=3;
EXPLAIN
{
  "query_block": {
    "select_id": 1,
    "nested_loop": [
      {
        "table": {
          "table_name": "t10",
          "access_type": "const",
          "possible_keys": ["PRIMARY"],
          "key": "PRIMARY",
          "key_length": "4",
          "used_key_parts": ["pk"],
          "ref": ["const"],
          "rows": 1,
          "filtered": 100
        }
      },
      {
        "table": {
          "table_name": "t2",
          "access_type": "range",
          "possible_keys": ["mb4"],
          "key": "mb4",
          "key_length": "131",
          "used_key_parts": ["mb4"],
          "rows": 3,
          "filtered": 100,
          "index_condition": "t2.mb4 > '????'"
        }
      }
    ]
  }
}
#
# Check multiple equalities
#
# - ref acccess lookup keys do use equality substitution,
# - concat() arguments don't
explain format=json
select straight_join * from t10,t1 force index(mb3),t2
where
t1.mb3=t2.mb4 and t2.mb4=t10.mb4 and concat(t1.mb3, t2.mb4, t10.mb4)<>'Bebebe';
EXPLAIN
{
  "query_block": {
    "select_id": 1,
    "nested_loop": [
      {
        "table": {
          "table_name": "t10",
          "access_type": "ALL",
          "rows": 4,
          "filtered": 100,
          "attached_condition": "t10.mb4 is not null and t10.mb4 is not null"
        }
      },
      {
        "table": {
          "table_name": "t1",
          "access_type": "ref",
          "possible_keys": ["mb3"],
          "key": "mb3",
          "key_length": "99",
          "used_key_parts": ["mb3"],
          "ref": ["test.t10.mb4"],
          "rows": 1,
          "filtered": 100,
          "index_condition": "t1.mb3 = t10.mb4"
        }
      },
      {
        "table": {
          "table_name": "t2",
          "access_type": "ref",
          "possible_keys": ["mb4"],
          "key": "mb4",
          "key_length": "131",
          "used_key_parts": ["mb4"],
          "ref": ["test.t10.mb4"],
          "rows": 1,
          "filtered": 100,
          "index_condition": "concat(convert(t1.mb3 using utf8mb4),t2.mb4,t10.mb4) <> 'Bebebe'"
        }
      }
    ]
  }
}
select json_detailed(json_extract(trace, '$**.condition_processing')) as JS
from information_schema.optimizer_trace;
JS
[
    {
        "condition": "WHERE",
        "original_condition": "t1.mb3 = t2.mb4 and t2.mb4 = t10.mb4 and concat(convert(t1.mb3 using utf8mb4),t2.mb4,t10.mb4) <> 'Bebebe'",
        "steps": 
        [
            {
                "transformation": "equality_propagation",
                "resulting_condition": "concat(convert(t1.mb3 using utf8mb4),t2.mb4,t10.mb4) <> 'Bebebe' and multiple equal(t1.mb3, t2.mb4, t10.mb4)"
            },
            {
                "transformation": "constant_propagation",
                "resulting_condition": "concat(convert(t1.mb3 using utf8mb4),t2.mb4,t10.mb4) <> 'Bebebe' and multiple equal(t1.mb3, t2.mb4, t10.mb4)"
            },
            {
                "transformation": "trivial_condition_removal",
                "resulting_condition": "concat(convert(t1.mb3 using utf8mb4),t2.mb4,t10.mb4) <> 'Bebebe' and multiple equal(t1.mb3, t2.mb4, t10.mb4)"
            }
        ]
    }
]
select straight_join * from t10,t1 force index(mb3),t2
where
t1.mb3=t2.mb4 and t2.mb4=t10.mb4 and concat(t1.mb3, t2.mb4, t10.mb4)<>'Bebebe';
pk	mb4name	mb4	mb3name	mb3	mb4name	mb4
1	mb4-question-mark	?	mb3-question-mark	?	mb3-question-mark	?
2	mb4-replacement-char	�	mb3-replacement-char	�	mb3-replacement-char	�
3	mb4-smiley	😊	mb3-replacement-char	�	mb3-replacement-char	�
4	1	1	1	1	1	1
# Equality substitution doesn't happen for constants, for both narrowing
#  and non-narrowing comparisons:
explain format=json
select * from t10,t1,t2
where
t1.mb3=t2.mb4 and t2.mb4=t10.mb4 and t10.mb4='hello' and
concat(t1.mb3, t2.mb4, t10.mb4)<>'Bebebe';
EXPLAIN
{
  "query_block": {
    "select_id": 1,
    "nested_loop": [
      {
        "table": {
          "table_name": "t10",
          "access_type": "ALL",
          "rows": 4,
          "filtered": 25,
          "attached_condition": "t10.mb4 = 'hello'"
        }
      },
      {
        "table": {
          "table_name": "t1",
          "access_type": "ref",
          "possible_keys": ["mb3"],
          "key": "mb3",
          "key_length": "99",
          "used_key_parts": ["mb3"],
          "ref": ["const"],
          "rows": 1,
          "filtered": 100,
          "index_condition": "t1.mb3 = t10.mb4"
        }
      },
      {
        "table": {
          "table_name": "t2",
          "access_type": "ref",
          "possible_keys": ["mb4"],
          "key": "mb4",
          "key_length": "131",
          "used_key_parts": ["mb4"],
          "ref": ["const"],
          "rows": 1,
          "filtered": 100,
          "index_condition": "t2.mb4 = t10.mb4 and concat(convert(t1.mb3 using utf8mb4),t2.mb4,t10.mb4) <> 'Bebebe'"
        }
      }
    ]
  }
}
select json_detailed(json_extract(trace, '$**.condition_processing')) as JS
from information_schema.optimizer_trace;
JS
[
    {
        "condition": "WHERE",
        "original_condition": "t1.mb3 = t2.mb4 and t2.mb4 = t10.mb4 and t10.mb4 = 'hello' and concat(convert(t1.mb3 using utf8mb4),t2.mb4,t10.mb4) <> 'Bebebe'",
        "steps": 
        [
            {
                "transformation": "equality_propagation",
                "resulting_condition": "t10.mb4 = 'hello' and concat(convert(t1.mb3 using utf8mb4),t2.mb4,t10.mb4) <> 'Bebebe' and multiple equal(t1.mb3, t2.mb4, t10.mb4)"
            },
            {
                "transformation": "constant_propagation",
                "resulting_condition": "t10.mb4 = 'hello' and concat(convert(t1.mb3 using utf8mb4),t2.mb4,t10.mb4) <> 'Bebebe' and multiple equal(t1.mb3, t2.mb4, t10.mb4)"
            },
            {
                "transformation": "trivial_condition_removal",
                "resulting_condition": "t10.mb4 = 'hello' and concat(convert(t1.mb3 using utf8mb4),t2.mb4,t10.mb4) <> 'Bebebe' and multiple equal(t1.mb3, t2.mb4, t10.mb4)"
            }
        ]
    }
]
drop table t2;
drop table t1, t10;
set optimizer_switch=@tmp_csetn_os;