Skip to content

Commit 81d7f88

Browse files
authored
Minor: Add doc comments and example for ScalarVaue::to_scalar (#7491)
1 parent dfd6851 commit 81d7f88

File tree

1 file changed

+29
-2
lines changed

1 file changed

+29
-2
lines changed

datafusion/common/src/scalar.rs

Lines changed: 29 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1321,7 +1321,34 @@ impl ScalarValue {
13211321
self.to_array_of_size(1)
13221322
}
13231323

1324-
/// Converts a scalar into an arrow [`Scalar`]
1324+
/// Converts a scalar into an arrow [`Scalar`] (which implements
1325+
/// the [`Datum`] interface).
1326+
///
1327+
/// This can be used to call arrow compute kernels such as `lt`
1328+
///
1329+
/// # Example
1330+
/// ```
1331+
/// use datafusion_common::ScalarValue;
1332+
/// use arrow::array::{BooleanArray, Int32Array};
1333+
///
1334+
/// let arr = Int32Array::from(vec![Some(1), None, Some(10)]);
1335+
/// let five = ScalarValue::Int32(Some(5));
1336+
///
1337+
/// let result = arrow::compute::kernels::cmp::lt(
1338+
/// &arr,
1339+
/// &five.to_scalar(),
1340+
/// ).unwrap();
1341+
///
1342+
/// let expected = BooleanArray::from(vec![
1343+
/// Some(true),
1344+
/// None,
1345+
/// Some(false)
1346+
/// ]
1347+
/// );
1348+
///
1349+
/// assert_eq!(&result, &expected);
1350+
/// ```
1351+
/// [`Datum`]: arrow_array::Datum
13251352
pub fn to_scalar(&self) -> Scalar<ArrayRef> {
13261353
Scalar::new(self.to_array_of_size(1))
13271354
}
@@ -1332,7 +1359,7 @@ impl ScalarValue {
13321359
/// Returns an error if the iterator is empty or if the
13331360
/// [`ScalarValue`]s are not all the same type
13341361
///
1335-
/// Example
1362+
/// # Example
13361363
/// ```
13371364
/// use datafusion_common::ScalarValue;
13381365
/// use arrow::array::{ArrayRef, BooleanArray};

0 commit comments

Comments
 (0)