forked from apache/datafusion
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathaggregate_query_sql.rs
169 lines (152 loc) · 4.95 KB
/
aggregate_query_sql.rs
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
// 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.
#[macro_use]
extern crate criterion;
extern crate arrow;
extern crate datafusion;
mod data_utils;
use crate::criterion::Criterion;
use data_utils::create_table_provider;
use datafusion::error::Result;
use datafusion::execution::context::SessionContext;
use parking_lot::Mutex;
use std::sync::Arc;
use tokio::runtime::Runtime;
fn query(ctx: Arc<Mutex<SessionContext>>, sql: &str) {
let rt = Runtime::new().unwrap();
let df = rt.block_on(ctx.lock().sql(sql)).unwrap();
criterion::black_box(rt.block_on(df.collect()).unwrap());
}
fn create_context(
partitions_len: usize,
array_len: usize,
batch_size: usize,
) -> Result<Arc<Mutex<SessionContext>>> {
let ctx = SessionContext::new();
let provider = create_table_provider(partitions_len, array_len, batch_size)?;
ctx.register_table("t", provider)?;
Ok(Arc::new(Mutex::new(ctx)))
}
fn criterion_benchmark(c: &mut Criterion) {
let partitions_len = 8;
let array_len = 32768 * 2; // 2^16
let batch_size = 2048; // 2^11
let ctx = create_context(partitions_len, array_len, batch_size).unwrap();
c.bench_function("aggregate_query_no_group_by 15 12", |b| {
b.iter(|| {
query(
ctx.clone(),
"SELECT MIN(f64), AVG(f64), COUNT(f64) \
FROM t",
)
})
});
c.bench_function("aggregate_query_no_group_by_min_max_f64", |b| {
b.iter(|| {
query(
ctx.clone(),
"SELECT MIN(f64), MAX(f64) \
FROM t",
)
})
});
c.bench_function("aggregate_query_no_group_by_count_distinct_wide", |b| {
b.iter(|| {
query(
ctx.clone(),
"SELECT COUNT(DISTINCT u64_wide) \
FROM t",
)
})
});
c.bench_function("aggregate_query_no_group_by_count_distinct_narrow", |b| {
b.iter(|| {
query(
ctx.clone(),
"SELECT COUNT(DISTINCT u64_narrow) \
FROM t",
)
})
});
c.bench_function("aggregate_query_group_by", |b| {
b.iter(|| {
query(
ctx.clone(),
"SELECT utf8, MIN(f64), AVG(f64), COUNT(f64) \
FROM t GROUP BY utf8",
)
})
});
c.bench_function("aggregate_query_group_by_with_filter", |b| {
b.iter(|| {
query(
ctx.clone(),
"SELECT utf8, MIN(f64), AVG(f64), COUNT(f64) \
FROM t \
WHERE f32 > 10 AND f32 < 20 GROUP BY utf8",
)
})
});
c.bench_function("aggregate_query_group_by_u64 15 12", |b| {
b.iter(|| {
query(
ctx.clone(),
"SELECT u64_narrow, MIN(f64), AVG(f64), COUNT(f64) \
FROM t GROUP BY u64_narrow",
)
})
});
c.bench_function("aggregate_query_group_by_with_filter_u64 15 12", |b| {
b.iter(|| {
query(
ctx.clone(),
"SELECT u64_narrow, MIN(f64), AVG(f64), COUNT(f64) \
FROM t \
WHERE f32 > 10 AND f32 < 20 GROUP BY u64_narrow",
)
})
});
c.bench_function("aggregate_query_group_by_u64_multiple_keys", |b| {
b.iter(|| {
query(
ctx.clone(),
"SELECT u64_wide, utf8, MIN(f64), AVG(f64), COUNT(f64) \
FROM t GROUP BY u64_wide, utf8",
)
})
});
c.bench_function("aggregate_query_approx_percentile_cont_on_u64", |b| {
b.iter(|| {
query(
ctx.clone(),
"SELECT utf8, approx_percentile_cont(u64_wide, 0.5, 2500) \
FROM t GROUP BY utf8",
)
})
});
c.bench_function("aggregate_query_approx_percentile_cont_on_f32", |b| {
b.iter(|| {
query(
ctx.clone(),
"SELECT utf8, approx_percentile_cont(f32, 0.5, 2500) \
FROM t GROUP BY utf8",
)
})
});
}
criterion_group!(benches, criterion_benchmark);
criterion_main!(benches);