-
Notifications
You must be signed in to change notification settings - Fork 4.1k
Expand file tree
/
Copy pathdistinct
More file actions
351 lines (326 loc) · 10.5 KB
/
Copy pathdistinct
File metadata and controls
351 lines (326 loc) · 10.5 KB
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
# tests adapted from logictest -- aggregate and distinct
exec-ddl
CREATE TABLE xyz (
x INT PRIMARY KEY,
y INT,
z FLOAT,
INDEX xy (x, y),
INDEX zyx (z, y, x),
FAMILY (x),
FAMILY (y),
FAMILY (z)
)
----
build
SELECT y, z FROM xyz
----
project
├── columns: y:2 z:3
└── scan xyz
└── columns: x:1!null y:2 z:3 crdb_internal_mvcc_timestamp:4 tableoid:5
build
SELECT DISTINCT y, z FROM xyz
----
distinct-on
├── columns: y:2 z:3
├── grouping columns: y:2 z:3
└── project
├── columns: y:2 z:3
└── scan xyz
└── columns: x:1!null y:2 z:3 crdb_internal_mvcc_timestamp:4 tableoid:5
build
SELECT y FROM (SELECT DISTINCT y, z FROM xyz)
----
project
├── columns: y:2
└── distinct-on
├── columns: y:2 z:3
├── grouping columns: y:2 z:3
└── project
├── columns: y:2 z:3
└── scan xyz
└── columns: x:1!null y:2 z:3 crdb_internal_mvcc_timestamp:4 tableoid:5
build
SELECT DISTINCT y, z FROM xyz ORDER BY z
----
sort
├── columns: y:2 z:3
├── ordering: +3
└── distinct-on
├── columns: y:2 z:3
├── grouping columns: y:2 z:3
└── project
├── columns: y:2 z:3
└── scan xyz
└── columns: x:1!null y:2 z:3 crdb_internal_mvcc_timestamp:4 tableoid:5
build
SELECT DISTINCT y, z FROM xyz ORDER BY y
----
sort
├── columns: y:2 z:3
├── ordering: +2
└── distinct-on
├── columns: y:2 z:3
├── grouping columns: y:2 z:3
└── project
├── columns: y:2 z:3
└── scan xyz
└── columns: x:1!null y:2 z:3 crdb_internal_mvcc_timestamp:4 tableoid:5
build
SELECT DISTINCT y, z FROM xyz ORDER BY y, z
----
distinct-on
├── columns: y:2 z:3
├── grouping columns: y:2 z:3
├── ordering: +2,+3
└── sort
├── columns: y:2 z:3
├── ordering: +2,+3
└── project
├── columns: y:2 z:3
└── scan xyz
└── columns: x:1!null y:2 z:3 crdb_internal_mvcc_timestamp:4 tableoid:5
build
SELECT DISTINCT y + x AS r FROM xyz ORDER by (y + x)
----
distinct-on
├── columns: r:6
├── grouping columns: r:6
├── ordering: +6
└── sort
├── columns: r:6
├── ordering: +6
└── project
├── columns: r:6
├── scan xyz
│ └── columns: x:1!null y:2 z:3 crdb_internal_mvcc_timestamp:4 tableoid:5
└── projections
└── y:2 + x:1 [as=r:6]
build
SELECT DISTINCT y + x AS r FROM xyz ORDER BY y + x
----
distinct-on
├── columns: r:6
├── grouping columns: r:6
├── ordering: +6
└── sort
├── columns: r:6
├── ordering: +6
└── project
├── columns: r:6
├── scan xyz
│ └── columns: x:1!null y:2 z:3 crdb_internal_mvcc_timestamp:4 tableoid:5
└── projections
└── y:2 + x:1 [as=r:6]
build
SELECT DISTINCT y + z FROM xyz ORDER BY y + z
----
error (22023): unsupported binary operator: <int> + <float>
# This query causes an error in Postgres, and the optimizer has followed
# that lead.
build
SELECT DISTINCT y AS w FROM xyz ORDER by z
----
error (42P10): for SELECT DISTINCT, ORDER BY expressions must appear in select list
build
SELECT DISTINCT y AS w FROM xyz ORDER by y
----
sort
├── columns: w:2
├── ordering: +2
└── distinct-on
├── columns: y:2
├── grouping columns: y:2
└── project
├── columns: y:2
└── scan xyz
└── columns: x:1!null y:2 z:3 crdb_internal_mvcc_timestamp:4 tableoid:5
build
SELECT DISTINCT (y,z) AS r FROM xyz
----
distinct-on
├── columns: r:6
├── grouping columns: r:6
└── project
├── columns: r:6
├── scan xyz
│ └── columns: x:1!null y:2 z:3 crdb_internal_mvcc_timestamp:4 tableoid:5
└── projections
└── (y:2, z:3) [as=r:6]
build
SELECT count(*) FROM (SELECT DISTINCT y FROM xyz)
----
scalar-group-by
├── columns: count:6!null
├── project
│ └── distinct-on
│ ├── columns: y:2
│ ├── grouping columns: y:2
│ └── project
│ ├── columns: y:2
│ └── scan xyz
│ └── columns: x:1!null y:2 z:3 crdb_internal_mvcc_timestamp:4 tableoid:5
└── aggregations
└── count-rows [as=count_rows:6]
build
SELECT DISTINCT x FROM xyz WHERE x > 0
----
distinct-on
├── columns: x:1!null
├── grouping columns: x:1!null
└── project
├── columns: x:1!null
└── select
├── columns: x:1!null y:2 z:3 crdb_internal_mvcc_timestamp:4 tableoid:5
├── scan xyz
│ └── columns: x:1!null y:2 z:3 crdb_internal_mvcc_timestamp:4 tableoid:5
└── filters
└── x:1 > 0
build
SELECT DISTINCT z FROM xyz WHERE x > 0
----
distinct-on
├── columns: z:3
├── grouping columns: z:3
└── project
├── columns: z:3
└── select
├── columns: x:1!null y:2 z:3 crdb_internal_mvcc_timestamp:4 tableoid:5
├── scan xyz
│ └── columns: x:1!null y:2 z:3 crdb_internal_mvcc_timestamp:4 tableoid:5
└── filters
└── x:1 > 0
build
SELECT DISTINCT max(x) FROM xyz GROUP BY x
----
distinct-on
├── columns: max:6!null
├── grouping columns: max:6!null
└── project
├── columns: max:6!null
└── group-by (hash)
├── columns: x:1!null max:6!null
├── grouping columns: x:1!null
├── project
│ ├── columns: x:1!null
│ └── scan xyz
│ └── columns: x:1!null y:2 z:3 crdb_internal_mvcc_timestamp:4 tableoid:5
└── aggregations
└── max [as=max:6]
└── x:1
build
SELECT DISTINCT x+y AS r FROM xyz
----
distinct-on
├── columns: r:6
├── grouping columns: r:6
└── project
├── columns: r:6
├── scan xyz
│ └── columns: x:1!null y:2 z:3 crdb_internal_mvcc_timestamp:4 tableoid:5
└── projections
└── x:1 + y:2 [as=r:6]
build
SELECT DISTINCT 3 r FROM xyz
----
distinct-on
├── columns: r:6!null
├── grouping columns: r:6!null
└── project
├── columns: r:6!null
├── scan xyz
│ └── columns: x:1!null y:2 z:3 crdb_internal_mvcc_timestamp:4 tableoid:5
└── projections
└── 3 [as=r:6]
build
SELECT DISTINCT 3 r
----
distinct-on
├── columns: r:1!null
├── grouping columns: r:1!null
└── project
├── columns: r:1!null
├── values
│ └── ()
└── projections
└── 3 [as=r:1]
build
SELECT DISTINCT max(z), x+y AS r, 3 AS s FROM xyz GROUP BY x, y HAVING y > 4
----
distinct-on
├── columns: max:6 r:7!null s:8!null
├── grouping columns: max:6 r:7!null s:8!null
└── project
├── columns: r:7!null s:8!null max:6
├── select
│ ├── columns: x:1!null y:2!null max:6
│ ├── group-by (hash)
│ │ ├── columns: x:1!null y:2 max:6
│ │ ├── grouping columns: x:1!null y:2
│ │ ├── project
│ │ │ ├── columns: x:1!null y:2 z:3
│ │ │ └── scan xyz
│ │ │ └── columns: x:1!null y:2 z:3 crdb_internal_mvcc_timestamp:4 tableoid:5
│ │ └── aggregations
│ │ └── max [as=max:6]
│ │ └── z:3
│ └── filters
│ └── y:2 > 4
└── projections
├── x:1 + y:2 [as=r:7]
└── 3 [as=s:8]
exec-ddl
CREATE TABLE abcd (
a INT,
b INT,
c INT,
d INT NOT NULL,
PRIMARY KEY (a, b, c),
UNIQUE INDEX (d, b)
)
----
build
SELECT DISTINCT 1 AS x, d, b FROM abcd ORDER BY d, b
----
sort
├── columns: x:7!null d:4!null b:2!null
├── ordering: +4,+2
└── distinct-on
├── columns: b:2!null d:4!null x:7!null
├── grouping columns: b:2!null d:4!null x:7!null
└── project
├── columns: x:7!null b:2!null d:4!null
├── scan abcd
│ └── columns: a:1!null b:2!null c:3!null d:4!null crdb_internal_mvcc_timestamp:5 tableoid:6
└── projections
└── 1 [as=x:7]
# Regression test for #168317. Under null_ordered_last, a SELECT DISTINCT whose
# ORDER BY references select-list columns synthesizes (col IS NULL) ordering
# columns (to implement non-default NULLS ordering). These synthesized columns
# must not trigger a spurious "ORDER BY expressions must appear in select list"
# error, mirroring the DISTINCT ON handling (case 3 in buildDistinctOn).
build set=null_ordered_last=true
SELECT DISTINCT y, z FROM xyz ORDER BY y, z
----
distinct-on
├── columns: y:2 z:3 [hidden: nulls_ordering_y:6!null nulls_ordering_z:7!null]
├── grouping columns: y:2 z:3 nulls_ordering_y:6!null nulls_ordering_z:7!null
├── ordering: +6,+2,+7,+3
└── sort
├── columns: y:2 z:3 nulls_ordering_y:6!null nulls_ordering_z:7!null
├── ordering: +6,+2,+7,+3
└── project
├── columns: nulls_ordering_y:6!null nulls_ordering_z:7!null y:2 z:3
├── project
│ ├── columns: y:2 z:3
│ └── scan xyz
│ └── columns: x:1!null y:2 z:3 crdb_internal_mvcc_timestamp:4 tableoid:5
└── projections
├── y:2 IS NULL [as=nulls_ordering_y:6]
└── z:3 IS NULL [as=nulls_ordering_z:7]
# A genuinely invalid query (ORDER BY a non-projected column) must still error,
# even though null_ordered_last synthesizes an IS NULL column for it.
build set=null_ordered_last=true
SELECT DISTINCT y FROM xyz ORDER BY z
----
error (42P10): for SELECT DISTINCT, ORDER BY expressions must appear in select list