Input Parameter Types
When parameters are passed into JavaScript UDFs, Rockset values will be converted to JavaScript values according to their types, as follows:
Rockset Data Type | Javascript Type | Remarks |
---|---|---|
NULL | null | |
UNDEFINED | null | Note that this is not mapped to JavaScript undefined . You can use JavaScript undefined to detect that your function was called with fewer arguments than specified. |
int | number | Note that this will result in loss of precision for values ≥ 2^53 . If you want to handle large integers up to int64 , see instructions regarding BigInt below. |
int | BigInt | When function property input_convert_int64_to_bigint is set to true |
float | number | |
string | string | |
bool | boolean or number | The parameter value could be a JavaScript boolean or a JavaScript number and will evaluate to “true” or “false” when used in a boolean expression. |
object | object | |
object | Map | When function property input_convert_object_to_map is set to true |
array | array or TypedArray | Note that a Rockset array consisting solely of booleans may be converted to Uint8Array . |
date | Date | |
time | Date | |
datetime | Date | |
timestamp | Date |
Special Properties for Input Parameters
You can optionally do the following data type conversions on the JavaScript UDF input parameters by setting certain properties on the function definition:
int64
→BigInt
- Set the property
input_convert_int64_to_bigint = true
- Set the property
object
→Map
- Set the propertyinput_convert_object_to_map = true
For example:
script {{{
export function echo(n) { return n }
echo.input_convert_int64_to_bigint = true
}}}
SELECT _script.echo(9223372036854775807);
The above function will return the correct value 9223372036854775807
since the Rockset datatype will get mapped to BigInt
.
If input_convert_int64_to_bigint
was not set to true, then the SQL above will return 9223372036854776000
instead, since JavaScript numbers lose precision beyond 2^53.
Output Return Types
JavaScript values returned by the UDF will be converted into Rockset values according to their types, as follows:
Javascript Type | Rockset Data Type | Remarks |
---|---|---|
null | NULL | |
undefined | NULL | Note that this is not Rockset UNDEFINED |
number | int or float | Based on whether value is integral or not. It will always return a Rockset float when function property output_always_convert_number_to_float is set to true. |
BigInt | int or error | Note that Rockset int data type are signed 64-bit integers. If value returned in JavaScript BigInt doesn’t fit in Rockset int , an error will be thrown. |
string | string | |
boolean | bool | |
plain JS object | object | The Rockset object will contain all enumerable, non-Symbol keys from the JS object. If either of these keys or their corresponding values cannot be converted to Rockset types, an error will be thrown. |
Map | object | |
Set | array | |
array | array | |
TypedArray | array | Except Uint8Array , see below. |
Uint8Array | array of bool | Note that the values in the JavaScript Uint8Array are interpreted as boolean instead of number . |
Date | timestamp |
Special Properties for Output Return Types
You can optionally do the following data type conversions on the JavaScript UDF return values by setting certain properties on the function definition:
number
->float
- Set the property
output_always_convert_number_to_float = true
- Set the property
For Example:
script {{{
export function echo(n) { return n }
echo.output_always_convert_number_to_float = true
}}}
SELECT typeof(_script.echo(10));
The above function will return float
. But if output_always_convert_number_to_float
was not set to true, then it will return int
.