Skip to content

Commit d4c6cbb

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

16 files changed

Lines changed: 442 additions & 125 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: 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: 18 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
}

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

Lines changed: 29 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,7 @@ public class AggregateFunction extends Function {
102102
// empty input in BE).
103103
private boolean returnsNonNullOnEmpty;
104104

105-
//use for java-udaf to point the class of user define
105+
// use for java-udaf to point the class of user define
106106
private String symbolName;
107107

108108
// only used for serialization
@@ -154,28 +154,28 @@ public static AggregateFunction createBuiltin(String name,
154154
}
155155

156156
public AggregateFunction(FunctionName fnName, List<Type> argTypes,
157-
Type retType, Type intermediateType,
158-
URI location, String updateFnSymbol, String initFnSymbol,
159-
String serializeFnSymbol, String mergeFnSymbol, String getValueFnSymbol,
160-
String removeFnSymbol, String finalizeFnSymbol, boolean vectorized) {
157+
Type retType, Type intermediateType,
158+
URI location, String updateFnSymbol, String initFnSymbol,
159+
String serializeFnSymbol, String mergeFnSymbol, String getValueFnSymbol,
160+
String removeFnSymbol, String finalizeFnSymbol, boolean vectorized) {
161161
this(fnName, argTypes, retType, intermediateType, false, location,
162162
updateFnSymbol, initFnSymbol, serializeFnSymbol,
163163
mergeFnSymbol, getValueFnSymbol, removeFnSymbol, finalizeFnSymbol, vectorized);
164164
}
165165

166166
public AggregateFunction(FunctionName fnName, List<Type> argTypes,
167-
Type retType, Type intermediateType, boolean hasVarArgs,
168-
URI location, String updateFnSymbol, String initFnSymbol,
169-
String serializeFnSymbol, String mergeFnSymbol, String getValueFnSymbol,
170-
String removeFnSymbol, String finalizeFnSymbol, boolean vectorized) {
167+
Type retType, Type intermediateType, boolean hasVarArgs,
168+
URI location, String updateFnSymbol, String initFnSymbol,
169+
String serializeFnSymbol, String mergeFnSymbol, String getValueFnSymbol,
170+
String removeFnSymbol, String finalizeFnSymbol, boolean vectorized) {
171171
// only `count` is always not nullable, other aggregate function is always nullable
172172
super(fnName, argTypes, retType, hasVarArgs, vectorized,
173173
AggregateFunction.NOT_NULLABLE_AGGREGATE_FUNCTION_NAME_SET.contains(fnName.getFunction())
174174
? NullableMode.ALWAYS_NOT_NULLABLE :
175-
AggregateFunction.ALWAYS_NULLABLE_AGGREGATE_FUNCTION_NAME_SET.contains(fnName.getFunction())
176-
? NullableMode.ALWAYS_NULLABLE :
177-
AggregateFunction.CUSTOM_AGGREGATE_FUNCTION_NAME_SET.contains(fnName.getFunction())
178-
? NullableMode.CUSTOM : NullableMode.DEPEND_ON_ARGUMENT);
175+
AggregateFunction.ALWAYS_NULLABLE_AGGREGATE_FUNCTION_NAME_SET.contains(fnName.getFunction())
176+
? NullableMode.ALWAYS_NULLABLE :
177+
AggregateFunction.CUSTOM_AGGREGATE_FUNCTION_NAME_SET.contains(fnName.getFunction())
178+
? NullableMode.CUSTOM : NullableMode.DEPEND_ON_ARGUMENT);
179179
setLocation(location);
180180
this.intermediateType = (intermediateType.equals(retType)) ? null : intermediateType;
181181
this.updateFnSymbol = updateFnSymbol;
@@ -330,10 +330,10 @@ public static AggregateFunction createAnalyticBuiltin(String name,
330330

331331
// Used to create UDAF
332332
public AggregateFunction(FunctionName fnName, Type[] argTypes,
333-
Type retType, boolean hasVarArgs, Type intermediateType, URI location,
334-
String initFnSymbol, String updateFnSymbol, String mergeFnSymbol,
335-
String serializeFnSymbol, String finalizeFnSymbol,
336-
String getValueFnSymbol, String removeFnSymbol) {
333+
Type retType, boolean hasVarArgs, Type intermediateType, URI location,
334+
String initFnSymbol, String updateFnSymbol, String mergeFnSymbol,
335+
String serializeFnSymbol, String finalizeFnSymbol,
336+
String getValueFnSymbol, String removeFnSymbol) {
337337
super(fnName, Arrays.asList(argTypes), retType, hasVarArgs);
338338
this.setLocation(location);
339339
this.intermediateType = (intermediateType.equals(retType)) ? null : intermediateType;
@@ -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
}
@@ -610,13 +616,13 @@ public TFunction toThrift(Type realReturnType, Type[] realArgTypes) {
610616
aggFn.setSerializeFnSymbol(serializeFnSymbol);
611617
}
612618
aggFn.setMergeFnSymbol(mergeFnSymbol);
613-
if (getValueFnSymbol != null) {
619+
if (getValueFnSymbol != null) {
614620
aggFn.setGetValueFnSymbol(getValueFnSymbol);
615621
}
616-
if (removeFnSymbol != null) {
622+
if (removeFnSymbol != null) {
617623
aggFn.setRemoveFnSymbol(removeFnSymbol);
618624
}
619-
if (finalizeFnSymbol != null) {
625+
if (finalizeFnSymbol != null) {
620626
aggFn.setFinalizeFnSymbol(finalizeFnSymbol);
621627
}
622628
if (symbolName != null) {
@@ -691,7 +697,7 @@ public String getProperties() {
691697
properties.put(CreateFunctionStmt.SERIALIZE_KEY, serializeFnSymbol);
692698
properties.put(CreateFunctionStmt.FINALIZE_KEY, finalizeFnSymbol);
693699

694-
//getValueFn and removeFn may be null if not analytic agg
700+
// getValueFn and removeFn may be null if not analytic agg
695701
if (getValueFnSymbol != null) {
696702
properties.put(CreateFunctionStmt.GET_VALUE_KEY, getValueFnSymbol);
697703
}

0 commit comments

Comments
 (0)