polars_core/scalar/
mod.rs

1mod from;
2mod new;
3pub mod reduce;
4#[cfg(any(feature = "serde", feature = "dsl-schema"))]
5mod serde;
6
7use std::hash::Hash;
8
9use polars_error::PolarsResult;
10use polars_utils::IdxSize;
11use polars_utils::pl_str::PlSmallStr;
12
13use crate::chunked_array::cast::CastOptions;
14use crate::datatypes::{AnyValue, DataType};
15use crate::prelude::{Column, Series};
16
17#[derive(Clone, Debug, PartialEq)]
18pub struct Scalar {
19    dtype: DataType,
20    value: AnyValue<'static>,
21}
22
23impl Hash for Scalar {
24    fn hash<H: std::hash::Hasher>(&self, state: &mut H) {
25        self.dtype.hash(state);
26        self.value.hash_impl(state, true);
27    }
28}
29
30impl Default for Scalar {
31    fn default() -> Self {
32        Self {
33            dtype: DataType::Null,
34            value: AnyValue::Null,
35        }
36    }
37}
38
39impl Scalar {
40    #[inline(always)]
41    pub const fn new(dtype: DataType, value: AnyValue<'static>) -> Self {
42        Self { dtype, value }
43    }
44
45    pub const fn null(dtype: DataType) -> Self {
46        Self::new(dtype, AnyValue::Null)
47    }
48
49    pub fn new_idxsize(value: IdxSize) -> Self {
50        value.into()
51    }
52
53    pub fn cast_with_options(self, dtype: &DataType, options: CastOptions) -> PolarsResult<Self> {
54        if self.dtype() == dtype {
55            return Ok(self);
56        }
57
58        // @Optimize: If we have fully fleshed out casting semantics, we could just specify the
59        // cast on AnyValue.
60        let s = self
61            .into_series(PlSmallStr::from_static("scalar"))
62            .cast_with_options(dtype, options)?;
63        let value = s.get(0).unwrap();
64        Ok(Self::new(s.dtype().clone(), value.into_static()))
65    }
66
67    #[inline(always)]
68    pub fn is_null(&self) -> bool {
69        self.value.is_null()
70    }
71
72    #[inline(always)]
73    pub fn is_nan(&self) -> bool {
74        self.value.is_nan()
75    }
76
77    #[inline(always)]
78    pub fn into_value(self) -> AnyValue<'static> {
79        self.value
80    }
81
82    #[inline(always)]
83    pub fn value(&self) -> &AnyValue<'static> {
84        &self.value
85    }
86
87    pub fn as_any_value(&self) -> AnyValue<'_> {
88        self.value
89            .strict_cast(&self.dtype)
90            .unwrap_or_else(|| self.value.clone())
91    }
92
93    pub fn into_series(self, name: PlSmallStr) -> Series {
94        Series::from_any_values_and_dtype(name, &[self.as_any_value()], &self.dtype, true).unwrap()
95    }
96
97    /// Turn a scalar into a column with `length=1`.
98    pub fn into_column(self, name: PlSmallStr) -> Column {
99        Column::new_scalar(name, self, 1)
100    }
101
102    #[inline(always)]
103    pub fn dtype(&self) -> &DataType {
104        &self.dtype
105    }
106
107    #[inline(always)]
108    pub fn update(&mut self, value: AnyValue<'static>) {
109        self.value = value;
110    }
111
112    #[inline(always)]
113    pub fn with_value(mut self, value: AnyValue<'static>) -> Self {
114        self.update(value);
115        self
116    }
117
118    #[inline(always)]
119    pub fn any_value_mut(&mut self) -> &mut AnyValue<'static> {
120        &mut self.value
121    }
122
123    pub fn to_physical(mut self) -> Scalar {
124        self.dtype = self.dtype.to_physical();
125        self.value = self.value.to_physical();
126        self
127    }
128}