@@ -38,8 +38,8 @@ pub(crate) struct PyDataFrame {
38
38
39
39
impl PyDataFrame {
40
40
/// creates a new PyDataFrame
41
- pub fn new ( df : Arc < DataFrame > ) -> Self {
42
- Self { df }
41
+ pub fn new ( df : DataFrame ) -> Self {
42
+ Self { df : Arc :: new ( df ) }
43
43
}
44
44
}
45
45
@@ -71,51 +71,51 @@ impl PyDataFrame {
71
71
72
72
#[ args( args = "*" ) ]
73
73
fn select_columns ( & self , args : Vec < & str > ) -> PyResult < Self > {
74
- let df = self . df . select_columns ( & args) ?;
74
+ let df = self . df . as_ref ( ) . clone ( ) . select_columns ( & args) ?;
75
75
Ok ( Self :: new ( df) )
76
76
}
77
77
78
78
#[ args( args = "*" ) ]
79
79
fn select ( & self , args : Vec < PyExpr > ) -> PyResult < Self > {
80
80
let expr = args. into_iter ( ) . map ( |e| e. into ( ) ) . collect ( ) ;
81
- let df = self . df . select ( expr) ?;
81
+ let df = self . df . as_ref ( ) . clone ( ) . select ( expr) ?;
82
82
Ok ( Self :: new ( df) )
83
83
}
84
84
85
85
fn filter ( & self , predicate : PyExpr ) -> PyResult < Self > {
86
- let df = self . df . filter ( predicate. into ( ) ) ?;
86
+ let df = self . df . as_ref ( ) . clone ( ) . filter ( predicate. into ( ) ) ?;
87
87
Ok ( Self :: new ( df) )
88
88
}
89
89
90
90
fn with_column ( & self , name : & str , expr : PyExpr ) -> PyResult < Self > {
91
- let df = self . df . with_column ( name, expr. into ( ) ) ?;
91
+ let df = self . df . as_ref ( ) . clone ( ) . with_column ( name, expr. into ( ) ) ?;
92
92
Ok ( Self :: new ( df) )
93
93
}
94
94
95
95
fn aggregate ( & self , group_by : Vec < PyExpr > , aggs : Vec < PyExpr > ) -> PyResult < Self > {
96
96
let group_by = group_by. into_iter ( ) . map ( |e| e. into ( ) ) . collect ( ) ;
97
97
let aggs = aggs. into_iter ( ) . map ( |e| e. into ( ) ) . collect ( ) ;
98
- let df = self . df . aggregate ( group_by, aggs) ?;
98
+ let df = self . df . as_ref ( ) . clone ( ) . aggregate ( group_by, aggs) ?;
99
99
Ok ( Self :: new ( df) )
100
100
}
101
101
102
102
#[ args( exprs = "*" ) ]
103
103
fn sort ( & self , exprs : Vec < PyExpr > ) -> PyResult < Self > {
104
104
let exprs = exprs. into_iter ( ) . map ( |e| e. into ( ) ) . collect ( ) ;
105
- let df = self . df . sort ( exprs) ?;
105
+ let df = self . df . as_ref ( ) . clone ( ) . sort ( exprs) ?;
106
106
Ok ( Self :: new ( df) )
107
107
}
108
108
109
109
fn limit ( & self , count : usize ) -> PyResult < Self > {
110
- let df = self . df . limit ( 0 , Some ( count) ) ?;
110
+ let df = self . df . as_ref ( ) . clone ( ) . limit ( 0 , Some ( count) ) ?;
111
111
Ok ( Self :: new ( df) )
112
112
}
113
113
114
114
/// Executes the plan, returning a list of `RecordBatch`es.
115
115
/// Unless some order is specified in the plan, there is no
116
116
/// guarantee of the order of the result.
117
117
fn collect ( & self , py : Python ) -> PyResult < Vec < PyObject > > {
118
- let batches = wait_for_future ( py, self . df . collect ( ) ) ?;
118
+ let batches = wait_for_future ( py, self . df . as_ref ( ) . clone ( ) . collect ( ) ) ?;
119
119
// cannot use PyResult<Vec<RecordBatch>> return type due to
120
120
// https://github.com/PyO3/pyo3/issues/1813
121
121
batches. into_iter ( ) . map ( |rb| rb. to_pyarrow ( py) ) . collect ( )
@@ -124,7 +124,7 @@ impl PyDataFrame {
124
124
/// Print the result, 20 lines by default
125
125
#[ args( num = "20" ) ]
126
126
fn show ( & self , py : Python , num : usize ) -> PyResult < ( ) > {
127
- let df = self . df . limit ( 0 , Some ( num) ) ?;
127
+ let df = self . df . as_ref ( ) . clone ( ) . limit ( 0 , Some ( num) ) ?;
128
128
let batches = wait_for_future ( py, df. collect ( ) ) ?;
129
129
pretty:: print_batches ( & batches)
130
130
. map_err ( |err| PyArrowException :: new_err ( err. to_string ( ) ) )
@@ -153,9 +153,13 @@ impl PyDataFrame {
153
153
}
154
154
} ;
155
155
156
- let df = self
157
- . df
158
- . join ( right. df , join_type, & join_keys. 0 , & join_keys. 1 , None ) ?;
156
+ let df = self . df . as_ref ( ) . clone ( ) . join (
157
+ right. df . as_ref ( ) . clone ( ) ,
158
+ join_type,
159
+ & join_keys. 0 ,
160
+ & join_keys. 1 ,
161
+ None ,
162
+ ) ?;
159
163
Ok ( Self :: new ( df) )
160
164
}
161
165
0 commit comments