|
| 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::Int32Builder, RecordBatch}; |
| 19 | +use arrow_schema::{DataType, Field, Schema}; |
| 20 | +use comet::execution::datafusion::expressions::cast::{Cast, EvalMode}; |
| 21 | +use criterion::{criterion_group, criterion_main, Criterion}; |
| 22 | +use datafusion_physical_expr::{expressions::Column, PhysicalExpr}; |
| 23 | +use std::sync::Arc; |
| 24 | + |
| 25 | +fn criterion_benchmark(c: &mut Criterion) { |
| 26 | + let batch = create_int32_batch(); |
| 27 | + let expr = Arc::new(Column::new("a", 0)); |
| 28 | + let timezone = "".to_string(); |
| 29 | + let cast_i32_to_i8 = Cast::new( |
| 30 | + expr.clone(), |
| 31 | + DataType::Int8, |
| 32 | + EvalMode::Legacy, |
| 33 | + timezone.clone(), |
| 34 | + ); |
| 35 | + let cast_i32_to_i16 = Cast::new( |
| 36 | + expr.clone(), |
| 37 | + DataType::Int16, |
| 38 | + EvalMode::Legacy, |
| 39 | + timezone.clone(), |
| 40 | + ); |
| 41 | + let cast_i32_to_i64 = Cast::new(expr, DataType::Int64, EvalMode::Legacy, timezone); |
| 42 | + |
| 43 | + let mut group = c.benchmark_group("cast_int_to_int"); |
| 44 | + group.bench_function("cast_i32_to_i8", |b| { |
| 45 | + b.iter(|| cast_i32_to_i8.evaluate(&batch).unwrap()); |
| 46 | + }); |
| 47 | + group.bench_function("cast_i32_to_i16", |b| { |
| 48 | + b.iter(|| cast_i32_to_i16.evaluate(&batch).unwrap()); |
| 49 | + }); |
| 50 | + group.bench_function("cast_i32_to_i64", |b| { |
| 51 | + b.iter(|| cast_i32_to_i64.evaluate(&batch).unwrap()); |
| 52 | + }); |
| 53 | +} |
| 54 | + |
| 55 | +fn create_int32_batch() -> RecordBatch { |
| 56 | + let schema = Arc::new(Schema::new(vec![Field::new("a", DataType::Int32, true)])); |
| 57 | + let mut b = Int32Builder::new(); |
| 58 | + for i in 0..1000 { |
| 59 | + if i % 10 == 0 { |
| 60 | + b.append_null(); |
| 61 | + } else { |
| 62 | + b.append_value(rand::random::<i32>()); |
| 63 | + } |
| 64 | + } |
| 65 | + let array = b.finish(); |
| 66 | + let batch = RecordBatch::try_new(schema.clone(), vec![Arc::new(array)]).unwrap(); |
| 67 | + batch |
| 68 | +} |
| 69 | + |
| 70 | +fn config() -> Criterion { |
| 71 | + Criterion::default() |
| 72 | +} |
| 73 | + |
| 74 | +criterion_group! { |
| 75 | + name = benches; |
| 76 | + config = config(); |
| 77 | + targets = criterion_benchmark |
| 78 | +} |
| 79 | +criterion_main!(benches); |
0 commit comments