andygrove commented on code in PR #4916: URL: https://github.com/apache/datafusion-comet/pull/4916#discussion_r3603949191
########## native/spark-expr/benches/cast_string_to_decimal.rs: ########## @@ -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}")); Review Comment: Deleted `benches/cast_string_to_decimal.rs` and folded its improvements (seeded `StdRng`, 8192-row batch, `decimal_18_2` cast) into the existing `create_decimal_cast_string_batch` in `cast_from_string.rs`. The two benches no longer collide on the same criterion group. ########## native/spark-expr/src/conversion_funcs/string.rs: ########## @@ -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 { + 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> { Review Comment: Added `test_digits_to_i128_boundary`: 38 nines parses, 39 nines returns `None`, 38 leading zeros plus `42` reduces to 42. Added `test_parse_string_to_decimal_boundary` at the higher level too. ########## native/spark-expr/src/conversion_funcs/string.rs: ########## @@ -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 { Review Comment: Changed `pow10_i128` to return `Option<i128>`. The two bounded call sites (`scale_adjustment <= 38` / `abs_scale_adjustment <= 38`) call `.unwrap()` with a comment; the previously-unbounded combine step now threads the Option through and an over-long fractional part maps to the `invalid_decimal_cast` error instead of the debug panic from `10_i128.pow(40)`. -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: [email protected] For queries about this service, please contact Infrastructure at: [email protected] --------------------------------------------------------------------- To unsubscribe, e-mail: [email protected] For additional commands, e-mail: [email protected]
