Skip to content
Open
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
4 changes: 4 additions & 0 deletions native/spark-expr/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -139,3 +139,7 @@ harness = false
[[bench]]
name = "to_json"
harness = false

[[bench]]
name = "cast_string_to_decimal"
harness = false
93 changes: 93 additions & 0 deletions native/spark-expr/benches/cast_string_to_decimal.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
// 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::{builder::StringBuilder, 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 rand::rngs::StdRng;
use rand::{RngExt, SeedableRng};
use std::hint::black_box;
use std::sync::Arc;

/// A batch of decimal strings covering the shapes Spark sees in practice: plain
/// integers, fixed-point values, negatives, and scientific notation.
fn create_decimal_string_batch(size: usize) -> RecordBatch {
let schema = Arc::new(Schema::new(vec![Field::new("a", DataType::Utf8, true)]));
let mut rng = StdRng::seed_from_u64(42);
let mut b = StringBuilder::new();
for i in 0..size {
if i % 10 == 0 {
b.append_null();
} else {
match i % 5 {
0 => b.append_value(format!(
"{}.{}",
rng.random_range(0..1_000_000u32),
rng.random_range(0..100_000u32)
)),
1 => b.append_value(format!(
"{}.{}E{}",
rng.random_range(0..10u32),
rng.random_range(0..100u32),
rng.random_range(0..10u32)
)),
2 => b.append_value(format!(
"-{}.{}",
rng.random_range(0..1_000_000u32),
rng.random_range(0..100_000u32)
)),
3 => b.append_value(format!("{}", rng.random_range(-1_000_000..1_000_000i32))),
_ => b.append_value(format!("0.{:05}", rng.random_range(0..100_000u32))),
}
}
}
RecordBatch::try_new(schema, vec![Arc::new(b.finish())]).unwrap()
}

fn criterion_benchmark(c: &mut Criterion) {
let batch = create_decimal_string_batch(8192);
let expr = Arc::new(Column::new("a", 0));

for (mode, mode_name) in [
(EvalMode::Legacy, "legacy"),
(EvalMode::Ansi, "ansi"),
(EvalMode::Try, "try"),
] {
let mut group = c.benchmark_group(format!("cast_string_to_decimal/{mode_name}"));

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

native/spark-expr/benches/cast_string_to_decimal.rs:72 creates group cast_string_to_decimal/{mode_name} with a decimal_38_10 bench function. The existing native/spark-expr/benches/cast_from_string.rs:124-131 already creates the identical group cast_string_to_decimal/{mode_name} with an identical decimal_38_10 function, over the same three eval modes and the same string shapes (plain, fixed-point, negative, scientific). Criterion keys saved results by group/function, so the two benches write to and overwrite the same target/criterion/cast_string_to_decimal/legacy/decimal_38_10 directory. Whichever bench runs last clobbers the other's baseline, which makes the reported before/after comparison unreliable.

The only thing the new file adds over the existing one is the decimal_18_2 variant and a seeded StdRng (the existing bench uses unseeded rand::random, so its numbers are not reproducible run to run).

Suggested change: do not add a second benchmark file. Fold the improvements into the existing create_decimal_cast_string_batch path in cast_from_string.rs: switch it to a seeded StdRng and add the decimal_18_2 data type to the existing loop at cast_from_string.rs:116-133. Delete benches/cast_string_to_decimal.rs and the corresponding [[bench]] entry in Cargo.toml. If you keep a separate file, rename its group to something distinct like parse_string_to_decimal/... so it does not overwrite the existing baseline.

for (data_type, name) in [
(DataType::Decimal128(38, 10), "decimal_38_10"),
(DataType::Decimal128(18, 2), "decimal_18_2"),
] {
let cast = Cast::new(
expr.clone(),
data_type,
SparkCastOptions::new(mode, "", false),
None,
None,
);
group.bench_function(name, |b| {
b.iter(|| black_box(cast.evaluate(&batch).unwrap()));
});
}
group.finish();
}
}

criterion_group!(benches, criterion_benchmark);
criterion_main!(benches);
192 changes: 122 additions & 70 deletions native/spark-expr/src/conversion_funcs/string.rs
Original file line number Diff line number Diff line change
Expand Up @@ -469,6 +469,71 @@ fn normalize_fullwidth_digits(s: &str) -> String {
unsafe { String::from_utf8_unchecked(out) }
}

/// Powers of ten that fit in an `i128` (`10^0` through `10^38`).
const POW10_I128: [i128; 39] = {
let mut table = [1i128; 39];
let mut i = 1;
while i < 39 {
table[i] = table[i - 1] * 10;
i += 1;
}
table
};

