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