Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
56 changes: 37 additions & 19 deletions .zed/rules
Original file line number Diff line number Diff line change
Expand Up @@ -1246,8 +1246,7 @@ data. The rules:
dropped tags still order the members, so the data lists are as before. Their values are not
carried: a step that needs them makes the column data before grouping (at the source, or
DerivedSource(tags_as_data=...)). A list-valued tag of the input (a list-typed source tag)
can still order members, so the member sort key (_null_last_key) is
recursive and sorts nulls last at every depth. A grouped stream grouped again on a coarser
can still order members, element by element, nulls last at every depth. A grouped stream grouped again on a coarser
key orders its members by the first grouping's keys outside the coarser key, then by record id. An opt-in mode that keeps non-key tags is ITL-1071.
- GroupBy never depends on how its input's columns are arranged (developer, November 17,
2026, ITL-1226). Members are ordered by the non-key tags in name order (each ascending,
Expand All @@ -1261,23 +1260,42 @@ data. The rules:
record ids, folds, source-info tokens and hashes; only the output's column order follows
the input, as presentation. `TagBy`'s identity is its sorted names, and its new tags are
placed in name order.
- `GroupBy` groups keys as the joins match them, except null (developer, November 17, 2026,
ITL-1234): all rows with a null key form one group, and so do all rows with a NaN key (the
grouping key holds every NaN as one object, `_grouping_key`); `0.0` equals `-0.0`. A NaN key
sorts after every number and before null, as in the member order (`_null_last_key`, which
also sorts a NaN tag after every number). The hashes read a float's bits, so a group emits,
for each float key column, its members' value with the smallest bit pattern at the column's
own width, float16, float32 or float64 (`_emitted_key_row`: `0.0` over `-0.0`, a positive NaN
over a negative one; a signaling NaN has smaller bits than the canonical quiet NaN, so it
wins), taken from the input's Arrow column, never through Python (a float32 signaling NaN
comes back quiet from Python): always a value a member holds, never the first row's, never
chosen by `member_order`. Bits are read only for a group whose key is zero or NaN. A logical
type stored as a float, or a dictionary-encoded float, is a float key, read through its
storage or its values (`_float_storage`). Every
other key is rebuilt from its Python value, as before: equal Python values that differ in
Arrow (`time64[ns]` within a microsecond) are one group with one rebuilt key; grouping by
the exact value is ITL-1250. The stance on `0.0`/`-0.0` and NaN for the whole library
(match by value, as Polars does; show the smallest bit pattern; rewrite nothing) is ITL-1251.
- `GroupBy` groups keys by their Arrow value, as the joins match them, except null (developer,
November 17, 2026, ITL-1234; November 19, 2026, ITL-1250): all rows with a null key form one
group, and so do all rows with a NaN key; `0.0` equals `-0.0`. It decides which rows group
and in what order with Polars, never through Python values: one stable sort of a frame of
the keys (name order), the member-order tags, the record-id and source-id columns
(`nulls_last`; Polars ties `0.0` with `-0.0` and every NaN, and puts NaN after every number
and before null), group boundaries by Polars' equality (`rle_id`). Keys that differ below
Python's precision (`time64[ns]` within a microsecond, `date64` on one day) are separate
groups. A list-valued tag sorts through `(is_null, value)` pairs at every depth (`_sort_key`:
Polars puts a null inside a list first). The sort frame only sees prepared columns
(`_polars_ready`: a logical type as its storage, a dictionary decoded in one place,
`_without_dictionary`, a temporal value as its integer, `float16` as `float32`, a view as
its large variant, `decimal256` as four 64-bit limbs, which Polars cannot read (it panics,
which no caller can catch), every list kind, fixed-size lists included, as a large list
over its prepared values). What may reach it is one rule, `_unorderable_part` (an integer,
float, decimal, boolean, string, binary, date, time, timestamp, duration or null, through
logical storage, dictionary values and lists of every kind at every depth), checked from
the schema at validation and from the table when grouping (the async path skips
validation); anything else is refused, never passed through. The output is built from the input's Arrow table: `take()` in the
sorted order and list arrays from group offsets (`arrow_utils.aggregated_list_array`, the
array-based sibling of `build_aggregated_table`), so every key and list element is a value
a member holds, bit for bit (`time64[ns]` keeps its nanoseconds, a float32 signaling NaN
its bits; ITL-1248 for `GroupBy`). The hashes read a float's bits, so a group emits, for
each float key column (a logical type stored as a float, or a dictionary over floats,
included), its members' value with the smallest bit pattern at the column's own width
(`0.0` over `-0.0`, a positive NaN over a negative one; a signaling NaN has smaller bits
than the canonical quiet NaN, so it wins): never the first row's, never chosen by
`member_order`. Every other key is the group's first member's (equal values are identical).
Only the system-tag and key source-info folds read Python values, which are text. A user tag
whose type has no order (a struct or a map, such as a user class or a `dict[K, V]`, an
interval, a union, a list view) is refused (`InputValidationError`,
`_refuse_unorderable_tags`), as a key and as a non-key tag (which orders the members): a
struct has no decided order (developer, November 19, 2026), so drop it before grouping. A
key that is a list of any kind (a fixed-size list included) is refused as list-valued. `Batch` and `MergeJoin` still rebuild their lists from Python
(ITL-1281). The stance on `0.0`/`-0.0` and NaN for the whole library (match by value, as
Polars does; show the smallest bit pattern; rewrite nothing) is ITL-1251.
- Data source-info values may be str, None, or list[...], nested to any depth; Arrow/Python
types are derived from the value, with None mapping to large_string. That rule has one
definition, arrow_utils.source_info_arrow_type, and applies only to tokens supplied as
Expand Down
56 changes: 37 additions & 19 deletions CLAUDE.md
Original file line number Diff line number Diff line change
Expand Up @@ -1293,8 +1293,7 @@ data. The rules:
dropped tags still order the members, so the data lists are as before. Their values are not
carried: a step that needs them makes the column data before grouping (at the source, or
`DerivedSource(tags_as_data=...)`). A list-valued tag of the input (a list-typed source tag)
can still order members, so the member sort key (`_null_last_key`) is
recursive and sorts nulls last at every depth. A grouped stream grouped again on a coarser key
can still order members, element by element, nulls last at every depth. A grouped stream grouped again on a coarser key
orders its members by the first grouping's keys outside the coarser key, then by record id. An opt-in mode that keeps non-key tags is ITL-1071.
- GroupBy never depends on how its input's columns are arranged (developer, November 17,
2026, ITL-1226). Members are ordered by the non-key tags in name order (each ascending,
Expand All @@ -1308,23 +1307,42 @@ data. The rules:
record ids, folds, source-info tokens and hashes; only the output's column order follows
the input, as presentation. `TagBy`'s identity is its sorted names, and its new tags are
placed in name order.
- `GroupBy` groups keys as the joins match them, except null (developer, November 17, 2026,
ITL-1234): all rows with a null key form one group, and so do all rows with a NaN key (the
grouping key holds every NaN as one object, `_grouping_key`); `0.0` equals `-0.0`. A NaN key
sorts after every number and before null, as in the member order (`_null_last_key`, which
also sorts a NaN tag after every number). The hashes read a float's bits, so a group emits,
for each float key column, its members' value with the smallest bit pattern at the column's
own width, float16, float32 or float64 (`_emitted_key_row`: `0.0` over `-0.0`, a positive NaN
over a negative one; a signaling NaN has smaller bits than the canonical quiet NaN, so it
wins), taken from the input's Arrow column, never through Python (a float32 signaling NaN
comes back quiet from Python): always a value a member holds, never the first row's, never
chosen by `member_order`. Bits are read only for a group whose key is zero or NaN. A logical
type stored as a float, or a dictionary-encoded float, is a float key, read through its
storage or its values (`_float_storage`). Every
other key is rebuilt from its Python value, as before: equal Python values that differ in
Arrow (`time64[ns]` within a microsecond) are one group with one rebuilt key; grouping by
the exact value is ITL-1250. The stance on `0.0`/`-0.0` and NaN for the whole library
(match by value, as Polars does; show the smallest bit pattern; rewrite nothing) is ITL-1251.
- `GroupBy` groups keys by their Arrow value, as the joins match them, except null (developer,
November 17, 2026, ITL-1234; November 19, 2026, ITL-1250): all rows with a null key form one
group, and so do all rows with a NaN key; `0.0` equals `-0.0`. It decides which rows group
and in what order with Polars, never through Python values: one stable sort of a frame of
the keys (name order), the member-order tags, the record-id and source-id columns
(`nulls_last`; Polars ties `0.0` with `-0.0` and every NaN, and puts NaN after every number
and before null), group boundaries by Polars' equality (`rle_id`). Keys that differ below
Python's precision (`time64[ns]` within a microsecond, `date64` on one day) are separate
groups. A list-valued tag sorts through `(is_null, value)` pairs at every depth (`_sort_key`:
Polars puts a null inside a list first). The sort frame only sees prepared columns
(`_polars_ready`: a logical type as its storage, a dictionary decoded in one place,
`_without_dictionary`, a temporal value as its integer, `float16` as `float32`, a view as
its large variant, `decimal256` as four 64-bit limbs, which Polars cannot read (it panics,
which no caller can catch), every list kind, fixed-size lists included, as a large list
over its prepared values). What may reach it is one rule, `_unorderable_part` (an integer,
float, decimal, boolean, string, binary, date, time, timestamp, duration or null, through
logical storage, dictionary values and lists of every kind at every depth), checked from
the schema at validation and from the table when grouping (the async path skips
validation); anything else is refused, never passed through. The output is built from the input's Arrow table: `take()` in the
sorted order and list arrays from group offsets (`arrow_utils.aggregated_list_array`, the
array-based sibling of `build_aggregated_table`), so every key and list element is a value
a member holds, bit for bit (`time64[ns]` keeps its nanoseconds, a float32 signaling NaN
its bits; ITL-1248 for `GroupBy`). The hashes read a float's bits, so a group emits, for
each float key column (a logical type stored as a float, or a dictionary over floats,
included), its members' value with the smallest bit pattern at the column's own width
(`0.0` over `-0.0`, a positive NaN over a negative one; a signaling NaN has smaller bits
than the canonical quiet NaN, so it wins): never the first row's, never chosen by
`member_order`. Every other key is the group's first member's (equal values are identical).
Only the system-tag and key source-info folds read Python values, which are text. A user tag
whose type has no order (a struct or a map, such as a user class or a `dict[K, V]`, an
interval, a union, a list view) is refused (`InputValidationError`,
`_refuse_unorderable_tags`), as a key and as a non-key tag (which orders the members): a
struct has no decided order (developer, November 19, 2026), so drop it before grouping. A
key that is a list of any kind (a fixed-size list included) is refused as list-valued. `Batch` and `MergeJoin` still rebuild their lists from Python
(ITL-1281). The stance on `0.0`/`-0.0` and NaN for the whole library (match by value, as
Polars does; show the smallest bit pattern; rewrite nothing) is ITL-1251.
- `Data` source-info values may be `str`, `None`, or `list[...]`, nested to any depth; the
Arrow and Python types are derived from the value, with `None` mapping to `large_string`.
That rule has one definition, `arrow_utils.source_info_arrow_type`, and applies only to
Expand Down
21 changes: 21 additions & 0 deletions changelog.d/ITL-1250.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
### Breaking changes and migration