/// `10^exp`, using the precomputed table for the range that fits in an `i128`.
#[inline]
fn pow10_i128(exp: u32) -> i128 {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

pow10_i128 falls back to 10_i128.pow(exp) on a table miss (exp >= 39). Two of the three call sites are guarded by scale_adjustment > 38 / abs_scale_adjustment > 38 checks, so they never miss. The checked_mul(pow10_i128(fractional_scale as u32)) combine step at the end of parse_decimal_str, however, is not bounded: fractional_scale is fractional_part.len(), so an input like "0." + "0"*40 calls pow10_i128(40), misses the table, and evaluates 10_i128.pow(40), which panics in debug and wraps in release. This is identical to the old code (10_i128.pow(fractional_scale as u32)), so the PR introduces no regression, but the new named helper is the natural place to make the fallback total.

Suggested change: make pow10_i128 return Option<i128> (or add a checked_pow10 variant) and thread it through the combine step so an over-long fraction produces the invalid_decimal_cast error path instead of a panic. If you would rather keep this PR scoped to performance, leave a one-line comment on pow10_i128 noting that callers must keep exp <= 38 and that the 10_i128.pow fallback is unreachable in current callers, so it is not silently masking a bug.

match POW10_I128.get(exp as usize) {
Some(v) => *v,
None => 10_i128.pow(exp),
}
}

/// Accumulate an ASCII-digit slice into an `i128`, returning `None` on overflow.
///
/// The first 38 digits always fit (`i128::MAX` is ~1.7e38), so only the digits past
/// them need the per-digit overflow checks.
#[inline]
fn digits_to_i128(digits: &[u8]) -> Option<i128> {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

digits_to_i128 at native/spark-expr/src/conversion_funcs/string.rs (new helper) splits the digit slice at 38 and skips overflow checks on the first 38 digits, relying on the fact that 38 nines (about 9.99e37) fit under i128::MAX (about 1.7e38). The reasoning is correct, and leading zeros are handled correctly because zeros in the head accumulate to 0 and the tail uses checked_mul/checked_add. But this boundary is exactly where a future edit could regress, and the current Scala fuzz tests (CometCastSuite.scala:958-961) cap the generated digit count at 38, so they never exercise a 39-plus-digit integral part.

Suggested change: add a Rust unit test in the #[cfg(test)] block of string.rs asserting parse results for a 38-digit value (parses), a 39-digit value (overflows to the invalid_decimal_cast error / NULL in non-ANSI), and a 40-digit value with leading zeros that reduce to a small value (parses). This locks the boundary that the optimization now depends on.

let (head, tail) = digits.split_at(digits.len().min(38));
let mut value: i128 = 0;
for &d in head {
value = value * 10 + (d - b'0') as i128;
}
for &d in tail {
value = value.checked_mul(10)?.checked_add((d - b'0') as i128)?;
}
Some(value)
}

/// Values that Spark parses as NULL rather than as a decimal, matched case-insensitively.
const SPECIAL_DECIMAL_VALUES: [&str; 7] = [
"inf",
"+inf",
"-inf",
"infinity",
"+infinity",
"-infinity",
"nan",
];

/// True if `trimmed` is one of [`SPECIAL_DECIMAL_VALUES`].
#[inline]
fn is_special_value(trimmed: &str) -> bool {
// Every special value starts with `i`/`n`, or with a sign followed by `i`, so
// ordinary numeric input is ruled out after inspecting a single byte.
let bytes = trimmed.as_bytes();
let plausible = match bytes.first() {
Some(b'i' | b'I' | b'n' | b'N') => true,
Some(b'+' | b'-') => matches!(bytes.get(1), Some(b'i' | b'I')),
_ => false,
};
plausible
&& SPECIAL_DECIMAL_VALUES
.iter()
.any(|v| trimmed.eq_ignore_ascii_case(v))
}

