@@ -23,7 +23,7 @@ use std::sync::Arc;
23
23
24
24
use super :: BuiltInWindowFunctionExpr ;
25
25
use super :: WindowExpr ;
26
- use crate :: window:: window_expr:: WindowFn ;
26
+ use crate :: window:: window_expr:: { get_orderby_values , WindowFn } ;
27
27
use crate :: window:: { PartitionBatches , PartitionWindowAggStates , WindowState } ;
28
28
use crate :: { expressions:: PhysicalSortExpr , reverse_order_bys, PhysicalExpr } ;
29
29
use arrow:: array:: { new_empty_array, ArrayRef } ;
@@ -101,14 +101,19 @@ impl WindowExpr for BuiltInWindowExpr {
101
101
self . order_by . iter ( ) . map ( |o| o. options ) . collect ( ) ;
102
102
let mut row_wise_results = vec ! [ ] ;
103
103
104
- let ( values, order_bys) = self . get_values_orderbys ( batch) ?;
104
+ let mut values = self . evaluate_args ( batch) ?;
105
+ let order_bys = get_orderby_values ( self . order_by_columns ( batch) ?) ;
106
+ let n_args = values. len ( ) ;
107
+ values. extend ( order_bys) ;
108
+ let order_bys_ref = & values[ n_args..] ;
109
+
105
110
let mut window_frame_ctx =
106
111
WindowFrameContext :: new ( self . window_frame . clone ( ) , sort_options) ;
107
112
let mut last_range = Range { start : 0 , end : 0 } ;
108
113
// We iterate on each row to calculate window frame range and and window function result
109
114
for idx in 0 ..num_rows {
110
115
let range = window_frame_ctx. calculate_range (
111
- & order_bys ,
116
+ order_bys_ref ,
112
117
& last_range,
113
118
num_rows,
114
119
idx,
@@ -119,11 +124,11 @@ impl WindowExpr for BuiltInWindowExpr {
119
124
}
120
125
ScalarValue :: iter_to_array ( row_wise_results. into_iter ( ) )
121
126
} else if evaluator. include_rank ( ) {
122
- let columns = self . sort_columns ( batch) ?;
127
+ let columns = self . order_by_columns ( batch) ?;
123
128
let sort_partition_points = evaluate_partition_ranges ( num_rows, & columns) ?;
124
129
evaluator. evaluate_all_with_rank ( num_rows, & sort_partition_points)
125
130
} else {
126
- let ( values, _ ) = self . get_values_orderbys ( batch) ?;
131
+ let values = self . evaluate_args ( batch) ?;
127
132
evaluator. evaluate_all ( & values, num_rows)
128
133
}
129
134
}
@@ -157,18 +162,20 @@ impl WindowExpr for BuiltInWindowExpr {
157
162
} ;
158
163
let state = & mut window_state. state ;
159
164
160
- let ( values, order_bys) =
161
- self . get_values_orderbys ( & partition_batch_state. record_batch ) ?;
165
+ let batch_ref = & partition_batch_state. record_batch ;
166
+ let mut values = self . evaluate_args ( batch_ref) ?;
167
+ let order_bys = if evaluator. uses_window_frame ( ) || evaluator. include_rank ( ) {
168
+ get_orderby_values ( self . order_by_columns ( batch_ref) ?)
169
+ } else {
170
+ vec ! [ ]
171
+ } ;
172
+ let n_args = values. len ( ) ;
173
+ values. extend ( order_bys) ;
174
+ let order_bys_ref = & values[ n_args..] ;
162
175
163
176
// We iterate on each row to perform a running calculation.
164
177
let record_batch = & partition_batch_state. record_batch ;
165
178
let num_rows = record_batch. num_rows ( ) ;
166
- let sort_partition_points = if evaluator. include_rank ( ) {
167
- let columns = self . sort_columns ( record_batch) ?;
168
- evaluate_partition_ranges ( num_rows, & columns) ?
169
- } else {
170
- vec ! [ ]
171
- } ;
172
179
let mut row_wise_results: Vec < ScalarValue > = vec ! [ ] ;
173
180
for idx in state. last_calculated_index ..num_rows {
174
181
let frame_range = if evaluator. uses_window_frame ( ) {
@@ -181,7 +188,7 @@ impl WindowExpr for BuiltInWindowExpr {
181
188
)
182
189
} )
183
190
. calculate_range (
184
- & order_bys ,
191
+ order_bys_ref ,
185
192
// Start search from the last range
186
193
& state. window_frame_range ,
187
194
num_rows,
@@ -197,7 +204,6 @@ impl WindowExpr for BuiltInWindowExpr {
197
204
}
198
205
// Update last range
199
206
state. window_frame_range = frame_range;
200
- evaluator. update_state ( state, idx, & order_bys, & sort_partition_points) ?;
201
207
row_wise_results
202
208
. push ( evaluator. evaluate ( & values, & state. window_frame_range ) ?) ;
203
209
}
0 commit comments