Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
pola-rs
GitHub Repository: pola-rs/polars
Path: blob/main/crates/polars-expr/src/expressions/element.rs
7884 views
1
use polars_core::prelude::*;
2
3
use super::*;
4
use crate::expressions::{AggregationContext, PhysicalExpr};
5
6
#[derive(Debug)]
7
pub struct ElementExpr {
8
output_field: Field,
9
}
10
11
impl ElementExpr {
12
pub fn new(output_field: Field) -> Self {
13
Self { output_field }
14
}
15
}
16
17
impl PhysicalExpr for ElementExpr {
18
fn as_expression(&self) -> Option<&Expr> {
19
Some(&Expr::Element)
20
}
21
22
fn evaluate(&self, _df: &DataFrame, state: &ExecutionState) -> PolarsResult<Column> {
23
let (flattened, _validity) = state.element.as_ref().clone().ok_or_else(
24
|| polars_err!(InvalidOperation: "`element` is not allowed in this context"),
25
)?;
26
Ok(flattened)
27
}
28
29
fn evaluate_on_groups<'a>(
30
&self,
31
df: &DataFrame,
32
groups: &'a GroupPositions,
33
state: &ExecutionState,
34
) -> PolarsResult<AggregationContext<'a>> {
35
let c = self.evaluate(df, state)?;
36
Ok(AggregationContext::new(c, Cow::Borrowed(groups), false))
37
}
38
39
fn to_field(&self, _input_schema: &Schema) -> PolarsResult<Field> {
40
Ok(self.output_field.clone())
41
}
42
43
fn is_scalar(&self) -> bool {
44
false
45
}
46
}
47
48