Use AI functions in dynamic tables¶
AI function calls may cost significantly more than most SQL operations. For more information, see
Cost considerations for Cortex AI Functions. When a dynamic table calls
AI functions, use REFRESH_MODE = CUSTOM_INCREMENTAL so that Snowflake
calls the function only for new rows and for rows whose AI input changed. Avoid calling AI functions in the
definition of a SELECT-based dynamic table.
Choose a pattern¶
Both patterns use a custom incremental dynamic table that calls the AI function and stores the result. Choose the pattern that matches how rows in the base table change:
| Rows in the base table | Pattern | Example |
|---|---|---|
| Are only added, never updated or deleted | INSERT INTO SELF with CHANGES(INFORMATION => APPEND_ONLY) | Example: insert-only |
| Can be inserted, updated, or deleted, and have a stable unique key | MERGE INTO SELF that compares each change with the stored row | Example: update and delete rows with a stable key |
Create the base table¶
The examples on this page use the following base table of support tickets:
Example: insert-only¶
If rows are only added to the base table, use INSERT INTO SELF with CHANGES(INFORMATION => APPEND_ONLY).
Snowflake calls the AI function once for each new row and never revisits existing rows.
The ai_fn column labels the version of the AI function call that produced each row, and ai_at records when
the call ran. Because APPEND_ONLY returns inserts only, updates and deletes in raw_tickets are ignored.
Example: update and delete rows with a stable key¶
If rows in the base table can be updated or deleted, use MERGE INTO SELF. The following definition calls
the AI function only when a ticket is new or when ticket_text changes. When only status changes, Snowflake
updates the stored row without calling the function.
Create a SELECT-based dynamic table downstream to extract typed columns from the raw result. Changing this definition later doesn’t call the AI function again.
Parts of the MERGE definition¶
The numbers in the following list match the comments in the definition of dt_tickets_ai.
- Source and consolidation.
CHANGES(INFORMATION => DEFAULT)returns each change as anINSERTorDELETErow. An update appears as aDELETEandINSERTpair, and so does a base table that’s rewritten with identical data. TheQUALIFYclause keeps one row for each key: theINSERTrow if one exists, otherwise theDELETErow. TheORDER BYclause lays out the table by key, which keeps later refreshes small. - Match.
ONmatches each change to the stored row with the same key. - Clauses. Snowflake evaluates the
WHENclauses in order and runs the first one that matches. The following table describes each clause:
| Clause | Runs when | Calls the AI function |
|---|---|---|
| 3a | The key was deleted from the base table. | No |
| 3b | The key is new. | Yes |
| 3c | The key exists and an AI input column differs from the stored row. | Yes |
| 3d | The key exists, the AI input is unchanged, and another column differs from the stored row. | No |
If the base table is rewritten with identical data, or an update leaves every column unchanged, no clause matches and the refresh doesn’t write any rows.
Pipeline structure¶
The examples on this page use two kinds of dynamic tables:
- A custom incremental dynamic table calls the AI function. It’s the only dynamic table in the pipeline that
calls AI functions. It stores the raw result in a
VARIANTcolumn, along with the other columns that downstream tables need. - A SELECT-based dynamic table reads the custom incremental dynamic table and extracts the typed columns from the raw result. Joins, filters, and aggregations also belong in SELECT-based dynamic tables.
Guidelines¶
- Use a key that’s unique across all rows that feed the table and that doesn’t change when a row is rewritten, such as an ID column. If two rows have the same key, the stored row is overwritten and the AI function is called again.
- Compare only the columns that you pass to the AI function in the AI-input clause (3c). Compare every other
column in the non-AI clause (3d). Set every column in the
INSERTclause and in the AI-inputUPDATEclause. - Use
EQUAL_NULLto compare values. A comparison with<>returns NULL when either value is NULL, so a change to or from NULL is missed. - Store the raw result as
VARIANTand extract typed columns in a downstream SELECT-based dynamic table. You can’t change the type of a column in a custom incremental dynamic table later. - Write the AI function call only in the
INSERTclause and the AI-inputUPDATEclause, with identical arguments in both. UseCOALESCEon prompt arguments that can be NULL. - Use
CREATE OR ALTERto change the definition.CREATE OR REPLACEdiscards the stored results, and the next refresh calls the AI function for every row. - To change the model or prompt, update the function call in both clauses with
CREATE OR ALTERand change theai_fnlabel. Existing rows keep their results. Only new rows and rows with a changed AI input use the new call. - Make sure change tracking is enabled on the base table, and that its data retention period is longer than the longest gap between refreshes. For more information, see Usage notes.
- To clean up stored AI results that you no longer need, use a storage lifecycle policy. For more information, see Use storage lifecycle policies with dynamic tables.
Verify AI function calls¶
Rows that the AI function processed in a refresh have a recent ai_at value. To see how many rows each
version of the call produced, and when the function last ran, query the custom incremental dynamic table:
To see how many rows each refresh inserted and deleted, query the DYNAMIC_TABLE_REFRESH_HISTORY function.
Limitations¶
Custom incremental dynamic tables have the limitations described in Limitations. For example, you can’t use dbt to manage them.
What’s next¶
- Custom incrementalization: Syntax and behavior of
REFRESH USING. - Decision guide for dynamic tables: Choose between dynamic tables, streams and tasks, and materialized views.
- Understanding costs for dynamic tables: Estimate and control the cost of dynamic tables.