soma.cell

Relational inner_join: enrich order rows with customer details, then roll up spend per customer tier

Demonstrates inner_join on a shared key, select to project columns, and agg over the joined result.

A Soma program from the data part of the verified corpus: 57 lines, cells OrderJoin, OrderJoinTests. It passes soma check with soma 3.2.4.

How it is built

The cell OrderJoin has no face and no memory. Handlers: orders (line 6), customers (line 17), enriched (line 26), enriched_view (line 28), spend_by_tier (line 33) and run (line 39).

The file also holds 1 test cell (OrderJoinTests) with 4 assertions, run by soma test.

Generated from soma describe on this file.

The source

inner_join_orders.cell

// Relational inner_join: enrich order rows with customer details, then
// roll up spend per customer tier. Demonstrates inner_join on a shared key,
// select to project columns, and agg over the joined result.

cell OrderJoin {
    on orders() {
        return list(
            map("order_id", 1, "customer_id", "c1", "total", 120),
            map("order_id", 2, "customer_id", "c2", "total", 80),
            map("order_id", 3, "customer_id", "c1", "total", 60),
            map("order_id", 4, "customer_id", "c3", "total", 200),
            map("order_id", 5, "customer_id", "c2", "total", 40),
            map("order_id", 6, "customer_id", "c9", "total", 999)
        )
    }

    on customers() {
        return list(
            map("customer_id", "c1", "name", "Acme",   "tier", "gold"),
            map("customer_id", "c2", "name", "Globex", "tier", "silver"),
            map("customer_id", "c3", "name", "Initech","tier", "gold")
        )
    }

    // Inner join drops order 6 (customer c9 has no match).
    on enriched() { return orders() |> inner_join(customers(), "customer_id") }

    on enriched_view() {
        return enriched() |> select("order_id", "name", "tier", "total")
    }

    // Total spend per tier among matched orders.
    on spend_by_tier() {
        return enriched()
            |> agg("tier", "total:sum")
            |> sort_by("total_sum", "desc")
    }

    on run() {
        for r in enriched_view() { print("#{r.order_id} {r.name} ({r.tier}): {r.total}") }
        return spend_by_tier()
    }
}

cell test OrderJoinTests {
    rules {
        // 5 of 6 orders match a customer (c9 is dropped)
        assert len(enriched()) == 5
        // gold = Acme(120+60) + Initech(200) = 380
        assert (spend_by_tier() |> filter_by("tier", "=", "gold"))[0].total_sum == 380
        // silver = Globex(80+40) = 120
        assert (spend_by_tier() |> filter_by("tier", "=", "silver"))[0].total_sum == 120
        // gold outspends silver
        assert spend_by_tier()[0].tier == "gold"
    }
}

What the toolchain says

Run on a fresh copy of the program at build time; the outputs are pasted unedited.

$ soma check inner_join_orders.cell
✓ All checks passed.
$ soma test inner_join_orders.cell
test OrderJoinTests ...
  ✓ assert len(enriched()) == 5
  ✓ assert (spend_by_tier() |> filter_by("tier", "=", "gold"))[0].total_sum == 380
  ✓ assert (spend_by_tier() |> filter_by("tier", "=", "silver"))[0].total_sum == 120
  ✓ assert spend_by_tier()[0].tier == "gold"

4 tests: 4 passed, 0 failed
$ soma verify inner_join_orders.cell
Verifying inner_join_orders.cell...
No state machine in this program: nothing to prove beyond `soma check` (invariants and lifecycles are what verify proves).
VERIFY OK (vacuous — no state machine)

Builtins used

agg, filter_by, inner_join, len, list, map, print, select, sort_by

Source file: /corpus/data/inner_join_orders.cell · examples/corpus/data/inner_join_orders.cell