File: distinct_innodb.result

package info (click to toggle)
mysql-8.0 8.0.43-3
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 1,273,924 kB
  • sloc: cpp: 4,684,605; ansic: 412,450; pascal: 108,398; java: 83,641; perl: 30,221; cs: 27,067; sql: 26,594; sh: 24,181; python: 21,816; yacc: 17,169; php: 11,522; xml: 7,388; javascript: 7,076; makefile: 2,194; lex: 1,075; awk: 670; asm: 520; objc: 183; ruby: 97; lisp: 86
file content (961 lines) | stat: -rw-r--r-- 44,096 bytes parent folder | download
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
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
#
# Bug#13335170 - ASSERT IN
# PLAN_CHANGE_WATCHDOG::~PLAN_CHANGE_WATCHDOG() ON SELECT DISTINCT 
#
CREATE TABLE t1 (
col_int_key int(11) NOT NULL,
col_time_key time NOT NULL,
col_datetime_key datetime NOT NULL,
KEY col_int_key (col_int_key),
KEY col_time_key (col_time_key),
KEY col_datetime_key (col_datetime_key)
) ENGINE=InnoDB;
Warnings:
Warning	1681	Integer display width is deprecated and will be removed in a future release.
INSERT INTO t1 VALUES (7,'06:17:39','2003-08-21 00:00:00');
SELECT DISTINCT col_int_key
FROM t1
WHERE col_int_key IN  ( 18, 6, 84, 4, 0, 2, 8, 3, 7, 9, 1 )
AND col_datetime_key BETWEEN '2001-08-04' AND '2003-06-13'
ORDER BY col_time_key
LIMIT 3;
col_int_key
DROP TABLE t1;

# BUG#13581713 ONLY_FULL_GROUP_BY DOES NOT BLOCK "SELECT
# DISTINCT A ORDER BY B"

