## Coverage Report

#### Created: 2025-11-22 06:00

next uncovered line (L), next uncovered region (R), next uncovered branch (B)

    /Users/andrewlamb/Software/datafusion/datafusion/physical-expr/src/expressions/binary.rs

    Line
    Count
    Source
    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
    mod kernels;
    19
    20
    use crate::intervals::cp_solver::{propagate_arithmetic, propagate_comparison};
    21
    use crate::PhysicalExpr;
    22
    use std::hash::Hash;
    23
    use std::{any::Any, sync::Arc};
    24
    25
    use arrow::array::*;
    26
    use arrow::compute::kernels::boolean::{and_kleene, or_kleene};
    27
    use arrow::compute::kernels::concat_elements::concat_elements_utf8;
    28
    use arrow::compute::{cast, filter_record_batch, SlicesIterator};
    29
    use arrow::datatypes::*;
    30
    use arrow::error::ArrowError;
    31
    use datafusion_common::cast::as_boolean_array;
    32
    use datafusion_common::{internal_err, not_impl_err, Result, ScalarValue};
    33
    use datafusion_expr::binary::BinaryTypeCoercer;
    34
    use datafusion_expr::interval_arithmetic::{apply_operator, Interval};
    35
    use datafusion_expr::sort_properties::ExprProperties;
    36
    use datafusion_expr::statistics::Distribution::{Bernoulli, Gaussian};
    37
    use datafusion_expr::statistics::{
    38
        combine_bernoullis, combine_gaussians, create_bernoulli_from_comparison,
    39
        new_generic_from_binary_op, Distribution,
    40
    };
    41
    use datafusion_expr::{ColumnarValue, Operator};
    42
    use datafusion_physical_expr_common::datum::{apply, apply_cmp};
    43
    44
    use kernels::{
    45
        bitwise_and_dyn, bitwise_and_dyn_scalar, bitwise_or_dyn, bitwise_or_dyn_scalar,
    46
        bitwise_shift_left_dyn, bitwise_shift_left_dyn_scalar, bitwise_shift_right_dyn,
    47
        bitwise_shift_right_dyn_scalar, bitwise_xor_dyn, bitwise_xor_dyn_scalar,
    48
        concat_elements_utf8view, regex_match_dyn, regex_match_dyn_scalar,
    49
    };
    50
    51
    /// Binary expression
    52
    #[derive(Debug, Clone, Eq)]
    53
    pub struct BinaryExpr {
    54
        left: Arc<dyn PhysicalExpr>,
    55
        op: Operator,
    56
        right: Arc<dyn PhysicalExpr>,
    57
        /// Specifies whether an error is returned on overflow or not
    58
        fail_on_overflow: bool,
    59
    }
    60
    61
    // Manually derive PartialEq and Hash to work around https://github.com/rust-lang/rust/issues/78808
    62
    impl PartialEq for BinaryExpr {
    63
    0
        fn eq(&self, other: &Self) -> bool {
    64
    0
            self.left.eq(&other.left)
    65
    0
                && self.op.eq(&other.op)
    66
    0
                && self.right.eq(&other.right)
    67
    0
                && self.fail_on_overflow.eq(&other.fail_on_overflow)
    68
    0
        }
    69
    }
    70
    impl Hash for BinaryExpr {
    71
    0
        fn hash<H: std::hash::Hasher>(&self, state: &mut H) {
    72
    0
            self.left.hash(state);
    73
    0
            self.op.hash(state);
    74
    0
            self.right.hash(state);
    75
    0
            self.fail_on_overflow.hash(state);
    76
    0
        }
    77
    }
    78
    79
    impl BinaryExpr {
    80
        /// Create new binary expression
    81
    0
        pub fn new(
    82
    0
            left: Arc<dyn PhysicalExpr>,
    83
    0
            op: Operator,
    84
    0
            right: Arc<dyn PhysicalExpr>,
    85
    0
        ) -> Self {
    86
    0
            Self {
    87
    0
                left,
    88
    0
                op,
    89
    0
                right,
    90
    0
                fail_on_overflow: false,
    91
    0
            }
    92
    0
        }
    93
    94
        /// Create new binary expression with explicit fail_on_overflow value
    95
    0
        pub fn with_fail_on_overflow(self, fail_on_overflow: bool) -> Self {
    96
    0
            Self {
    97
    0
                left: self.left,
    98
    0
                op: self.op,
    99
    0
                right: self.right,
    100
    0
                fail_on_overflow,
    101
    0
            }
    102
    0
        }
    103
    104
        /// Get the left side of the binary expression
    105
    0
        pub fn left(&self) -> &Arc<dyn PhysicalExpr> {
    106
    0
            &self.left
    107
    0
        }
    108
    109
        /// Get the right side of the binary expression
    110
    0
        pub fn right(&self) -> &Arc<dyn PhysicalExpr> {
    111
    0
            &self.right
    112
    0
        }
    113
    114
        /// Get the operator for this binary expression
    115
    0
        pub fn op(&self) -> &Operator {
    116
    0
            &self.op
    117
    0
        }
    118
    }
    119
    120
    impl std::fmt::Display for BinaryExpr {
    121
    0
        fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
    122
            // Put parentheses around child binary expressions so that we can see the difference
    123
            // between `(a OR b) AND c` and `a OR (b AND c)`. We only insert parentheses when needed,
    124
            // based on operator precedence. For example, `(a AND b) OR c` and `a AND b OR c` are
    125
            // equivalent and the parentheses are not necessary.
    126
    127
    0
            fn write_child(
    128
    0
                f: &mut std::fmt::Formatter,
    129
    0
                expr: &dyn PhysicalExpr,
    130
    0
                precedence: u8,
    131
    0
            ) -> std::fmt::Result {
    132
    0
                if let Some(child) = expr.as_any().downcast_ref::<BinaryExpr>() {
    133
    0
                    let p = child.op.precedence();
    134
    0
                    if p == 0 || p < precedence {
    135
    0
                        write!(f, "({child})")?;
    136
                    } else {
    137
    0
                        write!(f, "{child}")?;
    138
                    }
    139
                } else {
    140
    0
                    write!(f, "{expr}")?;
    141
                }
    142
    143
    0
                Ok(())
    144
    0
            }
    145
    146
    0
            let precedence = self.op.precedence();
    147
    0
            write_child(f, self.left.as_ref(), precedence)?;
    148
    0
            write!(f, " {} ", self.op)?;
    149
    0
            write_child(f, self.right.as_ref(), precedence)
    150
    0
        }
    151
    }
    152
    153
    /// Invoke a boolean kernel on a pair of arrays
    154
    #[inline]
    155
    0
    fn boolean_op(
    156
    0
        left: &dyn Array,
    157
    0
        right: &dyn Array,
    158
    0
        op: impl FnOnce(&BooleanArray, &BooleanArray) -> Result<BooleanArray, ArrowError>,
    159
    0
    ) -> Result<Arc<dyn Array + 'static>, ArrowError> {
    160
    0
        let ll = as_boolean_array(left).expect("boolean_op failed to downcast left array");
    161
    0
        let rr = as_boolean_array(right).expect("boolean_op failed to downcast right array");
    162
    0
        op(ll, rr).map(|t| Arc::new(t) as _)
    163
    0
    }
    164
    165
    impl PhysicalExpr for BinaryExpr {
    166
        /// Return a reference to Any that can be used for downcasting
    167
    0
        fn as_any(&self) -> &dyn Any {
    168
    0
            self
    169
    0
        }
    170
    171
    0
        fn data_type(&self, input_schema: &Schema) -> Result<DataType> {
    172
    0
            BinaryTypeCoercer::new(
    173
    0
                &self.left.data_type(input_schema)?,
    174
    0
                &self.op,
    175
    0
                &self.right.data_type(input_schema)?,
    176
            )
    177
    0
            .get_result_type()
    178
    0
        }
    179
    180
    0
        fn nullable(&self, input_schema: &Schema) -> Result<bool> {
    181
    0
            Ok(self.left.nullable(input_schema)? || self.right.nullable(input_schema)?)
    182
    0
        }
    183
    184
    0
        fn evaluate(&self, batch: &RecordBatch) -> Result<ColumnarValue> {
    185
            use arrow::compute::kernels::numeric::*;
    186
    187
            // Evaluate left-hand side expression.
    188
    0
            let lhs = self.left.evaluate(batch)?;
    189
    190
            // Check if we can apply short-circuit evaluation.
    191
    0
            match check_short_circuit(&lhs, &self.op) {
    192
    0
                ShortCircuitStrategy::None => {}
    193
    0
                ShortCircuitStrategy::ReturnLeft => return Ok(lhs),
    194
                ShortCircuitStrategy::ReturnRight => {
    195
    0
                    let rhs = self.right.evaluate(batch)?;
    196
    0
                    return Ok(rhs);
    197
                }
    198
    0
                ShortCircuitStrategy::PreSelection(selection) => {
    199
                    // The function `evaluate_selection` was not called for filtering and calculation,
    200
                    // as it takes into account cases where the selection contains null values.
    201
    0
                    let batch = filter_record_batch(batch, selection)?;
    202
    0
                    let right_ret = self.right.evaluate(&batch)?;
    203
    204
    0
                    match &right_ret {
    205
    0
                        ColumnarValue::Array(array) => {
    206
                            // When the array on the right is all true or all false, skip the scatter process
    207
    0
                            let boolean_array = array.as_boolean();
    208
    0
                            let true_count = boolean_array.true_count();
    209
    0
                            let length = boolean_array.len();
    210
    0
                            if true_count == length {
    211
    0
                                return Ok(lhs);
    212
    0
                            } else if true_count == 0 && boolean_array.null_count() == 0 {
    213
                                // If the right-hand array is returned at this point,the lengths will be inconsistent;
    214
                                // returning a scalar can avoid this issue
    215
    0
                                return Ok(ColumnarValue::Scalar(ScalarValue::Boolean(
    216
    0
                                    Some(false),
    217
    0
                                )));
    218
    0
                            }
    219
    220
    0
                            return pre_selection_scatter(selection, Some(boolean_array));
    221
                        }
    222
    0
                        ColumnarValue::Scalar(scalar) => {
    223
    0
                            if let ScalarValue::Boolean(v) = scalar {
    224
                                // When the scalar is true or false, skip the scatter process
    225
    0
                                if let Some(v) = v {
    226
    0
                                    if *v {
    227
    0
                                        return Ok(lhs);
    228
                                    } else {
    229
    0
                                        return Ok(right_ret);
    230
                                    }
    231
                                } else {
    232
    0
                                    return pre_selection_scatter(selection, None);
    233
                                }
    234
                            } else {
    235
    0
                                return internal_err!(
    236
    0
                                    "Expected boolean scalar value, found: {right_ret:?}"
    237
                                );
    238
                            }
    239
                        }
    240
                    }
    241
                }
    242
            }
    243
    244
    0
            let rhs = self.right.evaluate(batch)?;
    245
    0
            let left_data_type = lhs.data_type();
    246
    0
            let right_data_type = rhs.data_type();
    247
    248
    0
            let schema = batch.schema();
    249
    0
            let input_schema = schema.as_ref();
    250
    251
    0
            match self.op {
    252
    0
                Operator::Plus if self.fail_on_overflow => return apply(&lhs, &rhs, add),
    253
    0
                Operator::Plus => return apply(&lhs, &rhs, add_wrapping),
    254
    0
                Operator::Minus if self.fail_on_overflow => return apply(&lhs, &rhs, sub),
    255
    0
                Operator::Minus => return apply(&lhs, &rhs, sub_wrapping),
    256
    0
                Operator::Multiply if self.fail_on_overflow => return apply(&lhs, &rhs, mul),
    257
    0
                Operator::Multiply => return apply(&lhs, &rhs, mul_wrapping),
    258
    0
                Operator::Divide => return apply(&lhs, &rhs, div),
    259
    0
                Operator::Modulo => return apply(&lhs, &rhs, rem),
    260
    261
                Operator::Eq
    262
                | Operator::NotEq
    263
                | Operator::Lt
    264
                | Operator::Gt
    265
                | Operator::LtEq
    266
                | Operator::GtEq
    267
                | Operator::IsDistinctFrom
    268
                | Operator::IsNotDistinctFrom
    269
                | Operator::LikeMatch
    270
                | Operator::ILikeMatch
    271
                | Operator::NotLikeMatch
    272
                | Operator::NotILikeMatch => {
    273
    0
                    return apply_cmp(self.op, &lhs, &rhs);
    274
                }
    275
    0
                _ => {}
    276
            }
    277
    278
    0
            let result_type = self.data_type(input_schema)?;
    279
    280
            // If the left-hand side is an array and the right-hand side is a non-null scalar, try the optimized kernel.
    281
    0
            if let (ColumnarValue::Array(array), ColumnarValue::Scalar(ref scalar)) =
    282
    0
                (&lhs, &rhs)
    283
            {
    284
    0
                if !scalar.is_null() {
    285
    0
                    if let Some(result_array) =
    286
    0
                        self.evaluate_array_scalar(array, scalar.clone())?
    287
                    {
    288
    0
                        let final_array = result_array
    289
    0
                            .and_then(|a| to_result_type_array(&self.op, a, &result_type));
    290
    0
                        return final_array.map(ColumnarValue::Array);
    291
    0
                    }
    292
    0
                }
    293
    0
            }
    294
    295
            // if both arrays or both literals - extract arrays and continue execution
    296
    0
            let (left, right) = (
    297
    0
                lhs.into_array(batch.num_rows())?,
    298
    0
                rhs.into_array(batch.num_rows())?,
    299
            );
    300
    0
            self.evaluate_with_resolved_args(left, &left_data_type, right, &right_data_type)
    301
    0
                .map(ColumnarValue::Array)
    302
    0
        }
    303
    304
    0
        fn children(&self) -> Vec<&Arc<dyn PhysicalExpr>> {
    305
    0
            vec![&self.left, &self.right]
    306
    0
        }
    307
    308
    0
        fn with_new_children(
    309
    0
            self: Arc<Self>,
    310
    0
            children: Vec<Arc<dyn PhysicalExpr>>,
    311
    0
        ) -> Result<Arc<dyn PhysicalExpr>> {
    312
    0
            Ok(Arc::new(
    313
    0
                BinaryExpr::new(Arc::clone(&children[0]), self.op, Arc::clone(&children[1]))
    314
    0
                    .with_fail_on_overflow(self.fail_on_overflow),
    315
    0
            ))
    316
    0
        }
    317
    318
    0
        fn evaluate_bounds(&self, children: &[&Interval]) -> Result<Interval> {
    319
            // Get children intervals:
    320
    0
            let left_interval = children[0];
    321
    0
            let right_interval = children[1];
    322
            // Calculate current node's interval:
    323
    0
            apply_operator(&self.op, left_interval, right_interval)
    324
    0
        }
    325
    326
    0
        fn propagate_constraints(
    327
    0
            &self,
    328
    0
            interval: &Interval,
    329
    0
            children: &[&Interval],
    330
    0
        ) -> Result<Option<Vec<Interval>>> {
    331
            // Get children intervals.
    332
    0
            let left_interval = children[0];
    333
    0
            let right_interval = children[1];
    334
    335
    0
            if self.op.eq(&Operator::And) {
    336
    0
                if interval.eq(&Interval::TRUE) {
    337
                    // A certainly true logical conjunction can only derive from possibly
    338
                    // true operands. Otherwise, we prove infeasibility.
    339
    0
                    Ok((!left_interval.eq(&Interval::FALSE)
    340
    0
                        && !right_interval.eq(&Interval::FALSE))
    341
    0
                    .then(|| vec![Interval::TRUE, Interval::TRUE]))
    342
    0
                } else if interval.eq(&Interval::FALSE) {
    343
                    // If the logical conjunction is certainly false, one of the
    344
                    // operands must be false. However, it's not always possible to
    345
                    // determine which operand is false, leading to different scenarios.
    346
    347
                    // If one operand is certainly true and the other one is uncertain,
    348
                    // then the latter must be certainly false.
    349
    0
                    if left_interval.eq(&Interval::TRUE)
    350
    0
                        && right_interval.eq(&Interval::TRUE_OR_FALSE)
    351
                    {
    352
    0
                        Ok(Some(vec![Interval::TRUE, Interval::FALSE]))
    353
    0
                    } else if right_interval.eq(&Interval::TRUE)
    354
    0
                        && left_interval.eq(&Interval::TRUE_OR_FALSE)
    355
                    {
    356
    0
                        Ok(Some(vec![Interval::FALSE, Interval::TRUE]))
    357
                    }
    358
                    // If both children are uncertain, or if one is certainly false,
    359
                    // we cannot conclusively refine their intervals. In this case,
    360
                    // propagation does not result in any interval changes.
    361
                    else {
    362
    0
                        Ok(Some(vec![]))
    363
                    }
    364
                } else {
    365
                    // An uncertain logical conjunction result can not shrink the
    366
                    // end-points of its children.
    367
    0
                    Ok(Some(vec![]))
    368
                }
    369
    0
            } else if self.op.eq(&Operator::Or) {
    370
    0
                if interval.eq(&Interval::FALSE) {
    371
                    // A certainly false logical disjunction can only derive from certainly
    372
                    // false operands. Otherwise, we prove infeasibility.
    373
    0
                    Ok((!left_interval.eq(&Interval::TRUE)
    374
    0
                        && !right_interval.eq(&Interval::TRUE))
    375
    0
                    .then(|| vec![Interval::FALSE, Interval::FALSE]))
    376
    0
                } else if interval.eq(&Interval::TRUE) {
    377
                    // If the logical disjunction is certainly true, one of the
    378
                    // operands must be true. However, it's not always possible to
    379
                    // determine which operand is true, leading to different scenarios.
    380
    381
                    // If one operand is certainly false and the other one is uncertain,
    382
                    // then the latter must be certainly true.
    383
    0
                    if left_interval.eq(&Interval::FALSE)
    384
    0
                        && right_interval.eq(&Interval::TRUE_OR_FALSE)
    385
                    {
    386
    0
                        Ok(Some(vec![Interval::FALSE, Interval::TRUE]))
    387
    0
                    } else if right_interval.eq(&Interval::FALSE)
    388
    0
                        && left_interval.eq(&Interval::TRUE_OR_FALSE)
    389
                    {
    390
    0
                        Ok(Some(vec![Interval::TRUE, Interval::FALSE]))
    391
                    }
    392
                    // If both children are uncertain, or if one is certainly true,
    393
                    // we cannot conclusively refine their intervals. In this case,
    394
                    // propagation does not result in any interval changes.
    395
                    else {
    396
    0
                        Ok(Some(vec![]))
    397
                    }
    398
                } else {
    399
                    // An uncertain logical disjunction result can not shrink the
    400
                    // end-points of its children.
    401
    0
                    Ok(Some(vec![]))
    402
                }
    403
    0
            } else if self.op.supports_propagation() {
    404
                Ok(
    405
    0
                    propagate_comparison(&self.op, interval, left_interval, right_interval)?
    406
    0
                        .map(|(left, right)| vec![left, right]),
    407
                )
    408
            } else {
    409
                Ok(
    410
    0
                    propagate_arithmetic(&self.op, interval, left_interval, right_interval)?
    411
    0
                        .map(|(left, right)| vec![left, right]),
    412
                )
    413
            }
    414
    0
        }
    415
    416
    0
        fn evaluate_statistics(&self, children: &[&Distribution]) -> Result<Distribution> {
    417
    0
            let (left, right) = (children[0], children[1]);
    418
    419
    0
            if self.op.is_numerical_operators() {
    420
                // We might be able to construct the output statistics more accurately,
    421
                // without falling back to an unknown distribution, if we are dealing
    422
                // with Gaussian distributions and numerical operations.
    423
    0
                if let (Gaussian(left), Gaussian(right)) = (left, right) {
    424
    0
                    if let Some(result) = combine_gaussians(&self.op, left, right)? {
    425
    0
                        return Ok(Gaussian(result));
    426
    0
                    }
    427
    0
                }
    428
    0
            } else if self.op.is_logic_operator() {
    429
                // If we are dealing with logical operators, we expect (and can only
    430
                // operate on) Bernoulli distributions.
    431
    0
                return if let (Bernoulli(left), Bernoulli(right)) = (left, right) {
    432
    0
                    combine_bernoullis(&self.op, left, right).map(Bernoulli)
    433
                } else {
    434
    0
                    internal_err!(
    435
    0
                        "Logical operators are only compatible with `Bernoulli` distributions"
    436
                    )
    437
                };
    438
    0
            } else if self.op.supports_propagation() {
    439
                // If we are handling comparison operators, we expect (and can only
    440
                // operate on) numeric distributions.
    441
    0
                return create_bernoulli_from_comparison(&self.op, left, right);
    442
    0
            }
    443
            // Fall back to an unknown distribution with only summary statistics:
    444
    0
            new_generic_from_binary_op(&self.op, left, right)
    445
    0
        }
    446
    447
        /// For each operator, [`BinaryExpr`] has distinct rules.
    448
        /// TODO: There may be rules specific to some data types and expression ranges.
    449
    0
        fn get_properties(&self, children: &[ExprProperties]) -> Result<ExprProperties> {
    450
    0
            let (l_order, l_range) = (children[0].sort_properties, &children[0].range);
    451
    0
            let (r_order, r_range) = (children[1].sort_properties, &children[1].range);
    452
    0
            match self.op() {
    453
                Operator::Plus => Ok(ExprProperties {
    454
    0
                    sort_properties: l_order.add(&r_order),
    455
    0
                    range: l_range.add(r_range)?,
    456
                    preserves_lex_ordering: false,
    457
                }),
    458
                Operator::Minus => Ok(ExprProperties {
    459
    0
                    sort_properties: l_order.sub(&r_order),
    460
    0
                    range: l_range.sub(r_range)?,
    461
                    preserves_lex_ordering: false,
    462
                }),
    463
                Operator::Gt => Ok(ExprProperties {
    464
    0
                    sort_properties: l_order.gt_or_gteq(&r_order),
    465
    0
                    range: l_range.gt(r_range)?,
    466
                    preserves_lex_ordering: false,
    467
                }),
    468
                Operator::GtEq => Ok(ExprProperties {
    469
    0
                    sort_properties: l_order.gt_or_gteq(&r_order),
    470
    0
                    range: l_range.gt_eq(r_range)?,
    471
                    preserves_lex_ordering: false,
    472
                }),
    473
                Operator::Lt => Ok(ExprProperties {
    474
    0
                    sort_properties: r_order.gt_or_gteq(&l_order),
    475
    0
                    range: l_range.lt(r_range)?,
    476
                    preserves_lex_ordering: false,
    477
                }),
    478
                Operator::LtEq => Ok(ExprProperties {
    479
    0
                    sort_properties: r_order.gt_or_gteq(&l_order),
    480
    0
                    range: l_range.lt_eq(r_range)?,
    481
                    preserves_lex_ordering: false,
    482
                }),
    483
                Operator::And => Ok(ExprProperties {
    484
    0
                    sort_properties: r_order.and_or(&l_order),
    485
    0
                    range: l_range.and(r_range)?,
    486
                    preserves_lex_ordering: false,
    487
                }),
    488
                Operator::Or => Ok(ExprProperties {
    489
    0
                    sort_properties: r_order.and_or(&l_order),
    490
    0
                    range: l_range.or(r_range)?,
    491
                    preserves_lex_ordering: false,
    492
                }),
    493
    0
                _ => Ok(ExprProperties::new_unknown()),
    494
            }
    495
    0
        }
    496
    497
    0
        fn fmt_sql(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
    498
    0
            fn write_child(
    499
    0
                f: &mut std::fmt::Formatter,
    500
    0
                expr: &dyn PhysicalExpr,
    501
    0
                precedence: u8,
    502
    0
            ) -> std::fmt::Result {
    503
    0
                if let Some(child) = expr.as_any().downcast_ref::<BinaryExpr>() {
    504
    0
                    let p = child.op.precedence();
    505
    0
                    if p == 0 || p < precedence {
    506
    0
                        write!(f, "(")?;
    507
    0
                        child.fmt_sql(f)?;
    508
    0
                        write!(f, ")")
    509
                    } else {
    510
    0
                        child.fmt_sql(f)
    511
                    }
    512
                } else {
    513
    0
                    expr.fmt_sql(f)
    514
                }
    515
    0
            }
    516
    517
    0
            let precedence = self.op.precedence();
    518
    0
            write_child(f, self.left.as_ref(), precedence)?;
    519
    0
            write!(f, " {} ", self.op)?;
    520
    0
            write_child(f, self.right.as_ref(), precedence)
    521
    0
        }
    522
    }
    523
    524
    /// Casts dictionary array to result type for binary numerical operators. Such operators
    525
    /// between array and scalar produce a dictionary array other than primitive array of the
    526
    /// same operators between array and array. This leads to inconsistent result types causing
    527
    /// errors in the following query execution. For such operators between array and scalar,
    528
    /// we cast the dictionary array to primitive array.
    529
    0
    fn to_result_type_array(
    530
    0
        op: &Operator,
    531
    0
        array: ArrayRef,
    532
    0
        result_type: &DataType,
    533
    0
    ) -> Result<ArrayRef> {
    534
    0
        if array.data_type() == result_type {
    535
    0
            Ok(array)
    536
    0
        } else if op.is_numerical_operators() {
    537
    0
            match array.data_type() {
    538
    0
                DataType::Dictionary(_, value_type) => {
    539
    0
                    if value_type.as_ref() == result_type {
    540
    0
                        Ok(cast(&array, result_type)?)
    541
                    } else {
    542
    0
                        internal_err!(
    543
    0
                                "Incompatible Dictionary value type {value_type} with result type {result_type} of Binary operator {op:?}"
    544
                            )
    545
                    }
    546
                }
    547
    0
                _ => Ok(array),
    548
            }
    549
        } else {
    550
    0
            Ok(array)
    551
        }
    552
    0
    }
    553
    554
    impl BinaryExpr {
    555
        /// Evaluate the expression of the left input is an array and
    556
        /// right is literal - use scalar operations
    557
    0
        fn evaluate_array_scalar(
    558
    0
            &self,
    559
    0
            array: &dyn Array,
    560
    0
            scalar: ScalarValue,
    561
    0
        ) -> Result<Option<Result<ArrayRef>>> {
    562
            use Operator::*;
    563
    0
            let scalar_result = match &self.op {
    564
    0
                RegexMatch => regex_match_dyn_scalar(array, &scalar, false, false),
    565
    0
                RegexIMatch => regex_match_dyn_scalar(array, &scalar, false, true),
    566
    0
                RegexNotMatch => regex_match_dyn_scalar(array, &scalar, true, false),
    567
    0
                RegexNotIMatch => regex_match_dyn_scalar(array, &scalar, true, true),
    568
    0
                BitwiseAnd => bitwise_and_dyn_scalar(array, scalar),
    569
    0
                BitwiseOr => bitwise_or_dyn_scalar(array, scalar),
    570
    0
                BitwiseXor => bitwise_xor_dyn_scalar(array, scalar),
    571
    0
                BitwiseShiftRight => bitwise_shift_right_dyn_scalar(array, scalar),
    572
    0
                BitwiseShiftLeft => bitwise_shift_left_dyn_scalar(array, scalar),
    573
                // if scalar operation is not supported - fallback to array implementation
    574
    0
                _ => None,
    575
            };
    576
    577
    0
            Ok(scalar_result)
    578
    0
        }
    579
    580
    0
        fn evaluate_with_resolved_args(
    581
    0
            &self,
    582
    0
            left: Arc<dyn Array>,
    583
    0
            left_data_type: &DataType,
    584
    0
            right: Arc<dyn Array>,
    585
    0
            right_data_type: &DataType,
    586
    0
        ) -> Result<ArrayRef> {
    587
            use Operator::*;
    588
    0
            match &self.op {
    589
                IsDistinctFrom | IsNotDistinctFrom | Lt | LtEq | Gt | GtEq | Eq | NotEq
    590
                | Plus | Minus | Multiply | Divide | Modulo | LikeMatch | ILikeMatch
    591
    0
                | NotLikeMatch | NotILikeMatch => unreachable!(),
    592
                And => {
    593
    0
                    if left_data_type == &DataType::Boolean {
    594
    0
                        Ok(boolean_op(&left, &right, and_kleene)?)
    595
                    } else {
    596
    0
                        internal_err!(
    597
    0
                            "Cannot evaluate binary expression {:?} with types {:?} and {:?}",
    598
                            self.op,
    599
    0
                            left.data_type(),
    600
    0
                            right.data_type()
    601
                        )
    602
                    }
    603
                }
    604
                Or => {
    605
    0
                    if left_data_type == &DataType::Boolean {
    606
    0
                        Ok(boolean_op(&left, &right, or_kleene)?)
    607
                    } else {
    608
    0
                        internal_err!(
    609
    0
                            "Cannot evaluate binary expression {:?} with types {:?} and {:?}",
    610
                            self.op,
    611
                            left_data_type,
    612
                            right_data_type
    613
                        )
    614
                    }
    615
                }
    616
    0
                RegexMatch => regex_match_dyn(&left, &right, false, false),
    617
    0
                RegexIMatch => regex_match_dyn(&left, &right, false, true),
    618
    0
                RegexNotMatch => regex_match_dyn(&left, &right, true, false),
    619
    0
                RegexNotIMatch => regex_match_dyn(&left, &right, true, true),
    620
    0
                BitwiseAnd => bitwise_and_dyn(left, right),
    621
    0
                BitwiseOr => bitwise_or_dyn(left, right),
    622
    0
                BitwiseXor => bitwise_xor_dyn(left, right),
    623
    0
                BitwiseShiftRight => bitwise_shift_right_dyn(left, right),
    624
    0
                BitwiseShiftLeft => bitwise_shift_left_dyn(left, right),
    625
    0
                StringConcat => concat_elements(&left, &right),
    626
                AtArrow | ArrowAt | Arrow | LongArrow | HashArrow | HashLongArrow | AtAt
    627
                | HashMinus | AtQuestion | Question | QuestionAnd | QuestionPipe
    628
                | IntegerDivide => {
    629
    0
                    not_impl_err!(
    630
    0
                        "Binary operator '{:?}' is not supported in the physical expr",
    631
                        self.op
    632
                    )
    633
                }
    634
            }
    635
    0
        }
    636
    }
    637
    638
    enum ShortCircuitStrategy<'a> {
    639
        None,
    640
        ReturnLeft,
    641
        ReturnRight,
    642
        PreSelection(&'a BooleanArray),
    643
    }
    644
    645
    /// Based on the results calculated from the left side of the short-circuit operation,
    646
    /// if the proportion of `true` is less than 0.2 and the current operation is an `and`,
    647
    /// the `RecordBatch` will be filtered in advance.
    648
    const PRE_SELECTION_THRESHOLD: f32 = 0.2;
    649
    650
    /// Checks if a logical operator (`AND`/`OR`) can short-circuit evaluation based on the left-hand side (lhs) result.
    651
    ///
    652
    /// Short-circuiting occurs under these circumstances:
    653
    /// - For `AND`:
    654
    ///    - if LHS is all false => short-circuit → return LHS
    655
    ///    - if LHS is all true  => short-circuit → return RHS
    656
    ///    - if LHS is mixed and true_count/sum_count <= [`PRE_SELECTION_THRESHOLD`] -> pre-selection
    657
    /// - For `OR`:
    658
    ///    - if LHS is all true  => short-circuit → return LHS
    659
    ///    - if LHS is all false => short-circuit → return RHS
    660
    /// # Arguments
    661
    /// * `lhs` - The left-hand side (lhs) columnar value (array or scalar)
    662
    /// * `lhs` - The left-hand side (lhs) columnar value (array or scalar)
    663
    /// * `op` - The logical operator (`AND` or `OR`)
    664
    ///
    665
    /// # Implementation Notes
    666
    /// 1. Only works with Boolean-typed arguments (other types automatically return `false`)
    667
    /// 2. Handles both scalar values and array values
    668
    /// 3. For arrays, uses optimized bit counting techniques for boolean arrays
    669
    0
    fn check_short_circuit<'a>(
    670
    0
        lhs: &'a ColumnarValue,
    671
    0
        op: &Operator,
    672
    0
    ) -> ShortCircuitStrategy<'a> {
    673
        // Quick reject for non-logical operators,and quick judgment when op is and
    674
    0
        let is_and = match op {
    675
    0
            Operator::And => true,
    676
    0
            Operator::Or => false,
    677
    0
            _ => return ShortCircuitStrategy::None,
    678
        };
    679
    680
        // Non-boolean types can't be short-circuited
    681
    0
        if lhs.data_type() != DataType::Boolean {
    682
    0
            return ShortCircuitStrategy::None;
    683
    0
        }
    684
    685
    0
        match lhs {
    686
    0
            ColumnarValue::Array(array) => {
    687
                // Fast path for arrays - try to downcast to boolean array
    688
    0
                if let Ok(bool_array) = as_boolean_array(array) {
    689
                    // Arrays with nulls can't be short-circuited
    690
    0
                    if bool_array.null_count() > 0 {
    691
    0
                        return ShortCircuitStrategy::None;
    692
    0
                    }
    693
    694
    0
                    let len = bool_array.len();
    695
    0
                    if len == 0 {
    696
    0
                        return ShortCircuitStrategy::None;
    697
    0
                    }
    698
    699
    0
                    let true_count = bool_array.values().count_set_bits();
    700
    0
                    if is_and {
    701
                        // For AND, prioritize checking for all-false (short circuit case)
    702
                        // Uses optimized false_count() method provided by Arrow
    703
    704
                        // Short circuit if all values are false
    705
    0
                        if true_count == 0 {
    706
    0
                            return ShortCircuitStrategy::ReturnLeft;
    707
    0
                        }
    708
    709
                        // If no false values, then all must be true
    710
    0
                        if true_count == len {
    711
    0
                            return ShortCircuitStrategy::ReturnRight;
    712
    0
                        }
    713
    714
                        // determine if we can pre-selection
    715
    0
                        if true_count as f32 / len as f32 <= PRE_SELECTION_THRESHOLD {
    716
    0
                            return ShortCircuitStrategy::PreSelection(bool_array);
    717
    0
                        }
    718
                    } else {
    719
                        // For OR, prioritize checking for all-true (short circuit case)
    720
                        // Uses optimized true_count() method provided by Arrow
    721
    722
                        // Short circuit if all values are true
    723
    0
                        if true_count == len {
    724
    0
                            return ShortCircuitStrategy::ReturnLeft;
    725
    0
                        }
    726
    727
                        // If no true values, then all must be false
    728
    0
                        if true_count == 0 {
    729
    0
                            return ShortCircuitStrategy::ReturnRight;
    730
    0
                        }
    731
                    }
    732
    0
                }
    733
            }
    734
    0
            ColumnarValue::Scalar(scalar) => {
    735
                // Fast path for scalar values
    736
    0
                if let ScalarValue::Boolean(Some(is_true)) = scalar {
    737
                    // Return Left for:
    738
                    // - AND with false value
    739
                    // - OR with true value
    740
    0
                    if (is_and && !is_true) || (!is_and && *is_true) {
    741
    0
                        return ShortCircuitStrategy::ReturnLeft;
    742
                    } else {
    743
    0
                        return ShortCircuitStrategy::ReturnRight;
    744
                    }
    745
    0
                }
    746
            }
    747
        }
    748
    749
        // If we can't short-circuit, indicate that normal evaluation should continue
    750
    0
        ShortCircuitStrategy::None
    751
    0
    }
    752
    753
    /// Creates a new boolean array based on the evaluation of the right expression,
    754
    /// but only for positions where the left_result is true.
    755
    ///
    756
    /// This function is used for short-circuit evaluation optimization of logical AND operations:
    757
    /// - When left_result has few true values, we only evaluate the right expression for those positions
    758
    /// - Values are copied from right_array where left_result is true
    759
    /// - All other positions are filled with false values
    760
    ///
    761
    /// # Parameters
    762
    /// - `left_result` Boolean array with selection mask (typically from left side of AND)
    763
    /// - `right_result` Result of evaluating right side of expression (only for selected positions)
    764
    ///
    765
    /// # Returns
    766
    /// A combined ColumnarValue with values from right_result where left_result is true
    767
    ///
    768
    /// # Example
    769
    ///  Initial Data: { 1, 2, 3, 4, 5 }
    770
    ///  Left Evaluation
    771
    ///     (Condition: Equal to 2 or 3)
    772
    ///          ↓
    773
    ///  Filtered Data: {2, 3}
    774
    ///    Left Bitmap: { 0, 1, 1, 0, 0 }
    775
    ///          ↓
    776
    ///   Right Evaluation
    777
    ///     (Condition: Even numbers)
    778
    ///          ↓
    779
    ///  Right Data: { 2 }
    780
    ///    Right Bitmap: { 1, 0 }
    781
    ///          ↓
    782
    ///   Combine Results
    783
    ///  Final Bitmap: { 0, 1, 0, 0, 0 }
    784
    ///
    785
    /// # Note
    786
    /// Perhaps it would be better to modify `left_result` directly without creating a copy?
    787
    /// In practice, `left_result` should have only one owner, so making changes should be safe.
    788
    /// However, this is difficult to achieve under the immutable constraints of [`Arc`] and [`BooleanArray`].
    789
    0
    fn pre_selection_scatter(
    790
    0
        left_result: &BooleanArray,
    791
    0
        right_result: Option<&BooleanArray>,
    792
    0
    ) -> Result<ColumnarValue> {
    793
    0
        let result_len = left_result.len();
    794
    795
    0
        let mut result_array_builder = BooleanArray::builder(result_len);
    796
    797
        // keep track of current position we have in right boolean array
    798
    0
        let mut right_array_pos = 0;
    799
    800
        // keep track of how much is filled
    801
    0
        let mut last_end = 0;
    802
        // reduce if condition in for_each
    803
    0
        match right_result {
    804
    0
            Some(right_result) => {
    805
    0
                SlicesIterator::new(left_result).for_each(|(start, end)| {
    806
                    // the gap needs to be filled with false
    807
    0
                    if start > last_end {
    808
    0
                        result_array_builder.append_n(start - last_end, false);
    809
    0
                    }
    810
    811
                    // copy values from right array for this slice
    812
    0
                    let len = end - start;
    813
    0
                    right_result
    814
    0
                        .slice(right_array_pos, len)
    815
    0
                        .iter()
    816
    0
                        .for_each(|v| result_array_builder.append_option(v));
    817
    818
    0
                    right_array_pos += len;
    819
    0
                    last_end = end;
    820
    0
                });
    821
            }
    822
    0
            None => SlicesIterator::new(left_result).for_each(|(start, end)| {
    823
                // the gap needs to be filled with false
    824
    0
                if start > last_end {
    825
    0
                    result_array_builder.append_n(start - last_end, false);
    826
    0
                }
    827
    828
                // append nulls for this slice derictly
    829
    0
                let len = end - start;
    830
    0
                result_array_builder.append_nulls(len);
    831
    832
    0
                last_end = end;
    833
    0
            }),
    834
        }
    835
    836
        // Fill any remaining positions with false
    837
    0
        if last_end < result_len {
    838
    0
            result_array_builder.append_n(result_len - last_end, false);
    839
    0
        }
    840
    0
        let boolean_result = result_array_builder.finish();
    841
    842
    0
        Ok(ColumnarValue::Array(Arc::new(boolean_result)))
    843
    0
    }
    844
    845
    0
    fn concat_elements(left: &ArrayRef, right: &ArrayRef) -> Result<ArrayRef> {
    846
    0
        Ok(match left.data_type() {
    847
    0
            DataType::Utf8 => Arc::new(concat_elements_utf8(
    848
    0
                left.as_string::<i32>(),
    849
    0
                right.as_string::<i32>(),
    850
    0
            )?),
    851
    0
            DataType::LargeUtf8 => Arc::new(concat_elements_utf8(
    852
    0
                left.as_string::<i64>(),
    853
    0
                right.as_string::<i64>(),
    854
    0
            )?),
    855
    0
            DataType::Utf8View => Arc::new(concat_elements_utf8view(
    856
    0
                left.as_string_view(),
    857
    0
                right.as_string_view(),
    858
    0
            )?),
    859
    0
            other => {
    860
    0
                return internal_err!(
    861
    0
                    "Data type {other:?} not supported for binary operation 'concat_elements' on string arrays"
    862
                );
    863
            }
    864
        })
    865
    0
    }
    866
    867
    /// Create a binary expression whose arguments are correctly coerced.
    868
    /// This function errors if it is not possible to coerce the arguments
    869
    /// to computational types supported by the operator.
    870
    0
    pub fn binary(
    871
    0
        lhs: Arc<dyn PhysicalExpr>,
    872
    0
        op: Operator,
    873
    0
        rhs: Arc<dyn PhysicalExpr>,
    874
    0
        _input_schema: &Schema,
    875
    0
    ) -> Result<Arc<dyn PhysicalExpr>> {
    876
    0
        Ok(Arc::new(BinaryExpr::new(lhs, op, rhs)))
    877
    0
    }
    878
    879
    /// Create a similar to expression
    880
    0
    pub fn similar_to(
    881
    0
        negated: bool,
    882
    0
        case_insensitive: bool,
    883
    0
        expr: Arc<dyn PhysicalExpr>,
    884
    0
        pattern: Arc<dyn PhysicalExpr>,
    885
    0
    ) -> Result<Arc<dyn PhysicalExpr>> {
    886
    0
        let binary_op = match (negated, case_insensitive) {
    887
    0
            (false, false) => Operator::RegexMatch,
    888
    0
            (false, true) => Operator::RegexIMatch,
    889
    0
            (true, false) => Operator::RegexNotMatch,
    890
    0
            (true, true) => Operator::RegexNotIMatch,
    891
        };
    892
    0
        Ok(Arc::new(BinaryExpr::new(expr, binary_op, pattern)))
    893
    0
    }
    894
    895
    #[cfg(test)]
    896
    mod tests {
    897
        use super::*;
    898
        use crate::expressions::{col, lit, try_cast, Column, Literal};
    899
        use datafusion_expr::lit as expr_lit;
    900
    901
        use datafusion_common::plan_datafusion_err;
    902
        use datafusion_physical_expr_common::physical_expr::fmt_sql;
    903
    904
        use crate::planner::logical2physical;
    905
        use arrow::array::BooleanArray;
    906
        use datafusion_expr::col as logical_col;
    907
        /// Performs a binary operation, applying any type coercion necessary
    908
    0
        fn binary_op(
    909
    0
            left: Arc<dyn PhysicalExpr>,
    910
    0
            op: Operator,
    911
    0
            right: Arc<dyn PhysicalExpr>,
    912
    0
            schema: &Schema,
    913
    0
        ) -> Result<Arc<dyn PhysicalExpr>> {
    914
    0
            let left_type = left.data_type(schema)?;
    915
    0
            let right_type = right.data_type(schema)?;
    916
    0
            let (lhs, rhs) =
    917
    0
                BinaryTypeCoercer::new(&left_type, &op, &right_type).get_input_types()?;
    918
    919
    0
            let left_expr = try_cast(left, schema, lhs)?;
    920
    0
            let right_expr = try_cast(right, schema, rhs)?;
    921
    0
            binary(left_expr, op, right_expr, schema)
    922
    0
        }
    923
    924
        #[test]
    925
    0
        fn binary_comparison() -> Result<()> {
    926
    0
            let schema = Schema::new(vec![
    927
    0
                Field::new("a", DataType::Int32, false),
    928
    0
                Field::new("b", DataType::Int32, false),
    929
            ]);
    930
    0
            let a = Int32Array::from(vec![1, 2, 3, 4, 5]);
    931
    0
            let b = Int32Array::from(vec![1, 2, 4, 8, 16]);
    932
    933
            // expression: "a < b"
    934
    0
            let lt = binary(
    935
    0
                col("a", &schema)?,
    936
    0
                Operator::Lt,
    937
    0
                col("b", &schema)?,
    938
    0
                &schema,
    939
    0
            )?;
    940
    0
            let batch =
    941
    0
                RecordBatch::try_new(Arc::new(schema), vec![Arc::new(a), Arc::new(b)])?;
    942
    943
    0
            let result = lt
    944
    0
                .evaluate(&batch)?
    945
    0
                .into_array(batch.num_rows())
    946
    0
                .expect("Failed to convert to array");
    947
    0
            assert_eq!(result.len(), 5);
    948
    949
    0
            let expected = [false, false, true, true, true];
    950
    0
            let result =
    951
    0
                as_boolean_array(&result).expect("failed to downcast to BooleanArray");
    952
    0
            for (i, &expected_item) in expected.iter().enumerate().take(5) {
    953
    0
                assert_eq!(result.value(i), expected_item);
    954
            }
    955
    956
    0
            Ok(())
    957
    0
        }
    958
    959
        #[test]
    960
    0
        fn binary_nested() -> Result<()> {
    961
    0
            let schema = Schema::new(vec![
    962
    0
                Field::new("a", DataType::Int32, false),
    963
    0
                Field::new("b", DataType::Int32, false),
    964
            ]);
    965
    0
            let a = Int32Array::from(vec![2, 4, 6, 8, 10]);
    966
    0
            let b = Int32Array::from(vec![2, 5, 4, 8, 8]);
    967
    968
            // expression: "a < b OR a == b"
    969
    0
            let expr = binary(
    970
    0
                binary(
    971
    0
                    col("a", &schema)?,
    972
    0
                    Operator::Lt,
    973
    0
                    col("b", &schema)?,
    974
    0
                    &schema,
    975
    0
                )?,
    976
    0
                Operator::Or,
    977
    0
                binary(
    978
    0
                    col("a", &schema)?,
    979
    0
                    Operator::Eq,
    980
    0
                    col("b", &schema)?,
    981
    0
                    &schema,
    982
    0
                )?,
    983
    0
                &schema,
    984
    0
            )?;
    985
    0
            let batch =
    986
    0
                RecordBatch::try_new(Arc::new(schema), vec![Arc::new(a), Arc::new(b)])?;
    987
    988
    0
            assert_eq!("a@0 < b@1 OR a@0 = b@1", format!("{expr}"));
    989
    990
    0
            let result = expr
    991
    0
                .evaluate(&batch)?
    992
    0
                .into_array(batch.num_rows())
    993
    0
                .expect("Failed to convert to array");
    994
    0
            assert_eq!(result.len(), 5);
    995
    996
    0
            let expected = [true, true, false, true, false];
    997
    0
            let result =
    998
    0
                as_boolean_array(&result).expect("failed to downcast to BooleanArray");
    999
    0
            for (i, &expected_item) in expected.iter().enumerate().take(5) {
    1000
    0
                assert_eq!(result.value(i), expected_item);
    1001
            }
    1002
    1003
    0
            Ok(())
    1004
    0
        }
    1005
    1006
        // runs an end-to-end test of physical type coercion:
    1007
        // 1. construct a record batch with two columns of type A and B
    1008
        //  (*_ARRAY is the Rust Arrow array type, and *_TYPE is the DataType of the elements)
    1009
        // 2. construct a physical expression of A OP B
    1010
        // 3. evaluate the expression
    1011
        // 4. verify that the resulting expression is of type C
    1012
        // 5. verify that the results of evaluation are $VEC
    1013
        macro_rules! test_coercion {
    1014
            ($A_ARRAY:ident, $A_TYPE:expr, $A_VEC:expr, $B_ARRAY:ident, $B_TYPE:expr, $B_VEC:expr, $OP:expr, $C_ARRAY:ident, $C_TYPE:expr, $VEC:expr,) => {{
    1015
                let schema = Schema::new(vec![
    1016
                    Field::new("a", $A_TYPE, false),
    1017
                    Field::new("b", $B_TYPE, false),
    1018
                ]);
    1019
                let a = $A_ARRAY::from($A_VEC);
    1020
                let b = $B_ARRAY::from($B_VEC);
    1021
                let (lhs, rhs) = BinaryTypeCoercer::new(&$A_TYPE, &$OP, &$B_TYPE).get_input_types()?;
    1022
    1023
                let left = try_cast(col("a", &schema)?, &schema, lhs)?;
    1024
                let right = try_cast(col("b", &schema)?, &schema, rhs)?;
    1025
    1026
                // verify that we can construct the expression
    1027
                let expression = binary(left, $OP, right, &schema)?;
    1028
                let batch = RecordBatch::try_new(
    1029
                    Arc::new(schema.clone()),
    1030
                    vec![Arc::new(a), Arc::new(b)],
    1031
                )?;
    1032
    1033
                // verify that the expression's type is correct
    1034
                assert_eq!(expression.data_type(&schema)?, $C_TYPE);
    1035
    1036
                // compute
    1037
                let result = expression.evaluate(&batch)?.into_array(batch.num_rows()).expect("Failed to convert to array");
    1038
    1039
                // verify that the array's data_type is correct
    1040
                assert_eq!(*result.data_type(), $C_TYPE);
    1041
    1042
                // verify that the data itself is downcastable
    1043
                let result = result
    1044
                    .as_any()
    1045
                    .downcast_ref::<$C_ARRAY>()
    1046
                    .expect("failed to downcast");
    1047
                // verify that the result itself is correct
    1048
                for (i, x) in $VEC.iter().enumerate() {
    1049
                    let v = result.value(i);
    1050
                    assert_eq!(
    1051
                        v,
    1052
                        *x,
    1053
                        "Unexpected output at position {i}:\n\nActual:\n{v}\n\nExpected:\n{x}"
    1054
                    );
    1055
                }
    1056
            }};
    1057
        }
    1058
    1059
        #[test]
    1060
    0
        fn test_type_coercion() -> Result<()> {
    1061
    0
            test_coercion!(
    1062
                Int32Array,
    1063
    0
                DataType::Int32,
    1064
    0
                vec![1i32, 2i32],
    1065
                UInt32Array,
    1066
    0
                DataType::UInt32,
    1067
    0
                vec![1u32, 2u32],
    1068
    0
                Operator::Plus,
    1069
                Int64Array,
    1070
                DataType::Int64,
    1071
    0
                [2i64, 4i64],
    1072
            );
    1073
    0
            test_coercion!(
    1074
                Int32Array,
    1075
    0
                DataType::Int32,
    1076
    0
                vec![1i32],
    1077
                UInt16Array,
    1078
    0
                DataType::UInt16,
    1079
    0
                vec![1u16],
    1080
    0
                Operator::Plus,
    1081
                Int32Array,
    1082
                DataType::Int32,
    1083
    0
                [2i32],
    1084
            );
    1085
    0
            test_coercion!(
    1086
                Float32Array,
    1087
    0
                DataType::Float32,
    1088
    0
                vec![1f32],
    1089
                UInt16Array,
    1090
    0
                DataType::UInt16,
    1091
    0
                vec![1u16],
    1092
    0
                Operator::Plus,
    1093
                Float32Array,
    1094
                DataType::Float32,
    1095
    0
                [2f32],
    1096
            );
    1097
    0
            test_coercion!(
    1098
                Float32Array,
    1099
    0
                DataType::Float32,
    1100
    0
                vec![2f32],
    1101
                UInt16Array,
    1102
    0
                DataType::UInt16,
    1103
    0
                vec![1u16],
    1104
    0
                Operator::Multiply,
    1105
                Float32Array,
    1106
                DataType::Float32,
    1107
    0
                [2f32],
    1108
            );
    1109
    0
            test_coercion!(
    1110
                StringArray,
    1111
    0
                DataType::Utf8,
    1112
    0
                vec!["1994-12-13", "1995-01-26"],
    1113
                Date32Array,
    1114
    0
                DataType::Date32,
    1115
    0
                vec![9112, 9156],
    1116
    0
                Operator::Eq,
    1117
                BooleanArray,
    1118
                DataType::Boolean,
    1119
    0
                [true, true],
    1120
            );
    1121
    0
            test_coercion!(
    1122
                StringArray,
    1123
    0
                DataType::Utf8,
    1124
    0
                vec!["1994-12-13", "1995-01-26"],
    1125
                Date32Array,
    1126
    0
                DataType::Date32,
    1127
    0
                vec![9113, 9154],
    1128
    0
                Operator::Lt,
    1129
                BooleanArray,
    1130
                DataType::Boolean,
    1131
    0
                [true, false],
    1132
            );
    1133
    0
            test_coercion!(
    1134
                StringArray,
    1135
    0
                DataType::Utf8,
    1136
    0
                vec!["1994-12-13T12:34:56", "1995-01-26T01:23:45"],
    1137
                Date64Array,
    1138
    0
                DataType::Date64,
    1139
    0
                vec![787322096000, 791083425000],
    1140
    0
                Operator::Eq,
    1141
                BooleanArray,
    1142
                DataType::Boolean,
    1143
    0
                [true, true],
    1144
            );
    1145
    0
            test_coercion!(
    1146
                StringArray,
    1147
    0
                DataType::Utf8,
    1148
    0
                vec!["1994-12-13T12:34:56", "1995-01-26T01:23:45"],
    1149
                Date64Array,
    1150
    0
                DataType::Date64,
    1151
    0
                vec![787322096001, 791083424999],
    1152
    0
                Operator::Lt,
    1153
                BooleanArray,
    1154
                DataType::Boolean,
    1155
    0
                [true, false],
    1156
            );
    1157
    0
            test_coercion!(
    1158
                StringViewArray,
    1159
    0
                DataType::Utf8View,
    1160
    0
                vec!["abc"; 5],
    1161
                StringArray,
    1162
    0
                DataType::Utf8,
    1163
    0
                vec!["^a", "^A", "(b|d)", "(B|D)", "^(b|c)"],
    1164
    0
                Operator::RegexMatch,
    1165
                BooleanArray,
    1166
                DataType::Boolean,
    1167
    0
                [true, false, true, false, false],
    1168
            );
    1169
    0
            test_coercion!(
    1170
                StringViewArray,
    1171
    0
                DataType::Utf8View,
    1172
    0
                vec!["abc"; 5],
    1173
                StringArray,
    1174
    0
                DataType::Utf8,
    1175
    0
                vec!["^a", "^A", "(b|d)", "(B|D)", "^(b|c)"],
    1176
    0
                Operator::RegexIMatch,
    1177
                BooleanArray,
    1178
                DataType::Boolean,
    1179
    0
                [true, true, true, true, false],
    1180
            );
    1181
    0
            test_coercion!(
    1182
                StringArray,
    1183
    0
                DataType::Utf8,
    1184
    0
                vec!["abc"; 5],
    1185
                StringViewArray,
    1186
    0
                DataType::Utf8View,
    1187
    0
                vec!["^a", "^A", "(b|d)", "(B|D)", "^(b|c)"],
    1188
    0
                Operator::RegexNotMatch,
    1189
                BooleanArray,
    1190
                DataType::Boolean,
    1191
    0
                [false, true, false, true, true],
    1192
            );
    1193
    0
            test_coercion!(
    1194
                StringArray,
    1195
    0
                DataType::Utf8,
    1196
    0
                vec!["abc"; 5],
    1197
                StringViewArray,
    1198
    0
                DataType::Utf8View,
    1199
    0
                vec!["^a", "^A", "(b|d)", "(B|D)", "^(b|c)"],
    1200
    0
                Operator::RegexNotIMatch,
    1201
                BooleanArray,
    1202
                DataType::Boolean,
    1203
    0
                [false, false, false, false, true],
    1204
            );
    1205
    0
            test_coercion!(
    1206
                StringArray,
    1207
    0
                DataType::Utf8,
    1208
    0
                vec!["abc"; 5],
    1209
                StringArray,
    1210
    0
                DataType::Utf8,
    1211
    0
                vec!["^a", "^A", "(b|d)", "(B|D)", "^(b|c)"],
    1212
    0
                Operator::RegexMatch,
    1213
                BooleanArray,
    1214
                DataType::Boolean,
    1215
    0
                [true, false, true, false, false],
    1216
            );
    1217
    0
            test_coercion!(
    1218
                StringArray,
    1219
    0
                DataType::Utf8,
    1220
    0
                vec!["abc"; 5],
    1221
                StringArray,
    1222
    0
                DataType::Utf8,
    1223
    0
                vec!["^a", "^A", "(b|d)", "(B|D)", "^(b|c)"],
    1224
    0
                Operator::RegexIMatch,
    1225
                BooleanArray,
    1226
                DataType::Boolean,
    1227
    0
                [true, true, true, true, false],
    1228
            );
    1229
    0
            test_coercion!(
    1230
                StringArray,
    1231
    0
                DataType::Utf8,
    1232
    0
                vec!["abc"; 5],
    1233
                StringArray,
    1234
    0
                DataType::Utf8,
    1235
    0
                vec!["^a", "^A", "(b|d)", "(B|D)", "^(b|c)"],
    1236
    0
                Operator::RegexNotMatch,
    1237
                BooleanArray,
    1238
                DataType::Boolean,
    1239
    0
                [false, true, false, true, true],
    1240
            );
    1241
    0
            test_coercion!(
    1242
                StringArray,
    1243
    0
                DataType::Utf8,
    1244
    0
                vec!["abc"; 5],
    1245
                StringArray,
    1246
    0
                DataType::Utf8,
    1247
    0
                vec!["^a", "^A", "(b|d)", "(B|D)", "^(b|c)"],
    1248
    0
                Operator::RegexNotIMatch,
    1249
                BooleanArray,
    1250
                DataType::Boolean,
    1251
    0
                [false, false, false, false, true],
    1252
            );
    1253
    0
            test_coercion!(
    1254
                LargeStringArray,
    1255
    0
                DataType::LargeUtf8,
    1256
    0
                vec!["abc"; 5],
    1257
                LargeStringArray,
    1258
    0
                DataType::LargeUtf8,
    1259
    0
                vec!["^a", "^A", "(b|d)", "(B|D)", "^(b|c)"],
    1260
    0
                Operator::RegexMatch,
    1261
                BooleanArray,
    1262
                DataType::Boolean,
    1263
    0
                [true, false, true, false, false],
    1264
            );
    1265
    0
            test_coercion!(
    1266
                LargeStringArray,
    1267
    0
                DataType::LargeUtf8,
    1268
    0
                vec!["abc"; 5],
    1269
                LargeStringArray,
    1270
    0
                DataType::LargeUtf8,
    1271
    0
                vec!["^a", "^A", "(b|d)", "(B|D)", "^(b|c)"],
    1272
    0
                Operator::RegexIMatch,
    1273
                BooleanArray,
    1274
                DataType::Boolean,
    1275
    0
                [true, true, true, true, false],
    1276
            );
    1277
    0
            test_coercion!(
    1278
                LargeStringArray,
    1279
    0
                DataType::LargeUtf8,
    1280
    0
                vec!["abc"; 5],
    1281
                LargeStringArray,
    1282
    0
                DataType::LargeUtf8,
    1283
    0
                vec!["^a", "^A", "(b|d)", "(B|D)", "^(b|c)"],
    1284
    0
                Operator::RegexNotMatch,
    1285
                BooleanArray,
    1286
                DataType::Boolean,
    1287
    0
                [false, true, false, true, true],
    1288
            );
    1289
    0
            test_coercion!(
    1290
                LargeStringArray,
    1291
    0
                DataType::LargeUtf8,
    1292
    0
                vec!["abc"; 5],
    1293
                LargeStringArray,
    1294
    0
                DataType::LargeUtf8,
    1295
    0
                vec!["^a", "^A", "(b|d)", "(B|D)", "^(b|c)"],
    1296
    0
                Operator::RegexNotIMatch,
    1297
                BooleanArray,
    1298
                DataType::Boolean,
    1299
    0
                [false, false, false, false, true],
    1300
            );
    1301
    0
            test_coercion!(
    1302
                StringArray,
    1303
    0
                DataType::Utf8,
    1304
    0
                vec!["abc"; 5],
    1305
                StringArray,
    1306
    0
                DataType::Utf8,
    1307
    0
                vec!["a__", "A%BC", "A_BC", "abc", "a%C"],
    1308
    0
                Operator::LikeMatch,
    1309
                BooleanArray,
    1310
                DataType::Boolean,
    1311
    0
                [true, false, false, true, false],
    1312
            );
    1313
    0
            test_coercion!(
    1314
                StringArray,
    1315
    0
                DataType::Utf8,
    1316
    0
                vec!["abc"; 5],
    1317
                StringArray,
    1318
    0
                DataType::Utf8,
    1319
    0
                vec!["a__", "A%BC", "A_BC", "abc", "a%C"],
    1320
    0
                Operator::ILikeMatch,
    1321
                BooleanArray,
    1322
                DataType::Boolean,
    1323
    0
                [true, true, false, true, true],
    1324
            );
    1325
    0
            test_coercion!(
    1326
                StringArray,
    1327
    0
                DataType::Utf8,
    1328
    0
                vec!["abc"; 5],
    1329
                StringArray,
    1330
    0
                DataType::Utf8,
    1331
    0
                vec!["a__", "A%BC", "A_BC", "abc", "a%C"],
    1332
    0
                Operator::NotLikeMatch,
    1333
                BooleanArray,
    1334
                DataType::Boolean,
    1335
    0
                [false, true, true, false, true],
    1336
            );
    1337
    0
            test_coercion!(
    1338
                StringArray,
    1339
    0
                DataType::Utf8,
    1340
    0
                vec!["abc"; 5],
    1341
                StringArray,
    1342
    0
                DataType::Utf8,
    1343
    0
                vec!["a__", "A%BC", "A_BC", "abc", "a%C"],
    1344
    0
                Operator::NotILikeMatch,
    1345
                BooleanArray,
    1346
                DataType::Boolean,
    1347
    0
                [false, false, true, false, false],
    1348
            );
    1349
    0
            test_coercion!(
    1350
                LargeStringArray,
    1351
    0
                DataType::LargeUtf8,
    1352
    0
                vec!["abc"; 5],
    1353
                LargeStringArray,
    1354
    0
                DataType::LargeUtf8,
    1355
    0
                vec!["a__", "A%BC", "A_BC", "abc", "a%C"],
    1356
    0
                Operator::LikeMatch,
    1357
                BooleanArray,
    1358
                DataType::Boolean,
    1359
    0
                [true, false, false, true, false],
    1360
            );
    1361
    0
            test_coercion!(
    1362
                LargeStringArray,
    1363
    0
                DataType::LargeUtf8,
    1364
    0
                vec!["abc"; 5],
    1365
                LargeStringArray,
    1366
    0
                DataType::LargeUtf8,
    1367
    0
                vec!["a__", "A%BC", "A_BC", "abc", "a%C"],
    1368
    0
                Operator::ILikeMatch,
    1369
                BooleanArray,
    1370
                DataType::Boolean,
    1371
    0
                [true, true, false, true, true],
    1372
            );
    1373
    0
            test_coercion!(
    1374
                LargeStringArray,
    1375
    0
                DataType::LargeUtf8,
    1376
    0
                vec!["abc"; 5],
    1377
                LargeStringArray,
    1378
    0
                DataType::LargeUtf8,
    1379
    0
                vec!["a__", "A%BC", "A_BC", "abc", "a%C"],
    1380
    0
                Operator::NotLikeMatch,
    1381
                BooleanArray,
    1382
                DataType::Boolean,
    1383
    0
                [false, true, true, false, true],
    1384
            );
    1385
    0
            test_coercion!(
    1386
                LargeStringArray,
    1387
    0
                DataType::LargeUtf8,
    1388
    0
                vec!["abc"; 5],
    1389
                LargeStringArray,
    1390
    0
                DataType::LargeUtf8,
    1391
    0
                vec!["a__", "A%BC", "A_BC", "abc", "a%C"],
    1392
    0
                Operator::NotILikeMatch,
    1393
                BooleanArray,
    1394
                DataType::Boolean,
    1395
    0
                [false, false, true, false, false],
    1396
            );
    1397
    0
            test_coercion!(
    1398
                Int16Array,
    1399
    0
                DataType::Int16,
    1400
    0
                vec![1i16, 2i16, 3i16],
    1401
                Int64Array,
    1402
    0
                DataType::Int64,
    1403
    0
                vec![10i64, 4i64, 5i64],
    1404
    0
                Operator::BitwiseAnd,
    1405
                Int64Array,
    1406
                DataType::Int64,
    1407
    0
                [0i64, 0i64, 1i64],
    1408
            );
    1409
    0
            test_coercion!(
    1410
                UInt16Array,
    1411
    0
                DataType::UInt16,
    1412
    0
                vec![1u16, 2u16, 3u16],
    1413
                UInt64Array,
    1414
    0
                DataType::UInt64,
    1415
    0
                vec![10u64, 4u64, 5u64],
    1416
    0
                Operator::BitwiseAnd,
    1417
                UInt64Array,
    1418
                DataType::UInt64,
    1419
    0
                [0u64, 0u64, 1u64],
    1420
            );
    1421
    0
            test_coercion!(
    1422
                Int16Array,
    1423
    0
                DataType::Int16,
    1424
    0
                vec![3i16, 2i16, 3i16],
    1425
                Int64Array,
    1426
    0
                DataType::Int64,
    1427
    0
                vec![10i64, 6i64, 5i64],
    1428
    0
                Operator::BitwiseOr,
    1429
                Int64Array,
    1430
                DataType::Int64,
    1431
    0
                [11i64, 6i64, 7i64],
    1432
            );
    1433
    0
            test_coercion!(
    1434
                UInt16Array,
    1435
    0
                DataType::UInt16,
    1436
    0
                vec![1u16, 2u16, 3u16],
    1437
                UInt64Array,
    1438
    0
                DataType::UInt64,
    1439
    0
                vec![10u64, 4u64, 5u64],
    1440
    0
                Operator::BitwiseOr,
    1441
                UInt64Array,
    1442
                DataType::UInt64,
    1443
    0
                [11u64, 6u64, 7u64],
    1444
            );
    1445
    0
            test_coercion!(
    1446
                Int16Array,
    1447
    0
                DataType::Int16,
    1448
    0
                vec![3i16, 2i16, 3i16],
    1449
                Int64Array,
    1450
    0
                DataType::Int64,
    1451
    0
                vec![10i64, 6i64, 5i64],
    1452
    0
                Operator::BitwiseXor,
    1453
                Int64Array,
    1454
                DataType::Int64,
    1455
    0
                [9i64, 4i64, 6i64],
    1456
            );
    1457
    0
            test_coercion!(
    1458
                UInt16Array,
    1459
    0
                DataType::UInt16,
    1460
    0
                vec![3u16, 2u16, 3u16],
    1461
                UInt64Array,
    1462
    0
                DataType::UInt64,
    1463
    0
                vec![10u64, 6u64, 5u64],
    1464
    0
                Operator::BitwiseXor,
    1465
                UInt64Array,
    1466
                DataType::UInt64,
    1467
    0
                [9u64, 4u64, 6u64],
    1468
            );
    1469
    0
            test_coercion!(
    1470
                Int16Array,
    1471
    0
                DataType::Int16,
    1472
    0
                vec![4i16, 27i16, 35i16],
    1473
                Int64Array,
    1474
    0
                DataType::Int64,
    1475
    0
                vec![2i64, 3i64, 4i64],
    1476
    0
                Operator::BitwiseShiftRight,
    1477
                Int64Array,
    1478
                DataType::Int64,
    1479
    0
                [1i64, 3i64, 2i64],
    1480
            );
    1481
    0
            test_coercion!(
    1482
                UInt16Array,
    1483
    0
                DataType::UInt16,
    1484
    0
                vec![4u16, 27u16, 35u16],
    1485
                UInt64Array,
    1486
    0
                DataType::UInt64,
    1487
    0
                vec![2u64, 3u64, 4u64],
    1488
    0
                Operator::BitwiseShiftRight,
    1489
                UInt64Array,
    1490
                DataType::UInt64,
    1491
    0
                [1u64, 3u64, 2u64],
    1492
            );
    1493
    0
            test_coercion!(
    1494
                Int16Array,
    1495
    0
                DataType::Int16,
    1496
    0
                vec![2i16, 3i16, 4i16],
    1497
                Int64Array,
    1498
    0
                DataType::Int64,
    1499
    0
                vec![4i64, 12i64, 7i64],
    1500
    0
                Operator::BitwiseShiftLeft,
    1501
                Int64Array,
    1502
                DataType::Int64,
    1503
    0
                [32i64, 12288i64, 512i64],
    1504
            );
    1505
    0
            test_coercion!(
    1506
                UInt16Array,
    1507
    0
                DataType::UInt16,
    1508
    0
                vec![2u16, 3u16, 4u16],
    1509
                UInt64Array,
    1510
    0
                DataType::UInt64,
    1511
    0
                vec![4u64, 12u64, 7u64],
    1512
    0
                Operator::BitwiseShiftLeft,
    1513
                UInt64Array,
    1514
                DataType::UInt64,
    1515
    0
                [32u64, 12288u64, 512u64],
    1516
            );
    1517
    0
            Ok(())
    1518
    0
        }
    1519
    1520
        // Note it would be nice to use the same test_coercion macro as
    1521
        // above, but sadly the type of the values of the dictionary are
    1522
        // not encoded in the rust type of the DictionaryArray. Thus there
    1523
        // is no way at the time of this writing to create a dictionary
    1524
        // array using the `From` trait
    1525
        #[test]
    1526
    0
        fn test_dictionary_type_to_array_coercion() -> Result<()> {
    1527
            // Test string  a string dictionary
    1528
    0
            let dict_type =
    1529
    0
                DataType::Dictionary(Box::new(DataType::Int32), Box::new(DataType::Utf8));
    1530
    0
            let string_type = DataType::Utf8;
    1531
    1532
            // build dictionary
    1533
    0
            let mut dict_builder = StringDictionaryBuilder::<Int32Type>::new();
    1534
    1535
    0
            dict_builder.append("one")?;
    1536
    0
            dict_builder.append_null();
    1537
    0
            dict_builder.append("three")?;
    1538
    0
            dict_builder.append("four")?;
    1539
    0
            let dict_array = Arc::new(dict_builder.finish()) as ArrayRef;
    1540
    1541
    0
            let str_array = Arc::new(StringArray::from(vec![
    1542
    0
                Some("not one"),
    1543
    0
                Some("two"),
    1544
    0
                None,
    1545
    0
                Some("four"),
    1546
    0
            ])) as ArrayRef;
    1547
    1548
    0
            let schema = Arc::new(Schema::new(vec![
    1549
    0
                Field::new("a", dict_type.clone(), true),
    1550
    0
                Field::new("b", string_type.clone(), true),
    1551
            ]));
    1552
    1553
            // Test 1: a = b
    1554
    0
            let result = BooleanArray::from(vec![Some(false), None, None, Some(true)]);
    1555
    0
            apply_logic_op(&schema, &dict_array, &str_array, Operator::Eq, result)?;
    1556
    1557
            // Test 2: now test the other direction
    1558
            // b = a
    1559
    0
            let schema = Arc::new(Schema::new(vec![
    1560
    0
                Field::new("a", string_type, true),
    1561
    0
                Field::new("b", dict_type, true),
    1562
            ]));
    1563
    0
            let result = BooleanArray::from(vec![Some(false), None, None, Some(true)]);
    1564
    0
            apply_logic_op(&schema, &str_array, &dict_array, Operator::Eq, result)?;
    1565
    1566
    0
            Ok(())
    1567
    0
        }
    1568
    1569
        #[test]
    1570
    0
        fn plus_op() -> Result<()> {
    1571
    0
            let schema = Schema::new(vec![
    1572
    0
                Field::new("a", DataType::Int32, false),
    1573
    0
                Field::new("b", DataType::Int32, false),
    1574
            ]);
    1575
    0
            let a = Int32Array::from(vec![1, 2, 3, 4, 5]);
    1576
    0
            let b = Int32Array::from(vec![1, 2, 4, 8, 16]);
    1577
    1578
    0
            apply_arithmetic::<Int32Type>(
    1579
    0
                Arc::new(schema),
    1580
    0
                vec![Arc::new(a), Arc::new(b)],
    1581
    0
                Operator::Plus,
    1582
    0
                Int32Array::from(vec![2, 4, 7, 12, 21]),
    1583
    0
            )?;
    1584
    1585
    0
            Ok(())
    1586
    0
        }
    1587
    1588
        #[test]
    1589
    0
        fn plus_op_dict() -> Result<()> {
    1590
    0
            let schema = Schema::new(vec![
    1591
    0
                Field::new(
    1592
                    "a",
    1593
    0
                    DataType::Dictionary(Box::new(DataType::Int8), Box::new(DataType::Int32)),
    1594
                    true,
    1595
                ),
    1596
    0
                Field::new(
    1597
                    "b",
    1598
    0
                    DataType::Dictionary(Box::new(DataType::Int8), Box::new(DataType::Int32)),
    1599
                    true,
    1600
                ),
    1601
            ]);
    1602
    1603
    0
            let a = Int32Array::from(vec![1, 2, 3, 4, 5]);
    1604
    0
            let keys = Int8Array::from(vec![Some(0), None, Some(1), Some(3), None]);
    1605
    0
            let a = DictionaryArray::try_new(keys, Arc::new(a))?;
    1606
    1607
    0
            let b = Int32Array::from(vec![1, 2, 4, 8, 16]);
    1608
    0
            let keys = Int8Array::from(vec![0, 1, 1, 2, 1]);
    1609
    0
            let b = DictionaryArray::try_new(keys, Arc::new(b))?;
    1610
    1611
    0
            apply_arithmetic::<Int32Type>(
    1612
    0
                Arc::new(schema),
    1613
    0
                vec![Arc::new(a), Arc::new(b)],
    1614
    0
                Operator::Plus,
    1615
    0
                Int32Array::from(vec![Some(2), None, Some(4), Some(8), None]),
    1616
    0
            )?;
    1617
    1618
    0
            Ok(())
    1619
    0
        }
    1620
    1621
        #[test]
    1622
    0
        fn plus_op_dict_decimal() -> Result<()> {
    1623
    0
            let schema = Schema::new(vec![
    1624
    0
                Field::new(
    1625
                    "a",
    1626
    0
                    DataType::Dictionary(
    1627
    0
                        Box::new(DataType::Int8),
    1628
    0
                        Box::new(DataType::Decimal128(10, 0)),
    1629
    0
                    ),
    1630
                    true,
    1631
                ),
    1632
    0
                Field::new(
    1633
                    "b",
    1634
    0
                    DataType::Dictionary(
    1635
    0
                        Box::new(DataType::Int8),
    1636
    0
                        Box::new(DataType::Decimal128(10, 0)),
    1637
    0
                    ),
    1638
                    true,
    1639
                ),
    1640
            ]);
    1641
    1642
    0
            let value = 123;
    1643
    0
            let decimal_array = Arc::new(create_decimal_array(
    1644
    0
                &[
    1645
    0
                    Some(value),
    1646
    0
                    Some(value + 2),
    1647
    0
                    Some(value - 1),
    1648
    0
                    Some(value + 1),
    1649
    0
                ],
    1650
                10,
    1651
                0,
    1652
            ));
    1653
    1654
    0
            let keys = Int8Array::from(vec![Some(0), Some(2), None, Some(3), Some(0)]);
    1655
    0
            let a = DictionaryArray::try_new(keys, decimal_array)?;
    1656
    1657
    0
            let keys = Int8Array::from(vec![Some(0), None, Some(3), Some(2), Some(2)]);
    1658
    0
            let decimal_array = Arc::new(create_decimal_array(
    1659
    0
                &[
    1660
    0
                    Some(value + 1),
    1661
    0
                    Some(value + 3),
    1662
    0
                    Some(value),
    1663
    0
                    Some(value + 2),
    1664
    0
                ],
    1665
                10,
    1666
                0,
    1667
            ));
    1668
    0
            let b = DictionaryArray::try_new(keys, decimal_array)?;
    1669
    1670
    0
            apply_arithmetic(
    1671
    0
                Arc::new(schema),
    1672
    0
                vec![Arc::new(a), Arc::new(b)],
    1673
    0
                Operator::Plus,
    1674
    0
                create_decimal_array(&[Some(247), None, None, Some(247), Some(246)], 11, 0),
    1675
    0
            )?;
    1676
    1677
    0
            Ok(())
    1678
    0
        }
    1679
    1680
        #[test]
    1681
    0
        fn plus_op_scalar() -> Result<()> {
    1682
    0
            let schema = Schema::new(vec![Field::new("a", DataType::Int32, false)]);
    1683
    0
            let a = Int32Array::from(vec![1, 2, 3, 4, 5]);
    1684
    1685
    0
            apply_arithmetic_scalar(
    1686
    0
                Arc::new(schema),
    1687
    0
                vec![Arc::new(a)],
    1688
    0
                Operator::Plus,
    1689
    0
                ScalarValue::Int32(Some(1)),
    1690
    0
                Arc::new(Int32Array::from(vec![2, 3, 4, 5, 6])),
    1691
    0
            )?;
    1692
    1693
    0
            Ok(())
    1694
    0
        }
    1695
    1696
        #[test]
    1697
    0
        fn plus_op_dict_scalar() -> Result<()> {
    1698
    0
            let schema = Schema::new(vec![Field::new(
    1699
                "a",
    1700
    0
                DataType::Dictionary(Box::new(DataType::Int8), Box::new(DataType::Int32)),
    1701
                true,
    1702
            )]);
    1703
    1704
    0
            let mut dict_builder = PrimitiveDictionaryBuilder::<Int8Type, Int32Type>::new();
    1705
    1706
    0
            dict_builder.append(1)?;
    1707
    0
            dict_builder.append_null();
    1708
    0
            dict_builder.append(2)?;
    1709
    0
            dict_builder.append(5)?;
    1710
    1711
    0
            let a = dict_builder.finish();
    1712
    1713
    0
            let expected: PrimitiveArray<Int32Type> =
    1714
    0
                PrimitiveArray::from(vec![Some(2), None, Some(3), Some(6)]);
    1715
    1716
    0
            apply_arithmetic_scalar(
    1717
    0
                Arc::new(schema),
    1718
    0
                vec![Arc::new(a)],
    1719
    0
                Operator::Plus,
    1720
    0
                ScalarValue::Dictionary(
    1721
    0
                    Box::new(DataType::Int8),
    1722
    0
                    Box::new(ScalarValue::Int32(Some(1))),
    1723
    0
                ),
    1724
    0
                Arc::new(expected),
    1725
    0
            )?;
    1726
    1727
    0
            Ok(())
    1728
    0
        }
    1729
    1730
        #[test]
    1731
    0
        fn plus_op_dict_scalar_decimal() -> Result<()> {
    1732
    0
            let schema = Schema::new(vec![Field::new(
    1733
                "a",
    1734
    0
                DataType::Dictionary(
    1735
    0
                    Box::new(DataType::Int8),
    1736
    0
                    Box::new(DataType::Decimal128(10, 0)),
    1737
    0
                ),
    1738
                true,
    1739
            )]);
    1740
    1741
    0
            let value = 123;
    1742
    0
            let decimal_array = Arc::new(create_decimal_array(
    1743
    0
                &[Some(value), None, Some(value - 1), Some(value + 1)],
    1744
                10,
    1745
                0,
    1746
            ));
    1747
    1748
    0
            let keys = Int8Array::from(vec![0, 2, 1, 3, 0]);
    1749
    0
            let a = DictionaryArray::try_new(keys, decimal_array)?;
    1750
    1751
    0
            let decimal_array = Arc::new(create_decimal_array(
    1752
    0
                &[
    1753
    0
                    Some(value + 1),
    1754
    0
                    Some(value),
    1755
    0
                    None,
    1756
    0
                    Some(value + 2),
    1757
    0
                    Some(value + 1),
    1758
    0
                ],
    1759
                11,
    1760
                0,
    1761
            ));
    1762
    1763
    0
            apply_arithmetic_scalar(
    1764
    0
                Arc::new(schema),
    1765
    0
                vec![Arc::new(a)],
    1766
    0
                Operator::Plus,
    1767
    0
                ScalarValue::Dictionary(
    1768
    0
                    Box::new(DataType::Int8),
    1769
    0
                    Box::new(ScalarValue::Decimal128(Some(1), 10, 0)),
    1770
    0
                ),
    1771
    0
                decimal_array,
    1772
    0
            )?;
    1773
    1774
    0
            Ok(())
    1775
    0
        }
    1776
    1777
        #[test]
    1778
    0
        fn minus_op() -> Result<()> {
    1779
    0
            let schema = Arc::new(Schema::new(vec![
    1780
    0
                Field::new("a", DataType::Int32, false),
    1781
    0
                Field::new("b", DataType::Int32, false),
    1782
            ]));
    1783
    0
            let a = Arc::new(Int32Array::from(vec![1, 2, 4, 8, 16]));
    1784
    0
            let b = Arc::new(Int32Array::from(vec![1, 2, 3, 4, 5]));
    1785
    1786
    0
            apply_arithmetic::<Int32Type>(
    1787
    0
                Arc::clone(&schema),
    1788
    0
                vec![
    1789
    0
                    Arc::clone(&a) as Arc<dyn Array>,
    1790
    0
                    Arc::clone(&b) as Arc<dyn Array>,
    1791
                ],
    1792
    0
                Operator::Minus,
    1793
    0
                Int32Array::from(vec![0, 0, 1, 4, 11]),
    1794
    0
            )?;
    1795
    1796
            // should handle have negative values in result (for signed)
    1797
    0
            apply_arithmetic::<Int32Type>(
    1798
    0
                schema,
    1799
    0
                vec![b, a],
    1800
    0
                Operator::Minus,
    1801
    0
                Int32Array::from(vec![0, 0, -1, -4, -11]),
    1802
    0
            )?;
    1803
    1804
    0
            Ok(())
    1805
    0
        }
    1806
    1807
        #[test]
    1808
    0
        fn minus_op_dict() -> Result<()> {
    1809
    0
            let schema = Schema::new(vec![
    1810
    0
                Field::new(
    1811
                    "a",
    1812
    0
                    DataType::Dictionary(Box::new(DataType::Int8), Box::new(DataType::Int32)),
    1813
                    true,
    1814
                ),
    1815
    0
                Field::new(
    1816
                    "b",
    1817
    0
                    DataType::Dictionary(Box::new(DataType::Int8), Box::new(DataType::Int32)),
    1818
                    true,
    1819
                ),
    1820
            ]);
    1821
    1822
    0
            let a = Int32Array::from(vec![1, 2, 3, 4, 5]);
    1823
    0
            let keys = Int8Array::from(vec![Some(0), None, Some(1), Some(3), None]);
    1824
    0
            let a = DictionaryArray::try_new(keys, Arc::new(a))?;
    1825
    1826
    0
            let b = Int32Array::from(vec![1, 2, 4, 8, 16]);
    1827
    0
            let keys = Int8Array::from(vec![0, 1, 1, 2, 1]);
    1828
    0
            let b = DictionaryArray::try_new(keys, Arc::new(b))?;
    1829
    1830
    0
            apply_arithmetic::<Int32Type>(
    1831
    0
                Arc::new(schema),
    1832
    0
                vec![Arc::new(a), Arc::new(b)],
    1833
    0
                Operator::Minus,
    1834
    0
                Int32Array::from(vec![Some(0), None, Some(0), Some(0), None]),
    1835
    0
            )?;
    1836
    1837
    0
            Ok(())
    1838
    0
        }
    1839
    1840
        #[test]
    1841
    0
        fn minus_op_dict_decimal() -> Result<()> {
    1842
    0
            let schema = Schema::new(vec![
    1843
    0
                Field::new(
    1844
                    "a",
    1845
    0
                    DataType::Dictionary(
    1846
    0
                        Box::new(DataType::Int8),
    1847
    0
                        Box::new(DataType::Decimal128(10, 0)),
    1848
    0
                    ),
    1849
                    true,
    1850
                ),
    1851
    0
                Field::new(
    1852
                    "b",
    1853
    0
                    DataType::Dictionary(
    1854
    0
                        Box::new(DataType::Int8),
    1855
    0
                        Box::new(DataType::Decimal128(10, 0)),
    1856
    0
                    ),
    1857
                    true,
    1858
                ),
    1859
            ]);
    1860
    1861
    0
            let value = 123;
    1862
    0
            let decimal_array = Arc::new(create_decimal_array(
    1863
    0
                &[
    1864
    0
                    Some(value),
    1865
    0
                    Some(value + 2),
    1866
    0
                    Some(value - 1),
    1867
    0
                    Some(value + 1),
    1868
    0
                ],
    1869
                10,
    1870
                0,
    1871
            ));
    1872
    1873
    0
            let keys = Int8Array::from(vec![Some(0), Some(2), None, Some(3), Some(0)]);
    1874
    0
            let a = DictionaryArray::try_new(keys, decimal_array)?;
    1875
    1876
    0
            let keys = Int8Array::from(vec![Some(0), None, Some(3), Some(2), Some(2)]);
    1877
    0
            let decimal_array = Arc::new(create_decimal_array(
    1878
    0
                &[
    1879
    0
                    Some(value + 1),
    1880
    0
                    Some(value + 3),
    1881
    0
                    Some(value),
    1882
    0
                    Some(value + 2),
    1883
    0
                ],
    1884
                10,
    1885
                0,
    1886
            ));
    1887
    0
            let b = DictionaryArray::try_new(keys, decimal_array)?;
    1888
    1889
    0
            apply_arithmetic(
    1890
    0
                Arc::new(schema),
    1891
    0
                vec![Arc::new(a), Arc::new(b)],
    1892
    0
                Operator::Minus,
    1893
    0
                create_decimal_array(&[Some(-1), None, None, Some(1), Some(0)], 11, 0),
    1894
    0
            )?;
    1895
    1896
    0
            Ok(())
    1897
    0
        }
    1898
    1899
        #[test]
    1900
    0
        fn minus_op_scalar() -> Result<()> {
    1901
    0
            let schema = Schema::new(vec![Field::new("a", DataType::Int32, false)]);
    1902
    0
            let a = Int32Array::from(vec![1, 2, 3, 4, 5]);
    1903
    1904
    0
            apply_arithmetic_scalar(
    1905
    0
                Arc::new(schema),
    1906
    0
                vec![Arc::new(a)],
    1907
    0
                Operator::Minus,
    1908
    0
                ScalarValue::Int32(Some(1)),
    1909
    0
                Arc::new(Int32Array::from(vec![0, 1, 2, 3, 4])),
    1910
    0
            )?;
    1911
    1912
    0
            Ok(())
    1913
    0
        }
    1914
    1915
        #[test]
    1916
    0
        fn minus_op_dict_scalar() -> Result<()> {
    1917
    0
            let schema = Schema::new(vec![Field::new(
    1918
                "a",
    1919
    0
                DataType::Dictionary(Box::new(DataType::Int8), Box::new(DataType::Int32)),
    1920
                true,
    1921
            )]);
    1922
    1923
    0
            let mut dict_builder = PrimitiveDictionaryBuilder::<Int8Type, Int32Type>::new();
    1924
    1925
    0
            dict_builder.append(1)?;
    1926
    0
            dict_builder.append_null();
    1927
    0
            dict_builder.append(2)?;
    1928
    0
            dict_builder.append(5)?;
    1929
    1930
    0
            let a = dict_builder.finish();
    1931
    1932
    0
            let expected: PrimitiveArray<Int32Type> =
    1933
    0
                PrimitiveArray::from(vec![Some(0), None, Some(1), Some(4)]);
    1934
    1935
    0
            apply_arithmetic_scalar(
    1936
    0
                Arc::new(schema),
    1937
    0
                vec![Arc::new(a)],
    1938
    0
                Operator::Minus,
    1939
    0
                ScalarValue::Dictionary(
    1940
    0
                    Box::new(DataType::Int8),
    1941
    0
                    Box::new(ScalarValue::Int32(Some(1))),
    1942
    0
                ),
    1943
    0
                Arc::new(expected),
    1944
    0
            )?;
    1945
    1946
    0
            Ok(())
    1947
    0
        }
    1948
    1949
        #[test]
    1950
    0
        fn minus_op_dict_scalar_decimal() -> Result<()> {
    1951
    0
            let schema = Schema::new(vec![Field::new(
    1952
                "a",
    1953
    0
                DataType::Dictionary(
    1954
    0
                    Box::new(DataType::Int8),
    1955
    0
                    Box::new(DataType::Decimal128(10, 0)),
    1956
    0
                ),
    1957
                true,
    1958
            )]);
    1959
    1960
    0
            let value = 123;
    1961
    0
            let decimal_array = Arc::new(create_decimal_array(
    1962
    0
                &[Some(value), None, Some(value - 1), Some(value + 1)],
    1963
                10,
    1964
                0,
    1965
            ));
    1966
    1967
    0
            let keys = Int8Array::from(vec![0, 2, 1, 3, 0]);
    1968
    0
            let a = DictionaryArray::try_new(keys, decimal_array)?;
    1969
    1970
    0
            let decimal_array = Arc::new(create_decimal_array(
    1971
    0
                &[
    1972
    0
                    Some(value - 1),
    1973
    0
                    Some(value - 2),
    1974
    0
                    None,
    1975
    0
                    Some(value),
    1976
    0
                    Some(value - 1),
    1977
    0
                ],
    1978
                11,
    1979
                0,
    1980
            ));
    1981
    1982
    0
            apply_arithmetic_scalar(
    1983
    0
                Arc::new(schema),
    1984
    0
                vec![Arc::new(a)],
    1985
    0
                Operator::Minus,
    1986
    0
                ScalarValue::Dictionary(
    1987
    0
                    Box::new(DataType::Int8),
    1988
    0
                    Box::new(ScalarValue::Decimal128(Some(1), 10, 0)),
    1989
    0
                ),
    1990
    0
                decimal_array,
    1991
    0
            )?;
    1992
    1993
    0
            Ok(())
    1994
    0
        }
    1995
    1996
        #[test]
    1997
    0
        fn multiply_op() -> Result<()> {
    1998
    0
            let schema = Arc::new(Schema::new(vec![
    1999
    0
                Field::new("a", DataType::Int32, false),
    2000
    0
                Field::new("b", DataType::Int32, false),
    2001
            ]));
    2002
    0
            let a = Arc::new(Int32Array::from(vec![4, 8, 16, 32, 64]));
    2003
    0
            let b = Arc::new(Int32Array::from(vec![2, 4, 8, 16, 32]));
    2004
    2005
    0
            apply_arithmetic::<Int32Type>(
    2006
    0
                schema,
    2007
    0
                vec![a, b],
    2008
    0
                Operator::Multiply,
    2009
    0
                Int32Array::from(vec![8, 32, 128, 512, 2048]),
    2010
    0
            )?;
    2011
    2012
    0
            Ok(())
    2013
    0
        }
    2014
    2015
        #[test]
    2016
    0
        fn multiply_op_dict() -> Result<()> {
    2017
    0
            let schema = Schema::new(vec![
    2018
    0
                Field::new(
    2019
                    "a",
    2020
    0
                    DataType::Dictionary(Box::new(DataType::Int8), Box::new(DataType::Int32)),
    2021
                    true,
    2022
                ),
    2023
    0
                Field::new(
    2024
                    "b",
    2025
    0
                    DataType::Dictionary(Box::new(DataType::Int8), Box::new(DataType::Int32)),
    2026
                    true,
    2027
                ),
    2028
            ]);
    2029
    2030
    0
            let a = Int32Array::from(vec![1, 2, 3, 4, 5]);
    2031
    0
            let keys = Int8Array::from(vec![Some(0), None, Some(1), Some(3), None]);
    2032
    0
            let a = DictionaryArray::try_new(keys, Arc::new(a))?;
    2033
    2034
    0
            let b = Int32Array::from(vec![1, 2, 4, 8, 16]);
    2035
    0
            let keys = Int8Array::from(vec![0, 1, 1, 2, 1]);
    2036
    0
            let b = DictionaryArray::try_new(keys, Arc::new(b))?;
    2037
    2038
    0
            apply_arithmetic::<Int32Type>(
    2039
    0
                Arc::new(schema),
    2040
    0
                vec![Arc::new(a), Arc::new(b)],
    2041
    0
                Operator::Multiply,
    2042
    0
                Int32Array::from(vec![Some(1), None, Some(4), Some(16), None]),
    2043
    0
            )?;
    2044
    2045
    0
            Ok(())
    2046
    0
        }
    2047
    2048
        #[test]
    2049
    0
        fn multiply_op_dict_decimal() -> Result<()> {
    2050
    0
            let schema = Schema::new(vec![
    2051
    0
                Field::new(
    2052
                    "a",
    2053
    0
                    DataType::Dictionary(
    2054
    0
                        Box::new(DataType::Int8),
    2055
    0
                        Box::new(DataType::Decimal128(10, 0)),
    2056
    0
                    ),
    2057
                    true,
    2058
                ),
    2059
    0
                Field::new(
    2060
                    "b",
    2061
    0
                    DataType::Dictionary(
    2062
    0
                        Box::new(DataType::Int8),
    2063
    0
                        Box::new(DataType::Decimal128(10, 0)),
    2064
    0
                    ),
    2065
                    true,
    2066
                ),
    2067
            ]);
    2068
    2069
    0
            let value = 123;
    2070
    0
            let decimal_array = Arc::new(create_decimal_array(
    2071
    0
                &[
    2072
    0
                    Some(value),
    2073
    0
                    Some(value + 2),
    2074
    0
                    Some(value - 1),
    2075
    0
                    Some(value + 1),
    2076
    0
                ],
    2077
    0
                10,
    2078
    0
                0,
    2079
    0
            )) as ArrayRef;
    2080
    2081
    0
            let keys = Int8Array::from(vec![Some(0), Some(2), None, Some(3), Some(0)]);
    2082
    0
            let a = DictionaryArray::try_new(keys, decimal_array)?;
    2083
    2084
    0
            let keys = Int8Array::from(vec![Some(0), None, Some(3), Some(2), Some(2)]);
    2085
    0
            let decimal_array = Arc::new(create_decimal_array(
    2086
    0
                &[
    2087
    0
                    Some(value + 1),
    2088
    0
                    Some(value + 3),
    2089
    0
                    Some(value),
    2090
    0
                    Some(value + 2),
    2091
    0
                ],
    2092
                10,
    2093
                0,
    2094
            ));
    2095
    0
            let b = DictionaryArray::try_new(keys, decimal_array)?;
    2096
    2097
    0
            apply_arithmetic(
    2098
    0
                Arc::new(schema),
    2099
    0
                vec![Arc::new(a), Arc::new(b)],
    2100
    0
                Operator::Multiply,
    2101
    0
                create_decimal_array(
    2102
    0
                    &[Some(15252), None, None, Some(15252), Some(15129)],
    2103
                    21,
    2104
                    0,
    2105
                ),
    2106
    0
            )?;
    2107
    2108
    0
            Ok(())
    2109
    0
        }
    2110
    2111
        #[test]
    2112
    0
        fn multiply_op_scalar() -> Result<()> {
    2113
    0
            let schema = Schema::new(vec![Field::new("a", DataType::Int32, false)]);
    2114
    0
            let a = Int32Array::from(vec![1, 2, 3, 4, 5]);
    2115
    2116
    0
            apply_arithmetic_scalar(
    2117
    0
                Arc::new(schema),
    2118
    0
                vec![Arc::new(a)],
    2119
    0
                Operator::Multiply,
    2120
    0
                ScalarValue::Int32(Some(2)),
    2121
    0
                Arc::new(Int32Array::from(vec![2, 4, 6, 8, 10])),
    2122
    0
            )?;
    2123
    2124
    0
            Ok(())
    2125
    0
        }
    2126
    2127
        #[test]
    2128
    0
        fn multiply_op_dict_scalar() -> Result<()> {
    2129
    0
            let schema = Schema::new(vec![Field::new(
    2130
                "a",
    2131
    0
                DataType::Dictionary(Box::new(DataType::Int8), Box::new(DataType::Int32)),
    2132
                true,
    2133
            )]);
    2134
    2135
    0
            let mut dict_builder = PrimitiveDictionaryBuilder::<Int8Type, Int32Type>::new();
    2136
    2137
    0
            dict_builder.append(1)?;
    2138
    0
            dict_builder.append_null();
    2139
    0
            dict_builder.append(2)?;
    2140
    0
            dict_builder.append(5)?;
    2141
    2142
    0
            let a = dict_builder.finish();
    2143
    2144
    0
            let expected: PrimitiveArray<Int32Type> =
    2145
    0
                PrimitiveArray::from(vec![Some(2), None, Some(4), Some(10)]);
    2146
    2147
    0
            apply_arithmetic_scalar(
    2148
    0
                Arc::new(schema),
    2149
    0
                vec![Arc::new(a)],
    2150
    0
                Operator::Multiply,
    2151
    0
                ScalarValue::Dictionary(
    2152
    0
                    Box::new(DataType::Int8),
    2153
    0
                    Box::new(ScalarValue::Int32(Some(2))),
    2154
    0
                ),
    2155
    0
                Arc::new(expected),
    2156
    0
            )?;
    2157
    2158
    0
            Ok(())
    2159
    0
        }
    2160
    2161
        #[test]
    2162
    0
        fn multiply_op_dict_scalar_decimal() -> Result<()> {
    2163
    0
            let schema = Schema::new(vec![Field::new(
    2164
                "a",
    2165
    0
                DataType::Dictionary(
    2166
    0
                    Box::new(DataType::Int8),
    2167
    0
                    Box::new(DataType::Decimal128(10, 0)),
    2168
    0
                ),
    2169
                true,
    2170
            )]);
    2171
    2172
    0
            let value = 123;
    2173
    0
            let decimal_array = Arc::new(create_decimal_array(
    2174
    0
                &[Some(value), None, Some(value - 1), Some(value + 1)],
    2175
                10,
    2176
                0,
    2177
            ));
    2178
    2179
    0
            let keys = Int8Array::from(vec![0, 2, 1, 3, 0]);
    2180
    0
            let a = DictionaryArray::try_new(keys, decimal_array)?;
    2181
    2182
    0
            let decimal_array = Arc::new(create_decimal_array(
    2183
    0
                &[Some(246), Some(244), None, Some(248), Some(246)],
    2184
                21,
    2185
                0,
    2186
            ));
    2187
    2188
    0
            apply_arithmetic_scalar(
    2189
    0
                Arc::new(schema),
    2190
    0
                vec![Arc::new(a)],
    2191
    0
                Operator::Multiply,
    2192
    0
                ScalarValue::Dictionary(
    2193
    0
                    Box::new(DataType::Int8),
    2194
    0
                    Box::new(ScalarValue::Decimal128(Some(2), 10, 0)),
    2195
    0
                ),
    2196
    0
                decimal_array,
    2197
    0
            )?;
    2198
    2199
    0
            Ok(())
    2200
    0
        }
    2201
    2202
        #[test]
    2203
    0
        fn divide_op() -> Result<()> {
    2204
    0
            let schema = Arc::new(Schema::new(vec![
    2205
    0
                Field::new("a", DataType::Int32, false),
    2206
    0
                Field::new("b", DataType::Int32, false),
    2207
            ]));
    2208
    0
            let a = Arc::new(Int32Array::from(vec![8, 32, 128, 512, 2048]));
    2209
    0
            let b = Arc::new(Int32Array::from(vec![2, 4, 8, 16, 32]));
    2210
    2211
    0
            apply_arithmetic::<Int32Type>(
    2212
    0
                schema,
    2213
    0
                vec![a, b],
    2214
    0
                Operator::Divide,
    2215
    0
                Int32Array::from(vec![4, 8, 16, 32, 64]),
    2216
    0
            )?;
    2217
    2218
    0
            Ok(())
    2219
    0
        }
    2220
    2221
        #[test]
    2222
    0
        fn divide_op_dict() -> Result<()> {
    2223
    0
            let schema = Schema::new(vec![
    2224
    0
                Field::new(
    2225
                    "a",
    2226
    0
                    DataType::Dictionary(Box::new(DataType::Int8), Box::new(DataType::Int32)),
    2227
                    true,
    2228
                ),
    2229
    0
                Field::new(
    2230
                    "b",
    2231
    0
                    DataType::Dictionary(Box::new(DataType::Int8), Box::new(DataType::Int32)),
    2232
                    true,
    2233
                ),
    2234
            ]);
    2235
    2236
    0
            let mut dict_builder = PrimitiveDictionaryBuilder::<Int8Type, Int32Type>::new();
    2237
    2238
    0
            dict_builder.append(1)?;
    2239
    0
            dict_builder.append_null();
    2240
    0
            dict_builder.append(2)?;
    2241
    0
            dict_builder.append(5)?;
    2242
    0
            dict_builder.append(0)?;
    2243
    2244
    0
            let a = dict_builder.finish();
    2245
    2246
    0
            let b = Int32Array::from(vec![1, 2, 4, 8, 16]);
    2247
    0
            let keys = Int8Array::from(vec![0, 1, 1, 2, 1]);
    2248
    0
            let b = DictionaryArray::try_new(keys, Arc::new(b))?;
    2249
    2250
    0
            apply_arithmetic::<Int32Type>(
    2251
    0
                Arc::new(schema),
    2252
    0
                vec![Arc::new(a), Arc::new(b)],
    2253
    0
                Operator::Divide,
    2254
    0
                Int32Array::from(vec![Some(1), None, Some(1), Some(1), Some(0)]),
    2255
    0
            )?;
    2256
    2257
    0
            Ok(())
    2258
    0
        }
    2259
    2260
        #[test]
    2261
    0
        fn divide_op_dict_decimal() -> Result<()> {
    2262
    0
            let schema = Schema::new(vec![
    2263
    0
                Field::new(
    2264
                    "a",
    2265
    0
                    DataType::Dictionary(
    2266
    0
                        Box::new(DataType::Int8),
    2267
    0
                        Box::new(DataType::Decimal128(10, 0)),
    2268
    0
                    ),
    2269
                    true,
    2270
                ),
    2271
    0
                Field::new(
    2272
                    "b",
    2273
    0
                    DataType::Dictionary(
    2274
    0
                        Box::new(DataType::Int8),
    2275
    0
                        Box::new(DataType::Decimal128(10, 0)),
    2276
    0
                    ),
    2277
                    true,
    2278
                ),
    2279
            ]);
    2280
    2281
    0
            let value = 123;
    2282
    0
            let decimal_array = Arc::new(create_decimal_array(
    2283
    0
                &[
    2284
    0
                    Some(value),
    2285
    0
                    Some(value + 2),
    2286
    0
                    Some(value - 1),
    2287
    0
                    Some(value + 1),
    2288
    0
                ],
    2289
                10,
    2290
                0,
    2291
            ));
    2292
    2293
    0
            let keys = Int8Array::from(vec![Some(0), Some(2), None, Some(3), Some(0)]);
    2294
    0
            let a = DictionaryArray::try_new(keys, decimal_array)?;
    2295
    2296
    0
            let keys = Int8Array::from(vec![Some(0), None, Some(3), Some(2), Some(2)]);
    2297
    0
            let decimal_array = Arc::new(create_decimal_array(
    2298
    0
                &[
    2299
    0
                    Some(value + 1),
    2300
    0
                    Some(value + 3),
    2301
    0
                    Some(value),
    2302
    0
                    Some(value + 2),
    2303
    0
                ],
    2304
                10,
    2305
                0,
    2306
            ));
    2307
    0
            let b = DictionaryArray::try_new(keys, decimal_array)?;
    2308
    2309
    0
            apply_arithmetic(
    2310
    0
                Arc::new(schema),
    2311
    0
                vec![Arc::new(a), Arc::new(b)],
    2312
    0
                Operator::Divide,
    2313
    0
                create_decimal_array(
    2314
    0
                    &[
    2315
    0
                        Some(9919), // 0.9919
    2316
    0
                        None,
    2317
    0
                        None,
    2318
    0
                        Some(10081), // 1.0081
    2319
    0
                        Some(10000), // 1.0
    2320
    0
                    ],
    2321
                    14,
    2322
                    4,
    2323
                ),
    2324
    0
            )?;
    2325
    2326
    0
            Ok(())
    2327
    0
        }
    2328
    2329
        #[test]
    2330
    0
        fn divide_op_scalar() -> Result<()> {
    2331
    0
            let schema = Schema::new(vec![Field::new("a", DataType::Int32, false)]);
    2332
    0
            let a = Int32Array::from(vec![1, 2, 3, 4, 5]);
    2333
    2334
    0
            apply_arithmetic_scalar(
    2335
    0
                Arc::new(schema),
    2336
    0
                vec![Arc::new(a)],
    2337
    0
                Operator::Divide,
    2338
    0
                ScalarValue::Int32(Some(2)),
    2339
    0
                Arc::new(Int32Array::from(vec![0, 1, 1, 2, 2])),
    2340
    0
            )?;
    2341
    2342
    0
            Ok(())
    2343
    0
        }
    2344
    2345
        #[test]
    2346
    0
        fn divide_op_dict_scalar() -> Result<()> {
    2347
    0
            let schema = Schema::new(vec![Field::new(
    2348
                "a",
    2349
    0
                DataType::Dictionary(Box::new(DataType::Int8), Box::new(DataType::Int32)),
    2350
                true,
    2351
            )]);
    2352
    2353
    0
            let mut dict_builder = PrimitiveDictionaryBuilder::<Int8Type, Int32Type>::new();
    2354
    2355
    0
            dict_builder.append(1)?;
    2356
    0
            dict_builder.append_null();
    2357
    0
            dict_builder.append(2)?;
    2358
    0
            dict_builder.append(5)?;
    2359
    2360
    0
            let a = dict_builder.finish();
    2361
    2362
    0
            let expected: PrimitiveArray<Int32Type> =
    2363
    0
                PrimitiveArray::from(vec![Some(0), None, Some(1), Some(2)]);
    2364
    2365
    0
            apply_arithmetic_scalar(
    2366
    0
                Arc::new(schema),
    2367
    0
                vec![Arc::new(a)],
    2368
    0
                Operator::Divide,
    2369
    0
                ScalarValue::Dictionary(
    2370
    0
                    Box::new(DataType::Int8),
    2371
    0
                    Box::new(ScalarValue::Int32(Some(2))),
    2372
    0
                ),
    2373
    0
                Arc::new(expected),
    2374
    0
            )?;
    2375
    2376
    0
            Ok(())
    2377
    0
        }
    2378
    2379
        #[test]
    2380
    0
        fn divide_op_dict_scalar_decimal() -> Result<()> {
    2381
    0
            let schema = Schema::new(vec![Field::new(
    2382
                "a",
    2383
    0
                DataType::Dictionary(
    2384
    0
                    Box::new(DataType::Int8),
    2385
    0
                    Box::new(DataType::Decimal128(10, 0)),
    2386
    0
                ),
    2387
                true,
    2388
            )]);
    2389
    2390
    0
            let value = 123;
    2391
    0
            let decimal_array = Arc::new(create_decimal_array(
    2392
    0
                &[Some(value), None, Some(value - 1), Some(value + 1)],
    2393
                10,
    2394
                0,
    2395
            ));
    2396
    2397
    0
            let keys = Int8Array::from(vec![0, 2, 1, 3, 0]);
    2398
    0
            let a = DictionaryArray::try_new(keys, decimal_array)?;
    2399
    2400
    0
            let decimal_array = Arc::new(create_decimal_array(
    2401
    0
                &[Some(615000), Some(610000), None, Some(620000), Some(615000)],
    2402
                14,
    2403
                4,
    2404
            ));
    2405
    2406
    0
            apply_arithmetic_scalar(
    2407
    0
                Arc::new(schema),
    2408
    0
                vec![Arc::new(a)],
    2409
    0
                Operator::Divide,
    2410
    0
                ScalarValue::Dictionary(
    2411
    0
                    Box::new(DataType::Int8),
    2412
    0
                    Box::new(ScalarValue::Decimal128(Some(2), 10, 0)),
    2413
    0
                ),
    2414
    0
                decimal_array,
    2415
    0
            )?;
    2416
    2417
    0
            Ok(())
    2418
    0
        }
    2419
    2420
        #[test]
    2421
    0
        fn modulus_op() -> Result<()> {
    2422
    0
            let schema = Arc::new(Schema::new(vec![
    2423
    0
                Field::new("a", DataType::Int32, false),
    2424
    0
                Field::new("b", DataType::Int32, false),
    2425
            ]));
    2426
    0
            let a = Arc::new(Int32Array::from(vec![8, 32, 128, 512, 2048]));
    2427
    0
            let b = Arc::new(Int32Array::from(vec![2, 4, 7, 14, 32]));
    2428
    2429
    0
            apply_arithmetic::<Int32Type>(
    2430
    0
                schema,
    2431
    0
                vec![a, b],
    2432
    0
                Operator::Modulo,
    2433
    0
                Int32Array::from(vec![0, 0, 2, 8, 0]),
    2434
    0
            )?;
    2435
    2436
    0
            Ok(())
    2437
    0
        }
    2438
    2439
        #[test]
    2440
    0
        fn modulus_op_dict() -> Result<()> {
    2441
    0
            let schema = Schema::new(vec![
    2442
    0
                Field::new(
    2443
                    "a",
    2444
    0
                    DataType::Dictionary(Box::new(DataType::Int8), Box::new(DataType::Int32)),
    2445
                    true,
    2446
                ),
    2447
    0
                Field::new(
    2448
                    "b",
    2449
    0
                    DataType::Dictionary(Box::new(DataType::Int8), Box::new(DataType::Int32)),
    2450
                    true,
    2451
                ),
    2452
            ]);
    2453
    2454
    0
            let mut dict_builder = PrimitiveDictionaryBuilder::<Int8Type, Int32Type>::new();
    2455
    2456
    0
            dict_builder.append(1)?;
    2457
    0
            dict_builder.append_null();
    2458
    0
            dict_builder.append(2)?;
    2459
    0
            dict_builder.append(5)?;
    2460
    0
            dict_builder.append(0)?;
    2461
    2462
    0
            let a = dict_builder.finish();
    2463
    2464
    0
            let b = Int32Array::from(vec![1, 2, 4, 8, 16]);
    2465
    0
            let keys = Int8Array::from(vec![0, 1, 1, 2, 1]);
    2466
    0
            let b = DictionaryArray::try_new(keys, Arc::new(b))?;
    2467
    2468
    0
            apply_arithmetic::<Int32Type>(
    2469
    0
                Arc::new(schema),
    2470
    0
                vec![Arc::new(a), Arc::new(b)],
    2471
    0
                Operator::Modulo,
    2472
    0
                Int32Array::from(vec![Some(0), None, Some(0), Some(1), Some(0)]),
    2473
    0
            )?;
    2474
    2475
    0
            Ok(())
    2476
    0
        }
    2477
    2478
        #[test]
    2479
    0
        fn modulus_op_dict_decimal() -> Result<()> {
    2480
    0
            let schema = Schema::new(vec![
    2481
    0
                Field::new(
    2482
                    "a",
    2483
    0
                    DataType::Dictionary(
    2484
    0
                        Box::new(DataType::Int8),
    2485
    0
                        Box::new(DataType::Decimal128(10, 0)),
    2486
    0
                    ),
    2487
                    true,
    2488
                ),
    2489
    0
                Field::new(
    2490
                    "b",
    2491
    0
                    DataType::Dictionary(
    2492
    0
                        Box::new(DataType::Int8),
    2493
    0
                        Box::new(DataType::Decimal128(10, 0)),
    2494
    0
                    ),
    2495
                    true,
    2496
                ),
    2497
            ]);
    2498
    2499
    0
            let value = 123;
    2500
    0
            let decimal_array = Arc::new(create_decimal_array(
    2501
    0
                &[
    2502
    0
                    Some(value),
    2503
    0
                    Some(value + 2),
    2504
    0
                    Some(value - 1),
    2505
    0
                    Some(value + 1),
    2506
    0
                ],
    2507
                10,
    2508
                0,
    2509
            ));
    2510
    2511
    0
            let keys = Int8Array::from(vec![Some(0), Some(2), None, Some(3), Some(0)]);
    2512
    0
            let a = DictionaryArray::try_new(keys, decimal_array)?;
    2513
    2514
    0
            let keys = Int8Array::from(vec![Some(0), None, Some(3), Some(2), Some(2)]);
    2515
    0
            let decimal_array = Arc::new(create_decimal_array(
    2516
    0
                &[
    2517
    0
                    Some(value + 1),
    2518
    0
                    Some(value + 3),
    2519
    0
                    Some(value),
    2520
    0
                    Some(value + 2),
    2521
    0
                ],
    2522
                10,
    2523
                0,
    2524
            ));
    2525
    0
            let b = DictionaryArray::try_new(keys, decimal_array)?;
    2526
    2527
    0
            apply_arithmetic(
    2528
    0
                Arc::new(schema),
    2529
    0
                vec![Arc::new(a), Arc::new(b)],
    2530
    0
                Operator::Modulo,
    2531
    0
                create_decimal_array(&[Some(123), None, None, Some(1), Some(0)], 10, 0),
    2532
    0
            )?;
    2533
    2534
    0
            Ok(())
    2535
    0
        }
    2536
    2537
        #[test]
    2538
    0
        fn modulus_op_scalar() -> Result<()> {
    2539
    0
            let schema = Schema::new(vec![Field::new("a", DataType::Int32, false)]);
    2540
    0
            let a = Int32Array::from(vec![1, 2, 3, 4, 5]);
    2541
    2542
    0
            apply_arithmetic_scalar(
    2543
    0
                Arc::new(schema),
    2544
    0
                vec![Arc::new(a)],
    2545
    0
                Operator::Modulo,
    2546
    0
                ScalarValue::Int32(Some(2)),
    2547
    0
                Arc::new(Int32Array::from(vec![1, 0, 1, 0, 1])),
    2548
    0
            )?;
    2549
    2550
    0
            Ok(())
    2551
    0
        }
    2552
    2553
        #[test]
    2554
    0
        fn modules_op_dict_scalar() -> Result<()> {
    2555
    0
            let schema = Schema::new(vec![Field::new(
    2556
                "a",
    2557
    0
                DataType::Dictionary(Box::new(DataType::Int8), Box::new(DataType::Int32)),
    2558
                true,
    2559
            )]);
    2560
    2561
    0
            let mut dict_builder = PrimitiveDictionaryBuilder::<Int8Type, Int32Type>::new();
    2562
    2563
    0
            dict_builder.append(1)?;
    2564
    0
            dict_builder.append_null();
    2565
    0
            dict_builder.append(2)?;
    2566
    0
            dict_builder.append(5)?;
    2567
    2568
    0
            let a = dict_builder.finish();
    2569
    2570
    0
            let expected: PrimitiveArray<Int32Type> =
    2571
    0
                PrimitiveArray::from(vec![Some(1), None, Some(0), Some(1)]);
    2572
    2573
    0
            apply_arithmetic_scalar(
    2574
    0
                Arc::new(schema),
    2575
    0
                vec![Arc::new(a)],
    2576
    0
                Operator::Modulo,
    2577
    0
                ScalarValue::Dictionary(
    2578
    0
                    Box::new(DataType::Int8),
    2579
    0
                    Box::new(ScalarValue::Int32(Some(2))),
    2580
    0
                ),
    2581
    0
                Arc::new(expected),
    2582
    0
            )?;
    2583
    2584
    0
            Ok(())
    2585
    0
        }
    2586
    2587
        #[test]
    2588
    0
        fn modulus_op_dict_scalar_decimal() -> Result<()> {
    2589
    0
            let schema = Schema::new(vec![Field::new(
    2590
                "a",
    2591
    0
                DataType::Dictionary(
    2592
    0
                    Box::new(DataType::Int8),
    2593
    0
                    Box::new(DataType::Decimal128(10, 0)),
    2594
    0
                ),
    2595
                true,
    2596
            )]);
    2597
    2598
    0
            let value = 123;
    2599
    0
            let decimal_array = Arc::new(create_decimal_array(
    2600
    0
                &[Some(value), None, Some(value - 1), Some(value + 1)],
    2601
                10,
    2602
                0,
    2603
            ));
    2604
    2605
    0
            let keys = Int8Array::from(vec![0, 2, 1, 3, 0]);
    2606
    0
            let a = DictionaryArray::try_new(keys, decimal_array)?;
    2607
    2608
    0
            let decimal_array = Arc::new(create_decimal_array(
    2609
    0
                &[Some(1), Some(0), None, Some(0), Some(1)],
    2610
                10,
    2611
                0,
    2612
            ));
    2613
    2614
    0
            apply_arithmetic_scalar(
    2615
    0
                Arc::new(schema),
    2616
    0
                vec![Arc::new(a)],
    2617
    0
                Operator::Modulo,
    2618
    0
                ScalarValue::Dictionary(
    2619
    0
                    Box::new(DataType::Int8),
    2620
    0
                    Box::new(ScalarValue::Decimal128(Some(2), 10, 0)),
    2621
    0
                ),
    2622
    0
                decimal_array,
    2623
    0
            )?;
    2624
    2625
    0
            Ok(())
    2626
    0
        }
    2627
    2628
    0
        fn apply_arithmetic<T: ArrowNumericType>(
    2629
    0
            schema: SchemaRef,
    2630
    0
            data: Vec<ArrayRef>,
    2631
    0
            op: Operator,
    2632
    0
            expected: PrimitiveArray<T>,
    2633
    0
        ) -> Result<()> {
    2634
    0
            let arithmetic_op =
    2635
    0
                binary_op(col("a", &schema)?, op, col("b", &schema)?, &schema)?;
    2636
    0
            let batch = RecordBatch::try_new(schema, data)?;
    2637
    0
            let result = arithmetic_op
    2638
    0
                .evaluate(&batch)?
    2639
    0
                .into_array(batch.num_rows())
    2640
    0
                .expect("Failed to convert to array");
    2641
    2642
    0
            assert_eq!(result.as_ref(), &expected);
    2643
    0
            Ok(())
    2644
    0
        }
    2645
    2646
    0
        fn apply_arithmetic_scalar(
    2647
    0
            schema: SchemaRef,
    2648
    0
            data: Vec<ArrayRef>,
    2649
    0
            op: Operator,
    2650
    0
            literal: ScalarValue,
    2651
    0
            expected: ArrayRef,
    2652
    0
        ) -> Result<()> {
    2653
    0
            let lit = Arc::new(Literal::new(literal));
    2654
    0
            let arithmetic_op = binary_op(col("a", &schema)?, op, lit, &schema)?;
    2655
    0
            let batch = RecordBatch::try_new(schema, data)?;
    2656
    0
            let result = arithmetic_op
    2657
    0
                .evaluate(&batch)?
    2658
    0
                .into_array(batch.num_rows())
    2659
    0
                .expect("Failed to convert to array");
    2660
    2661
    0
            assert_eq!(&result, &expected);
    2662
    0
            Ok(())
    2663
    0
        }
    2664
    2665
    0
        fn apply_logic_op(
    2666
    0
            schema: &SchemaRef,
    2667
    0
            left: &ArrayRef,
    2668
    0
            right: &ArrayRef,
    2669
    0
            op: Operator,
    2670
    0
            expected: BooleanArray,
    2671
    0
        ) -> Result<()> {
    2672
    0
            let op = binary_op(col("a", schema)?, op, col("b", schema)?, schema)?;
    2673
    0
            let data: Vec<ArrayRef> = vec![Arc::clone(left), Arc::clone(right)];
    2674
    0
            let batch = RecordBatch::try_new(Arc::clone(schema), data)?;
    2675
    0
            let result = op
    2676
    0
                .evaluate(&batch)?
    2677
    0
                .into_array(batch.num_rows())
    2678
    0
                .expect("Failed to convert to array");
    2679
    2680
    0
            assert_eq!(result.as_ref(), &expected);
    2681
    0
            Ok(())
    2682
    0
        }
    2683
    2684
        // Test `scalar <op> arr` produces expected
    2685
    0
        fn apply_logic_op_scalar_arr(
    2686
    0
            schema: &SchemaRef,
    2687
    0
            scalar: &ScalarValue,
    2688
    0
            arr: &ArrayRef,
    2689
    0
            op: Operator,
    2690
    0
            expected: &BooleanArray,
    2691
    0
        ) -> Result<()> {
    2692
    0
            let scalar = lit(scalar.clone());
    2693
    0
            let op = binary_op(scalar, op, col("a", schema)?, schema)?;
    2694
    0
            let batch = RecordBatch::try_new(Arc::clone(schema), vec![Arc::clone(arr)])?;
    2695
    0
            let result = op
    2696
    0
                .evaluate(&batch)?
    2697
    0
                .into_array(batch.num_rows())
    2698
    0
                .expect("Failed to convert to array");
    2699
    0
            assert_eq!(result.as_ref(), expected);
    2700
    2701
    0
            Ok(())
    2702
    0
        }
    2703
    2704
        // Test `arr <op> scalar` produces expected
    2705
    0
        fn apply_logic_op_arr_scalar(
    2706
    0
            schema: &SchemaRef,
    2707
    0
            arr: &ArrayRef,
    2708
    0
            scalar: &ScalarValue,
    2709
    0
            op: Operator,
    2710
    0
            expected: &BooleanArray,
    2711
    0
        ) -> Result<()> {
    2712
    0
            let scalar = lit(scalar.clone());
    2713
    0
            let op = binary_op(col("a", schema)?, op, scalar, schema)?;
    2714
    0
            let batch = RecordBatch::try_new(Arc::clone(schema), vec![Arc::clone(arr)])?;
    2715
    0
            let result = op
    2716
    0
                .evaluate(&batch)?
    2717
    0
                .into_array(batch.num_rows())
    2718
    0
                .expect("Failed to convert to array");
    2719
    0
            assert_eq!(result.as_ref(), expected);
    2720
    2721
    0
            Ok(())
    2722
    0
        }
    2723
    2724
        #[test]
    2725
    0
        fn and_with_nulls_op() -> Result<()> {
    2726
    0
            let schema = Schema::new(vec![
    2727
    0
                Field::new("a", DataType::Boolean, true),
    2728
    0
                Field::new("b", DataType::Boolean, true),
    2729
            ]);
    2730
    0
            let a = Arc::new(BooleanArray::from(vec![
    2731
    0
                Some(true),
    2732
    0
                Some(false),
    2733
    0
                None,
    2734
    0
                Some(true),
    2735
    0
                Some(false),
    2736
    0
                None,
    2737
    0
                Some(true),
    2738
    0
                Some(false),
    2739
    0
                None,
    2740
    0
            ])) as ArrayRef;
    2741
    0
            let b = Arc::new(BooleanArray::from(vec![
    2742
    0
                Some(true),
    2743
    0
                Some(true),
    2744
    0
                Some(true),
    2745
    0
                Some(false),
    2746
    0
                Some(false),
    2747
    0
                Some(false),
    2748
    0
                None,
    2749
    0
                None,
    2750
    0
                None,
    2751
    0
            ])) as ArrayRef;
    2752
    2753
    0
            let expected = BooleanArray::from(vec![
    2754
    0
                Some(true),
    2755
    0
                Some(false),
    2756
    0
                None,
    2757
    0
                Some(false),
    2758
    0
                Some(false),
    2759
    0
                Some(false),
    2760
    0
                None,
    2761
    0
                Some(false),
    2762
    0
                None,
    2763
            ]);
    2764
    0
            apply_logic_op(&Arc::new(schema), &a, &b, Operator::And, expected)?;
    2765
    2766
    0
            Ok(())
    2767
    0
        }
    2768
    2769
        #[test]
    2770
    0
        fn regex_with_nulls() -> Result<()> {
    2771
    0
            let schema = Schema::new(vec![
    2772
    0
                Field::new("a", DataType::Utf8, true),
    2773
    0
                Field::new("b", DataType::Utf8, true),
    2774
            ]);
    2775
    0
            let a = Arc::new(StringArray::from(vec![
    2776
    0
                Some("abc"),
    2777
    0
                None,
    2778
    0
                Some("abc"),
    2779
    0
                None,
    2780
    0
                Some("abc"),
    2781
    0
            ])) as ArrayRef;
    2782
    0
            let b = Arc::new(StringArray::from(vec![
    2783
    0
                Some("^a"),
    2784
    0
                Some("^A"),
    2785
    0
                None,
    2786
    0
                None,
    2787
    0
                Some("^(b|c)"),
    2788
    0
            ])) as ArrayRef;
    2789
    2790
    0
            let regex_expected =
    2791
    0
                BooleanArray::from(vec![Some(true), None, None, None, Some(false)]);
    2792
    0
            let regex_not_expected =
    2793
    0
                BooleanArray::from(vec![Some(false), None, None, None, Some(true)]);
    2794
    0
            apply_logic_op(
    2795
    0
                &Arc::new(schema.clone()),
    2796
    0
                &a,
    2797
    0
                &b,
    2798
    0
                Operator::RegexMatch,
    2799
    0
                regex_expected.clone(),
    2800
    0
            )?;
    2801
    0
            apply_logic_op(
    2802
    0
                &Arc::new(schema.clone()),
    2803
    0
                &a,
    2804
    0
                &b,
    2805
    0
                Operator::RegexIMatch,
    2806
    0
                regex_expected.clone(),
    2807
    0
            )?;
    2808
    0
            apply_logic_op(
    2809
    0
                &Arc::new(schema.clone()),
    2810
    0
                &a,
    2811
    0
                &b,
    2812
    0
                Operator::RegexNotMatch,
    2813
    0
                regex_not_expected.clone(),
    2814
    0
            )?;
    2815
    0
            apply_logic_op(
    2816
    0
                &Arc::new(schema),
    2817
    0
                &a,
    2818
    0
                &b,
    2819
    0
                Operator::RegexNotIMatch,
    2820
    0
                regex_not_expected.clone(),
    2821
    0
            )?;
    2822
    2823
    0
            let schema = Schema::new(vec![
    2824
    0
                Field::new("a", DataType::LargeUtf8, true),
    2825
    0
                Field::new("b", DataType::LargeUtf8, true),
    2826
            ]);
    2827
    0
            let a = Arc::new(LargeStringArray::from(vec![
    2828
    0
                Some("abc"),
    2829
    0
                None,
    2830
    0
                Some("abc"),
    2831
    0
                None,
    2832
    0
                Some("abc"),
    2833
    0
            ])) as ArrayRef;
    2834
    0
            let b = Arc::new(LargeStringArray::from(vec![
    2835
    0
                Some("^a"),
    2836
    0
                Some("^A"),
    2837
    0
                None,
    2838
    0
                None,
    2839
    0
                Some("^(b|c)"),
    2840
    0
            ])) as ArrayRef;
    2841
    2842
    0
            apply_logic_op(
    2843
    0
                &Arc::new(schema.clone()),
    2844
    0
                &a,
    2845
    0
                &b,
    2846
    0
                Operator::RegexMatch,
    2847
    0
                regex_expected.clone(),
    2848
    0
            )?;
    2849
    0
            apply_logic_op(
    2850
    0
                &Arc::new(schema.clone()),
    2851
    0
                &a,
    2852
    0
                &b,
    2853
    0
                Operator::RegexIMatch,
    2854
    0
                regex_expected,
    2855
    0
            )?;
    2856
    0
            apply_logic_op(
    2857
    0
                &Arc::new(schema.clone()),
    2858
    0
                &a,
    2859
    0
                &b,
    2860
    0
                Operator::RegexNotMatch,
    2861
    0
                regex_not_expected.clone(),
    2862
    0
            )?;
    2863
    0
            apply_logic_op(
    2864
    0
                &Arc::new(schema),
    2865
    0
                &a,
    2866
    0
                &b,
    2867
    0
                Operator::RegexNotIMatch,
    2868
    0
                regex_not_expected,
    2869
    0
            )?;
    2870
    2871
    0
            Ok(())
    2872
    0
        }
    2873
    2874
        #[test]
    2875
    0
        fn or_with_nulls_op() -> Result<()> {
    2876
    0
            let schema = Schema::new(vec![
    2877
    0
                Field::new("a", DataType::Boolean, true),
    2878
    0
                Field::new("b", DataType::Boolean, true),
    2879
            ]);
    2880
    0
            let a = Arc::new(BooleanArray::from(vec![
    2881
    0
                Some(true),
    2882
    0
                Some(false),
    2883
    0
                None,
    2884
    0
                Some(true),
    2885
    0
                Some(false),
    2886
    0
                None,
    2887
    0
                Some(true),
    2888
    0
                Some(false),
    2889
    0
                None,
    2890
    0
            ])) as ArrayRef;
    2891
    0
            let b = Arc::new(BooleanArray::from(vec![
    2892
    0
                Some(true),
    2893
    0
                Some(true),
    2894
    0
                Some(true),
    2895
    0
                Some(false),
    2896
    0
                Some(false),
    2897
    0
                Some(false),
    2898
    0
                None,
    2899
    0
                None,
    2900
    0
                None,
    2901
    0
            ])) as ArrayRef;
    2902
    2903
    0
            let expected = BooleanArray::from(vec![
    2904
    0
                Some(true),
    2905
    0
                Some(true),
    2906
    0
                Some(true),
    2907
    0
                Some(true),
    2908
    0
                Some(false),
    2909
    0
                None,
    2910
    0
                Some(true),
    2911
    0
                None,
    2912
    0
                None,
    2913
            ]);
    2914
    0
            apply_logic_op(&Arc::new(schema), &a, &b, Operator::Or, expected)?;
    2915
    2916
    0
            Ok(())
    2917
    0
        }
    2918
    2919
        /// Returns (schema, a: BooleanArray, b: BooleanArray) with all possible inputs
    2920
        ///
    2921
        /// a: [true, true, true,  NULL, NULL, NULL,  false, false, false]
    2922
        /// b: [true, NULL, false, true, NULL, false, true,  NULL,  false]
    2923
    0
        fn bool_test_arrays() -> (SchemaRef, ArrayRef, ArrayRef) {
    2924
    0
            let schema = Schema::new(vec![
    2925
    0
                Field::new("a", DataType::Boolean, true),
    2926
    0
                Field::new("b", DataType::Boolean, true),
    2927
            ]);
    2928
    0
            let a: BooleanArray = [
    2929
    0
                Some(true),
    2930
    0
                Some(true),
    2931
    0
                Some(true),
    2932
    0
                None,
    2933
    0
                None,
    2934
    0
                None,
    2935
    0
                Some(false),
    2936
    0
                Some(false),
    2937
    0
                Some(false),
    2938
    0
            ]
    2939
    0
            .iter()
    2940
    0
            .collect();
    2941
    0
            let b: BooleanArray = [
    2942
    0
                Some(true),
    2943
    0
                None,
    2944
    0
                Some(false),
    2945
    0
                Some(true),
    2946
    0
                None,
    2947
    0
                Some(false),
    2948
    0
                Some(true),
    2949
    0
                None,
    2950
    0
                Some(false),
    2951
    0
            ]
    2952
    0
            .iter()
    2953
    0
            .collect();
    2954
    0
            (Arc::new(schema), Arc::new(a), Arc::new(b))
    2955
    0
        }
    2956
    2957
        /// Returns (schema, BooleanArray) with [true, NULL, false]
    2958
    0
        fn scalar_bool_test_array() -> (SchemaRef, ArrayRef) {
    2959
    0
            let schema = Schema::new(vec![Field::new("a", DataType::Boolean, true)]);
    2960
    0
            let a: BooleanArray = [Some(true), None, Some(false)].iter().collect();
    2961
    0
            (Arc::new(schema), Arc::new(a))
    2962
    0
        }
    2963
    2964
        #[test]
    2965
    0
        fn eq_op_bool() {
    2966
    0
            let (schema, a, b) = bool_test_arrays();
    2967
    0
            let expected = [
    2968
    0
                Some(true),
    2969
    0
                None,
    2970
    0
                Some(false),
    2971
    0
                None,
    2972
    0
                None,
    2973
    0
                None,
    2974
    0
                Some(false),
    2975
    0
                None,
    2976
    0
                Some(true),
    2977
    0
            ]
    2978
    0
            .iter()
    2979
    0
            .collect();
    2980
    0
            apply_logic_op(&schema, &a, &b, Operator::Eq, expected).unwrap();
    2981
    0
        }
    2982
    2983
        #[test]
    2984
    0
        fn eq_op_bool_scalar() {
    2985
    0
            let (schema, a) = scalar_bool_test_array();
    2986
    0
            let expected = [Some(true), None, Some(false)].iter().collect();
    2987
    0
            apply_logic_op_scalar_arr(
    2988
    0
                &schema,
    2989
    0
                &ScalarValue::from(true),
    2990
    0
                &a,
    2991
    0
                Operator::Eq,
    2992
    0
                &expected,
    2993
            )
    2994
    0
            .unwrap();
    2995
    0
            apply_logic_op_arr_scalar(
    2996
    0
                &schema,
    2997
    0
                &a,
    2998
    0
                &ScalarValue::from(true),
    2999
    0
                Operator::Eq,
    3000
    0
                &expected,
    3001
            )
    3002
    0
            .unwrap();
    3003
    3004
    0
            let expected = [Some(false), None, Some(true)].iter().collect();
    3005
    0
            apply_logic_op_scalar_arr(
    3006
    0
                &schema,
    3007
    0
                &ScalarValue::from(false),
    3008
    0
                &a,
    3009
    0
                Operator::Eq,
    3010
    0
                &expected,
    3011
            )
    3012
    0
            .unwrap();
    3013
    0
            apply_logic_op_arr_scalar(
    3014
    0
                &schema,
    3015
    0
                &a,
    3016
    0
                &ScalarValue::from(false),
    3017
    0
                Operator::Eq,
    3018
    0
                &expected,
    3019
            )
    3020
    0
            .unwrap();
    3021
    0
        }
    3022
    3023
        #[test]
    3024
    0
        fn neq_op_bool() {
    3025
    0
            let (schema, a, b) = bool_test_arrays();
    3026
    0
            let expected = [
    3027
    0
                Some(false),
    3028
    0
                None,
    3029
    0
                Some(true),
    3030
    0
                None,
    3031
    0
                None,
    3032
    0
                None,
    3033
    0
                Some(true),
    3034
    0
                None,
    3035
    0
                Some(false),
    3036
    0
            ]
    3037
    0
            .iter()
    3038
    0
            .collect();
    3039
    0
            apply_logic_op(&schema, &a, &b, Operator::NotEq, expected).unwrap();
    3040
    0
        }
    3041
    3042
        #[test]
    3043
    0
        fn neq_op_bool_scalar() {
    3044
    0
            let (schema, a) = scalar_bool_test_array();
    3045
    0
            let expected = [Some(false), None, Some(true)].iter().collect();
    3046
    0
            apply_logic_op_scalar_arr(
    3047
    0
                &schema,
    3048
    0
                &ScalarValue::from(true),
    3049
    0
                &a,
    3050
    0
                Operator::NotEq,
    3051
    0
                &expected,
    3052
            )
    3053
    0
            .unwrap();
    3054
    0
            apply_logic_op_arr_scalar(
    3055
    0
                &schema,
    3056
    0
                &a,
    3057
    0
                &ScalarValue::from(true),
    3058
    0
                Operator::NotEq,
    3059
    0
                &expected,
    3060
            )
    3061
    0
            .unwrap();
    3062
    3063
    0
            let expected = [Some(true), None, Some(false)].iter().collect();
    3064
    0
            apply_logic_op_scalar_arr(
    3065
    0
                &schema,
    3066
    0
                &ScalarValue::from(false),
    3067
    0
                &a,
    3068
    0
                Operator::NotEq,
    3069
    0
                &expected,
    3070
            )
    3071
    0
            .unwrap();
    3072
    0
            apply_logic_op_arr_scalar(
    3073
    0
                &schema,
    3074
    0
                &a,
    3075
    0
                &ScalarValue::from(false),
    3076
    0
                Operator::NotEq,
    3077
    0
                &expected,
    3078
            )
    3079
    0
            .unwrap();
    3080
    0
        }
    3081
    3082
        #[test]
    3083
    0
        fn lt_op_bool() {
    3084
    0
            let (schema, a, b) = bool_test_arrays();
    3085
    0
            let expected = [
    3086
    0
                Some(false),
    3087
    0
                None,
    3088
    0
                Some(false),
    3089
    0
                None,
    3090
    0
                None,
    3091
    0
                None,
    3092
    0
                Some(true),
    3093
    0
                None,
    3094
    0
                Some(false),
    3095
    0
            ]
    3096
    0
            .iter()
    3097
    0
            .collect();
    3098
    0
            apply_logic_op(&schema, &a, &b, Operator::Lt, expected).unwrap();
    3099
    0
        }
    3100
    3101
        #[test]
    3102
    0
        fn lt_op_bool_scalar() {
    3103
    0
            let (schema, a) = scalar_bool_test_array();
    3104
    0
            let expected = [Some(false), None, Some(false)].iter().collect();
    3105
    0
            apply_logic_op_scalar_arr(
    3106
    0
                &schema,
    3107
    0
                &ScalarValue::from(true),
    3108
    0
                &a,
    3109
    0
                Operator::Lt,
    3110
    0
                &expected,
    3111
            )
    3112
    0
            .unwrap();
    3113
    3114
    0
            let expected = [Some(false), None, Some(true)].iter().collect();
    3115
    0
            apply_logic_op_arr_scalar(
    3116
    0
                &schema,
    3117
    0
                &a,
    3118
    0
                &ScalarValue::from(true),
    3119
    0
                Operator::Lt,
    3120
    0
                &expected,
    3121
            )
    3122
    0
            .unwrap();
    3123
    3124
    0
            let expected = [Some(true), None, Some(false)].iter().collect();
    3125
    0
            apply_logic_op_scalar_arr(
    3126
    0
                &schema,
    3127
    0
                &ScalarValue::from(false),
    3128
    0
                &a,
    3129
    0
                Operator::Lt,
    3130
    0
                &expected,
    3131
            )
    3132
    0
            .unwrap();
    3133
    3134
    0
            let expected = [Some(false), None, Some(false)].iter().collect();
    3135
    0
            apply_logic_op_arr_scalar(
    3136
    0
                &schema,
    3137
    0
                &a,
    3138
    0
                &ScalarValue::from(false),
    3139
    0
                Operator::Lt,
    3140
    0
                &expected,
    3141
            )
    3142
    0
            .unwrap();
    3143
    0
        }
    3144
    3145
        #[test]
    3146
    0
        fn lt_eq_op_bool() {
    3147
    0
            let (schema, a, b) = bool_test_arrays();
    3148
    0
            let expected = [
    3149
    0
                Some(true),
    3150
    0
                None,
    3151
    0
                Some(false),
    3152
    0
                None,
    3153
    0
                None,
    3154
    0
                None,
    3155
    0
                Some(true),
    3156
    0
                None,
    3157
    0
                Some(true),
    3158
    0
            ]
    3159
    0
            .iter()
    3160
    0
            .collect();
    3161
    0
            apply_logic_op(&schema, &a, &b, Operator::LtEq, expected).unwrap();
    3162
    0
        }
    3163
    3164
        #[test]
    3165
    0
        fn lt_eq_op_bool_scalar() {
    3166
    0
            let (schema, a) = scalar_bool_test_array();
    3167
    0
            let expected = [Some(true), None, Some(false)].iter().collect();
    3168
    0
            apply_logic_op_scalar_arr(
    3169
    0
                &schema,
    3170
    0
                &ScalarValue::from(true),
    3171
    0
                &a,
    3172
    0
                Operator::LtEq,
    3173
    0
                &expected,
    3174
            )
    3175
    0
            .unwrap();
    3176
    3177
    0
            let expected = [Some(true), None, Some(true)].iter().collect();
    3178
    0
            apply_logic_op_arr_scalar(
    3179
    0
                &schema,
    3180
    0
                &a,
    3181
    0
                &ScalarValue::from(true),
    3182
    0
                Operator::LtEq,
    3183
    0
                &expected,
    3184
            )
    3185
    0
            .unwrap();
    3186
    3187
    0
            let expected = [Some(true), None, Some(true)].iter().collect();
    3188
    0
            apply_logic_op_scalar_arr(
    3189
    0
                &schema,
    3190
    0
                &ScalarValue::from(false),
    3191
    0
                &a,
    3192
    0
                Operator::LtEq,
    3193
    0
                &expected,
    3194
            )
    3195
    0
            .unwrap();
    3196
    3197
    0
            let expected = [Some(false), None, Some(true)].iter().collect();
    3198
    0
            apply_logic_op_arr_scalar(
    3199
    0
                &schema,
    3200
    0
                &a,
    3201
    0
                &ScalarValue::from(false),
    3202
    0
                Operator::LtEq,
    3203
    0
                &expected,
    3204
            )
    3205
    0
            .unwrap();
    3206
    0
        }
    3207
    3208
        #[test]
    3209
    0
        fn gt_op_bool() {
    3210
    0
            let (schema, a, b) = bool_test_arrays();
    3211
    0
            let expected = [
    3212
    0
                Some(false),
    3213
    0
                None,
    3214
    0
                Some(true),
    3215
    0
                None,
    3216
    0
                None,
    3217
    0
                None,
    3218
    0
                Some(false),
    3219
    0
                None,
    3220
    0
                Some(false),
    3221
    0
            ]
    3222
    0
            .iter()
    3223
    0
            .collect();
    3224
    0
            apply_logic_op(&schema, &a, &b, Operator::Gt, expected).unwrap();
    3225
    0
        }
    3226
    3227
        #[test]
    3228
    0
        fn gt_op_bool_scalar() {
    3229
    0
            let (schema, a) = scalar_bool_test_array();
    3230
    0
            let expected = [Some(false), None, Some(true)].iter().collect();
    3231
    0
            apply_logic_op_scalar_arr(
    3232
    0
                &schema,
    3233
    0
                &ScalarValue::from(true),
    3234
    0
                &a,
    3235
    0
                Operator::Gt,
    3236
    0
                &expected,
    3237
            )
    3238
    0
            .unwrap();
    3239
    3240
    0
            let expected = [Some(false), None, Some(false)].iter().collect();
    3241
    0
            apply_logic_op_arr_scalar(
    3242
    0
                &schema,
    3243
    0
                &a,
    3244
    0
                &ScalarValue::from(true),
    3245
    0
                Operator::Gt,
    3246
    0
                &expected,
    3247
            )
    3248
    0
            .unwrap();
    3249
    3250
    0
            let expected = [Some(false), None, Some(false)].iter().collect();
    3251
    0
            apply_logic_op_scalar_arr(
    3252
    0
                &schema,
    3253
    0
                &ScalarValue::from(false),
    3254
    0
                &a,
    3255
    0
                Operator::Gt,
    3256
    0
                &expected,
    3257
            )
    3258
    0
            .unwrap();
    3259
    3260
    0
            let expected = [Some(true), None, Some(false)].iter().collect();
    3261
    0
            apply_logic_op_arr_scalar(
    3262
    0
                &schema,
    3263
    0
                &a,
    3264
    0
                &ScalarValue::from(false),
    3265
    0
                Operator::Gt,
    3266
    0
                &expected,
    3267
            )
    3268
    0
            .unwrap();
    3269
    0
        }
    3270
    3271
        #[test]
    3272
    0
        fn gt_eq_op_bool() {
    3273
    0
            let (schema, a, b) = bool_test_arrays();
    3274
    0
            let expected = [
    3275
    0
                Some(true),
    3276
    0
                None,
    3277
    0
                Some(true),
    3278
    0
                None,
    3279
    0
                None,
    3280
    0
                None,
    3281
    0
                Some(false),
    3282
    0
                None,
    3283
    0
                Some(true),
    3284
    0
            ]
    3285
    0
            .iter()
    3286
    0
            .collect();
    3287
    0
            apply_logic_op(&schema, &a, &b, Operator::GtEq, expected).unwrap();
    3288
    0
        }
    3289
    3290
        #[test]
    3291
    0
        fn gt_eq_op_bool_scalar() {
    3292
    0
            let (schema, a) = scalar_bool_test_array();
    3293
    0
            let expected = [Some(true), None, Some(true)].iter().collect();
    3294
    0
            apply_logic_op_scalar_arr(
    3295
    0
                &schema,
    3296
    0
                &ScalarValue::from(true),
    3297
    0
                &a,
    3298
    0
                Operator::GtEq,
    3299
    0
                &expected,
    3300
            )
    3301
    0
            .unwrap();
    3302
    3303
    0
            let expected = [Some(true), None, Some(false)].iter().collect();
    3304
    0
            apply_logic_op_arr_scalar(
    3305
    0
                &schema,
    3306
    0
                &a,
    3307
    0
                &ScalarValue::from(true),
    3308
    0
                Operator::GtEq,
    3309
    0
                &expected,
    3310
            )
    3311
    0
            .unwrap();
    3312
    3313
    0
            let expected = [Some(false), None, Some(true)].iter().collect();
    3314
    0
            apply_logic_op_scalar_arr(
    3315
    0
                &schema,
    3316
    0
                &ScalarValue::from(false),
    3317
    0
                &a,
    3318
    0
                Operator::GtEq,
    3319
    0
                &expected,
    3320
            )
    3321
    0
            .unwrap();
    3322
    3323
    0
            let expected = [Some(true), None, Some(true)].iter().collect();
    3324
    0
            apply_logic_op_arr_scalar(
    3325
    0
                &schema,
    3326
    0
                &a,
    3327
    0
                &ScalarValue::from(false),
    3328
    0
                Operator::GtEq,
    3329
    0
                &expected,
    3330
            )
    3331
    0
            .unwrap();
    3332
    0
        }
    3333
    3334
        #[test]
    3335
    0
        fn is_distinct_from_op_bool() {
    3336
    0
            let (schema, a, b) = bool_test_arrays();
    3337
    0
            let expected = [
    3338
    0
                Some(false),
    3339
    0
                Some(true),
    3340
    0
                Some(true),
    3341
    0
                Some(true),
    3342
    0
                Some(false),
    3343
    0
                Some(true),
    3344
    0
                Some(true),
    3345
    0
                Some(true),
    3346
    0
                Some(false),
    3347
    0
            ]
    3348
    0
            .iter()
    3349
    0
            .collect();
    3350
    0
            apply_logic_op(&schema, &a, &b, Operator::IsDistinctFrom, expected).unwrap();
    3351
    0
        }
    3352
    3353
        #[test]
    3354
    0
        fn is_not_distinct_from_op_bool() {
    3355
    0
            let (schema, a, b) = bool_test_arrays();
    3356
    0
            let expected = [
    3357
    0
                Some(true),
    3358
    0
                Some(false),
    3359
    0
                Some(false),
    3360
    0
                Some(false),
    3361
    0
                Some(true),
    3362
    0
                Some(false),
    3363
    0
                Some(false),
    3364
    0
                Some(false),
    3365
    0
                Some(true),
    3366
    0
            ]
    3367
    0
            .iter()
    3368
    0
            .collect();
    3369
    0
            apply_logic_op(&schema, &a, &b, Operator::IsNotDistinctFrom, expected).unwrap();
    3370
    0
        }
    3371
    3372
        #[test]
    3373
    0
        fn relatively_deeply_nested() {
    3374
            // Reproducer for https://github.com/apache/datafusion/issues/419
    3375
    3376
            // where even relatively shallow binary expressions overflowed
    3377
            // the stack in debug builds
    3378
    3379
    0
            let input: Vec<_> = vec![1, 2, 3, 4, 5].into_iter().map(Some).collect();
    3380
    0
            let a: Int32Array = input.iter().collect();
    3381
    3382
    0
            let batch = RecordBatch::try_from_iter(vec![("a", Arc::new(a) as _)]).unwrap();
    3383
    0
            let schema = batch.schema();
    3384
    3385
            // build a left deep tree ((((a + a) + a) + a ....
    3386
    0
            let tree_depth: i32 = 100;
    3387
    0
            let expr = (0..tree_depth)
    3388
    0
                .map(|_| col("a", schema.as_ref()).unwrap())
    3389
    0
                .reduce(|l, r| binary(l, Operator::Plus, r, &schema).unwrap())
    3390
    0
                .unwrap();
    3391
    3392
    0
            let result = expr
    3393
    0
                .evaluate(&batch)
    3394
    0
                .expect("evaluation")
    3395
    0
                .into_array(batch.num_rows())
    3396
    0
                .expect("Failed to convert to array");
    3397
    3398
    0
            let expected: Int32Array = input
    3399
    0
                .into_iter()
    3400
    0
                .map(|i| i.map(|i| i * tree_depth))
    3401
    0
                .collect();
    3402
    0
            assert_eq!(result.as_ref(), &expected);
    3403
    0
        }
    3404
    3405
    0
        fn create_decimal_array(
    3406
    0
            array: &[Option<i128>],
    3407
    0
            precision: u8,
    3408
    0
            scale: i8,
    3409
    0
        ) -> Decimal128Array {
    3410
    0
            let mut decimal_builder = Decimal128Builder::with_capacity(array.len());
    3411
    0
            for value in array.iter().copied() {
    3412
    0
                decimal_builder.append_option(value)
    3413
            }
    3414
    0
            decimal_builder
    3415
    0
                .finish()
    3416
    0
                .with_precision_and_scale(precision, scale)
    3417
    0
                .unwrap()
    3418
    0
        }
    3419
    3420
        #[test]
    3421
    0
        fn comparison_dict_decimal_scalar_expr_test() -> Result<()> {
    3422
            // scalar of decimal compare with dictionary decimal array
    3423
    0
            let value_i128 = 123;
    3424
    0
            let decimal_scalar = ScalarValue::Dictionary(
    3425
    0
                Box::new(DataType::Int8),
    3426
    0
                Box::new(ScalarValue::Decimal128(Some(value_i128), 25, 3)),
    3427
    0
            );
    3428
    0
            let schema = Arc::new(Schema::new(vec![Field::new(
    3429
                "a",
    3430
    0
                DataType::Dictionary(
    3431
    0
                    Box::new(DataType::Int8),
    3432
    0
                    Box::new(DataType::Decimal128(25, 3)),
    3433
    0
                ),
    3434
                true,
    3435
            )]));
    3436
    0
            let decimal_array = Arc::new(create_decimal_array(
    3437
    0
                &[
    3438
    0
                    Some(value_i128),
    3439
    0
                    None,
    3440
    0
                    Some(value_i128 - 1),
    3441
    0
                    Some(value_i128 + 1),
    3442
    0
                ],
    3443
                25,
    3444
                3,
    3445
            ));
    3446
    3447
    0
            let keys = Int8Array::from(vec![Some(0), None, Some(2), Some(3)]);
    3448
    0
            let dictionary =
    3449
    0
                Arc::new(DictionaryArray::try_new(keys, decimal_array)?) as ArrayRef;
    3450
    3451
            // array = scalar
    3452
    0
            apply_logic_op_arr_scalar(
    3453
    0
                &schema,
    3454
    0
                &dictionary,
    3455
    0
                &decimal_scalar,
    3456
    0
                Operator::Eq,
    3457
    0
                &BooleanArray::from(vec![Some(true), None, Some(false), Some(false)]),
    3458
            )
    3459
    0
            .unwrap();
    3460
            // array != scalar
    3461
    0
            apply_logic_op_arr_scalar(
    3462
    0
                &schema,
    3463
    0
                &dictionary,
    3464
    0
                &decimal_scalar,
    3465
    0
                Operator::NotEq,
    3466
    0
                &BooleanArray::from(vec![Some(false), None, Some(true), Some(true)]),
    3467
            )
    3468
    0
            .unwrap();
    3469
            //  array < scalar
    3470
    0
            apply_logic_op_arr_scalar(
    3471
    0
                &schema,
    3472
    0
                &dictionary,
    3473
    0
                &decimal_scalar,
    3474
    0
                Operator::Lt,
    3475
    0
                &BooleanArray::from(vec![Some(false), None, Some(true), Some(false)]),
    3476
            )
    3477
    0
            .unwrap();
    3478
    3479
            //  array <= scalar
    3480
    0
            apply_logic_op_arr_scalar(
    3481
    0
                &schema,
    3482
    0
                &dictionary,
    3483
    0
                &decimal_scalar,
    3484
    0
                Operator::LtEq,
    3485
    0
                &BooleanArray::from(vec![Some(true), None, Some(true), Some(false)]),
    3486
            )
    3487
    0
            .unwrap();
    3488
            // array > scalar
    3489
    0
            apply_logic_op_arr_scalar(
    3490
    0
                &schema,
    3491
    0
                &dictionary,
    3492
    0
                &decimal_scalar,
    3493
    0
                Operator::Gt,
    3494
    0
                &BooleanArray::from(vec![Some(false), None, Some(false), Some(true)]),
    3495
            )
    3496
    0
            .unwrap();
    3497
    3498
            // array >= scalar
    3499
    0
            apply_logic_op_arr_scalar(
    3500
    0
                &schema,
    3501
    0
                &dictionary,
    3502
    0
                &decimal_scalar,
    3503
    0
                Operator::GtEq,
    3504
    0
                &BooleanArray::from(vec![Some(true), None, Some(false), Some(true)]),
    3505
            )
    3506
    0
            .unwrap();
    3507
    3508
    0
            Ok(())
    3509
    0
        }
    3510
    3511
        #[test]
    3512
    0
        fn comparison_decimal_expr_test() -> Result<()> {
    3513
            // scalar of decimal compare with decimal array
    3514
    0
            let value_i128 = 123;
    3515
    0
            let decimal_scalar = ScalarValue::Decimal128(Some(value_i128), 25, 3);
    3516
    0
            let schema = Arc::new(Schema::new(vec![Field::new(
    3517
                "a",
    3518
    0
                DataType::Decimal128(25, 3),
    3519
                true,
    3520
            )]));
    3521
    0
            let decimal_array = Arc::new(create_decimal_array(
    3522
    0
                &[
    3523
    0
                    Some(value_i128),
    3524
    0
                    None,
    3525
    0
                    Some(value_i128 - 1),
    3526
    0
                    Some(value_i128 + 1),
    3527
    0
                ],
    3528
    0
                25,
    3529
    0
                3,
    3530
    0
            )) as ArrayRef;
    3531
            // array = scalar
    3532
    0
            apply_logic_op_arr_scalar(
    3533
    0
                &schema,
    3534
    0
                &decimal_array,
    3535
    0
                &decimal_scalar,
    3536
    0
                Operator::Eq,
    3537
    0
                &BooleanArray::from(vec![Some(true), None, Some(false), Some(false)]),
    3538
            )
    3539
    0
            .unwrap();
    3540
            // array != scalar
    3541
    0
            apply_logic_op_arr_scalar(
    3542
    0
                &schema,
    3543
    0
                &decimal_array,
    3544
    0
                &decimal_scalar,
    3545
    0
                Operator::NotEq,
    3546
    0
                &BooleanArray::from(vec![Some(false), None, Some(true), Some(true)]),
    3547
            )
    3548
    0
            .unwrap();
    3549
            //  array < scalar
    3550
    0
            apply_logic_op_arr_scalar(
    3551
    0
                &schema,
    3552
    0
                &decimal_array,
    3553
    0
                &decimal_scalar,
    3554
    0
                Operator::Lt,
    3555
    0
                &BooleanArray::from(vec![Some(false), None, Some(true), Some(false)]),
    3556
            )
    3557
    0
            .unwrap();
    3558
    3559
            //  array <= scalar
    3560
    0
            apply_logic_op_arr_scalar(
    3561
    0
                &schema,
    3562
    0
                &decimal_array,
    3563
    0
                &decimal_scalar,
    3564
    0
                Operator::LtEq,
    3565
    0
                &BooleanArray::from(vec![Some(true), None, Some(true), Some(false)]),
    3566
            )
    3567
    0
            .unwrap();
    3568
            // array > scalar
    3569
    0
            apply_logic_op_arr_scalar(
    3570
    0
                &schema,
    3571
    0
                &decimal_array,
    3572
    0
                &decimal_scalar,
    3573
    0
                Operator::Gt,
    3574
    0
                &BooleanArray::from(vec![Some(false), None, Some(false), Some(true)]),
    3575
            )
    3576
    0
            .unwrap();
    3577
    3578
            // array >= scalar
    3579
    0
            apply_logic_op_arr_scalar(
    3580
    0
                &schema,
    3581
    0
                &decimal_array,
    3582
    0
                &decimal_scalar,
    3583
    0
                Operator::GtEq,
    3584
    0
                &BooleanArray::from(vec![Some(true), None, Some(false), Some(true)]),
    3585
            )
    3586
    0
            .unwrap();
    3587
    3588
            // scalar of different data type with decimal array
    3589
    0
            let decimal_scalar = ScalarValue::Decimal128(Some(123_456), 10, 3);
    3590
    0
            let schema = Arc::new(Schema::new(vec![Field::new("a", DataType::Int64, true)]));
    3591
            // scalar == array
    3592
    0
            apply_logic_op_scalar_arr(
    3593
    0
                &schema,
    3594
    0
                &decimal_scalar,
    3595
    0
                &(Arc::new(Int64Array::from(vec![Some(124), None])) as ArrayRef),
    3596
    0
                Operator::Eq,
    3597
    0
                &BooleanArray::from(vec![Some(false), None]),
    3598
            )
    3599
    0
            .unwrap();
    3600
    3601
            // array != scalar
    3602
    0
            apply_logic_op_arr_scalar(
    3603
    0
                &schema,
    3604
    0
                &(Arc::new(Int64Array::from(vec![Some(123), None, Some(1)])) as ArrayRef),
    3605
    0
                &decimal_scalar,
    3606
    0
                Operator::NotEq,
    3607
    0
                &BooleanArray::from(vec![Some(true), None, Some(true)]),
    3608
            )
    3609
    0
            .unwrap();
    3610
    3611
            // array < scalar
    3612
    0
            apply_logic_op_arr_scalar(
    3613
    0
                &schema,
    3614
    0
                &(Arc::new(Int64Array::from(vec![Some(123), None, Some(124)])) as ArrayRef),
    3615
    0
                &decimal_scalar,
    3616
    0
                Operator::Lt,
    3617
    0
                &BooleanArray::from(vec![Some(true), None, Some(false)]),
    3618
            )
    3619
    0
            .unwrap();
    3620
    3621
            // array > scalar
    3622
    0
            apply_logic_op_arr_scalar(
    3623
    0
                &schema,
    3624
    0
                &(Arc::new(Int64Array::from(vec![Some(123), None, Some(124)])) as ArrayRef),
    3625
    0
                &decimal_scalar,
    3626
    0
                Operator::Gt,
    3627
    0
                &BooleanArray::from(vec![Some(false), None, Some(true)]),
    3628
            )
    3629
    0
            .unwrap();
    3630
    3631
    0
            let schema =
    3632
    0
                Arc::new(Schema::new(vec![Field::new("a", DataType::Float64, true)]));
    3633
            // array == scalar
    3634
    0
            apply_logic_op_arr_scalar(
    3635
    0
                &schema,
    3636
    0
                &(Arc::new(Float64Array::from(vec![Some(123.456), None, Some(123.457)]))
    3637
    0
                    as ArrayRef),
    3638
    0
                &decimal_scalar,
    3639
    0
                Operator::Eq,
    3640
    0
                &BooleanArray::from(vec![Some(true), None, Some(false)]),
    3641
            )
    3642
    0
            .unwrap();
    3643
    3644
            // array <= scalar
    3645
    0
            apply_logic_op_arr_scalar(
    3646
    0
                &schema,
    3647
    0
                &(Arc::new(Float64Array::from(vec![
    3648
    0
                    Some(123.456),
    3649
    0
                    None,
    3650
    0
                    Some(123.457),
    3651
    0
                    Some(123.45),
    3652
    0
                ])) as ArrayRef),
    3653
    0
                &decimal_scalar,
    3654
    0
                Operator::LtEq,
    3655
    0
                &BooleanArray::from(vec![Some(true), None, Some(false), Some(true)]),
    3656
            )
    3657
    0
            .unwrap();
    3658
            // array >= scalar
    3659
    0
            apply_logic_op_arr_scalar(
    3660
    0
                &schema,
    3661
    0
                &(Arc::new(Float64Array::from(vec![
    3662
    0
                    Some(123.456),
    3663
    0
                    None,
    3664
    0
                    Some(123.457),
    3665
    0
                    Some(123.45),
    3666
    0
                ])) as ArrayRef),
    3667
    0
                &decimal_scalar,
    3668
    0
                Operator::GtEq,
    3669
    0
                &BooleanArray::from(vec![Some(true), None, Some(true), Some(false)]),
    3670
            )
    3671
    0
            .unwrap();
    3672
    3673
    0
            let value: i128 = 123;
    3674
    0
            let decimal_array = Arc::new(create_decimal_array(
    3675
    0
                &[Some(value), None, Some(value - 1), Some(value + 1)],
    3676
    0
                10,
    3677
    0
                0,
    3678
    0
            )) as ArrayRef;
    3679
    3680
            // comparison array op for decimal array
    3681
    0
            let schema = Arc::new(Schema::new(vec![
    3682
    0
                Field::new("a", DataType::Decimal128(10, 0), true),
    3683
    0
                Field::new("b", DataType::Decimal128(10, 0), true),
    3684
            ]));
    3685
    0
            let right_decimal_array = Arc::new(create_decimal_array(
    3686
    0
                &[
    3687
    0
                    Some(value - 1),
    3688
    0
                    Some(value),
    3689
    0
                    Some(value + 1),
    3690
    0
                    Some(value + 1),
    3691
    0
                ],
    3692
    0
                10,
    3693
    0
                0,
    3694
    0
            )) as ArrayRef;
    3695
    3696
    0
            apply_logic_op(
    3697
    0
                &schema,
    3698
    0
                &decimal_array,
    3699
    0
                &right_decimal_array,
    3700
    0
                Operator::Eq,
    3701
    0
                BooleanArray::from(vec![Some(false), None, Some(false), Some(true)]),
    3702
            )
    3703
    0
            .unwrap();
    3704
    3705
    0
            apply_logic_op(
    3706
    0
                &schema,
    3707
    0
                &decimal_array,
    3708
    0
                &right_decimal_array,
    3709
    0
                Operator::NotEq,
    3710
    0
                BooleanArray::from(vec![Some(true), None, Some(true), Some(false)]),
    3711
            )
    3712
    0
            .unwrap();
    3713
    3714
    0
            apply_logic_op(
    3715
    0
                &schema,
    3716
    0
                &decimal_array,
    3717
    0
                &right_decimal_array,
    3718
    0
                Operator::Lt,
    3719
    0
                BooleanArray::from(vec![Some(false), None, Some(true), Some(false)]),
    3720
            )
    3721
    0
            .unwrap();
    3722
    3723
    0
            apply_logic_op(
    3724
    0
                &schema,
    3725
    0
                &decimal_array,
    3726
    0
                &right_decimal_array,
    3727
    0
                Operator::LtEq,
    3728
    0
                BooleanArray::from(vec![Some(false), None, Some(true), Some(true)]),
    3729
            )
    3730
    0
            .unwrap();
    3731
    3732
    0
            apply_logic_op(
    3733
    0
                &schema,
    3734
    0
                &decimal_array,
    3735
    0
                &right_decimal_array,
    3736
    0
                Operator::Gt,
    3737
    0
                BooleanArray::from(vec![Some(true), None, Some(false), Some(false)]),
    3738
            )
    3739
    0
            .unwrap();
    3740
    3741
    0
            apply_logic_op(
    3742
    0
                &schema,
    3743
    0
                &decimal_array,
    3744
    0
                &right_decimal_array,
    3745
    0
                Operator::GtEq,
    3746
    0
                BooleanArray::from(vec![Some(true), None, Some(false), Some(true)]),
    3747
            )
    3748
    0
            .unwrap();
    3749
    3750
            // compare decimal array with other array type
    3751
    0
            let value: i64 = 123;
    3752
    0
            let schema = Arc::new(Schema::new(vec![
    3753
    0
                Field::new("a", DataType::Int64, true),
    3754
    0
                Field::new("b", DataType::Decimal128(10, 0), true),
    3755
            ]));
    3756
    3757
    0
            let int64_array = Arc::new(Int64Array::from(vec![
    3758
    0
                Some(value),
    3759
    0
                Some(value - 1),
    3760
    0
                Some(value),
    3761
    0
                Some(value + 1),
    3762
    0
            ])) as ArrayRef;
    3763
    3764
            // eq: int64array == decimal array
    3765
    0
            apply_logic_op(
    3766
    0
                &schema,
    3767
    0
                &int64_array,
    3768
    0
                &decimal_array,
    3769
    0
                Operator::Eq,
    3770
    0
                BooleanArray::from(vec![Some(true), None, Some(false), Some(true)]),
    3771
            )
    3772
    0
            .unwrap();
    3773
            // neq: int64array != decimal array
    3774
    0
            apply_logic_op(
    3775
    0
                &schema,
    3776
    0
                &int64_array,
    3777
    0
                &decimal_array,
    3778
    0
                Operator::NotEq,
    3779
    0
                BooleanArray::from(vec![Some(false), None, Some(true), Some(false)]),
    3780
            )
    3781
    0
            .unwrap();
    3782
    3783
    0
            let schema = Arc::new(Schema::new(vec![
    3784
    0
                Field::new("a", DataType::Float64, true),
    3785
    0
                Field::new("b", DataType::Decimal128(10, 2), true),
    3786
            ]));
    3787
    3788
    0
            let value: i128 = 123;
    3789
    0
            let decimal_array = Arc::new(create_decimal_array(
    3790
    0
                &[
    3791
    0
                    Some(value), // 1.23
    3792
    0
                    None,
    3793
    0
                    Some(value - 1), // 1.22
    3794
    0
                    Some(value + 1), // 1.24
    3795
    0
                ],
    3796
    0
                10,
    3797
    0
                2,
    3798
    0
            )) as ArrayRef;
    3799
    0
            let float64_array = Arc::new(Float64Array::from(vec![
    3800
    0
                Some(1.23),
    3801
    0
                Some(1.22),
    3802
    0
                Some(1.23),
    3803
    0
                Some(1.24),
    3804
    0
            ])) as ArrayRef;
    3805
            // lt: float64array < decimal array
    3806
    0
            apply_logic_op(
    3807
    0
                &schema,
    3808
    0
                &float64_array,
    3809
    0
                &decimal_array,
    3810
    0
                Operator::Lt,
    3811
    0
                BooleanArray::from(vec![Some(false), None, Some(false), Some(false)]),
    3812
            )
    3813
    0
            .unwrap();
    3814
            // lt_eq: float64array <= decimal array
    3815
    0
            apply_logic_op(
    3816
    0
                &schema,
    3817
    0
                &float64_array,
    3818
    0
                &decimal_array,
    3819
    0
                Operator::LtEq,
    3820
    0
                BooleanArray::from(vec![Some(true), None, Some(false), Some(true)]),
    3821
            )
    3822
    0
            .unwrap();
    3823
            // gt: float64array > decimal array
    3824
    0
            apply_logic_op(
    3825
    0
                &schema,
    3826
    0
                &float64_array,
    3827
    0
                &decimal_array,
    3828
    0
                Operator::Gt,
    3829
    0
                BooleanArray::from(vec![Some(false), None, Some(true), Some(false)]),
    3830
            )
    3831
    0
            .unwrap();
    3832
    0
            apply_logic_op(
    3833
    0
                &schema,
    3834
    0
                &float64_array,
    3835
    0
                &decimal_array,
    3836
    0
                Operator::GtEq,
    3837
    0
                BooleanArray::from(vec![Some(true), None, Some(true), Some(true)]),
    3838
            )
    3839
    0
            .unwrap();
    3840
            // is distinct: float64array is distinct decimal array
    3841
            // TODO: now we do not refactor the `is distinct or is not distinct` rule of coercion.
    3842
            // traced by https://github.com/apache/datafusion/issues/1590
    3843
            // the decimal array will be casted to float64array
    3844
    0
            apply_logic_op(
    3845
    0
                &schema,
    3846
    0
                &float64_array,
    3847
    0
                &decimal_array,
    3848
    0
                Operator::IsDistinctFrom,
    3849
    0
                BooleanArray::from(vec![Some(false), Some(true), Some(true), Some(false)]),
    3850
            )
    3851
    0
            .unwrap();
    3852
            // is not distinct
    3853
    0
            apply_logic_op(
    3854
    0
                &schema,
    3855
    0
                &float64_array,
    3856
    0
                &decimal_array,
    3857
    0
                Operator::IsNotDistinctFrom,
    3858
    0
                BooleanArray::from(vec![Some(true), Some(false), Some(false), Some(true)]),
    3859
            )
    3860
    0
            .unwrap();
    3861
    3862
    0
            Ok(())
    3863
    0
        }
    3864
    3865
    0
        fn apply_decimal_arithmetic_op(
    3866
    0
            schema: &SchemaRef,
    3867
    0
            left: &ArrayRef,
    3868
    0
            right: &ArrayRef,
    3869
    0
            op: Operator,
    3870
    0
            expected: ArrayRef,
    3871
    0
        ) -> Result<()> {
    3872
    0
            let arithmetic_op = binary_op(col("a", schema)?, op, col("b", schema)?, schema)?;
    3873
    0
            let data: Vec<ArrayRef> = vec![Arc::clone(left), Arc::clone(right)];
    3874
    0
            let batch = RecordBatch::try_new(Arc::clone(schema), data)?;
    3875
    0
            let result = arithmetic_op
    3876
    0
                .evaluate(&batch)?
    3877
    0
                .into_array(batch.num_rows())
    3878
    0
                .expect("Failed to convert to array");
    3879
    3880
    0
            assert_eq!(result.as_ref(), expected.as_ref());
    3881
    0
            Ok(())
    3882
    0
        }
    3883
    3884
        #[test]
    3885
    0
        fn arithmetic_decimal_expr_test() -> Result<()> {
    3886
    0
            let schema = Arc::new(Schema::new(vec![
    3887
    0
                Field::new("a", DataType::Int32, true),
    3888
    0
                Field::new("b", DataType::Decimal128(10, 2), true),
    3889
            ]));
    3890
    0
            let value: i128 = 123;
    3891
    0
            let decimal_array = Arc::new(create_decimal_array(
    3892
    0
                &[
    3893
    0
                    Some(value), // 1.23
    3894
    0
                    None,
    3895
    0
                    Some(value - 1), // 1.22
    3896
    0
                    Some(value + 1), // 1.24
    3897
    0
                ],
    3898
    0
                10,
    3899
    0
                2,
    3900
    0
            )) as ArrayRef;
    3901
    0
            let int32_array = Arc::new(Int32Array::from(vec![
    3902
    0
                Some(123),
    3903
    0
                Some(122),
    3904
    0
                Some(123),
    3905
    0
                Some(124),
    3906
    0
            ])) as ArrayRef;
    3907
    3908
            // add: Int32array add decimal array
    3909
    0
            let expect = Arc::new(create_decimal_array(
    3910
    0
                &[Some(12423), None, Some(12422), Some(12524)],
    3911
    0
                13,
    3912
    0
                2,
    3913
    0
            )) as ArrayRef;
    3914
    0
            apply_decimal_arithmetic_op(
    3915
    0
                &schema,
    3916
    0
                &int32_array,
    3917
    0
                &decimal_array,
    3918
    0
                Operator::Plus,
    3919
    0
                expect,
    3920
            )
    3921
    0
            .unwrap();
    3922
    3923
            // subtract: decimal array subtract int32 array
    3924
    0
            let schema = Arc::new(Schema::new(vec![
    3925
    0
                Field::new("a", DataType::Decimal128(10, 2), true),
    3926
    0
                Field::new("b", DataType::Int32, true),
    3927
            ]));
    3928
    0
            let expect = Arc::new(create_decimal_array(
    3929
    0
                &[Some(-12177), None, Some(-12178), Some(-12276)],
    3930
    0
                13,
    3931
    0
                2,
    3932
    0
            )) as ArrayRef;
    3933
    0
            apply_decimal_arithmetic_op(
    3934
    0
                &schema,
    3935
    0
                &decimal_array,
    3936
    0
                &int32_array,
    3937
    0
                Operator::Minus,
    3938
    0
                expect,
    3939
            )
    3940
    0
            .unwrap();
    3941
    3942
            // multiply: decimal array multiply int32 array
    3943
    0
            let expect = Arc::new(create_decimal_array(
    3944
    0
                &[Some(15129), None, Some(15006), Some(15376)],
    3945
    0
                21,
    3946
    0
                2,
    3947
    0
            )) as ArrayRef;
    3948
    0
            apply_decimal_arithmetic_op(
    3949
    0
                &schema,
    3950
    0
                &decimal_array,
    3951
    0
                &int32_array,
    3952
    0
                Operator::Multiply,
    3953
    0
                expect,
    3954
            )
    3955
    0
            .unwrap();
    3956
    3957
            // divide: int32 array divide decimal array
    3958
    0
            let schema = Arc::new(Schema::new(vec![
    3959
    0
                Field::new("a", DataType::Int32, true),
    3960
    0
                Field::new("b", DataType::Decimal128(10, 2), true),
    3961
            ]));
    3962
    0
            let expect = Arc::new(create_decimal_array(
    3963
    0
                &[Some(1000000), None, Some(1008196), Some(1000000)],
    3964
    0
                16,
    3965
    0
                4,
    3966
    0
            )) as ArrayRef;
    3967
    0
            apply_decimal_arithmetic_op(
    3968
    0
                &schema,
    3969
    0
                &int32_array,
    3970
    0
                &decimal_array,
    3971
    0
                Operator::Divide,
    3972
    0
                expect,
    3973
            )
    3974
    0
            .unwrap();
    3975
    3976
            // modulus: int32 array modulus decimal array
    3977
    0
            let schema = Arc::new(Schema::new(vec![
    3978
    0
                Field::new("a", DataType::Int32, true),
    3979
    0
                Field::new("b", DataType::Decimal128(10, 2), true),
    3980
            ]));
    3981
    0
            let expect = Arc::new(create_decimal_array(
    3982
    0
                &[Some(000), None, Some(100), Some(000)],
    3983
    0
                10,
    3984
    0
                2,
    3985
    0
            )) as ArrayRef;
    3986
    0
            apply_decimal_arithmetic_op(
    3987
    0
                &schema,
    3988
    0
                &int32_array,
    3989
    0
                &decimal_array,
    3990
    0
                Operator::Modulo,
    3991
    0
                expect,
    3992
            )
    3993
    0
            .unwrap();
    3994
    3995
    0
            Ok(())
    3996
    0
        }
    3997
    3998
        #[test]
    3999
    0
        fn arithmetic_decimal_float_expr_test() -> Result<()> {
    4000
    0
            let schema = Arc::new(Schema::new(vec![
    4001
    0
                Field::new("a", DataType::Float64, true),
    4002
    0
                Field::new("b", DataType::Decimal128(10, 2), true),
    4003
            ]));
    4004
    0
            let value: i128 = 123;
    4005
    0
            let decimal_array = Arc::new(create_decimal_array(
    4006
    0
                &[
    4007
    0
                    Some(value), // 1.23
    4008
    0
                    None,
    4009
    0
                    Some(value - 1), // 1.22
    4010
    0
                    Some(value + 1), // 1.24
    4011
    0
                ],
    4012
    0
                10,
    4013
    0
                2,
    4014
    0
            )) as ArrayRef;
    4015
    0
            let float64_array = Arc::new(Float64Array::from(vec![
    4016
    0
                Some(123.0),
    4017
    0
                Some(122.0),
    4018
    0
                Some(123.0),
    4019
    0
                Some(124.0),
    4020
    0
            ])) as ArrayRef;
    4021
    4022
            // add: float64 array add decimal array
    4023
    0
            let expect = Arc::new(Float64Array::from(vec![
    4024
    0
                Some(124.23),
    4025
    0
                None,
    4026
    0
                Some(124.22),
    4027
    0
                Some(125.24),
    4028
    0
            ])) as ArrayRef;
    4029
    0
            apply_decimal_arithmetic_op(
    4030
    0
                &schema,
    4031
    0
                &float64_array,
    4032
    0
                &decimal_array,
    4033
    0
                Operator::Plus,
    4034
    0
                expect,
    4035
            )
    4036
    0
            .unwrap();
    4037
    4038
            // subtract: decimal array subtract float64 array
    4039
    0
            let schema = Arc::new(Schema::new(vec![
    4040
    0
                Field::new("a", DataType::Float64, true),
    4041
    0
                Field::new("b", DataType::Decimal128(10, 2), true),
    4042
            ]));
    4043
    0
            let expect = Arc::new(Float64Array::from(vec![
    4044
    0
                Some(121.77),
    4045
    0
                None,
    4046
    0
                Some(121.78),
    4047
    0
                Some(122.76),
    4048
    0
            ])) as ArrayRef;
    4049
    0
            apply_decimal_arithmetic_op(
    4050
    0
                &schema,
    4051
    0
                &float64_array,
    4052
    0
                &decimal_array,
    4053
    0
                Operator::Minus,
    4054
    0
                expect,
    4055
            )
    4056
    0
            .unwrap();
    4057
    4058
            // multiply: decimal array multiply float64 array
    4059
    0
            let expect = Arc::new(Float64Array::from(vec![
    4060
    0
                Some(151.29),
    4061
    0
                None,
    4062
    0
                Some(150.06),
    4063
    0
                Some(153.76),
    4064
    0
            ])) as ArrayRef;
    4065
    0
            apply_decimal_arithmetic_op(
    4066
    0
                &schema,
    4067
    0
                &float64_array,
    4068
    0
                &decimal_array,
    4069
    0
                Operator::Multiply,
    4070
    0
                expect,
    4071
            )
    4072
    0
            .unwrap();
    4073
    4074
            // divide: float64 array divide decimal array
    4075
    0
            let schema = Arc::new(Schema::new(vec![
    4076
    0
                Field::new("a", DataType::Float64, true),
    4077
    0
                Field::new("b", DataType::Decimal128(10, 2), true),
    4078
            ]));
    4079
    0
            let expect = Arc::new(Float64Array::from(vec![
    4080
    0
                Some(100.0),
    4081
    0
                None,
    4082
    0
                Some(100.81967213114754),
    4083
    0
                Some(100.0),
    4084
    0
            ])) as ArrayRef;
    4085
    0
            apply_decimal_arithmetic_op(
    4086
    0
                &schema,
    4087
    0
                &float64_array,
    4088
    0
                &decimal_array,
    4089
    0
                Operator::Divide,
    4090
    0
                expect,
    4091
            )
    4092
    0
            .unwrap();
    4093
    4094
            // modulus: float64 array modulus decimal array
    4095
    0
            let schema = Arc::new(Schema::new(vec![
    4096
    0
                Field::new("a", DataType::Float64, true),
    4097
    0
                Field::new("b", DataType::Decimal128(10, 2), true),
    4098
            ]));
    4099
    0
            let expect = Arc::new(Float64Array::from(vec![
    4100
    0
                Some(1.7763568394002505e-15),
    4101
    0
                None,
    4102
    0
                Some(1.0000000000000027),
    4103
    0
                Some(8.881784197001252e-16),
    4104
    0
            ])) as ArrayRef;
    4105
    0
            apply_decimal_arithmetic_op(
    4106
    0
                &schema,
    4107
    0
                &float64_array,
    4108
    0
                &decimal_array,
    4109
    0
                Operator::Modulo,
    4110
    0
                expect,
    4111
            )
    4112
    0
            .unwrap();
    4113
    4114
    0
            Ok(())
    4115
    0
        }
    4116
    4117
        #[test]
    4118
    0
        fn arithmetic_divide_zero() -> Result<()> {
    4119
            // other data type
    4120
    0
            let schema = Arc::new(Schema::new(vec![
    4121
    0
                Field::new("a", DataType::Int32, true),
    4122
    0
                Field::new("b", DataType::Int32, true),
    4123
            ]));
    4124
    0
            let a = Arc::new(Int32Array::from(vec![100]));
    4125
    0
            let b = Arc::new(Int32Array::from(vec![0]));
    4126
    4127
    0
            let err = apply_arithmetic::<Int32Type>(
    4128
    0
                schema,
    4129
    0
                vec![a, b],
    4130
    0
                Operator::Divide,
    4131
    0
                Int32Array::from(vec![Some(4), Some(8), Some(16), Some(32), Some(64)]),
    4132
            )
    4133
    0
            .unwrap_err();
    4134
    4135
    0
            let _expected = plan_datafusion_err!("Divide by zero");
    4136
    4137
    0
            assert!(matches!(err, ref _expected), "{err}");
    4138
    4139
            // decimal
    4140
    0
            let schema = Arc::new(Schema::new(vec![
    4141
    0
                Field::new("a", DataType::Decimal128(25, 3), true),
    4142
    0
                Field::new("b", DataType::Decimal128(25, 3), true),
    4143
            ]));
    4144
    0
            let left_decimal_array = Arc::new(create_decimal_array(&[Some(1234567)], 25, 3));
    4145
    0
            let right_decimal_array = Arc::new(create_decimal_array(&[Some(0)], 25, 3));
    4146
    4147
    0
            let err = apply_arithmetic::<Decimal128Type>(
    4148
    0
                schema,
    4149
    0
                vec![left_decimal_array, right_decimal_array],
    4150
    0
                Operator::Divide,
    4151
    0
                create_decimal_array(
    4152
    0
                    &[Some(12345670000000000000000000000000000), None],
    4153
                    38,
    4154
                    29,
    4155
                ),
    4156
            )
    4157
    0
            .unwrap_err();
    4158
    4159
    0
            assert!(matches!(err, ref _expected), "{err}");
    4160
    4161
    0
            Ok(())
    4162
    0
        }
    4163
    4164
        #[test]
    4165
    0
        fn bitwise_array_test() -> Result<()> {
    4166
    0
            let left = Arc::new(Int32Array::from(vec![Some(12), None, Some(11)])) as ArrayRef;
    4167
    0
            let right =
    4168
    0
                Arc::new(Int32Array::from(vec![Some(1), Some(3), Some(7)])) as ArrayRef;
    4169
    0
            let mut result = bitwise_and_dyn(Arc::clone(&left), Arc::clone(&right))?;
    4170
    0
            let expected = Int32Array::from(vec![Some(0), None, Some(3)]);
    4171
    0
            assert_eq!(result.as_ref(), &expected);
    4172
    4173
    0
            result = bitwise_or_dyn(Arc::clone(&left), Arc::clone(&right))?;
    4174
    0
            let expected = Int32Array::from(vec![Some(13), None, Some(15)]);
    4175
    0
            assert_eq!(result.as_ref(), &expected);
    4176
    4177
    0
            result = bitwise_xor_dyn(Arc::clone(&left), Arc::clone(&right))?;
    4178
    0
            let expected = Int32Array::from(vec![Some(13), None, Some(12)]);
    4179
    0
            assert_eq!(result.as_ref(), &expected);
    4180
    4181
    0
            let left =
    4182
    0
                Arc::new(UInt32Array::from(vec![Some(12), None, Some(11)])) as ArrayRef;
    4183
    0
            let right =
    4184
    0
                Arc::new(UInt32Array::from(vec![Some(1), Some(3), Some(7)])) as ArrayRef;
    4185
    0
            let mut result = bitwise_and_dyn(Arc::clone(&left), Arc::clone(&right))?;
    4186
    0
            let expected = UInt32Array::from(vec![Some(0), None, Some(3)]);
    4187
    0
            assert_eq!(result.as_ref(), &expected);
    4188
    4189
    0
            result = bitwise_or_dyn(Arc::clone(&left), Arc::clone(&right))?;
    4190
    0
            let expected = UInt32Array::from(vec![Some(13), None, Some(15)]);
    4191
    0
            assert_eq!(result.as_ref(), &expected);
    4192
    4193
    0
            result = bitwise_xor_dyn(Arc::clone(&left), Arc::clone(&right))?;
    4194
    0
            let expected = UInt32Array::from(vec![Some(13), None, Some(12)]);
    4195
    0
            assert_eq!(result.as_ref(), &expected);
    4196
    4197
    0
            Ok(())
    4198
    0
        }
    4199
    4200
        #[test]
    4201
    0
        fn bitwise_shift_array_test() -> Result<()> {
    4202
    0
            let input = Arc::new(Int32Array::from(vec![Some(2), None, Some(10)])) as ArrayRef;
    4203
    0
            let modules =
    4204
    0
                Arc::new(Int32Array::from(vec![Some(2), Some(4), Some(8)])) as ArrayRef;
    4205
    0
            let mut result =
    4206
    0
                bitwise_shift_left_dyn(Arc::clone(&input), Arc::clone(&modules))?;
    4207
    4208
    0
            let expected = Int32Array::from(vec![Some(8), None, Some(2560)]);
    4209
    0
            assert_eq!(result.as_ref(), &expected);
    4210
    4211
    0
            result = bitwise_shift_right_dyn(Arc::clone(&result), Arc::clone(&modules))?;
    4212
    0
            assert_eq!(result.as_ref(), &input);
    4213
    4214
    0
            let input =
    4215
    0
                Arc::new(UInt32Array::from(vec![Some(2), None, Some(10)])) as ArrayRef;
    4216
    0
            let modules =
    4217
    0
                Arc::new(UInt32Array::from(vec![Some(2), Some(4), Some(8)])) as ArrayRef;
    4218
    0
            let mut result =
    4219
    0
                bitwise_shift_left_dyn(Arc::clone(&input), Arc::clone(&modules))?;
    4220
    4221
    0
            let expected = UInt32Array::from(vec![Some(8), None, Some(2560)]);
    4222
    0
            assert_eq!(result.as_ref(), &expected);
    4223
    4224
    0
            result = bitwise_shift_right_dyn(Arc::clone(&result), Arc::clone(&modules))?;
    4225
    0
            assert_eq!(result.as_ref(), &input);
    4226
    0
            Ok(())
    4227
    0
        }
    4228
    4229
        #[test]
    4230
    0
        fn bitwise_shift_array_overflow_test() -> Result<()> {
    4231
    0
            let input = Arc::new(Int32Array::from(vec![Some(2)])) as ArrayRef;
    4232
    0
            let modules = Arc::new(Int32Array::from(vec![Some(100)])) as ArrayRef;
    4233
    0
            let result = bitwise_shift_left_dyn(Arc::clone(&input), Arc::clone(&modules))?;
    4234
    4235
    0
            let expected = Int32Array::from(vec![Some(32)]);
    4236
    0
            assert_eq!(result.as_ref(), &expected);
    4237
    4238
    0
            let input = Arc::new(UInt32Array::from(vec![Some(2)])) as ArrayRef;
    4239
    0
            let modules = Arc::new(UInt32Array::from(vec![Some(100)])) as ArrayRef;
    4240
    0
            let result = bitwise_shift_left_dyn(Arc::clone(&input), Arc::clone(&modules))?;
    4241
    4242
    0
            let expected = UInt32Array::from(vec![Some(32)]);
    4243
    0
            assert_eq!(result.as_ref(), &expected);
    4244
    0
            Ok(())
    4245
    0
        }
    4246
    4247
        #[test]
    4248
    0
        fn bitwise_scalar_test() -> Result<()> {
    4249
    0
            let left = Arc::new(Int32Array::from(vec![Some(12), None, Some(11)])) as ArrayRef;
    4250
    0
            let right = ScalarValue::from(3i32);
    4251
    0
            let mut result = bitwise_and_dyn_scalar(&left, right.clone()).unwrap()?;
    4252
    0
            let expected = Int32Array::from(vec![Some(0), None, Some(3)]);
    4253
    0
            assert_eq!(result.as_ref(), &expected);
    4254
    4255
    0
            result = bitwise_or_dyn_scalar(&left, right.clone()).unwrap()?;
    4256
    0
            let expected = Int32Array::from(vec![Some(15), None, Some(11)]);
    4257
    0
            assert_eq!(result.as_ref(), &expected);
    4258
    4259
    0
            result = bitwise_xor_dyn_scalar(&left, right).unwrap()?;
    4260
    0
            let expected = Int32Array::from(vec![Some(15), None, Some(8)]);
    4261
    0
            assert_eq!(result.as_ref(), &expected);
    4262
    4263
    0
            let left =
    4264
    0
                Arc::new(UInt32Array::from(vec![Some(12), None, Some(11)])) as ArrayRef;
    4265
    0
            let right = ScalarValue::from(3u32);
    4266
    0
            let mut result = bitwise_and_dyn_scalar(&left, right.clone()).unwrap()?;
    4267
    0
            let expected = UInt32Array::from(vec![Some(0), None, Some(3)]);
    4268
    0
            assert_eq!(result.as_ref(), &expected);
    4269
    4270
    0
            result = bitwise_or_dyn_scalar(&left, right.clone()).unwrap()?;
    4271
    0
            let expected = UInt32Array::from(vec![Some(15), None, Some(11)]);
    4272
    0
            assert_eq!(result.as_ref(), &expected);
    4273
    4274
    0
            result = bitwise_xor_dyn_scalar(&left, right).unwrap()?;
    4275
    0
            let expected = UInt32Array::from(vec![Some(15), None, Some(8)]);
    4276
    0
            assert_eq!(result.as_ref(), &expected);
    4277
    0
            Ok(())
    4278
    0
        }
    4279
    4280
        #[test]
    4281
    0
        fn bitwise_shift_scalar_test() -> Result<()> {
    4282
    0
            let input = Arc::new(Int32Array::from(vec![Some(2), None, Some(4)])) as ArrayRef;
    4283
    0
            let module = ScalarValue::from(10i32);
    4284
    0
            let mut result =
    4285
    0
                bitwise_shift_left_dyn_scalar(&input, module.clone()).unwrap()?;
    4286
    4287
    0
            let expected = Int32Array::from(vec![Some(2048), None, Some(4096)]);
    4288
    0
            assert_eq!(result.as_ref(), &expected);
    4289
    4290
    0
            result = bitwise_shift_right_dyn_scalar(&result, module).unwrap()?;
    4291
    0
            assert_eq!(result.as_ref(), &input);
    4292
    4293
    0
            let input = Arc::new(UInt32Array::from(vec![Some(2), None, Some(4)])) as ArrayRef;
    4294
    0
            let module = ScalarValue::from(10u32);
    4295
    0
            let mut result =
    4296
    0
                bitwise_shift_left_dyn_scalar(&input, module.clone()).unwrap()?;
    4297
    4298
    0
            let expected = UInt32Array::from(vec![Some(2048), None, Some(4096)]);
    4299
    0
            assert_eq!(result.as_ref(), &expected);
    4300
    4301
    0
            result = bitwise_shift_right_dyn_scalar(&result, module).unwrap()?;
    4302
    0
            assert_eq!(result.as_ref(), &input);
    4303
    0
            Ok(())
    4304
    0
        }
    4305
    4306
        #[test]
    4307
    0
        fn test_display_and_or_combo() {
    4308
    0
            let expr = BinaryExpr::new(
    4309
    0
                Arc::new(BinaryExpr::new(
    4310
    0
                    lit(ScalarValue::from(1)),
    4311
    0
                    Operator::And,
    4312
    0
                    lit(ScalarValue::from(2)),
    4313
    0
                )),
    4314
    0
                Operator::And,
    4315
    0
                Arc::new(BinaryExpr::new(
    4316
    0
                    lit(ScalarValue::from(3)),
    4317
    0
                    Operator::And,
    4318
    0
                    lit(ScalarValue::from(4)),
    4319
    0
                )),
    4320
            );
    4321
    0
            assert_eq!(expr.to_string(), "1 AND 2 AND 3 AND 4");
    4322
    4323
    0
            let expr = BinaryExpr::new(
    4324
    0
                Arc::new(BinaryExpr::new(
    4325
    0
                    lit(ScalarValue::from(1)),
    4326
    0
                    Operator::Or,
    4327
    0
                    lit(ScalarValue::from(2)),
    4328
    0
                )),
    4329
    0
                Operator::Or,
    4330
    0
                Arc::new(BinaryExpr::new(
    4331
    0
                    lit(ScalarValue::from(3)),
    4332
    0
                    Operator::Or,
    4333
    0
                    lit(ScalarValue::from(4)),
    4334
    0
                )),
    4335
            );
    4336
    0
            assert_eq!(expr.to_string(), "1 OR 2 OR 3 OR 4");
    4337
    4338
    0
            let expr = BinaryExpr::new(
    4339
    0
                Arc::new(BinaryExpr::new(
    4340
    0
                    lit(ScalarValue::from(1)),
    4341
    0
                    Operator::And,
    4342
    0
                    lit(ScalarValue::from(2)),
    4343
    0
                )),
    4344
    0
                Operator::Or,
    4345
    0
                Arc::new(BinaryExpr::new(
    4346
    0
                    lit(ScalarValue::from(3)),
    4347
    0
                    Operator::And,
    4348
    0
                    lit(ScalarValue::from(4)),
    4349
    0
                )),
    4350
            );
    4351
    0
            assert_eq!(expr.to_string(), "1 AND 2 OR 3 AND 4");
    4352
    4353
    0
            let expr = BinaryExpr::new(
    4354
    0
                Arc::new(BinaryExpr::new(
    4355
    0
                    lit(ScalarValue::from(1)),
    4356
    0
                    Operator::Or,
    4357
    0
                    lit(ScalarValue::from(2)),
    4358
    0
                )),
    4359
    0
                Operator::And,
    4360
    0
                Arc::new(BinaryExpr::new(
    4361
    0
                    lit(ScalarValue::from(3)),
    4362
    0
                    Operator::Or,
    4363
    0
                    lit(ScalarValue::from(4)),
    4364
    0
                )),
    4365
            );
    4366
    0
            assert_eq!(expr.to_string(), "(1 OR 2) AND (3 OR 4)");
    4367
    0
        }
    4368
    4369
        #[test]
    4370
    0
        fn test_to_result_type_array() {
    4371
    0
            let values = Arc::new(Int32Array::from(vec![1, 2, 3, 4]));
    4372
    0
            let keys = Int8Array::from(vec![Some(0), None, Some(2), Some(3)]);
    4373
    0
            let dictionary =
    4374
    0
                Arc::new(DictionaryArray::try_new(keys, values).unwrap()) as ArrayRef;
    4375
    4376
            // Casting Dictionary to Int32
    4377
    0
            let casted = to_result_type_array(
    4378
    0
                &Operator::Plus,
    4379
    0
                Arc::clone(&dictionary),
    4380
    0
                &DataType::Int32,
    4381
            )
    4382
    0
            .unwrap();
    4383
    0
            assert_eq!(
    4384
    0
                &casted,
    4385
    0
                &(Arc::new(Int32Array::from(vec![Some(1), None, Some(3), Some(4)]))
    4386
    0
                    as ArrayRef)
    4387
            );
    4388
    4389
            // Array has same datatype as result type, no casting
    4390
    0
            let casted = to_result_type_array(
    4391
    0
                &Operator::Plus,
    4392
    0
                Arc::clone(&dictionary),
    4393
    0
                dictionary.data_type(),
    4394
            )
    4395
    0
            .unwrap();
    4396
    0
            assert_eq!(&casted, &dictionary);
    4397
    4398
            // Not numerical operator, no casting
    4399
    0
            let casted = to_result_type_array(
    4400
    0
                &Operator::Eq,
    4401
    0
                Arc::clone(&dictionary),
    4402
    0
                &DataType::Int32,
    4403
            )
    4404
    0
            .unwrap();
    4405
    0
            assert_eq!(&casted, &dictionary);
    4406
    0
        }
    4407
    4408
        #[test]
    4409
    0
        fn test_add_with_overflow() -> Result<()> {
    4410
            // create test data
    4411
    0
            let l = Arc::new(Int32Array::from(vec![1, i32::MAX]));
    4412
    0
            let r = Arc::new(Int32Array::from(vec![2, 1]));
    4413
    0
            let schema = Arc::new(Schema::new(vec![
    4414
    0
                Field::new("l", DataType::Int32, false),
    4415
    0
                Field::new("r", DataType::Int32, false),
    4416
            ]));
    4417
    0
            let batch = RecordBatch::try_new(schema, vec![l, r])?;
    4418
    4419
            // create expression
    4420
    0
            let expr = BinaryExpr::new(
    4421
    0
                Arc::new(Column::new("l", 0)),
    4422
    0
                Operator::Plus,
    4423
    0
                Arc::new(Column::new("r", 1)),
    4424
            )
    4425
    0
            .with_fail_on_overflow(true);
    4426
    4427
            // evaluate expression
    4428
    0
            let result = expr.evaluate(&batch);
    4429
    0
            assert!(result
    4430
    0
                .err()
    4431
    0
                .unwrap()
    4432
    0
                .to_string()
    4433
    0
                .contains("Overflow happened on: 2147483647 + 1"));
    4434
    0
            Ok(())
    4435
    0
        }
    4436
    4437
        #[test]
    4438
    0
        fn test_subtract_with_overflow() -> Result<()> {
    4439
            // create test data
    4440
    0
            let l = Arc::new(Int32Array::from(vec![1, i32::MIN]));
    4441
    0
            let r = Arc::new(Int32Array::from(vec![2, 1]));
    4442
    0
            let schema = Arc::new(Schema::new(vec![
    4443
    0
                Field::new("l", DataType::Int32, false),
    4444
    0
                Field::new("r", DataType::Int32, false),
    4445
            ]));
    4446
    0
            let batch = RecordBatch::try_new(schema, vec![l, r])?;
    4447
    4448
            // create expression
    4449
    0
            let expr = BinaryExpr::new(
    4450
    0
                Arc::new(Column::new("l", 0)),
    4451
    0
                Operator::Minus,
    4452
    0
                Arc::new(Column::new("r", 1)),
    4453
            )
    4454
    0
            .with_fail_on_overflow(true);
    4455
    4456
            // evaluate expression
    4457
    0
            let result = expr.evaluate(&batch);
    4458
    0
            assert!(result
    4459
    0
                .err()
    4460
    0
                .unwrap()
    4461
    0
                .to_string()
    4462
    0
                .contains("Overflow happened on: -2147483648 - 1"));
    4463
    0
            Ok(())
    4464
    0
        }
    4465
    4466
        #[test]
    4467
    0
        fn test_mul_with_overflow() -> Result<()> {
    4468
            // create test data
    4469
    0
            let l = Arc::new(Int32Array::from(vec![1, i32::MAX]));
    4470
    0
            let r = Arc::new(Int32Array::from(vec![2, 2]));
    4471
    0
            let schema = Arc::new(Schema::new(vec![
    4472
    0
                Field::new("l", DataType::Int32, false),
    4473
    0
                Field::new("r", DataType::Int32, false),
    4474
            ]));
    4475
    0
            let batch = RecordBatch::try_new(schema, vec![l, r])?;
    4476
    4477
            // create expression
    4478
    0
            let expr = BinaryExpr::new(
    4479
    0
                Arc::new(Column::new("l", 0)),
    4480
    0
                Operator::Multiply,
    4481
    0
                Arc::new(Column::new("r", 1)),
    4482
            )
    4483
    0
            .with_fail_on_overflow(true);
    4484
    4485
            // evaluate expression
    4486
    0
            let result = expr.evaluate(&batch);
    4487
    0
            assert!(result
    4488
    0
                .err()
    4489
    0
                .unwrap()
    4490
    0
                .to_string()
    4491
    0
                .contains("Overflow happened on: 2147483647 * 2"));
    4492
    0
            Ok(())
    4493
    0
        }
    4494
    4495
        /// Test helper for SIMILAR TO binary operation
    4496
    0
        fn apply_similar_to(
    4497
    0
            schema: &SchemaRef,
    4498
    0
            va: Vec<&str>,
    4499
    0
            vb: Vec<&str>,
    4500
    0
            negated: bool,
    4501
    0
            case_insensitive: bool,
    4502
    0
            expected: &BooleanArray,
    4503
    0
        ) -> Result<()> {
    4504
    0
            let a = StringArray::from(va);
    4505
    0
            let b = StringArray::from(vb);
    4506
    0
            let op = similar_to(
    4507
    0
                negated,
    4508
    0
                case_insensitive,
    4509
    0
                col("a", schema)?,
    4510
    0
                col("b", schema)?,
    4511
    0
            )?;
    4512
    0
            let batch =
    4513
    0
                RecordBatch::try_new(Arc::clone(schema), vec![Arc::new(a), Arc::new(b)])?;
    4514
    0
            let result = op
    4515
    0
                .evaluate(&batch)?
    4516
    0
                .into_array(batch.num_rows())
    4517
    0
                .expect("Failed to convert to array");
    4518
    0
            assert_eq!(result.as_ref(), expected);
    4519
    4520
    0
            Ok(())
    4521
    0
        }
    4522
    4523
        #[test]
    4524
    0
        fn test_similar_to() {
    4525
    0
            let schema = Arc::new(Schema::new(vec![
    4526
    0
                Field::new("a", DataType::Utf8, false),
    4527
    0
                Field::new("b", DataType::Utf8, false),
    4528
            ]));
    4529
    4530
    0
            let expected = [Some(true), Some(false)].iter().collect();
    4531
            // case-sensitive
    4532
    0
            apply_similar_to(
    4533
    0
                &schema,
    4534
    0
                vec!["hello world", "Hello World"],
    4535
    0
                vec!["hello.*", "hello.*"],
    4536
                false,
    4537
                false,
    4538
    0
                &expected,
    4539
            )
    4540
    0
            .unwrap();
    4541
            // case-insensitive
    4542
    0
            apply_similar_to(
    4543
    0
                &schema,
    4544
    0
                vec!["hello world", "bye"],
    4545
    0
                vec!["hello.*", "hello.*"],
    4546
                false,
    4547
                true,
    4548
    0
                &expected,
    4549
            )
    4550
    0
            .unwrap();
    4551
    0
        }
    4552
    4553
    0
        pub fn binary_expr(
    4554
    0
            left: Arc<dyn PhysicalExpr>,
    4555
    0
            op: Operator,
    4556
    0
            right: Arc<dyn PhysicalExpr>,
    4557
    0
            schema: &Schema,
    4558
    0
        ) -> Result<BinaryExpr> {
    4559
    0
            Ok(binary_op(left, op, right, schema)?
    4560
    0
                .as_any()
    4561
    0
                .downcast_ref::<BinaryExpr>()
    4562
    0
                .unwrap()
    4563
    0
                .clone())
    4564
    0
        }
    4565
    4566
        /// Test for Uniform-Uniform, Unknown-Uniform, Uniform-Unknown and Unknown-Unknown evaluation.
    4567
        #[test]
    4568
    0
        fn test_evaluate_statistics_combination_of_range_holders() -> Result<()> {
    4569
    0
            let schema = &Schema::new(vec![Field::new("a", DataType::Float64, false)]);
    4570
    0
            let a = Arc::new(Column::new("a", 0)) as _;
    4571
    0
            let b = lit(ScalarValue::from(12.0));
    4572
    4573
    0
            let left_interval = Interval::make(Some(0.0), Some(12.0))?;
    4574
    0
            let right_interval = Interval::make(Some(12.0), Some(36.0))?;
    4575
    0
            let (left_mean, right_mean) = (ScalarValue::from(6.0), ScalarValue::from(24.0));
    4576
    0
            let (left_med, right_med) = (ScalarValue::from(6.0), ScalarValue::from(24.0));
    4577
    4578
    0
            for children in [
    4579
    0
                vec![
    4580
    0
                    &Distribution::new_uniform(left_interval.clone())?,
    4581
    0
                    &Distribution::new_uniform(right_interval.clone())?,
    4582
                ],
    4583
    0
                vec![
    4584
    0
                    &Distribution::new_generic(
    4585
    0
                        left_mean.clone(),
    4586
    0
                        left_med.clone(),
    4587
    0
                        ScalarValue::Float64(None),
    4588
    0
                        left_interval.clone(),
    4589
    0
                    )?,
    4590
    0
                    &Distribution::new_uniform(right_interval.clone())?,
    4591
                ],
    4592
    0
                vec![
    4593
    0
                    &Distribution::new_uniform(right_interval.clone())?,
    4594
    0
                    &Distribution::new_generic(
    4595
    0
                        right_mean.clone(),
    4596
    0
                        right_med.clone(),
    4597
    0
                        ScalarValue::Float64(None),
    4598
    0
                        right_interval.clone(),
    4599
    0
                    )?,
    4600
                ],
    4601
    0
                vec![
    4602
    0
                    &Distribution::new_generic(
    4603
    0
                        left_mean.clone(),
    4604
    0
                        left_med.clone(),
    4605
    0
                        ScalarValue::Float64(None),
    4606
    0
                        left_interval.clone(),
    4607
    0
                    )?,
    4608
    0
                    &Distribution::new_generic(
    4609
    0
                        right_mean.clone(),
    4610
    0
                        right_med.clone(),
    4611
    0
                        ScalarValue::Float64(None),
    4612
    0
                        right_interval.clone(),
    4613
    0
                    )?,
    4614
                ],
    4615
            ] {
    4616
    0
                let ops = vec![
    4617
    0
                    Operator::Plus,
    4618
    0
                    Operator::Minus,
    4619
    0
                    Operator::Multiply,
    4620
    0
                    Operator::Divide,
    4621
                ];
    4622
    4623
    0
                for op in ops {
    4624
    0
                    let expr = binary_expr(Arc::clone(&a), op, Arc::clone(&b), schema)?;
    4625
    0
                    assert_eq!(
    4626
    0
                        expr.evaluate_statistics(&children)?,
    4627
    0
                        new_generic_from_binary_op(&op, children[0], children[1])?
    4628
                    );
    4629
                }
    4630
            }
    4631
    0
            Ok(())
    4632
    0
        }
    4633
    4634
        #[test]
    4635
    0
        fn test_evaluate_statistics_bernoulli() -> Result<()> {
    4636
    0
            let schema = &Schema::new(vec![
    4637
    0
                Field::new("a", DataType::Int64, false),
    4638
    0
                Field::new("b", DataType::Int64, false),
    4639
    0
            ]);
    4640
    0
            let a = Arc::new(Column::new("a", 0)) as _;
    4641
    0
            let b = Arc::new(Column::new("b", 1)) as _;
    4642
    0
            let eq = Arc::new(binary_expr(
    4643
    0
                Arc::clone(&a),
    4644
    0
                Operator::Eq,
    4645
    0
                Arc::clone(&b),
    4646
    0
                schema,
    4647
    0
            )?);
    4648
    0
            let neq = Arc::new(binary_expr(a, Operator::NotEq, b, schema)?);
    4649
    4650
    0
            let left_stat = &Distribution::new_uniform(Interval::make(Some(0), Some(7))?)?;
    4651
    0
            let right_stat = &Distribution::new_uniform(Interval::make(Some(4), Some(11))?)?;
    4652
    4653
            // Intervals: [0, 7], [4, 11].
    4654
            // The intersection is [4, 7], so the probability of equality is 4 / 64 = 1 / 16.
    4655
    0
            assert_eq!(
    4656
    0
                eq.evaluate_statistics(&[left_stat, right_stat])?,
    4657
    0
                Distribution::new_bernoulli(ScalarValue::from(1.0 / 16.0))?
    4658
            );
    4659
    4660
            // The probability of being distinct is 1 - 1 / 16 = 15 / 16.
    4661
    0
            assert_eq!(
    4662
    0
                neq.evaluate_statistics(&[left_stat, right_stat])?,
    4663
    0
                Distribution::new_bernoulli(ScalarValue::from(15.0 / 16.0))?
    4664
            );
    4665
    4666
    0
            Ok(())
    4667
    0
        }
    4668
    4669
        #[test]
    4670
    0
        fn test_propagate_statistics_combination_of_range_holders_arithmetic() -> Result<()> {
    4671
    0
            let schema = &Schema::new(vec![Field::new("a", DataType::Float64, false)]);
    4672
    0
            let a = Arc::new(Column::new("a", 0)) as _;
    4673
    0
            let b = lit(ScalarValue::from(12.0));
    4674
    4675
    0
            let left_interval = Interval::make(Some(0.0), Some(12.0))?;
    4676
    0
            let right_interval = Interval::make(Some(12.0), Some(36.0))?;
    4677
    4678
    0
            let parent = Distribution::new_uniform(Interval::make(Some(-432.), Some(432.))?)?;
    4679
    0
            let children = vec![
    4680
    0
                vec![
    4681
    0
                    Distribution::new_uniform(left_interval.clone())?,
    4682
    0
                    Distribution::new_uniform(right_interval.clone())?,
    4683
                ],
    4684
    0
                vec![
    4685
    0
                    Distribution::new_generic(
    4686
    0
                        ScalarValue::from(6.),
    4687
    0
                        ScalarValue::from(6.),
    4688
    0
                        ScalarValue::Float64(None),
    4689
    0
                        left_interval.clone(),
    4690
    0
                    )?,
    4691
    0
                    Distribution::new_uniform(right_interval.clone())?,
    4692
                ],
    4693
    0
                vec![
    4694
    0
                    Distribution::new_uniform(left_interval.clone())?,
    4695
    0
                    Distribution::new_generic(
    4696
    0
                        ScalarValue::from(12.),
    4697
    0
                        ScalarValue::from(12.),
    4698
    0
                        ScalarValue::Float64(None),
    4699
    0
                        right_interval.clone(),
    4700
    0
                    )?,
    4701
                ],
    4702
    0
                vec![
    4703
    0
                    Distribution::new_generic(
    4704
    0
                        ScalarValue::from(6.),
    4705
    0
                        ScalarValue::from(6.),
    4706
    0
                        ScalarValue::Float64(None),
    4707
    0
                        left_interval.clone(),
    4708
    0
                    )?,
    4709
    0
                    Distribution::new_generic(
    4710
    0
                        ScalarValue::from(12.),
    4711
    0
                        ScalarValue::from(12.),
    4712
    0
                        ScalarValue::Float64(None),
    4713
    0
                        right_interval.clone(),
    4714
    0
                    )?,
    4715
                ],
    4716
            ];
    4717
    4718
    0
            let ops = vec![
    4719
    0
                Operator::Plus,
    4720
    0
                Operator::Minus,
    4721
    0
                Operator::Multiply,
    4722
    0
                Operator::Divide,
    4723
            ];
    4724
    4725
    0
            for child_view in children {
    4726
    0
                let child_refs = child_view.iter().collect::<Vec<_>>();
    4727
    0
                for op in &ops {
    4728
    0
                    let expr = binary_expr(Arc::clone(&a), *op, Arc::clone(&b), schema)?;
    4729
    0
                    assert_eq!(
    4730
    0
                        expr.propagate_statistics(&parent, child_refs.as_slice())?,
    4731
    0
                        Some(child_view.clone())
    4732
                    );
    4733
                }
    4734
            }
    4735
    0
            Ok(())
    4736
    0
        }
    4737
    4738
        #[test]
    4739
    0
        fn test_propagate_statistics_combination_of_range_holders_comparison() -> Result<()> {
    4740
    0
            let schema = &Schema::new(vec![Field::new("a", DataType::Float64, false)]);
    4741
    0
            let a = Arc::new(Column::new("a", 0)) as _;
    4742
    0
            let b = lit(ScalarValue::from(12.0));
    4743
    4744
    0
            let left_interval = Interval::make(Some(0.0), Some(12.0))?;
    4745
    0
            let right_interval = Interval::make(Some(6.0), Some(18.0))?;
    4746
    4747
    0
            let one = ScalarValue::from(1.0);
    4748
    0
            let parent = Distribution::new_bernoulli(one)?;
    4749
    0
            let children = vec![
    4750
    0
                vec![
    4751
    0
                    Distribution::new_uniform(left_interval.clone())?,
    4752
    0
                    Distribution::new_uniform(right_interval.clone())?,
    4753
                ],
    4754
    0
                vec![
    4755
    0
                    Distribution::new_generic(
    4756
    0
                        ScalarValue::from(6.),
    4757
    0
                        ScalarValue::from(6.),
    4758
    0
                        ScalarValue::Float64(None),
    4759
    0
                        left_interval.clone(),
    4760
    0
                    )?,
    4761
    0
                    Distribution::new_uniform(right_interval.clone())?,
    4762
                ],
    4763
    0
                vec![
    4764
    0
                    Distribution::new_uniform(left_interval.clone())?,
    4765
    0
                    Distribution::new_generic(
    4766
    0
                        ScalarValue::from(12.),
    4767
    0
                        ScalarValue::from(12.),
    4768
    0
                        ScalarValue::Float64(None),
    4769
    0
                        right_interval.clone(),
    4770
    0
                    )?,
    4771
                ],
    4772
    0
                vec![
    4773
    0
                    Distribution::new_generic(
    4774
    0
                        ScalarValue::from(6.),
    4775
    0
                        ScalarValue::from(6.),
    4776
    0
                        ScalarValue::Float64(None),
    4777
    0
                        left_interval.clone(),
    4778
    0
                    )?,
    4779
    0
                    Distribution::new_generic(
    4780
    0
                        ScalarValue::from(12.),
    4781
    0
                        ScalarValue::from(12.),
    4782
    0
                        ScalarValue::Float64(None),
    4783
    0
                        right_interval.clone(),
    4784
    0
                    )?,
    4785
                ],
    4786
            ];
    4787
    4788
    0
            let ops = vec![
    4789
    0
                Operator::Eq,
    4790
    0
                Operator::Gt,
    4791
    0
                Operator::GtEq,
    4792
    0
                Operator::Lt,
    4793
    0
                Operator::LtEq,
    4794
            ];
    4795
    4796
    0
            for child_view in children {
    4797
    0
                let child_refs = child_view.iter().collect::<Vec<_>>();
    4798
    0
                for op in &ops {
    4799
    0
                    let expr = binary_expr(Arc::clone(&a), *op, Arc::clone(&b), schema)?;
    4800
    0
                    assert!(expr
    4801
    0
                        .propagate_statistics(&parent, child_refs.as_slice())?
    4802
    0
                        .is_some());
    4803
                }
    4804
            }
    4805
    4806
    0
            Ok(())
    4807
    0
        }
    4808
    4809
        #[test]
    4810
    0
        fn test_fmt_sql() -> Result<()> {
    4811
    0
            let schema = Schema::new(vec![
    4812
    0
                Field::new("a", DataType::Int32, false),
    4813
    0
                Field::new("b", DataType::Int32, false),
    4814
            ]);
    4815
    4816
            // Test basic binary expressions
    4817
    0
            let simple_expr = binary_expr(
    4818
    0
                col("a", &schema)?,
    4819
    0
                Operator::Plus,
    4820
    0
                col("b", &schema)?,
    4821
    0
                &schema,
    4822
    0
            )?;
    4823
    0
            let display_string = simple_expr.to_string();
    4824
    0
            assert_eq!(display_string, "a@0 + b@1");
    4825
    0
            let sql_string = fmt_sql(&simple_expr).to_string();
    4826
    0
            assert_eq!(sql_string, "a + b");
    4827
    4828
            // Test nested expressions with different operator precedence
    4829
    0
            let nested_expr = binary_expr(
    4830
    0
                Arc::new(binary_expr(
    4831
    0
                    col("a", &schema)?,
    4832
    0
                    Operator::Plus,
    4833
    0
                    col("b", &schema)?,
    4834
    0
                    &schema,
    4835
    0
                )?),
    4836
    0
                Operator::Multiply,
    4837
    0
                col("b", &schema)?,
    4838
    0
                &schema,
    4839
    0
            )?;
    4840
    0
            let display_string = nested_expr.to_string();
    4841
    0
            assert_eq!(display_string, "(a@0 + b@1) * b@1");
    4842
    0
            let sql_string = fmt_sql(&nested_expr).to_string();
    4843
    0
            assert_eq!(sql_string, "(a + b) * b");
    4844
    4845
            // Test nested expressions with same operator precedence
    4846
    0
            let nested_same_prec = binary_expr(
    4847
    0
                Arc::new(binary_expr(
    4848
    0
                    col("a", &schema)?,
    4849
    0
                    Operator::Plus,
    4850
    0
                    col("b", &schema)?,
    4851
    0
                    &schema,
    4852
    0
                )?),
    4853
    0
                Operator::Plus,
    4854
    0
                col("b", &schema)?,
    4855
    0
                &schema,
    4856
    0
            )?;
    4857
    0
            let display_string = nested_same_prec.to_string();
    4858
    0
            assert_eq!(display_string, "a@0 + b@1 + b@1");
    4859
    0
            let sql_string = fmt_sql(&nested_same_prec).to_string();
    4860
    0
            assert_eq!(sql_string, "a + b + b");
    4861
    4862
            // Test with literals
    4863
    0
            let lit_expr = binary_expr(
    4864
    0
                col("a", &schema)?,
    4865
    0
                Operator::Eq,
    4866
    0
                lit(ScalarValue::Int32(Some(42))),
    4867
    0
                &schema,
    4868
    0
            )?;
    4869
    0
            let display_string = lit_expr.to_string();
    4870
    0
            assert_eq!(display_string, "a@0 = 42");
    4871
    0
            let sql_string = fmt_sql(&lit_expr).to_string();
    4872
    0
            assert_eq!(sql_string, "a = 42");
    4873
    4874
    0
            Ok(())
    4875
    0
        }
    4876
    4877
        #[test]
    4878
    0
        fn test_check_short_circuit() {
    4879
            // Test with non-nullable arrays
    4880
    0
            let schema = Arc::new(Schema::new(vec![
    4881
    0
                Field::new("a", DataType::Int32, false),
    4882
    0
                Field::new("b", DataType::Int32, false),
    4883
            ]));
    4884
    0
            let a_array = Int32Array::from(vec![1, 3, 4, 5, 6]);
    4885
    0
            let b_array = Int32Array::from(vec![1, 2, 3, 4, 5]);
    4886
    0
            let batch = RecordBatch::try_new(
    4887
    0
                Arc::clone(&schema),
    4888
    0
                vec![Arc::new(a_array), Arc::new(b_array)],
    4889
            )
    4890
    0
            .unwrap();
    4891
    4892
            // op: AND left: all false
    4893
    0
            let left_expr = logical2physical(&logical_col("a").eq(expr_lit(2)), &schema);
    4894
    0
            let left_value = left_expr.evaluate(&batch).unwrap();
    4895
    0
            assert!(matches!(
    4896
    0
                check_short_circuit(&left_value, &Operator::And),
    4897
                ShortCircuitStrategy::ReturnLeft
    4898
            ));
    4899
    4900
            // op: AND left: not all false
    4901
    0
            let left_expr = logical2physical(&logical_col("a").eq(expr_lit(3)), &schema);
    4902
    0
            let left_value = left_expr.evaluate(&batch).unwrap();
    4903
    0
            let ColumnarValue::Array(array) = &left_value else {
    4904
    0
                panic!("Expected ColumnarValue::Array");
    4905
            };
    4906
    0
            let ShortCircuitStrategy::PreSelection(value) =
    4907
    0
                check_short_circuit(&left_value, &Operator::And)
    4908
            else {
    4909
    0
                panic!("Expected ShortCircuitStrategy::PreSelection");
    4910
            };
    4911
    0
            let expected_boolean_arr: Vec<_> =
    4912
    0
                as_boolean_array(array).unwrap().iter().collect();
    4913
    0
            let boolean_arr: Vec<_> = value.iter().collect();
    4914
    0
            assert_eq!(expected_boolean_arr, boolean_arr);
    4915
    4916
            // op: OR left: all true
    4917
    0
            let left_expr = logical2physical(&logical_col("a").gt(expr_lit(0)), &schema);
    4918
    0
            let left_value = left_expr.evaluate(&batch).unwrap();
    4919
    0
            assert!(matches!(
    4920
    0
                check_short_circuit(&left_value, &Operator::Or),
    4921
                ShortCircuitStrategy::ReturnLeft
    4922
            ));
    4923
    4924
            // op: OR left: not all true
    4925
    0
            let left_expr: Arc<dyn PhysicalExpr> =
    4926
    0
                logical2physical(&logical_col("a").gt(expr_lit(2)), &schema);
    4927
    0
            let left_value = left_expr.evaluate(&batch).unwrap();
    4928
    0
            assert!(matches!(
    4929
    0
                check_short_circuit(&left_value, &Operator::Or),
    4930
                ShortCircuitStrategy::None
    4931
            ));
    4932
    4933
            // Test with nullable arrays and null values
    4934
    0
            let schema_nullable = Arc::new(Schema::new(vec![
    4935
    0
                Field::new("c", DataType::Boolean, true),
    4936
    0
                Field::new("d", DataType::Boolean, true),
    4937
            ]));
    4938
    4939
            // Create arrays with null values
    4940
    0
            let c_array = Arc::new(BooleanArray::from(vec![
    4941
    0
                Some(true),
    4942
    0
                Some(false),
    4943
    0
                None,
    4944
    0
                Some(true),
    4945
    0
                None,
    4946
    0
            ])) as ArrayRef;
    4947
    0
            let d_array = Arc::new(BooleanArray::from(vec![
    4948
    0
                Some(false),
    4949
    0
                Some(true),
    4950
    0
                Some(false),
    4951
    0
                None,
    4952
    0
                Some(true),
    4953
    0
            ])) as ArrayRef;
    4954
    4955
    0
            let batch_nullable = RecordBatch::try_new(
    4956
    0
                Arc::clone(&schema_nullable),
    4957
    0
                vec![Arc::clone(&c_array), Arc::clone(&d_array)],
    4958
            )
    4959
    0
            .unwrap();
    4960
    4961
            // Case: Mixed values with nulls - shouldn't short-circuit for AND
    4962
    0
            let mixed_nulls = logical2physical(&logical_col("c"), &schema_nullable);
    4963
    0
            let mixed_nulls_value = mixed_nulls.evaluate(&batch_nullable).unwrap();
    4964
    0
            assert!(matches!(
    4965
    0
                check_short_circuit(&mixed_nulls_value, &Operator::And),
    4966
                ShortCircuitStrategy::None
    4967
            ));
    4968
    4969
            // Case: Mixed values with nulls - shouldn't short-circuit for OR
    4970
    0
            assert!(matches!(
    4971
    0
                check_short_circuit(&mixed_nulls_value, &Operator::Or),
    4972
                ShortCircuitStrategy::None
    4973
            ));
    4974
    4975
            // Test with all nulls
    4976
    0
            let all_nulls = Arc::new(BooleanArray::from(vec![None, None, None])) as ArrayRef;
    4977
    0
            let null_batch = RecordBatch::try_new(
    4978
    0
                Arc::new(Schema::new(vec![Field::new("e", DataType::Boolean, true)])),
    4979
    0
                vec![all_nulls],
    4980
            )
    4981
    0
            .unwrap();
    4982
    4983
    0
            let null_expr = logical2physical(&logical_col("e"), &null_batch.schema());
    4984
    0
            let null_value = null_expr.evaluate(&null_batch).unwrap();
    4985
    4986
            // All nulls shouldn't short-circuit for AND or OR
    4987
    0
            assert!(matches!(
    4988
    0
                check_short_circuit(&null_value, &Operator::And),
    4989
                ShortCircuitStrategy::None
    4990
            ));
    4991
    0
            assert!(matches!(
    4992
    0
                check_short_circuit(&null_value, &Operator::Or),
    4993
                ShortCircuitStrategy::None
    4994
            ));
    4995
    4996
            // Test with scalar values
    4997
            // Scalar true
    4998
    0
            let scalar_true = ColumnarValue::Scalar(ScalarValue::Boolean(Some(true)));
    4999
    0
            assert!(matches!(
    5000
    0
                check_short_circuit(&scalar_true, &Operator::Or),
    5001
                ShortCircuitStrategy::ReturnLeft
    5002
            )); // Should short-circuit OR
    5003
    0
            assert!(matches!(
    5004
    0
                check_short_circuit(&scalar_true, &Operator::And),
    5005
                ShortCircuitStrategy::ReturnRight
    5006
            )); // Should return the RHS for AND
    5007
    5008
            // Scalar false
    5009
    0
            let scalar_false = ColumnarValue::Scalar(ScalarValue::Boolean(Some(false)));
    5010
    0
            assert!(matches!(
    5011
    0
                check_short_circuit(&scalar_false, &Operator::And),
    5012
                ShortCircuitStrategy::ReturnLeft
    5013
            )); // Should short-circuit AND
    5014
    0
            assert!(matches!(
    5015
    0
                check_short_circuit(&scalar_false, &Operator::Or),
    5016
                ShortCircuitStrategy::ReturnRight
    5017
            )); // Should return the RHS for OR
    5018
    5019
            // Scalar null
    5020
    0
            let scalar_null = ColumnarValue::Scalar(ScalarValue::Boolean(None));
    5021
    0
            assert!(matches!(
    5022
    0
                check_short_circuit(&scalar_null, &Operator::And),
    5023
                ShortCircuitStrategy::None
    5024
            ));
    5025
    0
            assert!(matches!(
    5026
    0
                check_short_circuit(&scalar_null, &Operator::Or),
    5027
                ShortCircuitStrategy::None
    5028
            ));
    5029
    0
        }
    5030
    5031
        /// Test for [pre_selection_scatter]
    5032
        /// Since [check_short_circuit] ensures that the left side does not contain null and is neither all_true nor all_false, as well as not being empty,
    5033
        /// the following tests have been designed:
    5034
        /// 1. Test sparse left with interleaved true/false
    5035
        /// 2. Test multiple consecutive true blocks
    5036
        /// 3. Test multiple consecutive true blocks
    5037
        /// 4. Test single true at first position
    5038
        /// 5. Test single true at last position
    5039
        /// 6. Test nulls in right array
    5040
        #[test]
    5041
    0
        fn test_pre_selection_scatter() {
    5042
    0
            fn create_bool_array(bools: Vec<bool>) -> BooleanArray {
    5043
    0
                BooleanArray::from(bools.into_iter().map(Some).collect::<Vec<_>>())
    5044
    0
            }
    5045
            // Test sparse left with interleaved true/false
    5046
            {
    5047
                // Left: [T, F, T, F, T]
    5048
                // Right: [F, T, F] (values for 3 true positions)
    5049
    0
                let left = create_bool_array(vec![true, false, true, false, true]);
    5050
    0
                let right = create_bool_array(vec![false, true, false]);
    5051
    5052
    0
                let result = pre_selection_scatter(&left, Some(&right)).unwrap();
    5053
    0
                let result_arr = result.into_array(left.len()).unwrap();
    5054
    5055
    0
                let expected = create_bool_array(vec![false, false, true, false, false]);
    5056
    0
                assert_eq!(&expected, result_arr.as_boolean());
    5057
            }
    5058
            // Test multiple consecutive true blocks
    5059
            {
    5060
                // Left: [F, T, T, F, T, T, T]
    5061
                // Right: [T, F, F, T, F]
    5062
    0
                let left =
    5063
    0
                    create_bool_array(vec![false, true, true, false, true, true, true]);
    5064
    0
                let right = create_bool_array(vec![true, false, false, true, false]);
    5065
    5066
    0
                let result = pre_selection_scatter(&left, Some(&right)).unwrap();
    5067
    0
                let result_arr = result.into_array(left.len()).unwrap();
    5068
    5069
    0
                let expected =
    5070
    0
                    create_bool_array(vec![false, true, false, false, false, true, false]);
    5071
    0
                assert_eq!(&expected, result_arr.as_boolean());
    5072
            }
    5073
            // Test single true at first position
    5074
            {
    5075
                // Left: [T, F, F]
    5076
                // Right: [F]
    5077
    0
                let left = create_bool_array(vec![true, false, false]);
    5078
    0
                let right = create_bool_array(vec![false]);
    5079
    5080
    0
                let result = pre_selection_scatter(&left, Some(&right)).unwrap();
    5081
    0
                let result_arr = result.into_array(left.len()).unwrap();
    5082
    5083
    0
                let expected = create_bool_array(vec![false, false, false]);
    5084
    0
                assert_eq!(&expected, result_arr.as_boolean());
    5085
            }
    5086
            // Test single true at last position
    5087
            {
    5088
                // Left: [F, F, T]
    5089
                // Right: [F]
    5090
    0
                let left = create_bool_array(vec![false, false, true]);
    5091
    0
                let right = create_bool_array(vec![false]);
    5092
    5093
    0
                let result = pre_selection_scatter(&left, Some(&right)).unwrap();
    5094
    0
                let result_arr = result.into_array(left.len()).unwrap();
    5095
    5096
    0
                let expected = create_bool_array(vec![false, false, false]);
    5097
    0
                assert_eq!(&expected, result_arr.as_boolean());
    5098
            }
    5099
            // Test nulls in right array
    5100
            {
    5101
                // Left: [F, T, F, T]
    5102
                // Right: [None, Some(false)] (with null at first position)
    5103
    0
                let left = create_bool_array(vec![false, true, false, true]);
    5104
    0
                let right = BooleanArray::from(vec![None, Some(false)]);
    5105
    5106
    0
                let result = pre_selection_scatter(&left, Some(&right)).unwrap();
    5107
    0
                let result_arr = result.into_array(left.len()).unwrap();
    5108
    5109
    0
                let expected = BooleanArray::from(vec![
    5110
    0
                    Some(false),
    5111
    0
                    None, // null from right
    5112
    0
                    Some(false),
    5113
    0
                    Some(false),
    5114
                ]);
    5115
    0
                assert_eq!(&expected, result_arr.as_boolean());
    5116
            }
    5117
    0
        }
    5118
    5119
        #[test]
    5120
    0
        fn test_and_true_preselection_returns_lhs() {
    5121
    0
            let schema =
    5122
    0
                Arc::new(Schema::new(vec![Field::new("c", DataType::Boolean, false)]));
    5123
    0
            let c_array = Arc::new(BooleanArray::from(vec![false, true, false, false, false]))
    5124
    0
                as ArrayRef;
    5125
    0
            let batch = RecordBatch::try_new(Arc::clone(&schema), vec![Arc::clone(&c_array)])
    5126
    0
                .unwrap();
    5127
    5128
    0
            let expr = logical2physical(&logical_col("c").and(expr_lit(true)), &schema);
    5129
    5130
    0
            let result = expr.evaluate(&batch).unwrap();
    5131
    0
            let ColumnarValue::Array(result_arr) = result else {
    5132
    0
                panic!("Expected ColumnarValue::Array");
    5133
            };
    5134
    5135
    0
            let expected: Vec<_> = c_array.as_boolean().iter().collect();
    5136
    0
            let actual: Vec<_> = result_arr.as_boolean().iter().collect();
    5137
    0
            assert_eq!(
    5138
                expected, actual,
    5139
    0
                "AND with TRUE must equal LHS even with PreSelection"
    5140
            );
    5141
    0
        }
    5142
    5143
        #[test]
    5144
    0
        fn test_evaluate_bounds_int32() {
    5145
    0
            let schema = Schema::new(vec![
    5146
    0
                Field::new("a", DataType::Int32, false),
    5147
    0
                Field::new("b", DataType::Int32, false),
    5148
            ]);
    5149
    5150
    0
            let a = Arc::new(Column::new("a", 0)) as _;
    5151
    0
            let b = Arc::new(Column::new("b", 1)) as _;
    5152
    5153
            // Test addition bounds
    5154
    0
            let add_expr =
    5155
    0
                binary_expr(Arc::clone(&a), Operator::Plus, Arc::clone(&b), &schema).unwrap();
    5156
    0
            let add_bounds = add_expr
    5157
    0
                .evaluate_bounds(&[
    5158
    0
                    &Interval::make(Some(1), Some(10)).unwrap(),
    5159
    0
                    &Interval::make(Some(5), Some(15)).unwrap(),
    5160
    0
                ])
    5161
    0
                .unwrap();
    5162
    0
            assert_eq!(add_bounds, Interval::make(Some(6), Some(25)).unwrap());
    5163
    5164
            // Test subtraction bounds
    5165
    0
            let sub_expr =
    5166
    0
                binary_expr(Arc::clone(&a), Operator::Minus, Arc::clone(&b), &schema)
    5167
    0
                    .unwrap();
    5168
    0
            let sub_bounds = sub_expr
    5169
    0
                .evaluate_bounds(&[
    5170
    0
                    &Interval::make(Some(1), Some(10)).unwrap(),
    5171
    0
                    &Interval::make(Some(5), Some(15)).unwrap(),
    5172
    0
                ])
    5173
    0
                .unwrap();
    5174
    0
            assert_eq!(sub_bounds, Interval::make(Some(-14), Some(5)).unwrap());
    5175
    5176
            // Test multiplication bounds
    5177
    0
            let mul_expr =
    5178
    0
                binary_expr(Arc::clone(&a), Operator::Multiply, Arc::clone(&b), &schema)
    5179
    0
                    .unwrap();
    5180
    0
            let mul_bounds = mul_expr
    5181
    0
                .evaluate_bounds(&[
    5182
    0
                    &Interval::make(Some(1), Some(10)).unwrap(),
    5183
    0
                    &Interval::make(Some(5), Some(15)).unwrap(),
    5184
    0
                ])
    5185
    0
                .unwrap();
    5186
    0
            assert_eq!(mul_bounds, Interval::make(Some(5), Some(150)).unwrap());
    5187
    5188
            // Test division bounds
    5189
    0
            let div_expr =
    5190
    0
                binary_expr(Arc::clone(&a), Operator::Divide, Arc::clone(&b), &schema)
    5191
    0
                    .unwrap();
    5192
    0
            let div_bounds = div_expr
    5193
    0
                .evaluate_bounds(&[
    5194
    0
                    &Interval::make(Some(10), Some(20)).unwrap(),
    5195
    0
                    &Interval::make(Some(2), Some(5)).unwrap(),
    5196
    0
                ])
    5197
    0
                .unwrap();
    5198
    0
            assert_eq!(div_bounds, Interval::make(Some(2), Some(10)).unwrap());
    5199
    0
        }
    5200
    5201
        #[test]
    5202
    0
        fn test_evaluate_bounds_bool() {
    5203
    0
            let schema = Schema::new(vec![
    5204
    0
                Field::new("a", DataType::Boolean, false),
    5205
    0
                Field::new("b", DataType::Boolean, false),
    5206
            ]);
    5207
    5208
    0
            let a = Arc::new(Column::new("a", 0)) as _;
    5209
    0
            let b = Arc::new(Column::new("b", 1)) as _;
    5210
    5211
            // Test OR bounds
    5212
    0
            let or_expr =
    5213
    0
                binary_expr(Arc::clone(&a), Operator::Or, Arc::clone(&b), &schema).unwrap();
    5214
    0
            let or_bounds = or_expr
    5215
    0
                .evaluate_bounds(&[
    5216
    0
                    &Interval::make(Some(true), Some(true)).unwrap(),
    5217
    0
                    &Interval::make(Some(false), Some(false)).unwrap(),
    5218
    0
                ])
    5219
    0
                .unwrap();
    5220
    0
            assert_eq!(or_bounds, Interval::make(Some(true), Some(true)).unwrap());
    5221
    5222
            // Test AND bounds
    5223
    0
            let and_expr =
    5224
    0
                binary_expr(Arc::clone(&a), Operator::And, Arc::clone(&b), &schema).unwrap();
    5225
    0
            let and_bounds = and_expr
    5226
    0
                .evaluate_bounds(&[
    5227
    0
                    &Interval::make(Some(true), Some(true)).unwrap(),
    5228
    0
                    &Interval::make(Some(false), Some(false)).unwrap(),
    5229
    0
                ])
    5230
    0
                .unwrap();
    5231
    0
            assert_eq!(
    5232
                and_bounds,
    5233
    0
                Interval::make(Some(false), Some(false)).unwrap()
    5234
            );
    5235
    0
        }
    5236
    5237
        #[test]
    5238
    0
        fn test_evaluate_nested_type() {
    5239
    0
            let batch_schema = Arc::new(Schema::new(vec![
    5240
    0
                Field::new(
    5241
                    "a",
    5242
    0
                    DataType::List(Arc::new(Field::new_list_field(DataType::Int32, true))),
    5243
                    true,
    5244
                ),
    5245
    0
                Field::new(
    5246
                    "b",
    5247
    0
                    DataType::List(Arc::new(Field::new_list_field(DataType::Int32, true))),
    5248
                    true,
    5249
                ),
    5250
            ]));
    5251
    5252
    0
            let mut list_builder_a = ListBuilder::new(Int32Builder::new());
    5253
    5254
    0
            list_builder_a.append_value([Some(1)]);
    5255
    0
            list_builder_a.append_value([Some(2)]);
    5256
    0
            list_builder_a.append_value([]);
    5257
    0
            list_builder_a.append_value([None]);
    5258
    5259
    0
            let list_array_a: ArrayRef = Arc::new(list_builder_a.finish());
    5260
    5261
    0
            let mut list_builder_b = ListBuilder::new(Int32Builder::new());
    5262
    5263
    0
            list_builder_b.append_value([Some(1)]);
    5264
    0
            list_builder_b.append_value([Some(2)]);
    5265
    0
            list_builder_b.append_value([]);
    5266
    0
            list_builder_b.append_value([None]);
    5267
    5268
    0
            let list_array_b: ArrayRef = Arc::new(list_builder_b.finish());
    5269
    5270
    0
            let batch =
    5271
    0
                RecordBatch::try_new(batch_schema, vec![list_array_a, list_array_b]).unwrap();
    5272
    5273
    0
            let schema = Arc::new(Schema::new(vec![
    5274
    0
                Field::new(
    5275
                    "a",
    5276
    0
                    DataType::List(Arc::new(Field::new("foo", DataType::Int32, true))),
    5277
                    true,
    5278
                ),
    5279
    0
                Field::new(
    5280
                    "b",
    5281
    0
                    DataType::List(Arc::new(Field::new("bar", DataType::Int32, true))),
    5282
                    true,
    5283
                ),
    5284
            ]));
    5285
    5286
    0
            let a = Arc::new(Column::new("a", 0)) as _;
    5287
    0
            let b = Arc::new(Column::new("b", 1)) as _;
    5288
    5289
    0
            let eq_expr =
    5290
    0
                binary_expr(Arc::clone(&a), Operator::Eq, Arc::clone(&b), &schema).unwrap();
    5291
    5292
    0
            let eq_result = eq_expr.evaluate(&batch).unwrap();
    5293
    0
            let expected =
    5294
    0
                BooleanArray::from_iter(vec![Some(true), Some(true), Some(true), Some(true)]);
    5295
    0
            assert_eq!(eq_result.into_array(4).unwrap().as_boolean(), &expected);
    5296
    0
        }
    5297
    }