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