StreamBase Documentation
StreamBase Expression Language and Functions
This topic describes the StreamBase expression language and each of the built-in functions that you can use with StreamBase applications. In application diagrams, you use StreamBase functions in operators (except Heartbeat, Metronome, Split, and Union, which do not take an expression). You can also use StreamBase expressions in statements within StreamSQL applications, as described in the StreamSQL Guide.
|
Evaluating Expressions
A useful tool for understanding and testing expressions is the sbd --eval command, documented in the sbd help topic. Use sbd --eval to test the results of expressions as you read this topic, and before using them in your StreamBase applications. For example:
$ sbd --eval "1e1 * (15 % -4)" (double) 30.0
Data Types
Expressions have static data types. Supported types:
boolintlongdoubletimestampstring
Booleans
In the Filter and Join operators, you will notice the term "Predicate," which is a type of an expression that always evaluates to either true or false (a Boolean).
Integers
The valid range of integers (int) is -2147483648 to 2147483647, inclusive.
Integers are always signed. So the following expression is invalid, because the number 2147483648 is invalid:
2147483648 + 0
However, integer computations wrap around unchecked, so this next expression is valid and evaluates to
-2147483648:
2147483647 + 1
The following expression is valid, because the dot (.) means that the number is treated as a floating-point value, which can have a much greater value:
2147483648. + 0.
Doubles
A double is always 8 bytes. Any numeric literal in scientific notation is considered a double. That is, you double (instead of an int). For example, 1e1 is considered to be 10.0 (double) instead of 10 (integer).
Strings
StreamBase provides support for large tuples, including large string fields. In releases prior to StreamBase 3.0.0, the maximum for an individual string field in a tuple was 32K. While the theoretical maximum for a string in a single-field tuple is now around 2 gigabytes (2 GB), of course the practical limit is much smaller. Be aware that moving huge amounts of data through any application will negatively impact throughput.
Important: Before you can use tuples that exceed the operating system's page size (such as 4096 bytes on Windows or Linux, or 8192 bytes on Solaris), you must increase the StreamBase memory parameters defined in the sbd.sbconf configuration file. The StreamBase page-size parameter must be a multiple of the system page size. You must allocate sufficient memory for the StreamBase Server process (sbd) so that it can host your application and its largest tuples. However, do so with the caveats that the operating system will be constrained by competing concurrent requests for RAM space from other processes. Thus you will need to estimate the total amount of requests for memory resources on the machine during peak loads. Always keep in mind that you can help your application's throughput by limiting the size of input tuples to the bare minimum. Keeping the input tuples as small as reasonably possible is always more efficient than dropping tuples during the processing of the StreamBase application.
For details about the memory parameters, please see the StreamBase Server Configuration XML: <page-pool> element topic in the Reference Guide. For general ideas about tuning StreamBase applications, please see the Tuning Guide.
Null Literals in Expressions
You can use nulls in the StreamBase expression language. Starting in StreamBase 2.0.0, we introduced five null literals, one for each type:
int(null) double(null) bool(null) string(null) timestamp(null)
Each null literal is case sensitive; for example, Int(NULL) is invalid. The data type of each null is never implicit: you must specify which type of null you are using in the expression.
In general, when you apply any arithmetic operator or function with <data-type>(null) as one of the arguments, the result is null. Three exceptions are the isnull() and notnull() functions, which test whether an expression evaluates to null, and the coalesce() function, which selects a non-null value from a list of potentially null arguments.
| Expression Example... | Result |
|---|---|
3 + int(null) |
A null int |
int(null) + int(null) |
A null int |
int(null) + bool(null) |
A typecheck error. You cannot add an int and a bool. |
if bool(null) then 3 else 4 |
A null int |
int(null) == int(null) |
A null bool, because null is not equal to itself |
int(null) != int(null) |
A null bool, because null is not equal to itself |
isnull(int(null)) |
A bool that evaluates to true |
notnull(int(null)) |
A bool that evaluates to false |
For related information, please see Using Nulls in StreamBase Applications.
Case Sensitivity
Expressions are case sensitive. Function names should be all lowercase.
Identifiers
Only use alphabetic characters, numbers, and underscores. The first character must be alphabetic or an underscore. Do not use hyphens ( - ) or other special characters. Identifier rules apply to the name you assign to any StreamBase component, including schemas, fields, operators, tables, lock sets, and modules.
Note: the StreamBase software creates identifiers that include colons, but you may not use colons in your identifiers.
Reserved Words
When you name fields, do not use the following reserved words:
truefalseifthenelsenullintlongbooldoubletimestampstring
In an Input Stream's Properties View, StreamBase Studio will not prevent you from specifying a reserved word for a field name. However, a schema that contains reserved words will cause one or more typecheck errors in the connected downstream components. The error message in the Typecheck View will identify the offending field's name. Rename the field in the Input Stream's Property View.
Qualifying Field Names
If a name conflict occurs in a field name that's used in an expression within an operator instance, you can qualify them to clarify the inbound tuple stream to which you are referring.
In the Properties view for a Gather operator, you can qualify the fields as input[port-number].field-name.
Example: input1.is_alarm, input2.is_alarm, input3.is_alarm.
In the Properties view for a Join operator, you can qualify the fields in the
two input streams as input1.field-name and
input2.field-name. The input1.field-name
refers to a field in the stream arriving at the "top" port (#1). The input2.field-name refers to a field in the stream arriving at the "bottom" port (#2).
Examples:
input1.SKU input2.SKU
In the Properties view for a Query operator, you can qualify the fields in the two input streams as input.field-name and old.field-name. The input.field-name refers to the current input tuple, while old.field-name refers to the field's old (prior) value.
Examples:
Table1_current_Name Nasdaq100Table_old_Symbol
Arithmetic Operators
You may use +, -, *, /, and %. The ^ expression is not supported.
For example, in the Compliance sample that we provide with the StreamBase kit, the is_alarm field is a bool. In one of the Compliance application's Map operators, we use this expression for the is_alarm check on each tuple:
((((sector_in_fund_value_t + (shares_traded * price_at_trade)) * 100) / fund_value_t) > 25)
This expression is designed to execute the 25% sector test; that is, one sector is not allowed to be more than 25% of the total fund value. is_alarm will be set to true if the test fails. If you are interested in viewing the full sample, use File > Load Samples from the StreamBase Studio top menu, and select Compliance from the list.
Modulus Operators
The modulus operator ( % ) finds the remainder of a division operation.
Division and modulus are always related such that a/b*b+(a%b) == a, where a/b is an integer division.
For example, 15%4 can be resolved as follows:
as:
15 / 4 * 4 + MOD == 15
3 * 4 + MOD = 15
12 + MOD = 15
MOD = 3
This relationship is preserved with negative divisors. As a result, whenever the quotient is negative, the modulus is negative. For example:
15 % 4 = 3
15 % -4 = 3
-15 % 4 = -3
-15% -4 = -3
Also note that if you divide a smaller number by a larger number, the modulus is always the smaller number. For example:
4 % 15 = 4
This behavior may be different than modulus in some programming languages. Hint: The easiest way to understand StreamBase modulus operations is to try different expressions out using the sbd --eval command.
Unary Operators
Unary operators act on only one operand in an expression. +a and -a are valid for integer and double types.
Relational Operators
You may use the <, <=, = or ==, >, >=, and != relational operators. The relational operator <> is not supported.
Logical Operators
You may use && or AND, || or OR, and !.
| Logical Operator | Meaning |
|---|---|
&& or AND |
AND |
|| or OR |
OR |
! |
NOT |
Note: The evaluation of expressions using "&&" and "||" will short-circuit. This means that after StreamBase encounters the first term that evaluates as false, no other terms in a statement using "&&" will be evaluated. Similarly, evaluation of a statement using "||" will stop after the first true term.
For related information on Boolean logic and nulls, see Effect of null values in Boolean logic.
Precedence Order
The precedence order of mathematical operators in expressions, from highest to lowest, is as follows:
- Unary operators:
!and-The logical negation operator,
!, reverses the meaning of its operand.The unary negation operator,
-, produces the negative of its operand. - Multiplicative operators:
*and/and%Multiplication:
*Division:
/Modulus, the remainder from division:
% - Additive operators:
+and-Addition:
+Subtraction:
- - Relational operators: < and <= and > and >=
Less than:
<Less than or equal to:
<=Greater than:
>Greater than or equal to:
>= - Equality operator:
==or= - Not equal:
!= - Logical AND:
&&orAND - Logical OR:
||orOR
You can use parentheses in expressions to override the default precedence.
Casting
In an expression, to cast a value to a double, use the double() function.
To cast a value to a string, use the string() function.
To cast a value to a Boolean, use the bool() function.
The bool() function treats numeric (int or double) 0 as false; all other
numeric values are cast to true. A "true" string (case-insensitive)
maps to true and a string of "false" maps to false.
All other strings are errors.
White Space
The StreamBase parser ignores any spaces, new lines, and tab characters in expressions.
String Literals
You can use single quotes ( 'string' ) or double quotes ( "string" ) around strings. Escape characters are supported, as follows:
| Character | Results in... |
|---|---|
| \" | Double quotation mark |
| \' | Single quotation mark |
| \n | Newline |
| \t | Tab |
| \b | Backspace |
| \r | Carriage return |
| \\ | Backslash |
Examples:
| String Literal | Results in... |
|---|---|
"He said, \"Hello.\"" |
He said, "Hello." |
'She said, \'Hello.\'' |
She said, 'Hello.' |
"A\tB" |
A<tab>B |
"A\nB" |
A<newline>B |
"C:\\WINDOWS" |
C:\WINDOWS |
Boolean Literals
The Boolean literals true and false are supported.
Conditional Expressions
In an expression such as: if p then e1 else e2, p must be a valid bool expression, and both e1 and e2 must be expressions that have the same type. If p is true, then e1 will be evaluated and the result returned. If p is false, e2 will be evaluated and the result returned. In either case the other sub-expression is not evaluated.
Note: In the StreamBase expression language, each if clause must have a pair of explicit then and else clauses. Should you create compound if clauses, to avoid ambiguity, remember to specify the then and else clauses for each if clause. For an example, please see the next section.
Compound Conditional Expressions
You can use combinations of if...then and else if...then statements to form compound conditional expressions. For example:
if i==0 then "Buy" else if i==1 then "Sell" else if i==2 then "Hold" else "None of the above"
Here is a second example (indented for clarity), where we nest an "if then else" in a then clause:
if p1
then
if p2
then 1
else 2
else
if p2
then 3
else 4
Notice how each if clause contains a then clause and an else clause.
Concatenation in Expressions
You can enter an expression that performs concatenation. For a string variable, the concatenation expression is stringvar + x. For example: b + 25. For a numeric variable with a static string, the concatenation expression is "staticstring" + numvar. For example: "foo" + a. For two variables, the concatenation expression is stringvar + numvar. For example: b+a. Note that when you use an expression to concatenate a number to a string, the total size becomes the original string size + 12.
Timestamp Expressions
Timestamp expressions are represented in the following date/time format:
YYYY-MM-DD hh:mm:ss
The maximum timestamp precision is one millisecond.
StreamBase parses timestamps as follows:
| "2005-10-08" | Implicit time of "00:00:00", local time zone |
| "2005-10-08 13:17" | Implicit seconds of "00", local time zone |
| "2005-10-08 13:17:23" | Local time zone |
| "2005-10-08 13:17:23.12355" | Fractional seconds |
| "2005-10-08 13:17:23" | == "2005-10-08 13:17:23" |
| "2005-10-08 24:00:00" | == "2005-10-09 00:00:00" |
| "2005-10-08 13:17:23-0500" | Explicit time zone (EST in this case) |
| "2005-10-08 13:17-05:00" | Explicit time zone, implicit seconds |
When used with comparison operators ( == != > < <= >= ) you must compare time interval-to-interval, or timestamp-to-timestamp. You cannot use the comparison operators with interval-to-timestamp.
To express constant intervals, you can use the seconds() and minutes() functions. For example, if you want to add 60 seconds to a timestamp, you can enter expressions such as:
t + seconds(60) t + minutes(1)
Parameters in Expressions
You can declare operator-parameters in the sbd.sbconf file, and then use them as parameters within StreamBase expressions. StreamBase Server Configuration XML explains how to declare operator-parameters.
To then reference a parameter in an expression, wrap the operator-parameter name in braces and prefix the open brace with a dollar sign. If you are referencing an operator-parameter that is defined as a string, use quotes around the entire reference.
For example, consider an sbd.sbconf file that defines these two operator-parameters:
<operator-parameters>
<operator-parameter name="MyInt" value="2"/>
<operator-parameter name="MyString" value="somestring"/>
</operator-parameters>
You could reference the first parameter (for example, in an output field) using an expression like 35* ${myInt}. The expression would evaluate at run time to 70. This StreamSQL statement references the second parameter:
SELECT * FROM InputStream1 "${MyString}" AS source
Notice the quotation marks: they are needed so that the expression is resolved as a string.
Note: Expression parameters are distinct from both module parameters, which are described in Using Module Parameters, and StreamSQL CREATE PARAMETER statements:
- A module parameter is defined only for a parameterized module (or, in StreamSQL, a Java operator or an embedded Java adapter), not globally in the sbd configuration.
- If you use an expression parameter and a module parameter with the same name, the module parameter value takes precedence.
- Expression parameters can only be used within expressions. StreamSQL parameters are more flexible: they can be used to represent expressions as well as other parts of a statement.
Note: Expression parameters can be referenced in both EventFlows and StreamSQL applications. However, if you convert an EventFlow .sbapp file to StreamSQL, any expression parameters are resolved during the conversion; the parameter itself is not preserved.
StreamBase Functions
This section provides reference information for all the StreamBase functions. StreamBase provides two types of functions:
Simple functions are evaluated over one set of arguments, and return a single result. The strlen() function is an example of a simple function. Simple functions can be used in expressions for any StreamBase operator, except Heartbeat, Metronome, and Union, which do not use an expression.
Aggregate functions are evaluated over multiple sets of arguments and return a single result. In StreamBase applications, aggregate functions are used only in Aggregate operators and in Query operators that do query read operations. The avg() function, which calculates an average value for tuples in an Aggregate window, is an example of an aggregate function.
function_name(arg1,arg2,arg3,...)
Alphabetical Index to All StreamBase Functions
For your convenience, here is a complete, alphabetized index into the available functions provided by StreamBase, with links to the function descriptions in this section. Note that if you do not find a built-in function that you need here, you can use the StreamBase custom function APIs to implement your own function and configure your application to use it. For details, please see these topics in the API Guide:
| Function Name & Link to Section | Summary Comment |
|---|---|
| abs function | Simple function: math |
| acos function | Simple function: math |
| alpha function | Aggregate function: statistical calculations |
| avg function | Aggregate function: statistical calculations |
| asin function | Simple function: math |
| atan function | Simple function: math |
| atan2 function | Simple function: math |
| beta function | Aggregate function: statistical calculations |
| black_scholes function | Simple function: financial |
| bool function | Simple function: type conversions |
There are two versions of callcpp:
|
Simple function: C++ API call Aggregate function: C++ API call |
There are two versions of calljava:
|
Simple function: Java API call Aggregate function: Java API call |
| catchexception function | Simple function: check for errors in expression |
| cbrt function | Simple function: math |
| closeval function | Aggregate function: for windowing |
| coalesce function | Simple function: working with nulls |
| compound_interest | Simple function: financial |
| correlation_coefficient function | Aggregate function: statistical calculations |
| correlation_coefficientp function | Aggregate function: statistical calculations |
| cos function | Simple function: math |
| count function | Aggregate function: statistical calculations |
| count_distinct function | Aggregate function: statistical calculations |
| covariance function | Aggregate function: statistical calculations |
| covariancep function | Aggregate function: statistical calculations |
| date function | Simple function: creating absolute timestamps |
| days function | Simple function: creating interval timestamps |
| double function | Simple function: type conversions |
| error function | Simple function: error |
| exp function | Simple function: math |
| exp_moving_avg function | Aggregate function: statistical calculations |
| firstval function | Aggregate function: for windowing |
| floor function | Simple function: math |
| from_gmtime function | Simple function: creating absolute timestamps |
| from_localtime function | Simple function: creating absolute timestamps |
| get_day_of_week function | Simple function: working with timestamp fields |
| get_day_of_month function | Simple function: working with timestamp fields |
| get_hour function | Simple function: working with timestamp fields |
| get_minute function | Simple function: working with timestamp fields |
| get_month function | Simple function: working with timestamp fields |
| get_second function | Simple function: working with timestamp fields |
| get_year function | Simple function: working with timestamp fields |
| hash function | Simple function: type conversions |
| hours function | Simple function: creating interval timestamps |
| indexof function | Simple function: strings |
| int function | Simple function: type conversions |
| intercept function | Aggregate function: statistical calculations |
| interval function | Simple function: creating absolute timestamps |
| isnan function | Simple function: NaN (not a number) |
| isnull function | Simple function: detecting null values |
| lastindexof function | Simple function: strings |
| lastval function | Aggregate function: for windowing |
| ln function | Simple function: math |
| log10 function | Simple function: math |
There are two versions of max():
|
Simple function: math Aggregate function: statistical calculations |
There are two versions of min():
|
Simple function: math Aggregate function: statistical calculations |
| minutes function | Simple function: creating interval timestamps |
| notnan function | Simple function: detecting NaN (not a number) |
| notnull function | Simple function: detecting null values |
| now function | Simple function: creating absolute timestamps |
| openval function | Aggregate function: for windowing |
| pow function | Simple function: math |
| product function | Aggregate function: statistical calculations |
| seconds function | Simple function: creating interval timestamps |
| set_day_of_month function | Simple function: working with timestamp fields |
| set_day_of_week function | Simple function: timestamp |
| set_hour function | Simple function: working with timestamp fields |
| set_minute function | Simple function: working with timestamp fields |
| set_month function | Simple function: working with timestamp fields |
| set_second function | Simple function: working with timestamp fields |
| set_year function | Simple function: working with timestamp fields |
| sin function | Simple function: math |
| slope function | Aggregate function: statistical calculations |
| sqrt function | Simple function: math |
| stdev function | Aggregate function: statistical calculations |
| stdevp function | Aggregate function: statistical calculations |
| strftime function | Simple function: creating absolute timestamps |
| string function | Simple function: type conversions |
| strlen function | Simple function: strings |
| strpinterval function | Simple function: creating absolute timestamps |
| strptime function | Simple function: creating absolute timestamps |
| strresize function | Simple function: strings |
| strresizetrunc function | Simple function: strings |
| substr function | Simple function: strings |
| sum function | Aggregate function: statistical calculations |
| tan function | Simple function: math |
| time function | Simple function: creating absolute timestamps |
| timestamp function | Simple function: type conversions |
| today function | Simple function: creating absolute timestamps |
| to_seconds function | Simple function: working with timestamp fields |
| variance function | Aggregate function: statistical calculations |
| variancep function | Aggregate function: statistical calculations |
| vwap function | Aggregate function: statistical calculations |
| weeks function | Simple function: creating interval timestamps |
Simple Functions
This section describes the simple functions that you can use in expressions for any StreamBase operator, except the Heartbeat, Metronome, and Union operators, which do not accept an expression.
This section is organized according to categories of simple functions:
- Simple Functions: Financial
- Simple Functions: Math
- Simple Functions: NaN
- Simple Functions: Nulls
- Simple Functions: Errors
- Simple Functions: Strings
- Simple Functions: Timestamps
- Simple Functions: Type Conversions
- Simple Functions: C++ API Call
- Simple Functions: Java API Call
Simple Functions: Financial
black_scholes(type, underlying, strike, dividendYield, riskFreeInterestRate, Volatility, exerciseDate, dealDate)The
black_scholesfunction calculates fair-value and risk statistics (delta, gamma, vega...) for European style options on securities with continuous dividend yields. This is known as the "Black-Scholes Generalized" model. It also calculates implied volatility.The
black_scholesfunction takes eight input arguments; the first seven arguments are required, while the eighth argument,dealDate, is optional.The arguments are:
type: Required. Should be set to one of the followingstringvalues:Call(The price of an option.)Put(The price of an option.)DeltaCall(The sensitivity of the price of an option to changes in the price of the stock.)DeltaPut(The sensitivity of the price of an option to changes in the price of the stock.)ThetaCall(Theta measures how the price of an option changes with time.)ThetaPut(Theta measures how the price of an option changes with time.)Gamma(The sensitivity of a stock's delta to the stock price.)Vega(The rate of change of the value of an option with respect to the volatility of the stock's price.)RhoCall(The sensitivity of the price of an option with respect to the risk-free interest rate.)RhoPut(The sensitivity of the price of an option with respect to the risk-free interest rate.)ImpliedVolatilityCall(Implied volatility of the underlying stock for a given price.)ImpliedVolatilityPut(Implied volatility of the underlying stock for a given price.)
underlying: Required. Adouble. The price of the underlying stock.strike: Required. Adouble. The strike price of the stock on the exercise date.dividendYield: Required. Adouble. For example,0.03for 3.0%.riskFreeInterestRate: Required. Adouble. For example,0.05for 5.0%.Volatility(orvalue): Required. Adouble. This input argument is overloaded. When computing anything except the implied volatility for a given call or put price this argument is the volatility of the stock. For example,0.2for 20%. When computing implied volatility this argument is the option price.exerciseDate: Required. Atimestamp. The option exercise date.dealDate: Optional. Atimestamp. The option deal date. If not provided, the current date is the default.
compound_interest(principleValue, matureValue, numberOfPeriods, guess)Returns the compound interest given the principle value, mature value, and number of periods. An optional fourth argument,
guess, can be used to set the initial compound interest used by the function. Thecompound_interestfunction uses the Newton-Raphson algorithm to compute compound interest rate. This algorithm performs the same calculations repetitively. Each iteration results in a compound interest that is closer to the final result. The number of iterations required to get the results within an acceptable inaccuracy (1.0e-4) depends on the number of periods and the initial compound interest used to start the calculation.If the
guessargument is not provided, the default initial value is 0.01 (1%). If the function returns a NaN (means"not a number"), you can provide the initial compound interest in this fourth argument. If the initial value is closer to the actual rate the computation, it can be sped up significantly.
Simple Functions: Math
abs(e)Returns the absolute value of the double or integer Expression
e. The return type is the same as the expression's type.acos(x)Calculate in radians the arc cosine of
x, that is the value whose cosine isx. Undefined outside of the range -1 to 1.asin(x)Calculate in radians the arc sine of x, that is the value whose sine is x. Undefined outside of the range -1 to 1.
atan(x)Calculate in radians the arc tangent of x, that is the value whose tangent is x.
atan2(x, y)Calculate in radians the arc tangent of two variables, x and y. This is similar to calculating the arc tangent of (y/x), except that the signs of both arguments are used to determine the quadrant of the result. tangent is x.
cbrt(x)cos(x)exp(x)Calculate the value of e (the base of natural logarithms) raised to the power of x.
floor(d)Returns the largest (closest to positive infinity) integer value not greater than the double value
d.ln(x)log10(x)max(e1,...,en)Returns the maximum value from an expression, where
e1throughenmust all be either integer or double expressions. The return type is the same as its inputs.min(e1,...,en)Returns the minimum value from an expression, where
e1throughenmust all be either integer or double expressions. The return type is the same as its inputs.pow(x, y)Calculate the value of
xraised to the power ofy.sin(x)Calculate the sine of
x, wherexis given in radians.sqrt(x)Calculate the non-negative square root of x.
tan(x)Calculate the tangent of
x, wherexis given in radians.
Simple Functions: NaN
The functions in this section detect NaN (not a number) values. They require one input argument and
return a bool. The input argument data type can be int, double, timestamp, or null.
bool and string are not valid data types.
isnan(value)- Returns
trueif the input argument is NaN - Returns null if the input argument is null
- Returns
falseotherwise
- Returns
notnan(value)- Returns
falseif the input argument is NaN - Returns null if the input argument is null
- Returns
trueotherwise
Note that
notnanreturns the opposite ofisnan, except if the argument is null, in which case they both return null.- Returns
Simple Functions: Nulls
isnull(value)Returns true if the argument is a null value.
notnull(value)Returns true if the argument is not a null value. This function always returns the opposite of
isnull(value).coalesce(value1, value2...)Returns the first argument that is non-null, or a
nullvalue if all arguments are null. All arguments must have the same type.
Simple Functions: Errors
catchexception(v1, v2, ...)Attempts to evaluate all arguments in order, returning the first one that evaluates without an error, or null if all arguments evaluate to an error. All arguments must have the same type.
For instance, the following attempts to parse
stras an interval, but returns a null timestamp if parsing fails:catchexception(interval(str))The following attempts to divide
abyb, but returns-1if the division fails:catchexception(a/b, -1)error(s)Throw an error with message
s. In the default configuration, this shuts down the server.
Simple Functions: Strings
In addition to the functions in this section, see the string() function that is in the Type Conversions section.
indexof(haystack, needle, [start])Returns the index of the first instance of the string
needlewithin the stringhaystack, starting at positionstart. Returns -1 if the string is not found. Ifstartis unspecified, it is taken to be 0, the start of the string.lastindexof(haystack, needle, [lastStart])Returns the index within
haystackof the rightmost instance ofneedle, for which the index is less than or equal tolastStart. Thus, the return value will always be less than or equal tolastStart. Returns-1if theneedleis not found. IflastStartis omitted, the entire string is searched.strlen(s)Returns the length of the string
s.strresize(string, length)All StreamBase fields have a fixed maximum size; this function lets you change the maximum size of a string field. The function returns the original string, unmodified, but with its maximum size set to the given length. If this function is called with a string whose length exceeds the given length, a runtime error is triggered and evaluation is aborted. The length must be an integer constant. Note that this function does not pad the original string with spaces if it is smaller than the given length; the actual string value is returned unmodified.
strresizetrunc(string, length)Similar in function to
strresize, but strings that are too large are truncated, rather than triggering an evaluation error.substr(s, begin, length)Returns a substring of the string
s, starting at characterbegin(zero-indexed) with a size oflengthcharacters.Note: Prior to StreamBase 2.0.0, the
substrfunction used these arguments:substr(s, begin, end). Please check your StreamBase applications, and ifsubstr()is used, make the appropriate modifications to the third argument's value.
Simple Functions: Timestamps
Creating absolute timestamps:
date(ts)Return a timestamp value for the beginning of the day containing the timestamp
ts(midnight local time). For instance, given the timestamp2005-07-15 18:01:17.918in the local time zone, this function will return2005-07-15 00:00:00.000.from_gmtime(year, month, dayOfMonth, hour, minute, second)Create a timestamp in the UTC (GMT) timezone, from the given parameters. For an example, please see the
from_localtimefunction.from_localtime(year, month, dayOfMonth, hour, minute, second)Create a timestamp in the local timezone, from the given parameters.
The following example covers the
from_gmtimeandfrom_localtimefunctions. Thefrom_gmtimefunction can be used to create a timestamp in the UTC (GMT) timezone. For example, the timestamp returned from the following function:from_gmtime(1904, 6, 16, 16, 30, 0)
...is "
1904-06-16 16:30:00.000+0000". Notice the UTC timezone on the printed representation of the timestamp. If this timestamp were printed on a machine in the US/Eastern timezone, the representation would be1904-06-16 12:30:00.000-0400.This function could be used to create a constant timestamp that you would add to an interval to get a timestamp. For example, to convert from "unix time" (represented as an integer number of seconds since the epoch, which is January 1, 1970 at 00:00:00), you could use an expression like the following:
from_gmtime(1970, 1, 1, 0, 0, 0) + seconds(unixtime)
Or, given a stream that contains a broken-down date as multiple fields, the date could be re-composed, using an expression like:
from_gmtime(year, month, dayOfMonth, hour, minute, second)
The
from_localtimefunctions can be used similarly, with the corresponding dates coming out in the local timezone. For example, in the US/Eastern timezone, this function call:from_localtime(1904, 6, 16, 16, 30, 0)
yields:
1904-06-16 16:30:00.000-0400
interval(str)Parse
stras an interval, with the time format%H:%M:%S!.!%f. For example, you could use this function to parse intervals with any of these formats:18:01:17.91818:01:17.18:01:17
now()Returns a
timestampvalue representing the current time.Note that in the StreamBase Server sbd.sbconf configuration, you can select one of two
now()implementations. The value is set in the<runtime>section:<param name="now-implementation" value="system"/>
Specify one of these values, as appropriate for your StreamBase application:
Value Meaning systemThe now()function will use Java'sSystem.currentTimeMillis(). This is the default.thread The now()function will use a background thread that checks the time approximately every millisecond. This option will result in decreased accuracy, but may be more efficient than "system" if you callnow()more frequently than 1000 times per second.strptime(str, format)Parse
strwith the provided time format. The time format is the same as that used by thestrptimelibrary call described in the Open Group's Single Unix Specification. Additionally:- You may use the
%fformat specifier to represent the decimal portion of a timestamp's seconds value. - You may use
!(an exclamation point) in the format to indicate that the remainder of the input string is optional.
For example, you could use:
strptime(value, "%Y-%m-%d %H:%M:%S.%f")...to parse a timestamp string like
2005-07-15 18:01:17.918. Or you could use:strptime(value, "%Y-%m-%d %H:%M:%S!.%f")...to parse a timestamp string either like
2005-07-15 18:01:17.918or2005-07-15 18:01:17.strpinterval(str, format)Parse
strwith the provided time format, returning an interval. The time format is similar to that used bystrptime, except that only%H,%M,%S,%f, and!may be used. For example, you could use:strpinterval(value, "%H:%M:%S.%f")...to parse a timestamp string like
18:01:17.918.strftime(format, time)This function formats
timewith the provided time format. The time format is the same as that used by thestrftimelibrary call described in the Open Group's Single Unix Specification. Additionally, you may use the%fformat specifier to represent the decimal portion of a timestamp's seconds value. For example, you could use:strftime("%Y-%m-%d %H:%M:%S.%f", value)...to format a timestamp string like
2005-07-15 18:01:17.918.time(ts)Return an interval corresponding to the amount of time that, at time
ts, will have elapsed since the start of the day. For instance, given the timestamp2005-07-15 18:01:17.918in the local time zone, this function will return the interval 64877.918 (i.e., 64877.918 seconds since midnight).today()Return a timestamp value for the beginning of the current day (midnight local time).
Creating interval timestamps:
seconds(x)Returns a
timestamprepresenting an interval of x seconds.minutes(x)Returns a
timestamprepresenting an interval ofxminutes.hours(x)Returns a
timestamprepresenting an interval ofxhours.days(x)Returns a
timestamprepresenting an interval ofxdays.weeks(x)Returns a
timestamprepresenting an interval of x weeks.
Working with timestamp fields:
get_second(x)Returns the seconds portion of a
timestampxget_minute(x)Returns the minutes portion of a
timestampxget_hour(x)Returns the hours portion of a
timestampxget_day_of_week(x)Returns the day of the week represented by a
timestampx. The return value is between 0 (Sunday) and 6 (Saturday). Day 7 is also considered to be Sunday.get_day_of_month(x)Returns the day of the month represented by a
timestampx. The return value is between 1 and 31.get_month(x)Returns the month represented by a
timestampx. The return value is between 1 (January) and 12 (December).get_year(x)Returns the year represented by a
timestampx.set_second(t, x)Return a modified version of the
timestampt, with the seconds part changed to x.set_minute(t, x)Return a modified version of the
timestampt, with the minutes part changed to x.set_hour(t, x)Return a modified version of the
timestampt, with the hours part changed to x.set_day_of_month(t, x)Return a modified version of the
timestampt, with the day of month part changed to x.set_day_of_week(t, i)Return a new timestamp that has the same value as timestamp
t, but with the day of the week set to the value ofi. Days of the week are numbered from 0 to 6, Sunday through Saturday. Day 7 is also considered to be Sunday.set_month(t, x)Return a modified version of the
timestampt, with the month part changed to x.set_year(t, x)Return a modified version of the
timestampt, with the years part changed to x.to_seconds(t)Converts a timestamp to seconds. If the timestamp is an interval, returns the number of seconds in the interval. If the timestamp is an absolute time, it will be the number of seconds since the epoch, Jan 1, 1970. For example, the timestamp
converts toWed Apr 21 16:46:37 20041082580397, and the intervalseconds(12)converts back to 12.
Simple Functions: Type Conversions
bool(e)Converts
eto a value of typeboolwhereeis abool,int,double, orstring.- An
intvalue of 0 will return the Booleanfalse. All otherintvalues, positive or negative, will returntrue. - A
doublevalue of 0 will return the Booleanfalse. All otherdoublevalues, positive or negative, will returntrue. - A
stringvaluefalsewill return a Booleanfalseand the stringtruewill return Booleantrue. The string is compared in a case-insensitive manner. For example,bool("FALSE")andbool("false")are equivalent. Any string other thanfalseortrueis an error. - A
boolvalue will return itself.
- An
double(e)Converts
eto a value of typedoublewhereeis abool,int,double, orstring.- An
intvalue will return the same value, but with typedouble. For example,double(3)returns3.0. - A
doublevalue will return that samedoublevalue. - A
stringvalue will be parsed as a decimal number. For example,double("123.456")returns123.456, butdouble("7abc")is an error. Scientific notation is supported, sodouble("1.2E4")returns12000.0. - A
boolvalue will return1.0if true, or0.0if false. - For
timestampvalues, you should use theto_seconds()function instead.
Also See: The to_seconds() function, used to convert timestamps to double.
- An
hash(s)Uses a hashing algorithm to convert a string
sto an integer. For example, you could use thehashfunction to convert a stock symbol to an integer, so that the StreamBase data parallelism feature can distribute incoming tuples across the multiple instances of a module. Data parallelism bases the distribution on a tuple field that you designate, and that field must be of typeinteger.int(e)Converts
eto a value of typeintwhereeis abool,int,double, orstring.- An
intvalue will return that sameintvalue. - A
doublevalue will have its fractional part truncated. For example,int(3.4)returns3. - A
stringvalue will be parsed as a decimal number. For example,int("123")returns123, butint("7abc")is an error. - A
boolvalue will return1if true, or0if false.
- An
string(e)Converts
eto a value of typestringwhereeis abool,int,double,string, ortimestamp.- An
intvalue will return the string representation of that value. - A
doublevalue will return the string representation of that value. - A
stringvalue will return itself. - A
boolvalue will return the stringtrueif true, orfalseif false. - A
timestampvalue, if represented as a date/time, will return a string which includes the explicit time zone. A timestamp represented as a numeric value will return the string representation of that value.
- An
timestamp(s)Return a
timestampparsed from the arguments, a string representation of a date.Also See: The strptime() function, used to convert strings to timestamps.
Simple Functions: C++ API Call
callcpp("function" [,arg...])Runs a C++ custom function directly from a StreamBase operator. Custom C++ functions can be accessed by the simple form of callcpp in any expression except in aggregate expressions. (To use callcpp in aggregate expressions, refer to Aggregate Functions: C++ API Call.)
To learn about coding custom C++ functions, refer to Creating Custom C++ Functions in the API guide.
Simple Functions: Java API Call
calljava("class", "method" [,arg...])Runs a Java method directly from the StreamBase expression language.
The first two arguments must be a public class and public static method in a Java application that you have imported.
Custom Java functions can be accessed by the simple form of calljava in any expression except in aggregate functions. (To use calljava in aggregate functions, refer to Aggregate Functions:Java API Call.)
To learn about coding custom Java functions, refer to Creating Custom Java Functions in the API guide.
Note: A StreamBase string is not the same as a java.lang.String. No encoding is used when passing it as a byte[]. The raw data that was transmitted through StreamBase is used.
Return Types and Argument Types
There can be any number of arguments (including none). Each argument associates a StreamBase type with one of the following primitive or object Java types:
| StreamBase Type | Java Primitive | Java Object |
|---|---|---|
bool |
boolean |
Boolean |
int |
int |
Integer |
double |
double |
Double |
timestamp |
com.streambase.sb.Timestamp |
None: use the Java primitive |
string |
byte[] |
String |
Notes:
- If any primitive value is null, the custom function is not called. However, object arguments can be used to pass in null values to the custom function.
- In general, use the primitive form if speed is important; use objects if you need to specify null values.
Using strresize for Java Methods That Return byte
When using calljava to invoke a Java method that returns a byte[], you
may need to use the strresize() function provided by StreamBase, so that the output length of the string is known. For example, if you attempt to use the following expression as a field value in a Map operator:
calljava('com.mycompany.MyMethod', 'myFunction', abc)
...and myFunction is declared as returning a byte[], you will
encounter an "Unable to determine string length" typecheck error. Use
strresize to specify the maximum length of the output, so the operator's
output schema can be determined. For example:
strresize(calljava('com.mycompany.MyMethod', 'myFunction', abc)
Note: A StreamBase string is not the same as a java.lang.String. No encoding is used when passing it as a byte[]. The raw data that was transmitted through StreamBase is used.
Aggregate Functions
This section is organized as follows:
- Introduction
- Aggregate Functions for Windowing
- Aggregate Functions for Statistical Calculations
- Aggregate Function for C++ Call
- Aggregate Function for Java API Call
Introduction
Unlike simple functions, which operate on a single field, aggregate functions evaluate columns of data from windows or tables, and return a single result. Aggregate functions can be used only within aggregate expressions in Aggregate operators, or within output expressions in Query Operators that do read operations. In StreamSQL applications, aggregate functions can be be used in SELECT statements related to aggregate or query read operations.
Aggregate expressions can contain a single aggregate function, or they can combine multiple aggregate functions together with fields from an input stream or an associated Query Table, and constants. The following screen illustrates:

Here is the equivalent expression in a fragment of a StreamSQL SELECT statement:
SELECT count() * avg(price) AS cost
Aggregate Functions: Windowing
Windowing functions work on columns of data from either aggregate windows or read operations on tables.
firstval(f)Returns the first value for field
fin a window or table column.lastval(f)Returns the last value for field
fin a window or table column.openval("dimension")Can only be used in an Aggregate operator, not in a Query Operator. Returns the lower limit of the specified dimension for the current window. The dimension name is passed as a quoted string, and must match the name of a dimension in the Aggregate operator. This function works even if the dimension specified is not the one that actually opened the current window; in that case the value returned is the value of the dimension at the time the window was created.
Note that the value returned by openval might different from any of the the actual tuple values included in the window. For example, consider an Aggregate operator with a dimension CountDim, where the windows created by that dimension include a window with a range of 5 to 10. If the actual tuple values in the window are 6, 7, and 9, then openval(CountDim) will return 5 for that window, not 6.
closeval("dimension")Can only be used in an Aggregate operator, not in a Query Operator. Returns the upper limit of the specified dimension when the window is closed. The dimension name is passed as a quoted string, and must match the name of a dimension in the Aggregate operator. This function works even if the dimension specified is not the one that actually closed the current window; in that case the value returned is the value of the dimension at the time the window was closed.
Note that the value returned by closeval might different from any of the actual tuple values included in the window. For example, consider an Aggregate operator with a dimension CountDim, where the windows created by that dimension include a window with a range of 5 to 10. If the actual tuple values in the window are 6, 7, and 9, then closeval(CountDim) returns 10 for that window, not 9.
Aggregate Functions: Statistical Calculations
alpha(index, price, dividend)Returns a double that may indicate the part of a stock's movement that is independent of the Index's movement.
Examples of stocks increasing in
alphacould be those with take-over rumors, under strong syndicate manipulation, or having strong expectations of good results; that is, factors that make them increasingly move independently off the Index.The beta of a stock is defined as the slope of a regression line in a scatter graph of paired data points representing percentage changes of an index (return of an index) and the corresponding change (return of stock) in the price of a stock. The StreamBase
betafunction also includes the stock dividend in the calculation of the stock return.The
alphais the point where this regression line cuts the Y-axis. To reiterate, a stock'sbetacan be described as that part of a stock's movement that is influenced by the Index, while a stock'salphacan be regarded as that part of a stock's movement that is independent of the Index's movement.The
alphafunction's arguments are:index: the end-of-period market index (required, double)price: the end-of-period stock price (required, double)dividend: the stock dividend of the period (optional, double)
The stock price must have already figured in any stock split (or reverse split). The
alphais usually calculated over a period of 61 months. This aggregate function assumes that the input data is already normalized. That is,index,price, anddividendare of the same period. If they are of different periods, the yield (return) should be normalized first and then use the intercept function to calculatealpha.avg(f)Returns the average value computed for field
ffor all tuples in the Aggregate's window.beta(index, price, dividend)Returns a
doublethat may indicate the tendency of a security's returns to respond to swings in the market. A beta of 1 indicates that the security's price will move with the market. A beta of less than 1 means that the security will be less volatile than the market. A beta value that is greater than 1 indicates that the security's price will be more volatile than the market.For related information, see the description of the alpha function.
Beta is a measure of a security's or portfolio's volatility, or systematic risk, in comparison to the market as a whole. For example, if a stock's beta is 1.2, it is theoretically 20% more volatile than the market.
The function's arguments are:
index: the end-of-period market index (required, double)price: the end-of-period stock price (required, double)dividend: the stock dividend of the period (optional, double)
The stock price must have already figured in any stock split (or reverse split). The beta is usually calculated over a period of 61 months. This aggregate function assumes that the input data is already normalized. That is,
index,price, anddividendare of the same period. If they are of different periods, the yield should be normalized first and then use the slope function to calculate beta.correlation_coefficient(price, index)Returns the correlation coefficient for two fields,
priceandindex, for all tuples in the Aggregate's window. This function takes two input arguments (both can be an int or a double) and returns a double. The correlations coefficient is a measure that determines the degree to which two variable's movements are associated. The correlation coefficient will vary from The value-1.0to1.0.-1.0indicates perfect negative correlation, and1.0indicates perfect positive correlation. 0 means no correlation.Notes:
- The correlation_coefficient of 0 items is null.
- The correlation_coefficient of 1 item is 1.0.
- The correlation_coefficient of N identical items is 1.0.
correlation_coefficientp(price, index)Returns the correlation coefficient for two fields,
priceandindex, for all tuples in the Aggregate's window. Thecorrelation_coefficientpfunction is similar to thecorrelation_coefficientfunction; however you should usecorrelation_coefficientpwhen the data provided is the entire population, while thecorrelation_coefficientfunction is used for a random sample. Thecorrelation_coefficientpfunction is calculated using the "biased" (or "n") method. Thecorrelation_coefficientfunction is calculated using the "unbiased" (or "n-1") method.This function takes two input arguments (both can be an int or a double) and returns a double. The correlations coefficient is a measure that determines the degree to which two variable's movements are associated. The correlation coefficient will vary from
The value-1.0to1.0.-1.0indicates perfect negative correlation, and1.0indicates perfect positive correlation. 0 means no correlation.Notes:
- The correlation_coefficientp of 0 items is null.
- The correlation_coefficientp of 1 item is 1.0.
- The correlation_coefficientp of N identical items is 1.0.
count([expr])Returns the number of tuples in the Aggregate's window. If this function is called with an argument, then tuples for which the argument evaluates to null will not contribute to the count. The argument can be of any type, and it's actual value is ignored, other than the fact that it is non-null. If no argument is specified, then all tuples are included in the count.
count_distinct(e1[,...])Returns the number of tuples in the Aggregate's window with distinct values for the parameters. This function must be called with at least one argument. Use commas to separate subsequent arguments. For the purpose of this function, null values of a field are considered equivalent to each other.
covariance(price, index)Returns the covariance for two fields,
priceandindex, for all tuples in the Aggregate's window. This function takes two input arguments (both can be an int or a double) and returns a double. In financial applications, covariance can be used to measure the degree to which returns on two risky assets move in tandem. A positive covariance means that asset returns move together. A negative covariance means returns vary inversely.In the arguments, the
pricefield is a stock price, and theindexis the industry segment index.One method of calculating covariance is by looking at return deviations from the expected return in each scenario. Another method is to multiply correlation between the two variables by the standard deviation of each variable.
For example, if you owned one asset that had a high covariance with another asset that you
did not own, then you would receive very little increased diversification by adding the second asset. Of course, the opposite is true as well, adding assets with low covariance to your portfolio would lower the overall portfolio risk.covariancep(price, index)Returns the covariance for two fields,
priceandindex, for all tuples in the Aggregate's window. Thecovariancepfunction is similar to thecovariancefunction; however you should usecovariancepwhen the data provided is the entire population, while thecovariancefunction is used for a random sample. Thecovariancepfunction is calculated using the "biased" (or "n") method. Thecovariancefunction is calculated using the "unbiased" (or "n-1") method.This function takes two input arguments (both can be an int or a double) and returns a double. In financial applications, covariance can be used to measure the degree to which returns on two risky assets move in tandem. A positive covariance means that asset returns move together. A negative covariance means returns vary inversely.
In the arguments, the
pricefield is a stock price, and theindexis the industry segment index.One method of calculating covariance is by looking at return deviations from the expected return in each scenario. Another method is to multiply correlation between the two variables by the standard deviation of each variable.
For example, if you owned one asset that had a high covariance with another asset that you
did not own, then you would receive very little increased diversification by adding the second asset. Of course, the opposite is true as well, adding assets with low covariance to your portfolio would lower the overall portfolio risk.exp_moving_avg(price, count, weight)Returns a type of moving average that is similar to a simple moving average, except that more weight is given to the most recent data. Also known as an exponentially weighted moving average, this type of moving average reacts faster to recent price changes than a simple moving average.
The function's arguments are:
price: the price of a stock (required, int or double)count: the number of periods over which the exponential moving average is calculated (required, int)weight: the weight (0 < weight < 1) of the price of the last tuple. (optional, double)
The value of the third argument must between 0 and 1. If the value is not between 0 and 1 or if the argument is not provided, the weight is calculated from the following formula:
weight = 2 / (count + 1)
This function returns a
double. This Exponential Moving Average function is implemented using an infinite aggregate window size. When configuring a StreamBase Aggregate operator that will use thisexp_moving_avg(price, count, weight)function, do not configure theAdvanceandSizefields in the IDE, which results in infinite window size. Instead, set theEmitto 1 so that the aggregate emits for every tuple.The
exp_moving_avgfunction is calculated using the following formula:EMA = (CDV * weight) + (PDA * ( 1 - weight))
- EMA: exponential moving average of current period
- PDA: exponential moving average of previous period
- CDV: value of current period
For the first EMA, there is no PDA. A simple moving average (SMA) is computed instead. When computing the SMA, the
Nullvalue is emitted for each input tuple until the aggregate operator receives enough tuples to compute SMA. For example, if you want to compute a 20-day exponential moving average of the prices of a securities, aNullis returned by theexp_moving_avgfunction for the first 19-days. On the 20th day, the average of the prices of the first 20 days is returned. Starting from the 21st day, the above formula is used to calculate EMA of each day.intercept(x,y)For the data set of fields
xandyfor all tuples in the Aggregate's window, a "best-fit" linear regression line is calculated, and then the function returns the value where the line intersects the y-axis. The method that is used to find the line that best fits a group of points is called "least squares" (or "linear least squares"). This function takes two input arguments (both can be an int or a double) and returns a double.max(f)Returns the maximum value computed for field
ffor all tuples in the Aggregate's window.min(f)Returns the minimum value computed for field
ffor all tuples in the Aggregate's window.product(f)Returns the product (multiplication) computed for field
ffor all tuples in the Aggregate's window. Supports datatypesintanddouble.slope(x,y)For the data set of fields
xandyfor all tuples in the Aggregate's window, a "best-fit" linear regression line is calculated, and then the function returns the line's slope. The method that is used to find the line that best fits a group of points is called "least squares" (or "linear least squares"). Theslopefunction takes two input arguments (both can be an int or a double) and returns a double.stdev(f)Returns the standard deviation for field
ffor all tuples in the Aggregate's window. The function takes one input argument (an int or a double) and returns a double. Standard deviation is a measure of the dispersion of a set of data from its mean. The more spread apart the data is, the higher the deviation. For example, in financial applications, standard deviation could be applied to the annual rate of return of an investment to measure the investment's volatility (risk). A volatile stock would have a high standard deviation. In mutual funds, the standard deviation indicates how much the return on the fund is deviating from the expected normal returns.stdevp(f)Returns the standard deviation for field
ffor all tuples in the Aggregate's window. Thestdevpfunction is similar to thestdevfunction; however you should usestdevpwhen the data provided is the entire population, while thestdevfunction is used for a random sample. Thestdevpfunction is calculated using the "biased" (or "n") method. Thestdevfunction is calculated using the "unbiased" (or "n-1") method.The function takes one input argument (an int or a double) and returns a double. Standard deviation is a measure of the dispersion of a set of data from its mean. The more spread apart the data is, the higher the deviation. For example, in financial applications, standard deviation could be applied to the annual rate of return of an investment to measure the investment's volatility (risk). A volatile stock would have a high standard deviation. In mutual funds, the standard deviation indicates how much the return on the fund is deviating from the expected normal returns.
sum(f)Returns the sum value computed for field
ffor all tuples in the Aggregate's window.variance(f)Returns the variance for field
ffor all tuples in the Aggregate's window. The function takes one input argument (an int or a double) and returns a double. Variance is a measure of the dispersion of a set of data points around their mean value. It is a mathematical expectation of the average squared deviations from the mean. Variance measures the variability (volatility) from an average. Volatility is a measure of risk. So for example, this statistic can help determine the risk an investor might take on when purchasing a specific security.variancep(f)Returns the variance for field
ffor all tuples in the Aggregate's window. Thevariancepfunction is similar to thevariancefunction; however you should usevariancepwhen the data provided is the entire population, while thevariancefunction is used for a random sample. Thevariancepfunction is calculated using the "biased" (or "n") method. Thevariancefunction is calculated using the "unbiased" (or "n-1") method. The function takes one input argument (an int or a double) and returns a double. Variance is a measure of the dispersion of a set of data points around their mean value. It is a mathematical expectation of the average squared deviations from the mean. Variance measures the variability (volatility) from an average. Volatility is a measure of risk. So for example, this statistic can help determine the risk an investor might take on when purchasing a specific security.vwap(price, volume)Returns the volume-weighted average-price value from (typically) the
priceandvolumevalues, for all tuples in the Aggregate's window.
Aggregate Functions: C++ API Call
callcpp("function" [,arg...])Runs a C++ custom aggregate function directly from a StreamBase operator. Custom C++ functions can be accessed by the aggregate form of callcpp in any aggregate expression. To use callcpp in simple expressions, refer to Simple Functions: C++ API Call.)
To learn about coding custom C++ functions, refer to Creating Custom C++ Functions in the API guide.
Aggregate Functions: Java API Call
calljava("class" [,arg...])In an aggregate function, runs a custom Java method directly from the StreamBase expression language.
The function's first argument is a public class, extending the StreamBase AggregateWindow class, in a Java application that you have imported.
Note that you do not specify the method name, as you do with the simple calljava function, because the method names are already known.
For the valid argument types, please refer to the table in Simple Functions: Java API Call.
To learn about coding custom Java functions, refer to Creating Custom Java Functions in the API guide.
Using strresize for Java Methods That Return byte
When using calljava to invoke a Java method that returns a byte[], you
may need to use the strresize() function provided by StreamBase, so that the output length of the string is known. For example, if you attempt to use the following expression as a field value in a Map operator:
calljava("com.mycompany.MyClass", "myFunction", "abc")
...and myFunction is declared as returning a byte[], you will
encounter an "Unable to determine string length" typecheck error. Use
strresize to specify the maximum length of the output, so the operator's
output schema can be determined. For example:
strresize(calljava("com.mycompany.MyClass", "myFunction", "abc"), 20)
Note: A StreamBase string is not the same as a java.lang.String. No encoding is used when passing it as a byte[]. The raw data that was transmitted through StreamBase is used.
