Window From Expression
Usually, a window is populated with a field value taken directly from the stream message fields. For example, if the message has the field “bid”, you can create a window to run stats using “bid”. However, a window can also be populated with values from an expression (like a function). For example, suppose the stream provides you a “speed” field with values in miles-per-hour, but you want to populate the window with values converted into meters-per-second. You can populate the window with values that are pre-calculated by an expression. This can be very useful. For example, if the stream messages provide GPS x,y coordinates, but you want the window aggregate stats on the distance traveled between messages, it can be achieved using an expression.
Access Rule
Requires WINDOW, STREAM or ADMIN role.
Syntax
FROM NUMBER ( <expression returning a numeric value> )
FROM STRING ( <expression returning a string value> )
Scalar Functions are supported in the expression.
Note, that the expression must contain at least one stream.field, because RioDB still needs to know what stream populates this window.
For example, the following will not work, since the static value provided does not tell RioDB which stream is feeding this window.
FROM NUMBER ( 1 )
Example
CREATE WINDOW last_1000_bids
RUNNING min, max, avg, slope
FROM NUMBER ( my_stock_stream.bid * 1.185)
RANGE 1000;
CREATE WINDOW last_1000_bids
RUNNING min, max, count_distinct
FROM STRING ( CONCAT( my_stock_stream.symbol, '-USA' ) )
RANGE 1000;
Important Considerations:
Your expression can raise errors like division by zero, or string index out of bounds. When that’s the case, the message will be discarded and will not enter the window.