# Call Chalk Functions from SQL
source: https://docs.chalk.ai/docs/chalksql/catalog-functions

## Call deployed Chalk Functions from Chalk SQL and run eligible Python logic inside the query with static acceleration.

Call a deployed Chalk Function in a SQL expression
with functions.<name>(...). Chalk resolves the function in your environment's
catalog and passes the SQL arguments to it for each row. To find deployed
functions and their qualified catalog names, query
chalk.information_schema.compute_functions.

```sql
SELECT function_name, qualified_name
FROM chalk.information_schema.compute_functions
ORDER BY function_name
```

### Audit deployment events against release policies

Your deploy pipeline lets staging run release candidates and reserves production
for stable releases. To audit deployment events, reuse the pipeline's Python
release gate in SQL. The gate checks the whole version string and uses each
environment's policy to decide whether it accepts a prerelease.

Save this code as release_gate.py and run it in the Chalk environment
where you plan to query it. The decorator registers the function.
See Defining a function for deployment details.

```py
import re

import chalkcompute

@chalkcompute.function
def release_gate(version: str, allow_prerelease: bool) -> str:
    if re.fullmatch(r"v?[1-9]\d*(\.\d+)*", version) is not None:
        return "stable"
    if re.fullmatch(r"v?[1-9]\d*(\.\d+)*-(alpha|beta|rc)\.\d+", version) is None:
        return "invalid_version"
    return "prerelease_allowed" if allow_prerelease else "prerelease_blocked"
```

The query joins deployment events from BigQuery to release policies from
PostgreSQL on environment. Each call takes the version from the event and
allow_prerelease from the matching policy. The worksheet shows sample results.
To run the query on your data, deploy the function and replace the sample table
names in the SQL Explorer.

[Interactive example: Audit deployments with a Python release gate in Chalk SQL](https://chalk.ai/marketing-components?pane=chalk-sql-catalog-function)

```sql
SELECT
    d.deployment_id,
    d.environment,
    d.version,
    functions.release_gate(
        d.version,
        p.allow_prerelease
    ) AS gate_result
FROM my_bigquery.analytics.deployment_events AS d
JOIN my_postgres.public.release_policies AS p
    ON d.environment = p.environment
ORDER BY d.deployment_id
```

| deployment_id | environment | version     | gate_result        |
| ------------- | ----------- | ----------- | ------------------ |
| 101           | production  | v2.4.1      | stable             |
| 102           | staging     | v2.4.2-rc.1 | prerelease_allowed |
| 103           | production  | v2.4.2-rc.1 | prerelease_blocked |
| 104           | staging     | latest      | invalid_version    |

The same release candidate passes in staging and is blocked in production.
The latest tag fails the version checks and is flagged for review.

The SQL namespace is functions and the qualified catalog name is
function.release_gate. To call it by that name, use
catalog_call('function.release_gate', d.version, p.allow_prerelease).

### Static acceleration

In environments with function acceleration enabled, Chalk tries to compile the
deployed Python function into a native expression when you deploy a new
version. The compiler turns the re.fullmatch checks and Python branches in
release_gate into native operations. When the compiled expression is available
and compatible with the SQL arguments, Chalk evaluates it over batches of joined
rows inside the query plan without sending a request to the function's scaling
group. Use non-nullable version and allow_prerelease columns for this example.

Read about how Chalk compiles Python to native expressions on the
Chalk engineering blog.

Chalk calls the deployed function remotely when it cannot compile the body or
use the compiled expression for a call. Functions that perform I/O, such as
fetching data over HTTP, run remotely. Calls that request queued execution or
set invocation options such as a timeout also use remote execution. See
Calling from SQL for invocation
options and named arguments.





