|
| 1 | +// Licensed to the Apache Software Foundation (ASF) under one |
| 2 | +// or more contributor license agreements. See the NOTICE file |
| 3 | +// distributed with this work for additional information |
| 4 | +// regarding copyright ownership. The ASF licenses this file |
| 5 | +// to you under the Apache License, Version 2.0 (the |
| 6 | +// "License"); you may not use this file except in compliance |
| 7 | +// with the License. You may obtain a copy of the License at |
| 8 | +// |
| 9 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | +// |
| 11 | +// Unless required by applicable law or agreed to in writing, |
| 12 | +// software distributed under the License is distributed on an |
| 13 | +// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 14 | +// KIND, either express or implied. See the License for the |
| 15 | +// specific language governing permissions and limitations |
| 16 | +// under the License. |
| 17 | + |
| 18 | +//! Benchmark for the TopKRepartition optimizer rule. |
| 19 | +//! |
| 20 | +//! Measures the benefit of pushing TopK (Sort with fetch) below hash |
| 21 | +//! repartition when running partitioned window functions with LIMIT. |
| 22 | +
|
| 23 | +mod data_utils; |
| 24 | + |
| 25 | +use criterion::{BenchmarkId, Criterion, criterion_group, criterion_main}; |
| 26 | +use data_utils::create_table_provider; |
| 27 | +use datafusion::prelude::{SessionConfig, SessionContext}; |
| 28 | +use parking_lot::Mutex; |
| 29 | +use std::hint::black_box; |
| 30 | +use std::sync::Arc; |
| 31 | +use tokio::runtime::Runtime; |
| 32 | + |
| 33 | +#[expect(clippy::needless_pass_by_value)] |
| 34 | +fn query(ctx: Arc<Mutex<SessionContext>>, rt: &Runtime, sql: &str) { |
| 35 | + let df = rt.block_on(ctx.lock().sql(sql)).unwrap(); |
| 36 | + black_box(rt.block_on(df.collect()).unwrap()); |
| 37 | +} |
| 38 | + |
| 39 | +fn create_context( |
| 40 | + partitions_len: usize, |
| 41 | + target_partitions: usize, |
| 42 | + enable_topk_repartition: bool, |
| 43 | +) -> Arc<Mutex<SessionContext>> { |
| 44 | + let array_len = 1024 * 1024; |
| 45 | + let batch_size = 8 * 1024; |
| 46 | + let mut config = SessionConfig::new().with_target_partitions(target_partitions); |
| 47 | + config.options_mut().optimizer.enable_topk_repartition = enable_topk_repartition; |
| 48 | + let ctx = SessionContext::new_with_config(config); |
| 49 | + let rt = Runtime::new().unwrap(); |
| 50 | + rt.block_on(async { |
| 51 | + let provider = |
| 52 | + create_table_provider(partitions_len, array_len, batch_size).unwrap(); |
| 53 | + ctx.register_table("t", provider).unwrap(); |
| 54 | + }); |
| 55 | + Arc::new(Mutex::new(ctx)) |
| 56 | +} |
| 57 | + |
| 58 | +fn criterion_benchmark(c: &mut Criterion) { |
| 59 | + let rt = Runtime::new().unwrap(); |
| 60 | + |
| 61 | + let limits = [10, 1_000, 10_000, 100_000]; |
| 62 | + let scans = 16; |
| 63 | + let target_partitions = 4; |
| 64 | + |
| 65 | + let group = format!("topk_repartition_{scans}_to_{target_partitions}"); |
| 66 | + let mut group = c.benchmark_group(group); |
| 67 | + for limit in limits { |
| 68 | + let sql = format!( |
| 69 | + "SELECT \ |
| 70 | + SUM(f64) OVER (PARTITION BY u64_narrow ORDER BY u64_wide ROWS UNBOUNDED PRECEDING) \ |
| 71 | + FROM t \ |
| 72 | + ORDER BY u64_narrow, u64_wide \ |
| 73 | + LIMIT {limit}" |
| 74 | + ); |
| 75 | + |
| 76 | + let ctx_disabled = create_context(scans, target_partitions, false); |
| 77 | + group.bench_function(BenchmarkId::new("disabled", limit), |b| { |
| 78 | + b.iter(|| query(ctx_disabled.clone(), &rt, &sql)) |
| 79 | + }); |
| 80 | + |
| 81 | + let ctx_enabled = create_context(scans, target_partitions, true); |
| 82 | + group.bench_function(BenchmarkId::new("enabled", limit), |b| { |
| 83 | + b.iter(|| query(ctx_enabled.clone(), &rt, &sql)) |
| 84 | + }); |
| 85 | + } |
| 86 | + group.finish(); |
| 87 | +} |
| 88 | + |
| 89 | +criterion_group!(benches, criterion_benchmark); |
| 90 | +criterion_main!(benches); |
0 commit comments