Skip to content

Commit 2663c6d

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

17 files changed

Lines changed: 530 additions & 121 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: 38 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,13 +42,25 @@ 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+
5264
### Example
5365
5466
````sql
@@ -83,6 +95,31 @@ mysql> show builtin functions in testDb like 'year%';
8395
| years_sub |
8496
+---------------+
8597
2 rows in set (0.00 sec)
98+
99+
mysql> show global full functions\G;
100+
*************************** 1. row ***************************
101+
Signature: decimal(ALL, INT, INT)
102+
Return Type: VARCHAR
103+
Function Type: Alias
104+
Intermediate Type: NULL
105+
Properties: {"parameter":"col, precision, scale","origin_function":"CAST(`col` AS decimal(`precision`, `scale`))"}
106+
*************************** 2. row ***************************
107+
Signature: id_masking(BIGINT)
108+
Return Type: VARCHAR
109+
Function Type: Alias
110+
Intermediate Type: NULL
111+
Properties: {"parameter":"id","origin_function":"concat(left(`id`, 3), `****`, right(`id`, 4))"}
112+
2 rows in set (0.00 sec)
113+
114+
mysql> show global functions ;
115+
+---------------+
116+
| Function Name |
117+
+---------------+
118+
| decimal |
119+
| id_masking |
120+
+---------------+
121+
2 rows in set (0.00 sec)
122+
86123
````
87124
88125
### Keywords

‎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: 38 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,13 +42,25 @@ 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+
5264
### Example
5365

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

88125
### Keywords

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

Lines changed: 6 additions & 2 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
@@ -3856,6 +3856,10 @@ show_param ::=
38563856
{:
38573857
RESULT = new ShowFunctionsStmt(dbName, isBuiltin, parser.isVerbose, parser.wild, parser.where);
38583858
:}
3859+
| KW_GLOBAL opt_full KW_FUNCTIONS opt_wild_where
3860+
{:
3861+
RESULT = new ShowFunctionsStmt(parser.isVerbose, parser.wild, parser.where);
3862+
:}
38593863
| KW_TYPECAST opt_db:dbName
38603864
{:
38613865
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)