Skip to content

Commit

Permalink
Minor: Add doc comments and example for ScalarVaue::to_scalar
Browse files Browse the repository at this point in the history
  • Loading branch information
alamb committed Sep 7, 2023
1 parent dfd6851 commit 9a15bf8
Showing 1 changed file with 29 additions and 2 deletions.
31 changes: 29 additions & 2 deletions datafusion/common/src/scalar.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1321,7 +1321,34 @@ impl ScalarValue {
self.to_array_of_size(1)
}

/// Converts a scalar into an arrow [`Scalar`]
/// Converts a scalar into an arrow [`Scalar`] (which implements
/// the [`Datum`] interface).
///
/// This can be used to call arrow compute kernels such as `lt`
///
/// # Example
/// ```
/// use datafusion_common::ScalarValue;
/// use arrow::array::{BooleanArray, Int32Array};
///
/// let arr = Int32Array::from(vec![Some(1), None, Some(10)]);
/// let five = ScalarValue::Int32(Some(5));
///
/// let result = arrow::compute::kernels::cmp::lt(
/// &arr,
/// &five.to_scalar(),
/// ).unwrap();
///
/// let expected = BooleanArray::from(vec![
/// Some(true),
/// None,
/// Some(false)
/// ]
/// );
///
/// assert_eq!(&result, &expected);
/// ```
/// [`Datum`]: arrow_array::Datum
pub fn to_scalar(&self) -> Scalar<ArrayRef> {
Scalar::new(self.to_array_of_size(1))
}
Expand All @@ -1332,7 +1359,7 @@ impl ScalarValue {
/// Returns an error if the iterator is empty or if the
/// [`ScalarValue`]s are not all the same type
///
/// Example
/// # Example
/// ```
/// use datafusion_common::ScalarValue;
/// use arrow::array::{ArrayRef, BooleanArray};
Expand Down

0 comments on commit 9a15bf8

Please sign in to comment.