Keep money exact
This guide keeps amounts in DECIMAL from the stream to the table that serving reads, so that sums of money stay exact to the cent.
The tutorials declare amounts DOUBLE to stay short. A DOUBLE cannot hold most cent values exactly, so its sums drift. For money, use DECIMAL at every step.
Before you start, have the hook's stream, statement and sink written ( Buckets per key builds them with DOUBLE).
1. Declare the amount DECIMAL in the stream's record
PAYMENTS := EXTERNAL_COLUMNS(payment_id BIGINT, merchant_id BIGINT, amount DECIMAL(12, 2), event_time TIMESTAMP(3)
FROM TABLE(STREAM[payments])),
A JSON number or a numeric string is decoded exactly from its text, never through a floating-point number ( Records ). The statement's input takes the same type: AMOUNT := INPUT(DECIMAL(12, 2)).
2. Know the type of each sum
Each way of summing gives its own type:
DEFINE
pays := ARRAY(
ROW(10.10::DECIMAL(12,2) AS amount),
ROW(0.20::DECIMAL(12,2) AS amount)
)
RETURN
transform_sum := TYPEOF(
pays.TRANSFORM(RETURN total := SUM(amount)).UNWRAP_ONE()
),
array_sum := TYPEOF(ARRAY_SUM(pays[amount])),
divided := TYPEOF(ARRAY_SUM(pays[amount]) / 2),
rounded_up := 0.005::DECIMAL(12,3)::DECIMAL(12,2) = 0.01::DECIMAL(12,2)
;| transform_sum VARCHAR | array_sum VARCHAR | divided VARCHAR | rounded_up BOOLEAN |
|---|---|---|---|
| DECIMAL(38,2) | DECIMAL(38,2) | DOUBLE | TRUE |
SUMin aTRANSFORMandARRAY_SUMkeep the scale, with 38 digits:DECIMAL(38, 2).- A division by an integer is a
DOUBLE. Divide last, after the cast to the column's type. - A cast to a narrower scale rounds half away from zero:
0.005becomes0.01.
3. Cast the sum to the column's type
A bucket's total is stored with the type of the sum, DECIMAL(38, 2): no cast, so nothing can fail, and the prepared statement registers:
CREATE OR REPLACE FEATURES IN FM.TUTORIALS.MONEY AS
PERSIST
minute_pays := INPUT(ARRAY<ROW<payment_id BIGINT, amount DECIMAL(12,2)>>),
minute_total := ROW(
ARRAY_COUNT(minute_pays) AS n,
COALESCE(ARRAY_SUM(minute_pays[amount]), 0::DECIMAL(38,2)) AS total
),
minute_total_ps := PREPARED_STATEMENT(minute_total USING minute_pays)
;| feature_name VARCHAR | status VARCHAR | message VARCHAR |
|---|---|---|
| FM.TUTORIALS.MONEY.MINUTE_PAYS | CREATED | Feature created as not exists |
| FM.TUTORIALS.MONEY.MINUTE_TOTAL | CREATED | Feature created as not exists |
| FM.TUTORIALS.MONEY.MINUTE_TOTAL_PS | CREATED | Feature created as not exists |
A sum has 36 integer digits. Its cast to a narrower column, DECIMAL(24, 2), drops some, can fail at runtime, and is refused in a prepared statement until it is acknowledged ( Runtime errors in a prepared statement ):
CREATE OR REPLACE FEATURES IN FM.TUTORIALS.MONEY AS
PERSIST
minute_total_24 := ARRAY_SUM(minute_pays[amount])::DECIMAL(24,2),
minute_total_24_ps := PREPARED_STATEMENT(minute_total_24 USING minute_pays)
;Acknowledge it only when no key's total can reach the column's bound (10^22 for DECIMAL(24, 2)): past it, the call fails.
4. Give the table the same type
Declare the column with the type the statement writes. SHOW CREATE SINK … READER prints the reader for the layout the statement writes ( Reading back ), and the column types follow it:
| The statement writes | PostgreSQL column | MySQL, MariaDB column |
|---|---|---|
DECIMAL(38, 2) | NUMERIC(38, 2) | DECIMAL(38, 2) |
DECIMAL(24, 2), after an acknowledged cast | NUMERIC(24, 2) | DECIMAL(24, 2) |
CREATE TABLE per_minute (merchant_id BIGINT, minute TIMESTAMP, n BIGINT, total NUMERIC(38, 2), …); The layout columns that follow the value (request_time, compute_time, datakey and the others) are in Tables .
Check the result
Sum the same payments in your warehouse, in DECIMAL, and compare with the stored totals: they match to the cent. A total that differs in its last digits went through a DOUBLE somewhere: the record, an input, a division, or the column.
Common mistakes
- A
DOUBLEanywhere on the path. OneDOUBLEinput or column makes every sum after it inexact. - A narrowing cast acknowledged too quickly. A total past the column's bound fails the call. In a hook, a failing call removes the key's value ( Tombstones ). Store
DECIMAL(38, 2)when the bound is not certain. - An average computed before the cast.
total / nis aDOUBLE. Cast the total first, divide last, and store the average in its ownDOUBLEcolumn if you need it.
Related
- Late, corrected and missing events : reversals with negated amounts.
- Column types : how serving reads
NUMERICandDECIMALcolumns.