datafusion_python/expr/
aggregate_expr.rs1use std::fmt::{Display, Formatter};
19
20use datafusion::logical_expr::expr::AggregateFunction;
21use pyo3::prelude::*;
22
23use crate::expr::PyExpr;
24
25#[pyclass(
26 frozen,
27 name = "AggregateFunction",
28 module = "datafusion.expr",
29 subclass
30)]
31#[derive(Clone)]
32pub struct PyAggregateFunction {
33 aggr: AggregateFunction,
34}
35
36impl From<PyAggregateFunction> for AggregateFunction {
37 fn from(aggr: PyAggregateFunction) -> Self {
38 aggr.aggr
39 }
40}
41
42impl From<AggregateFunction> for PyAggregateFunction {
43 fn from(aggr: AggregateFunction) -> PyAggregateFunction {
44 PyAggregateFunction { aggr }
45 }
46}
47
48impl Display for PyAggregateFunction {
49 fn fmt(&self, f: &mut Formatter) -> std::fmt::Result {
50 let args: Vec<String> = self
51 .aggr
52 .params
53 .args
54 .iter()
55 .map(|expr| expr.to_string())
56 .collect();
57 write!(f, "{}({})", self.aggr.func.name(), args.join(", "))
58 }
59}
60
61#[pymethods]
62impl PyAggregateFunction {
63 fn aggregate_type(&self) -> String {
65 self.aggr.func.name().to_string()
66 }
67
68 fn is_distinct(&self) -> bool {
70 self.aggr.params.distinct
71 }
72
73 fn args(&self) -> Vec<PyExpr> {
75 self.aggr
76 .params
77 .args
78 .iter()
79 .map(|expr| PyExpr::from(expr.clone()))
80 .collect()
81 }
82
83 fn __repr__(&self) -> String {
85 format!("{self}")
86 }
87}