You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: docs/js-udf.md
+69-63Lines changed: 69 additions & 63 deletions
Original file line number
Diff line number
Diff line change
@@ -1,27 +1,21 @@
1
1
# JavaScript UDF
2
2
3
-
In addition to [Remote UDF](/remote-udf), Timeplus Proton also supports JavaScript-based UDF running in the SQL engine. You can develop User-defined scalar functions (UDFs) or User-defined aggregate functions (UDAFs) with modern JavaScript (powered by [V8](https://v8.dev/)). No need to deploy extra server/service for the UDF. More languages will be supported in the future.
4
-
5
-
:::info
6
-
7
-
The JavaScript-based UDF can run in both Timeplus and Proton local deployments. It runs "locally" in the database engine. It doesn't mean this feature is only available for local deployment.
8
-
9
-
:::
3
+
Timeplus supports JavaScript-based UDF running in the SQL engine. You can develop User-defined scalar functions (UDFs) or User-defined aggregate functions (UDAFs) with modern JavaScript (powered by [V8](https://v8.dev/)). No need to deploy extra server/service for the UDF. More languages will be supported in the future.
10
4
11
5
## Register a JS UDF via SQL {#ddl}
12
-
Please check [CREATE FUNCTION](/sql-create-function) page for the SQL syntax.
6
+
Please check [CREATE FUNCTION](/sql-create-function#javascript-udf) page for the SQL syntax.
13
7
14
8
## Register a JS UDF via Web Console {#register}
15
9
16
-
1. Open "UDFs" from the navigation menu on the left, and click the 'Register New Function' button.
10
+
1. Open "UDFs" from the navigation menu on the left, and click the 'New UDF' button.
17
11
2. Specify a function name, such as `second_max`. Make sure the name won't conflict with built-in functions or other UDF. Description is optional.
18
12
3. Choose the data type for input parameters and return value.
19
13
4. Choose "JavaScript" as the UDF type.
20
14
5. Specify whether the function is for aggregation or not.
21
15
6. Enter the JavaScript source for the UDF. (We will explain more how to write the code.)
22
16
7. Click **Create** button to register the function.
23
17
24
-
###Arguments
18
+
## Arguments
25
19
26
20
Unlike Remote UDF, the argument names don't matter when you register a JS UDF. Make sure you the list of arguments matches the input parameter lists in your JavaScript function.
27
21
@@ -35,7 +29,7 @@ The input data are in Timeplus data type. They will be converted to JavaScript d
35
29
| date/date32/datetime/datetime64 | Date (in milliseconds) |
36
30
| array(Type) | Array |
37
31
38
-
###Returned value
32
+
## Returned value
39
33
40
34
The JavaScript UDF can return the following data types and they will be converted back to the specified Timeplus data types. The supported return type are similar to argument types. The only difference is that if you return a complex data structure as an `object`, it will be converted to a named `tuple` in Timeplus.
41
35
@@ -64,10 +58,14 @@ SELECT * FROM user_clicks where is_work_email(email)
64
58
65
59
You can use the following code to define a new function `is_work_email` with one input type `string` and return `bool`.
66
60
67
-
```javascript
61
+
```sql
62
+
CREATE OR REPLACEFUNCTIONis_work_email(email string)
To register this function, steps are different in Timeplus Enterprise and Proton:
194
-
195
-
* With Timeplus UI: choose JavaScript as UDF type, make sure to turn on 'is aggregation'. Set the function name say `second_max` (you don't need to repeat the function name in JS code). Add one argument in `float` type and set return type to `float` too. Please note, unlike JavaScript scalar function, you need to put all functions under an object `{}`. You can define internal private functions, as long as the name won't conflict with native functions in JavaScript, or in the UDF lifecycle.
196
-
* With SQL in Proton Client: check the example at [here](/js-udf#udaf).
202
+
To register this function with Timeplus Console: choose JavaScript as UDF type, make sure to turn on 'is aggregation'. Set the function name say `second_max` (you don't need to repeat the function name in JS code). Add one argument in `float` type and set return type to `float` too. Please note, unlike JavaScript scalar function, you need to put all functions under an object `{}`. You can define internal private functions, as long as the name won't conflict with native functions in JavaScript, or in the UDF lifecycle.
197
203
198
204
### Advanced Example for Complex Event Processing {#adv_udaf}
Copy file name to clipboardExpand all lines: docs/sql-create-function.md
+78-12Lines changed: 78 additions & 12 deletions
Original file line number
Diff line number
Diff line change
@@ -1,15 +1,5 @@
1
1
# CREATE FUNCTION
2
-
At Timeplus, we leverage SQL to make powerful streaming analytics more accessible to a broad range of users. Without SQL, you have to learn and call low-level programming API, then compile/package/deploy them to get analytics results. This is a repetitive and tedious process, even for small changes.
3
-
4
-
But some developers have concerns that complex logic or systems integration are hard to express using SQL.
5
-
6
-
That's why we add User-Defined Functions (UDF) support in Timeplus. This enables users to leverage existing programming libraries, integrate with external systems, or just make SQL easier to maintain.
7
-
8
-
Timeplus Proton and Timeplus Enterprise support [SQL UDF](/) and [Local UDF in JavaScript](/js-udf). You can develop User-defined scalar functions (UDFs) in SQL, or develop UDFs or User-defined aggregate functions (UDAFs) with modern JavaScript (powered by V8). No need to deploy extra server/service for the UDF. More languages will be supported.
9
-
10
-
:::info
11
-
In Timeplus Enterprise, the Python UDF will be ready soon.
12
-
:::
2
+
Timeplus supports four ways to develop/register UDF. Please check [UDF](/udf) page for the overview.
13
3
14
4
## SQL UDF
15
5
You can create or replace a SQL UDF, by specifying the function name, parameters and the expression.
@@ -27,6 +17,26 @@ CREATE OR REPLACE FUNCTION color_hex AS (r, g, b) -> '#'||hex(r)||hex(g)||hex(b)
27
17
28
18
[Learn More](/sql-udf)
29
19
20
+
## Remote UDF
21
+
Register a webhook as the UDF. You may use any programming language/framework to develop/deploy the webhook. A good starting point is using AWS Lambda.
22
+
23
+
Syntax:
24
+
```sql
25
+
CREATE REMOTE FUNCTION udf_name(ip string) RETURNS string
26
+
URL 'https://the_url'
27
+
AUTH_METHOD 'none'
28
+
```
29
+
If you need to protect the end point and only accept requests with a certain HTTP header, you can use the AUTH_HEADER and AUTH_KEY setting, e,g.
30
+
```sql
31
+
CREATE REMOTE FUNCTION udf_name(ip string) RETURNS string
32
+
URL 'https://the_url'
33
+
AUTH_METHOD 'auth_header'
34
+
AUTH_HEADER 'header_name'
35
+
AUTH_KEY 'value';
36
+
```
37
+
38
+
[Learn More](/remote-udf)
39
+
30
40
## JavaScript UDF
31
41
32
42
### UDF {#js-udf}
@@ -62,11 +72,13 @@ You can also add `EXECUTION_TIMEOUT <num>` to the end of the `CREATE FUNCTION` t
62
72
You can add debug information via `console.log(..)` in the JavaScript UDF. The logs will be available in the server log files.
63
73
:::
64
74
75
+
Check [more examples](js-udf#udf) for scalar function with 2 or more arguments or 0 argument.
76
+
65
77
### UDAF {#js-udaf}
66
78
67
79
Creating a user-defined-aggregation function (UDAF) requires a bit more effort. Please check [this documentation](/js-udf#udaf) for the 3 required and 3 optional functions.
starting from v2.7, Timeplus Enterprise also supports Python-based UDF. You can develop User-defined scalar functions (UDFs) or User-defined aggregate functions (UDAFs) with the embedded Python 3.10 runtime in Timeplus core engine. No need to deploy extra server/service for the UDF.
135
+
136
+
[Learn more](/py-udf) why Python UDF, and how to map the data types in Timeplus and Python, as well as how to manage dependencies.
137
+
138
+
### UDF {#py-udf}
139
+
Syntax:
140
+
```sql
141
+
CREATE OR REPLACEFUNCTIONudf_name(param1 type1,..)
142
+
RETURNS type2 LANGUAGE PYTHON AS
143
+
$$
144
+
import …
145
+
146
+
def udf_name(col1..):
147
+
…
148
+
149
+
$$
150
+
SETTINGS ...
151
+
```
152
+
153
+
### UDAF {#py-udaf}
154
+
UDAF or User Defined Aggregation Function is stateful. It takes one or more columns from a set of rows and return the aggregated result.
155
+
156
+
Syntax:
157
+
```sql
158
+
CREATE OR REPLACE AGGREGATION FUNCTION uda_name(param1 type1,...)
Copy file name to clipboardExpand all lines: docs/udf.md
+2Lines changed: 2 additions & 0 deletions
Original file line number
Diff line number
Diff line change
@@ -28,3 +28,5 @@ Please note, there are many factors to determine the number of function calls. F
28
28
Long story short, developers should not make assumption for the number of function calls. For User-defined scalar functions (UDFs) it should be stateless, and for User-defined aggregate functions (UDAFs), data might be aggregated more than once, but the final result is correct.
29
29
30
30
:::
31
+
32
+
Check [CREATE FUNCTION](/sql-create-function) for how to create functions via SQL.
0 commit comments