|
| 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::{Int16Builder, Int32Builder, Int64Builder, Int8Builder}; |
| 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 | + // Test with UTC timezone |
| 30 | + let spark_cast_options = SparkCastOptions::new(EvalMode::Legacy, "UTC", false); |
| 31 | + let timestamp_type = DataType::Timestamp(TimeUnit::Microsecond, Some("UTC".into())); |
| 32 | + |
| 33 | + let mut group = c.benchmark_group("cast_int_to_timestamp"); |
| 34 | + |
| 35 | + // Int8 -> Timestamp |
| 36 | + let batch_i8 = create_int8_batch(); |
| 37 | + let expr_i8 = Arc::new(Column::new("a", 0)); |
| 38 | + let cast_i8_to_ts = Cast::new(expr_i8, timestamp_type.clone(), spark_cast_options.clone()); |
| 39 | + group.bench_function("cast_i8_to_timestamp", |b| { |
| 40 | + b.iter(|| cast_i8_to_ts.evaluate(&batch_i8).unwrap()); |
| 41 | + }); |
| 42 | + |
| 43 | + // Int16 -> Timestamp |
| 44 | + let batch_i16 = create_int16_batch(); |
| 45 | + let expr_i16 = Arc::new(Column::new("a", 0)); |
| 46 | + let cast_i16_to_ts = Cast::new(expr_i16, timestamp_type.clone(), spark_cast_options.clone()); |
| 47 | + group.bench_function("cast_i16_to_timestamp", |b| { |
| 48 | + b.iter(|| cast_i16_to_ts.evaluate(&batch_i16).unwrap()); |
| 49 | + }); |
| 50 | + |
| 51 | + // Int32 -> Timestamp |
| 52 | + let batch_i32 = create_int32_batch(); |
| 53 | + let expr_i32 = Arc::new(Column::new("a", 0)); |
| 54 | + let cast_i32_to_ts = Cast::new(expr_i32, timestamp_type.clone(), spark_cast_options.clone()); |
| 55 | + group.bench_function("cast_i32_to_timestamp", |b| { |
| 56 | + b.iter(|| cast_i32_to_ts.evaluate(&batch_i32).unwrap()); |
| 57 | + }); |
| 58 | + |
| 59 | + // Int64 -> Timestamp |
| 60 | + let batch_i64 = create_int64_batch(); |
| 61 | + let expr_i64 = Arc::new(Column::new("a", 0)); |
| 62 | + let cast_i64_to_ts = Cast::new(expr_i64, timestamp_type.clone(), spark_cast_options.clone()); |
| 63 | + group.bench_function("cast_i64_to_timestamp", |b| { |
| 64 | + b.iter(|| cast_i64_to_ts.evaluate(&batch_i64).unwrap()); |
| 65 | + }); |
| 66 | + |
| 67 | + group.finish(); |
| 68 | +} |
| 69 | + |
| 70 | +fn create_int8_batch() -> RecordBatch { |
| 71 | + let schema = Arc::new(Schema::new(vec![Field::new("a", DataType::Int8, true)])); |
| 72 | + let mut b = Int8Builder::with_capacity(BATCH_SIZE); |
| 73 | + for i in 0..BATCH_SIZE { |
| 74 | + if i % 10 == 0 { |
| 75 | + b.append_null(); |
| 76 | + } else { |
| 77 | + b.append_value(rand::random::<i8>()); |
| 78 | + } |
| 79 | + } |
| 80 | + RecordBatch::try_new(schema, vec![Arc::new(b.finish())]).unwrap() |
| 81 | +} |
| 82 | + |
| 83 | +fn create_int16_batch() -> RecordBatch { |
| 84 | + let schema = Arc::new(Schema::new(vec![Field::new("a", DataType::Int16, true)])); |
| 85 | + let mut b = Int16Builder::with_capacity(BATCH_SIZE); |
| 86 | + for i in 0..BATCH_SIZE { |
| 87 | + if i % 10 == 0 { |
| 88 | + b.append_null(); |
| 89 | + } else { |
| 90 | + b.append_value(rand::random::<i16>()); |
| 91 | + } |
| 92 | + } |
| 93 | + RecordBatch::try_new(schema, vec![Arc::new(b.finish())]).unwrap() |
| 94 | +} |
| 95 | + |
| 96 | +fn create_int32_batch() -> RecordBatch { |
| 97 | + let schema = Arc::new(Schema::new(vec![Field::new("a", DataType::Int32, true)])); |
| 98 | + let mut b = Int32Builder::with_capacity(BATCH_SIZE); |
| 99 | + for i in 0..BATCH_SIZE { |
| 100 | + if i % 10 == 0 { |
| 101 | + b.append_null(); |
| 102 | + } else { |
| 103 | + b.append_value(rand::random::<i32>()); |
| 104 | + } |
| 105 | + } |
| 106 | + RecordBatch::try_new(schema, vec![Arc::new(b.finish())]).unwrap() |
| 107 | +} |
| 108 | + |
| 109 | +fn create_int64_batch() -> RecordBatch { |
| 110 | + let schema = Arc::new(Schema::new(vec![Field::new("a", DataType::Int64, true)])); |
| 111 | + let mut b = Int64Builder::with_capacity(BATCH_SIZE); |
| 112 | + for i in 0..BATCH_SIZE { |
| 113 | + if i % 10 == 0 { |
| 114 | + b.append_null(); |
| 115 | + } else { |
| 116 | + b.append_value(rand::random::<i64>()); |
| 117 | + } |
| 118 | + } |
| 119 | + RecordBatch::try_new(schema, vec![Arc::new(b.finish())]).unwrap() |
| 120 | +} |
| 121 | + |
| 122 | +fn config() -> Criterion { |
| 123 | + Criterion::default() |
| 124 | +} |
| 125 | + |
| 126 | +criterion_group! { |
| 127 | + name = benches; |
| 128 | + config = config(); |
| 129 | + targets = criterion_benchmark |
| 130 | +} |
| 131 | +criterion_main!(benches); |
0 commit comments