Note
Access to this page requires authorization. You can try signing in or changing directories.
Access to this page requires authorization. You can try changing directories.
Applies to: ✅ SQL analytics endpoint and Warehouse in Microsoft Fabric
Important
This feature is in preview.
An external function is a T-SQL proxy that calls a published Python function in a Fabric user data functions item. You create the SQL proxy in Fabric Warehouse or the SQL analytics endpoint. The Python logic runs in the user data functions service, not in the T-SQL engine.
The SQL proxy returns a scalar value that you can use in query expressions. For steps to publish the Python function and call it from SQL, see Use user data functions in Fabric Data Warehouse.
Creating a proxy in the SQL analytics endpoint doesn't make persisted table data writable.
Why use external functions?
Use external functions to bring published Python logic into your SQL queries:
- Use Python packages. Add specialized spatial, numeric, or data science logic that isn't available as a built-in T-SQL operation.
- Call an existing Python implementation. Call a published user data function from SQL without translating its implementation into T-SQL.
- Enrich query results from services. Wrap an approved external API or a connection to another Fabric item in Python, and return the result to your query.
Prefer built-in T-SQL functions or T-SQL scalar functions when they can express the calculation. External calls add remote execution overhead and depend on the availability and limits of the services they call.
Supported syntax
Use the following syntax to define and call SQL proxies, and to manage their permissions.
Create or alter a function
For T-SQL syntax, see the external function syntax in CREATE FUNCTION (Transact-SQL).
CREATE [ OR ALTER ] FUNCTION [ schema_name. ] function_name
[ RETURNS return_data_type ]
AS EXTERNAL FUNCTION function_set_name.external_function_name
[;]
Square brackets indicate optional syntax. Use CREATE OR ALTER to create the proxy or update
its SQL definition. If the published Python signature changes,
recreate the SQL proxy.
function_set_name is the name of the published user data functions item, not a SQL schema.
external_function_name is the Python function's name in that item. Your SQL proxy can have a
different name from the Python function.
Don't declare a SQL parameter list or a T-SQL body. Fabric imports the parameters,
parameter types, and return type from the published function. The optional RETURNS clause
overrides the inferred SQL return type.
T-SQL body clauses and scalar UDF options such as INLINE = AUTO, SCHEMABINDING, and
RETURNS NULL ON NULL INPUT aren't part of this external function syntax.
Drop a function
For T-SQL syntax, see DROP FUNCTION (Transact-SQL).
DROP FUNCTION [ IF EXISTS ] [ schema_name. ] function_name
[;]
Call a function
Use the proxy in a scalar expression, not as a table source. This abbreviated form shows a direct call. For full query syntax, see SELECT (Transact-SQL).
SELECT schema_name.function_name
( [ expression [ ,...n ] ] ) [ AS column_alias ]
[;]
Use the schema-qualified SQL proxy name, and include parentheses even if the published function doesn't have input parameters.
Grant permissions on a function
Use these forms to grant common permissions for calling and managing a proxy. For T-SQL syntax, see GRANT object permissions (Transact-SQL).
GRANT <function_permission> [ ,...n ]
ON OBJECT::[ schema_name. ] function_name
TO principal [ ,...n ]
[ WITH GRANT OPTION ]
[;]
<function_permission> ::=
ALTER | CONTROL | EXECUTE | TAKE OWNERSHIP | VIEW DEFINITION
WITH GRANT OPTION lets the recipient grant the permission to others. Calling the SQL
proxy requires EXECUTE, not the SELECT permission used for inline table-valued functions.
Manage Fabric item permissions separately from SQL permissions.
Deny permissions on a function
Use the same function_permission list as for GRANT. For T-SQL syntax, see
DENY (Transact-SQL).
DENY <function_permission> [ ,...n ]
ON OBJECT::[ schema_name. ] function_name
TO principal [ ,...n ]
[ CASCADE ]
[;]
CASCADE also denies the permission to principals who received it from the target principal.
It's required when the target holds the permission with GRANT OPTION.
Revoke permissions on a function
Use the same function_permission list as for GRANT. For T-SQL syntax, see
REVOKE (Transact-SQL).
REVOKE <function_permission> [ ,...n ]
ON OBJECT::[ schema_name. ] function_name
FROM principal [ ,...n ]
[ CASCADE ]
[;]
CASCADE also revokes permissions delegated by the recipient.
Create, alter, and drop a function
Before you create a proxy, publish the Python function in a user data functions item.
These examples assume you published hello_fabric(name: str) -> str in the item.
The function returns the name you pass to it, as shown in
Use user data functions in Fabric Data Warehouse.
Replace FunctionSetName with your user data functions item name. Create the SQL proxy in
your warehouse or SQL analytics endpoint:
CREATE OR ALTER FUNCTION dbo.hello_fabric
AS EXTERNAL FUNCTION FunctionSetName.hello_fabric;
GO
Use schema-qualified SQL names. Keep each function definition in its own batch.
GO separates batches in SQL tools. If you're using an application driver, submit each
batch separately without sending GO.
Override the return type
Fabric typically infers varchar(max) for a Python str return value. If you know the
maximum length of the returned values, specify a more precise SQL type:
CREATE OR ALTER FUNCTION dbo.hello_fabric
RETURNS varchar(100)
AS EXTERNAL FUNCTION FunctionSetName.hello_fabric;
GO
Choose a type and length that fit the values returned by Python. The RETURNS clause doesn't
change the Python implementation or its return annotation.
Update or remove a proxy
Use CREATE OR ALTER to update the SQL proxy definition. This statement doesn't edit or
publish the Python function.
If the published function's signature changes, publish the updated Python function, then rerun
CREATE OR ALTER FUNCTION to import the updated parameters and return type without dropping the proxy.
Changing the remote signature doesn't automatically update the database metadata. Coordinate these
changes with anyone who calls the SQL proxy.
Drop an obsolete proxy with:
DROP FUNCTION IF EXISTS dbo.hello_fabric_obsolete;
Dropping the proxy doesn't delete the published Python function or its Fabric item. Dropping and recreating a proxy removes its object-level permissions; reapply the required grants after recreating it.
Keep the SQL proxy definition and Python implementation in source control. For the Python item, see Git integration and deployment pipelines.
Function implementation
Write and maintain the executable logic in the published user data function. Don't write
BEGIN...END, RETURN, or a query body in the SQL proxy definition.
Use the Python programming model to define annotated parameters and a return type, add libraries, and configure connections. Test and publish the function before binding or updating its SQL proxy.
If you rename or delete the published Python function, update the SQL reference accordingly. The SQL proxy doesn't copy the Python implementation into the database.
Remote execution and batching
An external function call sends input values to the user data functions service and returns the result to the query. Plan for remote latency, payload sizes, execution timeouts, and errors from downstream services. For current limits, see Service details and limitations of Fabric user data functions.
Batch mode can group up to 900 function invocations in one remote request. Enable the
batch-mode preview and configure max_batch_size=900 on the Python function's
@udf.function() decorator, as described in
Configure batch mode execution.
Fabric imports the remote batch configuration when you create or alter the SQL proxy. If you
change max_batch_size, publish the updated Python function, then rerun CREATE OR ALTER FUNCTION
to synchronize the proxy's metadata. Execution fails if the stored and published batch sizes differ.
When the query pattern supports batching, Fabric groups calls into remote requests. Don't assume
every query uses batching or sends exactly 900 inputs. Batching reduces request overhead; it
doesn't move Python execution into the T-SQL engine.
Call external functions
Call the proxy in a query expression:
SELECT dbo.hello_fabric('Fabric') AS Result;
With the sample Python function, the query returns Fabric.
Pass parameter values
Pass arguments in the order shown in the imported SQL signature. Use literals, variables, or expressions that can be converted to the expected SQL parameter types:
DECLARE @Name varchar(100) = 'Fabric';
SELECT dbo.hello_fabric(@Name) AS Result;
These examples pass each argument explicitly; they don't rely on Python defaults or SQL's
DEFAULT keyword. Inspect the imported parameter names and types in sys.parameters.
Test null inputs, Python errors, and SQL/Python type conversions with your published function before you use it in queries over many rows.
Apply a function to query inputs
This example uses GENERATE_SERIES (Transact-SQL) to produce three input rows without requiring a sample table:
SELECT s.value AS InputValue,
dbo.hello_fabric(CONCAT('Row ', s.value)) AS Result
FROM GENERATE_SERIES(1, 3, 1) AS s
ORDER BY s.value;
For the same published function, the returned strings are Row 1, Row 2, and Row 3.
The query processor determines whether these calls can use batch execution.
You can also use proxies in WHERE and GROUP BY clauses. In Warehouse, you can use them
in expressions in INSERT and UPDATE statements. Keep remote calls to those your query
needs, because repeated expressions can add remote work.
For SQL query logic that returns rows rather than a scalar value, use an inline table-valued function.
Security and permissions
To connect to the warehouse or SQL analytics endpoint, you need at least Read permission on the item, either directly or through a workspace role. See Fabric security and SQL granular permissions.
You also need Read access to the user data functions item and permission
to invoke its published functions. GRANT EXECUTE on the SQL proxy doesn't configure access
to that Fabric item or the connections used by Python.
If you're a workspace Admin, Member, or Contributor, you can create SQL functions.
| Operation | Required SQL permission |
|---|---|
| Create a SQL proxy | CREATE FUNCTION in the database and ALTER on the target schema. |
| Alter a SQL proxy | ALTER on the function. |
| Drop a SQL proxy | CONTROL on the function or ALTER on its schema. |
| Call a SQL proxy | EXECUTE on the function. |
| View protected SQL metadata | VIEW DEFINITION on the function. This permission doesn't grant access to the Python implementation. |
| Deny or revoke a SQL permission | CONTROL on the function. |
You can also meet these requirements through higher-level permissions. To grant a permission,
you need GRANT OPTION or a higher permission that lets you grant it.
For example, grant SQL execution and metadata access separately. Replace FunctionUsers
with the name of your Microsoft Entra user or group.
GRANT EXECUTE ON OBJECT::dbo.hello_fabric TO [FunctionUsers];
GRANT VIEW DEFINITION ON OBJECT::dbo.hello_fabric TO [FunctionUsers];
You can't run CREATE USER. Fabric creates the database user automatically when you grant
or deny permissions.
Prefer object-level grants. A schema-level EXECUTE grant includes current and future
executable objects in that schema.
Use DENY to block a permission, including grants inherited through roles or groups.
It doesn't restrict the SQL proxy's owner. For example, block calls through this proxy:
DENY EXECUTE ON OBJECT::dbo.hello_fabric TO [FunctionUsers];
Use REVOKE to remove an explicit GRANT or DENY without blocking other grants.
For example, remove the preceding denial:
REVOKE EXECUTE ON OBJECT::dbo.hello_fabric FROM [FunctionUsers];
Removing the denial doesn't restore the earlier explicit GRANT. Other grants can still
allow access.
Denying calls to a SQL proxy doesn't prevent separate invocation paths to the published
Python function. Manage those paths through the user data functions item's permissions.
Caution
Python functions can send data to external services. Use approved endpoints and connections, avoid sending sensitive data unless authorized, and review the identity and permissions used by each connection. SQL permissions on a proxy don't define the security of downstream systems.
Metadata and function definitions
Query these catalog views and functions in the database that contains the SQL proxy:
| Catalog view or function | Information |
|---|---|
| sys.objects (Transact-SQL) | External SQL proxies have type = 'XF', unlike T-SQL scalar functions with type = 'FN'. |
| sys.parameters (Transact-SQL) | Imported input names, order, and SQL types. parameter_id = 0 describes the scalar return value; positive IDs describe inputs. |
| sys.types (Transact-SQL) | Type names for the imported SQL signature. Join to sys.parameters by user_type_id. |
| OBJECT_ID (Transact-SQL) | Returns the SQL proxy's object ID from its schema-qualified name. |
| SCHEMA_NAME (Transact-SQL) | Returns a schema name from a schema ID, such as sys.objects.schema_id. |
For example, list the external functions in your current database:
SELECT SCHEMA_NAME(schema_id) AS SchemaName,
name AS FunctionName,
object_id,
type,
type_desc
FROM sys.objects
WHERE type = 'XF';
Inspect a proxy's imported signature:
SELECT p.parameter_id,
p.name AS ParameterName,
t.name AS TypeName,
p.max_length,
p.precision,
p.scale
FROM sys.parameters AS p
INNER JOIN sys.types AS t
ON t.user_type_id = p.user_type_id
WHERE p.object_id = OBJECT_ID('dbo.hello_fabric')
ORDER BY p.parameter_id;
The metadata you can see depends on the objects you own and the permissions you have.
Name lookups can return NULL if the proxy doesn't exist or you don't have metadata permissions.
These lookups use the current database. Object IDs aren't globally unique.
The database stores the proxy's metadata, not the Python source. sys.sql_modules and
OBJECT_DEFINITION aren't a way to retrieve the Python implementation. View and maintain that
implementation in its user data functions item.
For execution diagnostics, see
Troubleshoot user data functions.
Use queryinsights.exec_requests_history to locate queries and
queryinsights.external_api_call_stats for call counts, retries, batch execution, remote wait
time, and payload sizes. To view Python execution logs, open the
user data functions item.
Related content
- Use user data functions in Fabric Data Warehouse
- Fabric user data functions overview
- Fabric user data function programming model overview
- Service details and limitations of Fabric user data functions
- SQL granular permissions in Microsoft Fabric
- Stored procedures
- Views
- T-SQL scalar functions
- Inline table-valued functions
- Develop warehouse projects in Visual Studio Code
- CREATE FUNCTION (Transact-SQL)
- DROP FUNCTION (Transact-SQL)