create table t1(a int, b int, c int) engine=InnoDB;
create table t2(a int, b int, c int) engine=InnoDB;
insert into t2 values();
analyze table t2;
Table	Op	Msg_type	Msg_text
test.t2	analyze	status	OK
# Test when selecting from base table
insert into t1 values(100,1,2),(200,1,1),(300,2,1),(400,2,2);
analyze table t1;
Table	Op	Msg_type	Msg_text
test.t1	analyze	status	OK
select distinct b from t1 order by c;
b
2
1
select distinct min(b) from t1 group by a order by min(c);
min(b)
1
2
explain select distinct min(b) from t1 group by a order by min(c);
id	select_type	table	partitions	type	possible_keys	key	key_len	ref	rows	filtered	Extra
1	SIMPLE	t1	NULL	ALL	NULL	NULL	NULL	#	4	100.00	Using temporary; Using filesort
Note	1003	/* select#1 */ select distinct min(`test`.`t1`.`b`) AS `min(b)` from `test`.`t1` group by `test`.`t1`.`a` order by min(`test`.`t1`.`c`)
Warnings:
Insert rows in different order:
delete from t1;
insert into t1 values(200,1,1),(100,1,2),(400,2,2),(300,2,1);
analyze table t1;
Table	Op	Msg_type	Msg_text
test.t1	analyze	status	OK
And get a different order. Query is executed like this:
- First, DISTINCT, using a tmp MEMORY table with a unique
index, thus if two rows have the same 'b' but a different 'c',
the second row is rejected, so the first value of 'c' wins
(=> randomness of 'c')
- Second, ORDER BY on the random 'c'.
select distinct b from t1 order by c;
b
1
2
Random order too (same reason):
select distinct min(b) from t1 group by a order by min(c);
min(b)
1
2
This query gives random order:
select distinct b from t1 order by c;
ERROR HY000: Expression #1 of ORDER BY clause is not in SELECT list, references column 'test.t1.c' which is not in SELECT list; this is incompatible with DISTINCT
and this one too:
select distinct b from t1 order by b-1,b+1,c;
ERROR HY000: Expression #3 of ORDER BY clause is not in SELECT list, references column 'test.t1.c' which is not in SELECT list; this is incompatible with DISTINCT
and this one too:
select distinct min(b) from t1 group by a order by min(c);
ERROR HY000: Expression #1 of ORDER BY clause is not in SELECT list, contains aggregate function; this is incompatible with DISTINCT
Not random (though Standard bans aggregates from ORDER BY):
select distinct min(b) from t1 group by a order by min(b);
min(b)
1
2
select distinct min(b) from t1 group by a order by -min(b);
ERROR HY000: Expression #1 of ORDER BY clause is not in SELECT list, contains aggregate function; this is incompatible with DISTINCT
All group exprs are in select list => DISTINCT is removed => no error
select distinct a, min(b) from t1 group by a order by max(b-2)-min(c*5);
a	min(b)
100	1
400	2
200	1
300	2
This one is standard:
select distinct min(b) as z from t1 group by a order by z;
z
1
2
Other queries:
select distinct b from t1 where b<0 order by rand();
b
select distinct b from t1 order by 45.0+3;
b
1
2
select (select distinct b from t1 as S2 where b=7 order by S3.a) from t1 as S3;
(select distinct b from t1 as S2 where b=7 order by S3.a)
NULL
NULL
NULL
NULL
select distinct b from t1 order by abs(b);
b
1
2
select distinct b as z from t1 order by abs(z);
z
1
2
select distinct b from t1 order by abs(b+a);
ERROR HY000: Expression #1 of ORDER BY clause is not in SELECT list, references column 'test.t1.a' which is not in SELECT list; this is incompatible with DISTINCT
select distinct abs(b) as z from t1 order by z;
z
1
2
select distinct abs(b) as z from t1 order by abs(b);
z
1
2
select distinct abs(b) from t1 order by abs(b);
abs(b)
1
2
Not ok: ABS(b)+1 is neither a SELECTed expression nor an alias
to one, and mentions a column of FROM tables.
select distinct abs(b) as z from t1 order by abs(b)+1;
ERROR HY000: Expression #1 of ORDER BY clause is not in SELECT list, references column 'test.t1.b' which is not in SELECT list; this is incompatible with DISTINCT
select distinct abs(b) as z from t1 order by z+1;
z
1
2
select distinct abs(b) from t1 order by abs(b)+1;
ERROR HY000: Expression #1 of ORDER BY clause is not in SELECT list, references column 'test.t1.b' which is not in SELECT list; this is incompatible with DISTINCT
select distinct abs(b) as z from t1 order by floor(10*b);
ERROR HY000: Expression #1 of ORDER BY clause is not in SELECT list, references column 'test.t1.b' which is not in SELECT list; this is incompatible with DISTINCT
select distinct abs(b) from t1 order by floor(10*b);
ERROR HY000: Expression #1 of ORDER BY clause is not in SELECT list, references column 'test.t1.b' which is not in SELECT list; this is incompatible with DISTINCT
Two offending columns; error message needs to report only one
select distinct abs(b) from t1 order by floor(10*b),floor(10*a);
ERROR HY000: Expression #1 of ORDER BY clause is not in SELECT list, references column 'test.t1.b' which is not in SELECT list; this is incompatible with DISTINCT
select distinct abs(b) from t1 as S2 order by
(select floor(10*S2.b) from t1 as S3 limit 1);
ERROR HY000: Expression #1 of ORDER BY clause is not in SELECT list, references column 'test.S2.b' which is not in SELECT list; this is incompatible with DISTINCT
Ok as S2.b in SELECT list
select distinct abs(b),b from t1 as S2 order by
(select floor(10*S2.b) from t1 as S3 limit 1);
abs(b)	b
1	1
2	2
Ok as subq does not use columns of FROM clause of ordered Q.
select distinct abs(b) from t1 as S2 order by
(select floor(10*S3.b) from t1 as S3 limit 1);
abs(b)
1
2
Subq as alias => ok
select distinct abs(b),
(select floor(10*S3.b) from t1 as S3 limit 1) as subq
from t1 as S2 order by subq;
abs(b)	subq
1	10
2	10
Bad field in left or right argument of ALL/ANY(subq):
select distinct abs(b) from t1 as S2 order by
floor(10*S2.b) IN (select floor(10*S3.b) from t1 as S3);
ERROR HY000: Expression #1 of ORDER BY clause is not in SELECT list, references column 'test.S2.b' which is not in SELECT list; this is incompatible with DISTINCT
select distinct abs(b) from t1 as S2 order by
floor(10*S2.b) > ALL(select floor(10*S3.b) from t1 as S3);
ERROR HY000: Expression #1 of ORDER BY clause is not in SELECT list, references column 'test.S2.b' which is not in SELECT list; this is incompatible with DISTINCT
select distinct abs(b) from t1 as S2 order by
floor(10*10) IN (select floor(10*S2.b) from t1 as S3);
ERROR HY000: Expression #1 of ORDER BY clause is not in SELECT list, references column 'test.S2.b' which is not in SELECT list; this is incompatible with DISTINCT
select distinct abs(b) from t1 as S2 order by
floor(10*10) > ALL(select floor(10*S2.b) from t1 as S3);
ERROR HY000: Expression #1 of ORDER BY clause is not in SELECT list, references column 'test.S2.b' which is not in SELECT list; this is incompatible with DISTINCT
Aggregates:
SELECT distinct 1 FROM t1 group by a order by count(*);
ERROR HY000: Expression #1 of ORDER BY clause is not in SELECT list, contains aggregate function; this is incompatible with DISTINCT
SELECT distinct 1 FROM t1 group by a order by count(*)-count(*);
ERROR HY000: Expression #1 of ORDER BY clause is not in SELECT list, contains aggregate function; this is incompatible with DISTINCT
Test ANY_VALUE
SELECT distinct 1 FROM t1 group by a order by any_value(count(*)-count(b));
1
1
SELECT distinct 1 FROM t1 group by a order by any_value(count(*))-any_value(count(b));
1
1
All group exprs are in select list => DISTINCT is removed => no error
SELECT distinct a, min(b) FROM t1 group by a order by count(*)-count(*);
a	min(b)
100	1
200	1
300	2
400	2
SELECT distinct 1 FROM t1 group by a order by count(*)-count(b);
ERROR HY000: Expression #1 of ORDER BY clause is not in SELECT list, contains aggregate function; this is incompatible with DISTINCT
aggregation in outer Q => constant in inner Q
select * from t1 as t2 where t2.a in
(SELECT distinct 1 FROM t1 group by a order by count(t2.a)-max(t2.a));
a	b	c
ORDER BY expressions are in SELECT list => ok
SELECT distinct 1, count(*)-count(b) FROM t1 group by a order by count(*)-count(b);
1	count(*)-count(b)
1	0
Without GROUP BY, aggregates yield a single row, no random order
SELECT distinct sum(a) FROM t1 order by count(*)-count(*);
sum(a)
1000
SELECT distinct sum(a) FROM t1 order by count(*)-count(b);
sum(a)
1000
Verify that DISTINCT is optimized away even if the aggregate
function is hidden in a subquery
EXPLAIN SELECT DISTINCT MAX(b) FROM t1;
id	select_type	table	partitions	type	possible_keys	key	key_len	ref	rows	filtered	Extra
1	SIMPLE	t1	NULL	ALL	NULL	NULL	NULL	NULL	4	100.00	NULL
Warnings:
Note	1003	/* select#1 */ select max(`test`.`t1`.`b`) AS `MAX(b)` from `test`.`t1`
EXPLAIN SELECT DISTINCT (SELECT MAX(t1.b) FROM t1 AS t2 LIMIT 1) FROM t1;
id	select_type	table	partitions	type	possible_keys	key	key_len	ref	rows	filtered	Extra
1	PRIMARY	t1	NULL	ALL	NULL	NULL	NULL	NULL	4	100.00	NULL
2	DEPENDENT SUBQUERY	t2	NULL	ALL	NULL	NULL	NULL	NULL	4	100.00	NULL
Warnings:
Note	1276	Field or reference 'test.t1.b' of SELECT #2 was resolved in SELECT #1
Note	1003	/* select#1 */ select (/* select#2 */ select max(`test`.`t1`.`b`) from `test`.`t1` `t2` limit 1) AS `(SELECT MAX(t1.b) FROM t1 AS t2 LIMIT 1)` from `test`.`t1`
but if the subquery is the aggregation query, DISTINCT must stay:
EXPLAIN SELECT DISTINCT (SELECT MAX(t1.b+0*t2.a) FROM t1 AS t2 LIMIT 1) FROM t1;
id	select_type	table	partitions	type	possible_keys	key	key_len	ref	rows	filtered	Extra
1	PRIMARY	t1	NULL	ALL	NULL	NULL	NULL	NULL	4	100.00	Using temporary
2	DEPENDENT SUBQUERY	t2	NULL	ALL	NULL	NULL	NULL	NULL	4	100.00	NULL
Warnings:
Note	1276	Field or reference 'test.t1.b' of SELECT #2 was resolved in SELECT #1
Note	1003	/* select#1 */ select distinct (/* select#2 */ select max((`test`.`t1`.`b` + (0 * `test`.`t2`.`a`))) from `test`.`t1` `t2` limit 1) AS `(SELECT MAX(t1.b+0*t2.a) FROM t1 AS t2 LIMIT 1)` from `test`.`t1`
QA's query is properly rejected:
SELECT DISTINCT GP1.a AS g1 FROM t1 AS GP1
WHERE GP1.a >= 0
ORDER BY GP1.b LIMIT 8;
ERROR HY000: Expression #1 of ORDER BY clause is not in SELECT list, references column 'test.GP1.b' which is not in SELECT list; this is incompatible with DISTINCT
result order does change depending on chosen plan.
SELECT DISTINCT GP1.a AS g1 FROM t1 AS GP1
WHERE GP1.a >= 0
ORDER BY 2+ANY_VALUE(GP1.b) LIMIT 8;
g1
100
200
300
400
DELETE FROM t1;
# Test when selecting from view
create view v1 as select t1.* from t1 left join t2 on 1;
insert into t1 values(100,1,2),(200,1,1),(300,2,1),(400,2,2);
analyze table t1;
Table	Op	Msg_type	Msg_text
test.t1	analyze	status	OK
select distinct b from v1 order by c;
b
2
1
select distinct min(b) from v1 group by a order by min(c);
min(b)
1
2
explain select distinct min(b) from v1 group by a order by min(c);
id	select_type	table	partitions	type	possible_keys	key	key_len	ref	rows	filtered	Extra
1	SIMPLE	t1	NULL	ALL	NULL	NULL	NULL	#	4	100.00	Using temporary; Using filesort
1	SIMPLE	t2	NULL	ALL	NULL	NULL	NULL	#	1	100.00	Using where; Using join buffer (hash join)
Note	1003	/* select#1 */ select distinct min(`test`.`t1`.`b`) AS `min(b)` from `test`.`t1` left join `test`.`t2` on(true) where true group by `test`.`t1`.`a` order by min(`test`.`t1`.`c`)
Warnings:
Insert rows in different order:
delete from t1;
insert into t1 values(200,1,1),(100,1,2),(400,2,2),(300,2,1);
analyze table t1;
Table	Op	Msg_type	Msg_text
test.t1	analyze	status	OK
And get a different order. Query is executed like this:
- First, DISTINCT, using a tmp MEMORY table with a unique
index, thus if two rows have the same 'b' but a different 'c',
the second row is rejected, so the first value of 'c' wins
(=> randomness of 'c')
- Second, ORDER BY on the random 'c'.
select distinct b from v1 order by c;
b
1
2
Random order too (same reason):
select distinct min(b) from v1 group by a order by min(c);
min(b)
1
2
This query gives random order:
select distinct b from v1 order by c;
ERROR HY000: Expression #1 of ORDER BY clause is not in SELECT list, references column 'test.v1.c' which is not in SELECT list; this is incompatible with DISTINCT
and this one too:
select distinct b from v1 order by b-1,b+1,c;
ERROR HY000: Expression #3 of ORDER BY clause is not in SELECT list, references column 'test.v1.c' which is not in SELECT list; this is incompatible with DISTINCT
and this one too:
select distinct min(b) from v1 group by a order by min(c);
ERROR HY000: Expression #1 of ORDER BY clause is not in SELECT list, contains aggregate function; this is incompatible with DISTINCT
Not random (though Standard bans aggregates from ORDER BY):
select distinct min(b) from v1 group by a order by min(b);
min(b)
1
2
select distinct min(b) from v1 group by a order by -min(b);
ERROR HY000: Expression #1 of ORDER BY clause is not in SELECT list, contains aggregate function; this is incompatible with DISTINCT
All group exprs are in select list => DISTINCT is removed => no error
select distinct a, min(b) from v1 group by a order by max(b-2)-min(c*5);
a	min(b)
100	1
400	2
200	1
300	2
This one is standard:
select distinct min(b) as z from v1 group by a order by z;
z
1
2
Other queries:
select distinct b from v1 where b<0 order by rand();
b
select distinct b from v1 order by 45.0+3;
b
1
2
select (select distinct b from v1 as S2 where b=7 order by S3.a) from v1 as S3;
(select distinct b from v1 as S2 where b=7 order by S3.a)
NULL
NULL
NULL
NULL
select distinct b from v1 order by abs(b);
b
1
2
select distinct b as z from v1 order by abs(z);
z
1
2
select distinct b from v1 order by abs(b+a);
ERROR HY000: Expression #1 of ORDER BY clause is not in SELECT list, references column 'test.v1.a' which is not in SELECT list; this is incompatible with DISTINCT
select distinct abs(b) as z from v1 order by z;
z
1
2
select distinct abs(b) as z from v1 order by abs(b);
z
1
2
select distinct abs(b) from v1 order by abs(b);
abs(b)
1
2
Not ok: ABS(b)+1 is neither a SELECTed expression nor an alias
to one, and mentions a column of FROM tables.
select distinct abs(b) as z from v1 order by abs(b)+1;
ERROR HY000: Expression #1 of ORDER BY clause is not in SELECT list, references column 'test.v1.b' which is not in SELECT list; this is incompatible with DISTINCT
select distinct abs(b) as z from v1 order by z+1;
z
1
2
select distinct abs(b) from v1 order by abs(b)+1;
ERROR HY000: Expression #1 of ORDER BY clause is not in SELECT list, references column 'test.v1.b' which is not in SELECT list; this is incompatible with DISTINCT
select distinct abs(b) as z from v1 order by floor(10*b);
ERROR HY000: Expression #1 of ORDER BY clause is not in SELECT list, references column 'test.v1.b' which is not in SELECT list; this is incompatible with DISTINCT
select distinct abs(b) from v1 order by floor(10*b);
ERROR HY000: Expression #1 of ORDER BY clause is not in SELECT list, references column 'test.v1.b' which is not in SELECT list; this is incompatible with DISTINCT
Two offending columns; error message needs to report only one
select distinct abs(b) from v1 order by floor(10*b),floor(10*a);
ERROR HY000: Expression #1 of ORDER BY clause is not in SELECT list, references column 'test.v1.b' which is not in SELECT list; this is incompatible with DISTINCT
select distinct abs(b) from v1 as S2 order by
(select floor(10*S2.b) from v1 as S3 limit 1);
ERROR HY000: Expression #1 of ORDER BY clause is not in SELECT list, references column 'test.S2.b' which is not in SELECT list; this is incompatible with DISTINCT
Ok as S2.b in SELECT list
select distinct abs(b),b from v1 as S2 order by
(select floor(10*S2.b) from v1 as S3 limit 1);
abs(b)	b
1	1
2	2
Ok as subq does not use columns of FROM clause of ordered Q.
select distinct abs(b) from v1 as S2 order by
(select floor(10*S3.b) from v1 as S3 limit 1);
abs(b)
1
2
Subq as alias => ok
select distinct abs(b),
(select floor(10*S3.b) from v1 as S3 limit 1) as subq
from v1 as S2 order by subq;
abs(b)	subq
1	10
2	10
Bad field in left or right argument of ALL/ANY(subq):
select distinct abs(b) from v1 as S2 order by
floor(10*S2.b) IN (select floor(10*S3.b) from v1 as S3);
ERROR HY000: Expression #1 of ORDER BY clause is not in SELECT list, references column 'test.S2.b' which is not in SELECT list; this is incompatible with DISTINCT
select distinct abs(b) from v1 as S2 order by
floor(10*S2.b) > ALL(select floor(10*S3.b) from v1 as S3);
ERROR HY000: Expression #1 of ORDER BY clause is not in SELECT list, references column 'test.S2.b' which is not in SELECT list; this is incompatible with DISTINCT
select distinct abs(b) from v1 as S2 order by
floor(10*10) IN (select floor(10*S2.b) from v1 as S3);
ERROR HY000: Expression #1 of ORDER BY clause is not in SELECT list, references column 'test.S2.b' which is not in SELECT list; this is incompatible with DISTINCT
select distinct abs(b) from v1 as S2 order by
floor(10*10) > ALL(select floor(10*S2.b) from v1 as S3);
ERROR HY000: Expression #1 of ORDER BY clause is not in SELECT list, references column 'test.S2.b' which is not in SELECT list; this is incompatible with DISTINCT
Aggregates:
SELECT distinct 1 FROM t1 group by a order by count(*);
ERROR HY000: Expression #1 of ORDER BY clause is not in SELECT list, contains aggregate function; this is incompatible with DISTINCT
SELECT distinct 1 FROM t1 group by a order by count(*)-count(*);
ERROR HY000: Expression #1 of ORDER BY clause is not in SELECT list, contains aggregate function; this is incompatible with DISTINCT
Test ANY_VALUE
SELECT distinct 1 FROM t1 group by a order by any_value(count(*)-count(b));
1
1
SELECT distinct 1 FROM t1 group by a order by any_value(count(*))-any_value(count(b));
1
1
All group exprs are in select list => DISTINCT is removed => no error
SELECT distinct a, min(b) FROM t1 group by a order by count(*)-count(*);
a	min(b)
100	1
200	1
300	2
400	2
SELECT distinct 1 FROM t1 group by a order by count(*)-count(b);
ERROR HY000: Expression #1 of ORDER BY clause is not in SELECT list, contains aggregate function; this is incompatible with DISTINCT
aggregation in outer Q => constant in inner Q
select * from t1 as t2 where t2.a in
(SELECT distinct 1 FROM t1 group by a order by count(t2.a)-max(t2.a));
a	b	c
ORDER BY expressions are in SELECT list => ok
SELECT distinct 1, count(*)-count(b) FROM t1 group by a order by count(*)-count(b);
1	count(*)-count(b)
1	0
Without GROUP BY, aggregates yield a single row, no random order
SELECT distinct sum(a) FROM t1 order by count(*)-count(*);
sum(a)
1000
SELECT distinct sum(a) FROM t1 order by count(*)-count(b);
sum(a)
1000
Verify that DISTINCT is optimized away even if the aggregate
function is hidden in a subquery
EXPLAIN SELECT DISTINCT MAX(b) FROM t1;
id	select_type	table	partitions	type	possible_keys	key	key_len	ref	rows	filtered	Extra
1	SIMPLE	t1	NULL	ALL	NULL	NULL	NULL	NULL	4	100.00	NULL
Warnings:
Note	1003	/* select#1 */ select max(`test`.`t1`.`b`) AS `MAX(b)` from `test`.`t1`
EXPLAIN SELECT DISTINCT (SELECT MAX(t1.b) FROM t1 AS t2 LIMIT 1) FROM t1;
id	select_type	table	partitions	type	possible_keys	key	key_len	ref	rows	filtered	Extra
1	PRIMARY	t1	NULL	ALL	NULL	NULL	NULL	NULL	4	100.00	NULL
2	DEPENDENT SUBQUERY	t2	NULL	ALL	NULL	NULL	NULL	NULL	4	100.00	NULL
Warnings:
Note	1276	Field or reference 'test.t1.b' of SELECT #2 was resolved in SELECT #1
Note	1003	/* select#1 */ select (/* select#2 */ select max(`test`.`t1`.`b`) from `test`.`t1` `t2` limit 1) AS `(SELECT MAX(t1.b) FROM t1 AS t2 LIMIT 1)` from `test`.`t1`
but if the subquery is the aggregation query, DISTINCT must stay:
EXPLAIN SELECT DISTINCT (SELECT MAX(t1.b+0*t2.a) FROM t1 AS t2 LIMIT 1) FROM t1;
id	select_type	table	partitions	type	possible_keys	key	key_len	ref	rows	filtered	Extra
1	PRIMARY	t1	NULL	ALL	NULL	NULL	NULL	NULL	4	100.00	Using temporary
2	DEPENDENT SUBQUERY	t2	NULL	ALL	NULL	NULL	NULL	NULL	4	100.00	NULL
Warnings:
Note	1276	Field or reference 'test.t1.b' of SELECT #2 was resolved in SELECT #1
Note	1003	/* select#1 */ select distinct (/* select#2 */ select max((`test`.`t1`.`b` + (0 * `test`.`t2`.`a`))) from `test`.`t1` `t2` limit 1) AS `(SELECT MAX(t1.b+0*t2.a) FROM t1 AS t2 LIMIT 1)` from `test`.`t1`
QA's query is properly rejected:
SELECT DISTINCT GP1.a AS g1 FROM v1 AS GP1
WHERE GP1.a >= 0
ORDER BY GP1.b LIMIT 8;
ERROR HY000: Expression #1 of ORDER BY clause is not in SELECT list, references column 'test.GP1.b' which is not in SELECT list; this is incompatible with DISTINCT
result order does change depending on chosen plan.
SELECT DISTINCT GP1.a AS g1 FROM v1 AS GP1
WHERE GP1.a >= 0
ORDER BY 2+ANY_VALUE(GP1.b) LIMIT 8;
g1
100
200
300
400
DELETE FROM t1;
drop view v1;
# Test when selecting from view, again
create view v1 as select t1.a*2 as a, t1.b*2 as b, t1.c*2 as c from t1;
insert into t1 values(100,1,2),(200,1,1),(300,2,1),(400,2,2);
analyze table t1;
Table	Op	Msg_type	Msg_text
test.t1	analyze	status	OK
select distinct b from v1 order by c;
b
4
2
select distinct min(b) from v1 group by a order by min(c);
min(b)
2
4
explain select distinct min(b) from v1 group by a order by min(c);
id	select_type	table	partitions	type	possible_keys	key	key_len	ref	rows	filtered	Extra
1	SIMPLE	t1	NULL	ALL	NULL	NULL	NULL	#	4	100.00	Using temporary; Using filesort
Note	1003	/* select#1 */ select distinct min((`test`.`t1`.`b` * 2)) AS `min(b)` from `test`.`t1` group by (`test`.`t1`.`a` * 2) order by min((`test`.`t1`.`c` * 2))
Warnings:
Insert rows in different order:
delete from t1;
insert into t1 values(200,1,1),(100,1,2),(400,2,2),(300,2,1);
analyze table t1;
Table	Op	Msg_type	Msg_text
test.t1	analyze	status	OK
And get a different order. Query is executed like this:
- First, DISTINCT, using a tmp MEMORY table with a unique
index, thus if two rows have the same 'b' but a different 'c',
the second row is rejected, so the first value of 'c' wins
(=> randomness of 'c')
- Second, ORDER BY on the random 'c'.
select distinct b from v1 order by c;
b
2
4
Random order too (same reason):
select distinct min(b) from v1 group by a order by min(c);
min(b)
2
4
This query gives random order:
select distinct b from v1 order by c;
ERROR HY000: Expression #1 of ORDER BY clause is not in SELECT list, references column 'test.v1.c' which is not in SELECT list; this is incompatible with DISTINCT
and this one too:
select distinct b from v1 order by b-1,b+1,c;
ERROR HY000: Expression #3 of ORDER BY clause is not in SELECT list, references column 'test.v1.c' which is not in SELECT list; this is incompatible with DISTINCT
and this one too:
select distinct min(b) from v1 group by a order by min(c);
ERROR HY000: Expression #1 of ORDER BY clause is not in SELECT list, contains aggregate function; this is incompatible with DISTINCT
Not random (though Standard bans aggregates from ORDER BY):
select distinct min(b) from v1 group by a order by min(b);
min(b)
2
4
select distinct min(b) from v1 group by a order by -min(b);
ERROR HY000: Expression #1 of ORDER BY clause is not in SELECT list, contains aggregate function; this is incompatible with DISTINCT
All group exprs are in select list => DISTINCT is removed => no error
select distinct a, min(b) from v1 group by a order by max(b-2)-min(c*5);
a	min(b)
200	2
800	4
400	2
600	4
This one is standard:
select distinct min(b) as z from v1 group by a order by z;
z
2
4
Other queries:
select distinct b from v1 where b<0 order by rand();
b
select distinct b from v1 order by 45.0+3;
b
2
4
select (select distinct b from v1 as S2 where b=7 order by S3.a) from v1 as S3;
(select distinct b from v1 as S2 where b=7 order by S3.a)
NULL
NULL
NULL
NULL
select distinct b from v1 order by abs(b);
b
2
4
select distinct b as z from v1 order by abs(z);
z
2
4
select distinct b from v1 order by abs(b+a);
ERROR HY000: Expression #1 of ORDER BY clause is not in SELECT list, references column 'test.v1.a' which is not in SELECT list; this is incompatible with DISTINCT
select distinct abs(b) as z from v1 order by z;
z
2
4
select distinct abs(b) as z from v1 order by abs(b);
z
2
4
select distinct abs(b) from v1 order by abs(b);
abs(b)
2
4
Not ok: ABS(b)+1 is neither a SELECTed expression nor an alias
to one, and mentions a column of FROM tables.
select distinct abs(b) as z from v1 order by abs(b)+1;
ERROR HY000: Expression #1 of ORDER BY clause is not in SELECT list, references column 'test.v1.b' which is not in SELECT list; this is incompatible with DISTINCT
select distinct abs(b) as z from v1 order by z+1;
z
2
4
select distinct abs(b) from v1 order by abs(b)+1;
ERROR HY000: Expression #1 of ORDER BY clause is not in SELECT list, references column 'test.v1.b' which is not in SELECT list; this is incompatible with DISTINCT
select distinct abs(b) as z from v1 order by floor(10*b);
ERROR HY000: Expression #1 of ORDER BY clause is not in SELECT list, references column 'test.v1.b' which is not in SELECT list; this is incompatible with DISTINCT
select distinct abs(b) from v1 order by floor(10*b);
ERROR HY000: Expression #1 of ORDER BY clause is not in SELECT list, references column 'test.v1.b' which is not in SELECT list; this is incompatible with DISTINCT
Two offending columns; error message needs to report only one
select distinct abs(b) from v1 order by floor(10*b),floor(10*a);
ERROR HY000: Expression #1 of ORDER BY clause is not in SELECT list, references column 'test.v1.b' which is not in SELECT list; this is incompatible with DISTINCT
select distinct abs(b) from v1 as S2 order by
(select floor(10*S2.b) from v1 as S3 limit 1);
ERROR HY000: Expression #1 of ORDER BY clause is not in SELECT list, references column 'test.S2.b' which is not in SELECT list; this is incompatible with DISTINCT
Ok as S2.b in SELECT list
select distinct abs(b),b from v1 as S2 order by
(select floor(10*S2.b) from v1 as S3 limit 1);
abs(b)	b
2	2
4	4
Ok as subq does not use columns of FROM clause of ordered Q.
select distinct abs(b) from v1 as S2 order by
(select floor(10*S3.b) from v1 as S3 limit 1);
abs(b)
2
4
Subq as alias => ok
select distinct abs(b),
(select floor(10*S3.b) from v1 as S3 limit 1) as subq
from v1 as S2 order by subq;
abs(b)	subq
2	20
4	20
Bad field in left or right argument of ALL/ANY(subq):
select distinct abs(b) from v1 as S2 order by
floor(10*S2.b) IN (select floor(10*S3.b) from v1 as S3);
ERROR HY000: Expression #1 of ORDER BY clause is not in SELECT list, references column 'test.S2.b' which is not in SELECT list; this is incompatible with DISTINCT
select distinct abs(b) from v1 as S2 order by
floor(10*S2.b) > ALL(select floor(10*S3.b) from v1 as S3);
ERROR HY000: Expression #1 of ORDER BY clause is not in SELECT list, references column 'test.S2.b' which is not in SELECT list; this is incompatible with DISTINCT
select distinct abs(b) from v1 as S2 order by
floor(10*10) IN (select floor(10*S2.b) from v1 as S3);
ERROR HY000: Expression #1 of ORDER BY clause is not in SELECT list, references column 'test.S2.b' which is not in SELECT list; this is incompatible with DISTINCT
select distinct abs(b) from v1 as S2 order by
floor(10*10) > ALL(select floor(10*S2.b) from v1 as S3);
ERROR HY000: Expression #1 of ORDER BY clause is not in SELECT list, references column 'test.S2.b' which is not in SELECT list; this is incompatible with DISTINCT
Aggregates:
SELECT distinct 1 FROM t1 group by a order by count(*);
ERROR HY000: Expression #1 of ORDER BY clause is not in SELECT list, contains aggregate function; this is incompatible with DISTINCT
SELECT distinct 1 FROM t1 group by a order by count(*)-count(*);
ERROR HY000: Expression #1 of ORDER BY clause is not in SELECT list, contains aggregate function; this is incompatible with DISTINCT
Test ANY_VALUE
SELECT distinct 1 FROM t1 group by a order by any_value(count(*)-count(b));
1
1
SELECT distinct 1 FROM t1 group by a order by any_value(count(*))-any_value(count(b));
1
1
All group exprs are in select list => DISTINCT is removed => no error
SELECT distinct a, min(b) FROM t1 group by a order by count(*)-count(*);
a	min(b)
100	1
200	1
300	2
400	2
SELECT distinct 1 FROM t1 group by a order by count(*)-count(b);
ERROR HY000: Expression #1 of ORDER BY clause is not in SELECT list, contains aggregate function; this is incompatible with DISTINCT
aggregation in outer Q => constant in inner Q
select * from t1 as t2 where t2.a in
(SELECT distinct 1 FROM t1 group by a order by count(t2.a)-max(t2.a));
a	b	c
ORDER BY expressions are in SELECT list => ok
SELECT distinct 1, count(*)-count(b) FROM t1 group by a order by count(*)-count(b);
1	count(*)-count(b)
1	0
Without GROUP BY, aggregates yield a single row, no random order
SELECT distinct sum(a) FROM t1 order by count(*)-count(*);
sum(a)
1000
SELECT distinct sum(a) FROM t1 order by count(*)-count(b);
sum(a)
1000
Verify that DISTINCT is optimized away even if the aggregate
function is hidden in a subquery
EXPLAIN SELECT DISTINCT MAX(b) FROM t1;
id	select_type	table	partitions	type	possible_keys	key	key_len	ref	rows	filtered	Extra
1	SIMPLE	t1	NULL	ALL	NULL	NULL	NULL	NULL	4	100.00	NULL
Warnings:
Note	1003	/* select#1 */ select max(`test`.`t1`.`b`) AS `MAX(b)` from `test`.`t1`
EXPLAIN SELECT DISTINCT (SELECT MAX(t1.b) FROM t1 AS t2 LIMIT 1) FROM t1;
id	select_type	table	partitions	type	possible_keys	key	key_len	ref	rows	filtered	Extra
1	PRIMARY	t1	NULL	ALL	NULL	NULL	NULL	NULL	4	100.00	NULL
2	DEPENDENT SUBQUERY	t2	NULL	ALL	NULL	NULL	NULL	NULL	4	100.00	NULL
Warnings:
Note	1276	Field or reference 'test.t1.b' of SELECT #2 was resolved in SELECT #1
Note	1003	/* select#1 */ select (/* select#2 */ select max(`test`.`t1`.`b`) from `test`.`t1` `t2` limit 1) AS `(SELECT MAX(t1.b) FROM t1 AS t2 LIMIT 1)` from `test`.`t1`
but if the subquery is the aggregation query, DISTINCT must stay:
EXPLAIN SELECT DISTINCT (SELECT MAX(t1.b+0*t2.a) FROM t1 AS t2 LIMIT 1) FROM t1;
id	select_type	table	partitions	type	possible_keys	key	key_len	ref	rows	filtered	Extra
1	PRIMARY	t1	NULL	ALL	NULL	NULL	NULL	NULL	4	100.00	Using temporary
2	DEPENDENT SUBQUERY	t2	NULL	ALL	NULL	NULL	NULL	NULL	4	100.00	NULL
Warnings:
Note	1276	Field or reference 'test.t1.b' of SELECT #2 was resolved in SELECT #1
Note	1003	/* select#1 */ select distinct (/* select#2 */ select max((`test`.`t1`.`b` + (0 * `test`.`t2`.`a`))) from `test`.`t1` `t2` limit 1) AS `(SELECT MAX(t1.b+0*t2.a) FROM t1 AS t2 LIMIT 1)` from `test`.`t1`
QA's query is properly rejected:
SELECT DISTINCT GP1.a AS g1 FROM v1 AS GP1
WHERE GP1.a >= 0
ORDER BY GP1.b LIMIT 8;
ERROR HY000: Expression #1 of ORDER BY clause is not in SELECT list, references column 'test.GP1.b' which is not in SELECT list; this is incompatible with DISTINCT
result order does change depending on chosen plan.
SELECT DISTINCT GP1.a AS g1 FROM v1 AS GP1
WHERE GP1.a >= 0
ORDER BY 2+ANY_VALUE(GP1.b) LIMIT 8;
g1
200
400
600
800
DELETE FROM t1;
drop view v1;
# Test when selecting from derived table
insert into t1 values(100,1,2),(200,1,1),(300,2,1),(400,2,2);
analyze table t1;
Table	Op	Msg_type	Msg_text
test.t1	analyze	status	OK
select distinct b from (SELECT t1.* FROM t1 left join t2 on 1) AS derived order by c;
b
2
1
select distinct min(b) from (SELECT t1.* FROM t1 left join t2 on 1) AS derived group by a order by min(c);
min(b)
1
2
explain select distinct min(b) from (SELECT t1.* FROM t1 left join t2 on 1) AS derived group by a order by min(c);
id	select_type	table	partitions	type	possible_keys	key	key_len	ref	rows	filtered	Extra
1	SIMPLE	t1	NULL	ALL	NULL	NULL	NULL	#	4	100.00	Using temporary; Using filesort
1	SIMPLE	t2	NULL	ALL	NULL	NULL	NULL	#	1	100.00	Using where; Using join buffer (hash join)
Note	1003	/* select#1 */ select distinct min(`test`.`t1`.`b`) AS `min(b)` from `test`.`t1` left join `test`.`t2` on(true) where true group by `test`.`t1`.`a` order by min(`test`.`t1`.`c`)
Warnings:
Insert rows in different order:
delete from t1;
insert into t1 values(200,1,1),(100,1,2),(400,2,2),(300,2,1);
analyze table t1;
Table	Op	Msg_type	Msg_text
test.t1	analyze	status	OK
And get a different order. Query is executed like this:
- First, DISTINCT, using a tmp MEMORY table with a unique
index, thus if two rows have the same 'b' but a different 'c',
the second row is rejected, so the first value of 'c' wins
(=> randomness of 'c')
- Second, ORDER BY on the random 'c'.
select distinct b from (SELECT t1.* FROM t1 left join t2 on 1) AS derived order by c;
b
1
2
Random order too (same reason):
select distinct min(b) from (SELECT t1.* FROM t1 left join t2 on 1) AS derived group by a order by min(c);
min(b)
1
2
This query gives random order:
select distinct b from (SELECT t1.* FROM t1 left join t2 on 1) AS derived order by c;
ERROR HY000: Expression #1 of ORDER BY clause is not in SELECT list, references column 'derived.c' which is not in SELECT list; this is incompatible with DISTINCT
and this one too:
select distinct b from (SELECT t1.* FROM t1 left join t2 on 1) AS derived order by b-1,b+1,c;
ERROR HY000: Expression #3 of ORDER BY clause is not in SELECT list, references column 'derived.c' which is not in SELECT list; this is incompatible with DISTINCT
and this one too:
select distinct min(b) from (SELECT t1.* FROM t1 left join t2 on 1) AS derived group by a order by min(c);
ERROR HY000: Expression #1 of ORDER BY clause is not in SELECT list, contains aggregate function; this is incompatible with DISTINCT
Not random (though Standard bans aggregates from ORDER BY):
select distinct min(b) from (SELECT t1.* FROM t1 left join t2 on 1) AS derived group by a order by min(b);
min(b)
1
2
select distinct min(b) from (SELECT t1.* FROM t1 left join t2 on 1) AS derived group by a order by -min(b);
ERROR HY000: Expression #1 of ORDER BY clause is not in SELECT list, contains aggregate function; this is incompatible with DISTINCT
All group exprs are in select list => DISTINCT is removed => no error
select distinct a, min(b) from (SELECT t1.* FROM t1 left join t2 on 1) AS derived group by a order by max(b-2)-min(c*5);
a	min(b)
100	1
400	2
200	1
300	2
This one is standard:
select distinct min(b) as z from (SELECT t1.* FROM t1 left join t2 on 1) AS derived group by a order by z;
z
1
2
Other queries:
select distinct b from (SELECT t1.* FROM t1 left join t2 on 1) AS derived where b<0 order by rand();
b
select distinct b from (SELECT t1.* FROM t1 left join t2 on 1) AS derived order by 45.0+3;
b
1
2
select (select distinct b from (SELECT t1.* FROM t1 left join t2 on 1) as S2 where b=7 order by S3.a) from (SELECT t1.* FROM t1 left join t2 on 1) as S3;
(select distinct b from (SELECT t1.* FROM t1 left join t2 on 1) as S2 where b=7 order by S3.a)
NULL
NULL
NULL
NULL
select distinct b from (SELECT t1.* FROM t1 left join t2 on 1) AS derived order by abs(b);
b
1
2
select distinct b as z from (SELECT t1.* FROM t1 left join t2 on 1) AS derived order by abs(z);
z
1
2
select distinct b from (SELECT t1.* FROM t1 left join t2 on 1) AS derived order by abs(b+a);
ERROR HY000: Expression #1 of ORDER BY clause is not in SELECT list, references column 'derived.a' which is not in SELECT list; this is incompatible with DISTINCT
select distinct abs(b) as z from (SELECT t1.* FROM t1 left join t2 on 1) AS derived order by z;
z
1
2
select distinct abs(b) as z from (SELECT t1.* FROM t1 left join t2 on 1) AS derived order by abs(b);
z
1
2
select distinct abs(b) from (SELECT t1.* FROM t1 left join t2 on 1) AS derived order by abs(b);
abs(b)
1
2
Not ok: ABS(b)+1 is neither a SELECTed expression nor an alias
to one, and mentions a column of FROM tables.
select distinct abs(b) as z from (SELECT t1.* FROM t1 left join t2 on 1) AS derived order by abs(b)+1;
ERROR HY000: Expression #1 of ORDER BY clause is not in SELECT list, references column 'derived.b' which is not in SELECT list; this is incompatible with DISTINCT
select distinct abs(b) as z from (SELECT t1.* FROM t1 left join t2 on 1) AS derived order by z+1;
z
1
2
select distinct abs(b) from (SELECT t1.* FROM t1 left join t2 on 1) AS derived order by abs(b)+1;
ERROR HY000: Expression #1 of ORDER BY clause is not in SELECT list, references column 'derived.b' which is not in SELECT list; this is incompatible with DISTINCT
select distinct abs(b) as z from (SELECT t1.* FROM t1 left join t2 on 1) AS derived order by floor(10*b);
ERROR HY000: Expression #1 of ORDER BY clause is not in SELECT list, references column 'derived.b' which is not in SELECT list; this is incompatible with DISTINCT
select distinct abs(b) from (SELECT t1.* FROM t1 left join t2 on 1) AS derived order by floor(10*b);
ERROR HY000: Expression #1 of ORDER BY clause is not in SELECT list, references column 'derived.b' which is not in SELECT list; this is incompatible with DISTINCT
Two offending columns; error message needs to report only one
select distinct abs(b) from (SELECT t1.* FROM t1 left join t2 on 1) AS derived order by floor(10*b),floor(10*a);
ERROR HY000: Expression #1 of ORDER BY clause is not in SELECT list, references column 'derived.b' which is not in SELECT list; this is incompatible with DISTINCT
select distinct abs(b) from (SELECT t1.* FROM t1 left join t2 on 1) as S2 order by
(select floor(10*S2.b) from (SELECT t1.* FROM t1 left join t2 on 1) as S3 limit 1);
ERROR HY000: Expression #1 of ORDER BY clause is not in SELECT list, references column 'S2.b' which is not in SELECT list; this is incompatible with DISTINCT
Ok as S2.b in SELECT list
select distinct abs(b),b from (SELECT t1.* FROM t1 left join t2 on 1) as S2 order by
(select floor(10*S2.b) from (SELECT t1.* FROM t1 left join t2 on 1) as S3 limit 1);
abs(b)	b
1	1
2	2
Ok as subq does not use columns of FROM clause of ordered Q.
select distinct abs(b) from (SELECT t1.* FROM t1 left join t2 on 1) as S2 order by
(select floor(10*S3.b) from (SELECT t1.* FROM t1 left join t2 on 1) as S3 limit 1);
abs(b)
1
2
Subq as alias => ok
select distinct abs(b),
(select floor(10*S3.b) from (SELECT t1.* FROM t1 left join t2 on 1) as S3 limit 1) as subq
from (SELECT t1.* FROM t1 left join t2 on 1) as S2 order by subq;
abs(b)	subq
1	10
2	10
Bad field in left or right argument of ALL/ANY(subq):
select distinct abs(b) from (SELECT t1.* FROM t1 left join t2 on 1) as S2 order by
floor(10*S2.b) IN (select floor(10*S3.b) from (SELECT t1.* FROM t1 left join t2 on 1) as S3);
ERROR HY000: Expression #1 of ORDER BY clause is not in SELECT list, references column 'S2.b' which is not in SELECT list; this is incompatible with DISTINCT
select distinct abs(b) from (SELECT t1.* FROM t1 left join t2 on 1) as S2 order by
floor(10*S2.b) > ALL(select floor(10*S3.b) from (SELECT t1.* FROM t1 left join t2 on 1) as S3);
ERROR HY000: Expression #1 of ORDER BY clause is not in SELECT list, references column 'S2.b' which is not in SELECT list; this is incompatible with DISTINCT
select distinct abs(b) from (SELECT t1.* FROM t1 left join t2 on 1) as S2 order by
floor(10*10) IN (select floor(10*S2.b) from (SELECT t1.* FROM t1 left join t2 on 1) as S3);
ERROR HY000: Expression #1 of ORDER BY clause is not in SELECT list, references column 'S2.b' which is not in SELECT list; this is incompatible with DISTINCT
select distinct abs(b) from (SELECT t1.* FROM t1 left join t2 on 1) as S2 order by
floor(10*10) > ALL(select floor(10*S2.b) from (SELECT t1.* FROM t1 left join t2 on 1) as S3);
ERROR HY000: Expression #1 of ORDER BY clause is not in SELECT list, references column 'S2.b' which is not in SELECT list; this is incompatible with DISTINCT
Aggregates:
SELECT distinct 1 FROM t1 group by a order by count(*);
ERROR HY000: Expression #1 of ORDER BY clause is not in SELECT list, contains aggregate function; this is incompatible with DISTINCT
SELECT distinct 1 FROM t1 group by a order by count(*)-count(*);
ERROR HY000: Expression #1 of ORDER BY clause is not in SELECT list, contains aggregate function; this is incompatible with DISTINCT
Test ANY_VALUE
SELECT distinct 1 FROM t1 group by a order by any_value(count(*)-count(b));
1
1
SELECT distinct 1 FROM t1 group by a order by any_value(count(*))-any_value(count(b));
1
1
All group exprs are in select list => DISTINCT is removed => no error
SELECT distinct a, min(b) FROM t1 group by a order by count(*)-count(*);
a	min(b)
100	1
200	1
300	2
400	2
SELECT distinct 1 FROM t1 group by a order by count(*)-count(b);
ERROR HY000: Expression #1 of ORDER BY clause is not in SELECT list, contains aggregate function; this is incompatible with DISTINCT
aggregation in outer Q => constant in inner Q
select * from t1 as t2 where t2.a in
(SELECT distinct 1 FROM t1 group by a order by count(t2.a)-max(t2.a));
a	b	c
ORDER BY expressions are in SELECT list => ok
SELECT distinct 1, count(*)-count(b) FROM t1 group by a order by count(*)-count(b);
1	count(*)-count(b)
1	0
Without GROUP BY, aggregates yield a single row, no random order
SELECT distinct sum(a) FROM t1 order by count(*)-count(*);
sum(a)
1000
SELECT distinct sum(a) FROM t1 order by count(*)-count(b);
sum(a)
1000
Verify that DISTINCT is optimized away even if the aggregate
function is hidden in a subquery
EXPLAIN SELECT DISTINCT MAX(b) FROM t1;
id	select_type	table	partitions	type	possible_keys	key	key_len	ref	rows	filtered	Extra
1	SIMPLE	t1	NULL	ALL	NULL	NULL	NULL	NULL	4	100.00	NULL
Warnings:
Note	1003	/* select#1 */ select max(`test`.`t1`.`b`) AS `MAX(b)` from `test`.`t1`
EXPLAIN SELECT DISTINCT (SELECT MAX(t1.b) FROM t1 AS t2 LIMIT 1) FROM t1;
id	select_type	table	partitions	type	possible_keys	key	key_len	ref	rows	filtered	Extra
1	PRIMARY	t1	NULL	ALL	NULL	NULL	NULL	NULL	4	100.00	NULL
2	DEPENDENT SUBQUERY	t2	NULL	ALL	NULL	NULL	NULL	NULL	4	100.00	NULL
Warnings:
Note	1276	Field or reference 'test.t1.b' of SELECT #2 was resolved in SELECT #1
Note	1003	/* select#1 */ select (/* select#2 */ select max(`test`.`t1`.`b`) from `test`.`t1` `t2` limit 1) AS `(SELECT MAX(t1.b) FROM t1 AS t2 LIMIT 1)` from `test`.`t1`
but if the subquery is the aggregation query, DISTINCT must stay:
EXPLAIN SELECT DISTINCT (SELECT MAX(t1.b+0*t2.a) FROM t1 AS t2 LIMIT 1) FROM t1;
id	select_type	table	partitions	type	possible_keys	key	key_len	ref	rows	filtered	Extra
1	PRIMARY	t1	NULL	ALL	NULL	NULL	NULL	NULL	4	100.00	Using temporary
2	DEPENDENT SUBQUERY	t2	NULL	ALL	NULL	NULL	NULL	NULL	4	100.00	NULL
Warnings:
Note	1276	Field or reference 'test.t1.b' of SELECT #2 was resolved in SELECT #1
Note	1003	/* select#1 */ select distinct (/* select#2 */ select max((`test`.`t1`.`b` + (0 * `test`.`t2`.`a`))) from `test`.`t1` `t2` limit 1) AS `(SELECT MAX(t1.b+0*t2.a) FROM t1 AS t2 LIMIT 1)` from `test`.`t1`
QA's query is properly rejected:
SELECT DISTINCT GP1.a AS g1 FROM (SELECT t1.* FROM t1 left join t2 on 1) AS GP1
WHERE GP1.a >= 0
ORDER BY GP1.b LIMIT 8;
ERROR HY000: Expression #1 of ORDER BY clause is not in SELECT list, references column 'GP1.b' which is not in SELECT list; this is incompatible with DISTINCT
result order does change depending on chosen plan.
SELECT DISTINCT GP1.a AS g1 FROM (SELECT t1.* FROM t1 left join t2 on 1) AS GP1
WHERE GP1.a >= 0
ORDER BY 2+ANY_VALUE(GP1.b) LIMIT 8;
g1
100
200
300
400
DELETE FROM t1;
select distinct t1_outer.a from t1 t1_outer
order by t1_outer.b;
ERROR HY000: Expression #1 of ORDER BY clause is not in SELECT list, references column 'test.t1_outer.b' which is not in SELECT list; this is incompatible with DISTINCT
select distinct t1_outer.a from t1 t1_outer
order by (select max(t1_outer.b+t1_inner.b) from t1 t1_inner);
ERROR HY000: Expression #1 of ORDER BY clause is not in SELECT list, references column 'test.t1_outer.b' which is not in SELECT list; this is incompatible with DISTINCT
select
(select distinct 1 from t1 t1_inner
group by t1_inner.a order by max(t1_outer.b))
from t1 t1_outer;
(select distinct 1 from t1 t1_inner
group by t1_inner.a order by max(t1_outer.b))
NULL
drop table t1, t2;