DHLQ query reference

A DHLQ query is a source followed by stages, one per line, each starting with |.

query Late(owner: Identity, today: Text(len=10))
    returns List<PayableRow>
    api authenticated
    dhlq
    std.business.payable
    | where owner == $owner and status != "paid" and due_on < $today
    | sort due_on, -amount_minor
    | take 50
    | select subject as id, supplier, amount_minor, currency, due_on

Arguments are written $name.

Sources

SourceReads
selfThis contract's records
std.business.payableAnother contract's records, by namespace
search $q in ns.field, ns.other fuzz=1Full-text search over fields

Every record row has subject (its id) and seq (its revision) besides its public fields.

Stages

StageExampleDoes
wherewhere owner == $owner and active == trueKeeps matching rows
validatevalidate $limit > 0 and $limit <= 100Checks arguments once; refuses the query otherwise
letlet remaining = received - reservedAdds a computed field
sortsort due_on, -amount_minorSorts; - for descending
dropdrop 20Skips rows
taketake 100Keeps at most this many
select (or sel)select subject as id, ref, titleChooses and renames columns
join (or lj)join std.crm.contact as c on supplier_id = c.subjectJoins another contract's rows
explodeexplode tags as tagOne row per list item
group by (or by)group by currencyGroups rows
aggagg count() as n, sum(amount_minor) as totalAggregates: count, sum, avg, min, max
bucketbucket created_at 1d as dayGroups timestamps into intervals

Totals

    dhlq
    self
    | where cart_subject.id == $cart_id and quantity > 0
    | agg count() as line_count, sum(total_minor) as total_minor

Per group

    dhlq
    std.personal.finance_transaction
    | where owner == $owner
    | group by category
    | agg sum(amount_minor) as spent, count() as n

Paging

Page by a stable key rather than by offset: pass the last key you saw and filter on it.

    | where owner == $owner and ref > $after
    | sort ref
    | take 100

What the engine maintains

Queries over records are answered from projections the engine keeps up to date. Projections support where, sort, drop, take, select, let, validate, group by, agg and bucket. A query using search, join or explode is part of the language, but the engine refuses to activate a plan it cannot maintain natively: try it in the The Workbench, which shows whether it runs.

Indexed equality filters (owner == $owner) are fast: about 8 ms at 10,000 records.