Skip to content

Commit f3050f0

Browse files
author
lexluo
committed
[enhancement] Function(create/drop) support the global operation (apache#16973)
1 parent 779d94f commit f3050f0

18 files changed

Lines changed: 677 additions & 164 deletions

File tree

‎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: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1777,15 +1777,15 @@ create_stmt ::=
17771777
RESULT = new CreateClusterStmt(name, properties, password);
17781778
:}*/
17791779
/* Function */
1780-
| KW_CREATE opt_aggregate:isAggregate KW_FUNCTION opt_if_not_exists:ifNotExists function_name:functionName LPAREN func_args_def:args RPAREN
1780+
| 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
17811781
KW_RETURNS type_def:returnType opt_intermediate_type:intermediateType opt_properties:properties
17821782
{:
1783-
RESULT = new CreateFunctionStmt(ifNotExists, isAggregate, functionName, args, returnType, intermediateType, properties);
1783+
RESULT = new CreateFunctionStmt(type, ifNotExists, isAggregate, functionName, args, returnType, intermediateType, properties);
17841784
:}
1785-
| KW_CREATE KW_ALIAS KW_FUNCTION opt_if_not_exists:ifNotExists function_name:functionName LPAREN func_args_def:args RPAREN
1785+
| KW_CREATE opt_var_type:type KW_ALIAS KW_FUNCTION opt_if_not_exists:ifNotExists function_name:functionName LPAREN func_args_def:args RPAREN
17861786
KW_WITH KW_PARAMETER LPAREN ident_list:parameters RPAREN KW_AS expr:func
17871787
{:
1788-
RESULT = new CreateFunctionStmt(ifNotExists, functionName, args, parameters, func);
1788+
RESULT = new CreateFunctionStmt(type, ifNotExists, functionName, args, parameters, func);
17891789
:}
17901790
/* Table */
17911791
| 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
@@ -2878,9 +2878,9 @@ drop_stmt ::=
28782878
RESULT = new DropClusterStmt(ifExists, cluster);
28792879
:}
28802880
/* Function */
2881-
| KW_DROP KW_FUNCTION opt_if_exists:ifExists function_name:functionName LPAREN func_args_def:args RPAREN
2881+
| KW_DROP opt_var_type:type KW_FUNCTION opt_if_exists:ifExists function_name:functionName LPAREN func_args_def:args RPAREN
28822882
{:
2883-
RESULT = new DropFunctionStmt(ifExists, functionName, args);
2883+
RESULT = new DropFunctionStmt(type, ifExists, functionName, args);
28842884
:}
28852885
/* Table */
28862886
| KW_DROP KW_TABLE opt_if_exists:ifExists table_name:name opt_force:force
@@ -3611,9 +3611,9 @@ show_param ::=
36113611
RESULT = new ShowCreateCatalogStmt(catalogName);
36123612
:}
36133613
/* Create Function */
3614-
| KW_CREATE KW_FUNCTION function_name:functionName LPAREN func_args_def:args RPAREN opt_db:dbName
3614+
| KW_CREATE opt_var_type:type KW_FUNCTION function_name:functionName LPAREN func_args_def:args RPAREN opt_db:dbName
36153615
{:
3616-
RESULT = new ShowCreateFunctionStmt(dbName, functionName, args);
3616+
RESULT = new ShowCreateFunctionStmt(type, dbName, functionName, args);
36173617
:}
36183618
/* Cluster */
36193619
| KW_CLUSTERS

‎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
}
@@ -188,7 +195,7 @@ public void analyze(Analyzer analyzer) throws UserException {
188195

189196
private void analyzeCommon(Analyzer analyzer) throws AnalysisException {
190197
// check function name
191-
functionName.analyze(analyzer);
198+
functionName.analyze(analyzer, this.type);
192199

193200
// check operation privilege
194201
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
@@ -1300,6 +1300,13 @@ && collectChildReturnTypes()[0].isDecimalV3()) {
13001300
Function.CompareMode.IS_NONSTRICT_SUPERTYPE_OF);
13011301
}
13021302
}
1303+
// find from the internal database first, if not, then from the global functions
1304+
if (fn == null) {
1305+
Function searchDesc =
1306+
new Function(fnName, Arrays.asList(collectChildReturnTypes()), Type.INVALID, false);
1307+
fn = Env.getCurrentEnv().getGlobalFunctionMgr().getFunction(searchDesc,
1308+
Function.CompareMode.IS_NONSTRICT_SUPERTYPE_OF);
1309+
}
13031310
}
13041311
}
13051312
}

‎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 {

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

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,10 +41,12 @@ public class ShowCreateFunctionStmt extends ShowStmt {
4141
private final FunctionName functionName;
4242
private final FunctionArgsDef argsDef;
4343

44+
private SetType type = SetType.DEFAULT;
4445
// set after analyzed
4546
private FunctionSearchDesc function;
4647

47-
public ShowCreateFunctionStmt(String dbName, FunctionName functionName, FunctionArgsDef argsDef) {
48+
public ShowCreateFunctionStmt(SetType type, String dbName, FunctionName functionName, FunctionArgsDef argsDef) {
49+
this.type = type;
4850
this.dbName = dbName;
4951
this.functionName = functionName;
5052
this.argsDef = argsDef;
@@ -76,7 +78,7 @@ public void analyze(Analyzer analyzer) throws UserException {
7678
}
7779

7880
// analyze function name
79-
functionName.analyze(analyzer);
81+
functionName.analyze(analyzer, SetType.DEFAULT);
8082

8183
// check operation privilege
8284
if (!Env.getCurrentEnv().getAccessManager().checkDbPriv(ConnectContext.get(), dbName, PrivPredicate.SHOW)) {

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

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,21 @@ public class ShowFunctionsStmt extends ShowStmt {
5151

5252
private Expr expr;
5353

54-
public ShowFunctionsStmt(String dbName, boolean isBuiltin, boolean isVerbose, String wild, Expr expr) {
54+
private SetType type = SetType.DEFAULT;
55+
56+
public ShowFunctionsStmt(String dbName, boolean isBuiltin, boolean isVerbose, String wild,
57+
Expr expr) {
58+
this.type = type;
59+
this.dbName = dbName;
60+
this.isBuiltin = isBuiltin;
61+
this.isVerbose = isVerbose;
62+
this.wild = wild;
63+
this.expr = expr;
64+
}
65+
66+
public ShowFunctionsStmt(SetType type, String dbName, boolean isBuiltin, boolean isVerbose, String wild,
67+
Expr expr) {
68+
this.type = type;
5569
this.dbName = dbName;
5670
this.isBuiltin = isBuiltin;
5771
this.isVerbose = isVerbose;

0 commit comments

Comments
 (0)