- **`GroupBy` refuses a tag whose type has no order** (a struct or a map, such as a user
class or a `dict[K, V]`, an interval, a union), as a key and as a non-key tag, with
`InputValidationError`: every non-key tag orders a group's members. Before, such a tag
raised a bare `TypeError` while grouping (except when no group had two members), or an
error from Polars. Drop the tag before grouping (`select_tag_columns`); `GroupBy` drops a
non-key tag anyway. A fixed-size-list key is refused as list-valued, as any list key is.
(ITL-1250)

### Fixed

- **`GroupBy` groups, orders and emits by Arrow value, never by Python value.** It decides
which rows form a group and in what order with Polars, and builds its output from the
input's Arrow columns. Keys that differ only below what Python keeps are now separate
groups (`time64[ns]` values of 1 ns and 2 ns were one group emitting 0 ns; two `date64`
values on one day were one group at midnight), and members are ordered by such tags
exactly. The data lists hold the members' values bit for bit: before, a `time64[ns]` value
lost its nanoseconds, a `date64` value its time of day, and a float32 signaling NaN came
back quiet (ITL-1248). Results change only for inputs holding such values; no golden
value moves. `GroupBy` is also 5 to 8 times faster on 1e6 rows. (ITL-1250)
21 changes: 18 additions & 3 deletions docs/concepts/operators.md
Original file line number Diff line number Diff line change
Expand Up @@ -316,9 +316,12 @@ cache keys:
- **Groups** are emitted sorted by their keys, taken in name order. The order of the names
in `by` is not part of the operator: `GroupBy(by=["a", "b"])` and `GroupBy(by=["b", "a"])`
are one operator.
- **Keys** group as the joins match them, except for null: rows with equal keys form one
group, all rows with a null key form one group, and so do all rows with a NaN key (NaN
equals NaN here; `0.0` equals `-0.0`). A NaN key sorts after every number and before null.
- **Keys** group by their Arrow value, as the joins match them, except for null: rows with
equal keys form one group, all rows with a null key form one group, and so do all rows
with a NaN key (NaN equals NaN here; `0.0` equals `-0.0`). A NaN key sorts after every
number and before null. `GroupBy` compares and orders values with Polars, never as Python
values, so keys that differ only below what Python keeps are two groups: `time64[ns]`
values of 1 ns and 2 ns, or two `date64` values on one day.
When the members of a group hold a float key with different bits (`0.0` and `-0.0`, or NaN
with another sign or payload), the group emits the one with the smallest bit pattern at
the column's own width (`float16`, `float32` or `float64`; a logical type stored as a
Expand All @@ -327,6 +330,10 @@ cache keys:
NaN has smaller bits than the canonical quiet NaN, so when a member holds one, it is the
key emitted. The emitted key is always a value a member holds, bit for bit, and neither
the order of the rows nor `member_order` decides it.
- **Values are kept exactly.** Every emitted key and every element of a data list is taken
from the input's Arrow columns, so a list holds its members' values bit for bit: a
`time64[ns]` value keeps its nanoseconds, a `date64` value its time of day, a float its
bits (a signaling NaN stays signaling).
- **No arrangement of the input's columns changes the result.** The order of its tags, the
order of its data columns and the layout of its table leave every value, record id and
hash of the output unchanged. The output lists its columns as the input does (the keys in
Expand Down Expand Up @@ -369,6 +376,14 @@ Every name in `by` must be a **scalar tag column** of the input. `GroupBy` raise
the last usually means the stream came from `Batch`, so group *before* batching rather than
after.

A tag whose type has no order can be neither a key nor a non-key tag of the input, because
every non-key tag orders the members: a struct or a map (a user class, a `dict[K, V]`), an
interval, a union. `GroupBy` raises `InputValidationError` for it, from the schema alone. A
key that is a list of any kind, a fixed-size list included, is refused as list-valued. Since
`GroupBy` drops a non-key tag anyway, drop it before grouping, with `select_tag_columns`
naming the tags to keep. A list-valued non-key tag orders the members element by element,
nulls last at every depth.

Streams also expose this as a fluent method. A single column may be given as a string, as
for every operator that takes column names (a string is one name, never its characters):

Expand Down
Loading
Loading