# `Localize.Unit.Ecto.Query`
[🔗](https://github.com/elixir-localize/localize_sql/blob/v2.0.0/lib/localize/unit/ecto/query.ex#L2)

Query expressions for comparing units stored in the `cldr_unit` and
`cldr_unit_with_usage` composite types.

The unit Ecto types store a value in the base unit of its dimension,
so `2 kilometer` is stored as `2000 meter`. PostgreSQL compares two
composite values field by field, the unit and then the value, so a
comparison of two stored units is a comparison of their values within
one dimension, and a range whose bounds are in one dimension never
selects a value of another.

A bound has to reach PostgreSQL as a value of the composite type.
Compared with a column, a parameter is otherwise read as an anonymous
`record`, which Postgrex cannot encode. `cldr_unit/1` and
`cldr_unit_with_usage/1` dump a bound through the Ecto type, in the
base unit, and cast it to the composite type, so a bound may be given
in any unit of the dimension:

    import Ecto.Query
    import Localize.Unit.Ecto.Query

    from m in Measurement, where: m.distance >= cldr_unit(^lower)

    from m in Measurement, where: unit_between(m.distance, ^lower, ^upper)

### Indexes

An ordinary btree on the column serves these comparisons, and since
every value of a dimension shares its base unit it orders them by
value. A count over a range is then an index-only scan:

    create index(:measurements, [:distance])

### Aggregates

`unit_sum/1`, `unit_avg/1`, `unit_min/1` and `unit_max/1` aggregate a
`cldr_unit` column with PostgreSQL's own aggregates over its fields,
which run in parallel, and raise when a set holds more than one unit,
as the `sum`, `avg`, `min` and `max` aggregates of
`Localize.Unit.DDL` do. They are about four times faster than those
aggregates, which are written in PL/pgSQL. The mismatch is raised by
the `cldr_unit_mismatch` function that
`Localize.Unit.DDL.define_aggregate_functions/1` creates.

    from m in Measurement, select: unit_sum(m.distance)

# `cldr_unit`
*macro* 

Casts a unit to a `cldr_unit` query parameter in the base unit of its
dimension.

### Arguments

* `value` is a pinned `t:Localize.Unit.t/0` (`^value`), in any unit
  of its dimension.

### Returns

* A query fragment `value::cldr_unit`, whose parameter is the unit in
  its base unit, for comparison with a `cldr_unit` column.

### Examples

    iex> import Ecto.Query
    iex> {:ok, lower} = Localize.Unit.new(1, "kilometer")
    iex> query = from m in "measurements", where: m.distance >= cldr_unit(^lower), select: m.id
    iex> match?(%Ecto.Query{}, query)
    true

# `cldr_unit_with_usage`
*macro* 

Casts a unit to a `cldr_unit_with_usage` query parameter in the base
unit of its dimension.

The usage is the third field of the composite, so it orders values
that are equal: compare with a bound of the same usage.

### Arguments

* `value` is a pinned `t:Localize.Unit.t/0` (`^value`), in any unit
  of its dimension, with or without a usage.

### Returns

* A query fragment `value::cldr_unit_with_usage`, whose parameter is
  the unit in its base unit, for comparison with a
  `cldr_unit_with_usage` column.

### Examples

    iex> import Ecto.Query
    iex> {:ok, lower} = Localize.Unit.new(1, "kilometer", usage: "road")
    iex> query = from t in "trips", where: t.distance >= cldr_unit_with_usage(^lower), select: t.id
    iex> match?(%Ecto.Query{}, query)
    true

# `unit_avg`
*macro* 

Averages a `cldr_unit` column.

### Arguments

* `expression` is an Ecto query expression that evaluates to a
  `cldr_unit`.

### Returns

* A query expression that loads as a `t:Localize.Unit.t/0` in the
  base unit, or `nil` for an empty set. A set holding more than one
  unit raises a PostgreSQL error with SQLSTATE `22033`.

### Examples

    iex> import Ecto.Query
    iex> query = from m in "measurements", select: unit_avg(m.distance)
    iex> match?(%Ecto.Query{}, query)
    true

# `unit_between`
*macro* 

Tests whether a `cldr_unit` lies between two units, inclusive.

Both bounds are dumped in the base unit of their dimension, so they
may be given in any unit of it. A row of another dimension is never
between them.

### Arguments

* `expression` is an Ecto query expression that evaluates to a
  `cldr_unit`.

* `lower` is a pinned `t:Localize.Unit.t/0` (`^lower`).

* `upper` is a pinned `t:Localize.Unit.t/0` (`^upper`) of the same
  dimension as `lower`.

### Returns

* A boolean query fragment
  `expression BETWEEN lower::cldr_unit AND upper::cldr_unit`.

### Examples

    iex> import Ecto.Query
    iex> {:ok, lower} = Localize.Unit.new(500, "meter")
    iex> {:ok, upper} = Localize.Unit.new(1, "mile")
    iex> query = from m in "measurements", where: unit_between(m.distance, ^lower, ^upper), select: m.id
    iex> match?(%Ecto.Query{}, query)
    true

# `unit_max`
*macro* 

Returns the greatest value of a `cldr_unit` column.

### Arguments

* `expression` is an Ecto query expression that evaluates to a
  `cldr_unit`.

### Returns

* A query expression that loads as a `t:Localize.Unit.t/0` in the
  base unit, or `nil` for an empty set. A set holding more than one
  unit raises a PostgreSQL error with SQLSTATE `22033`.

### Examples

    iex> import Ecto.Query
    iex> query = from m in "measurements", select: unit_max(m.distance)
    iex> match?(%Ecto.Query{}, query)
    true

# `unit_min`
*macro* 

Returns the least value of a `cldr_unit` column.

### Arguments

* `expression` is an Ecto query expression that evaluates to a
  `cldr_unit`.

### Returns

* A query expression that loads as a `t:Localize.Unit.t/0` in the
  base unit, or `nil` for an empty set. A set holding more than one
  unit raises a PostgreSQL error with SQLSTATE `22033`.

### Examples

    iex> import Ecto.Query
    iex> query = from m in "measurements", select: unit_min(m.distance)
    iex> match?(%Ecto.Query{}, query)
    true

# `unit_sum`
*macro* 

Sums a `cldr_unit` column.

### Arguments

* `expression` is an Ecto query expression that evaluates to a
  `cldr_unit`.

### Returns

* A query expression that loads as a `t:Localize.Unit.t/0` in the
  base unit, or `nil` for an empty set. A set holding more than one
  unit raises a PostgreSQL error with SQLSTATE `22033`.

### Examples

    iex> import Ecto.Query
    iex> query = from m in "measurements", select: unit_sum(m.distance)
    iex> match?(%Ecto.Query{}, query)
    true

---

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