# `PgRest.SQL.Aggregate`
[🔗](https://github.com/agoodway/pgrest/blob/v0.2.0/lib/pg_rest/sql/aggregate.ex#L1)

The aggregate half of the read compiler: aggregate select items, the to-one spread
hoisting and the implicit `GROUP BY`, mirroring PostgREST's `pgFmtApplyAggregate`,
`hoistSpreadAggFunctions` and `groupF`.

  * `select_item/4` renders `[CAST(]<fun>([CAST(]<column>[ AS <input_cast>)])[ AS <cast>)]
    AS "<alias or fun name>"`; `count()` is `count(*)`. Both casts go through
    `PgRest.SQL.Builder.cast/2`, the only way a request type name reaches SQL.
  * A to-one spread does not aggregate in its own lateral, where each parent row sees at
    most one child row. It selects the aggregate's input under the output name
    (`hoisted_input/4`) and the nearest parent that is not a to-one spread applies the
    function to that lateral column (`hoisted_item/2`) and groups by the rest.
  * `group_by/2` groups a node with aggregates by every other item of its select list.

Aggregates read the node's scoped source like any column, so scope holds under them.

# `aggregating?`

```elixir
@spec aggregating?(PgRest.Plan.Read.t()) :: boolean()
```

Whether the node's rows are aggregates rather than its source rows: it has aggregates
(`aggregates?`) or selects the legacy bare `count`.

# `group_by`

```elixir
@spec group_by(PgRest.Plan.Read.t(), [boolean()]) :: iodata() | []
```

The `GROUP BY` of a node that is not a to-one spread, as PostgREST's `groupF`: emitted only
when the node has aggregates (`aggregates?`). `groupable` is one flag per item of the node's
select list, in order, as the compiler built it (`PgRest.SQL.Read`, `PgRest.SQL.Read.Embed`):
false for an aggregate, true for a column, an embed's output or a spread column. `[]` when
no item is groupable.

Terms are select-list positions. They name exactly the items the select list computes, so a
JSON-path key bound once is not bound again, and an output name can never be read as an input
column of the same name (which `GROUP BY "<alias>"` would).

# `hoisted_input`

```elixir
@spec hoisted_input(
  PgRest.AST.Aggregate.t(),
  String.t(),
  PgRest.SQL.Builder.t(),
  keyword()
) ::
  {iodata(), PgRest.SQL.Builder.t()}
```

What a to-one spread selects for an aggregate its parent computes: the cast input under the
output name (`1` for `count()`, so the parent counts the joined rows).

# `hoisted_item`

```elixir
@spec hoisted_item(PgRest.AST.Aggregate.t(), String.t()) :: iodata()
```

The parent's select item for an aggregate hoisted from the spread lateral `lateral_alias`.

# `select_item`

```elixir
@spec select_item(
  PgRest.AST.Aggregate.t(),
  String.t(),
  PgRest.SQL.Builder.t(),
  keyword()
) ::
  {iodata(), PgRest.SQL.Builder.t()}
```

The select item of an aggregate over `table_alias`, named by its alias or its function.

Options: `:column_types` (Ecto field name to type); a JSON path on a column that is not
`json`/`jsonb` reads it through `to_jsonb`, as for a plain select.

---

*Consult [api-reference.md](api-reference.md) for complete listing*
