|
| 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 | +use std::collections::HashMap; |
| 19 | +use std::hint::black_box; |
| 20 | +use std::sync::Arc; |
| 21 | + |
| 22 | +use arrow::datatypes::{DataType, Field}; |
| 23 | +use criterion::{BenchmarkId, Criterion, criterion_group, criterion_main}; |
| 24 | +use datafusion_common::DFSchema; |
| 25 | +use datafusion_common::metadata::FieldMetadata; |
| 26 | +use datafusion_expr::{ExprSchemable, col}; |
| 27 | + |
| 28 | +fn nested_alias_to_field(c: &mut Criterion) { |
| 29 | + let source_metadata = HashMap::from([ |
| 30 | + ("keep".to_string(), "base".to_string()), |
| 31 | + ("override".to_string(), "base".to_string()), |
| 32 | + ]); |
| 33 | + let schema = DFSchema::from_unqualified_fields( |
| 34 | + vec![Arc::new( |
| 35 | + Field::new("source", DataType::Int32, false).with_metadata(source_metadata), |
| 36 | + )] |
| 37 | + .into(), |
| 38 | + HashMap::new(), |
| 39 | + ) |
| 40 | + .unwrap(); |
| 41 | + |
| 42 | + let mut group = c.benchmark_group("nested_alias_to_field"); |
| 43 | + for depth in [0, 1, 4, 8, 12] { |
| 44 | + for with_metadata in [false, true] { |
| 45 | + let mut expr = col("source"); |
| 46 | + for index in 0..depth { |
| 47 | + expr = if with_metadata { |
| 48 | + expr.alias_with_metadata( |
| 49 | + format!("alias{index}"), |
| 50 | + Some(FieldMetadata::from(HashMap::from([( |
| 51 | + "override".to_string(), |
| 52 | + index.to_string(), |
| 53 | + )]))), |
| 54 | + ) |
| 55 | + } else { |
| 56 | + expr.alias(format!("alias{index}")) |
| 57 | + }; |
| 58 | + } |
| 59 | + // Rust API builds directly nested aliases, avoiding SQL's surface syntax limitations. |
| 60 | + let mode = if with_metadata { "metadata" } else { "plain" }; |
| 61 | + group.bench_with_input(BenchmarkId::new(mode, depth), &expr, |b, expr| { |
| 62 | + b.iter(|| { |
| 63 | + black_box(black_box(expr).to_field(black_box(&schema)).unwrap()) |
| 64 | + }) |
| 65 | + }); |
| 66 | + } |
| 67 | + } |
| 68 | + group.finish(); |
| 69 | +} |
| 70 | + |
| 71 | +criterion_group!(benches, nested_alias_to_field); |
| 72 | +criterion_main!(benches); |
0 commit comments