This repository has been archived by the owner on Jul 25, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 12
/
expression.rs
147 lines (124 loc) · 4.23 KB
/
expression.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
// Licensed to the Apache Software Foundation (ASF) under one
// or more contributor license agreements. See the NOTICE file
// distributed with this work for additional information
// regarding copyright ownership. The ASF licenses this file
// to you under the Apache License, Version 2.0 (the
// "License"); you may not use this file except in compliance
// with the License. You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing,
// software distributed under the License is distributed on an
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
// KIND, either express or implied. See the License for the
// specific language governing permissions and limitations
// under the License.
use pyo3::PyMappingProtocol;
use pyo3::{basic::CompareOp, prelude::*, PyNumberProtocol, PyObjectProtocol};
use std::convert::{From, Into};
use datafusion::arrow::datatypes::DataType;
use datafusion::logical_plan::{col, lit, Expr};
use datafusion::scalar::ScalarValue;
/// An PyExpr that can be used on a DataFrame
#[pyclass(name = "Expression", module = "datafusion", subclass)]
#[derive(Debug, Clone)]
pub(crate) struct PyExpr {
pub(crate) expr: Expr,
}
impl From<PyExpr> for Expr {
fn from(expr: PyExpr) -> Expr {
expr.expr
}
}
impl From<Expr> for PyExpr {
fn from(expr: Expr) -> PyExpr {
PyExpr { expr }
}
}
#[pyproto]
impl PyNumberProtocol for PyExpr {
fn __add__(lhs: PyExpr, rhs: PyExpr) -> PyResult<PyExpr> {
Ok((lhs.expr + rhs.expr).into())
}
fn __sub__(lhs: PyExpr, rhs: PyExpr) -> PyResult<PyExpr> {
Ok((lhs.expr - rhs.expr).into())
}
fn __truediv__(lhs: PyExpr, rhs: PyExpr) -> PyResult<PyExpr> {
Ok((lhs.expr / rhs.expr).into())
}
fn __mul__(lhs: PyExpr, rhs: PyExpr) -> PyResult<PyExpr> {
Ok((lhs.expr * rhs.expr).into())
}
fn __mod__(lhs: PyExpr, rhs: PyExpr) -> PyResult<PyExpr> {
Ok(lhs.expr.modulus(rhs.expr).into())
}
fn __and__(lhs: PyExpr, rhs: PyExpr) -> PyResult<PyExpr> {
Ok(lhs.expr.and(rhs.expr).into())
}
fn __or__(lhs: PyExpr, rhs: PyExpr) -> PyResult<PyExpr> {
Ok(lhs.expr.or(rhs.expr).into())
}
fn __invert__(&self) -> PyResult<PyExpr> {
Ok(self.expr.clone().not().into())
}
}
#[pyproto]
impl PyObjectProtocol for PyExpr {
fn __richcmp__(&self, other: PyExpr, op: CompareOp) -> PyExpr {
let expr = match op {
CompareOp::Lt => self.expr.clone().lt(other.expr),
CompareOp::Le => self.expr.clone().lt_eq(other.expr),
CompareOp::Eq => self.expr.clone().eq(other.expr),
CompareOp::Ne => self.expr.clone().not_eq(other.expr),
CompareOp::Gt => self.expr.clone().gt(other.expr),
CompareOp::Ge => self.expr.clone().gt_eq(other.expr),
};
expr.into()
}
fn __str__(&self) -> PyResult<String> {
Ok(format!("{}", self.expr))
}
}
#[pymethods]
impl PyExpr {
#[staticmethod]
pub fn literal(value: ScalarValue) -> PyExpr {
lit(value).into()
}
#[staticmethod]
pub fn column(value: &str) -> PyExpr {
col(value).into()
}
/// assign a name to the PyExpr
pub fn alias(&self, name: &str) -> PyExpr {
self.expr.clone().alias(name).into()
}
/// Create a sort PyExpr from an existing PyExpr.
#[args(ascending = true, nulls_first = true)]
pub fn sort(&self, ascending: bool, nulls_first: bool) -> PyExpr {
self.expr.clone().sort(ascending, nulls_first).into()
}
pub fn is_null(&self) -> PyExpr {
self.expr.clone().is_null().into()
}
pub fn cast(&self, to: DataType) -> PyExpr {
// self.expr.cast_to() requires DFSchema to validate that the cast
// is supported, omit that for now
let expr = Expr::Cast {
expr: Box::new(self.expr.clone()),
data_type: to,
};
expr.into()
}
}
#[pyproto]
impl PyMappingProtocol for PyExpr {
fn __getitem__(&self, key: &str) -> PyResult<PyExpr> {
Ok(Expr::GetIndexedField {
expr: Box::new(self.expr.clone()),
key: ScalarValue::Utf8(Some(key.to_string())),
}
.into())
}
}