forked from apache/datafusion
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathaggregates.rs
More file actions
323 lines (295 loc) · 15.7 KB
/
Copy pathaggregates.rs
File metadata and controls
323 lines (295 loc) · 15.7 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
// Licensed to the Apache Software Foundation (ASF) under one
// or more contributor license agreements. See the NOTICE file
// distributed with this work for additional information
// regarding copyright ownership. The ASF licenses this file
// to you under the Apache License, Version 2.0 (the
// "License"); you may not use this file except in compliance
// with the License. You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing,
// software distributed under the License is distributed on an
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
// KIND, either express or implied. See the License for the
// specific language governing permissions and limitations
// under the License.
use super::*;
use datafusion::scalar::ScalarValue;
#[tokio::test]
async fn csv_query_array_agg_distinct() -> Result<()> {
let ctx = SessionContext::new();
register_aggregate_csv(&ctx).await?;
let sql = "SELECT array_agg(distinct c2) FROM aggregate_test_100";
let actual = execute_to_batches(&ctx, sql).await;
// The results for this query should be something like the following:
// +------------------------------------------+
// | ARRAY_AGG(DISTINCT aggregate_test_100.c2) |
// +------------------------------------------+
// | [4, 2, 3, 5, 1] |
// +------------------------------------------+
// Since ARRAY_AGG(DISTINCT) ordering is nondeterministic, check the schema and contents.
assert_eq!(
*actual[0].schema(),
Schema::new(vec![Field::new_list(
"array_agg(DISTINCT aggregate_test_100.c2)",
Field::new_list_field(DataType::UInt32, true),
true
),])
);
// We should have 1 row containing a list
let column = actual[0].column(0);
assert_eq!(column.len(), 1);
let scalar_vec = ScalarValue::convert_array_to_scalar_vec(&column)?;
let mut scalars = scalar_vec[0].clone();
// workaround lack of Ord of ScalarValue
let cmp = |a: &ScalarValue, b: &ScalarValue| {
a.partial_cmp(b).expect("Can compare ScalarValues")
};
scalars.sort_by(cmp);
assert_eq!(
scalars,
vec![
ScalarValue::UInt32(Some(1)),
ScalarValue::UInt32(Some(2)),
ScalarValue::UInt32(Some(3)),
ScalarValue::UInt32(Some(4)),
ScalarValue::UInt32(Some(5))
]
);
Ok(())
}
#[tokio::test]
async fn count_partitioned() -> Result<()> {
let results =
execute_with_partition("SELECT count(c1), count(c2) FROM test", 4).await?;
assert_eq!(results.len(), 1);
let expected = [
"+----------------+----------------+",
"| count(test.c1) | count(test.c2) |",
"+----------------+----------------+",
"| 40 | 40 |",
"+----------------+----------------+",
];
assert_batches_sorted_eq!(expected, &results);
Ok(())
}
#[tokio::test]
async fn count_aggregated() -> Result<()> {
let results =
execute_with_partition("SELECT c1, count(c2) FROM test GROUP BY c1", 4).await?;
let expected = [
"+----+----------------+",
"| c1 | count(test.c2) |",
"+----+----------------+",
"| 0 | 10 |",
"| 1 | 10 |",
"| 2 | 10 |",
"| 3 | 10 |",
"+----+----------------+",
];
assert_batches_sorted_eq!(expected, &results);
Ok(())
}
#[tokio::test]
async fn count_aggregated_cube() -> Result<()> {
let results = execute_with_partition(
"SELECT c1, c2, count(c3) FROM test GROUP BY CUBE (c1, c2) ORDER BY c1, c2",
4,
)
.await?;
let expected = vec![
"+----+----+----------------+",
"| c1 | c2 | count(test.c3) |",
"+----+----+----------------+",
"| | | 40 |",
"| | 1 | 4 |",
"| | 10 | 4 |",
"| | 2 | 4 |",
"| | 3 | 4 |",
"| | 4 | 4 |",
"| | 5 | 4 |",
"| | 6 | 4 |",
"| | 7 | 4 |",
"| | 8 | 4 |",
"| | 9 | 4 |",
"| 0 | | 10 |",
"| 0 | 1 | 1 |",
"| 0 | 10 | 1 |",
"| 0 | 2 | 1 |",
"| 0 | 3 | 1 |",
"| 0 | 4 | 1 |",
"| 0 | 5 | 1 |",
"| 0 | 6 | 1 |",
"| 0 | 7 | 1 |",
"| 0 | 8 | 1 |",
"| 0 | 9 | 1 |",
"| 1 | | 10 |",
"| 1 | 1 | 1 |",
"| 1 | 10 | 1 |",
"| 1 | 2 | 1 |",
"| 1 | 3 | 1 |",
"| 1 | 4 | 1 |",
"| 1 | 5 | 1 |",
"| 1 | 6 | 1 |",
"| 1 | 7 | 1 |",
"| 1 | 8 | 1 |",
"| 1 | 9 | 1 |",
"| 2 | | 10 |",
"| 2 | 1 | 1 |",
"| 2 | 10 | 1 |",
"| 2 | 2 | 1 |",
"| 2 | 3 | 1 |",
"| 2 | 4 | 1 |",
"| 2 | 5 | 1 |",
"| 2 | 6 | 1 |",
"| 2 | 7 | 1 |",
"| 2 | 8 | 1 |",
"| 2 | 9 | 1 |",
"| 3 | | 10 |",
"| 3 | 1 | 1 |",
"| 3 | 10 | 1 |",
"| 3 | 2 | 1 |",
"| 3 | 3 | 1 |",
"| 3 | 4 | 1 |",
"| 3 | 5 | 1 |",
"| 3 | 6 | 1 |",
"| 3 | 7 | 1 |",
"| 3 | 8 | 1 |",
"| 3 | 9 | 1 |",
"+----+----+----------------+",
];
assert_batches_sorted_eq!(expected, &results);
Ok(())
}
async fn run_count_distinct_integers_aggregated_scenario(
partitions: Vec<Vec<(&str, u64)>>,
) -> Result<Vec<RecordBatch>> {
let tmp_dir = TempDir::new()?;
let ctx = SessionContext::new();
let schema = Arc::new(Schema::new(vec![
Field::new("c_group", DataType::Utf8, false),
Field::new("c_int8", DataType::Int8, false),
Field::new("c_int16", DataType::Int16, false),
Field::new("c_int32", DataType::Int32, false),
Field::new("c_int64", DataType::Int64, false),
Field::new("c_uint8", DataType::UInt8, false),
Field::new("c_uint16", DataType::UInt16, false),
Field::new("c_uint32", DataType::UInt32, false),
Field::new("c_uint64", DataType::UInt64, false),
]));
for (i, partition) in partitions.iter().enumerate() {
let filename = format!("partition-{i}.csv");
let file_path = tmp_dir.path().join(filename);
let mut file = File::create(file_path)?;
for row in partition {
let row_str = format!(
"{},{}\n",
row.0,
// Populate values for each of the integer fields in the
// schema.
(0..8)
.map(|_| { row.1.to_string() })
.collect::<Vec<_>>()
.join(","),
);
file.write_all(row_str.as_bytes())?;
}
}
ctx.register_csv(
"test",
tmp_dir.path().to_str().unwrap(),
CsvReadOptions::new().schema(&schema).has_header(false),
)
.await?;
let results = plan_and_collect(
&ctx,
"
SELECT
c_group,
count(c_uint64),
count(DISTINCT c_int8),
count(DISTINCT c_int16),
count(DISTINCT c_int32),
count(DISTINCT c_int64),
count(DISTINCT c_uint8),
count(DISTINCT c_uint16),
count(DISTINCT c_uint32),
count(DISTINCT c_uint64)
FROM test
GROUP BY c_group
",
)
.await?;
Ok(results)
}
#[tokio::test]
async fn count_distinct_integers_aggregated_single_partition() -> Result<()> {
let partitions = vec![
// The first member of each tuple will be the value for the
// `c_group` column, and the second member will be the value for
// each of the int/uint fields.
vec![
("a", 1),
("a", 1),
("a", 2),
("b", 9),
("c", 9),
("c", 10),
("c", 9),
],
];
let results = run_count_distinct_integers_aggregated_scenario(partitions).await?;
let expected = ["+---------+----------------------+-----------------------------+------------------------------+------------------------------+------------------------------+------------------------------+-------------------------------+-------------------------------+-------------------------------+",
"| c_group | count(test.c_uint64) | count(DISTINCT test.c_int8) | count(DISTINCT test.c_int16) | count(DISTINCT test.c_int32) | count(DISTINCT test.c_int64) | count(DISTINCT test.c_uint8) | count(DISTINCT test.c_uint16) | count(DISTINCT test.c_uint32) | count(DISTINCT test.c_uint64) |",
"+---------+----------------------+-----------------------------+------------------------------+------------------------------+------------------------------+------------------------------+-------------------------------+-------------------------------+-------------------------------+",
"| a | 3 | 2 | 2 | 2 | 2 | 2 | 2 | 2 | 2 |",
"| b | 1 | 1 | 1 | 1 | 1 | 1 | 1 | 1 | 1 |",
"| c | 3 | 2 | 2 | 2 | 2 | 2 | 2 | 2 | 2 |",
"+---------+----------------------+-----------------------------+------------------------------+------------------------------+------------------------------+------------------------------+-------------------------------+-------------------------------+-------------------------------+"];
assert_batches_sorted_eq!(expected, &results);
Ok(())
}
#[tokio::test]
async fn count_distinct_integers_aggregated_multiple_partitions() -> Result<()> {
let partitions = vec![
// The first member of each tuple will be the value for the
// `c_group` column, and the second member will be the value for
// each of the int/uint fields.
vec![("a", 1), ("a", 1), ("a", 2), ("b", 9), ("c", 9)],
vec![("a", 1), ("a", 3), ("b", 8), ("b", 9), ("b", 10), ("b", 11)],
];
let results = run_count_distinct_integers_aggregated_scenario(partitions).await?;
let expected = ["+---------+----------------------+-----------------------------+------------------------------+------------------------------+------------------------------+------------------------------+-------------------------------+-------------------------------+-------------------------------+",
"| c_group | count(test.c_uint64) | count(DISTINCT test.c_int8) | count(DISTINCT test.c_int16) | count(DISTINCT test.c_int32) | count(DISTINCT test.c_int64) | count(DISTINCT test.c_uint8) | count(DISTINCT test.c_uint16) | count(DISTINCT test.c_uint32) | count(DISTINCT test.c_uint64) |",
"+---------+----------------------+-----------------------------+------------------------------+------------------------------+------------------------------+------------------------------+-------------------------------+-------------------------------+-------------------------------+",
"| a | 5 | 3 | 3 | 3 | 3 | 3 | 3 | 3 | 3 |",
"| b | 5 | 4 | 4 | 4 | 4 | 4 | 4 | 4 | 4 |",
"| c | 1 | 1 | 1 | 1 | 1 | 1 | 1 | 1 | 1 |",
"+---------+----------------------+-----------------------------+------------------------------+------------------------------+------------------------------+------------------------------+-------------------------------+-------------------------------+-------------------------------+"];
assert_batches_sorted_eq!(expected, &results);
Ok(())
}
#[tokio::test]
async fn test_accumulator_row_accumulator() -> Result<()> {
let config = SessionConfig::new();
let ctx = SessionContext::new_with_config(config);
register_aggregate_csv(&ctx).await?;
let sql = "SELECT c1, c2, MIN(c13) as min1, MIN(c9) as min2, MAX(c13) as max1, MAX(c9) as max2, AVG(c9) as avg1, MIN(c13) as min3, count(C9) as cnt1, 0.5*SUM(c9-c8) as sum1
FROM aggregate_test_100
GROUP BY c1, c2
ORDER BY c1, c2
LIMIT 5";
let actual = execute_to_batches(&ctx, sql).await;
let expected = ["+----+----+--------------------------------+-----------+--------------------------------+------------+--------------------+--------------------------------+------+--------------+",
"| c1 | c2 | min1 | min2 | max1 | max2 | avg1 | min3 | cnt1 | sum1 |",
"+----+----+--------------------------------+-----------+--------------------------------+------------+--------------------+--------------------------------+------+--------------+",
"| a | 1 | 0keZ5G8BffGwgF2RwQD59TFzMStxCB | 774637006 | waIGbOGl1PM6gnzZ4uuZt4E2yDWRHs | 4015442341 | 2437927011.0 | 0keZ5G8BffGwgF2RwQD59TFzMStxCB | 5 | 6094771121.5 |",
"| a | 2 | b3b9esRhTzFEawbs6XhpKnD9ojutHB | 145294611 | ukyD7b0Efj7tNlFSRmzZ0IqkEzg2a8 | 3717551163 | 2267588664.0 | b3b9esRhTzFEawbs6XhpKnD9ojutHB | 3 | 3401364777.0 |",
"| a | 3 | Amn2K87Db5Es3dFQO9cw9cvpAM6h35 | 431948861 | oLZ21P2JEDooxV1pU31cIxQHEeeoLu | 3998790955 | 2225685115.1666665 | Amn2K87Db5Es3dFQO9cw9cvpAM6h35 | 6 | 6676994872.5 |",
"| a | 4 | KJFcmTVjdkCMv94wYCtfHMFhzyRsmH | 466439833 | ydkwycaISlYSlEq3TlkS2m15I2pcp8 | 2502326480 | 1655431654.0 | KJFcmTVjdkCMv94wYCtfHMFhzyRsmH | 4 | 3310812222.5 |",
"| a | 5 | MeSTAXq8gVxVjbEjgkvU9YLte0X9uE | 141047417 | QJYm7YRA3YetcBHI5wkMZeLXVmfuNy | 2496054700 | 1216992989.6666667 | MeSTAXq8gVxVjbEjgkvU9YLte0X9uE | 3 | 1825431770.0 |",
"+----+----+--------------------------------+-----------+--------------------------------+------------+--------------------+--------------------------------+------+--------------+"];
assert_batches_eq!(expected, &actual);
Ok(())
}