Skip to content

Commit 562f572

Browse files
authored
[enhancement](UDF) The user defined functions support global ('show functions'/'show create') operation (#16973) (#17964)
1. add the global keyword. SHOW [GLOBAL] [FULL] [BUILTIN] FUNCTIONS [IN|FROM db] [LIKE 'function_pattern'] SHOW CREATE GLOBAL FUNCTION function_name(arg_type [, ...]); 2. show the details of the global udf.
1 parent 354d109 commit 562f572

17 files changed

Lines changed: 537 additions & 120 deletions

File tree

‎docs/en/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,14 +37,16 @@ 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+
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
4448

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
49+
**Note: the "global" keyword is only available after v2.0**
4850

4951
### Example
5052

@@ -53,6 +55,11 @@ illustrate:
5355
```sql
5456
SHOW CREATE FUNCTION my_add(INT, INT)
5557
````
58+
2. Show the creation statement of the specified global function
59+
60+
```sql
61+
SHOW CREATE GLOBAL FUNCTION my_add(INT, INT)
62+
````
5663

5764
### Keywords
5865

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

Lines changed: 40 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,13 +42,27 @@ grammar
4242
SHOW [FULL] [BUILTIN] FUNCTIONS [IN|FROM db] [LIKE 'function_pattern']
4343
````
4444

45-
Parameters
45+
Parameters
4646

4747
>`full`: Indicates the detailed information of the display function
4848
>`builtin`: Indicates the functions provided by the display system
4949
>`db`: database name to query
5050
>`function_pattern`: parameter used to filter function names
5151

52+
grammar
53+
54+
```sql
55+
SHOW GLOBAL [FULL] FUNCTIONS [LIKE 'function_pattern']
56+
````
57+
58+
Parameters
59+
60+
>`global`: Indicates it means that the show function is a global function
61+
>`full`: Indicates the detailed information of the display function
62+
>`function_pattern`: parameter used to filter function names
63+
64+
**Note: the "global" keyword is only available after v2.0**
65+
5266
### Example
5367
5468
````sql
@@ -83,6 +97,31 @@ mysql> show builtin functions in testDb like 'year%';
8397
| years_sub |
8498
+---------------+
8599
2 rows in set (0.00 sec)
100+
101+
mysql> show global full functions\G;
102+
*************************** 1. row ***************************
103+
Signature: decimal(ALL, INT, INT)
104+
Return Type: VARCHAR
105+
Function Type: Alias
106+
Intermediate Type: NULL
107+
Properties: {"parameter":"col, precision, scale","origin_function":"CAST(`col` AS decimal(`precision`, `scale`))"}
108+
*************************** 2. row ***************************
109+
Signature: id_masking(BIGINT)
110+
Return Type: VARCHAR
111+
Function Type: Alias
112+
Intermediate Type: NULL
113+
Properties: {"parameter":"id","origin_function":"concat(left(`id`, 3), `****`, right(`id`, 4))"}
114+
2 rows in set (0.00 sec)
115+
116+
mysql> show global functions ;
117+
+---------------+
118+
| Function Name |
119+
+---------------+
120+
| decimal |
121+
| id_masking |
122+
+---------------+
123+
2 rows in set (0.00 sec)
124+
86125
````
87126
88127
### Keywords

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

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -37,13 +37,16 @@ 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
48+
49+
**注意: "global"关键字在v2.0版本及以后才可用**
4750

4851
### Example
4952

@@ -53,6 +56,12 @@ SHOW CREATE FUNCTION function_name(arg_type [, ...]) [FROM db_name]];
5356
SHOW CREATE FUNCTION my_add(INT, INT)
5457
```
5558

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

5867
SHOW, CREATE, FUNCTION

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

Lines changed: 40 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,13 +42,27 @@ SHOW FUNCTIONS
4242
SHOW [FULL] [BUILTIN] FUNCTIONS [IN|FROM db] [LIKE 'function_pattern']
4343
```
4444

45-
Parameters
45+
Parameters
4646

4747
>`full`:表示显示函数的详细信息
4848
>`builtin`:表示显示系统提供的函数
4949
>`db`: 要查询的数据库名字
5050
>`function_pattern`: 用来过滤函数名称的参数
5151
52+
语法
53+
54+
```sql
55+
SHOW GLOBAL [FULL] FUNCTIONS [LIKE 'function_pattern']
56+
```
57+
58+
Parameters
59+
60+
>`global`:表示要展示的是全局函数
61+
>`full`:表示显示函数的详细信息
62+
>`function_pattern`: 用来过滤函数名称的参数
63+
64+
**注意: "global"关键字在v2.0版本及以后才可用**
65+
5266
### Example
5367

5468
```sql
@@ -83,6 +97,31 @@ mysql> show builtin functions in testDb like 'year%';
8397
| years_sub |
8498
+---------------+
8599
2 rows in set (0.00 sec)
100+
101+
mysql> show global full functions\G;
102+
*************************** 1. row ***************************
103+
Signature: decimal(ALL, INT, INT)
104+
Return Type: VARCHAR
105+
Function Type: Alias
106+
Intermediate Type: NULL
107+
Properties: {"parameter":"col, precision, scale","origin_function":"CAST(`col` AS decimal(`precision`, `scale`))"}
108+
*************************** 2. row ***************************
109+
Signature: id_masking(BIGINT)
110+
Return Type: VARCHAR
111+
Function Type: Alias
112+
Intermediate Type: NULL
113+
Properties: {"parameter":"id","origin_function":"concat(left(`id`, 3), `****`, right(`id`, 4))"}
114+
2 rows in set (0.00 sec)
115+
116+
mysql> show global functions ;
117+
+---------------+
118+
| Function Name |
119+
+---------------+
120+
| decimal |
121+
| id_masking |
122+
+---------------+
123+
2 rows in set (0.00 sec)
124+
86125
```
87126

88127
### Keywords

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

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3641,9 +3641,9 @@ show_param ::=
36413641
RESULT = new ShowCreateCatalogStmt(catalogName);
36423642
:}
36433643
/* Create Function */
3644-
| KW_CREATE KW_FUNCTION function_name:functionName LPAREN func_args_def:args RPAREN opt_db:dbName
3644+
| KW_CREATE opt_var_type:type KW_FUNCTION function_name:functionName LPAREN func_args_def:args RPAREN opt_db:dbName
36453645
{:
3646-
RESULT = new ShowCreateFunctionStmt(dbName, functionName, args);
3646+
RESULT = new ShowCreateFunctionStmt(type, dbName, functionName, args);
36473647
:}
36483648
/* Cluster */
36493649
| KW_CLUSTERS
@@ -3860,6 +3860,10 @@ show_param ::=
38603860
{:
38613861
RESULT = new ShowFunctionsStmt(dbName, isBuiltin, parser.isVerbose, parser.wild, parser.where);
38623862
:}
3863+
| KW_GLOBAL opt_full KW_FUNCTIONS opt_wild_where
3864+
{:
3865+
RESULT = new ShowFunctionsStmt(parser.isVerbose, parser.wild, parser.where);
3866+
:}
38633867
| KW_TYPECAST opt_db:dbName
38643868
{:
38653869
RESULT = new ShowTypeCastStmt(dbName, parser.where);

‎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: 21 additions & 9 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,6 +51,8 @@ public class ShowFunctionsStmt extends ShowStmt {
5151

5252
private Expr expr;
5353

54+
private SetType type = SetType.DEFAULT;
55+
5456
public ShowFunctionsStmt(String dbName, boolean isBuiltin, boolean isVerbose, String wild,
5557
Expr expr) {
5658
this.dbName = dbName;
@@ -60,6 +62,13 @@ public ShowFunctionsStmt(String dbName, boolean isBuiltin, boolean isVerbose, St
6062
this.expr = expr;
6163
}
6264

65+
public ShowFunctionsStmt(boolean isVerbose, String wild, Expr expr) {
66+
this.type = SetType.GLOBAL;
67+
this.isVerbose = isVerbose;
68+
this.wild = wild;
69+
this.expr = expr;
70+
}
71+
6372
public String getDbName() {
6473
return dbName;
6574
}
@@ -80,6 +89,10 @@ public Expr getExpr() {
8089
return expr;
8190
}
8291

92+
public SetType getType() {
93+
return type;
94+
}
95+
8396
public boolean like(String str) {
8497
str = str.toLowerCase();
8598
return str.matches(wild.replace(".", "\\.").replace("?", ".").replace("%", ".*").toLowerCase());
@@ -89,16 +102,12 @@ public boolean like(String str) {
89102
public void analyze(Analyzer analyzer) throws UserException {
90103
super.analyze(analyzer);
91104

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);
105+
if (!FunctionUtil.isGlobalFunction(this.type)) {
106+
this.dbName = FunctionUtil.reAcquireDbName(analyzer, dbName, getClusterName());
99107
}
100108

101-
if (!Env.getCurrentEnv().getAccessManager().checkDbPriv(ConnectContext.get(), dbName, PrivPredicate.SHOW)) {
109+
if (!FunctionUtil.isGlobalFunction(this.type) && !Env.getCurrentEnv().getAccessManager()
110+
.checkDbPriv(ConnectContext.get(), dbName, PrivPredicate.SHOW)) {
102111
ErrorReport.reportAnalysisException(
103112
ErrorCode.ERR_DBACCESS_DENIED_ERROR, ConnectContext.get().getQualifiedUser(), dbName);
104113
}
@@ -118,6 +127,9 @@ public ShowResultSetMetaData getMetaData() {
118127
public String toSql() {
119128
StringBuilder sb = new StringBuilder();
120129
sb.append("SHOW ");
130+
if (FunctionUtil.isGlobalFunction(this.type)) {
131+
sb.append("GLOBAL ");
132+
}
121133
if (isVerbose) {
122134
sb.append("FULL ");
123135
}

0 commit comments

Comments
 (0)