Skip to main content

Functions Reference

EventFlux provides a comprehensive set of built-in functions for data transformation, mathematical operations, string manipulation, and more.

Function Types

EventFlux has two distinct types of functions:

Scalar Functions (Stateless)

Scalar functions operate on individual values and return a single result. They are stateless - each invocation is independent and doesn't remember previous calls.

  • Execute once per event
  • No memory of previous events
  • Can be used anywhere in SELECT, WHERE, HAVING clauses
  • Examples: UPPER(), ABS(), CONCAT(), ROUND()
-- Scalar functions transform individual values
SELECT symbol,
UPPER(name) AS upper_name, -- String transformation
ABS(delta) AS abs_delta, -- Math operation
ROUND(price, 2) AS rounded_price -- Rounding
FROM Trades
INSERT INTO Processed;

Aggregate Functions (Stateful)

Aggregate functions accumulate values over a group of events (typically within a window) and return a summary result. They are stateful - they maintain internal state that tracks values across multiple events.

  • Accumulate values across multiple events
  • Require a window or GROUP BY context
  • State resets when window expires (except "forever" variants)
  • Examples: SUM(), AVG(), COUNT(), FIRST(), LAST()
-- Aggregate functions summarize groups of events
SELECT symbol,
SUM(volume) AS total_volume, -- Accumulates over window
AVG(price) AS avg_price, -- Running average
FIRST(price) AS open_price, -- First value in window
LAST(price) AS close_price -- Most recent value
FROM Trades
WINDOW TUMBLING(5 min)
GROUP BY symbol
INSERT INTO Summary;

See Aggregations for detailed coverage of aggregate functions.

Mathematical Functions

Basic Math

FunctionDescriptionExampleResult
ABS(x)Absolute valueABS(-5)5
CEIL(x)Round upCEIL(4.2)5
FLOOR(x)Round downFLOOR(4.8)4
ROUND(x)Round to nearestROUND(4.5)5
ROUND(x, n)Round to n decimalsROUND(4.567, 2)4.57
TRUNC(x)Truncate to integerTRUNC(4.9)4
TRUNC(x, n)Truncate to n decimalsTRUNC(4.567, 2)4.56
SIGN(x)Returns -1, 0, or 1SIGN(-5)-1
MOD(x, y)Modulo (remainder)MOD(17, 5)2
MAXIMUM(a, b, ...)Maximum of valuesMAXIMUM(1, 5, 3)5
MINIMUM(a, b, ...)Minimum of valuesMINIMUM(1, 5, 3)1

Advanced Math

FunctionDescriptionExample
SQRT(x)Square rootSQRT(16)4
POWER(x, y)x raised to yPOWER(2, 3)8
EXP(x)e raised to xEXP(1)2.718...
LN(x)Natural logarithmLN(2.718)1
LOG(x)Base-10 logarithmLOG(100)2
LOG(base, x)Logarithm with baseLOG(2, 8)3

Trigonometric Functions

FunctionDescription
SIN(x)Sine (radians)
COS(x)Cosine (radians)
TAN(x)Tangent (radians)
ASIN(x)Arc sine
ACOS(x)Arc cosine
ATAN(x)Arc tangent

Example

SELECT sensor_id,
ABS(delta) AS abs_delta,
SQRT(variance) AS std_dev,
ROUND(value, 2) AS rounded_value,
POWER(growth_rate, 2) AS squared_growth
FROM Measurements
INSERT INTO Processed;

String Functions

Basic String Operations

FunctionDescriptionExampleResult
LENGTH(s)String lengthLENGTH('hello')5
UPPER(s)UppercaseUPPER('hello')'HELLO'
LOWER(s)LowercaseLOWER('HELLO')'hello'
TRIM(s)Remove whitespace (both ends)TRIM(' hi ')'hi'
LTRIM(s)Remove leading whitespaceLTRIM(' hi ')'hi '
RTRIM(s)Remove trailing whitespaceRTRIM(' hi ')' hi'

String Manipulation

FunctionDescriptionExampleResult
CONCAT(a, b, ...)ConcatenateCONCAT('a', 'b', 'c')'abc'
SUBSTRING(s, start, len)Extract substringSUBSTRING('hello', 1, 3)'hel'
LEFT(s, n)Get leftmost n charactersLEFT('hello', 3)'hel'
RIGHT(s, n)Get rightmost n charactersRIGHT('hello', 3)'llo'
LPAD(s, len, pad)Left-pad string to lengthLPAD('hi', 5, '*')'***hi'
RPAD(s, len, pad)Right-pad string to lengthRPAD('hi', 5, '*')'hi***'
REPLACE(s, from, to)Replace textREPLACE('hello', 'l', 'x')'hexxo'
REVERSE(s)Reverse stringREVERSE('hello')'olleh'
REPEAT(s, n)Repeat string n timesREPEAT('ab', 3)'ababab'

String Searching

