Forum Discussion
Fabric User Data Function parameter defaults?
- 1 year ago
Hi MangoMagic
Thank you for contacting the Microsoft Fabric Community Forum.
Currently, Microsoft Fabric's Python User Defined Functions (UDFs) created with the @udf.function decorator do not allow default values or optional parameters in their definitions. If you try to set a default value for a parameter, such as option: str = "Get watermark", the function will not publish or compile correctly in the Fabric environment.
To avoid issues, make sure all parameters are explicitly defined and provided when calling the function. If you need default behavior, handle it inside the function by checking if a parameter is empty or null and then assigning a value as needed. This method works within the platform's current limitations and ensures your UDFs will run as expected.
from microsoft.fabric import udf import microsoft.fabric.functions as fn @udf.connection(argName="sqlDB", alias="Control") @udf.function() def GetWatermark(sqlDB: fn.FabricSqlConnection, ID: int, option: str, description: str) -> list: if not option: option = "get watermark" else: option = option.lower() if option == "get watermark": query = f"EXEC [dbo].[Get_ETLSettings] @ID = {ID}, @Option = '{option}';" else: query = f"EXEC [dbo].[Get_ETLSettings] @ID = {ID}, @Option = '{option}';" connection = sqlDB.connect() cursor = connection.cursor() cursor.execute(query) results = cursor.fetchall() cursor.close() connection.close() return resultsRegards,
Karpurapu D,
Microsoft Fabric Community Support Team.
Hi MangoMagic
Thank you for contacting the Microsoft Fabric Community Forum.
Currently, Microsoft Fabric's Python User Defined Functions (UDFs) created with the @udf.function decorator do not allow default values or optional parameters in their definitions. If you try to set a default value for a parameter, such as option: str = "Get watermark", the function will not publish or compile correctly in the Fabric environment.
To avoid issues, make sure all parameters are explicitly defined and provided when calling the function. If you need default behavior, handle it inside the function by checking if a parameter is empty or null and then assigning a value as needed. This method works within the platform's current limitations and ensures your UDFs will run as expected.
from microsoft.fabric import udf
import microsoft.fabric.functions as fn
@udf.connection(argName="sqlDB", alias="Control")
@udf.function()
def GetWatermark(sqlDB: fn.FabricSqlConnection, ID: int, option: str, description: str) -> list:
if not option:
option = "get watermark"
else:
option = option.lower()
if option == "get watermark":
query = f"EXEC [dbo].[Get_ETLSettings] @ID = {ID}, @Option = '{option}';"
else:
query = f"EXEC [dbo].[Get_ETLSettings] @ID = {ID}, @Option = '{option}';"
connection = sqlDB.connect()
cursor = connection.cursor()
cursor.execute(query)
results = cursor.fetchall()
cursor.close()
connection.close()
return results
Regards,
Karpurapu D,
Microsoft Fabric Community Support Team.
Thank you, that worked 😃