# `Macaw.Frame.Columns`
[🔗](https://github.com/dcoai/macaw_frame/blob/v0.1.0/lib/macaw/frame/columns.ex#L1)

The one place the Frame and its layers read a table's columns (spec/05 §4.2, spec/12
§3): data stays columnar and uncopied along the whole path, a delivery's
`Macaw.Data.Table` held as it arrived, and every read of its values goes through this
module.

**Packed reads** hand on the chunks' own buffers (macaw_data spec/01 §1.1.6), nothing
decoded and nothing copied:

- `packed/2` a column's pieces, one a chunk, and `window/4` a run of its rows, every
  buffer a sub-binary of its chunk's;
- `numbers/2` a `:number` or `:time` column as int64 or float64 pieces, a time as integer
  microseconds, which `Macaw.Draw.Scale.apply_column/2` maps to pixels in one pass;
- `extent/2`, `count_valid/2` and `fold/4`, read from the buffers.

**The decoding edge** is `read/3`: a column's values as a list, the whole column or, with
`window: {first, count}`, only that run of rows, for what must be terms (a category, a
text, a reading a summary writes). Every decode in the Frame is a
`Macaw.Data.Table.column/2` call made here, so a layer kit counts it (spec/04 §3,
`:no_decode`). `rows_at/2` answers rows at some indices, the edge that names a row;
`type/2` a column's type and `names/1` its names, decoding nothing; `bound/2` a table read
through a cell's binding of roles to fields, which rebuilds a table only when a binding
names a field other than its role: an identity binding answers the table as given.

# `number_piece`

```elixir
@type number_piece() ::
  {{:int64 | :float64, binary(), binary() | nil}, non_neg_integer(),
   non_neg_integer()}
```

A number piece: int64 or float64 slots, a validity bitmap or `nil`, and the run.

# `piece`

```elixir
@type piece() :: Macaw.Data.Table.piece()
```

A run of a column in one chunk: `{encoding, offset, count}` (macaw_data's piece).

# `bound`

```elixir
@spec bound(Macaw.Data.Table.t(), map()) :: Macaw.Data.Table.t()
```

A table read through a binding of roles to fields (`%{role => field}`, spec/05 §1.3):
each bound field's column under its role's name, every other column as it is. A binding
whose every field is its role's name answers the table as given, with nothing read.

# `count_valid`

```elixir
@spec count_valid(term(), term()) :: non_neg_integer() | nil
```

How many of a column's values are present (`Macaw.Data.Table.count_valid/2`), counted
from its bitmaps; `nil` for a name the table lacks or a value that is no table.

# `extent`

```elixir
@spec extent(term(), term()) :: {term(), term()} | nil
```

The least and the greatest of a column's present values (`Macaw.Data.Table.extent/2`),
read from its buffers, decoding only the two answered; `nil` for a column with none, of
another type, a name the table lacks or a value that is no table.

# `fold`

```elixir
@spec fold(term(), term(), acc, (term(), acc -&gt; acc)) :: acc when acc: term()
```

`fun.(value, acc)` over a column's values in row order, `nil` where missing
(`Macaw.Data.Table.fold_column/4`): read from the buffers, no list built; `acc` for a
name the table lacks or a value that is no table.

# `gather`

```elixir
@spec gather(Macaw.Data.Table.t(), [non_neg_integer()]) :: Macaw.Data.Table.t()
```

The rows at `indices`, ascending, as a table of the same columns read where they lie
(spec/04 §1.12): each run of consecutive indices a view of the chunks it spans, so
nothing is decoded or copied. A value that is no table answers itself.

    iex> t = Macaw.Data.Table.new!([{"v", :number, [10, 20, 30, 40]}])
    iex> t |> Macaw.Frame.Columns.gather([0, 2, 3]) |> Macaw.Frame.Columns.read("v")
    [10, 30, 40]

# `names`

```elixir
@spec names(term()) :: [String.t()]
```

A table's column names, in its order, `[]` for a value that is no table; reads no value.

# `numbers`

```elixir
@spec numbers(term(), term()) :: [number_piece()] | nil
```

A `:number` or `:time` column as number pieces (spec/04 §1.3): int64 or float64 slots with
their bitmap, a time as integer microseconds since the epoch (a naive time as its
wall-clock microseconds, a date as its midnight's), what
`Macaw.Draw.Scale.apply_column/2` maps. The int64 and float64 numbers and every time but
a date hand on the chunk's own buffers; an integral column its float slots, which read
the same values. A date, a union of integers and floats and a list form are written once
into a buffer of their own. `nil` for a name the table lacks, a column of another type,
or a value that is no table.

    iex> t = Macaw.Data.Table.new!([{"t", :time, [~U[1970-01-01 00:00:01Z], nil]}])
    iex> [{{:int64, us, validity}, 0, 2}] = Macaw.Frame.Columns.numbers(t, "t")
    iex> {us, validity}
    {<<1_000_000::signed-little-64, 0::64>>, <<1>>}

# `packed`

```elixir
@spec packed(term(), term()) :: [piece()] | nil
```

A column as its chunks hold it (`Macaw.Data.Table.packed/2`): one piece a chunk the table
reads, nothing decoded or copied; `nil` for a name the table lacks or a value that is no
table.

    iex> t = Macaw.Data.Table.new!([{"v", :number, [1, 2]}])
    iex> [{{:int64, data, _validity}, 0, 2}] = Macaw.Frame.Columns.packed(t, "v")
    iex> data
    <<1::signed-little-64, 2::signed-little-64>>

# `pick`

```elixir
@spec pick(term(), term(), [non_neg_integer()]) :: tuple()
```

A column's values at some rows, `indices` ascending: a tuple with one value an index,
`nil` past the end or for a name the table lacks (spec/04 §1.12). One pass over the
column's buffers (`Macaw.Data.Table.fold_column/4`) keeps the values named and builds no
list of the rest, so an edge that names a few rows never decodes a column whole.

    iex> t = Macaw.Data.Table.new!([{"v", :number, [10, 20, 30, 40]}])
    iex> Macaw.Frame.Columns.pick(t, "v", [1, 3, 9])
    {20, 40, nil}

# `read`

```elixir
@spec read(term(), term(), keyword()) :: list()
```

A column's values by name (spec/05 §4.2): a list, `[]` for a name the table lacks or a
table that is no table. `window: {first, count}` reads only that run of rows.

    iex> {:ok, t} = Macaw.Data.Table.from([%{"v" => 1}, %{"v" => 2}, %{"v" => 3}])
    iex> {Macaw.Frame.Columns.read(t, "v"), Macaw.Frame.Columns.read(t, "v", window: {1, 2})}
    {[1, 2, 3], [2, 3]}

# `rows_at`

```elixir
@spec rows_at(term(), [non_neg_integer()]) :: [map()]
```

The rows at `indices` as row maps, in one gather (spec/04 §1.2); an index the table lacks
answers `%{}` in its place.

# `type`

```elixir
@spec type(term(), term()) :: Macaw.Data.Table.type() | nil
```

A column's type, `nil` for a name the table lacks; reads no value.

# `window`

```elixir
@spec window(term(), term(), term(), term()) :: [piece()] | nil
```

Rows `first` to `first + count - 1` of a column as pieces (`Macaw.Data.Table.window/4`),
every buffer a sub-binary of its chunk's; `nil` for a name the table lacks, a value that
is no table, or a run that is no pair of non-negative integers.

---

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