Skip to content

Apache DataFusion SQL Query Engine

fix: `median` retract logic for sliding window frames (#21300)

lyne9eefb7c952bc
diff --git a/datafusion/functions-aggregate/src/median.rs b/datafusion/functions-aggregate/src/median.rs
index 00c0ee3cd4..ab6d9e1832 100644
--- a/datafusion/functions-aggregate/src/median.rs
+++ b/datafusion/functions-aggregate/src/median.rs
@@ -314,8 +314,9 @@ impl<T: ArrowNumericType> Accumulator for MedianAccumulator<T> {
break;
}
}
+ } else {
+ i += 1;
}
- i += 1;
}
Ok(())
}
diff --git a/datafusion/sqllogictest/test_files/aggregate.slt b/datafusion/sqllogictest/test_files/aggregate.slt
index 0232367163..962d1d5103 100644
--- a/datafusion/sqllogictest/test_files/aggregate.slt
+++ b/datafusion/sqllogictest/test_files/aggregate.slt
@@ -1223,6 +1223,22 @@ ORDER BY tags, timestamp;
4 tag2 90 75 80 95
5 tag2 100 80 80 100
+# Test median on a sliding RANGE window with repeated values.
+query RR
+SELECT
+ x,
+ median(x) OVER (
+ ORDER BY x
+ RANGE BETWEEN 1 PRECEDING AND CURRENT ROW
+ ) AS median_val
+FROM (VALUES (1.0), (1.0), (2.0), (5.0)) AS t(x)
+ORDER BY x;
+----
+1 1
+1 1
+2 1
+5 5
+
###########
# Issue #19612: Test that percentile_cont produces correct results
# in window frame queries. Previously percentile_cont consumed its internal state