Skip to content

Commit c95eb8a

Browse files
lexluo09lexluo
andauthored
[enhancement] Function(create/drop) support the global operation (#16973) (#17608)
Support create/drop global function. When you create a custom function, it can only be used within in one database. It cannot be used in other database/catalog. When there are many databases/catalog, it needs to create function one by one. ## Problem summary Describe your changes. 1、 When a function is created or deleted, add the global keyword. CREATE [GLOBAL] [AGGREGATE] [ALIAS] FUNCTION function_name (arg_type [, ...]) [RETURNS ret_type] [INTERMEDIATE inter_type] [WITH PARAMETER(param [,...]) AS origin_function] [PROPERTIES ("key" = "value" [, ...]) ] DROP [GLOBAL] FUNCTION function_name (arg_type [, ...]) 2、A completely global global function is set, and the global function metadata is stored in the image. The function lookup strategy is to look in the database first, and if it can't be found, it looks in the global function. Co-authored-by: lexluo <[email protected]>
1 parent 49a053b commit c95eb8a

23 files changed

Lines changed: 677 additions & 167 deletions

File tree

‎docs/en/docs/sql-manual/sql-reference/Data-Definition-Statements/Create/CREATE-FUNCTION.md‎

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ If `function_name` contains the database name, then the custom function will be
3939
grammar:
4040

4141
```sql
42-
CREATE [AGGREGATE] [ALIAS] FUNCTION function_name
42+
CREATE [GLOBAL] [AGGREGATE] [ALIAS] FUNCTION function_name
4343
(arg_type [, ...])
4444
[RETURNS ret_type]
4545
[INTERMEDIATE inter_type]
@@ -49,6 +49,8 @@ CREATE [AGGREGATE] [ALIAS] FUNCTION function_name
4949

5050
Parameter Description:
5151

52+
- `GLOBAL`: If there is this item, it means that the created function is a global function.
53+
5254
- `AGGREGATE`: If there is this item, it means that the created function is an aggregate function.
5355

5456

@@ -147,6 +149,21 @@ Parameter Description:
147149
CREATE ALIAS FUNCTION id_masking(INT) WITH PARAMETER(id) AS CONCAT(LEFT(id, 3), '****', RIGHT(id, 4));
148150
````
149151
152+
6. Create a global custom scalar function
153+
154+
```sql
155+
CREATE GLOBAL FUNCTION my_add(INT, INT) RETURNS INT PROPERTIES (
156+
"symbol" = "_ZN9doris_udf6AddUdfEPNS_15FunctionContextERKNS_6IntValES4_",
157+
"object_file" = "http://host:port/libmyadd.so"
158+
);
159+
````
160+
161+
7. Create a global custom alias function
162+
163+
```sql
164+
CREATE GLOBAL ALIAS FUNCTION id_masking(INT) WITH PARAMETER(id) AS CONCAT(LEFT(id, 3), '****', RIGHT(id, 4));
165+
````
166+
150167
### Keywords
151168
152169
CREATE, FUNCTION

‎docs/en/docs/sql-manual/sql-reference/Data-Definition-Statements/Drop/DROP-FUNCTION.md‎

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ Delete a custom function. Function names and parameter types are exactly the sam
3737
grammar:
3838

3939
```sql
40-
DROP FUNCTION function_name
40+
DROP [GLOBAL] FUNCTION function_name
4141
(arg_type [, ...])
4242
````
4343

@@ -53,6 +53,11 @@ Parameter Description:
5353
```sql
5454
DROP FUNCTION my_add(INT, INT)
5555
````
56+
2. Delete a global function
57+
58+
```sql
59+
DROP GLOBAL FUNCTION my_add(INT, INT)
60+
````
5661

5762
### Keywords
5863

‎docs/zh-CN/docs/sql-manual/sql-reference/Data-Definition-Statements/Create/CREATE-FUNCTION.md‎

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ CREATE FUNCTION
3939
语法:
4040

4141
```sql
42-
CREATE [AGGREGATE] [ALIAS] FUNCTION function_name
42+
CREATE [GLOBAL] [AGGREGATE] [ALIAS] FUNCTION function_name
4343
(arg_type [, ...])
4444
[RETURNS ret_type]
4545
[INTERMEDIATE inter_type]
@@ -49,6 +49,8 @@ CREATE [AGGREGATE] [ALIAS] FUNCTION function_name
4949

5050
参数说明:
5151

52+
- `GLOBAL`: 如果有此项,表示的是创建的函数是全局范围内生效。
53+
5254
- `AGGREGATE`: 如果有此项,表示的是创建的函数是一个聚合函数。
5355

5456

@@ -147,6 +149,21 @@ CREATE [AGGREGATE] [ALIAS] FUNCTION function_name
147149
CREATE ALIAS FUNCTION id_masking(INT) WITH PARAMETER(id) AS CONCAT(LEFT(id, 3), '****', RIGHT(id, 4));
148150
```
149151

152+
6. 创建一个全局自定义标量函数
153+
154+
```sql
155+
CREATE GLOBAL FUNCTION my_add(INT, INT) RETURNS INT PROPERTIES (
156+
"symbol" = "_ZN9doris_udf6AddUdfEPNS_15FunctionContextERKNS_6IntValES4_",
157+
"object_file" = "http://host:port/libmyadd.so"
158+
);
159+
````
160+
161+
7. 创建一个全局自定义别名函数
162+
163+
```sql
164+
CREATE GLOBAL ALIAS FUNCTION id_masking(INT) WITH PARAMETER(id) AS CONCAT(LEFT(id, 3), '****', RIGHT(id, 4));
165+
````
166+
150167
### Keywords
151168
152169
CREATE, FUNCTION

‎docs/zh-CN/docs/sql-manual/sql-reference/Data-Definition-Statements/Drop/DROP-FUNCTION.md‎

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ DROP FUNCTION
3737
语法:
3838

3939
```sql
40-
DROP FUNCTION function_name
40+
DROP [GLOBAL] FUNCTION function_name
4141
(arg_type [, ...])
4242
```
4343

@@ -53,6 +53,11 @@ DROP FUNCTION function_name
5353
```sql
5454
DROP FUNCTION my_add(INT, INT)
5555
```
56+
2. 删除掉一个全局函数
57+
58+
```sql
59+
DROP GLOBAL FUNCTION my_add(INT, INT)
60+
````
5661

5762
### Keywords
5863

‎fe/fe-common/src/main/java/org/apache/doris/common/io/Writable.java‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,8 +29,8 @@
2929
* Class A implements Writable {
3030
* @Override
3131
* public void write(DataOutput out) throws IOException {
32-
* in.write(x);
33-
* in.write(y);
32+
* out.write(x);
33+
* out.write(y);
3434
* ...
3535
* }
3636
*

‎fe/fe-core/src/main/cup/sql_parser.cup‎

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1795,15 +1795,15 @@ create_stmt ::=
17951795
RESULT = new CreateClusterStmt(name, properties, password);
17961796
:}*/
17971797
/* Function */
1798-
| KW_CREATE opt_aggregate:isAggregate KW_FUNCTION opt_if_not_exists:ifNotExists function_name:functionName LPAREN func_args_def:args RPAREN
1798+
| KW_CREATE opt_var_type:type opt_aggregate:isAggregate KW_FUNCTION opt_if_not_exists:ifNotExists function_name:functionName LPAREN func_args_def:args RPAREN
17991799
KW_RETURNS type_def:returnType opt_intermediate_type:intermediateType opt_properties:properties
18001800
{:
1801-
RESULT = new CreateFunctionStmt(ifNotExists, isAggregate, functionName, args, returnType, intermediateType, properties);
1801+
RESULT = new CreateFunctionStmt(type, ifNotExists, isAggregate, functionName, args, returnType, intermediateType, properties);
18021802
:}
1803-
| KW_CREATE KW_ALIAS KW_FUNCTION opt_if_not_exists:ifNotExists function_name:functionName LPAREN func_args_def:args RPAREN
1803+
| KW_CREATE opt_var_type:type KW_ALIAS KW_FUNCTION opt_if_not_exists:ifNotExists function_name:functionName LPAREN func_args_def:args RPAREN
18041804
KW_WITH KW_PARAMETER LPAREN ident_list:parameters RPAREN KW_AS expr:func
18051805
{:
1806-
RESULT = new CreateFunctionStmt(ifNotExists, functionName, args, parameters, func);
1806+
RESULT = new CreateFunctionStmt(type, ifNotExists, functionName, args, parameters, func);
18071807
:}
18081808
/* Table */
18091809
| KW_CREATE opt_external:isExternal KW_TABLE opt_if_not_exists:ifNotExists table_name:name KW_LIKE table_name:existed_name KW_WITH KW_ROLLUP LPAREN ident_list:rollupNames RPAREN
@@ -2904,9 +2904,9 @@ drop_stmt ::=
29042904
RESULT = new DropClusterStmt(ifExists, cluster);
29052905
:}
29062906
/* Function */
2907-
| KW_DROP KW_FUNCTION opt_if_exists:ifExists function_name:functionName LPAREN func_args_def:args RPAREN
2907+
| KW_DROP opt_var_type:type KW_FUNCTION opt_if_exists:ifExists function_name:functionName LPAREN func_args_def:args RPAREN
29082908
{:
2909-
RESULT = new DropFunctionStmt(ifExists, functionName, args);
2909+
RESULT = new DropFunctionStmt(type, ifExists, functionName, args);
29102910
:}
29112911
/* Table */
29122912
| KW_DROP KW_TABLE opt_if_exists:ifExists table_name:name opt_force:force

‎fe/fe-core/src/main/java/org/apache/doris/analysis/CreateFunctionStmt.java‎

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,7 @@ public class CreateFunctionStmt extends DdlStmt {
9595
public static final String IS_RETURN_NULL = "always_nullable";
9696
private static final Logger LOG = LogManager.getLogger(CreateFunctionStmt.class);
9797

98+
private SetType type = SetType.DEFAULT;
9899
private final boolean ifNotExists;
99100
private final FunctionName functionName;
100101
private final boolean isAggregate;
@@ -119,9 +120,10 @@ public class CreateFunctionStmt extends DdlStmt {
119120
// timeout for both connection and read. 10 seconds is long enough.
120121
private static final int HTTP_TIMEOUT_MS = 10000;
121122

122-
public CreateFunctionStmt(boolean ifNotExists, boolean isAggregate, FunctionName functionName,
123-
FunctionArgsDef argsDef,
124-
TypeDef returnType, TypeDef intermediateType, Map<String, String> properties) {
123+
public CreateFunctionStmt(SetType type, boolean ifNotExists, boolean isAggregate, FunctionName functionName,
124+
FunctionArgsDef argsDef,
125+
TypeDef returnType, TypeDef intermediateType, Map<String, String> properties) {
126+
this.type = type;
125127
this.ifNotExists = ifNotExists;
126128
this.functionName = functionName;
127129
this.isAggregate = isAggregate;
@@ -138,8 +140,9 @@ public CreateFunctionStmt(boolean ifNotExists, boolean isAggregate, FunctionName
138140
this.originFunction = null;
139141
}
140142

141-
public CreateFunctionStmt(boolean ifNotExists, FunctionName functionName, FunctionArgsDef argsDef,
143+
public CreateFunctionStmt(SetType type, boolean ifNotExists, FunctionName functionName, FunctionArgsDef argsDef,
142144
List<String> parameters, Expr originFunction) {
145+
this.type = type;
143146
this.ifNotExists = ifNotExists;
144147
this.functionName = functionName;
145148
this.isAlias = true;
@@ -155,6 +158,10 @@ public CreateFunctionStmt(boolean ifNotExists, FunctionName functionName, Functi
155158
this.properties = ImmutableSortedMap.of();
156159
}
157160

161+
public SetType getType() {
162+
return type;
163+
}
164+
158165
public boolean isIfNotExists() {
159166
return ifNotExists;
160167
}
@@ -205,7 +212,7 @@ public void analyze(Analyzer analyzer) throws UserException {
205212

206213
private void analyzeCommon(Analyzer analyzer) throws AnalysisException {
207214
// check function name
208-
functionName.analyze(analyzer);
215+
functionName.analyze(analyzer, this.type);
209216

210217
// check operation privilege
211218
if (!Env.getCurrentEnv().getAccessManager().checkGlobalPriv(ConnectContext.get(), PrivPredicate.ADMIN)) {

‎fe/fe-core/src/main/java/org/apache/doris/analysis/DropFunctionStmt.java‎

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,16 +29,22 @@ public class DropFunctionStmt extends DdlStmt {
2929
private final boolean ifExists;
3030
private final FunctionName functionName;
3131
private final FunctionArgsDef argsDef;
32+
private SetType type = SetType.DEFAULT;
3233

3334
// set after analyzed
3435
private FunctionSearchDesc function;
3536

36-
public DropFunctionStmt(boolean ifExists, FunctionName functionName, FunctionArgsDef argsDef) {
37+
public DropFunctionStmt(SetType type, boolean ifExists, FunctionName functionName, FunctionArgsDef argsDef) {
38+
this.type = type;
3739
this.ifExists = ifExists;
3840
this.functionName = functionName;
3941
this.argsDef = argsDef;
4042
}
4143

44+
public SetType getType() {
45+
return type;
46+
}
47+
4248
public boolean isIfExists() {
4349
return ifExists;
4450
}
@@ -56,7 +62,7 @@ public void analyze(Analyzer analyzer) throws UserException {
5662
super.analyze(analyzer);
5763

5864
// analyze function name
59-
functionName.analyze(analyzer);
65+
functionName.analyze(analyzer, this.type);
6066

6167
// check operation privilege
6268
if (!Env.getCurrentEnv().getAccessManager().checkGlobalPriv(ConnectContext.get(), PrivPredicate.ADMIN)) {

‎fe/fe-core/src/main/java/org/apache/doris/analysis/FunctionCallExpr.java‎

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1327,6 +1327,13 @@ && collectChildReturnTypes()[0].isDecimalV3()) {
13271327
Function.CompareMode.IS_NONSTRICT_SUPERTYPE_OF);
13281328
}
13291329
}
1330+
// find from the internal database first, if not, then from the global functions
1331+
if (fn == null) {
1332+
Function searchDesc =
1333+
new Function(fnName, Arrays.asList(collectChildReturnTypes()), Type.INVALID, false);
1334+
fn = Env.getCurrentEnv().getGlobalFunctionMgr().getFunction(searchDesc,
1335+
Function.CompareMode.IS_NONSTRICT_SUPERTYPE_OF);
1336+
}
13301337
}
13311338
}
13321339
}

‎fe/fe-core/src/main/java/org/apache/doris/analysis/FunctionName.java‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -135,7 +135,7 @@ public String analyzeDb(Analyzer analyzer) throws AnalysisException {
135135
return db;
136136
}
137137

138-
public void analyze(Analyzer analyzer) throws AnalysisException {
138+
public void analyze(Analyzer analyzer, SetType type) throws AnalysisException {
139139
if (fn.length() == 0) {
140140
throw new AnalysisException("Function name can not be empty.");
141141
}
@@ -150,7 +150,7 @@ public void analyze(Analyzer analyzer) throws AnalysisException {
150150
}
151151
if (db == null) {
152152
db = analyzer.getDefaultDb();
153-
if (Strings.isNullOrEmpty(db)) {
153+
if (Strings.isNullOrEmpty(db) && type != SetType.GLOBAL) {
154154
ErrorReport.reportAnalysisException(ErrorCode.ERR_NO_DB_ERROR);
155155
}
156156
} else {

0 commit comments

Comments
 (0)