Getting Started

Tutorial: Building with CleaveDB

Group & Aggregate Stages

In addition to standard filtering and projection, CleaveDB allows powerful group-and-aggregate stages inside a pipeline. The output of an aggregation stage is itself a stream of structured records that can be further sorted, limited, or transformed by downstream THEN clauses.

Multi-metric group aggregation

Group documents by an attribute and compute multiple statistical metrics in one pass:

CleaveQLExample · Department-level group aggregation
PIPE FROM emp
  THEN GROUP BY dept
    TALLY AS n,
    MIN OF age AS youngest,
    MAX OF age AS oldest,
    AVERAGE OF age AS avg

This groups all employee records by department and emits structured summary objects containing the count, minimum age, maximum age, and average age per department.

Sorting by computed aggregate aliases

Because stages feed forward into the next stage, you can sort and limit by the computed aliases produced during aggregation:

CleaveQLExample · Top 5 revenue regions
PIPE FROM orders
  THEN WHERE status = "completed"
  THEN GROUP BY region TOTAL OF revenue AS region_total
  THEN ARRANGED BY region_total GOING DOWN
  THEN LIMIT 5

Notice how ARRANGED BY region_total GOING DOWN directly targets the alias created by the preceding TOTAL OF revenue AS region_total stage, and THEN LIMIT 5 yields the top 5 highest-revenue regions.

Supported aggregate functions in PIPE

FunctionSyntax patternDescription
Count / TallyTALLY AS nCounts documents matching the partition
Total / SumTOTAL OF price AS total_priceSums numeric values
AverageAVERAGE OF score AS avg_scoreCalculates arithmetic mean
Minimum / MaximumMIN OF age AS min_a, MAX OF age AS max_aCalculates extreme values
SpreadSPREAD OF latency AS latency_rangeCalculates the difference between max and min