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