FunctionDescriptionExampleResult
POSITION(sub, s)Find position (1-based)POSITION('ll', 'hello')3
LOCATE(sub, s)Find position (alias)LOCATE('ll', 'hello')3
INSTR(sub, s)Find position (alias)INSTR('ll', 'hello')3
STARTS_WITH(s, prefix)Check prefixSTARTS_WITH('hello', 'he')true
ENDS_WITH(s, suffix)Check suffixENDS_WITH('hello', 'lo')true
CONTAINS(s, sub)Contains checkCONTAINS('hello', 'ell')true

Character Functions

FunctionDescriptionExampleResult
ASCII(s)ASCII code of first characterASCII('A')65
CHR(n)Character from ASCII codeCHR(65)'A'
CHAR(n)Character from ASCII code (alias)CHAR(65)'A'

Example

SELECT user_id,
UPPER(first_name) AS first_name,
LOWER(email) AS email,
CONCAT(first_name, ' ', last_name) AS full_name,
LENGTH(description) AS desc_length
FROM Users
INSERT INTO ProcessedUsers;

Conditional Functions

CASE Expression

CASE expressions provide SQL-standard conditional logic with full support for both Searched CASE and Simple CASE syntax.

Searched CASE (Boolean Conditions)

Evaluates boolean conditions in order, returning the result of the first matching condition:

SELECT symbol, price,
CASE
WHEN price > 1000 THEN 'EXPENSIVE'
WHEN price > 100 THEN 'MODERATE'
WHEN price > 10 THEN 'CHEAP'
ELSE 'PENNY'
END AS price_category
FROM Stocks
INSERT INTO Categorized;

Simple CASE (Value Matching)

Compares an expression against multiple values:

SELECT symbol, status,
CASE status
WHEN 'ACTIVE' THEN 1
WHEN 'PENDING' THEN 2
WHEN 'INACTIVE' THEN 3
ELSE 0
END AS status_code
FROM Orders
INSERT INTO StatusCodes;

Nested CASE Expressions

CASE expressions can be nested for complex decision trees:

SELECT symbol, price, volume,
CASE
WHEN price > 100 THEN
CASE
WHEN volume > 1000 THEN 'PREMIUM_HIGH_VOL'
ELSE 'PREMIUM_LOW_VOL'
END
ELSE
CASE
WHEN volume > 1000 THEN 'BUDGET_HIGH_VOL'
ELSE 'BUDGET_LOW_VOL'
END
END AS classification
FROM Trades
INSERT INTO Classified;

CASE in WHERE Clause

Use CASE for conditional filtering:

SELECT *
FROM Transactions
WHERE CASE
WHEN amount > 10000 THEN true
ELSE false
END
INSERT INTO HighValueTransactions;

Multiple CASE Expressions

Combine multiple CASE expressions in a single query:

SELECT symbol, price, volume,
CASE
WHEN price > 100 THEN 'EXPENSIVE'
WHEN price > 50 THEN 'MODERATE'
ELSE 'CHEAP'
END AS price_tier,
CASE
WHEN volume > 1000 THEN 'HIGH_VOLUME'
WHEN volume > 500 THEN 'MEDIUM_VOLUME'
ELSE 'LOW_VOLUME'
END AS volume_tier
FROM MarketData
INSERT INTO Tiered;

CASE with Complex Expressions

Use arithmetic and logical expressions in CASE conditions:

SELECT symbol, price, volume,
CASE
WHEN price * volume > 100000 THEN 'MEGA_TRADE'
WHEN price * volume > 10000 THEN 'LARGE_TRADE'
ELSE 'SMALL_TRADE'
END AS trade_size
FROM Trades
INSERT INTO SizedTrades;
CASE Expression Tips
  • CASE evaluates conditions in order and returns the first match (short-circuit evaluation)
  • Always include ELSE to handle unmatched cases (defaults to NULL if omitted)
  • All result expressions must return compatible types
  • Simple CASE uses equality comparison; use Searched CASE for complex conditions

COALESCE

Returns the first non-null value:

SELECT user_id,
COALESCE(nickname, username, email, 'Anonymous') AS display_name
FROM Users
INSERT INTO DisplayNames;

NULLIF

Returns null if values are equal:

SELECT order_id,
NULLIF(status, 'UNKNOWN') AS valid_status,
total / NULLIF(quantity, 0) AS unit_price -- Avoid division by zero
FROM Orders
INSERT INTO Processed;

IF / IIF

Conditional value selection:

SELECT symbol,
price,
IF(price > previous_price, 'UP', 'DOWN') AS direction,
IIF(volume > avg_volume, 'HIGH', 'NORMAL') AS volume_status
FROM Trades
INSERT INTO Analysis;

Type Conversion Functions

CAST

Convert between types:

SELECT
CAST(price AS INT) AS price_int,
CAST(quantity AS DOUBLE) AS quantity_double,
CAST(timestamp AS STRING) AS timestamp_str
FROM Orders
INSERT INTO Converted;

