forked from apache/datafusion
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsql_planner.rs
121 lines (107 loc) · 3.85 KB
/
sql_planner.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
// 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 arrow::datatypes::{DataType, Field, Schema};
use datafusion::datasource::MemTable;
use datafusion::execution::context::SessionContext;
use std::sync::Arc;
use tokio::runtime::Runtime;
/// Create a logical plan from the specified sql
fn logical_plan(ctx: &SessionContext, sql: &str) {
let rt = Runtime::new().unwrap();
criterion::black_box(rt.block_on(ctx.sql(sql)).unwrap());
}
/// Create a physical ExecutionPlan (by way of logical plan)
fn physical_plan(ctx: &SessionContext, sql: &str) {
let rt = Runtime::new().unwrap();
criterion::black_box(rt.block_on(async {
ctx.sql(sql)
.await
.unwrap()
.create_physical_plan()
.await
.unwrap()
}));
}
/// Create schema with the specified number of columns
pub fn create_schema(column_prefix: &str, num_columns: usize) -> Schema {
let fields = (0..num_columns)
.map(|i| Field::new(format!("{column_prefix}{i}"), DataType::Int32, true))
.collect();
Schema::new(fields)
}
pub fn create_table_provider(column_prefix: &str, num_columns: usize) -> Arc<MemTable> {
let schema = Arc::new(create_schema(column_prefix, num_columns));
MemTable::try_new(schema, vec![]).map(Arc::new).unwrap()
}
fn create_context() -> SessionContext {
let ctx = SessionContext::new();
ctx.register_table("t1", create_table_provider("a", 200))
.unwrap();
ctx.register_table("t2", create_table_provider("b", 200))
.unwrap();
ctx.register_table("t700", create_table_provider("c", 700))
.unwrap();
ctx
}
fn criterion_benchmark(c: &mut Criterion) {
let ctx = create_context();
// Test simplest
// https://github.com/apache/arrow-datafusion/issues/5157
c.bench_function("logical_select_one_from_700", |b| {
b.iter(|| logical_plan(&ctx, "SELECT c1 FROM t700"))
});
// Test simplest
// https://github.com/apache/arrow-datafusion/issues/5157
c.bench_function("physical_select_one_from_700", |b| {
b.iter(|| physical_plan(&ctx, "SELECT c1 FROM t700"))
});
c.bench_function("logical_trivial_join_low_numbered_columns", |b| {
b.iter(|| {
logical_plan(
&ctx,
"SELECT t1.a2, t2.b2 \
FROM t1, t2 WHERE a1 = b1",
)
})
});
c.bench_function("logical_trivial_join_high_numbered_columns", |b| {
b.iter(|| {
logical_plan(
&ctx,
"SELECT t1.a99, t2.b99 \
FROM t1, t2 WHERE a199 = b199",
)
})
});
c.bench_function("logical_aggregate_with_join", |b| {
b.iter(|| {
logical_plan(
&ctx,
"SELECT t1.a99, MIN(t2.b1), MAX(t2.b199), AVG(t2.b123), COUNT(t2.b73) \
FROM t1 JOIN t2 ON t1.a199 = t2.b199 GROUP BY t1.a99",
)
})
});
}
criterion_group!(benches, criterion_benchmark);
criterion_main!(benches);