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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
// 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 datafusion_expr::logical_plan::{Join, JoinConstraint, JoinType};
use pyo3::prelude::*;
use std::fmt::{self, Display, Formatter};

use crate::common::df_schema::PyDFSchema;
use crate::expr::{logical_node::LogicalNode, PyExpr};
use crate::sql::logical::PyLogicalPlan;

#[derive(Debug, Clone, PartialEq, Eq, Hash)]
#[pyclass(name = "JoinType", module = "datafusion.expr")]
pub struct PyJoinType {
    join_type: JoinType,
}

impl From<JoinType> for PyJoinType {
    fn from(join_type: JoinType) -> PyJoinType {
        PyJoinType { join_type }
    }
}

impl From<PyJoinType> for JoinType {
    fn from(join_type: PyJoinType) -> Self {
        join_type.join_type
    }
}

#[pymethods]
impl PyJoinType {
    pub fn is_outer(&self) -> bool {
        self.join_type.is_outer()
    }
}

impl Display for PyJoinType {
    fn fmt(&self, f: &mut Formatter) -> fmt::Result {
        write!(f, "{}", self.join_type)
    }
}

#[derive(Debug, Clone, Copy)]
#[pyclass(name = "JoinConstraint", module = "datafusion.expr")]
pub struct PyJoinConstraint {
    join_constraint: JoinConstraint,
}

impl From<JoinConstraint> for PyJoinConstraint {
    fn from(join_constraint: JoinConstraint) -> PyJoinConstraint {
        PyJoinConstraint { join_constraint }
    }
}

impl From<PyJoinConstraint> for JoinConstraint {
    fn from(join_constraint: PyJoinConstraint) -> Self {
        join_constraint.join_constraint
    }
}

#[pyclass(name = "Join", module = "datafusion.expr", subclass)]
#[derive(Clone)]
pub struct PyJoin {
    join: Join,
}

impl From<Join> for PyJoin {
    fn from(join: Join) -> PyJoin {
        PyJoin { join }
    }
}

impl From<PyJoin> for Join {
    fn from(join: PyJoin) -> Self {
        join.join
    }
}

impl Display for PyJoin {
    fn fmt(&self, f: &mut Formatter) -> fmt::Result {
        write!(
            f,
            "Join
            Left: {:?}
            Right: {:?}
            On: {:?}
            Filter: {:?}
            JoinType: {:?}
            JoinConstraint: {:?}
            Schema: {:?}
            NullEqualsNull: {:?}",
            &self.join.left,
            &self.join.right,
            &self.join.on,
            &self.join.filter,
            &self.join.join_type,
            &self.join.join_constraint,
            &self.join.schema,
            &self.join.null_equals_null,
        )
    }
}

#[pymethods]
impl PyJoin {
    /// Retrieves the left input `LogicalPlan` to this `Join` node
    fn left(&self) -> PyResult<PyLogicalPlan> {
        Ok(self.join.left.as_ref().clone().into())
    }

    /// Retrieves the right input `LogicalPlan` to this `Join` node
    fn right(&self) -> PyResult<PyLogicalPlan> {
        Ok(self.join.right.as_ref().clone().into())
    }

    /// Retrieves the right input `LogicalPlan` to this `Join` node
    fn on(&self) -> PyResult<Vec<(PyExpr, PyExpr)>> {
        Ok(self
            .join
            .on
            .iter()
            .map(|(l, r)| (PyExpr::from(l.clone()), PyExpr::from(r.clone())))
            .collect())
    }

    /// Retrieves the filter `Option<PyExpr>` of this `Join` node
    fn filter(&self) -> PyResult<Option<PyExpr>> {
        Ok(self.join.filter.clone().map(Into::into))
    }

    /// Retrieves the `JoinType` to this `Join` node
    fn join_type(&self) -> PyResult<PyJoinType> {
        Ok(self.join.join_type.into())
    }

    /// Retrieves the `JoinConstraint` to this `Join` node
    fn join_constraint(&self) -> PyResult<PyJoinConstraint> {
        Ok(self.join.join_constraint.into())
    }

    /// Resulting Schema for this `Join` node instance
    fn schema(&self) -> PyResult<PyDFSchema> {
        Ok(self.join.schema.as_ref().clone().into())
    }

    /// If null_equals_null is true, null == null else null != null
    fn null_equals_null(&self) -> PyResult<bool> {
        Ok(self.join.null_equals_null)
    }

    fn __repr__(&self) -> PyResult<String> {
        Ok(format!("Join({})", self))
    }

    fn __name__(&self) -> PyResult<String> {
        Ok("Join".to_string())
    }
}

impl LogicalNode for PyJoin {
    fn inputs(&self) -> Vec<PyLogicalPlan> {
        vec![
            PyLogicalPlan::from((*self.join.left).clone()),
            PyLogicalPlan::from((*self.join.right).clone()),
        ]
    }

    fn to_variant(&self, py: Python) -> PyResult<PyObject> {
        Ok(self.clone().into_py(py))
    }
}