kosiew commented on code in PR #27047:
URL: https://github.com/apache/datafusion/pull/27047#discussion_r4640740677


##########
datafusion/functions-aggregate/src/first_last.rs:
##########
@@ -463,9 +486,29 @@ impl<S: ValueState> FirstLastGroupsAccumulator<S> {
             touched_groups_buf: Vec::new(),
             pick_first_in_group,
             is_input_pre_ordered,
+            ordering_timezones,
         })
     }
 
+    /// Returns `columns` with the time zone removed from the ordering columns
+    /// (`columns[1..=ordering_req.len()]`) that have one. See
+    /// `ordering_timezones`.
+    fn strip_ordering_timezones<'a>(
+        &self,
+        columns: &'a [ArrayRef],
+    ) -> Cow<'a, [ArrayRef]> {
+        if self.ordering_timezones.iter().all(Option::is_none) {
+            return Cow::Borrowed(columns);
+        }
+        let mut columns = columns.to_vec();
+        for (column, tz) in 
columns[1..].iter_mut().zip(&self.ordering_timezones) {

Review Comment:
   Could we use `columns.iter_mut().skip(1)` instead of 
`columns[1..].iter_mut()`? Empty merge state currently panics instead of 
returning the existing `Empty row in FIRST_VALUE` error.



##########
datafusion/functions-aggregate/src/first_last.rs:
##########
@@ -1815,6 +1903,80 @@ mod tests {
         Ok(())
     }
 
+    #[test]
+    fn test_group_acc_timezone_ordering() -> Result<()> {
+        let tz: Arc<str> = Arc::from("+02:00");
+        let ts_type = DataType::Timestamp(TimeUnit::Microsecond, 
Some(Arc::clone(&tz)));
+        let schema = Arc::new(Schema::new(vec![
+            Field::new("v", DataType::Int64, true),
+            Field::new("ts", ts_type.clone(), true),
+        ]));
+        let sort_keys = [PhysicalSortExpr {
+            expr: col("ts", &schema)?,
+            options: SortOptions::default(),
+        }];
+        let new_acc = |pick_first_in_group| {
+            FirstLastGroupsAccumulator::try_new(
+                PrimitiveValueState::<Int64Type>::new(DataType::Int64),
+                sort_keys.clone().into(),
+                false,
+                std::slice::from_ref(&ts_type),
+                pick_first_in_group,
+                false,
+            )
+        };
+        let ts = |values: Vec<Option<i64>>| -> ArrayRef {
+            Arc::new(
+                TimestampMicrosecondArray::from(values)
+                    .with_timezone_opt(Some(Arc::clone(&tz))),
+            )
+        };
+
+        for (pick_first_in_group, expected) in [(true, [1, 5]), (false, [3, 
4])] {
+            let mut partial = new_acc(pick_first_in_group)?;
+            partial.update_batch(
+                &[
+                    Arc::new(Int64Array::from(vec![1, 2, 3, 4, 5])),
+                    ts(vec![Some(10), Some(20), Some(30), Some(15), Some(5)]),
+                ],
+                &[0, 0, 0, 1, 1],
+                None,
+                2,
+            )?;
+            // The ordering values are kept without the time zone ...
+            assert!(
+                partial.orderings.iter().flatten().all(|v| matches!(
+                    v,
+                    ScalarValue::TimestampMicrosecond(Some(_), None)
+                ))
+            );
+            assert_eq!(
+                partial.size_of_orderings,
+                partial.compute_size_of_orderings()
+            );
+
+            // ... and the state carries it again, with unchanged values, also
+            // when it is emitted in parts.
+            let first = partial.state(EmitTo::First(1))?;
+            let rest = partial.state(EmitTo::All)?;
+            assert_eq!(first[1].data_type(), &ts_type);
+            assert_eq!(rest[1].data_type(), &ts_type);

Review Comment:
   Could we assert the emitted timestamp ticks in both state chunks? Expected 
values are `[10]` and `[5]` for FIRST_VALUE, and `[30]` and `[15]` for 
LAST_VALUE. This verifies timezone restoration preserves timestamp values.



-- 
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