Macaw.Data.Source.Static (macaw_data v0.21.1)

View Source

A source of fixed data (spec/01 §10.4): rows written once, served as they are, so a tutorial, a test fixture or a demo needs no source of its own.

new/2 takes rows in any shape a delivery takes (Macaw.Data.Delivery.new/2): row maps, tuples with names:, a map or keyword list of columns, an Nx.Tensor with names:, or a Macaw.Data.Table. Its schema is schema: (a Macaw.Data.Schema or the fields Macaw.Data.Schema.new/1 takes), else one inferred from the rows by Macaw.Data.Table.column_types/1, and its description takes params:, viewer_independent:, lateness:, idle_after: and chunk_span: as Macaw.Data.Source.Description.new/1 judges them.

As a source it is pulled, never pushed, and describe/2 reads nothing but the value. It answers every request with its rows as given: one stored answer honors no capability for every request without recomputing it, so its supports is [], and a window, box, region, latest, order or budget a request asks is finished by the runtime (spec/01 §2.4), which only removes rows. Its delivery names its schema's version and serves the request's parameters; it states no window, resolution or cut. It reads no clock: the request's now is the only time there is. It answers every viewer alike, so it is viewer_independent unless viewer_independent: false says otherwise. It is a plain value, holding no process.

capture/2 makes one of a source's answer instead: fetched again it answers that same delivery, every served fact included, so a reader draws the state the answer was taken with. Its map and JSON forms, which carry either as data, are macaw's (Macaw.Definition.Static, spec/01 §10.4); assemble/3 and judge/2 are the parts they build it from.

Examples

iex> alias Macaw.Data.{Schema, Source}
iex> {:ok, static} = Source.Static.new([%{t: 1, v: 3.5}, %{t: 2, v: 4.0}])
iex> {:ok, description} = Source.describe(static, nil)
iex> {description.supports, Schema.window_field(description.schema)}
{[], nil}
iex> request = Source.Request.new!(now: ~U[2026-11-16 00:00:00Z])
iex> {:ok, delivery} = Source.fetch(static, description, request)
iex> Macaw.Data.Delivery.rows(delivery)
{:ok, [%{t: 1, v: 3.5}, %{t: 2, v: 4.0}]}

iex> Macaw.Data.Source.Static.new([%{t: "x"}], schema: %{t: %{type: :number}}, span: 1)
{:error, [{[:span], :unknown_key}]}

Summary

Types

A fault: where, and why (format_error/1).

t()

A static source: the delivery it answers, its description, and whether the delivery is a captured answer, served with its own parameters, or rows, served with the request's.

Functions

A static source from its parts, as Delivery.new/2 and Description.new/1 answer them (spec/01 §10.4): {:ok, source}, or every fault at once.

A captured answer as a static source: delivery, as some source answered it, and that source's description. Fetched again it answers the same delivery, its rows and every served fact (window, resolution, capped, stale, reduced_by, reduced_with, ordered, params, note, position, complete_until) and its origin, so a reader draws the state it was taken with (stale, capped), never a fresh live; as_of is the captured one, and no clock is read. {:ok, source}, or every fault at once.

As capture/2, the source alone, raising Macaw.Data.Error naming the faults.

A fault's words (spec/00 §5): this module's, else the library's.

A delivery's rows held to a schema, as the conformance kit holds a source's answer (spec/01 §10.4): {:ok, table}, the table the rows pack into under the schema's types, or {:error, faults}: every fault of the first check that fails, in this order.

A static source of rows: {:ok, source}, or every fault at once (spec/00 §5).

As new/2, the source alone, raising Macaw.Data.Error naming the faults.

Types

fault()

@type fault() :: {[term()], term()}

A fault: where, and why (format_error/1).

t()

@type t() :: %Macaw.Data.Source.Static{
  captured: boolean(),
  delivery: Macaw.Data.Delivery.t(),
  description: Macaw.Data.Source.Description.t()
}

A static source: the delivery it answers, its description, and whether the delivery is a captured answer, served with its own parameters, or rows, served with the request's.

Functions

assemble(delivery, description, opts \\ [])

@spec assemble(term(), term(), term()) :: {:ok, t()} | {:error, [fault(), ...]}

A static source from its parts, as Delivery.new/2 and Description.new/1 answer them (spec/01 §10.4): {:ok, source}, or every fault at once.

It is how a reader of a document builds the source it read: the parts are values already, so neither is judged again as capture/2 judges an answer some other source gave. What holds them together is checked:

  • the delivery names the description's schema version, else {[:delivery, :schema], {:version, named, described}};
  • the description supports nothing, else {[:description, :supports], :static}: one stored answer honors no capability for every request;
  • the rows are the schema's (judge/2), their faults under [:delivery].

