mod
Use the mod() function to calculate the remainder (modulus) of the division of two numeric values.
Syntax
mod(<dividend>, <divisor>)Parameters
dividend
integer, float
Yes
The number to be divided (numerator).
divisor
integer, float
Yes
The number to divide by (denominator). Must not be zero.
Returns
Type: integer or float (matches input types)
Description: The mod() function returns the remainder after dividing the dividend by the divisor. If either input is null or the divisor is zero, the function returns null.
Usage notes
Input type: XQL doesn't support NaN or infinite values as input and these value types also can not be returned.
Division by Zero: If the
divisoris zero, the function returnsnullor raises an error.Null Handling: If either input expression is
null, the function returnsnull.Sign of Result: The sign of the result matches the sign of the
dividend.Common Use Cases: This function is typically used within the
alterstage for cyclic calculations, even/odd detection, bucketing, time-based grouping (for example, grouping by hour of day), and hash-based distribution.
Examples
Example 1: Calculate modulus of literal values
Goal: Calculate the remainder for specific division operations.
XQL code:
Explanation: mod(10, 3) returns 1 (10 = 3�3 + 1), mod(15, 5) returns 0 (15 is evenly divisible by 5), and mod(-7, 3) returns -1 (the sign follows the dividend).
Output:
1
0
-1
Example 2: Determine even or odd event IDs
Goal: Classify events as having even or odd event IDs using the modulus operator.
XQL code:
Explanation: This query uses mod(event_id, 2) to check if the event ID is divisible by 2. If the remainder is 0, the event is classified as "even"; otherwise, it is "odd".
Output:
101
odd
102
even
103
odd
104
even
Example 3: Group events into cyclic buckets
Goal: Distribute events into 5 cyclic buckets based on their event ID.
XQL code:
Explanation: This query assigns each event to one of 5 buckets (0 through 4) using mod(event_id, 5). This creates a cyclic distribution pattern useful for load balancing or sampling.
Output:
101
1
102
2
103
3
104
4
105
0
Related articles
Functions:
divide(),multiply(),if()Datasets:
xdr_data
Last updated
Was this helpful?
