Problem
Avito Fintech's BI team must show what happened to every bonus, for IFRS and tax reporting and audits. Accruals and spendings are separate feeds, and the earliest accruals must be consumed first (FIFO). One accrual is split into N accounting rows, and it is spent in M parts, sometimes dozens, plus a burn of the remainder. Trino has no procedural SQL, and a plain join by amount or date counts a write-off once per accrual row.
Example from the article: a spending of 150 ₽ on a paid listing took 80 ₽ from accrual ACC-1 (children's clothing, tariff) and 70 ₽ from accrual ACC-2 (home appliances, promotion).
Solution in brief
Turn FIFO into geometry. A running total of amounts on each feed within a payment makes every accrual bucket and every spending a segment on one axis of cumulative amount. The allocation for a pair is the overlap length of its two segments, computed by one range join with no cursors. The mart carries precomputed payment totals, and a balance filter separates closed bonuses from live or late ones.
Questions this case answers
- How to allocate outgoing amounts to incoming amounts in strict arrival order, per entity, with pure set-based SQL and no cursors or recursion
- How to design a granular fact table so that consumers cannot get inflated totals from repeated parent amounts
- How to match each bonus spending to the accruals it consumed in first-in-first-out order, in plain SQL on millions of payments?
- How to keep accounting detail (subcategory, revenue type) when one accrual is split into several rows and spent in many parts?
Tools and methods
- Cumulative sum per payment — makes each bucket and spending a segment on one axis
- Interval overlap join — the overlap length is the allocation
- Precomputed totals with
any_value— consumers cannot inflate sums - Balance filter — separates closed bonuses from live and late ones
- Trino — the engine the article is written for
Google or a one-time email code. No password.