|
| 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::builder::{BooleanBuilder, Decimal128Builder, Float32Builder, Float64Builder}; |
| 19 | +use arrow::array::RecordBatch; |
| 20 | +use arrow::datatypes::{DataType, Field, Schema, TimeUnit}; |
| 21 | +use criterion::{criterion_group, criterion_main, Criterion}; |
| 22 | +use datafusion::physical_expr::{expressions::Column, PhysicalExpr}; |
| 23 | +use datafusion_comet_spark_expr::{Cast, EvalMode, SparkCastOptions}; |
| 24 | +use std::sync::Arc; |
| 25 | + |
| 26 | +const BATCH_SIZE: usize = 8192; |
| 27 | + |
| 28 | +fn criterion_benchmark(c: &mut Criterion) { |
| 29 | + let spark_cast_options = SparkCastOptions::new(EvalMode::Legacy, "UTC", false); |
| 30 | + let timestamp_type = DataType::Timestamp(TimeUnit::Microsecond, Some("UTC".into())); |
| 31 | + |
| 32 | + let mut group = c.benchmark_group("cast_non_int_numeric_to_timestamp"); |
| 33 | + |
| 34 | + // Float32 -> Timestamp |
| 35 | + let batch_f32 = create_float32_batch(); |
| 36 | + let expr_f32 = Arc::new(Column::new("a", 0)); |
| 37 | + let cast_f32_to_ts = Cast::new(expr_f32, timestamp_type.clone(), spark_cast_options.clone()); |
| 38 | + group.bench_function("cast_f32_to_timestamp", |b| { |
| 39 | + b.iter(|| cast_f32_to_ts.evaluate(&batch_f32).unwrap()); |
| 40 | + }); |
| 41 | + |
| 42 | + // Float64 -> Timestamp |
| 43 | + let batch_f64 = create_float64_batch(); |
| 44 | + let expr_f64 = Arc::new(Column::new("a", 0)); |
| 45 | + let cast_f64_to_ts = Cast::new(expr_f64, timestamp_type.clone(), spark_cast_options.clone()); |
| 46 | + group.bench_function("cast_f64_to_timestamp", |b| { |
| 47 | + b.iter(|| cast_f64_to_ts.evaluate(&batch_f64).unwrap()); |
| 48 | + }); |
| 49 | + |
| 50 | + // Boolean -> Timestamp |
| 51 | + let batch_bool = create_boolean_batch(); |
| 52 | + let expr_bool = Arc::new(Column::new("a", 0)); |
| 53 | + let cast_bool_to_ts = Cast::new( |
| 54 | + expr_bool, |
| 55 | + timestamp_type.clone(), |
| 56 | + spark_cast_options.clone(), |
| 57 | + ); |
| 58 | + group.bench_function("cast_bool_to_timestamp", |b| { |
| 59 | + b.iter(|| cast_bool_to_ts.evaluate(&batch_bool).unwrap()); |
| 60 | + }); |
| 61 | + |
| 62 | + // Decimal128 -> Timestamp |
| 63 | + let batch_decimal = create_decimal128_batch(); |
| 64 | + let expr_decimal = Arc::new(Column::new("a", 0)); |
| 65 | + let cast_decimal_to_ts = Cast::new( |
| 66 | + expr_decimal, |
| 67 | + timestamp_type.clone(), |
| 68 | + spark_cast_options.clone(), |
| 69 | + ); |
| 70 | + group.bench_function("cast_decimal_to_timestamp", |b| { |
| 71 | + b.iter(|| cast_decimal_to_ts.evaluate(&batch_decimal).unwrap()); |
| 72 | + }); |
| 73 | + |
| 74 | + group.finish(); |
| 75 | +} |
| 76 | + |
| 77 | +fn create_float32_batch() -> RecordBatch { |
| 78 | + let schema = Arc::new(Schema::new(vec![Field::new("a", DataType::Float32, true)])); |
| 79 | + let mut b = Float32Builder::with_capacity(BATCH_SIZE); |
| 80 | + for i in 0..BATCH_SIZE { |
| 81 | + if i % 10 == 0 { |
| 82 | + b.append_null(); |
| 83 | + } else { |
| 84 | + b.append_value(rand::random::<f32>()); |
| 85 | + } |
| 86 | + } |
| 87 | + RecordBatch::try_new(schema, vec![Arc::new(b.finish())]).unwrap() |
| 88 | +} |
| 89 | + |
| 90 | +fn create_float64_batch() -> RecordBatch { |
| 91 | + let schema = Arc::new(Schema::new(vec![Field::new("a", DataType::Float64, true)])); |
| 92 | + let mut b = Float64Builder::with_capacity(BATCH_SIZE); |
| 93 | + for i in 0..BATCH_SIZE { |
| 94 | + if i % 10 == 0 { |
| 95 | + b.append_null(); |
| 96 | + } else { |
| 97 | + b.append_value(rand::random::<f64>()); |
| 98 | + } |
| 99 | + } |
| 100 | + RecordBatch::try_new(schema, vec![Arc::new(b.finish())]).unwrap() |
| 101 | +} |
| 102 | + |
| 103 | +fn create_boolean_batch() -> RecordBatch { |
| 104 | + let schema = Arc::new(Schema::new(vec![Field::new("a", DataType::Boolean, true)])); |
| 105 | + let mut b = BooleanBuilder::with_capacity(BATCH_SIZE); |
| 106 | + for i in 0..BATCH_SIZE { |
| 107 | + if i % 10 == 0 { |
| 108 | + b.append_null(); |
| 109 | + } else { |
| 110 | + b.append_value(rand::random::<bool>()); |
| 111 | + } |
| 112 | + } |
| 113 | + RecordBatch::try_new(schema, vec![Arc::new(b.finish())]).unwrap() |
| 114 | +} |
| 115 | + |
| 116 | +fn create_decimal128_batch() -> RecordBatch { |
| 117 | + let schema = Arc::new(Schema::new(vec![Field::new( |
| 118 | + "a", |
| 119 | + DataType::Decimal128(18, 6), |
| 120 | + true, |
| 121 | + )])); |
| 122 | + let mut b = Decimal128Builder::with_capacity(BATCH_SIZE); |
| 123 | + for i in 0..BATCH_SIZE { |
| 124 | + if i % 10 == 0 { |
| 125 | + b.append_null(); |
| 126 | + } else { |
| 127 | + b.append_value(i as i128 * 1_000_000); |
| 128 | + } |
| 129 | + } |
| 130 | + let array = b.finish().with_precision_and_scale(18, 6).unwrap(); |
| 131 | + RecordBatch::try_new(schema, vec![Arc::new(array)]).unwrap() |
| 132 | +} |
| 133 | + |
| 134 | +fn config() -> Criterion { |
| 135 | + Criterion::default() |
| 136 | +} |
| 137 | + |
| 138 | +criterion_group! { |
| 139 | + name = benches; |
| 140 | + config = config(); |
| 141 | + targets = criterion_benchmark |
| 142 | +} |
| 143 | +criterion_main!(benches); |
0 commit comments