Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -37,5 +37,6 @@
- Spark registers the type-name conversion functions (`bigint`, `binary`, `boolean`, `date`, `decimal`, `double`, `float`, `int`, `smallint`, `string`, `timestamp`, `tinyint`) as cast aliases. Each lowers to the same `Cast` node, so Comet handles it via the `cast` implementation with the same compatibility profile.
- Performance (tuned 2026-07-14, PR #4920): narrowing integer casts (`spark_cast_int_to_int`) map the values buffer in a single pass with Arrow `unary`/`try_unary` and carry the null buffer over untouched, replacing an element-by-element `Option`/`Result` iterator-collect. Up to 100x faster on narrowing casts. Benchmark: `benches/cast_numeric.rs`.
- Performance (tuned 2026-07-15, PR #4940): float/double-to-decimal casts (`cast_floating_point_to_decimal128`) now convert in a single vectorized `unary_opt` pass that maps out-of-range values (NaN, infinity, precision overflow) to null, replacing the per-element `Decimal128Builder` loop. ANSI raises via an O(1) null-count check plus a rare element-wise rescan. 15-36% faster with no regression on any shape. Benchmark: `benches/cast_float_to_decimal.rs`.
- Performance (tuned 2026-07-15, PR #4941): float-to-int and decimal-to-int narrowing casts (`cast_float_to_int16_down`/`cast_float_to_int32_up`/`cast_decimal_to_int16_down`/`cast_decimal_to_int32_up`) now map the values buffer with Arrow `unary` (legacy) / `try_unary` (ANSI) instead of a per-element `Option`/`Result` iterator-collect, and the decimal macros hoist the constant `10^scale` divisor out of the loop. 49-91% faster with no regression; overflow/NaN/wrap semantics unchanged. Benchmark: `benches/cast_narrowing.rs`.

[Spark Expression Support]: ../../user-guide/latest/expressions.md
4 changes: 4 additions & 0 deletions native/spark-expr/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -163,6 +163,10 @@ harness = false
name = "to_json"
harness = false

[[bench]]
name = "cast_narrowing"
harness = false

[[bench]]
name = "cast_float_to_string"
harness = false
Expand Down
100 changes: 100 additions & 0 deletions native/spark-expr/benches/cast_narrowing.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
// 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.

use arrow::array::{Array, Decimal128Array, Float64Array, RecordBatch};
use arrow::datatypes::{DataType, Field, Schema};
use criterion::{criterion_group, criterion_main, Criterion};
use datafusion::physical_expr::{expressions::Column, PhysicalExpr};
use datafusion_comet_spark_expr::{Cast, EvalMode, SparkCastOptions};
use std::hint::black_box;
use std::sync::Arc;

fn f64_batch(size: usize) -> RecordBatch {
// Small in-range values so narrowing to i8 does not overflow.
let a: Float64Array = (0..size)
.map(|i| {
if i % 10 == 0 {
None
} else {
Some((i % 100) as f64)
}
})
.collect();
let schema = Arc::new(Schema::new(vec![Field::new("a", DataType::Float64, true)]));
RecordBatch::try_new(schema, vec![Arc::new(a)]).unwrap()
}

fn dec_batch(size: usize) -> RecordBatch {
let a: Decimal128Array = (0..size)
.map(|i| {
if i % 10 == 0 {
None
} else {
Some((i % 100) as i128 * 100)
}
})
.collect::<Decimal128Array>()
.with_precision_and_scale(10, 2)
.unwrap();
let schema = Arc::new(Schema::new(vec![Field::new(
"a",
a.data_type().clone(),
true,
)]));
RecordBatch::try_new(schema, vec![Arc::new(a)]).unwrap()
}

fn cast(to: DataType, mode: EvalMode) -> Cast {
Cast::new(
Arc::new(Column::new("a", 0)),
to,
SparkCastOptions::new_without_timezone(mode, false),
None,
None,
)
}

fn criterion_benchmark(c: &mut Criterion) {
let size = 8192;
let f = f64_batch(size);
let d = dec_batch(size);

let f_i8 = cast(DataType::Int8, EvalMode::Legacy);
let f_i32 = cast(DataType::Int32, EvalMode::Legacy);
let f_i32_ansi = cast(DataType::Int32, EvalMode::Ansi);
let d_i8 = cast(DataType::Int8, EvalMode::Legacy);
let d_i32 = cast(DataType::Int32, EvalMode::Legacy);

c.bench_function("cast_narrowing: f64 -> i8", |b| {
b.iter(|| black_box(f_i8.evaluate(black_box(&f)).unwrap()))
});
c.bench_function("cast_narrowing: f64 -> i32", |b| {
b.iter(|| black_box(f_i32.evaluate(black_box(&f)).unwrap()))
});
c.bench_function("cast_narrowing: f64 -> i32 ansi", |b| {
b.iter(|| black_box(f_i32_ansi.evaluate(black_box(&f)).unwrap()))
});
c.bench_function("cast_narrowing: dec -> i8", |b| {
b.iter(|| black_box(d_i8.evaluate(black_box(&d)).unwrap()))
});
c.bench_function("cast_narrowing: dec -> i32", |b| {
b.iter(|| black_box(d_i32.evaluate(black_box(&d)).unwrap()))
});
}

criterion_group!(benches, criterion_benchmark);
criterion_main!(benches);
Loading