Common calling rules
Passing arguments
Functions support positional arguments and named arguments in the formargument_name => value. Named arguments can appear in any order and allow you to specify optional arguments without supplying defaults for earlier arguments.
chat_provider refers to an existing chat AI Provider.
Provider and model
All functions have optionalprovider and model arguments. Both must be constant VARCHAR expressions.
ai_embed requires an embedding Provider. All other functions require a chat Provider.
Provider configuration includes an endpoint and optional credentials, and supports HTTP and HTTPS. Each execution plan captures a snapshot of the selected Provider’s configuration. Changing a Provider or switching the default affects newly planned queries. For management syntax, see CREATE AI PROVIDER and SET DEFAULT AI PROVIDER.
JSON options
Onlyai_complete and ai_embed support the optional options argument. It must be a constant JSON expression, and any value other than SQL NULL must be a JSON object. When omitted, it defaults to an empty object. Both json_object() and SQL NULL mean that no additional options are passed.
ai_complete
Generates text from a prompt using the chat completion capability of a chat Provider.Syntax
Arguments
prompt: A VARCHAR expression describing the task for the model. You can use a text column.options: An optional constant JSON object. See JSON options.
provider and model, see Common calling rules.
Return value
Returns VARCHAR containing the text inchoices[0].message.content from the response body.
Examples
ai_embed
Converts text into an embedding using an embedding Provider. URL strings are treated as plain text; the content at the URL is not fetched.Syntax
Arguments
input: A VARCHAR expression containing the text to embed.options: An optional constant JSON object.dimensionsdefaults to the Provider configuration and can be overridden through options. The model and service must support the requested dimensions.
provider and model, see Common calling rules.
Return value
Returns a nullableARRAY<FLOAT>. The service response must contain one nonempty embedding at index 0, with finite FLOAT values. Invalid responses follow the error handling rules.
Examples
ai_sentiment
Analyzes the overall sentiment of text.Syntax
Arguments
input: A VARCHAR expression containing the text to analyze.
provider and model, see Common calling rules.
Return value
Returns a nullable VARCHAR. Valid results are normalized to one ofpositive, negative, neutral, mixed, or unknown. Other responses are treated as row-level errors.
Examples
ai_classify
Asks the model to classify text into one of the given categories.Syntax
Arguments
input: A VARCHAR expression containing the text to classify.categories: A nonempty constantARRAY<VARCHAR>containing the candidate categories.
provider and model, see Common calling rules.
Return value
Returns a nullable JSON object. The requested structure is{"labels":["selected category"]}, for example, {"labels":["support"]}.
The database checks that the result is a JSON object, but does not validate the number of labels, category membership, or classification accuracy.
Examples
ai_extract
Extracts information from text using the given field names.Syntax
Arguments
input: A VARCHAR expression containing the text to extract information from.labels: A nonempty constantARRAY<VARCHAR>containing the field names to extract.
provider and model, see Common calling rules.
Return value
Returns a nullable JSON object. The requested structure is{"response":{"field name":"value"}}, with JSON null requested for fields that are not found. Example result: {"response":{"name":"Alice","city":"Paris"}}.
The database checks that the result is a JSON object, but does not validate the field structure of response or the accuracy of the extracted content.
Examples
ai_fix_grammar
Corrects grammar and spelling, asking the model to preserve the original meaning and tone.Syntax
Arguments
input: A VARCHAR expression containing the text to correct.
provider and model, see Common calling rules.
Return value
Returns a nullable VARCHAR containing the corrected text.Examples
ai_redact
Asks the model to identify sensitive information in the specified categories and replace it with category markers.Syntax
Arguments
input: A VARCHAR expression containing the original text to process.categories: A nonempty constantARRAY<VARCHAR>containing the categories to redact, such asname,email, andphone.
provider and model, see Common calling rules.
Return value
Returns a nullable VARCHAR. The model is instructed to replace identified information with the uppercase category name in square brackets, such as[EMAIL], and return the original text if no matching information is found.
The original text is sent to the model service. This function does not redact data locally before transmission, and the model may miss sensitive information.
Examples
ai_translate
Translates text from the source language into the target language.Syntax
Arguments
input: A VARCHAR expression containing the text to translate.source_language: A VARCHAR expression specifying the source language. Use an empty string''to request automatic detection.target_language: A VARCHAR expression specifying the target language.
en, zh, and fr, or language names the model understands. The database does not define a fixed list of supported languages.
For the types and default behavior of provider and model, see Common calling rules.
Return value
Returns a nullable VARCHAR containing the translated text. Returns NULL if the input text is NULL at runtime. A runtime NULL source language is treated as automatic detection. A runtime NULL, empty, or ASCII-whitespace-only target language returns NULL without sending a request. All three semantic arguments are required and cannot be bare NULL literals. To detect the source language automatically, explicitly pass an empty string.Examples
ai_similarity
Asks a chat model to evaluate the semantic similarity of two texts.Syntax
Arguments
input: A VARCHAR expression containing the first text.other: A VARCHAR expression containing the second text.
provider and model, see Common calling rules.
Return value
Returns a nullable FLOAT in the range[0, 1]. A higher value indicates greater semantic similarity according to the model. Responses that cannot be parsed or are outside this range are treated as row-level errors.
The score is generated by a chat model; it is not a distance calculation between embedding vectors.
Examples
ai_summarize
Asks the model to generate a short summary that preserves the main information.Syntax
Arguments
input: A VARCHAR expression containing the text to summarize.
provider and model, see Common calling rules.
Return value
Returns a nullable VARCHAR containing the summary generated by the model.Examples
ai_filter
Determines whether text meets a condition expressed in natural language.Syntax
Arguments
input: A VARCHAR expression containing the text to evaluate.condition: A VARCHAR expression describing the condition in natural language.
provider and model, see Common calling rules.
Return value
Returns a nullable BOOLEAN. The model response must normalize totrue or false. Other responses are treated as row-level errors.
Examples
Error handling
ai_function_on_error controls row-level failures. The default, ignore, returns NULL for a failed row and continues the query; fail aborts the query. Analysis or configuration errors, query cancellation, and deadline expiration are not ignored. A NULL result therefore does not necessarily mean the input was NULL; it can also indicate a failed model call or response validation.
Runtime limits and retries
- Text generation and helper functions use
ai_function_rate_limit_qps_chat, while embedding functions useai_function_rate_limit_qps_embedding. Their respective QPS limits andai_function_max_inflightapply independently on each BE. QPS limits are maintained in buckets keyed by endpoint, credential, and capability, while the in-flight limit is process-wide. Fair admission based on WorkGroup and Query shares the applicable limits among queued requests. Every initial or retry HTTP attempt must acquire both kinds of admission permits. - The database cannot guarantee that a model provider executes a request only once. An attempt that times out or otherwise fails may already have reached the provider, so retries can repeat work at the provider and incur additional charges. Configure
ai_function_max_retriesandai_function_max_retries_on_throttleaccording to the provider’s behavior and billing policy. - Request and response payloads are accounted for in the Query memory tracker. The execution Pipeline applies backpressure while requests are outstanding.
ai_function_max_response_bytesis a hard limit for each HTTP response body. - The independent task timeout remains fixed after task creation and does not restart on retries. Query cancellation and deadline updates remain effective throughout asynchronous execution. For timeout, worker thread, and scheduling granularity controls, see the other BE AI function configuration items.
SQL usage restrictions
- AI functions cannot be used in
GROUP BY,SELECT DISTINCT, aggregate function arguments, or window function expressions. - They cannot be used in
IF,IFNULL,NULLIF,COALESCE, orCASEconditional expressions, or as table function arguments. - They cannot be used in synchronous materialized view definitions, LOAD expressions, or generated column expressions.
- They cannot be used in Lambda expression bodies or SQL UDF bodies.
- SQL Plan Baselines cannot be created or bound for statements containing AI functions. Other queries containing AI functions do not use SPM rewrites either.
PREPAREis supported, but statements containing AI functions are fully replanned on everyEXECUTE; execution plans are not reused.- In a correlated subquery block, AI expressions in the
SELECTlist,WHERE,HAVING,ORDER BY, andJOIN ONare rejected even if the AI expression references only columns in that block. AI expressions in join conditions are supported only forINNER JOINandCROSS JOIN. - Each non-NULL input row may produce a remote request. Before using AI functions on many rows, consider network latency, provider quotas, query deadlines, costs, and data egress policies.