Jefffrey commented on code in PR #19278:
URL: https://github.com/apache/datafusion/pull/19278#discussion_r2617236521


##########
datafusion/sqllogictest/test_files/aggregate.slt:
##########
@@ -991,6 +991,89 @@ SELECT approx_median(col_f64_nan) FROM median_table
 ----
 NaN
 
+# median_sliding_window
+statement ok
+CREATE TABLE median_window_test (
+    timestamp INT,
+    tags VARCHAR,
+    value DOUBLE
+);
+
+statement ok
+INSERT INTO median_window_test (timestamp, tags, value) VALUES
+(1, 'tag1', 10.0),
+(2, 'tag1', 20.0),
+(3, 'tag1', 30.0),
+(4, 'tag1', 40.0),
+(5, 'tag1', 50.0),
+(1, 'tag2', 60.0),
+(2, 'tag2', 70.0),
+(3, 'tag2', 80.0),
+(4, 'tag2', 90.0),
+(5, 'tag2', 100.0);
+
+query ITRR
+SELECT
+    timestamp,
+    tags,
+    value,
+    median(value) OVER (
+        PARTITION BY tags
+        ORDER BY timestamp
+        ROWS BETWEEN 1 PRECEDING AND 1 FOLLOWING
+    ) AS value_median_3
+FROM median_window_test
+ORDER BY tags, timestamp;
+----
+1 tag1 10 15
+2 tag1 20 20
+3 tag1 30 30
+4 tag1 40 40
+5 tag1 50 45
+1 tag2 60 65
+2 tag2 70 70
+3 tag2 80 80
+4 tag2 90 90
+5 tag2 100 95
+
+# median_non_sliding_window
+query ITRRRR
+SELECT
+    timestamp,
+    tags,
+    value,
+    median(value) OVER (
+        PARTITION BY tags
+        ORDER BY timestamp
+        ROWS BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW
+    ) AS value_median_unbounded_preceding,
+    median(value) OVER (
+        PARTITION BY tags
+        ORDER BY timestamp
+        ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING
+    ) AS value_median_unbounded_both,

Review Comment:
   There is this quite informative comment which seems to explain why this is 
the case:
   
   
https://github.com/apache/datafusion/blob/befaf93a15946be357dffbc08f5728b2f990a98b/datafusion/physical-expr/src/aggregate.rs#L490-L538



##########
datafusion/core/tests/dataframe/mod.rs:
##########
@@ -1102,26 +1102,26 @@ async fn window_using_aggregates() -> Result<()> {
     | first_value | last_val | approx_distinct | approx_median | median | max 
| min  | c2 | c3   |
     
+-------------+----------+-----------------+---------------+--------+-----+------+----+------+
     |             |          |                 |               |        |     
|      | 1  | -85  |
-    | -85         | -101     | 14              | -12           | -101   | 83  
| -101 | 4  | -54  |
-    | -85         | -101     | 17              | -25           | -101   | 83  
| -101 | 5  | -31  |
-    | -85         | -12      | 10              | -32           | -12    | 83  
| -85  | 3  | 13   |
-    | -85         | -25      | 3               | -56           | -25    | -25 
| -85  | 1  | -5   |
-    | -85         | -31      | 18              | -29           | -31    | 83  
| -101 | 5  | 36   |
-    | -85         | -38      | 16              | -25           | -38    | 83  
| -101 | 4  | 65   |
+    | -85         | -101     | 14              | -12           | -12    | 83  
| -101 | 4  | -54  |
+    | -85         | -101     | 17              | -25           | -25    | 83  
| -101 | 5  | -31  |
+    | -85         | -12      | 10              | -32           | -34    | 83  
| -85  | 3  | 13   |
+    | -85         | -25      | 3               | -56           | -56    | -25 
| -85  | 1  | -5   |
+    | -85         | -31      | 18              | -29           | -28    | 83  
| -101 | 5  | 36   |
+    | -85         | -38      | 16              | -25           | -25    | 83  
| -101 | 4  | 65   |
     | -85         | -43      | 7               | -43           | -43    | 83  
| -85  | 2  | 45   |
-    | -85         | -48      | 6               | -35           | -48    | 83  
| -85  | 2  | -43  |
-    | -85         | -5       | 4               | -37           | -5     | -5  
| -85  | 1  | 83   |
-    | -85         | -54      | 15              | -17           | -54    | 83  
| -101 | 4  | -38  |
-    | -85         | -56      | 2               | -70           | -56    | -56 
| -85  | 1  | -25  |
-    | -85         | -72      | 9               | -43           | -72    | 83  
| -85  | 3  | -12  |
+    | -85         | -48      | 6               | -35           | -36    | 83  
| -85  | 2  | -43  |
+    | -85         | -5       | 4               | -37           | -40    | -5  
| -85  | 1  | 83   |
+    | -85         | -54      | 15              | -17           | -18    | 83  
| -101 | 4  | -38  |
+    | -85         | -56      | 2               | -70           | 57     | -56 
| -85  | 1  | -25  |

Review Comment:
   I find this interesting, how we have `-70` for the approx median but `57` 
for median 🤔 



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to