/// Parse a decimal string into mantissa and scale
/// e.g., "123.45" -> (12345, 2), "-0.001" -> (-1, 3) , 0e50 -> (0,50) etc
/// Parse a string to decimal following Spark's behavior
Expand All @@ -494,25 +559,18 @@ fn parse_string_to_decimal(input_str: &str, precision: u8, scale: i8) -> SparkRe
// Normalize fullwidth digits to ASCII. Fast path skips the allocation for
// pure-ASCII strings, which is the common case.
let normalized;
let trimmed = if trimmed.bytes().any(|b| b > 0x7F) {
let trimmed = if trimmed.is_ascii() {
trimmed
} else {
normalized = normalize_fullwidth_digits(trimmed);
normalized.as_str()
} else {
trimmed
};

if trimmed.is_empty() {
return Ok(None);
}
// Handle special values (inf, nan, etc.)
if trimmed.eq_ignore_ascii_case("inf")
|| trimmed.eq_ignore_ascii_case("+inf")
|| trimmed.eq_ignore_ascii_case("infinity")
|| trimmed.eq_ignore_ascii_case("+infinity")
|| trimmed.eq_ignore_ascii_case("-inf")
|| trimmed.eq_ignore_ascii_case("-infinity")
|| trimmed.eq_ignore_ascii_case("nan")
{
if is_special_value(trimmed) {
return Ok(None);
}

Expand All @@ -538,15 +596,15 @@ fn parse_string_to_decimal(input_str: &str, precision: u8, scale: i8) -> SparkRe
if scale_adjustment > 38 {
return Ok(None);
}
mantissa.checked_mul(10_i128.pow(scale_adjustment as u32))
mantissa.checked_mul(pow10_i128(scale_adjustment as u32))
} else {
// Need to divide (decrease scale)
let abs_scale_adjustment = (-scale_adjustment) as u32;
if abs_scale_adjustment > 38 {
return Ok(Some(0));
}

let divisor = 10_i128.pow(abs_scale_adjustment);
let divisor = pow10_i128(abs_scale_adjustment);
let quotient_opt = mantissa.checked_div(divisor);
// Check if divisor is 0
if quotient_opt.is_none() {
Expand Down Expand Up @@ -609,79 +667,73 @@ fn parse_decimal_str(
precision: u8,
scale: i8,
) -> SparkResult<(i128, i32)> {
if s.is_empty() {
return Err(invalid_decimal_cast(original_str, precision, scale));
}

let (mantissa_str, exponent) = if let Some(e_pos) = s.find(|c| ['e', 'E'].contains(&c)) {
let mantissa_part = &s[..e_pos];
let exponent_part = &s[e_pos + 1..];
// Parse exponent
let exp: i32 = exponent_part
.parse()
.map_err(|_| invalid_decimal_cast(original_str, precision, scale))?;

(mantissa_part, exp)
} else {
(s, 0)
};
let bytes = s.as_bytes();

let negative = mantissa_str.starts_with('-');
let mantissa_str = if negative || mantissa_str.starts_with('+') {
&mantissa_str[1..]
} else {
mantissa_str
let mut pos = 0;
let negative = match bytes.first() {
Some(b'-') => {
pos = 1;
true
}
Some(b'+') => {
pos = 1;
false
}
_ => false,
};

if mantissa_str.starts_with('+') || mantissa_str.starts_with('-') {
return Err(invalid_decimal_cast(original_str, precision, scale));
}

let (integral_part, fractional_part) = match mantissa_str.find('.') {
Some(dot_pos) => {
if mantissa_str[dot_pos + 1..].contains('.') {
return Err(invalid_decimal_cast(original_str, precision, scale));
// Single validating pass over the mantissa: ASCII digits with at most one `.`,
// ending at the optional exponent marker. It also locates the integral/fractional
// split and the start of the exponent. `.`, `e` and `E` are ASCII, so scanning
// bytes can never land inside a multi-byte character and every index taken here is
// on a char boundary.
let digits_start = pos;
let mut dot_pos = None;
let mut exp_pos = None;
while pos < bytes.len() {
match bytes[pos] {
b'0'..=b'9' => pos += 1,
b'.' if dot_pos.is_none() => {
dot_pos = Some(pos);
pos += 1;
}
b'e' | b'E' => {
exp_pos = Some(pos);
break;
}
(&mantissa_str[..dot_pos], &mantissa_str[dot_pos + 1..])
_ => return Err(invalid_decimal_cast(original_str, precision, scale)),
}
None => (mantissa_str, ""),
};

if integral_part.is_empty() && fractional_part.is_empty() {
return Err(invalid_decimal_cast(original_str, precision, scale));
}

if !integral_part.is_empty() && !integral_part.bytes().all(|b| b.is_ascii_digit()) {
return Err(invalid_decimal_cast(original_str, precision, scale));
}
let exponent: i32 = match exp_pos {
Some(e_pos) => s[e_pos + 1..]
.parse()
.map_err(|_| invalid_decimal_cast(original_str, precision, scale))?,
None => 0,
};

if !fractional_part.is_empty() && !fractional_part.bytes().all(|b| b.is_ascii_digit()) {
// An empty integral part is valid (e.g. ".5" or "-.7e9"), as is an empty
// fractional part, but they cannot both be empty.
let mantissa_end = exp_pos.unwrap_or(pos);
let (integral_part, fractional_part): (&[u8], &[u8]) = match dot_pos {
Some(dot) => (&bytes[digits_start..dot], &bytes[dot + 1..mantissa_end]),
None => (&bytes[digits_start..mantissa_end], &[]),
};

if integral_part.is_empty() && fractional_part.is_empty() {
return Err(invalid_decimal_cast(original_str, precision, scale));
}

// Parse integral part
let integral_value: i128 = if integral_part.is_empty() {
// Empty integral part is valid (e.g., ".5" or "-.7e9")
0
} else {
integral_part
.parse()
.map_err(|_| invalid_decimal_cast(original_str, precision, scale))?
};
let integral_value = digits_to_i128(integral_part)
.ok_or_else(|| invalid_decimal_cast(original_str, precision, scale))?;

// Parse fractional part
let fractional_scale = fractional_part.len() as i32;
let fractional_value: i128 = if fractional_part.is_empty() {
0
} else {
fractional_part
.parse()
.map_err(|_| invalid_decimal_cast(original_str, precision, scale))?
};
let fractional_value = digits_to_i128(fractional_part)
.ok_or_else(|| invalid_decimal_cast(original_str, precision, scale))?;

// Combine: value = integral * 10^fractional_scale + fractional
let mantissa = integral_value
.checked_mul(10_i128.pow(fractional_scale as u32))
.checked_mul(pow10_i128(fractional_scale as u32))
.and_then(|v| v.checked_add(fractional_value))
.ok_or_else(|| invalid_decimal_cast(original_str, precision, scale))?;

Expand Down
Loading