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


##########
datafusion/functions-aggregate/src/first_last.rs:
##########
@@ -930,6 +858,433 @@ impl<S: ValueState + 'static> GroupsAccumulator for 
FirstLastGroupsAccumulator<S
     }
 }
 
+/// The ordering values of each group's winner.
+enum GroupOrderings {
+    /// Arrow row format: comparing two orderings is a byte comparison and no
+    /// [`ScalarValue`] is built per group.
+    Rows(OrderingRows),
+    /// [`ScalarValue`]s compared with `compare_rows`, for ordering types whose
+    /// row encoding does not order like `compare_rows` (nested and union
+    /// types) or does not decode back to the planned type.

Review Comment:
   Dictionaries whose value type arrow's `cast` cannot pack. Row decoding 
returns the plain values (`Dictionary(Int8, Boolean)` comes back as `Boolean`), 
and casting back to the planned type fails:
   
   ```
   Unsupported output type for dictionary packing: Boolean
   ```
   
   With arrow 60.2 this is exactly `Dictionary(_, Boolean)`, `Dictionary(_, 
Duration(_))` and `Dictionary(_, Interval(_))`, for any key type; every other 
flat value type round trips.
   
   These are exotic, but one shows up from pandas: a `category` column of 
booleans written to Arrow IPC.
   
   ```python
   import pandas as pd, pyarrow as pa, pyarrow.feather as feather
   
   df = pd.DataFrame({
       "account": [1, 1, 2, 2],
       "amount": [10, 20, 30, 40],
       "ts": pd.to_datetime(["2026-01-01", "2026-01-01", "2026-01-02", 
"2026-01-02"]),
       "is_refund": pd.Categorical([True, False, True, False]),
   })
   feather.write_feather(pa.Table.from_pandas(df, preserve_index=False), 
"tx.arrow")
   ```
   
   ```sql
   CREATE EXTERNAL TABLE tx STORED AS ARROW LOCATION 'tx.arrow';
   SELECT arrow_typeof(is_refund) FROM tx LIMIT 1;  -- Dictionary(Int8, Boolean)
   SELECT account, first_value(amount ORDER BY ts, is_refund) FROM tx GROUP BY 
account;
   ```
   
   With the round trip check disabled the last query fails with the error 
above; with the check it uses the `ScalarValue` storage and returns the same 
result as main (1 -> 20, 2 -> 40).
   
   The probe in `OrderingRows::try_new` is generic but not very readable - so I 
am thinking of replacing it with an explicit exclusion of these three 
dictionary value types in `row_comparable`, next to the nested and union types. 
Would that be preferable?
   



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