@@ -71,7 +71,7 @@ public Optional<Signature> match(Collection<? extends SqlFunction> candidates, L
7171 .filter (function -> !function .getSignature ().getTypeVariableConstraints ().isEmpty ())
7272 .collect (Collectors .toList ());
7373
74- match = matchFunctionExact (genericCandidates , parameterTypes );
74+ match = matchFunctionGeneric (genericCandidates , parameterTypes );
7575 if (match .isPresent ()) {
7676 return match ;
7777 }
@@ -91,6 +91,28 @@ private Optional<Signature> matchFunctionExact(List<SqlFunction> candidates, Lis
9191 return matchFunction (candidates , actualParameters , false );
9292 }
9393
94+ private Optional <Signature > matchFunctionGeneric (List <SqlFunction > candidates , List <TypeSignatureProvider > actualParameters )
95+ {
96+ List <ApplicableFunction > applicableFunctions = identifyApplicableFunctions (candidates , actualParameters , false );
97+ if (applicableFunctions .isEmpty ()) {
98+ return Optional .empty ();
99+ }
100+
101+ if (applicableFunctions .size () == 1 ) {
102+ return Optional .of (getOnlyElement (applicableFunctions ).getBoundSignature ());
103+ }
104+
105+ List <Signature > deduplicatedSignatures = applicableFunctions .stream ()
106+ .map (applicableFunction -> applicableFunction .boundSignature )
107+ .distinct ()
108+ .collect (toImmutableList ());
109+ if (deduplicatedSignatures .size () == 1 ) {
110+ return Optional .of (getOnlyElement (deduplicatedSignatures ));
111+ }
112+
113+ throw new PrestoException (AMBIGUOUS_FUNCTION_CALL , getErrorMessage (applicableFunctions ));
114+ }
115+
94116 private Optional <Signature > matchFunctionWithCoercion (Collection <? extends SqlFunction > candidates , List <TypeSignatureProvider > actualParameters )
95117 {
96118 return matchFunction (candidates , actualParameters , true );
@@ -112,6 +134,11 @@ private Optional<Signature> matchFunction(Collection<? extends SqlFunction> cand
112134 return Optional .of (getOnlyElement (applicableFunctions ).getBoundSignature ());
113135 }
114136
137+ throw new PrestoException (AMBIGUOUS_FUNCTION_CALL , getErrorMessage (applicableFunctions ));
138+ }
139+
140+ private String getErrorMessage (List <ApplicableFunction > applicableFunctions )
141+ {
115142 StringBuilder errorMessageBuilder = new StringBuilder ();
116143 errorMessageBuilder .append ("Could not choose a best candidate operator. Explicit type casts must be added.\n " );
117144 errorMessageBuilder .append ("Candidates are:\n " );
@@ -120,7 +147,8 @@ private Optional<Signature> matchFunction(Collection<? extends SqlFunction> cand
120147 errorMessageBuilder .append (function .getBoundSignature ().toString ());
121148 errorMessageBuilder .append ("\n " );
122149 }
123- throw new PrestoException (AMBIGUOUS_FUNCTION_CALL , errorMessageBuilder .toString ());
150+
151+ return errorMessageBuilder .toString ();
124152 }
125153
126154 private List <ApplicableFunction > identifyApplicableFunctions (Collection <? extends SqlFunction > candidates , List <TypeSignatureProvider > actualParameters , boolean allowCoercion )
0 commit comments