1616import com .facebook .airlift .log .Logger ;
1717import com .facebook .presto .common .CatalogSchemaName ;
1818import com .facebook .presto .common .QualifiedObjectName ;
19+ import com .facebook .presto .common .type .NamedTypeSignature ;
1920import com .facebook .presto .common .type .Type ;
2021import com .facebook .presto .common .type .TypeManager ;
2122import com .facebook .presto .common .type .TypeSignature ;
@@ -91,6 +92,7 @@ public class NativeFunctionNamespaceManager
9192 private final Supplier <Map <SqlFunctionId , SqlInvokedFunction >> memoizedFunctionsSupplier ;
9293 private final FunctionMetadataManager functionMetadataManager ;
9394 private final LoadingCache <Signature , SqlFunctionSupplier > specializedFunctionKeyCache ;
95+ private final List <Type > types ;
9496
9597 @ Inject
9698 public NativeFunctionNamespaceManager (
@@ -111,6 +113,7 @@ public NativeFunctionNamespaceManager(
111113 .maximumSize (1000 )
112114 .expireAfterWrite (1 , HOURS )
113115 .build (CacheLoader .from (this ::doGetSpecializedFunctionKey ));
116+ this .types = functionMetadataManager .getTypes ();
114117 }
115118
116119 private SqlFunctionSupplier doGetSpecializedFunctionKey (Signature signature )
@@ -221,20 +224,8 @@ protected SqlInvokedFunction createSqlInvokedFunction(String functionName, JsonB
221224 List <TypeVariableConstraint > typeVariableConstraintsList = jsonBasedUdfFunctionMetaData .getTypeVariableConstraints ().isPresent () ?
222225 jsonBasedUdfFunctionMetaData .getTypeVariableConstraints ().get () : Collections .emptyList ();
223226
224- if (functionName .equals ("approx_distinct" )) {
225- List <TypeSignature > newParameterTypeList = new ArrayList <>();
226- for (int j = 0 ; j < parameterNameList .size (); j ++) {
227- if (parameterTypeList .get (j ).getTypeSignatureBase ().getStandardTypeBase ().startsWith ("decimal" ) || parameterTypeList .get (j ).getTypeSignatureBase ().getStandardTypeBase ().startsWith ("DECIMAL" )) {
228- List <TypeSignature > paramtersList = parameterTypeList .get (j ).getTypeParametersAsTypeSignatures ();
229- newParameterTypeList .add (new TypeSignature (
230- "decimal" , TypeSignatureParameter .of (paramtersList .get (0 ).getBase ()), TypeSignatureParameter .of (paramtersList .get (1 ).getBase ())));
231- }
232- else {
233- newParameterTypeList .add (parameterTypeList .get (j ));
234- }
235- }
236- parameterTypeList = newParameterTypeList ;
237- }
227+ TypeSignature outputType = helperTypeSignature (jsonBasedUdfFunctionMetaData .getOutputType (), types );
228+ parameterTypeList = helperTypeSignatureList (parameterTypeList , types );
238229
239230 ImmutableList .Builder <Parameter > parameterBuilder = ImmutableList .builder ();
240231 for (int i = 0 ; i < parameterNameList .size (); i ++) {
@@ -256,7 +247,7 @@ protected SqlInvokedFunction createSqlInvokedFunction(String functionName, JsonB
256247 qualifiedFunctionName ,
257248 parameterBuilder .build (),
258249 typeVariableConstraintsBuilder .build (),
259- jsonBasedUdfFunctionMetaData . getOutputType () ,
250+ outputType ,
260251 jsonBasedUdfFunctionMetaData .getDocString (),
261252 jsonBasedUdfFunctionMetaData .getRoutineCharacteristics (),
262253 "" ,
@@ -360,6 +351,94 @@ public final FunctionHandle getFunctionHandle(Optional<? extends FunctionNamespa
360351 return functionHandle ;
361352 }
362353
354+ public static TypeSignature helperTypeSignature (TypeSignature typeSignature , List <Type > types )
355+ {
356+ List <TypeSignature > typeSignatureList = helperTypeSignatureList (ImmutableList .of (typeSignature ), types );
357+ checkArgument (!typeSignatureList .isEmpty (), "Type signature list is empty for : " + typeSignature );
358+ return typeSignatureList .get (0 );
359+ }
360+
361+ public static List <TypeSignature > helperTypeSignatureList (List <TypeSignature > typeSignatures , List <Type > types )
362+ {
363+ List <TypeSignature > newTypeSignaturesList = new ArrayList <>();
364+ for (TypeSignature typeSignature : typeSignatures ) {
365+ if (!typeSignature .getParameters ().isEmpty ()) {
366+ TypeSignature newTypeSignature =
367+ new TypeSignature (
368+ typeSignature .getBase (),
369+ getAccurateTypeSignatureParameterKindLists (
370+ typeSignature .getParameters (), types ));
371+ newTypeSignaturesList .add (newTypeSignature );
372+ }
373+ else {
374+ newTypeSignaturesList .add (typeSignature );
375+ }
376+ }
377+ return newTypeSignaturesList ;
378+ }
379+
380+ private static List <TypeSignatureParameter > getAccurateTypeSignatureParameterKindLists (List <TypeSignatureParameter > typeSignatureParameterList , List <Type > types )
381+ {
382+ List <TypeSignatureParameter > newParameterTypeList = new ArrayList <>();
383+ for (TypeSignatureParameter parameter : typeSignatureParameterList ) {
384+ if (parameter .isLongLiteral ()) {
385+ newParameterTypeList .add (parameter );
386+ continue ;
387+ }
388+
389+ boolean isNamedTypeSignature = parameter .isNamedTypeSignature ();
390+ TypeSignature parameterTypeSignature ;
391+ // If it's a named type signatures only in the case of row signature types.
392+ if (isNamedTypeSignature ) {
393+ parameterTypeSignature = parameter .getNamedTypeSignature ().getTypeSignature ();
394+ }
395+ else {
396+ parameterTypeSignature = parameter .getTypeSignature ();
397+ }
398+
399+ if (parameterTypeSignature .getParameters ().isEmpty ()) {
400+ boolean changeTypeToVariable = helperFunctions (parameterTypeSignature , types );
401+ if (changeTypeToVariable ) {
402+ newParameterTypeList .add (
403+ TypeSignatureParameter .of (parameterTypeSignature .getBase ()));
404+ }
405+ else {
406+ if (isNamedTypeSignature ) {
407+ newParameterTypeList .add (TypeSignatureParameter .of (parameter .getNamedTypeSignature ()));
408+ }
409+ else {
410+ newParameterTypeList .add (TypeSignatureParameter .of (parameterTypeSignature ));
411+ }
412+ }
413+ }
414+ else {
415+ TypeSignature typeSignature =
416+ new TypeSignature (
417+ parameterTypeSignature .getBase (),
418+ getAccurateTypeSignatureParameterKindLists (
419+ parameterTypeSignature .getParameters (), types ));
420+ if (isNamedTypeSignature ) {
421+ newParameterTypeList .add (
422+ TypeSignatureParameter .of (
423+ new NamedTypeSignature (
424+ Optional .empty (),
425+ typeSignature )));
426+ }
427+ else {
428+ newParameterTypeList .add (TypeSignatureParameter .of (typeSignature ));
429+ }
430+ }
431+ }
432+ return newParameterTypeList ;
433+ }
434+
435+ private static boolean helperFunctions (TypeSignature parameterType , List <Type > types )
436+ {
437+ return types .stream ().noneMatch (key ->
438+ key .getTypeSignature ().getTypeSignatureBase ().getStandardTypeBase ()
439+ .equals (parameterType .getStandardTypeSignature ().getTypeSignatureBase ().getStandardTypeBase ()));
440+ }
441+
363442 private SqlFunction getSqlFunctionFromSignature (Signature signature )
364443 {
365444 SqlFunctionSupplier functionKey ;
0 commit comments