3333import com .google .common .collect .Lists ;
3434import com .google .common .collect .Maps ;
3535import org .apache .commons .collections .CollectionUtils ;
36+ import org .apache .commons .lang3 .StringUtils ;
3637
3738import java .util .List ;
3839import java .util .Map ;
@@ -86,24 +87,31 @@ public Optional<List<FunctionBuilder>> tryGetBuiltinBuilders(String name) {
8687
8788 // currently we only find function by name and arity and args' types.
8889 public FunctionBuilder findFunctionBuilder (String dbName , String name , List <?> arguments ) {
90+ List <FunctionBuilder > functionBuilders = null ;
8991 int arity = arguments .size ();
90- List <FunctionBuilder > functionBuilders = name2InternalBuiltinBuilders .get (name .toLowerCase ());
91- if (CollectionUtils .isEmpty (functionBuilders ) && AggStateFunctionBuilder .isAggStateCombinator (name )) {
92- String nestedName = AggStateFunctionBuilder .getNestedName (name );
93- String combinatorSuffix = AggStateFunctionBuilder .getCombinatorSuffix (name );
94-
95- functionBuilders = name2InternalBuiltinBuilders .get (nestedName .toLowerCase ());
96-
97- if (functionBuilders != null ) {
98- functionBuilders = functionBuilders .stream ().map (builder -> {
99- return new AggStateFunctionBuilder (combinatorSuffix , builder );
100- }).filter (functionBuilder -> functionBuilder .canApply (arguments )).collect (Collectors .toList ());
92+ String qualifiedName = StringUtils .isEmpty (dbName ) ? name : dbName + "." + name ;
93+
94+ if (StringUtils .isEmpty (dbName )) {
95+ // search internal function only if dbName is empty
96+ functionBuilders = name2InternalBuiltinBuilders .get (name .toLowerCase ());
97+ if (CollectionUtils .isEmpty (functionBuilders ) && AggStateFunctionBuilder .isAggStateCombinator (name )) {
98+ String nestedName = AggStateFunctionBuilder .getNestedName (name );
99+ String combinatorSuffix = AggStateFunctionBuilder .getCombinatorSuffix (name );
100+ functionBuilders = name2InternalBuiltinBuilders .get (nestedName .toLowerCase ());
101+ if (functionBuilders != null ) {
102+ functionBuilders = functionBuilders .stream ()
103+ .map (builder -> new AggStateFunctionBuilder (combinatorSuffix , builder ))
104+ .filter (functionBuilder -> functionBuilder .canApply (arguments ))
105+ .collect (Collectors .toList ());
106+ }
101107 }
102108 }
103- if (functionBuilders == null || functionBuilders .isEmpty ()) {
109+
110+ // search udf
111+ if (CollectionUtils .isEmpty (functionBuilders )) {
104112 functionBuilders = findUdfBuilder (dbName , name );
105113 if (functionBuilders == null || functionBuilders .isEmpty ()) {
106- throw new AnalysisException ("Can not found function '" + name + "'" );
114+ throw new AnalysisException ("Can not found function '" + qualifiedName + "'" );
107115 }
108116 }
109117
@@ -113,15 +121,15 @@ public FunctionBuilder findFunctionBuilder(String dbName, String name, List<?> a
113121 .collect (Collectors .toList ());
114122 if (candidateBuilders .isEmpty ()) {
115123 String candidateHints = getCandidateHint (name , functionBuilders );
116- throw new AnalysisException ("Can not found function '" + name
124+ throw new AnalysisException ("Can not found function '" + qualifiedName
117125 + "' which has " + arity + " arity. Candidate functions are: " + candidateHints );
118126 }
119-
120127 if (candidateBuilders .size () > 1 ) {
121128 String candidateHints = getCandidateHint (name , candidateBuilders );
122129 // NereidsPlanner not supported override function by the same arity, should we support it?
123- throw new AnalysisException ("Function '" + name + "' is ambiguous: " + candidateHints );
130+ throw new AnalysisException ("Function '" + qualifiedName + "' is ambiguous: " + candidateHints );
124131 }
132+
125133 return candidateBuilders .get (0 );
126134 }
127135
0 commit comments