Skip to main content
This page describes all AI functions for text processing. Before calling them, configure an AI Provider and obtain usage privileges on both the function and the selected Provider. See the AI functions overview.

Common calling rules

Passing arguments

Functions support positional arguments and named arguments in the form argument_name => value. Named arguments can appear in any order and allow you to specify optional arguments without supplying defaults for earlier arguments.
In these examples, chat_provider refers to an existing chat AI Provider.

Provider and model

All functions have optional provider 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

Only ai_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.
Use options to pass additional request parameters supported by the model service.

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.
For the types and default behavior of provider and model, see Common calling rules.

Return value

Returns VARCHAR containing the text in choices[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. dimensions defaults to the Provider configuration and can be overridden through options. The model and service must support the requested dimensions.
For the types and default behavior of provider and model, see Common calling rules.

Return value

Returns a nullable ARRAY<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.
For the types and default behavior of provider and model, see Common calling rules.

Return value

Returns a nullable VARCHAR. Valid results are normalized to one of positive, 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 constant ARRAY<VARCHAR> containing the candidate categories.
For the types and default behavior of 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 constant ARRAY<VARCHAR> containing the field names to extract.
For the types and default behavior of 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.
For the types and default behavior of 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 constant ARRAY<VARCHAR> containing the categories to redact, such as name, email, and phone.
For the types and default behavior of 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.
You can use identifiers such as 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.
For the types and default behavior of 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.
For the types and default behavior of 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.
For the types and default behavior of provider and model, see Common calling rules.

Return value

Returns a nullable BOOLEAN. The model response must normalize to true 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 use ai_function_rate_limit_qps_embedding. Their respective QPS limits and ai_function_max_inflight apply 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_retries and ai_function_max_retries_on_throttle according 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_bytes is 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.
For request, retry, and timeout metrics, see AI HTTP metrics.

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, or CASE conditional 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.
  • PREPARE is supported, but statements containing AI functions are fully replanned on every EXECUTE; execution plans are not reused.
  • In a correlated subquery block, AI expressions in the SELECT list, WHERE, HAVING, ORDER BY, and JOIN ON are rejected even if the AI expression references only columns in that block. AI expressions in join conditions are supported only for INNER JOIN and CROSS 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.