2020import org .apache .doris .catalog .Column ;
2121import org .apache .doris .catalog .Env ;
2222import org .apache .doris .catalog .FunctionSearchDesc ;
23+ import org .apache .doris .catalog .FunctionUtil ;
2324import org .apache .doris .catalog .ScalarType ;
2425import org .apache .doris .cluster .ClusterNamespace ;
26+ import org .apache .doris .common .AnalysisException ;
2527import org .apache .doris .common .ErrorCode ;
2628import org .apache .doris .common .ErrorReport ;
2729import 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}
0 commit comments