Aggregations Reference

InputLayer supports 9 aggregation functions for computing summary values over groups of data.

Basic Aggregations

count

Count the number of results.

Syntax:

count<Variable>

Example:

// Count all users
+user_count(count<Id>) <- user(Id, _)

// Count users per department
+dept_count(Dept, count<Id>) <- user(Id, _, Dept)

Returns: Integer count of distinct bindings for the variable.


count_distinct

Count distinct values of a variable.

Syntax:

count_distinct<Variable>

Example:

// Count unique departments
+unique_depts(count_distinct<Dept>) <- user(_, _, Dept)

Returns: Integer count of unique values.


sum

Sum numeric values.

Syntax:

sum<Variable>

Example:

// Total salary per department
+dept_salary(Dept, sum<Salary>) <- employee(_, _, Dept, Salary)

Returns: Sum as Integer or Float (matches input type).


min

Find minimum value.

Syntax:

min<Variable>

Example:

// Lowest price per category
+min_price(Category, min<Price>) <- product(_, Category, Price)

Returns: Minimum value (works with numbers and strings).


max

Find maximum value.

Syntax:

max<Variable>

Example:

// Highest score per game
+high_score(Game, max<Score>) <- scores(_, Game, Score)

Returns: Maximum value (works with numbers and strings).


avg

Compute average of numeric values.

Syntax:

avg<Variable>

Example:

// Average rating per product
+avg_rating(Product, avg<Rating>) <- reviews(_, Product, Rating)

Returns: Float average.


Ranking Aggregations

top_k

Select the top K results ordered by a variable.

Syntax:

top_k<K, OrderVariable>           // Ascending order (lowest K)
top_k<K, OrderVariable, desc>     // Descending order (highest K)

Parameters:

  • K - Number of results to return (integer)
  • OrderVariable - Variable to order by
  • desc - Optional: use descending order

Example:

// Top 10 highest scores
+top_scores(Name, top_k<10, Score, desc>) <- scores(Name, Score)

// Top 5 nearest neighbors by distance
+nearest(Id, top_k<5, Dist>) <-
    query_vec(QV),
    vectors(Id, V),
    Dist = euclidean(QV, V)

Returns: Up to K results with the ordering value.


top_k_threshold

Select top K results, but only if they meet a minimum threshold.

Syntax:

top_k_threshold<K, Threshold, OrderVariable>           // Ascending (default)
top_k_threshold<K, Threshold, OrderVariable, desc>     // Descending

Parameters:

  • K - Maximum number of results (integer)
  • Threshold - Minimum (or maximum for desc) value to include (float)
  • OrderVariable - Variable to order by (with optional :desc/:asc suffix)

Example:

// Top 10 products, but only if rating >= 4.0
+top_rated(Product, top_k_threshold<10, 4.0, Rating, desc>) <-
    reviews(Product, Rating)

// Nearest 5 neighbors within distance 0.5
+near_enough(Id, top_k_threshold<5, 0.5, Dist>) <-
    query_vec(QV),
    vectors(Id, V),
    Dist = euclidean(QV, V)

Returns: Up to K results that meet the threshold.


within_radius

Return all results within a distance threshold (range query).

Syntax:

within_radius<MaxDistance, DistanceVariable>

Parameters:

  • MaxDistance - Maximum distance to include (float)
  • DistanceVariable - Variable containing the distance (with optional :asc/:desc suffix)

Example:

// All vectors within distance 0.3
+nearby(Id, within_radius<0.3, Dist>) <-
    query_vec(QV),
    vectors(Id, V),
    Dist = cosine(QV, V)

// Vectors within distance 0.5
+close_vectors(Id, within_radius<0.5, D>) <-
    query_vec(QV),
    vectors(Id, V),
    D = euclidean(QV, V)

Returns: All results where distance <= MaxDistance.


Aggregation Rules

Grouping

Variables in the head that are NOT aggregated become grouping keys:

// Group by Department, aggregate Salary
+dept_stats(Dept, sum<Salary>, avg<Salary>) <-
    employee(_, _, Dept, Salary)

Multiple Aggregations

Multiple aggregations can be combined in one rule:

+stats(count<Id>, min<Value>, max<Value>, avg<Value>) <-
    data(Id, Value)

With Filters

Aggregations work with filter conditions:

// Only count active users
+active_count(count<Id>) <- user(Id, Active), Active = true

// Sum only positive values
+positive_sum(sum<V>) <- values(V), V > 0

Quick Reference

FunctionSyntaxReturns
countcount<X>Integer count
count_distinctcount_distinct<X>Integer unique count
sumsum<X>Sum (Int or Float)
minmin<X>Minimum value
maxmax<X>Maximum value
avgavg<X>Float average
top_ktop_k<K, X> or top_k<K, X, desc>Top K results
top_k_thresholdtop_k_threshold<K, T, X>Top K meeting threshold
within_radiuswithin_radius<Max, D>All within distance