|
| 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 | +use arrow_array::{ |
| 19 | + ArrayRef, Decimal32Array, Decimal64Array, Decimal128Array, Decimal256Array, Float32Array, |
| 20 | + Float64Array, StringArray, |
| 21 | +}; |
| 22 | +use arrow_buffer::i256; |
| 23 | +use arrow_schema::DataType::{ |
| 24 | + Decimal32, Decimal64, Decimal128, Decimal256, Float32, Float64, Int8, Int16, Int32, Int64, |
| 25 | + UInt8, UInt16, UInt32, UInt64, |
| 26 | +}; |
| 27 | +use criterion::*; |
| 28 | +use std::sync::Arc; |
| 29 | + |
| 30 | +fn cast_string_from_or_to_decimals(c: &mut Criterion) { |
| 31 | + let str_array = StringArray::from(vec![ |
| 32 | + Some("123.45"), |
| 33 | + Some("1.2345"), |
| 34 | + Some("0.12345"), |
| 35 | + Some("0.1267"), |
| 36 | + Some("1.263"), |
| 37 | + Some("12345.0"), |
| 38 | + Some("12345"), |
| 39 | + Some("000.123"), |
| 40 | + Some("12.234000"), |
| 41 | + None, |
| 42 | + Some(""), |
| 43 | + Some(" "), |
| 44 | + None, |
| 45 | + Some("-1.23499999"), |
| 46 | + Some("-1.23599999"), |
| 47 | + Some("-0.00001"), |
| 48 | + Some("-123"), |
| 49 | + Some("-123.234000"), |
| 50 | + Some("-000.123"), |
| 51 | + Some("+1.23499999"), |
| 52 | + Some("+1.23599999"), |
| 53 | + Some("+0.00001"), |
| 54 | + Some("+123"), |
| 55 | + Some("+123.234000"), |
| 56 | + Some("+000.123"), |
| 57 | + Some("1.-23499999"), |
| 58 | + Some("-1.-23499999"), |
| 59 | + Some("--1.23499999"), |
| 60 | + Some("0"), |
| 61 | + Some("000.000"), |
| 62 | + Some("0000000000000000012345.000"), |
| 63 | + ]); |
| 64 | + let array = Arc::new(str_array) as ArrayRef; |
| 65 | + |
| 66 | + let bench_suite = [ |
| 67 | + ("string2decimal32(9, 2)", Decimal32(9, 2)), |
| 68 | + ("string2decimal64(18, 2)", Decimal64(18, 2)), |
| 69 | + ("string2decimal128(38, 3)", Decimal128(38, 3)), |
| 70 | + ("string2decimal256(76, 4)", Decimal256(76, 4)), |
| 71 | + ]; |
| 72 | + for bench in bench_suite { |
| 73 | + c.bench_function(bench.0, |b| { |
| 74 | + b.iter(|| { |
| 75 | + let r = arrow_cast::cast(&array, &bench.1); |
| 76 | + std::hint::black_box(r) |
| 77 | + }) |
| 78 | + }); |
| 79 | + } |
| 80 | +} |
| 81 | + |
| 82 | +fn cast_float_to_decimals(c: &mut Criterion) { |
| 83 | + let bench_suite_float32 = [ |
| 84 | + ("float32_to_decimal32(9, 2)", Decimal32(9, 2)), |
| 85 | + ("float32_to_decimal64(18, 2)", Decimal64(18, 2)), |
| 86 | + ("float32_to_decimal128(38, 3)", Decimal128(38, 3)), |
| 87 | + ("float32_to_decimal256(76, 4)", Decimal256(76, 4)), |
| 88 | + ]; |
| 89 | + let float32_array = Float32Array::from(vec![-123.45f32, f32::MIN, 0f32, 123.45f32, f32::MAX]); |
| 90 | + for bench in &bench_suite_float32 { |
| 91 | + c.bench_function(bench.0, |b| { |
| 92 | + b.iter(|| { |
| 93 | + let r = arrow_cast::cast(&float32_array, &bench.1); |
| 94 | + std::hint::black_box(r) |
| 95 | + }) |
| 96 | + }); |
| 97 | + } |
| 98 | + |
| 99 | + let bench_suite_float64 = [ |
| 100 | + ("float64_to_decimal32(9, 2)", Decimal32(9, 2)), |
| 101 | + ("float64_to_decimal64(18, 4)", Decimal64(18, 2)), |
| 102 | + ("float64_to_decimal128(38, 3)", Decimal128(38, 3)), |
| 103 | + ("float64_to_decimal256(76, 4)", Decimal256(76, 4)), |
| 104 | + ]; |
| 105 | + let float64_array = Float64Array::from(vec![-123.45f64, f64::MIN, 0f64, 123.45f64, f64::MAX]); |
| 106 | + for bench in &bench_suite_float64 { |
| 107 | + c.bench_function(bench.0, |b| { |
| 108 | + b.iter(|| { |
| 109 | + let r = arrow_cast::cast(&float64_array, &bench.1); |
| 110 | + std::hint::black_box(r) |
| 111 | + }) |
| 112 | + }); |
| 113 | + } |
| 114 | +} |
| 115 | + |
| 116 | +fn cast_decimal_to_float(c: &mut Criterion) { |
| 117 | + let decimal32_array = Decimal32Array::from(vec![Some(12345), Some(23400), None, Some(-12342)]) |
| 118 | + .with_precision_and_scale(9, 2) |
| 119 | + .unwrap(); |
| 120 | + |
| 121 | + let bench_suite_decimal32 = [ |
| 122 | + ("decimal32(9, 2)_to_float32", Float32), |
| 123 | + ("decimal32(9, 2)_to_float64", Float64), |
| 124 | + ]; |
| 125 | + |
| 126 | + for bench in &bench_suite_decimal32 { |
| 127 | + c.bench_function(bench.0, |b| { |
| 128 | + b.iter(|| { |
| 129 | + let r = arrow_cast::cast(&decimal32_array, &bench.1); |
| 130 | + std::hint::black_box(r) |
| 131 | + }) |
| 132 | + }); |
| 133 | + } |
| 134 | + |
| 135 | + let decimal64_array = Decimal64Array::from(vec![ |
| 136 | + Some(123451), |
| 137 | + Some(234000), |
| 138 | + None, |
| 139 | + Some(1234100), |
| 140 | + Some(-12342), |
| 141 | + ]) |
| 142 | + .with_precision_and_scale(18, 2) |
| 143 | + .unwrap(); |
| 144 | + |
| 145 | + let bench_suite_decimal64 = [ |
| 146 | + ("decimal64(18, 2)_to_float32", Float32), |
| 147 | + ("decimal64(18, 2)_to_float64", Float64), |
| 148 | + ]; |
| 149 | + for bench in &bench_suite_decimal64 { |
| 150 | + c.bench_function(bench.0, |b| { |
| 151 | + b.iter(|| { |
| 152 | + let r = arrow_cast::cast(&decimal64_array, &bench.1); |
| 153 | + std::hint::black_box(r) |
| 154 | + }) |
| 155 | + }); |
| 156 | + } |
| 157 | + |
| 158 | + let decimal128_array = Decimal128Array::from(vec![ |
| 159 | + Some(123451), |
| 160 | + Some(234000), |
| 161 | + None, |
| 162 | + Some(1234100), |
| 163 | + Some(-12342), |
| 164 | + ]) |
| 165 | + .with_precision_and_scale(38, 3) |
| 166 | + .unwrap(); |
| 167 | + let bench_suite_decimal128 = [ |
| 168 | + ("decimal128(38, 3)_to_float32", Float32), |
| 169 | + ("decimal128(38, 3)_to_float64", Float64), |
| 170 | + ]; |
| 171 | + for bench in &bench_suite_decimal128 { |
| 172 | + c.bench_function(bench.0, |b| { |
| 173 | + b.iter(|| { |
| 174 | + let r = arrow_cast::cast(&decimal128_array, &bench.1); |
| 175 | + std::hint::black_box(r) |
| 176 | + }) |
| 177 | + }); |
| 178 | + } |
| 179 | + |
| 180 | + let decimal256_array = Decimal256Array::from(vec![ |
| 181 | + Some(i256::from_i128(2000)), |
| 182 | + Some(i256::from_i128(234000)), |
| 183 | + None, |
| 184 | + Some(i256::from_i128(1234100)), |
| 185 | + Some(i256::from_i128(-12342)), |
| 186 | + ]) |
| 187 | + .with_precision_and_scale(76, 4) |
| 188 | + .unwrap(); |
| 189 | + let bench_suite_decimal256 = [ |
| 190 | + ("decimal256(76, 4)_to_float32", Float32), |
| 191 | + ("decimal256(76, 4)_to_float64", Float64), |
| 192 | + ]; |
| 193 | + for bench in &bench_suite_decimal256 { |
| 194 | + c.bench_function(bench.0, |b| { |
| 195 | + b.iter(|| { |
| 196 | + let r = arrow_cast::cast(&decimal256_array, &bench.1); |
| 197 | + std::hint::black_box(r) |
| 198 | + }) |
| 199 | + }); |
| 200 | + } |
| 201 | +} |
| 202 | + |
| 203 | +fn cast_decimal_to_integer(c: &mut Criterion) { |
| 204 | + let decimal32_array = Decimal32Array::from(vec![Some(12345), Some(23400), None, Some(-12342)]) |
| 205 | + .with_precision_and_scale(9, 2) |
| 206 | + .unwrap(); |
| 207 | + |
| 208 | + let bench_suite_decimal32 = [ |
| 209 | + ("decimal32(9, 2)_to_int8", Int8), |
| 210 | + ("decimal32(9, 2)_to_int16", Int16), |
| 211 | + ("decimal32(9, 2)_to_int32", Int32), |
| 212 | + ("decimal32(9, 2)_to_int64", Int64), |
| 213 | + ("decimal32(9, 2)_to_uint8", UInt8), |
| 214 | + ("decimal32(9, 2)_to_uint16", UInt16), |
| 215 | + ("decimal32(9, 2)_to_uint32", UInt32), |
| 216 | + ("decimal32(9, 2)_to_uint64", UInt64), |
| 217 | + ]; |
| 218 | + |
| 219 | + for bench in &bench_suite_decimal32 { |
| 220 | + c.bench_function(bench.0, |b| { |
| 221 | + b.iter(|| { |
| 222 | + let r = arrow_cast::cast(&decimal32_array, &bench.1); |
| 223 | + std::hint::black_box(r) |
| 224 | + }) |
| 225 | + }); |
| 226 | + } |
| 227 | + |
| 228 | + let decimal64_array = Decimal64Array::from(vec![Some(12345), Some(23400), None, Some(-12342)]) |
| 229 | + .with_precision_and_scale(18, 2) |
| 230 | + .unwrap(); |
| 231 | + |
| 232 | + let bench_suite_decimal64 = [ |
| 233 | + ("decimal64(18, 2)_to_int8", Int8), |
| 234 | + ("decimal64(18, 2)_to_int16", Int16), |
| 235 | + ("decimal64(18, 2)_to_int32", Int32), |
| 236 | + ("decimal64(18, 2)_to_int64", Int64), |
| 237 | + ("decimal64(18, 2)_to_uint8", UInt8), |
| 238 | + ("decimal64(18, 2)_to_uint16", UInt16), |
| 239 | + ("decimal64(18, 2)_to_uint32", UInt32), |
| 240 | + ("decimal64(18, 2)_to_uint64", UInt64), |
| 241 | + ]; |
| 242 | + |
| 243 | + for bench in &bench_suite_decimal64 { |
| 244 | + c.bench_function(bench.0, |b| { |
| 245 | + b.iter(|| { |
| 246 | + let r = arrow_cast::cast(&decimal64_array, &bench.1); |
| 247 | + std::hint::black_box(r) |
| 248 | + }) |
| 249 | + }); |
| 250 | + } |
| 251 | + |
| 252 | + let decimal128_array = |
| 253 | + Decimal128Array::from(vec![Some(12345), Some(23400), None, Some(-12342)]) |
| 254 | + .with_precision_and_scale(38, 3) |
| 255 | + .unwrap(); |
| 256 | + |
| 257 | + let bench_suite_decimal128 = [ |
| 258 | + ("decimal128(38, 3)_to_int8", Int8), |
| 259 | + ("decimal128(38, 3)_to_int16", Int16), |
| 260 | + ("decimal128(38, 3)_to_int32", Int32), |
| 261 | + ("decimal128(38, 3)_to_int64", Int64), |
| 262 | + ("decimal128(38, 3)_to_uint8", UInt8), |
| 263 | + ("decimal128(38, 3)_to_uint16", UInt16), |
| 264 | + ("decimal128(38, 3)_to_uint32", UInt32), |
| 265 | + ("decimal128(38, 3)_to_uint64", UInt64), |
| 266 | + ]; |
| 267 | + |
| 268 | + for bench in &bench_suite_decimal128 { |
| 269 | + c.bench_function(bench.0, |b| { |
| 270 | + b.iter(|| { |
| 271 | + let r = arrow_cast::cast(&decimal128_array, &bench.1); |
| 272 | + std::hint::black_box(r) |
| 273 | + }) |
| 274 | + }); |
| 275 | + } |
| 276 | + let decimal256_array = Decimal256Array::from(vec![ |
| 277 | + Some(i256::from(12345)), |
| 278 | + Some(i256::from(23400)), |
| 279 | + None, |
| 280 | + Some(i256::from(-12342)), |
| 281 | + ]) |
| 282 | + .with_precision_and_scale(76, 4) |
| 283 | + .unwrap(); |
| 284 | + |
| 285 | + let bench_suite_decimal256 = [ |
| 286 | + ("decimal256(76, 4)_to_int8", Int8), |
| 287 | + ("decimal256(76, 4)_to_int16", Int16), |
| 288 | + ("decimal256(76, 4)_to_int32", Int32), |
| 289 | + ("decimal256(76, 4)_to_int64", Int64), |
| 290 | + ("decimal256(76, 4)_to_uint8", UInt8), |
| 291 | + ("decimal256(76, 4)_to_uint16", UInt16), |
| 292 | + ("decimal256(76, 4)_to_uint32", UInt32), |
| 293 | + ("decimal256(76, 4)_to_uint64", UInt64), |
| 294 | + ]; |
| 295 | + |
| 296 | + for bench in &bench_suite_decimal256 { |
| 297 | + c.bench_function(bench.0, |b| { |
| 298 | + b.iter(|| { |
| 299 | + let r = arrow_cast::cast(&decimal256_array, &bench.1); |
| 300 | + std::hint::black_box(r) |
| 301 | + }) |
| 302 | + }); |
| 303 | + } |
| 304 | +} |
| 305 | +criterion_group!( |
| 306 | + benches, |
| 307 | + cast_string_from_or_to_decimals, |
| 308 | + cast_float_to_decimals, |
| 309 | + cast_decimal_to_float, |
| 310 | + cast_decimal_to_integer |
| 311 | +); |
| 312 | +criterion_main!(benches); |
0 commit comments