pub enum ScalarValue {
Show 46 variants
Null,
Boolean(Option<bool>),
Float16(Option<f16>),
Float32(Option<f32>),
Float64(Option<f64>),
Decimal128(Option<i128>, u8, i8),
Decimal256(Option<i256>, u8, i8),
Int8(Option<i8>),
Int16(Option<i16>),
Int32(Option<i32>),
Int64(Option<i64>),
UInt8(Option<u8>),
UInt16(Option<u16>),
UInt32(Option<u32>),
UInt64(Option<u64>),
Utf8(Option<String>),
Utf8View(Option<String>),
LargeUtf8(Option<String>),
Binary(Option<Vec<u8>>),
BinaryView(Option<Vec<u8>>),
FixedSizeBinary(i32, Option<Vec<u8>>),
LargeBinary(Option<Vec<u8>>),
FixedSizeList(Arc<FixedSizeListArray>),
List(Arc<ListArray>),
LargeList(Arc<LargeListArray>),
Struct(Arc<StructArray>),
Map(Arc<MapArray>),
Date32(Option<i32>),
Date64(Option<i64>),
Time32Second(Option<i32>),
Time32Millisecond(Option<i32>),
Time64Microsecond(Option<i64>),
Time64Nanosecond(Option<i64>),
TimestampSecond(Option<i64>, Option<Arc<str>>),
TimestampMillisecond(Option<i64>, Option<Arc<str>>),
TimestampMicrosecond(Option<i64>, Option<Arc<str>>),
TimestampNanosecond(Option<i64>, Option<Arc<str>>),
IntervalYearMonth(Option<i32>),
IntervalDayTime(Option<IntervalDayTime>),
IntervalMonthDayNano(Option<IntervalMonthDayNano>),
DurationSecond(Option<i64>),
DurationMillisecond(Option<i64>),
DurationMicrosecond(Option<i64>),
DurationNanosecond(Option<i64>),
Union(Option<(i8, Box<ScalarValue>)>, UnionFields, UnionMode),
Dictionary(Box<DataType>, Box<ScalarValue>),
}
Expand description
A dynamically typed, nullable single value.
While an arrow Array
) stores one or more values of the same type, in a
single column, a ScalarValue
stores a single value of a single type, the
equivalent of 1 row and one column.
┌────────┐
│ value1 │
│ value2 │ ┌────────┐
│ value3 │ │ value2 │
│ ... │ └────────┘
│ valueN │
└────────┘
Array ScalarValue
stores multiple, stores a single,
possibly null, values of possible null, value
the same type
§Performance
In general, performance will be better using arrow Array
s rather than
ScalarValue
, as it is far more efficient to process multiple values at
once (vectorized processing).
§Example
// Create single scalar value for an Int32 value
let s1 = ScalarValue::Int32(Some(10));
// You can also create values using the From impl:
let s2 = ScalarValue::from(10i32);
assert_eq!(s1, s2);
§Null Handling
ScalarValue
represents null values in the same way as Arrow. Nulls are
“typed” in the sense that a null value in an Int32Array
is different
from a null value in a Float64Array
, and is different from the values in
a NullArray
.
// You can create a 'null' Int32 value directly:
let s1 = ScalarValue::Int32(None);
// You can also create a null value for a given datatype:
let s2 = ScalarValue::try_from(&DataType::Int32)?;
assert_eq!(s1, s2);
// Note that this is DIFFERENT than a `ScalarValue::Null`
let s3 = ScalarValue::Null;
assert_ne!(s1, s3);
§Nested Types
List
/ LargeList
/ FixedSizeList
/ Struct
/ Map
are represented as a
single element array of the corresponding type.
§Example: Creating ScalarValue::Struct
using ScalarStructBuilder
// Build a struct like: {a: 1, b: "foo"}
let field_a = Field::new("a", DataType::Int32, false);
let field_b = Field::new("b", DataType::Utf8, false);
let s1 = ScalarStructBuilder::new()
.with_scalar(field_a, ScalarValue::from(1i32))
.with_scalar(field_b, ScalarValue::from("foo"))
.build();
§Example: Creating a null ScalarValue::Struct
using ScalarStructBuilder
// Build a struct representing a NULL value
let fields = vec![
Field::new("a", DataType::Int32, false),
Field::new("b", DataType::Utf8, false),
];
let s1 = ScalarStructBuilder::new_null(fields);
§Example: Creating ScalarValue::Struct
directly
// Build a struct like: {a: 1, b: "foo"}
// Field description
let fields = Fields::from(vec![
Field::new("a", DataType::Int32, false),
Field::new("b", DataType::Utf8, false),
]);
// one row arrays for each field
let arrays: Vec<ArrayRef> = vec![
Arc::new(Int32Array::from(vec![1])),
Arc::new(StringArray::from(vec!["foo"])),
];
// no nulls for this array
let nulls = None;
let arr = StructArray::new(fields, arrays, nulls);
// Create a ScalarValue::Struct directly
let s1 = ScalarValue::Struct(Arc::new(arr));
§Further Reading
See datatypes for details on datatypes and the format for the definitive reference.
Variants§
Null
represents DataType::Null
(castable to/from any other type)
Boolean(Option<bool>)
true or false value
Float16(Option<f16>)
16bit float
Float32(Option<f32>)
32bit float
Float64(Option<f64>)
64bit float
Decimal128(Option<i128>, u8, i8)
128bit decimal, using the i128 to represent the decimal, precision scale
Decimal256(Option<i256>, u8, i8)
256bit decimal, using the i256 to represent the decimal, precision scale
Int8(Option<i8>)
signed 8bit int
Int16(Option<i16>)
signed 16bit int
Int32(Option<i32>)
signed 32bit int
Int64(Option<i64>)
signed 64bit int
UInt8(Option<u8>)
unsigned 8bit int
UInt16(Option<u16>)
unsigned 16bit int
UInt32(Option<u32>)
unsigned 32bit int
UInt64(Option<u64>)
unsigned 64bit int
Utf8(Option<String>)
utf-8 encoded string.
Utf8View(Option<String>)
utf-8 encoded string but from view types.
LargeUtf8(Option<String>)
utf-8 encoded string representing a LargeString’s arrow type.
Binary(Option<Vec<u8>>)
binary
BinaryView(Option<Vec<u8>>)
binary but from view types.
FixedSizeBinary(i32, Option<Vec<u8>>)
fixed size binary
LargeBinary(Option<Vec<u8>>)
large binary
FixedSizeList(Arc<FixedSizeListArray>)
Fixed size list scalar.
The array must be a FixedSizeListArray with length 1.
List(Arc<ListArray>)
Represents a single element of a ListArray
as an ArrayRef
The array must be a ListArray with length 1.
LargeList(Arc<LargeListArray>)
The array must be a LargeListArray with length 1.
Struct(Arc<StructArray>)
Represents a single element StructArray
as an ArrayRef
. See
ScalarValue
for examples of how to create instances of this type.
Map(Arc<MapArray>)
Date32(Option<i32>)
Date stored as a signed 32bit int days since UNIX epoch 1970-01-01
Date64(Option<i64>)
Date stored as a signed 64bit int milliseconds since UNIX epoch 1970-01-01
Time32Second(Option<i32>)
Time stored as a signed 32bit int as seconds since midnight
Time32Millisecond(Option<i32>)
Time stored as a signed 32bit int as milliseconds since midnight
Time64Microsecond(Option<i64>)
Time stored as a signed 64bit int as microseconds since midnight
Time64Nanosecond(Option<i64>)
Time stored as a signed 64bit int as nanoseconds since midnight
TimestampSecond(Option<i64>, Option<Arc<str>>)
Timestamp Second
TimestampMillisecond(Option<i64>, Option<Arc<str>>)
Timestamp Milliseconds
TimestampMicrosecond(Option<i64>, Option<Arc<str>>)
Timestamp Microseconds
TimestampNanosecond(Option<i64>, Option<Arc<str>>)
Timestamp Nanoseconds
IntervalYearMonth(Option<i32>)
Number of elapsed whole months
IntervalDayTime(Option<IntervalDayTime>)
Number of elapsed days and milliseconds (no leap seconds) stored as 2 contiguous 32-bit signed integers
IntervalMonthDayNano(Option<IntervalMonthDayNano>)
A triple of the number of elapsed months, days, and nanoseconds. Months and days are encoded as 32-bit signed integers. Nanoseconds is encoded as a 64-bit signed integer (no leap seconds).
DurationSecond(Option<i64>)
Duration in seconds
DurationMillisecond(Option<i64>)
Duration in milliseconds
DurationMicrosecond(Option<i64>)
Duration in microseconds
DurationNanosecond(Option<i64>)
Duration in nanoseconds
Union(Option<(i8, Box<ScalarValue>)>, UnionFields, UnionMode)
A nested datatype that can represent slots of differing types. Components:
.0
: a tuple of union type_id
and the single value held by this Scalar
.1
: the list of fields, zero-to-one of which will by set in .0
.2
: the physical storage of the source/destination UnionArray from which this Scalar came
Dictionary(Box<DataType>, Box<ScalarValue>)
Dictionary type: index type and value
Implementations§
Source§impl ScalarValue
impl ScalarValue
Sourcepub fn new_primitive<T: ArrowPrimitiveType>(
a: Option<T::Native>,
d: &DataType,
) -> Result<Self>
pub fn new_primitive<T: ArrowPrimitiveType>( a: Option<T::Native>, d: &DataType, ) -> Result<Self>
Create a Result<ScalarValue>
with the provided value and datatype
§Panics
Panics if d is not compatible with T
Sourcepub fn try_new_decimal128(value: i128, precision: u8, scale: i8) -> Result<Self>
pub fn try_new_decimal128(value: i128, precision: u8, scale: i8) -> Result<Self>
Create a decimal Scalar from value/precision and scale.
Sourcepub fn new_utf8(val: impl Into<String>) -> Self
pub fn new_utf8(val: impl Into<String>) -> Self
Returns a ScalarValue::Utf8
representing val
Sourcepub fn new_utf8view(val: impl Into<String>) -> Self
pub fn new_utf8view(val: impl Into<String>) -> Self
Returns a ScalarValue::Utf8View
representing val
Sourcepub fn new_interval_ym(years: i32, months: i32) -> Self
pub fn new_interval_ym(years: i32, months: i32) -> Self
Returns a ScalarValue::IntervalYearMonth
representing
years
years and months
months
Sourcepub fn new_interval_dt(days: i32, millis: i32) -> Self
pub fn new_interval_dt(days: i32, millis: i32) -> Self
Returns a ScalarValue::IntervalDayTime
representing
days
days and millis
milliseconds
Sourcepub fn new_interval_mdn(months: i32, days: i32, nanos: i64) -> Self
pub fn new_interval_mdn(months: i32, days: i32, nanos: i64) -> Self
Returns a ScalarValue::IntervalMonthDayNano
representing
months
months and days
days, and nanos
nanoseconds
Sourcepub fn new_timestamp<T: ArrowTimestampType>(
value: Option<i64>,
tz_opt: Option<Arc<str>>,
) -> Self
pub fn new_timestamp<T: ArrowTimestampType>( value: Option<i64>, tz_opt: Option<Arc<str>>, ) -> Self
Returns a ScalarValue
representing
value
and tz_opt
timezone
Sourcepub fn new_pi(datatype: &DataType) -> Result<ScalarValue>
pub fn new_pi(datatype: &DataType) -> Result<ScalarValue>
Returns a ScalarValue
representing PI
Sourcepub fn new_pi_upper(datatype: &DataType) -> Result<ScalarValue>
pub fn new_pi_upper(datatype: &DataType) -> Result<ScalarValue>
Returns a ScalarValue
representing PI’s upper bound
Sourcepub fn new_negative_pi_lower(datatype: &DataType) -> Result<ScalarValue>
pub fn new_negative_pi_lower(datatype: &DataType) -> Result<ScalarValue>
Returns a ScalarValue
representing -PI’s lower bound
Sourcepub fn new_frac_pi_2_upper(datatype: &DataType) -> Result<ScalarValue>
pub fn new_frac_pi_2_upper(datatype: &DataType) -> Result<ScalarValue>
Returns a ScalarValue
representing FRAC_PI_2’s upper bound
pub fn new_neg_frac_pi_2_lower(datatype: &DataType) -> Result<ScalarValue>
Sourcepub fn new_negative_pi(datatype: &DataType) -> Result<ScalarValue>
pub fn new_negative_pi(datatype: &DataType) -> Result<ScalarValue>
Returns a ScalarValue
representing -PI
Sourcepub fn new_frac_pi_2(datatype: &DataType) -> Result<ScalarValue>
pub fn new_frac_pi_2(datatype: &DataType) -> Result<ScalarValue>
Returns a ScalarValue
representing PI/2
Sourcepub fn new_neg_frac_pi_2(datatype: &DataType) -> Result<ScalarValue>
pub fn new_neg_frac_pi_2(datatype: &DataType) -> Result<ScalarValue>
Returns a ScalarValue
representing -PI/2
Sourcepub fn new_infinity(datatype: &DataType) -> Result<ScalarValue>
pub fn new_infinity(datatype: &DataType) -> Result<ScalarValue>
Returns a ScalarValue
representing infinity
Sourcepub fn new_neg_infinity(datatype: &DataType) -> Result<ScalarValue>
pub fn new_neg_infinity(datatype: &DataType) -> Result<ScalarValue>
Returns a ScalarValue
representing negative infinity
Sourcepub fn new_zero(datatype: &DataType) -> Result<ScalarValue>
pub fn new_zero(datatype: &DataType) -> Result<ScalarValue>
Create a zero value in the given type.
Sourcepub fn new_one(datatype: &DataType) -> Result<ScalarValue>
pub fn new_one(datatype: &DataType) -> Result<ScalarValue>
Create an one value in the given type.
Sourcepub fn new_negative_one(datatype: &DataType) -> Result<ScalarValue>
pub fn new_negative_one(datatype: &DataType) -> Result<ScalarValue>
Create a negative one value in the given type.
pub fn new_ten(datatype: &DataType) -> Result<ScalarValue>
Sourcepub fn arithmetic_negate(&self) -> Result<Self>
pub fn arithmetic_negate(&self) -> Result<Self>
Calculate arithmetic negation for a scalar value
Sourcepub fn add<T: Borrow<ScalarValue>>(&self, other: T) -> Result<ScalarValue>
pub fn add<T: Borrow<ScalarValue>>(&self, other: T) -> Result<ScalarValue>
Wrapping addition of ScalarValue
NB: operating on ScalarValue
directly is not efficient, performance sensitive code
should operate on Arrays directly, using vectorized array kernels
Sourcepub fn add_checked<T: Borrow<ScalarValue>>(
&self,
other: T,
) -> Result<ScalarValue>
pub fn add_checked<T: Borrow<ScalarValue>>( &self, other: T, ) -> Result<ScalarValue>
Checked addition of ScalarValue
NB: operating on ScalarValue
directly is not efficient, performance sensitive code
should operate on Arrays directly, using vectorized array kernels
Sourcepub fn sub<T: Borrow<ScalarValue>>(&self, other: T) -> Result<ScalarValue>
pub fn sub<T: Borrow<ScalarValue>>(&self, other: T) -> Result<ScalarValue>
Wrapping subtraction of ScalarValue
NB: operating on ScalarValue
directly is not efficient, performance sensitive code
should operate on Arrays directly, using vectorized array kernels
Sourcepub fn sub_checked<T: Borrow<ScalarValue>>(
&self,
other: T,
) -> Result<ScalarValue>
pub fn sub_checked<T: Borrow<ScalarValue>>( &self, other: T, ) -> Result<ScalarValue>
Checked subtraction of ScalarValue
NB: operating on ScalarValue
directly is not efficient, performance sensitive code
should operate on Arrays directly, using vectorized array kernels
Sourcepub fn mul<T: Borrow<ScalarValue>>(&self, other: T) -> Result<ScalarValue>
pub fn mul<T: Borrow<ScalarValue>>(&self, other: T) -> Result<ScalarValue>
Wrapping multiplication of ScalarValue
NB: operating on ScalarValue
directly is not efficient, performance sensitive code
should operate on Arrays directly, using vectorized array kernels.
Sourcepub fn mul_checked<T: Borrow<ScalarValue>>(
&self,
other: T,
) -> Result<ScalarValue>
pub fn mul_checked<T: Borrow<ScalarValue>>( &self, other: T, ) -> Result<ScalarValue>
Checked multiplication of ScalarValue
NB: operating on ScalarValue
directly is not efficient, performance sensitive code
should operate on Arrays directly, using vectorized array kernels.
Sourcepub fn div<T: Borrow<ScalarValue>>(&self, other: T) -> Result<ScalarValue>
pub fn div<T: Borrow<ScalarValue>>(&self, other: T) -> Result<ScalarValue>
Performs lhs / rhs
Overflow or division by zero will result in an error, with exception to floating point numbers, which instead follow the IEEE 754 rules.
NB: operating on ScalarValue
directly is not efficient, performance sensitive code
should operate on Arrays directly, using vectorized array kernels.
Sourcepub fn rem<T: Borrow<ScalarValue>>(&self, other: T) -> Result<ScalarValue>
pub fn rem<T: Borrow<ScalarValue>>(&self, other: T) -> Result<ScalarValue>
Performs lhs % rhs
Overflow or division by zero will result in an error, with exception to floating point numbers, which instead follow the IEEE 754 rules.
NB: operating on ScalarValue
directly is not efficient, performance sensitive code
should operate on Arrays directly, using vectorized array kernels.
pub fn is_unsigned(&self) -> bool
Sourcepub fn distance(&self, other: &ScalarValue) -> Option<usize>
pub fn distance(&self, other: &ScalarValue) -> Option<usize>
Absolute distance between two numeric values (of the same type). This method will return
None if either one of the arguments are null. It might also return None if the resulting
distance is greater than usize::MAX
. If the type is a float, then the distance will be
rounded to the nearest integer.
Note: the datatype itself must support subtraction.
Sourcepub fn to_array(&self) -> Result<ArrayRef>
pub fn to_array(&self) -> Result<ArrayRef>
Converts a scalar value into an 1-row array.
§Errors
Errors if the ScalarValue cannot be converted into a 1-row array
Sourcepub fn to_scalar(&self) -> Result<Scalar<ArrayRef>>
pub fn to_scalar(&self) -> Result<Scalar<ArrayRef>>
Converts a scalar into an arrow Scalar
(which implements
the Datum
interface).
This can be used to call arrow compute kernels such as lt
§Errors
Errors if the ScalarValue cannot be converted into a 1-row array
§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(),
).unwrap();
let expected = BooleanArray::from(vec![
Some(true),
None,
Some(false)
]
);
assert_eq!(&result, &expected);
Sourcepub fn iter_to_array(
scalars: impl IntoIterator<Item = ScalarValue>,
) -> Result<ArrayRef>
pub fn iter_to_array( scalars: impl IntoIterator<Item = ScalarValue>, ) -> Result<ArrayRef>
Converts an iterator of references ScalarValue
into an ArrayRef
corresponding to those values. For example, an iterator of
ScalarValue::Int32
would be converted to an Int32Array
.
Returns an error if the iterator is empty or if the
ScalarValue
s are not all the same type
§Panics
Panics if self
is a dictionary with invalid key type
§Example
use datafusion_common::ScalarValue;
use arrow::array::{ArrayRef, BooleanArray};
let scalars = vec![
ScalarValue::Boolean(Some(true)),
ScalarValue::Boolean(None),
ScalarValue::Boolean(Some(false)),
];
// Build an Array from the list of ScalarValues
let array = ScalarValue::iter_to_array(scalars.into_iter())
.unwrap();
let expected: ArrayRef = std::sync::Arc::new(
BooleanArray::from(vec![
Some(true),
None,
Some(false)
]
));
assert_eq!(&array, &expected);
Sourcepub fn new_list(
values: &[ScalarValue],
data_type: &DataType,
nullable: bool,
) -> Arc<ListArray>
pub fn new_list( values: &[ScalarValue], data_type: &DataType, nullable: bool, ) -> Arc<ListArray>
Converts Vec<ScalarValue>
where each element has type corresponding to
data_type
, to a single element ListArray
.
Example
use datafusion_common::ScalarValue;
use arrow::array::{ListArray, Int32Array};
use arrow::datatypes::{DataType, Int32Type};
use datafusion_common::cast::as_list_array;
let scalars = vec![
ScalarValue::Int32(Some(1)),
ScalarValue::Int32(None),
ScalarValue::Int32(Some(2))
];
let result = ScalarValue::new_list(&scalars, &DataType::Int32, true);
let expected = ListArray::from_iter_primitive::<Int32Type, _, _>(
vec![
Some(vec![Some(1), None, Some(2)])
]);
assert_eq!(*result, expected);
Sourcepub fn new_list_nullable(
values: &[ScalarValue],
data_type: &DataType,
) -> Arc<ListArray>
pub fn new_list_nullable( values: &[ScalarValue], data_type: &DataType, ) -> Arc<ListArray>
Same as ScalarValue::new_list
but with nullable set to true.
Sourcepub fn new_null_list(
data_type: DataType,
nullable: bool,
null_len: usize,
) -> Self
pub fn new_null_list( data_type: DataType, nullable: bool, null_len: usize, ) -> Self
Create ListArray with Null with specific data type
- new_null_list(i32, nullable, 1):
ListArray[NULL]
Sourcepub fn new_list_from_iter(
values: impl IntoIterator<Item = ScalarValue> + ExactSizeIterator,
data_type: &DataType,
nullable: bool,
) -> Arc<ListArray>
pub fn new_list_from_iter( values: impl IntoIterator<Item = ScalarValue> + ExactSizeIterator, data_type: &DataType, nullable: bool, ) -> Arc<ListArray>
Converts IntoIterator<Item = ScalarValue>
where each element has type corresponding to
data_type
, to a ListArray
.
Example
use datafusion_common::ScalarValue;
use arrow::array::{ListArray, Int32Array};
use arrow::datatypes::{DataType, Int32Type};
use datafusion_common::cast::as_list_array;
let scalars = vec![
ScalarValue::Int32(Some(1)),
ScalarValue::Int32(None),
ScalarValue::Int32(Some(2))
];
let result = ScalarValue::new_list_from_iter(scalars.into_iter(), &DataType::Int32, true);
let expected = ListArray::from_iter_primitive::<Int32Type, _, _>(
vec![
Some(vec![Some(1), None, Some(2)])
]);
assert_eq!(*result, expected);
Sourcepub fn new_large_list(
values: &[ScalarValue],
data_type: &DataType,
) -> Arc<LargeListArray>
pub fn new_large_list( values: &[ScalarValue], data_type: &DataType, ) -> Arc<LargeListArray>
Converts Vec<ScalarValue>
where each element has type corresponding to
data_type
, to a LargeListArray
.
Example
use datafusion_common::ScalarValue;
use arrow::array::{LargeListArray, Int32Array};
use arrow::datatypes::{DataType, Int32Type};
use datafusion_common::cast::as_large_list_array;
let scalars = vec![
ScalarValue::Int32(Some(1)),
ScalarValue::Int32(None),
ScalarValue::Int32(Some(2))
];
let result = ScalarValue::new_large_list(&scalars, &DataType::Int32);
let expected = LargeListArray::from_iter_primitive::<Int32Type, _, _>(
vec![
Some(vec![Some(1), None, Some(2)])
]);
assert_eq!(*result, expected);
Sourcepub fn to_array_of_size(&self, size: usize) -> Result<ArrayRef>
pub fn to_array_of_size(&self, size: usize) -> Result<ArrayRef>
Converts a scalar value into an array of size
rows.
§Errors
Errors if self
is
- a decimal that fails be converted to a decimal array of size
- a
Fixedsizelist
that fails to be concatenated into an array of size - a
List
that fails to be concatenated into an array of size - a
Dictionary
that fails be converted to a dictionary array of size
Sourcepub fn convert_array_to_scalar_vec(array: &dyn Array) -> Result<Vec<Vec<Self>>>
pub fn convert_array_to_scalar_vec(array: &dyn Array) -> Result<Vec<Vec<Self>>>
Retrieve ScalarValue for each row in array
Example 1: Array (ScalarValue::Int32)
use datafusion_common::ScalarValue;
use arrow::array::ListArray;
use arrow::datatypes::{DataType, Int32Type};
// Equivalent to [[1,2,3], [4,5]]
let list_arr = ListArray::from_iter_primitive::<Int32Type, _, _>(vec![
Some(vec![Some(1), Some(2), Some(3)]),
Some(vec![Some(4), Some(5)])
]);
// Convert the array into Scalar Values for each row
let scalar_vec = ScalarValue::convert_array_to_scalar_vec(&list_arr).unwrap();
let expected = vec![
vec![
ScalarValue::Int32(Some(1)),
ScalarValue::Int32(Some(2)),
ScalarValue::Int32(Some(3)),
],
vec![
ScalarValue::Int32(Some(4)),
ScalarValue::Int32(Some(5)),
],
];
assert_eq!(scalar_vec, expected);
Example 2: Nested array (ScalarValue::List)
use datafusion_common::ScalarValue;
use arrow::array::ListArray;
use arrow::datatypes::{DataType, Int32Type};
use datafusion_common::utils::array_into_list_array_nullable;
use std::sync::Arc;
let list_arr = ListArray::from_iter_primitive::<Int32Type, _, _>(vec![
Some(vec![Some(1), Some(2), Some(3)]),
Some(vec![Some(4), Some(5)])
]);
// Wrap into another layer of list, we got nested array as [ [[1,2,3], [4,5]] ]
let list_arr = array_into_list_array_nullable(Arc::new(list_arr));
// Convert the array into Scalar Values for each row, we got 1D arrays in this example
let scalar_vec = ScalarValue::convert_array_to_scalar_vec(&list_arr).unwrap();
let l1 = ListArray::from_iter_primitive::<Int32Type, _, _>(vec![
Some(vec![Some(1), Some(2), Some(3)]),
]);
let l2 = ListArray::from_iter_primitive::<Int32Type, _, _>(vec![
Some(vec![Some(4), Some(5)]),
]);
let expected = vec![
vec![
ScalarValue::List(Arc::new(l1)),
ScalarValue::List(Arc::new(l2)),
],
];
assert_eq!(scalar_vec, expected);
Sourcepub fn try_from_array(array: &dyn Array, index: usize) -> Result<Self>
pub fn try_from_array(array: &dyn Array, index: usize) -> Result<Self>
Converts a value in array
at index
into a ScalarValue
Sourcepub fn try_from_string(value: String, target_type: &DataType) -> Result<Self>
pub fn try_from_string(value: String, target_type: &DataType) -> Result<Self>
Try to parse value
into a ScalarValue of type target_type
Sourcepub fn cast_to(&self, target_type: &DataType) -> Result<Self>
pub fn cast_to(&self, target_type: &DataType) -> Result<Self>
Try to cast this value to a ScalarValue of type data_type
Sourcepub fn cast_to_with_options(
&self,
target_type: &DataType,
cast_options: &CastOptions<'static>,
) -> Result<Self>
pub fn cast_to_with_options( &self, target_type: &DataType, cast_options: &CastOptions<'static>, ) -> Result<Self>
Try to cast this value to a ScalarValue of type data_type
with CastOptions
Sourcepub fn eq_array(&self, array: &ArrayRef, index: usize) -> Result<bool>
pub fn eq_array(&self, array: &ArrayRef, index: usize) -> Result<bool>
Compares a single row of array @ index for equality with self, in an optimized fashion.
This method implements an optimized version of:
let arr_scalar = Self::try_from_array(array, index).unwrap();
arr_scalar.eq(self)
Performance note: the arrow compute kernels should be preferred over this function if at all possible as they can be vectorized and are generally much faster.
This function has a few narrow usescases such as hash table key comparisons where comparing a single row at a time is necessary.
§Errors
Errors if
- it fails to downcast
array
to the data type ofself
self
is aStruct
§Panics
Panics if self
is a dictionary with invalid key type
Sourcepub fn size(&self) -> usize
pub fn size(&self) -> usize
Estimate size if bytes including Self
. For values with internal containers such as String
includes the allocated size (capacity
) rather than the current length (len
)
Sourcepub fn size_of_vec(vec: &Vec<Self>) -> usize
pub fn size_of_vec(vec: &Vec<Self>) -> usize
Sourcepub fn size_of_vec_deque(vec_deque: &VecDeque<Self>) -> usize
pub fn size_of_vec_deque(vec_deque: &VecDeque<Self>) -> usize
Trait Implementations§
Source§impl Clone for ScalarValue
impl Clone for ScalarValue
Source§fn clone(&self) -> ScalarValue
fn clone(&self) -> ScalarValue
1.0.0 · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source
. Read moreSource§impl Debug for ScalarValue
impl Debug for ScalarValue
Source§impl Display for ScalarValue
impl Display for ScalarValue
Source§impl From<&str> for ScalarValue
impl From<&str> for ScalarValue
Source§impl From<String> for ScalarValue
impl From<String> for ScalarValue
Source§impl From<Vec<(&str, ScalarValue)>> for ScalarValue
impl From<Vec<(&str, ScalarValue)>> for ScalarValue
Wrapper to create ScalarValue::Struct for convenience
Source§impl From<bool> for ScalarValue
impl From<bool> for ScalarValue
Source§impl From<f32> for ScalarValue
impl From<f32> for ScalarValue
Source§impl From<f64> for ScalarValue
impl From<f64> for ScalarValue
Source§impl From<i16> for ScalarValue
impl From<i16> for ScalarValue
Source§impl From<i32> for ScalarValue
impl From<i32> for ScalarValue
Source§impl From<i64> for ScalarValue
impl From<i64> for ScalarValue
Source§impl From<i8> for ScalarValue
impl From<i8> for ScalarValue
Source§impl From<u16> for ScalarValue
impl From<u16> for ScalarValue
Source§impl From<u32> for ScalarValue
impl From<u32> for ScalarValue
Source§impl From<u64> for ScalarValue
impl From<u64> for ScalarValue
Source§impl From<u8> for ScalarValue
impl From<u8> for ScalarValue
Source§impl FromStr for ScalarValue
impl FromStr for ScalarValue
Source§impl Hash for ScalarValue
impl Hash for ScalarValue
Source§impl PartialEq for ScalarValue
impl PartialEq for ScalarValue
Source§impl PartialOrd for ScalarValue
impl PartialOrd for ScalarValue
Source§impl TryFrom<&DataType> for ScalarValue
impl TryFrom<&DataType> for ScalarValue
Source§impl TryFrom<DataType> for ScalarValue
impl TryFrom<DataType> for ScalarValue
Source§impl TryFrom<ScalarValue> for bool
impl TryFrom<ScalarValue> for bool
Source§type Error = DataFusionError
type Error = DataFusionError
Source§fn try_from(value: ScalarValue) -> Result<Self>
fn try_from(value: ScalarValue) -> Result<Self>
Source§impl TryFrom<ScalarValue> for f32
impl TryFrom<ScalarValue> for f32
Source§type Error = DataFusionError
type Error = DataFusionError
Source§fn try_from(value: ScalarValue) -> Result<Self>
fn try_from(value: ScalarValue) -> Result<Self>
Source§impl TryFrom<ScalarValue> for f64
impl TryFrom<ScalarValue> for f64
Source§type Error = DataFusionError
type Error = DataFusionError
Source§fn try_from(value: ScalarValue) -> Result<Self>
fn try_from(value: ScalarValue) -> Result<Self>
Source§impl TryFrom<ScalarValue> for i128
impl TryFrom<ScalarValue> for i128
Source§type Error = DataFusionError
type Error = DataFusionError
Source§fn try_from(value: ScalarValue) -> Result<Self>
fn try_from(value: ScalarValue) -> Result<Self>
Source§impl TryFrom<ScalarValue> for i16
impl TryFrom<ScalarValue> for i16
Source§type Error = DataFusionError
type Error = DataFusionError
Source§fn try_from(value: ScalarValue) -> Result<Self>
fn try_from(value: ScalarValue) -> Result<Self>
Source§impl TryFrom<ScalarValue> for i256
impl TryFrom<ScalarValue> for i256
Source§type Error = DataFusionError
type Error = DataFusionError
Source§fn try_from(value: ScalarValue) -> Result<Self>
fn try_from(value: ScalarValue) -> Result<Self>
Source§impl TryFrom<ScalarValue> for i32
impl TryFrom<ScalarValue> for i32
Source§type Error = DataFusionError
type Error = DataFusionError
Source§fn try_from(value: ScalarValue) -> Result<Self>
fn try_from(value: ScalarValue) -> Result<Self>
Source§impl TryFrom<ScalarValue> for i64
impl TryFrom<ScalarValue> for i64
Source§type Error = DataFusionError
type Error = DataFusionError
Source§fn try_from(value: ScalarValue) -> Result<Self>
fn try_from(value: ScalarValue) -> Result<Self>
Source§impl TryFrom<ScalarValue> for i8
impl TryFrom<ScalarValue> for i8
Source§type Error = DataFusionError
type Error = DataFusionError
Source§fn try_from(value: ScalarValue) -> Result<Self>
fn try_from(value: ScalarValue) -> Result<Self>
Source§impl TryFrom<ScalarValue> for u16
impl TryFrom<ScalarValue> for u16
Source§type Error = DataFusionError
type Error = DataFusionError
Source§fn try_from(value: ScalarValue) -> Result<Self>
fn try_from(value: ScalarValue) -> Result<Self>
Source§impl TryFrom<ScalarValue> for u32
impl TryFrom<ScalarValue> for u32
Source§type Error = DataFusionError
type Error = DataFusionError
Source§fn try_from(value: ScalarValue) -> Result<Self>
fn try_from(value: ScalarValue) -> Result<Self>
Source§impl TryFrom<ScalarValue> for u64
impl TryFrom<ScalarValue> for u64
Source§type Error = DataFusionError
type Error = DataFusionError
Source§fn try_from(value: ScalarValue) -> Result<Self>
fn try_from(value: ScalarValue) -> Result<Self>
Source§impl TryFrom<ScalarValue> for u8
impl TryFrom<ScalarValue> for u8
Source§type Error = DataFusionError
type Error = DataFusionError
Source§fn try_from(value: ScalarValue) -> Result<Self>
fn try_from(value: ScalarValue) -> Result<Self>
impl Eq for ScalarValue
Auto Trait Implementations§
impl Freeze for ScalarValue
impl !RefUnwindSafe for ScalarValue
impl Send for ScalarValue
impl Sync for ScalarValue
impl Unpin for ScalarValue
impl !UnwindSafe for ScalarValue
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
Source§unsafe fn clone_to_uninit(&self, dst: *mut T)
unsafe fn clone_to_uninit(&self, dst: *mut T)
clone_to_uninit
)Source§impl<Q, K> Equivalent<K> for Q
impl<Q, K> Equivalent<K> for Q
Source§impl<Q, K> Equivalent<K> for Q
impl<Q, K> Equivalent<K> for Q
Source§impl<Q, K> Equivalent<K> for Q
impl<Q, K> Equivalent<K> for Q
Source§fn equivalent(&self, key: &K) -> bool
fn equivalent(&self, key: &K) -> bool
key
and return true
if they are equal.