Skip to content

Commit d8ceaa8

Browse files
author
lexluo
committed
[enhancement](UDF) The user defined functions support global ('show functions'/'show create') operation (apache#16973)
1 parent 378789b commit d8ceaa8

17 files changed

Lines changed: 411 additions & 110 deletions

File tree

‎docs/en/docs/sql-manual/sql-reference/Show-Statements/SHOW-CREATE-FUNCTION.md‎

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -37,14 +37,14 @@ This statement is used to display the creation statement of the user-defined fun
3737
grammar:
3838

3939
```sql
40-
SHOW CREATE FUNCTION function_name(arg_type [, ...]) [FROM db_name]];
40+
SHOW CREATE [GLOBAL] FUNCTION function_name(arg_type [, ...]) [FROM db_name]];
4141
````
4242

4343
illustrate:
44-
45-
1. `function_name`: The name of the function to display
46-
2. `arg_type`: The parameter list of the function to display
47-
3. If db_name is not specified, the current default db is used
44+
1. `global`: The show function is global
45+
2. `function_name`: The name of the function to display
46+
3. `arg_type`: The parameter list of the function to display
47+
4. If db_name is not specified, the current default db is used
4848

4949
### Example
5050

@@ -53,6 +53,11 @@ illustrate:
5353
```sql
5454
SHOW CREATE FUNCTION my_add(INT, INT)
5555
````
56+
2. Show the creation statement of the specified global function
57+
58+
```sql
59+
SHOW CREATE GLOBAL FUNCTION my_add(INT, INT)
60+
````
5661

5762
### Keywords
5863

‎docs/en/docs/sql-manual/sql-reference/Show-Statements/SHOW-FUNCTIONS.md‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,11 +39,12 @@ Requires `SHOW` permission on this database
3939
grammar
4040

4141
```sql
42-
SHOW [FULL] [BUILTIN] FUNCTIONS [IN|FROM db] [LIKE 'function_pattern']
42+
SHOW [GLOBAL] [FULL] [BUILTIN] FUNCTIONS [IN|FROM db] [LIKE 'function_pattern']
4343
````
4444

4545
Parameters
4646

47+
>`global`: Indicates it means that the show function is a global function
4748
>`full`: Indicates the detailed information of the display function
4849
>`builtin`: Indicates the functions provided by the display system
4950
>`db`: database name to query

‎docs/zh-CN/docs/sql-manual/sql-reference/Show-Statements/SHOW-CREATE-FUNCTION.md‎

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -37,13 +37,14 @@ SHOW CREATE FUNCTION
3737
语法:
3838

3939
```sql
40-
SHOW CREATE FUNCTION function_name(arg_type [, ...]) [FROM db_name]];
40+
SHOW CREATE [GLOBAL] FUNCTION function_name(arg_type [, ...]) [FROM db_name]];
4141
```
4242

4343
说明:
44-
1. `function_name`: 要展示的函数名称
45-
2. `arg_type`: 要展示的函数的参数列表
46-
3. 如果不指定 db_name,使用当前默认 db
44+
1. `global`: 要展示的是全局函数
45+
2. `function_name`: 要展示的函数名称
46+
3. `arg_type`: 要展示的函数的参数列表
47+
4. 如果不指定 db_name,使用当前默认 db
4748

4849
### Example
4950

@@ -53,6 +54,12 @@ SHOW CREATE FUNCTION function_name(arg_type [, ...]) [FROM db_name]];
5354
SHOW CREATE FUNCTION my_add(INT, INT)
5455
```
5556

57+
2. 展示指定的全局函数的创建语句
58+
59+
```sql
60+
SHOW CREATE GLOBAL FUNCTION my_add(INT, INT)
61+
```
62+
5663
### Keywords
5764

5865
SHOW, CREATE, FUNCTION

‎docs/zh-CN/docs/sql-manual/sql-reference/Show-Statements/SHOW-FUNCTIONS.md‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,11 +39,12 @@ SHOW FUNCTIONS
3939
语法
4040

4141
```sql
42-
SHOW [FULL] [BUILTIN] FUNCTIONS [IN|FROM db] [LIKE 'function_pattern']
42+
SHOW [GLOBAL] [FULL] [BUILTIN] FUNCTIONS [IN|FROM db] [LIKE 'function_pattern']
4343
```
4444

4545
Parameters
4646

47+
>`global`:表示要展示的是全局函数
4748
>`full`:表示显示函数的详细信息
4849
>`builtin`:表示显示系统提供的函数
4950
>`db`: 要查询的数据库名字

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

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3637,9 +3637,9 @@ show_param ::=
36373637
RESULT = new ShowCreateCatalogStmt(catalogName);
36383638
:}
36393639
/* Create Function */
3640-
| KW_CREATE KW_FUNCTION function_name:functionName LPAREN func_args_def:args RPAREN opt_db:dbName
3640+
| KW_CREATE opt_var_type:type KW_FUNCTION function_name:functionName LPAREN func_args_def:args RPAREN opt_db:dbName
36413641
{:
3642-
RESULT = new ShowCreateFunctionStmt(dbName, functionName, args);
3642+
RESULT = new ShowCreateFunctionStmt(type, dbName, functionName, args);
36433643
:}
36443644
/* Cluster */
36453645
| KW_CLUSTERS
@@ -3852,9 +3852,9 @@ show_param ::=
38523852
{:
38533853
RESULT = new ShowRolesStmt();
38543854
:}
3855-
| opt_full opt_builtin:isBuiltin KW_FUNCTIONS opt_db:dbName opt_wild_where
3855+
| opt_var_type:type opt_full opt_builtin:isBuiltin KW_FUNCTIONS opt_db:dbName opt_wild_where
38563856
{:
3857-
RESULT = new ShowFunctionsStmt(dbName, isBuiltin, parser.isVerbose, parser.wild, parser.where);
3857+
RESULT = new ShowFunctionsStmt(type, dbName, isBuiltin, parser.isVerbose, parser.wild, parser.where);
38583858
:}
38593859
| KW_TYPECAST opt_db:dbName
38603860
{:

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

Lines changed: 39 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,10 @@
2020
import org.apache.doris.catalog.Column;
2121
import org.apache.doris.catalog.Env;
2222
import org.apache.doris.catalog.FunctionSearchDesc;
23+
import org.apache.doris.catalog.FunctionUtil;
2324
import org.apache.doris.catalog.ScalarType;
2425
import org.apache.doris.cluster.ClusterNamespace;
26+
import org.apache.doris.common.AnalysisException;
2527
import org.apache.doris.common.ErrorCode;
2628
import org.apache.doris.common.ErrorReport;
2729
import org.apache.doris.common.UserException;
@@ -40,11 +42,13 @@ public class ShowCreateFunctionStmt extends ShowStmt {
4042
private String dbName;
4143
private final FunctionName functionName;
4244
private final FunctionArgsDef argsDef;
45+
private SetType type = SetType.DEFAULT;
4346

4447
// set after analyzed
4548
private FunctionSearchDesc function;
4649

47-
public ShowCreateFunctionStmt(String dbName, FunctionName functionName, FunctionArgsDef argsDef) {
50+
public ShowCreateFunctionStmt(SetType type, String dbName, FunctionName functionName, FunctionArgsDef argsDef) {
51+
this.type = type;
4852
this.dbName = dbName;
4953
this.functionName = functionName;
5054
this.argsDef = argsDef;
@@ -66,20 +70,17 @@ public FunctionSearchDesc getFunction() {
6670
public void analyze(Analyzer analyzer) throws UserException {
6771
super.analyze(analyzer);
6872

69-
if (Strings.isNullOrEmpty(dbName)) {
70-
dbName = analyzer.getDefaultDb();
71-
if (Strings.isNullOrEmpty(dbName)) {
72-
ErrorReport.reportAnalysisException(ErrorCode.ERR_NO_DB_ERROR);
73-
}
74-
} else {
75-
dbName = ClusterNamespace.getFullName(getClusterName(), dbName);
73+
// the global function does not need to fetch/check the dbName
74+
if (!FunctionUtil.isGlobalFunction(this.type)) {
75+
dbName = FunctionUtil.reAcquireDbName(analyzer, dbName, getClusterName());
7676
}
7777

7878
// analyze function name
79-
functionName.analyze(analyzer, SetType.DEFAULT);
79+
functionName.analyze(analyzer, this.type);
8080

81-
// check operation privilege
82-
if (!Env.getCurrentEnv().getAccessManager().checkDbPriv(ConnectContext.get(), dbName, PrivPredicate.SHOW)) {
81+
// check operation privilege , except global function
82+
if (!FunctionUtil.isGlobalFunction(this.type) && !Env.getCurrentEnv().getAccessManager()
83+
.checkDbPriv(ConnectContext.get(), dbName, PrivPredicate.SHOW)) {
8384
ErrorReport.reportAnalysisException(
8485
ErrorCode.ERR_DBACCESS_DENIED_ERROR, ConnectContext.get().getQualifiedUser(), dbName);
8586
}
@@ -88,11 +89,32 @@ public void analyze(Analyzer analyzer) throws UserException {
8889
function = new FunctionSearchDesc(functionName, argsDef.getArgTypes(), argsDef.isVariadic());
8990
}
9091

92+
93+
/***
94+
* reAcquire dbName and check "No database selected"
95+
* @param analyzer
96+
* @throws AnalysisException
97+
*/
98+
private void reAcquireDbName(Analyzer analyzer) throws AnalysisException {
99+
if (Strings.isNullOrEmpty(dbName)) {
100+
dbName = analyzer.getDefaultDb();
101+
if (Strings.isNullOrEmpty(dbName)) {
102+
ErrorReport.reportAnalysisException(ErrorCode.ERR_NO_DB_ERROR);
103+
}
104+
} else {
105+
dbName = ClusterNamespace.getFullName(getClusterName(), dbName);
106+
}
107+
}
108+
91109
@Override
92110
public String toSql() {
93111
StringBuilder stringBuilder = new StringBuilder();
94-
stringBuilder.append("SHOW CREATE FUNCTION ").append(functionName).append(argsDef)
95-
.append(" IN ").append(dbName);
112+
if (FunctionUtil.isGlobalFunction(this.type)) {
113+
stringBuilder.append("SHOW CREATE GLOBAL FUNCTION ").append(functionName).append(argsDef);
114+
} else {
115+
stringBuilder.append("SHOW CREATE FUNCTION ").append(functionName).append(argsDef)
116+
.append(" IN ").append(dbName);
117+
}
96118
return stringBuilder.toString();
97119
}
98120

@@ -105,4 +127,8 @@ public String toString() {
105127
public ShowResultSetMetaData getMetaData() {
106128
return META_DATA;
107129
}
130+
131+
public SetType getType() {
132+
return type;
133+
}
108134
}

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

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,8 @@
1919

2020
import org.apache.doris.catalog.Column;
2121
import org.apache.doris.catalog.Env;
22+
import org.apache.doris.catalog.FunctionUtil;
2223
import org.apache.doris.catalog.ScalarType;
23-
import org.apache.doris.cluster.ClusterNamespace;
2424
import org.apache.doris.common.AnalysisException;
2525
import org.apache.doris.common.ErrorCode;
2626
import org.apache.doris.common.ErrorReport;
@@ -51,8 +51,11 @@ public class ShowFunctionsStmt extends ShowStmt {
5151

5252
private Expr expr;
5353

54-
public ShowFunctionsStmt(String dbName, boolean isBuiltin, boolean isVerbose, String wild,
54+
private SetType type = SetType.DEFAULT;
55+
56+
public ShowFunctionsStmt(SetType type, String dbName, boolean isBuiltin, boolean isVerbose, String wild,
5557
Expr expr) {
58+
this.type = type;
5659
this.dbName = dbName;
5760
this.isBuiltin = isBuiltin;
5861
this.isVerbose = isVerbose;
@@ -80,6 +83,10 @@ public Expr getExpr() {
8083
return expr;
8184
}
8285

86+
public SetType getType() {
87+
return type;
88+
}
89+
8390
public boolean like(String str) {
8491
str = str.toLowerCase();
8592
return str.matches(wild.replace(".", "\\.").replace("?", ".").replace("%", ".*").toLowerCase());
@@ -89,16 +96,12 @@ public boolean like(String str) {
8996
public void analyze(Analyzer analyzer) throws UserException {
9097
super.analyze(analyzer);
9198

92-
if (Strings.isNullOrEmpty(dbName)) {
93-
dbName = analyzer.getDefaultDb();
94-
if (Strings.isNullOrEmpty(dbName)) {
95-
ErrorReport.reportAnalysisException(ErrorCode.ERR_NO_DB_ERROR);
96-
}
97-
} else {
98-
dbName = ClusterNamespace.getFullName(getClusterName(), dbName);
99+
if (!FunctionUtil.isGlobalFunction(this.type)) {
100+
this.dbName = FunctionUtil.reAcquireDbName(analyzer, dbName, getClusterName());
99101
}
100102

101-
if (!Env.getCurrentEnv().getAccessManager().checkDbPriv(ConnectContext.get(), dbName, PrivPredicate.SHOW)) {
103+
if (!FunctionUtil.isGlobalFunction(this.type) && !Env.getCurrentEnv().getAccessManager()
104+
.checkDbPriv(ConnectContext.get(), dbName, PrivPredicate.SHOW)) {
102105
ErrorReport.reportAnalysisException(
103106
ErrorCode.ERR_DBACCESS_DENIED_ERROR, ConnectContext.get().getQualifiedUser(), dbName);
104107
}

‎fe/fe-core/src/main/java/org/apache/doris/catalog/AggregateFunction.java‎

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -558,8 +558,14 @@ public void setIntermediateType(Type t) {
558558
}
559559

560560
@Override
561-
public String toSql(boolean ifNotExists) {
562-
StringBuilder sb = new StringBuilder("CREATE AGGREGATE FUNCTION ");
561+
public String toSql(boolean ifNotExists, boolean isGlobal) {
562+
StringBuilder sb = new StringBuilder("CREATE ");
563+
564+
if (isGlobal) {
565+
sb.append("GLOBAL ");
566+
}
567+
sb.append("AGGREGATE FUNCTION ");
568+
563569
if (ifNotExists) {
564570
sb.append("IF NOT EXISTS ");
565571
}

‎fe/fe-core/src/main/java/org/apache/doris/catalog/AliasFunction.java‎

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -232,9 +232,15 @@ private boolean checkParams(Expr expr, String param) {
232232
}
233233

234234
@Override
235-
public String toSql(boolean ifNotExists) {
235+
public String toSql(boolean ifNotExists, boolean isGlobal) {
236236
setSlotRefLabel(originFunction);
237-
StringBuilder sb = new StringBuilder("CREATE ALIAS FUNCTION ");
237+
StringBuilder sb = new StringBuilder("CREATE ");
238+
239+
if (isGlobal) {
240+
sb.append("GLOBAL ");
241+
}
242+
sb.append("ALIAS FUNCTION ");
243+
238244
if (ifNotExists) {
239245
sb.append("IF NOT EXISTS ");
240246
}

‎fe/fe-core/src/main/java/org/apache/doris/catalog/Function.java‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -515,7 +515,7 @@ public TFunction toThrift(Type realReturnType, Type[] realArgTypes) {
515515
}
516516

517517
// Child classes must override this function.
518-
public String toSql(boolean ifNotExists) {
518+
public String toSql(boolean ifNotExists, boolean isGlobal) {
519519
return "";
520520
}
521521

0 commit comments

Comments
 (0)