rocicorp / rails

Replicache on Rails
Other
34 stars 3 forks source link

implement a `reduce` operator & ZQL group-by on top of it #45

Closed tantaman closed 7 months ago

tantaman commented 8 months ago

All aggregate functions can be modeled as reduce

This adds a reduce operator so we can do:

  1. group by
  2. avg, sum, count, min, max, etc.
  3. group_array

This targets SQL behavior for group-by.

tantaman commented 8 months ago

note to self: pullHistory messages can stop at a reduce operator if the operator has already been seeded with history.

tantaman commented 7 months ago

Not sure that I like the aggregation API:

https://github.com/rocicorp/rails/pull/45/files#diff-c88168576b354654e2ec061b5bbab48370b2e04ff6aeec9f2a52d741558d68daR449-R454

q
    .select('status')
    .groupBy('status')
    .array('assignee')
    .min('created');

maybe it should be:

q
    .select('status', agg.array('assignee'), agg.min('created'))
    .groupBy('status')
arv commented 7 months ago

Another strawman:

q
  .select('status').
  .agg('array', 'assignee')
  .agg('min', 'created')

But I think I prefer the first suggested API.

tantaman commented 7 months ago

updated the API to allow putting aggregate calls in select:

q
    .select(
      'status',
      agg.array('assignee'),
      agg.min('created', 'minCreated'),
      agg.max('created', 'maxCreated'),
    )