Option captured: (a boolean, false by default) says whether the delivery is a captured answer, served with its own parameters, or rows, served with the request's. A delivery or a description that is none is {[:delivery], {:type, :delivery}} or {[:description], {:type, :description}}; options that are no keyword list are {[], {:type, :keyword}}, an unknown one {[key], :unknown_key}, and a captured: that is no boolean {[:captured], {:type, :boolean}}.

capture(delivery, description)

@spec capture(term(), term()) :: {:ok, t()} | {:error, [fault(), ...]}

A captured answer as a static source: delivery, as some source answered it, and that source's description. Fetched again it answers the same delivery, its rows and every served fact (window, resolution, capped, stale, reduced_by, reduced_with, ordered, params, note, position, complete_until) and its origin, so a reader draws the state it was taken with (stale, capped), never a fresh live; as_of is the captured one, and no clock is read. {:ok, source}, or every fault at once.

Its description is the captured source's, its parameters, lateness, idle_after and chunk_span kept, with supports: []: a request asking another window, order or budget is finished by the runtime (spec/01 §2.4), the stored rows never recomputed. It answers every viewer alike, viewer_independent: true: a captured answer is the rows of the viewer it was taken for, shown to whoever reads the static source.

The faults: {[:delivery], {:type, :delivery}} and {[:description], {:type, :description}} for a term that is no Macaw.Data.Delivery or Macaw.Data.Source.Description, and each one's own faults under its key, as Delivery.new/2 and Description.new/1 judge them; {[:delivery, :retracted], :increment} and {[:delivery, :replace], :increment} for a pushed increment's changes, since a captured answer is a snapshot (spec/01 §2.5); {[:delivery, :schema], {:version, named, described}} for a delivery naming another schema version than the description's; and the rows' faults against the schema under [:delivery, :rows], as new/2 judges them.

capture!(delivery, description)

@spec capture!(term(), term()) :: t()

As capture/2, the source alone, raising Macaw.Data.Error naming the faults.

format_error(faults)

@spec format_error(fault() | term()) :: String.t()

A fault's words (spec/00 §5): this module's, else the library's.

judge(delivery, schema)

@spec judge(term(), term()) :: {:ok, Macaw.Data.Table.t()} | {:error, [fault(), ...]}

A delivery's rows held to a schema, as the conformance kit holds a source's answer (spec/01 §10.4): {:ok, table}, the table the rows pack into under the schema's types, or {:error, faults}: every fault of the first check that fails, in this order.

Every column is described ({[:rows, name], :undescribed}), every value packs as its field's type ({[:rows, name, i], {:type, type}}, a table's column of another type {[:rows, name], {:type, type}}), and every identity field holds a value in each row ({[:rows, name], :no_identity}). Tuple rows and a tensor need their names (Delivery.new/2's faults, under [:rows]).

The table is what a writer of the source's rows writes: new/2 and assemble/3 judge the rows with it. A delivery or a schema that is none is {[:delivery], {:type, :delivery}} or {[:schema], {:type, :schema}}.

new(rows, opts \\ [])

@spec new(term(), term()) :: {:ok, t()} | {:error, [fault(), ...]}

A static source of rows: {:ok, source}, or every fault at once (spec/00 §5).

Options: schema: (a Macaw.Data.Schema, or the fields Schema.new/1 takes), names: (tuple rows' or a tensor's column names, in order), and the description's params:, viewer_independent: (true), lateness:, idle_after: and chunk_span:.

Without schema: the schema is inferred: each column of the type Macaw.Data.Table.column_types/1 reads, a column it leaves out (mixed or empty) :text, and the one :time column, when there is exactly one, the window field.

The rows are judged against the schema, as a source's answer is (spec/01 §9's :deliveries), so the source passes the conformance kit as built: a column the schema does not describe is {[:rows, name], :undescribed}, a value its field's type does not pack {[:rows, name, i], {:type, type}} (a table's column of another type {[:rows, name], {:type, type}}), and a declared identity field without a value in some row {[:rows, name], :no_identity}.

The other faults: {[], {:type, :keyword}} for options that are not a keyword list and {[key], :unknown_key} for one it does not take; Delivery.new/2's for the rows and names:; the schema's own under [:schema] ({[:schema], {:type, :schema}} for a term that is no schema nor a form of one); and Description.new/1's for the description's options.

new!(rows, opts \\ [])

@spec new!(term(), term()) :: t()

As new/2, the source alone, raising Macaw.Data.Error naming the faults.