For the complete documentation index, see llms.txt. This page is also available as Markdown.

mod

Use the mod() function to calculate the remainder (modulus) of the division of two numeric values.

Syntax

mod(<dividend>, <divisor>)

Parameters

Name
Type
Required
Description

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 divisor is zero, the function returns null or raises an error.

  • Null Handling: If either input expression is null, the function returns null.

  • Sign of Result: The sign of the result matches the sign of the dividend.

  • Common Use Cases: This function is typically used within the alter stage 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:

RESULT1
RESULT2
RESULT3

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:

EVENT_ID
IS_EVEN

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:

EVENT_ID
BUCKET

101

1

102

2

103

3

104

4

105

0

Last updated

Was this helpful?