Calculations implementation in Postgres

When a resource is backed by Postgres are Ash Calculations implemented as generated columns?

1 Like

The calculations are translated to Postgres SQL code that does the computation.

here is an example:

  calculations do
    calculate :amount, AshMoney.Types.Money, expr(quantity * 10 / 4 * price)
  end

and here is the sql generated when the calculation is loaded:

SELECT d0."price", d0."quantity", (((d0."quantity"::decimal * $1)::decimal / $2::decimal) * d0."price"::money_with_currency)::money_with_currency::money_with_currency FROM "demo_orders" AS d0 LIMIT $3 [Decimal.new("10"), Decimal.new("4"), 1000]

2 Likes

If you use expr for your calculations they can be translated to SQL. If you use a module calculation they are done in Elixir after loading the data.

1 Like

A subset of calculations (ones that don’t require joins) may eventually be lowered to be generated columns automatically by ash_postgres.

1 Like