Skip to content

Commit 71be995

Browse files
author
lexluo
committed
[enhancement] Function(create/drop) support the global operation (apache#16973)
1 parent 2d7d810 commit 71be995

18 files changed

Lines changed: 682 additions & 167 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
@@ -1775,15 +1775,15 @@ create_stmt ::=
17751775
RESULT = new CreateClusterStmt(name, properties, password);
17761776
:}*/
17771777
/* Function */
1778-
| KW_CREATE opt_aggregate:isAggregate KW_FUNCTION opt_if_not_exists:ifNotExists function_name:functionName LPAREN func_args_def:args RPAREN
1778+
| 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
17791779
KW_RETURNS type_def:returnType opt_intermediate_type:intermediateType opt_properties:properties
17801780
{:
1781-
RESULT = new CreateFunctionStmt(ifNotExists, isAggregate, functionName, args, returnType, intermediateType, properties);
1781+
RESULT = new CreateFunctionStmt(type, ifNotExists, isAggregate, functionName, args, returnType, intermediateType, properties);
17821782
:}
1783-
| KW_CREATE KW_ALIAS KW_FUNCTION opt_if_not_exists:ifNotExists function_name:functionName LPAREN func_args_def:args RPAREN
1783+
| KW_CREATE opt_var_type:type KW_ALIAS KW_FUNCTION opt_if_not_exists:ifNotExists function_name:functionName LPAREN func_args_def:args RPAREN
17841784
KW_WITH KW_PARAMETER LPAREN ident_list:parameters RPAREN KW_AS expr:func
17851785
{:
1786-
RESULT = new CreateFunctionStmt(ifNotExists, functionName, args, parameters, func);
1786+
RESULT = new CreateFunctionStmt(type, ifNotExists, functionName, args, parameters, func);
17871787
:}
17881788
/* Table */
17891789
| 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
@@ -2866,9 +2866,9 @@ drop_stmt ::=
28662866
RESULT = new DropClusterStmt(ifExists, cluster);
28672867
:}
28682868
/* Function */
2869-
| KW_DROP KW_FUNCTION opt_if_exists:ifExists function_name:functionName LPAREN func_args_def:args RPAREN
2869+
| KW_DROP opt_var_type:type KW_FUNCTION opt_if_exists:ifExists function_name:functionName LPAREN func_args_def:args RPAREN
28702870
{:
2871-
RESULT = new DropFunctionStmt(ifExists, functionName, args);
2871+
RESULT = new DropFunctionStmt(type, ifExists, functionName, args);
28722872
:}
28732873
/* Table */
28742874
| KW_DROP KW_TABLE opt_if_exists:ifExists table_name:name opt_force:force
@@ -3588,9 +3588,9 @@ show_param ::=
35883588
RESULT = new ShowCreateCatalogStmt(catalogName);
35893589
:}
35903590
/* Create Function */
3591-
| KW_CREATE KW_FUNCTION function_name:functionName LPAREN func_args_def:args RPAREN opt_db:dbName
3591+
| KW_CREATE opt_var_type:type KW_FUNCTION function_name:functionName LPAREN func_args_def:args RPAREN opt_db:dbName
35923592
{:
3593-
RESULT = new ShowCreateFunctionStmt(dbName, functionName, args);
3593+
RESULT = new ShowCreateFunctionStmt(type, dbName, functionName, args);
35943594
:}
35953595
/* Cluster */
35963596
| 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
@@ -94,6 +94,7 @@ public class CreateFunctionStmt extends DdlStmt {
9494
public static final String IS_RETURN_NULL = "always_nullable";
9595
private static final Logger LOG = LogManager.getLogger(CreateFunctionStmt.class);
9696

97+
private SetType type = SetType.DEFAULT;
9798
private final boolean ifNotExists;
9899
private final FunctionName functionName;
99100
private final boolean isAggregate;
@@ -118,9 +119,10 @@ public class CreateFunctionStmt extends DdlStmt {
118119
// timeout for both connection and read. 10 seconds is long enough.
119120
private static final int HTTP_TIMEOUT_MS = 10000;
120121

121-
public CreateFunctionStmt(boolean ifNotExists, boolean isAggregate, FunctionName functionName,
122-
FunctionArgsDef argsDef,
123-
TypeDef returnType, TypeDef intermediateType, Map<String, String> properties) {
122+
public CreateFunctionStmt(SetType type, boolean ifNotExists, boolean isAggregate, FunctionName functionName,
123+
FunctionArgsDef argsDef,
124+
TypeDef returnType, TypeDef intermediateType, Map<String, String> properties) {
125+
this.type = type;
124126
this.ifNotExists = ifNotExists;
125127
this.functionName = functionName;
126128
this.isAggregate = isAggregate;
@@ -137,8 +139,9 @@ public CreateFunctionStmt(boolean ifNotExists, boolean isAggregate, FunctionName
137139
this.originFunction = null;
138140
}
139141

140-
public CreateFunctionStmt(boolean ifNotExists, FunctionName functionName, FunctionArgsDef argsDef,
142+
public CreateFunctionStmt(SetType type, boolean ifNotExists, FunctionName functionName, FunctionArgsDef argsDef,
141143
List<String> parameters, Expr originFunction) {
144+
this.type = type;
142145
this.ifNotExists = ifNotExists;
143146
this.functionName = functionName;
144147
this.isAlias = true;
@@ -154,6 +157,10 @@ public CreateFunctionStmt(boolean ifNotExists, FunctionName functionName, Functi
154157
this.properties = ImmutableSortedMap.of();
155158
}
156159

160+
public SetType getType() {
161+
return type;
162+
}
163+
157164
public boolean isIfNotExists() {
158165
return ifNotExists;
159166
}
@@ -187,7 +194,7 @@ public void analyze(Analyzer analyzer) throws UserException {
187194

188195
private void analyzeCommon(Analyzer analyzer) throws AnalysisException {
189196
// check function name
190-
functionName.analyze(analyzer);
197+
functionName.analyze(analyzer, this.type);
191198

192199
// check operation privilege
193200
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
@@ -1291,6 +1291,13 @@ && collectChildReturnTypes()[0].isDecimalV3()) {
12911291
Function.CompareMode.IS_NONSTRICT_SUPERTYPE_OF);
12921292
}
12931293
}
1294+
// find from the internal database first, if not, then from the global functions
1295+
if (fn == null) {
1296+
Function searchDesc =
1297+
new Function(fnName, Arrays.asList(collectChildReturnTypes()), Type.INVALID, false);
1298+
fn = Env.getCurrentEnv().getGlobalFunctionMgr().getFunction(searchDesc,
1299+
Function.CompareMode.IS_NONSTRICT_SUPERTYPE_OF);
1300+
}
12941301
}
12951302
}
12961303
}

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