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


##########
datafusion/functions-aggregate/src/array_agg.rs:
##########
@@ -394,19 +398,71 @@ impl Accumulator for ArrayAggAccumulator {
     }
 
     fn evaluate(&mut self) -> Result<ScalarValue> {
-        // Transform Vec<ListArr> to ListArr
-        let element_arrays: Vec<&dyn Array> =
-            self.values.iter().map(|a| a.as_ref()).collect();
+        if self.values.is_empty() {
+            return Ok(ScalarValue::new_null_list(self.datatype.clone(), true, 
1));
+        }
+
+        let element_arrays: Vec<ArrayRef> = self
+            .values
+            .iter()
+            .enumerate()
+            .map(|(i, a)| {
+                if i == 0 && self.front_offset > 0 {
+                    a.slice(self.front_offset, a.len() - self.front_offset)
+                } else {
+                    Arc::clone(a)
+                }
+            })
+            .collect();
 
-        if element_arrays.is_empty() {
+        let element_refs: Vec<&dyn Array> =
+            element_arrays.iter().map(|a| a.as_ref()).collect();
+
+        if element_refs.iter().all(|a| a.is_empty()) {
             return Ok(ScalarValue::new_null_list(self.datatype.clone(), true, 
1));
         }
 
-        let concated_array = arrow::compute::concat(&element_arrays)?;
+        let concated_array = arrow::compute::concat(&element_refs)?;
 
         Ok(SingleRowListArrayBuilder::new(concated_array).build_list_scalar())
     }
 
+    fn retract_batch(&mut self, values: &[ArrayRef]) -> Result<()> {
+        if values.is_empty() {
+            return Ok(());
+        }
+
+        assert_eq_or_internal_err!(values.len(), 1, "expects single batch");
+
+        let val = &values[0];
+        let mut to_retract = if self.ignore_nulls {
+            val.len() - val.null_count()

Review Comment:
   I think `retract_batch` should use the same null definition as 
`update_batch` here.
   
   `update_batch` filters `IGNORE NULLS` using `val.logical_nulls()`, but this 
path counts non-null rows with `val.len() - val.null_count()`. For arrays where 
logical nullability differs from physical nullability, such as dictionary or 
run arrays whose values contain logical nulls, the accumulator can store fewer 
rows than `retract_batch` later removes.
   
   That means a sliding `array_agg(... IGNORE NULLS)` window can over-retract 
and accidentally drop following values from the frame. Could we switch this to 
the same null semantics, for example `val.len() - val.logical_null_count()`, 
and add a regression test with an input type where logical nulls differ from 
`null_count()`?



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