|
// 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. |
|
|
|
//! Phase 0 measurements for apache/datafusion#25155. |
|
//! |
|
//! Quantifies the physical-plan serialization cost that the "serialize at the |
|
//! FFI optimizer boundary is too slow" objection rests on. Not a CI test -- |
|
//! run explicitly: |
|
//! |
|
//! ```text |
|
//! cargo test --release -p datafusion-proto --test phase0_ffi_bench -- --nocapture --test-threads=1 --ignored |
|
//! ``` |
|
|
|
use std::sync::Arc; |
|
use std::time::Duration; |
|
|
|
use arrow::array::{Float64Array, Int64Array, RecordBatch, StringArray}; |
|
use arrow::datatypes::{DataType, Field, Schema, SchemaRef}; |
|
use datafusion::datasource::listing::PartitionedFile; |
|
use datafusion::datasource::object_store::ObjectStoreUrl; |
|
use datafusion::datasource::physical_plan::{ |
|
FileGroup, FileScanConfigBuilder, ParquetSource, |
|
}; |
|
use datafusion::datasource::source::DataSourceExec; |
|
use datafusion::physical_optimizer::optimizer::PhysicalOptimizer; |
|
use datafusion::physical_plan::{ExecutionPlan, collect}; |
|
use datafusion::prelude::{ParquetReadOptions, SessionContext}; |
|
use datafusion_common::Result; |
|
use datafusion_common::instant::Instant; |
|
use datafusion_common::stats::Precision; |
|
use datafusion_common::tree_node::{Transformed, TreeNode}; |
|
use datafusion_common::{ColumnStatistics, ScalarValue, Statistics}; |
|
use datafusion_proto::bytes::{physical_plan_from_bytes, physical_plan_to_bytes}; |
|
|
|
// --------------------------------------------------------------------------- |
|
// timing helpers |
|
// --------------------------------------------------------------------------- |
|
|
|
/// Run `f` `reps` times after one warm-up, return the median duration. |
|
fn median_of<T>(reps: usize, mut f: impl FnMut() -> T) -> Duration { |
|
let _ = f(); |
|
let mut samples: Vec<Duration> = (0..reps) |
|
.map(|_| { |
|
let t = Instant::now(); |
|
let out = f(); |
|
let d = t.elapsed(); |
|
drop(out); |
|
d |
|
}) |
|
.collect(); |
|
samples.sort(); |
|
samples[samples.len() / 2] |
|
} |
|
|
|
fn ms(d: Duration) -> f64 { |
|
d.as_secs_f64() * 1e3 |
|
} |
|
|
|
/// Reps scaled down as the workload grows, so 10k-file cases stay quick. |
|
fn reps_for(n_files: usize) -> usize { |
|
match n_files { |
|
0..=10 => 50, |
|
11..=100 => 25, |
|
101..=1000 => 10, |
|
_ => 5, |
|
} |
|
} |
|
|
|
// --------------------------------------------------------------------------- |
|
// synthetic scan construction |
|
// --------------------------------------------------------------------------- |
|
|
|
/// A 20-column schema, roughly the width of a real analytics table. |
|
fn wide_schema() -> SchemaRef { |
|
let mut fields = Vec::with_capacity(20); |
|
for i in 0..20 { |
|
let (name, dt) = match i % 4 { |
|
0 => (format!("i{i}"), DataType::Int64), |
|
1 => (format!("s{i}"), DataType::Utf8), |
|
2 => (format!("f{i}"), DataType::Float64), |
|
_ => (format!("b{i}"), DataType::Boolean), |
|
}; |
|
fields.push(Field::new(name, dt, true)); |
|
} |
|
Arc::new(Schema::new(fields)) |
|
} |
|
|
|
/// Per-file statistics covering every column of `schema`, as a real listing |
|
/// table with collected statistics would carry. |
|
fn per_file_statistics(schema: &Schema, seed: i64) -> Arc<Statistics> { |
|
let column_statistics = schema |
|
.fields() |
|
.iter() |
|
.enumerate() |
|
.map(|(i, f)| { |
|
let (min, max) = match f.data_type() { |
|
DataType::Int64 => ( |
|
ScalarValue::Int64(Some(seed + i as i64)), |
|
ScalarValue::Int64(Some(seed + i as i64 + 1_000)), |
|
), |
|
DataType::Utf8 => ( |
|
ScalarValue::Utf8(Some(format!("min_value_{seed}_{i}"))), |
|
ScalarValue::Utf8(Some(format!("max_value_{seed}_{i}"))), |
|
), |
|
DataType::Float64 => ( |
|
ScalarValue::Float64(Some(seed as f64 + i as f64)), |
|
ScalarValue::Float64(Some(seed as f64 + i as f64 + 1000.0)), |
|
), |
|
_ => ( |
|
ScalarValue::Boolean(Some(false)), |
|
ScalarValue::Boolean(Some(true)), |
|
), |
|
}; |
|
ColumnStatistics { |
|
null_count: Precision::Exact(i), |
|
max_value: Precision::Exact(max), |
|
min_value: Precision::Exact(min), |
|
sum_value: Precision::Absent, |
|
distinct_count: Precision::Exact(1_000 + i), |
|
byte_size: Precision::Exact(4_096 * (i + 1)), |
|
} |
|
}) |
|
.collect(); |
|
|
|
Arc::new(Statistics { |
|
num_rows: Precision::Exact(1_000_000), |
|
total_byte_size: Precision::Exact(64 * 1024 * 1024), |
|
column_statistics, |
|
}) |
|
} |
|
|
|
/// Which optional per-file payloads to attach. Bench 2 varies exactly this. |
|
#[derive(Clone, Copy, PartialEq, Eq)] |
|
struct FileShape { |
|
stats: bool, |
|
arrow_schema: bool, |
|
} |
|
|
|
const FULL: FileShape = FileShape { |
|
stats: true, |
|
arrow_schema: true, |
|
}; |
|
const NO_STATS: FileShape = FileShape { |
|
stats: false, |
|
arrow_schema: true, |
|
}; |
|
const NO_SCHEMA: FileShape = FileShape { |
|
stats: true, |
|
arrow_schema: false, |
|
}; |
|
const MINIMAL: FileShape = FileShape { |
|
stats: false, |
|
arrow_schema: false, |
|
}; |
|
|
|
fn shape_name(s: FileShape) -> &'static str { |
|
match (s.stats, s.arrow_schema) { |
|
(true, true) => "full", |
|
(false, true) => "no per-file stats", |
|
(true, false) => "no per-file schema", |
|
(false, false) => "minimal (path+size only)", |
|
} |
|
} |
|
|
|
fn synthetic_files( |
|
schema: &SchemaRef, |
|
n: usize, |
|
shape: FileShape, |
|
) -> Vec<PartitionedFile> { |
|
(0..n) |
|
.map(|i| { |
|
// A realistic hive-partitioned object-store style path. |
|
let path = format!( |
|
"data/warehouse/events/year=2026/month=09/day={:02}/part-{:05}-c000-a1b2c3d4-e5f6-7890-abcd-ef0123456789.snappy.parquet", |
|
(i % 28) + 1, |
|
i |
|
); |
|
let mut f = PartitionedFile::new(path, 128 * 1024 * 1024); |
|
if shape.stats { |
|
f = f.with_statistics(per_file_statistics(schema, i as i64)); |
|
} |
|
if shape.arrow_schema { |
|
f = f.with_arrow_schema(Arc::clone(schema)); |
|
} |
|
f |
|
}) |
|
.collect() |
|
} |
|
|
|
/// A `DataSourceExec` over `n` synthetic parquet files in a single file group. |
|
fn synthetic_scan( |
|
schema: &SchemaRef, |
|
n: usize, |
|
shape: FileShape, |
|
) -> Arc<dyn ExecutionPlan> { |
|
let source = Arc::new(ParquetSource::new(Arc::clone(schema))); |
|
let config = FileScanConfigBuilder::new(ObjectStoreUrl::local_filesystem(), source) |
|
.with_file_group(FileGroup::new(synthetic_files(schema, n, shape))) |
|
.build(); |
|
DataSourceExec::from_data_source(config) |
|
} |
|
|
|
// --------------------------------------------------------------------------- |
|
// Bench 1 + 2: serialization cost vs file count, and per-field attribution |
|
// --------------------------------------------------------------------------- |
|
|
|
#[test] |
|
#[ignore = "measurement harness; run explicitly with --ignored"] |
|
fn bench1_and_2_serialization_vs_file_count() -> Result<()> { |
|
let ctx = SessionContext::new(); |
|
let task_ctx = ctx.task_ctx(); |
|
let schema = wide_schema(); |
|
let counts = [1usize, 10, 100, 1_000, 10_000]; |
|
let shapes = [FULL, NO_STATS, NO_SCHEMA, MINIMAL]; |
|
|
|
println!("\n=== Bench 1+2: DataSourceExec serialization vs file count ==="); |
|
println!( |
|
"{:>7} {:<24} {:>12} {:>12} {:>12} {:>12}", |
|
"files", "payload", "bytes", "encode ms", "decode ms", "round ms" |
|
); |
|
|
|
for shape in shapes { |
|
for n in counts { |
|
let plan = synthetic_scan(&schema, n, shape); |
|
let reps = reps_for(n); |
|
|
|
let bytes = physical_plan_to_bytes(Arc::clone(&plan))?; |
|
let len = bytes.len(); |
|
|
|
let enc = |
|
median_of(reps, || physical_plan_to_bytes(Arc::clone(&plan)).unwrap()); |
|
let dec = median_of(reps, || { |
|
physical_plan_from_bytes(&bytes, &task_ctx).unwrap() |
|
}); |
|
|
|
println!( |
|
"{:>7} {:<24} {:>12} {:>12.3} {:>12.3} {:>12.3}", |
|
n, |
|
shape_name(shape), |
|
len, |
|
ms(enc), |
|
ms(dec), |
|
ms(enc) + ms(dec), |
|
); |
|
} |
|
println!(); |
|
} |
|
|
|
Ok(()) |
|
} |
|
|
|
// --------------------------------------------------------------------------- |
|
// real data + real plans, shared by benches 3/4/5 |
|
// --------------------------------------------------------------------------- |
|
|
|
/// Write `n_files` parquet files of `rows` rows each under `dir/name`, and |
|
/// register the directory as a listing table. |
|
async fn make_parquet_table( |
|
ctx: &SessionContext, |
|
dir: &std::path::Path, |
|
name: &str, |
|
n_files: usize, |
|
rows: usize, |
|
) -> Result<()> { |
|
let table_dir = dir.join(name); |
|
std::fs::create_dir_all(&table_dir).unwrap(); |
|
|
|
let schema = Arc::new(Schema::new(vec![ |
|
Field::new("id", DataType::Int64, false), |
|
Field::new("key", DataType::Int64, false), |
|
Field::new("cat", DataType::Utf8, false), |
|
Field::new("val", DataType::Float64, false), |
|
])); |
|
|
|
for file_idx in 0..n_files { |
|
let base = (file_idx * rows) as i64; |
|
let batch = RecordBatch::try_new( |
|
Arc::clone(&schema), |
|
vec![ |
|
Arc::new(Int64Array::from_iter_values( |
|
(0..rows).map(|r| base + r as i64), |
|
)), |
|
Arc::new(Int64Array::from_iter_values( |
|
(0..rows).map(|r| (base + r as i64) % 1_000), |
|
)), |
|
Arc::new(StringArray::from_iter_values( |
|
(0..rows).map(|r| format!("cat_{}", r % 50)), |
|
)), |
|
Arc::new(Float64Array::from_iter_values( |
|
(0..rows).map(|r| (base + r as i64) as f64 * 1.5), |
|
)), |
|
], |
|
) |
|
.unwrap(); |
|
|
|
let path = table_dir.join(format!("part-{file_idx:05}.parquet")); |
|
let df = ctx.read_batch(batch)?; |
|
df.write_parquet(path.to_str().unwrap(), Default::default(), None) |
|
.await?; |
|
} |
|
|
|
ctx.register_parquet( |
|
name, |
|
table_dir.to_str().unwrap(), |
|
ParquetReadOptions::default(), |
|
) |
|
.await?; |
|
Ok(()) |
|
} |
|
|
|
/// Swap every `DataSourceExec` leaf for a synthetic scan over `n_files`, |
|
/// preserving the leaf's output schema so the parents stay valid. |
|
fn inject_file_count( |
|
plan: Arc<dyn ExecutionPlan>, |
|
n_files: usize, |
|
shape: FileShape, |
|
) -> Result<Arc<dyn ExecutionPlan>> { |
|
let transformed = plan.transform_up(|node| { |
|
if node.downcast_ref::<DataSourceExec>().is_some() { |
|
let schema = node.schema(); |
|
Ok(Transformed::yes(synthetic_scan(&schema, n_files, shape))) |
|
} else { |
|
Ok(Transformed::no(node)) |
|
} |
|
})?; |
|
Ok(transformed.data) |
|
} |
|
|
|
fn leaf_ptr(plan: &Arc<dyn ExecutionPlan>) -> Option<*const ()> { |
|
let mut cur = Arc::clone(plan); |
|
loop { |
|
if cur.downcast_ref::<DataSourceExec>().is_some() { |
|
return Some(Arc::as_ptr(&cur).cast::<()>()); |
|
} |
|
let children = cur.children(); |
|
let first = children.first().map(|c| Arc::clone(c))?; |
|
cur = first; |
|
} |
|
} |
|
|
|
const QUERIES: &[(&str, &str)] = &[ |
|
( |
|
"agg", |
|
"SELECT cat, count(*) AS n, sum(val) AS s FROM t1 \ |
|
WHERE key > 10 GROUP BY cat ORDER BY s DESC", |
|
), |
|
( |
|
"join_agg", |
|
"SELECT t1.cat, count(*) AS n, sum(t1.val + t2.val) AS s \ |
|
FROM t1 JOIN t2 ON t1.key = t2.key \ |
|
WHERE t1.key > 10 GROUP BY t1.cat ORDER BY s DESC", |
|
), |
|
]; |
|
|
|
// --------------------------------------------------------------------------- |
|
// Bench 3: the per-rule multiplier |
|
// --------------------------------------------------------------------------- |
|
|
|
#[tokio::test(flavor = "multi_thread")] |
|
#[ignore = "measurement harness; run explicitly with --ignored"] |
|
async fn bench3_per_rule_multiplier() -> Result<()> { |
|
let tmp = tempfile::tempdir().unwrap(); |
|
let ctx = SessionContext::new(); |
|
make_parquet_table(&ctx, tmp.path(), "t1", 2, 2_000).await?; |
|
make_parquet_table(&ctx, tmp.path(), "t2", 2, 2_000).await?; |
|
|
|
let optimizer = PhysicalOptimizer::default(); |
|
let n_rules = optimizer.rules.len(); |
|
let config = ctx.state().config_options().clone(); |
|
let task_ctx = ctx.task_ctx(); |
|
|
|
println!("\n=== Bench 3: optimizer chain cost, {n_rules} default rules ==="); |
|
println!( |
|
"{:<10} {:>7} {:>14} {:>14} {:>16} {:>10}", |
|
"query", "files", "rules only ms", "1x cross ms", "per-rule cross ms", "multiple" |
|
); |
|
|
|
for (qname, sql) in QUERIES { |
|
let base = ctx.sql(sql).await?.create_physical_plan().await?; |
|
|
|
for n_files in [1usize, 100, 1_000, 10_000] { |
|
let plan = inject_file_count(Arc::clone(&base), n_files, FULL)?; |
|
let reps = reps_for(n_files).min(10); |
|
|
|
// (a) rules only, no boundary crossing |
|
let rules_only = median_of(reps, || { |
|
let mut p = Arc::clone(&plan); |
|
for rule in &optimizer.rules { |
|
p = rule.optimize(p, &config).unwrap(); |
|
} |
|
p |
|
}); |
|
|
|
// (c) rules, then one crossing at the end |
|
let one_cross = median_of(reps, || { |
|
let mut p = Arc::clone(&plan); |
|
for rule in &optimizer.rules { |
|
p = rule.optimize(p, &config).unwrap(); |
|
} |
|
let bytes = physical_plan_to_bytes(p).unwrap(); |
|
physical_plan_from_bytes(&bytes, &task_ctx).unwrap() |
|
}); |
|
|
|
// (b) a crossing after every rule. Intermediate plans are not all |
|
// serializable with the default codec, so a failed crossing is |
|
// recorded and the plan continues un-serialized -- the reported |
|
// cost is therefore a *lower bound* on a true per-rule design. |
|
let mut unserializable: Vec<String> = vec![]; |
|
let mut crossings = 0usize; |
|
let per_rule = median_of(reps, || { |
|
let mut p = Arc::clone(&plan); |
|
unserializable.clear(); |
|
crossings = 0; |
|
for rule in &optimizer.rules { |
|
p = rule.optimize(p, &config).unwrap(); |
|
match physical_plan_to_bytes(Arc::clone(&p)) { |
|
Ok(bytes) => match physical_plan_from_bytes(&bytes, &task_ctx) { |
|
Ok(decoded) => { |
|
crossings += 1; |
|
p = decoded; |
|
} |
|
Err(_) => { |
|
unserializable.push(format!("{}(decode)", rule.name())) |
|
} |
|
}, |
|
Err(_) => unserializable.push(format!("{}(encode)", rule.name())), |
|
} |
|
} |
|
p |
|
}); |
|
|
|
println!( |
|
"{:<10} {:>7} {:>14.3} {:>14.3} {:>16.3} {:>9.1}x {}/{} crossings ok", |
|
qname, |
|
n_files, |
|
ms(rules_only), |
|
ms(one_cross), |
|
ms(per_rule), |
|
ms(per_rule) / ms(rules_only), |
|
crossings, |
|
n_rules, |
|
); |
|
if !unserializable.is_empty() { |
|
println!( |
|
"{:>19}not serializable after: {}", |
|
"", |
|
unserializable.join(", ") |
|
); |
|
} |
|
} |
|
} |
|
|
|
Ok(()) |
|
} |
|
|
|
// --------------------------------------------------------------------------- |
|
// Bench 4: serialization as a fraction of planning and of total query time |
|
// --------------------------------------------------------------------------- |
|
|
|
#[tokio::test(flavor = "multi_thread")] |
|
#[ignore = "measurement harness; run explicitly with --ignored"] |
|
async fn bench4_ratio_to_plan_and_execution() -> Result<()> { |
|
println!("\n=== Bench 4: serialization vs planning vs execution (real parquet) ==="); |
|
println!( |
|
"{:<10} {:>7} {:>10} {:>11} {:>12} {:>12} {:>10} {:>10}", |
|
"query", |
|
"files", |
|
"rows/file", |
|
"plan ms", |
|
"serde ms", |
|
"exec ms", |
|
"% of plan", |
|
"% of total" |
|
); |
|
|
|
for (n_files, rows) in [(1usize, 100_000usize), (10, 10_000), (100, 1_000)] { |
|
let tmp = tempfile::tempdir().unwrap(); |
|
let ctx = SessionContext::new(); |
|
make_parquet_table(&ctx, tmp.path(), "t1", n_files, rows).await?; |
|
make_parquet_table(&ctx, tmp.path(), "t2", n_files, rows).await?; |
|
|
|
for (qname, sql) in QUERIES { |
|
// planning (includes the full 21-rule chain) |
|
let t = Instant::now(); |
|
let plan = ctx.sql(sql).await?.create_physical_plan().await?; |
|
let _ = t.elapsed(); |
|
let plan_ms = { |
|
let mut samples = vec![]; |
|
for _ in 0..5 { |
|
let t = Instant::now(); |
|
let p = ctx.sql(sql).await?.create_physical_plan().await?; |
|
samples.push(t.elapsed()); |
|
drop(p); |
|
} |
|
samples.sort(); |
|
samples[samples.len() / 2] |
|
}; |
|
|
|
let task_ctx = ctx.task_ctx(); |
|
let serde_ms = match physical_plan_to_bytes(Arc::clone(&plan)) { |
|
Ok(bytes) => Some(median_of(10, || { |
|
let b = physical_plan_to_bytes(Arc::clone(&plan)).unwrap(); |
|
physical_plan_from_bytes(&b, &task_ctx).unwrap(); |
|
b.len() |
|
})) |
|
.map(|d| (d, bytes.len())), |
|
Err(e) => { |
|
println!("{qname:<10} {n_files:>7} serialization failed: {e}"); |
|
None |
|
} |
|
}; |
|
|
|
let exec = { |
|
let mut samples = vec![]; |
|
for _ in 0..3 { |
|
let p = ctx.sql(sql).await?.create_physical_plan().await?; |
|
let t = Instant::now(); |
|
let _ = collect(p, Arc::clone(&task_ctx)).await?; |
|
samples.push(t.elapsed()); |
|
} |
|
samples.sort(); |
|
samples[samples.len() / 2] |
|
}; |
|
|
|
match serde_ms { |
|
Some((sd, _len)) => println!( |
|
"{:<10} {:>7} {:>10} {:>11.3} {:>12.3} {:>12.3} {:>9.1}% {:>9.2}%", |
|
qname, |
|
n_files, |
|
rows, |
|
ms(plan_ms), |
|
ms(sd), |
|
ms(exec), |
|
100.0 * ms(sd) / ms(plan_ms), |
|
100.0 * ms(sd) / (ms(plan_ms) + ms(exec)), |
|
), |
|
None => println!( |
|
"{:<10} {:>7} {:>10} {:>11.3} {:>12} {:>12.3}", |
|
qname, |
|
n_files, |
|
rows, |
|
ms(plan_ms), |
|
"n/a", |
|
ms(exec) |
|
), |
|
} |
|
} |
|
} |
|
|
|
Ok(()) |
|
} |
|
|
|
// --------------------------------------------------------------------------- |
|
// Bench 5: does the leaf scan keep Arc identity across the rule chain? |
|
// --------------------------------------------------------------------------- |
|
|
|
#[tokio::test(flavor = "multi_thread")] |
|
#[ignore = "measurement harness; run explicitly with --ignored"] |
|
async fn bench5_leaf_arc_identity_across_rules() -> Result<()> { |
|
let tmp = tempfile::tempdir().unwrap(); |
|
let ctx = SessionContext::new(); |
|
make_parquet_table(&ctx, tmp.path(), "t1", 2, 2_000).await?; |
|
make_parquet_table(&ctx, tmp.path(), "t2", 2, 2_000).await?; |
|
|
|
let optimizer = PhysicalOptimizer::default(); |
|
let config = ctx.state().config_options().clone(); |
|
|
|
println!("\n=== Bench 5: leaf DataSourceExec Arc identity across rules ==="); |
|
|
|
for (qname, sql) in QUERIES { |
|
let base = ctx.sql(sql).await?.create_physical_plan().await?; |
|
let mut plan = inject_file_count(base, 100, FULL)?; |
|
|
|
let mut prev = leaf_ptr(&plan); |
|
let mut preserved = 0usize; |
|
let mut changed_by = vec![]; |
|
|
|
for rule in &optimizer.rules { |
|
plan = rule.optimize(plan, &config)?; |
|
let now = leaf_ptr(&plan); |
|
if now.is_some() && now == prev { |
|
preserved += 1; |
|
} else { |
|
changed_by.push(rule.name().to_string()); |
|
} |
|
prev = now; |
|
} |
|
|
|
println!( |
|
"{qname}: leaf Arc preserved by {preserved}/{} rules; rebuilt by: {}", |
|
optimizer.rules.len(), |
|
if changed_by.is_empty() { |
|
"(none)".to_string() |
|
} else { |
|
changed_by.join(", ") |
|
} |
|
); |
|
} |
|
|
|
Ok(()) |
|
} |
|
|
|
// --------------------------------------------------------------------------- |
|
// Bench 6: what a *real* listing table actually carries per file |
|
// --------------------------------------------------------------------------- |
|
|
|
#[tokio::test(flavor = "multi_thread")] |
|
#[ignore = "measurement harness; run explicitly with --ignored"] |
|
async fn bench6_real_listing_table_payload() -> Result<()> { |
|
use datafusion::datasource::physical_plan::FileScanConfig; |
|
|
|
println!("\n=== Bench 6: real listing-table per-file payload ==="); |
|
|
|
for n_files in [1usize, 10, 100] { |
|
let tmp = tempfile::tempdir().unwrap(); |
|
let ctx = SessionContext::new(); |
|
make_parquet_table(&ctx, tmp.path(), "t1", n_files, 1_000).await?; |
|
|
|
let plan = ctx |
|
.sql("SELECT * FROM t1") |
|
.await? |
|
.create_physical_plan() |
|
.await?; |
|
let mut cur = Arc::clone(&plan); |
|
let leaf = loop { |
|
if let Some(dse) = cur.downcast_ref::<DataSourceExec>() { |
|
break (dse.data_source().as_ref() as &dyn std::any::Any) |
|
.downcast_ref::<FileScanConfig>() |
|
.cloned(); |
|
} |
|
let children = cur.children(); |
|
match children.first() { |
|
Some(c) => cur = Arc::clone(c), |
|
None => break None, |
|
} |
|
}; |
|
|
|
let Some(cfg) = leaf else { |
|
println!("{n_files:>4} files: no FileScanConfig leaf found"); |
|
continue; |
|
}; |
|
|
|
let files: Vec<&PartitionedFile> = cfg |
|
.file_groups |
|
.iter() |
|
.flat_map(|g| g.files().iter()) |
|
.collect(); |
|
let with_stats = files.iter().filter(|f| f.statistics.is_some()).count(); |
|
let with_schema = files.iter().filter(|f| f.arrow_schema.is_some()).count(); |
|
let bytes = physical_plan_to_bytes(Arc::clone(&plan)) |
|
.map(|b| b.len()) |
|
.unwrap_or(0); |
|
|
|
println!( |
|
"{:>4} files: {} with per-file stats, {} with per-file arrow_schema, \ |
|
plan = {} bytes ({} bytes/file)", |
|
files.len(), |
|
with_stats, |
|
with_schema, |
|
bytes, |
|
if files.is_empty() { |
|
0 |
|
} else { |
|
bytes / files.len() |
|
}, |
|
); |
|
} |
|
|
|
Ok(()) |
|
} |
|
|
|
// --------------------------------------------------------------------------- |
|
// Bench 7: realistic shape (per-file stats, no per-file schema) vs width |
|
// --------------------------------------------------------------------------- |
|
|
|
fn schema_of_width(n_cols: usize) -> SchemaRef { |
|
let fields = (0..n_cols) |
|
.map(|i| { |
|
let dt = match i % 4 { |
|
0 => DataType::Int64, |
|
1 => DataType::Utf8, |
|
2 => DataType::Float64, |
|
_ => DataType::Boolean, |
|
}; |
|
Field::new(format!("c{i}"), dt, true) |
|
}) |
|
.collect::<Vec<_>>(); |
|
Arc::new(Schema::new(fields)) |
|
} |
|
|
|
#[test] |
|
#[ignore = "measurement harness; run explicitly with --ignored"] |
|
fn bench7_width_sweep() -> Result<()> { |
|
let ctx = SessionContext::new(); |
|
let task_ctx = ctx.task_ctx(); |
|
|
|
println!("\n=== Bench 7: realistic payload (per-file stats, no per-file schema) ==="); |
|
println!( |
|
"{:>6} {:>7} {:>12} {:>11} {:>12} {:>12}", |
|
"cols", "files", "bytes", "bytes/file", "round ms", "µs/file" |
|
); |
|
|
|
for n_cols in [4usize, 20, 105] { |
|
let schema = schema_of_width(n_cols); |
|
for n in [100usize, 1_000, 10_000] { |
|
let plan = synthetic_scan(&schema, n, NO_SCHEMA); |
|
let reps = reps_for(n); |
|
let bytes = physical_plan_to_bytes(Arc::clone(&plan))?; |
|
let len = bytes.len(); |
|
let enc = |
|
median_of(reps, || physical_plan_to_bytes(Arc::clone(&plan)).unwrap()); |
|
let dec = median_of(reps, || { |
|
physical_plan_from_bytes(&bytes, &task_ctx).unwrap() |
|
}); |
|
let round = ms(enc) + ms(dec); |
|
println!( |
|
"{:>6} {:>7} {:>12} {:>11} {:>12.3} {:>12.2}", |
|
n_cols, |
|
n, |
|
len, |
|
len / n, |
|
round, |
|
round * 1000.0 / n as f64, |
|
); |
|
} |
|
println!(); |
|
} |
|
Ok(()) |
|
} |
|
|
|
// --------------------------------------------------------------------------- |
|
// Bench 8: is the END of the default chain reliably serializable? |
|
// |
|
// A rule installed via `add_physical_optimizer_rule` is pushed to the end of |
|
// the list, so an FFI rule that serializes at its boundary sees exactly this |
|
// plan. Anything that fails here cannot use a serializing rule boundary. |
|
// --------------------------------------------------------------------------- |
|
|
|
#[tokio::test(flavor = "multi_thread")] |
|
#[ignore = "measurement harness; run explicitly with --ignored"] |
|
async fn bench8_end_of_chain_serializability() -> Result<()> { |
|
let tmp = tempfile::tempdir().unwrap(); |
|
let ctx = SessionContext::new(); |
|
make_parquet_table(&ctx, tmp.path(), "t1", 4, 5_000).await?; |
|
make_parquet_table(&ctx, tmp.path(), "t2", 4, 5_000).await?; |
|
|
|
let cases: &[(&str, &str)] = &[ |
|
("plain scan", "SELECT * FROM t1"), |
|
("filter", "SELECT * FROM t1 WHERE key > 10"), |
|
("agg", "SELECT cat, count(*) FROM t1 GROUP BY cat"), |
|
("sort", "SELECT * FROM t1 ORDER BY val"), |
|
( |
|
"topk (dyn filter)", |
|
"SELECT * FROM t1 ORDER BY val LIMIT 10", |
|
), |
|
( |
|
"topk on agg", |
|
"SELECT cat, sum(val) s FROM t1 GROUP BY cat ORDER BY s DESC LIMIT 5", |
|
), |
|
( |
|
"hash join (dyn filter)", |
|
"SELECT t1.id, t2.val FROM t1 JOIN t2 ON t1.key = t2.key", |
|
), |
|
( |
|
"join + topk", |
|
"SELECT t1.id, t2.val FROM t1 JOIN t2 ON t1.key = t2.key \ |
|
ORDER BY t2.val DESC LIMIT 10", |
|
), |
|
( |
|
"window", |
|
"SELECT id, row_number() OVER (ORDER BY val) FROM t1", |
|
), |
|
("union", "SELECT id FROM t1 UNION ALL SELECT id FROM t2"), |
|
("distinct", "SELECT DISTINCT cat FROM t1"), |
|
( |
|
"subquery", |
|
"SELECT * FROM t1 WHERE key IN (SELECT key FROM t2 WHERE val > 5)", |
|
), |
|
]; |
|
|
|
println!("\n=== Bench 8: end-of-chain plan serializability (default codec) ==="); |
|
let task_ctx = ctx.task_ctx(); |
|
let mut ok = 0usize; |
|
let mut fail = 0usize; |
|
|
|
for (name, sql) in cases { |
|
let plan = match ctx.sql(sql).await { |
|
Ok(df) => match df.create_physical_plan().await { |
|
Ok(p) => p, |
|
Err(e) => { |
|
println!("{name:<24} PLANNING FAILED: {e}"); |
|
continue; |
|
} |
|
}, |
|
Err(e) => { |
|
println!("{name:<24} SQL FAILED: {e}"); |
|
continue; |
|
} |
|
}; |
|
|
|
match physical_plan_to_bytes(Arc::clone(&plan)) { |
|
Ok(bytes) => match physical_plan_from_bytes(&bytes, &task_ctx) { |
|
Ok(_) => { |
|
ok += 1; |
|
println!("{name:<24} ok ({} bytes)", bytes.len()); |
|
} |
|
Err(e) => { |
|
fail += 1; |
|
let msg = e.to_string(); |
|
println!( |
|
"{name:<24} DECODE FAIL {}", |
|
msg.lines() |
|
.next() |
|
.unwrap_or("") |
|
.chars() |
|
.take(140) |
|
.collect::<String>() |
|
); |
|
} |
|
}, |
|
Err(e) => { |
|
fail += 1; |
|
let msg = e.to_string(); |
|
println!( |
|
"{name:<24} ENCODE FAIL {}", |
|
msg.lines() |
|
.next() |
|
.unwrap_or("") |
|
.chars() |
|
.take(140) |
|
.collect::<String>() |
|
); |
|
} |
|
} |
|
} |
|
|
|
println!("\n{ok} ok, {fail} failed out of {}", cases.len()); |
|
Ok(()) |
|
} |