Skip to content

Commit fa0158d

Browse files
committed
bench: measure the Iceberg system functions natively and against Iceberg's JVM
`native/spark-expr/benches/iceberg_transforms.rs` covers every transform over every supported type, with and without nulls, plus the dictionary-encoded string shape a Parquet scan produces. `CometIcebergSystemFunctionBenchmark` runs the same queries with Comet on and off. The Comet-off case is Iceberg's own JVM implementation, since Spark binds each function as a `StaticInvoke` of the class under `org.apache.iceberg.spark.functions`. The data stays in Parquet rather than an Iceberg table so both cases scan identically and only the transform differs.
1 parent 19ca6c3 commit fa0158d

3 files changed

Lines changed: 332 additions & 1 deletion

File tree

‎native/spark-expr/Cargo.toml‎

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -301,4 +301,8 @@ harness = false
301301

302302
[[bench]]
303303
name = "to_utc_timestamp"
304-
harness = false
304+
harness = false
305+
306+
[[bench]]
307+
name = "iceberg_transforms"
308+
harness = false
Lines changed: 218 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,218 @@
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);
Lines changed: 109 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,109 @@
1+
/*
2+
* Licensed to the Apache Software Foundation (ASF) under one
3+
* or more contributor license agreements. See the NOTICE file
4+
* distributed with this work for additional information
5+
* regarding copyright ownership. The ASF licenses this file
6+
* to you under the Apache License, Version 2.0 (the
7+
* "License"); you may not use this file except in compliance
8+
* with the License. You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing,
13+
* software distributed under the License is distributed on an
14+
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15+
* KIND, either express or implied. See the License for the
16+
* specific language governing permissions and limitations
17+
* under the License.
18+
*/
19+
20+
package org.apache.spark.sql.benchmark
21+
22+
import org.apache.comet.iceberg.IcebergReflection
23+
24+
/**
25+
* Benchmark of Iceberg's system functions (`bucket`, `truncate`, `years`, `months`, `days`,
26+
* `hours`) with Comet on and off. The Spark case is Iceberg's own JVM implementation: Spark binds
27+
* each function as a `StaticInvoke` of the matching class under
28+
* `org.apache.iceberg.spark.functions` and whole-stage codegen calls it once per row.
29+
*
30+
* To run this benchmark:
31+
* {{{
32+
* SPARK_GENERATE_BENCHMARK_FILES=1 make benchmark-org.apache.spark.sql.benchmark.CometIcebergSystemFunctionBenchmark
33+
* }}}
34+
* Results will be written to
35+
* "spark/benchmarks/CometIcebergSystemFunctionBenchmark-**results.txt".
36+
*/
37+
object CometIcebergSystemFunctionBenchmark extends CometBenchmarkBase {
38+
39+
private val catalog = "benchmark_cat"
40+
41+
/**
42+
* One case per (transform, input type). `c_str_dict` holds eight distinct values so Parquet
43+
* dictionary-encodes it, which is the shape a string partition column normally arrives in;
44+
* `c_str` is distinct per row.
45+
*/
46+
private def cases: Seq[(String, String)] = {
47+
val bucket = Seq("c_int", "c_long", "c_dec", "c_str_dict", "c_str", "c_bin", "c_date", "c_ts")
48+
.map(column => s"bucket($column)" -> s"select $catalog.system.bucket(16, $column)")
49+
val truncate = Seq("c_int", "c_long", "c_dec", "c_str_dict", "c_str", "c_bin")
50+
.map(column => s"truncate($column)" -> s"select $catalog.system.truncate(4, $column)")
51+
val temporal = Seq("years", "months", "days").flatMap { fn =>
52+
Seq("c_date", "c_ts").map(column =>
53+
s"$fn($column)" -> s"select $catalog.system.$fn($column)")
54+
} :+ ("hours(c_ts)" -> s"select $catalog.system.hours(c_ts)")
55+
(bucket ++ truncate ++ temporal).map { case (name, select) =>
56+
name -> s"$select from parquetV1Table"
57+
}
58+
}
59+
60+
override def runCometBenchmark(mainArgs: Array[String]): Unit = {
61+
if (!icebergOnClasspath) {
62+
// scalastyle:off println
63+
println("Iceberg is not on the classpath; skipping. Build with an Iceberg-enabled profile.")
64+
// scalastyle:on println
65+
return
66+
}
67+
// The Iceberg system functions are resolved through a v2 catalog, so one has to be
68+
// registered. No Iceberg table is read: the data stays in Parquet so both cases scan
69+
// identically and the only difference is who evaluates the transform.
70+
withTempPath { warehouse =>
71+
spark.conf.set(s"spark.sql.catalog.$catalog", "org.apache.iceberg.spark.SparkCatalog")
72+
spark.conf.set(s"spark.sql.catalog.$catalog.type", "hadoop")
73+
spark.conf.set(s"spark.sql.catalog.$catalog.warehouse", warehouse.getAbsolutePath)
74+
75+
runBenchmarkWithTable("Iceberg system functions", 1024 * 1024) { v =>
76+
withTempPath { dir =>
77+
withTempTable("parquetV1Table") {
78+
prepareTable(
79+
dir,
80+
spark.sql(s"""
81+
SELECT CAST(value AS INT) AS c_int,
82+
value AS c_long,
83+
CAST(value AS DECIMAL(38,10)) AS c_dec,
84+
CAST(PMOD(value, 8) AS STRING) AS c_str_dict,
85+
REPEAT(CAST(value AS STRING), 3) AS c_str,
86+
CAST(CAST(value AS STRING) AS BINARY) AS c_bin,
87+
DATE_ADD(DATE '1970-01-01', CAST(PMOD(value, 40000) AS INT)) AS c_date,
88+
TIMESTAMP_SECONDS(PMOD(value, 4000000000)) AS c_ts
89+
FROM $tbl"""))
90+
91+
cases.foreach { case (name, query) =>
92+
runBenchmark(name) {
93+
runExpressionBenchmark(name, v, query)
94+
}
95+
}
96+
}
97+
}
98+
}
99+
}
100+
}
101+
102+
private def icebergOnClasspath: Boolean =
103+
try {
104+
IcebergReflection.loadClass("org.apache.iceberg.spark.functions.BucketFunction")
105+
true
106+
} catch {
107+
case _: ClassNotFoundException => false
108+
}
109+
}

0 commit comments

Comments
 (0)