Skip to content

Optimize date_bin for the common nanosecond-stride cases #25314

Description

@niebayes

Problem

date_bin(interval, ts) is the standard time-series bucketing function (GROUP BY date_bin(INTERVAL '1 hour', ts)). Time-based interval strides (INTERVAL '1 hour', '1 day', '1 minute', ...) all carry months == 0 and take the nanosecond-stride path, which is the overwhelmingly common case for time-series workloads.

That path processes each value through a per-row hot loop with:

  • an indirect call through a function pointer (BinFunction),
  • a chain of checked arithmetic (checked_mul/checked_sub/checked_rem/
    checked_add) with error closures,
  • a Result -> Option conversion per value.

None of this is needed: for a positive nanosecond stride the whole computation is equivalent to bin = origin + floor((value*scale - origin)/stride)*stride, i.e. a few plain integer ops.

Proposal

Add a vectorized fast path for the positive nanosecond-stride case: a single infallible pass of plain integer arithmetic, guarded by a whole-array min/max safety check (with a margin covering origin/stride), falling back to the existing per-value path for pathological inputs / month strides / TIME types.

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions