|
| 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 | +//! Iceberg's system functions (`bucket`, `truncate`, `years`, `months`, `days`, `hours`) over one |
| 19 | +//! input array per supported type, with and without nulls, plus the dictionary-encoded string |
| 20 | +//! shape a Parquet scan produces for a partition column. |
| 21 | +
|
| 22 | +use arrow::array::{ |
| 23 | + ArrayRef, BinaryArray, Date32Array, Decimal128Array, DictionaryArray, Int32Array, Int64Array, |
| 24 | + StringArray, TimestampMicrosecondArray, |
| 25 | +}; |
| 26 | +use arrow::datatypes::{DataType, Field, Int32Type}; |
| 27 | +use criterion::{criterion_group, criterion_main, BenchmarkId, Criterion, Throughput}; |
| 28 | +use datafusion::common::ScalarValue; |
| 29 | +use datafusion::config::ConfigOptions; |
| 30 | +use datafusion::logical_expr::{ColumnarValue, ScalarFunctionArgs, ScalarUDFImpl}; |
| 31 | +use datafusion_comet_spark_expr::{ |
| 32 | + SparkIcebergBucket, SparkIcebergTemporalTransform, SparkIcebergTruncate, |
| 33 | +}; |
| 34 | +use std::hint::black_box; |
| 35 | +use std::sync::Arc; |
| 36 | + |
| 37 | +const ROWS: usize = 8_192; |
| 38 | +const MICROS_PER_DAY: i64 = 86_400_000_000; |
| 39 | +/// Every eighth row is null, matching the corpus the correctness suite writes. |
| 40 | +const NULL_STRIDE: usize = 8; |
| 41 | + |
| 42 | +fn maybe_null<T>(i: usize, nulls: bool, value: T) -> Option<T> { |
| 43 | + if nulls && i.is_multiple_of(NULL_STRIDE) { |
| 44 | + None |
| 45 | + } else { |
| 46 | + Some(value) |
| 47 | + } |
| 48 | +} |
| 49 | + |
| 50 | +/// A deterministic 64-bit value per row; the transforms are data dependent (bucket hashes it, |
| 51 | +/// truncate divides by it), so a constant column would not be representative. |
| 52 | +fn spread(i: usize) -> i64 { |
| 53 | + (i as i64) |
| 54 | + .wrapping_mul(6_364_136_223_846_793_005) |
| 55 | + .rotate_left(17) |
| 56 | +} |
| 57 | + |
| 58 | +/// The words a low-cardinality string partition column holds; row `i` picks `i % len`. |
| 59 | +const WORDS: [&str; 8] = [ |
| 60 | + "alpha", |
| 61 | + "bravo", |
| 62 | + "charlie", |
| 63 | + "delta", |
| 64 | + "echo", |
| 65 | + "foxtrot", |
| 66 | + "日本語テキスト", |
| 67 | + "a😀b😀c", |
| 68 | +]; |
| 69 | + |
| 70 | +fn inputs(nulls: bool) -> Vec<(&'static str, ArrayRef)> { |
| 71 | + let strings: StringArray = (0..ROWS) |
| 72 | + .map(|i| maybe_null(i, nulls, WORDS[i % WORDS.len()])) |
| 73 | + .collect(); |
| 74 | + let dictionary: DictionaryArray<Int32Type> = (0..ROWS) |
| 75 | + .map(|i| maybe_null(i, nulls, WORDS[i % WORDS.len()])) |
| 76 | + .collect(); |
| 77 | + let binaries: BinaryArray = (0..ROWS) |
| 78 | + .map(|i| maybe_null(i, nulls, spread(i).to_be_bytes())) |
| 79 | + .collect(); |
| 80 | + vec![ |
| 81 | + ( |
| 82 | + "int", |
| 83 | + Arc::new( |
| 84 | + (0..ROWS) |
| 85 | + .map(|i| maybe_null(i, nulls, spread(i) as i32)) |
| 86 | + .collect::<Int32Array>(), |
| 87 | + ), |
| 88 | + ), |
| 89 | + ( |
| 90 | + "long", |
| 91 | + Arc::new( |
| 92 | + (0..ROWS) |
| 93 | + .map(|i| maybe_null(i, nulls, spread(i))) |
| 94 | + .collect::<Int64Array>(), |
| 95 | + ), |
| 96 | + ), |
| 97 | + ( |
| 98 | + "decimal38", |
| 99 | + Arc::new( |
| 100 | + (0..ROWS) |
| 101 | + .map(|i| maybe_null(i, nulls, (spread(i) as i128) * 1_000_000_000)) |
| 102 | + .collect::<Decimal128Array>() |
| 103 | + .with_precision_and_scale(38, 10) |
| 104 | + .unwrap(), |
| 105 | + ), |
| 106 | + ), |
| 107 | + ("string", Arc::new(strings)), |
| 108 | + ("string_dict", Arc::new(dictionary)), |
| 109 | + ("binary", Arc::new(binaries)), |
| 110 | + ( |
| 111 | + "date", |
| 112 | + Arc::new( |
| 113 | + (0..ROWS) |
| 114 | + .map(|i| maybe_null(i, nulls, (spread(i) % 40_000) as i32)) |
| 115 | + .collect::<Date32Array>(), |
| 116 | + ), |
| 117 | + ), |
| 118 | + ( |
| 119 | + "timestamp", |
| 120 | + Arc::new( |
| 121 | + (0..ROWS) |
| 122 | + .map(|i| maybe_null(i, nulls, spread(i) % (40_000 * MICROS_PER_DAY))) |
| 123 | + .collect::<TimestampMicrosecondArray>() |
| 124 | + .with_timezone("UTC"), |
| 125 | + ), |
| 126 | + ), |
| 127 | + ] |
| 128 | +} |
| 129 | + |
| 130 | +fn invoke(udf: &dyn ScalarUDFImpl, args: &[ColumnarValue]) -> ArrayRef { |
| 131 | + let arg_fields: Vec<_> = args |
| 132 | + .iter() |
| 133 | + .enumerate() |
| 134 | + .map(|(i, a)| Arc::new(Field::new(format!("arg{i}"), a.data_type(), true))) |
| 135 | + .collect(); |
| 136 | + let arg_types: Vec<DataType> = arg_fields.iter().map(|f| f.data_type().clone()).collect(); |
| 137 | + let return_type = udf.return_type(&arg_types).unwrap(); |
| 138 | + udf.invoke_with_args(ScalarFunctionArgs { |
| 139 | + args: args.to_vec(), |
| 140 | + arg_fields, |
| 141 | + number_rows: ROWS, |
| 142 | + return_field: Arc::new(Field::new(udf.name(), return_type, true)), |
| 143 | + config_options: Arc::new(ConfigOptions::default()), |
| 144 | + }) |
| 145 | + .unwrap() |
| 146 | + .to_array(ROWS) |
| 147 | + .unwrap() |
| 148 | +} |
| 149 | + |
| 150 | +/// `true` when `udf` accepts `input`; the transforms are typed the same way Iceberg's `bind` is, |
| 151 | +/// so the type matrix is sparse. |
| 152 | +fn supported(udf: &dyn ScalarUDFImpl, args: &[ColumnarValue]) -> bool { |
| 153 | + std::panic::catch_unwind(std::panic::AssertUnwindSafe(|| { |
| 154 | + let arg_fields: Vec<_> = args |
| 155 | + .iter() |
| 156 | + .enumerate() |
| 157 | + .map(|(i, a)| Arc::new(Field::new(format!("arg{i}"), a.data_type(), true))) |
| 158 | + .collect(); |
| 159 | + let arg_types: Vec<DataType> = arg_fields.iter().map(|f| f.data_type().clone()).collect(); |
| 160 | + let Ok(return_type) = udf.return_type(&arg_types) else { |
| 161 | + return false; |
| 162 | + }; |
| 163 | + udf.invoke_with_args(ScalarFunctionArgs { |
| 164 | + args: args.to_vec(), |
| 165 | + arg_fields, |
| 166 | + number_rows: ROWS, |
| 167 | + return_field: Arc::new(Field::new(udf.name(), return_type, true)), |
| 168 | + config_options: Arc::new(ConfigOptions::default()), |
| 169 | + }) |
| 170 | + .is_ok() |
| 171 | + })) |
| 172 | + .unwrap_or(false) |
| 173 | +} |
| 174 | + |
| 175 | +fn criterion_benchmark(c: &mut Criterion) { |
| 176 | + let bucket = SparkIcebergBucket::new(); |
| 177 | + let truncate = SparkIcebergTruncate::new(); |
| 178 | + let years = SparkIcebergTemporalTransform::years(); |
| 179 | + let months = SparkIcebergTemporalTransform::months(); |
| 180 | + let days = SparkIcebergTemporalTransform::days(); |
| 181 | + let hours = SparkIcebergTemporalTransform::hours(); |
| 182 | + |
| 183 | + // (name, udf, parameter) -- `bucket` and `truncate` take a literal first argument. |
| 184 | + let transforms: Vec<(&str, &dyn ScalarUDFImpl, Option<i32>)> = vec![ |
| 185 | + ("iceberg_bucket", &bucket, Some(16)), |
| 186 | + ("iceberg_truncate", &truncate, Some(4)), |
| 187 | + ("iceberg_years", &years, None), |
| 188 | + ("iceberg_months", &months, None), |
| 189 | + ("iceberg_days", &days, None), |
| 190 | + ("iceberg_hours", &hours, None), |
| 191 | + ]; |
| 192 | + |
| 193 | + for (name, udf, parameter) in transforms { |
| 194 | + let mut group = c.benchmark_group(name); |
| 195 | + group.throughput(Throughput::Elements(ROWS as u64)); |
| 196 | + for (nulls, null_tag) in [(false, "no_nulls"), (true, "sparse_nulls")] { |
| 197 | + for (type_tag, array) in inputs(nulls) { |
| 198 | + let args: Vec<ColumnarValue> = parameter |
| 199 | + .map(|p| ColumnarValue::Scalar(ScalarValue::Int32(Some(p)))) |
| 200 | + .into_iter() |
| 201 | + .chain([ColumnarValue::Array(array)]) |
| 202 | + .collect(); |
| 203 | + if !supported(udf, &args) { |
| 204 | + continue; |
| 205 | + } |
| 206 | + group.bench_with_input( |
| 207 | + BenchmarkId::from_parameter(format!("{type_tag}/{null_tag}")), |
| 208 | + &args, |
| 209 | + |b, args| b.iter(|| black_box(invoke(udf, black_box(args)))), |
| 210 | + ); |
| 211 | + } |
| 212 | + } |
| 213 | + group.finish(); |
| 214 | + } |
| 215 | +} |
| 216 | + |
| 217 | +criterion_group!(benches, criterion_benchmark); |
| 218 | +criterion_main!(benches); |
0 commit comments