From e25564cccd20cd8cfc752de89c2a4f689ce83edd Mon Sep 17 00:00:00 2001 From: Phillip Cloud <417981+cpcloud@users.noreply.github.com> Date: Mon, 8 Jan 2024 15:34:16 -0500 Subject: [PATCH] fix(backends): ensure that the order_by expressions order is preserved when duplicates are introduced --- ibis/expr/analysis.py | 9 ++++++++- ibis/expr/types/generic.py | 1 - 2 files changed, 8 insertions(+), 2 deletions(-) diff --git a/ibis/expr/analysis.py b/ibis/expr/analysis.py index d086a7c827e35..386ede17090f0 100644 --- a/ibis/expr/analysis.py +++ b/ibis/expr/analysis.py @@ -182,7 +182,14 @@ def merge_windows(_, default_frame): group_by = tuple(toolz.unique(_.frame.group_by + default_frame.group_by)) order_by = {} - for sort_key in _.frame.order_by + default_frame.order_by: + # iterate in the order of the existing keys followed by the new keys + # + # this allows duplicates to be overridden with no effect on the original + # position + # + # see https://github.com/ibis-project/ibis/issues/7940 for how this + # originally manifested + for sort_key in default_frame.order_by + _.frame.order_by: order_by[sort_key.expr] = sort_key.ascending order_by = tuple(ops.SortKey(k, v) for k, v in order_by.items()) diff --git a/ibis/expr/types/generic.py b/ibis/expr/types/generic.py index 70a6c62eaa4ef..0f4ff974389ca 100644 --- a/ibis/expr/types/generic.py +++ b/ibis/expr/types/generic.py @@ -765,7 +765,6 @@ def bind(table): ) return expr - op = self.op() if isinstance(window, bl.WindowBuilder): if table := an.find_first_base_table(self.op()): return bind(table)