Utility Functions
Utility functions can be commonly used as parameters of any functions.
Context
context()
Syntax: context()
Returns context object of the script runtime.
key()
Syntax: key()
Returns the key of the current record.
value()
Syntax: value( [index] )
index
int index of the value array
Returns the whole value of the current records in array. If the index is given, it returns the element of the values.
For example, If the current value is [0, true, "hello", "world"]
value()
returns the whole value array[0, true, "hello", "world"]
value(0)
returns the first element of the value0
value(3)
returns the last element of the value"world"
payload()
Syntax: payload()
Returns the current input stream that sent from caller of the tql script.
If the tql script is called via HTTP, the result of payload()
is the stream of the body content of POST request.
If the tql script is called via MQTT, the payload()
returns the payload of the PUBLISH message.
param()
Syntax: param( name )
name
string name of the query parameter
When the tql script is called via HTTP, the requested query parameters can be accessed by param()
function.
Time
time()
Syntax: time( number|string [, duration] )
Example)
time('now')
return current time.time('now', '-10s50ms')
return the time 10.05 seconds before from now.time('now -10s50ms')
return the time 10.05 seconds before from now.time(1672531200*1000000000)
returns the time of Jan-1-2023 AM 12:00:00
roundTime()
Syntax: roundTime( time, duration )
Returns rounded time.
Example)
roundTime(time('now'), '1h')
roundTime(key(), '1s')
parseTime()
Syntax: parseTime( time, format, timezone )
time
string time expressionformat
string time format expressiontimezone
tz timezone value typically usetz()
function to get the demand location
Example)
parseTime("2023-03-01 14:01:02", "DEFAULT", tz("Asia/Tokyo"))
parseTime("2023-03-01 14:01:02", "DEFAULT", tz("local"))
tz()
Syntax: tz( name )
Returns time zone that matched with the given name
Example)
tz('local')
tz('UTC')
tz('EST')
tz("Europe/Paris")
timeformat()
Syntax: timeformat( format )
format
string
SQL(`select time, value from example limit 10`)
CSV(timeformat("DEFAULT"), tz("Asia/Seoul"))
ROWNUM column0 column1
1 22 Aug 23 09:58 KST 0.8906962016352916
2 22 Aug 23 09:58 KST 0.8439627151061103
format | result of timeformatting |
---|---|
DEFAULT | 2006-01-02 15:04:05.999 |
NUMERIC | 01/02 03:04:05PM ‘06 -0700 |
ANSIC | Mon Jan _2 15:04:05 2006 |
UNIX | Mon Jan _2 15:04:05 MST 2006 |
RUBY | Mon Jan 02 15:04:05 -0700 2006 |
RFC822 | 02 Jan 06 15:04 MST |
RFC822Z | 02 Jan 06 15:04 -0700 |
RFC850 | Monday, 02-Jan-06 15:04:05 MST |
RFC1123 | Mon, 02 Jan 2006 15:04:05 MST |
RFC1123Z | Mon, 02 Jan 2006 15:04:05 -0700 |
RFC3339 | 2006-01-02T15:04:05Z07:00 |
RFC3339NANO | 2006-01-02T15:04:05.999999999Z07:00 |
KITCHEN | 3:04:05PM |
STAMP | Jan _2 15:04:05 |
STAMPMILLI | Jan _2 15:04:05.000 |
STAMPMICRO | Jan _2 15:04:05.000000 |
STAMPNANO | Jan _2 15:04:05.000000000 |
s | unix epoch time in seconds |
ms | unix epoch time in milliseconds |
us | unix epoch time in microseconds |
ns | unix epoch time in nanoseconds |
s_ms | seconds and millisec (05.999) |
s_us | seconds and microsec (05.999999) |
s_ns | seconds and nanosec (05.999999999) |
s.ms | seconds and millisec, zero padding (05.000) |
s.us | seconds and microsec, zero padding (05.000000) |
s.ns | seconds and nanosec, zero padding (05.000000000) |
sqlTimeformat()
Syntax: sqlTimeformat( format )
format
string
SQL(`select time, value from example limit 10`)
CSV(sqlTimeformat("YYYY-MM-DD HH24:MI:SS.nnnnnn"), tz("Asia/Seoul"))
ROWNUM column0 column1
1 2023-08-22 09:58:58.216739 0.8906962016352916
2 2023-08-22 09:58:58.226739 0.8439627151061103
3 2023-08-22 09:58:58.236739 0.7897361125403046
format | result of timeformatting |
---|---|
YYYY | four-digit year value |
YY | two-digit year value |
MM | two-digit month value between 01 to 12 |
MMM | day of week |
DD | two-digit day of month between 01 to 31 |
HH24 | two-digit hour value between 00 to 23 |
HH12 | two-digit hour value between 0 to 12 |
HH | two-digit hour value between 0 to 12 |
MI | two-digit minute value between 00 to 59 |
SS | two-digit seconds value between 0 and 59 |
AM | AM/PM |
nnn… | 1 to 9 digits fractions of a second |
ansiTimeformat()
Syntax: ansiTimeformat( format )
format
string
SQL(`select time, value from example limit 10`)
CSV( ansiTimeformat("yyyy-mm-dd hh:nn:ss.ffffff"))
ROWNUM column0 column1
1 2023-08-22 09:58:58.216739 0.8906962016352916
2 2023-08-22 09:58:58.226739 0.8439627151061103
3 2023-08-22 09:58:58.236739 0.7897361125403046
format | result of timeformatting |
---|---|
yyyy | four-digit year value |
mm | two-digit month value between 01 to 12 |
dd | two-digit day value between 01 to 31 |
hh | two-digit hour value between 00 to 23 |
nn | two-digit minute value between 00 to 59 |
ss | two-digit seconds value between 0 and 59 |
fff… | 1 to 9 digits fractions of a second |
Others
count()
Syntax: count( array|tuple )
Returns the number of the elements.
Math
Mathematical functions. Since v8.0.6
This functions does not guarantee bit-identical results across system architectures.
function | description |
---|---|
abs(x) | the absolute value of x. |
acos(x) | the arccosine, in radians, of x. |
acosh(x) | the inverse hyperbolic cosine of x. |
asin(x) | the arcsine, in radians, of x. |
asinh(x) | the inverse hyperbolic sine of x. |
atan(x) | the arctangent, in radians, of x. |
atanh(x) | the inverse hyperbolic tangent of x. |
ceil(x) | the least integer value greater than or equal to x. |
cos(x) | the cosine of the radian argument x. |
cosh(x) | the hyperbolic cosine of x. |
exp(x) | e**x, the base-e exponential of x. |
exp2(x) | 2**x, the base-2 exponential of x. |
floor(x) | the greatest integer value less than or equal to x. |
log(x) | the natural logarithm of x. |
log2(x) | the binary logarithm of x. The special cases are the same as for log. |
log10(x) | the decimal logarithm of x. The special cases are the same as for log. |
max(x,y) | the larger of x or y. |
min(x,y) | the smaller of x or y. |
mod(x,y) | the floating-point remainder of x/y. The magnitude of the result is less than y and its sign agrees with that of x. |
pow(x, y) | x**y, the base-x exponential of y. |
pow10(x) | 10**x, the base-10 exponential of n. |
remainder(x,y) | the IEEE 754 floating-point remainder of x/y. |
round(x) | the nearest integer, rounding half away from zero. |
sin(x) | the sine of the radian argument x. |
sinh(x) | the hyperbolic sine of x. |
sqrt(x) | the square root of x. |
tan(x) | the tangent of the radian argument x. |
tanh(x) | the hyperbolic tangent of x. |
trunc(x) | the integer value of x. |
An example usage of math function sqrt
with MAPVALUE
.