|
| 1 | +// Licensed to the Apache Software Foundation (ASF) under one |
| 2 | +// or more contributor license agreements. See the NOTICE file |
| 3 | +// distributed with this work for additional information |
| 4 | +// regarding copyright ownership. The ASF licenses this file |
| 5 | +// to you under the Apache License, Version 2.0 (the |
| 6 | +// "License"); you may not use this file except in compliance |
| 7 | +// with the License. You may obtain a copy of the License at |
| 8 | +// |
| 9 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | +// |
| 11 | +// Unless required by applicable law or agreed to in writing, |
| 12 | +// software distributed under the License is distributed on an |
| 13 | +// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 14 | +// KIND, either express or implied. See the License for the |
| 15 | +// specific language governing permissions and limitations |
| 16 | +// under the License. |
| 17 | + |
| 18 | +use std::fmt::{self, Display, Formatter}; |
| 19 | + |
| 20 | +use datafusion::logical_expr::logical_plan::{AsOfJoin, AsOfMatch}; |
| 21 | +use pyo3::IntoPyObjectExt; |
| 22 | +use pyo3::prelude::*; |
| 23 | + |
| 24 | +use crate::common::df_schema::PyDFSchema; |
| 25 | +use crate::expr::PyExpr; |
| 26 | +use crate::expr::join::PyJoinConstraint; |
| 27 | +use crate::expr::logical_node::LogicalNode; |
| 28 | +use crate::sql::logical::PyLogicalPlan; |
| 29 | + |
| 30 | +#[derive(Debug, Clone)] |
| 31 | +#[pyclass(from_py_object, frozen, name = "AsOfMatch", module = "datafusion.expr")] |
| 32 | +pub struct PyAsOfMatch { |
| 33 | + match_condition: AsOfMatch, |
| 34 | +} |
| 35 | + |
| 36 | +impl From<AsOfMatch> for PyAsOfMatch { |
| 37 | + fn from(match_condition: AsOfMatch) -> PyAsOfMatch { |
| 38 | + PyAsOfMatch { match_condition } |
| 39 | + } |
| 40 | +} |
| 41 | + |
| 42 | +impl From<PyAsOfMatch> for AsOfMatch { |
| 43 | + fn from(match_condition: PyAsOfMatch) -> Self { |
| 44 | + match_condition.match_condition |
| 45 | + } |
| 46 | +} |
| 47 | + |
| 48 | +#[pymethods] |
| 49 | +impl PyAsOfMatch { |
| 50 | + fn left(&self) -> PyResult<PyExpr> { |
| 51 | + Ok(self.match_condition.left.clone().into()) |
| 52 | + } |
| 53 | + |
| 54 | + fn op(&self) -> PyResult<String> { |
| 55 | + Ok(self.match_condition.op.to_string()) |
| 56 | + } |
| 57 | + |
| 58 | + fn right(&self) -> PyResult<PyExpr> { |
| 59 | + Ok(self.match_condition.right.clone().into()) |
| 60 | + } |
| 61 | + |
| 62 | + fn __repr__(&self) -> PyResult<String> { |
| 63 | + Ok(format!("{}", self.match_condition)) |
| 64 | + } |
| 65 | +} |
| 66 | + |
| 67 | +impl Display for PyAsOfMatch { |
| 68 | + fn fmt(&self, f: &mut Formatter) -> fmt::Result { |
| 69 | + write!(f, "{}", self.match_condition) |
| 70 | + } |
| 71 | +} |
| 72 | + |
| 73 | +#[pyclass( |
| 74 | + from_py_object, |
| 75 | + frozen, |
| 76 | + name = "AsOfJoin", |
| 77 | + module = "datafusion.expr", |
| 78 | + subclass |
| 79 | +)] |
| 80 | +#[derive(Clone)] |
| 81 | +pub struct PyAsOfJoin { |
| 82 | + join: AsOfJoin, |
| 83 | +} |
| 84 | + |
| 85 | +impl From<AsOfJoin> for PyAsOfJoin { |
| 86 | + fn from(join: AsOfJoin) -> PyAsOfJoin { |
| 87 | + PyAsOfJoin { join } |
| 88 | + } |
| 89 | +} |
| 90 | + |
| 91 | +impl From<PyAsOfJoin> for AsOfJoin { |
| 92 | + fn from(join: PyAsOfJoin) -> Self { |
| 93 | + join.join |
| 94 | + } |
| 95 | +} |
| 96 | + |
| 97 | +impl Display for PyAsOfJoin { |
| 98 | + fn fmt(&self, f: &mut Formatter) -> fmt::Result { |
| 99 | + write!( |
| 100 | + f, |
| 101 | + "AsOfJoin |
| 102 | + Left: {:?} |
| 103 | + Right: {:?} |
| 104 | + On: {:?} |
| 105 | + MatchCondition: {:?} |
| 106 | + JoinConstraint: {:?} |
| 107 | + Schema: {:?}", |
| 108 | + self.join.left, |
| 109 | + self.join.right, |
| 110 | + self.join.on, |
| 111 | + self.join.match_condition, |
| 112 | + self.join.join_constraint, |
| 113 | + self.join.schema, |
| 114 | + ) |
| 115 | + } |
| 116 | +} |
| 117 | + |
| 118 | +#[pymethods] |
| 119 | +impl PyAsOfJoin { |
| 120 | + /// Retrieves the left input `LogicalPlan` to this `AsOfJoin` node |
| 121 | + fn left(&self) -> PyResult<PyLogicalPlan> { |
| 122 | + Ok(self.join.left.as_ref().clone().into()) |
| 123 | + } |
| 124 | + |
| 125 | + /// Retrieves the right input `LogicalPlan` to this `AsOfJoin` node |
| 126 | + fn right(&self) -> PyResult<PyLogicalPlan> { |
| 127 | + Ok(self.join.right.as_ref().clone().into()) |
| 128 | + } |
| 129 | + |
| 130 | + /// Retrieves the equality keys of this `AsOfJoin` node |
| 131 | + fn on(&self) -> PyResult<Vec<(PyExpr, PyExpr)>> { |
| 132 | + Ok(self |
| 133 | + .join |
| 134 | + .on |
| 135 | + .iter() |
| 136 | + .map(|(l, r)| (PyExpr::from(l.clone()), PyExpr::from(r.clone()))) |
| 137 | + .collect()) |
| 138 | + } |
| 139 | + |
| 140 | + /// Retrieves the `AsOfMatch` condition of this `AsOfJoin` node |
| 141 | + fn match_condition(&self) -> PyResult<PyAsOfMatch> { |
| 142 | + Ok(self.join.match_condition.as_ref().clone().into()) |
| 143 | + } |
| 144 | + |
| 145 | + /// Retrieves the `JoinConstraint` to this `AsOfJoin` node |
| 146 | + fn join_constraint(&self) -> PyResult<PyJoinConstraint> { |
| 147 | + Ok(self.join.join_constraint.into()) |
| 148 | + } |
| 149 | + |
| 150 | + /// Resulting Schema for this `AsOfJoin` node instance |
| 151 | + fn schema(&self) -> PyResult<PyDFSchema> { |
| 152 | + Ok(self.join.schema.as_ref().clone().into()) |
| 153 | + } |
| 154 | + |
| 155 | + fn __repr__(&self) -> PyResult<String> { |
| 156 | + Ok(format!("AsOfJoin({self})")) |
| 157 | + } |
| 158 | + |
| 159 | + fn __name__(&self) -> PyResult<String> { |
| 160 | + Ok("AsOfJoin".to_string()) |
| 161 | + } |
| 162 | +} |
| 163 | + |
| 164 | +impl LogicalNode for PyAsOfJoin { |
| 165 | + fn inputs(&self) -> Vec<PyLogicalPlan> { |
| 166 | + vec![ |
| 167 | + PyLogicalPlan::from((*self.join.left).clone()), |
| 168 | + PyLogicalPlan::from((*self.join.right).clone()), |
| 169 | + ] |
| 170 | + } |
| 171 | + |
| 172 | + fn to_variant<'py>(&self, py: Python<'py>) -> PyResult<Bound<'py, PyAny>> { |
| 173 | + self.clone().into_bound_py_any(py) |
| 174 | + } |
| 175 | +} |
0 commit comments