Skip to content

Commit

Permalink
revise some scalarValue fn implementation
Browse files Browse the repository at this point in the history
  • Loading branch information
WinkerDu committed Apr 29, 2022
1 parent 69d9051 commit 501add5
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 12 deletions.
18 changes: 7 additions & 11 deletions datafusion/common/src/scalar.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ use std::{convert::TryFrom, fmt, iter::repeat, sync::Arc};
/// This is the single-valued counter-part of arrow’s `Array`.
#[derive(Clone)]
pub enum ScalarValue {
/// represents null
/// represents `DataType::Null` (castable to/from any other type)
Null,
/// true or false value
Boolean(Option<bool>),
Expand Down Expand Up @@ -172,6 +172,7 @@ impl PartialEq for ScalarValue {
(IntervalMonthDayNano(_), _) => false,
(Struct(v1, t1), Struct(v2, t2)) => v1.eq(v2) && t1.eq(t2),
(Struct(_, _), _) => false,
(Null, Null) => true,
(Null, _) => false,
}
}
Expand Down Expand Up @@ -273,6 +274,7 @@ impl PartialOrd for ScalarValue {
}
}
(Struct(_, _), _) => None,
(Null, Null) => Some(Ordering::Equal),
(Null, _) => None,
}
}
Expand Down Expand Up @@ -844,10 +846,7 @@ impl ScalarValue {
ScalarValue::iter_to_decimal_array(scalars, precision, scale)?;
Arc::new(decimal_array)
}
DataType::Null => {
let null_array = ScalarValue::iter_to_null_array(scalars)?;
Arc::new(null_array)
}
DataType::Null => ScalarValue::iter_to_null_array(scalars),
DataType::Boolean => build_array_primitive!(BooleanArray, Boolean),
DataType::Float32 => build_array_primitive!(Float32Array, Float32),
DataType::Float64 => build_array_primitive!(Float64Array, Float64),
Expand Down Expand Up @@ -980,18 +979,15 @@ impl ScalarValue {
Ok(array)
}

fn iter_to_null_array(
scalars: impl IntoIterator<Item = ScalarValue>,
) -> Result<NullArray> {
fn iter_to_null_array(scalars: impl IntoIterator<Item = ScalarValue>) -> ArrayRef {
let length =
scalars
.into_iter()
.fold(0usize, |r, element: ScalarValue| match element {
ScalarValue::Null => r + 1,
_ => unreachable!(),
});
let array = NullArray::new(length);
Ok(array)
new_null_array(&DataType::Null, length)
}

fn iter_to_decimal_array(
Expand Down Expand Up @@ -1550,7 +1546,7 @@ impl ScalarValue {
eq_array_primitive!(array, index, IntervalMonthDayNanoArray, val)
}
ScalarValue::Struct(_, _) => unimplemented!(),
ScalarValue::Null => false,
ScalarValue::Null => array.data().is_null(index),
}
}

Expand Down
2 changes: 1 addition & 1 deletion datafusion/physical-expr/src/expressions/binary.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1278,7 +1278,7 @@ impl BinaryExpr {
Operator::NotEq => neq_dyn(&left, &right),
Operator::IsDistinctFrom => {
match (left_data_type, right_data_type) {
// exchange lhs and rhs to when lhs is Null since `binary_array_op` is
// exchange lhs and rhs when lhs is Null, since `binary_array_op` is
// always try to down cast array according to $LEFT expression.
(DataType::Null, _) => {
binary_array_op!(right, left, is_distinct_from)
Expand Down

0 comments on commit 501add5

Please sign in to comment.