AVG computes the average value over a specific column:
SELECT AVG(rating) FROM mock_items
WHERE id @@@ paradedb.all();
The field being averaged, in this case rating, must be indexed as fast.
By default, AVG ignores null values. Use COALESCE to include them in the final average:
SELECT AVG(COALESCE(rating, 0)) FROM mock_items
WHERE id @@@ paradedb.all();
To verify that AVG was pushed down, look for a ParadeDB Aggregate Scan with average in the EXPLAIN output:
EXPLAIN SELECT AVG(rating) FROM mock_items
WHERE id @@@ paradedb.all();
Expected Response
                                       QUERY PLAN
----------------------------------------------------------------------------------------
 Custom Scan (ParadeDB Aggregate Scan) on mock_items  (cost=0.00..0.00 rows=0 width=32)
   Index: search_idx
   Tantivy Query: {"with_index":{"query":"all"}}
   Aggregate Definition: {"0":{"avg":{"field":"rating"}}}
(4 rows)