Skip to content

Commit 4798c61

Browse files
committed
refactor: simplify window output binding
1 parent bdd3de7 commit 4798c61

2 files changed

Lines changed: 19 additions & 23 deletions

File tree

src/binder/window.rs

Lines changed: 17 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -61,22 +61,24 @@ impl ExprVisitorMut<'_> for WindowCollector<'_, '_> {
6161
}
6262
}
6363

64-
struct WindowOutputBinder {
65-
outputs: Vec<(ColumnRef, usize)>,
64+
struct WindowOutputBinder<'a> {
65+
groups: &'a [WindowGroup],
66+
base_position: usize,
6667
}
6768

68-
impl ExprVisitorMut<'_> for WindowOutputBinder {
69+
impl ExprVisitorMut<'_> for WindowOutputBinder<'_> {
6970
fn visit_column_ref(
7071
&mut self,
7172
column: &mut ColumnRef,
7273
position: &mut usize,
7374
) -> Result<(), DatabaseError> {
74-
if let Some((_, output_position)) = self
75-
.outputs
75+
if let Some(output_position) = self
76+
.groups
7677
.iter()
77-
.find(|(output_column, _)| output_column == column)
78+
.flat_map(|group| &group.output_columns)
79+
.position(|output_column| output_column == column)
7880
{
79-
*position = *output_position;
81+
*position = self.base_position + output_position;
8082
}
8183
Ok(())
8284
}
@@ -203,24 +205,16 @@ impl<T: Transaction, A: AsRef<[(&'static str, DataValue)]>> Binder<'_, '_, T, A>
203205
}
204206
}
205207

206-
let mut outputs = Vec::new();
207-
let mut position = base_position;
208-
for group in &groups {
209-
for column in &group.output_columns {
210-
outputs.push((*column, position));
211-
position += 1;
212-
}
213-
}
214-
215-
let mut output_binder = WindowOutputBinder { outputs };
216-
for expr in select_list {
208+
let mut output_binder = WindowOutputBinder {
209+
groups: &groups,
210+
base_position,
211+
};
212+
for expr in select_list
213+
.iter_mut()
214+
.chain(order_by.iter_mut().flatten().map(|field| &mut field.expr))
215+
{
217216
output_binder.visit(expr)?;
218217
}
219-
if let Some(order_by) = order_by {
220-
for field in order_by {
221-
output_binder.visit(&mut field.expr)?;
222-
}
223-
}
224218

225219
for group in groups {
226220
let sort_fields = group

src/execution/dql/window.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,7 @@ fn evaluate_partition(
9292
while peer_start < rows.len() {
9393
let mut peer_end = peer_start + 1;
9494
'peer: while peer_end < rows.len() {
95+
// TODO: Cache evaluated order keys to avoid recalculating the previous row.
9596
for field in order_by {
9697
if field.expr.eval(Some(&rows[peer_end - 1]))?
9798
!= field.expr.eval(Some(&rows[peer_end]))?
@@ -135,6 +136,7 @@ impl<'a, T: Transaction + 'a> ExecutorNode<'a, T> for Window {
135136
while arena.next_tuple(self.input, plan_arena)? {
136137
let tuple = mem::take(arena.result_tuple_mut());
137138
let mut same_partition = true;
139+
// TODO: Cache evaluated partition keys to avoid recalculating the previous row.
138140
for expr in &self.partition_by {
139141
if expr.eval(self.rows.last())? != expr.eval(Some(&tuple))? {
140142
same_partition = false;

0 commit comments

Comments
 (0)