Supported Conversions

FromToExample
INTDOUBLECAST(42 AS DOUBLE)42.0
DOUBLEINTCAST(42.9 AS INT)42
INTSTRINGCAST(42 AS STRING)'42'
STRINGINTCAST('42' AS INT)42
BOOLINTCAST(true AS INT)1

Aggregate Functions

See Aggregations for detailed coverage.

FunctionDescriptionResets on Window?
COUNT(*)Count all eventsYes
COUNT(attr)Count non-null valuesYes
COUNT(DISTINCT attr)Count unique valuesYes
DISTINCTCOUNT(attr)Count unique values (alias)Yes
SUM(attr)Sum of valuesYes
SUM(DISTINCT attr)Sum of unique valuesYes
AVG(attr)Average of valuesYes
MIN(attr)Minimum value in windowYes
MAX(attr)Maximum value in windowYes
FIRST(attr)First value in windowYes
LAST(attr)Last/most recent value in windowYes
STDDEV(attr)Standard deviation (Welford's algorithm)Yes
VARIANCE(attr)Variance of valuesYes
MINFOREVER(attr)All-time minimum (never resets)No
MAXFOREVER(attr)All-time maximum (never resets)No
Forever Aggregates

MINFOREVER and MAXFOREVER track all-time values that persist across window boundaries. Useful for tracking session highs/lows or all-time records.

Date/Time Functions

Current Time

FunctionDescription
NOW()Current timestamp in milliseconds
SELECT sensor_id,
NOW() AS processing_time,
value
FROM SensorData
INSERT INTO Timestamped;

Time Extraction

SELECT event_id,
EXTRACT(YEAR FROM timestamp) AS year,
EXTRACT(MONTH FROM timestamp) AS month,
EXTRACT(DAY FROM timestamp) AS day,
EXTRACT(HOUR FROM timestamp) AS hour,
EXTRACT(MINUTE FROM timestamp) AS minute
FROM Events
INSERT INTO TimeParts;

Time Arithmetic

SELECT event_id,
timestamp,
timestamp + INTERVAL '1' HOUR AS plus_one_hour,
timestamp - INTERVAL '30' MINUTE AS minus_thirty_min
FROM Events
INSERT INTO AdjustedTimes;

Utility Functions

NULL Handling

FunctionDescriptionExampleResult
COALESCE(a, b, ...)First non-null valueCOALESCE(NULL, 'hi')'hi'
DEFAULT(val, default)Return default if nullDEFAULT(NULL, 0)0
IFNULL(val, default)Return default if null (alias)IFNULL(NULL, 0)0
NULLIF(a, b)Return null if equalNULLIF(5, 5)NULL
SELECT
COALESCE(a, b, c, 'default') AS first_non_null,
DEFAULT(value, 0) AS value_or_zero,
IFNULL(value, 0) AS also_value_or_zero,
NULLIF(status, 'UNKNOWN') AS null_if_unknown
FROM Data
INSERT INTO Processed;
Numeric Type Widening

DEFAULT() and IFNULL() support automatic numeric type widening. You can use a LONG default with an INT value, or a DOUBLE default with a FLOAT value:

SELECT DEFAULT(int_column, 999999999999) AS widened_to_long,
DEFAULT(float_column, 3.14159265358979) AS widened_to_double
FROM Data;

Type Checking

SELECT
value,
IS_NULL(value) AS is_null,
IS_NOT_NULL(value) AS is_not_null
FROM Data
INSERT INTO Checks;

Examples

Financial Calculations

SELECT symbol,
price,
volume,
price * volume AS notional,
ROUND(price * volume / 1000000, 2) AS notional_millions,
ABS(price - previous_close) / previous_close * 100 AS pct_change
FROM MarketData
INSERT INTO Calculations;

Text Processing

SELECT order_id,
UPPER(TRIM(customer_name)) AS customer_name,
CONCAT(
SUBSTRING(phone, 1, 3), '-',
SUBSTRING(phone, 4, 3), '-',
SUBSTRING(phone, 7, 4)
) AS formatted_phone
FROM Orders
INSERT INTO FormattedOrders;

Data Cleansing

SELECT user_id,
COALESCE(NULLIF(TRIM(email), ''), 'unknown@example.com') AS email,
CASE
WHEN age < 0 THEN NULL
WHEN age > 150 THEN NULL
ELSE age
END AS valid_age,
UPPER(COALESCE(country, 'UNKNOWN')) AS country
FROM RawUsers
INSERT INTO CleanedUsers;

Best Practices

Function Usage
  1. Use COALESCE for defaults - Handle nulls gracefully
  2. Validate before CAST - Avoid runtime errors
  3. Filter early - Apply functions after WHERE when possible
  4. Use appropriate precision - ROUND for display, keep full precision for calculations

Next Steps