Docs Home
Viewing docs for
Self-ManagedNot available for BYOC

AI SQL Functions

On this page

VERA Engine 4.5 introduces AI SQL functions that let you call large language models (LLMs) directly from SQL. These functions use the LATERAL TABLE syntax and require a model created with CREATE TEMPORARY MODEL.

Supported Version

VERA Engine 4.5 or later.

Prerequisites

Syntax Pattern

All AI SQL functions follow this pattern:

SQL
1SELECT col1, result_col
2FROM source_table,
3LATERAL TABLE(AI_FUNCTION_NAME(
4  MODEL => model_ref,
5  INPUT => input_col
6  [, additional_parameters]
7)) AS T(result_col [, ...]);

AI_CLASSIFY

Classifies input text into one of the specified labels.

Syntax:

SQL
1LATERAL TABLE(AI_CLASSIFY(
2  MODEL => model_ref,
3  INPUT => text_col,
4  LABELS => ARRAY['label1', 'label2', ...]
5)) AS T(category, confidence)

Parameters:

ParameterTypeDescription
MODELModel referenceThe model to use for classification.
INPUTSTRINGThe text to classify.
LABELSARRAY<STRING>The set of labels to classify into.

Output columns:

ColumnTypeDescription
categorySTRINGThe predicted label.
confidenceDOUBLEConfidence score between 0 and 1.

Example:

SQL
1SELECT content, category, confidence
2FROM articles,
3LATERAL TABLE(AI_CLASSIFY(
4  MODEL => my_model,
5  INPUT => content,
6  LABELS => ARRAY['sports', 'politics', 'technology']
7)) AS T(category, confidence);

AI_SENTIMENT

Analyzes the sentiment of input text.

Syntax:

SQL
1LATERAL TABLE(AI_SENTIMENT(
2  MODEL => model_ref,
3  INPUT => text_col
4)) AS T(score, label, confidence)

Parameters:

ParameterTypeDescription
MODELModel referenceThe model to use for sentiment analysis.
INPUTSTRINGThe text to analyze.

Output columns:

ColumnTypeDescription
scoreDOUBLESentiment score.
labelSTRINGSentiment label (for example, positive, negative, neutral).
confidenceDOUBLEConfidence score between 0 and 1.

Example:

SQL
1SELECT review_text, label, confidence
2FROM reviews,
3LATERAL TABLE(AI_SENTIMENT(
4  MODEL => my_model,
5  INPUT => review_text
6)) AS T(score, label, confidence);

AI_EXTRACT

Extracts structured fields from input text based on a JSON schema.

Syntax:

SQL
1LATERAL TABLE(AI_EXTRACT(
2  MODEL => model_ref,
3  INPUT => text_col,
4  EXTRACT_SCHEMA => '{"field": "type", ...}'
5)) AS T(extracted_json)

Parameters:

ParameterTypeDescription
MODELModel referenceThe model to use for extraction.
INPUTSTRINGThe text to extract from.
EXTRACT_SCHEMASTRINGA JSON object defining field names and expected types.

Output columns:

ColumnTypeDescription
extracted_jsonSTRINGA JSON string containing the extracted fields.

Example:

SQL
1SELECT body, extracted_json
2FROM emails,
3LATERAL TABLE(AI_EXTRACT(
4  MODEL => my_model,
5  INPUT => body,
6  EXTRACT_SCHEMA => '{"sender_name": "string", "order_id": "string"}'
7)) AS T(extracted_json);

AI_SUMMARIZE

Summarizes input text.

Syntax:

SQL
1LATERAL TABLE(AI_SUMMARIZE(
2  MODEL => model_ref,
3  INPUT => text_col
4  [, MAX_LENGTH => max_tokens]
5)) AS T(summary)

Parameters:

ParameterTypeRequiredDescription
MODELModel referenceYesThe model to use for summarization.
INPUTSTRINGYesThe text to summarize.
MAX_LENGTHINTNoMaximum length of the summary in tokens.

Output columns:

ColumnTypeDescription
summarySTRINGThe generated summary.

Example:

SQL
1SELECT article_id, summary
2FROM articles,
3LATERAL TABLE(AI_SUMMARIZE(
4  MODEL => my_model,
5  INPUT => CONCAT('Summarize in English: ', content),
6  MAX_LENGTH => 100
7)) AS T(summary);

AI_EMBED

Generates a vector embedding for input text.

Syntax:

SQL
1LATERAL TABLE(AI_EMBED(
2  MODEL => model_ref,
3  INPUT => text_col
4)) AS T(embedding)

Parameters:

ParameterTypeDescription
MODELModel referenceThe embedding model to use.
INPUTSTRINGThe text to embed.

Output columns:

ColumnTypeDescription
embeddingARRAY<FLOAT>The vector embedding of the input text.

Example:

SQL
1SELECT doc_id, embedding
2FROM documents,
3LATERAL TABLE(AI_EMBED(
4  MODEL => my_embed_model,
5  INPUT => content
6)) AS T(embedding);

AI_TRANSLATE

Translates input text from a source language to a target language.

Syntax:

SQL
1LATERAL TABLE(AI_TRANSLATE(
2  MODEL => model_ref,
3  INPUT => text_col,
4  SOURCE_LANG => source_language,
5  TARGET_LANG => target_language
6)) AS T(translated_text, detected_language)

Parameters:

ParameterTypeDescription
MODELModel referenceThe model to use for translation.
INPUTSTRINGThe text to translate.
SOURCE_LANGSTRINGSource language code (for example, 'en'). Use 'auto' to detect the language automatically.
TARGET_LANGSTRINGTarget language code (for example, 'fr').

Output columns:

ColumnTypeDescription
translated_textSTRINGThe translated text.
detected_languageSTRINGThe detected source language. Populated when SOURCE_LANG is 'auto'.

Example:

SQL
1SELECT message_id, translated_text, detected_language
2FROM support_messages,
3LATERAL TABLE(AI_TRANSLATE(
4  MODEL => my_model,
5  INPUT => message_body,
6  SOURCE_LANG => 'auto',
7  TARGET_LANG => 'en'
8)) AS T(translated_text, detected_language);

AI_MASK

Masks personally identifiable information (PII) or other specified entities in input text.

Syntax:

SQL
1LATERAL TABLE(AI_MASK(
2  MODEL => model_ref,
3  INPUT => text_col,
4  MASK_ENTITIES => ARRAY['entity1', 'entity2', ...]
5)) AS T(masked_text, detected_entities)

Parameters:

ParameterTypeDescription
MODELModel referenceThe model to use for masking.
INPUTSTRINGThe text to mask.
MASK_ENTITIESARRAY<STRING>List of entity types to mask (for example, 'name', 'email', 'phone').

Output columns:

ColumnTypeDescription
masked_textSTRINGThe input text with specified entities replaced by placeholders.
detected_entitiesSTRINGA JSON string listing the detected entities and their positions.

Example:

SQL
1SELECT record_id, masked_text
2FROM customer_records,
3LATERAL TABLE(AI_MASK(
4  MODEL => my_model,
5  INPUT => record_text,
6  MASK_ENTITIES => ARRAY['name', 'email', 'phone']
7)) AS T(masked_text, detected_entities);

Limitations

  • Requires VERA Engine 4.5 or later.
  • All AI SQL functions use LATERAL TABLE syntax and cannot be used as scalar expressions.
  • AI_EMBED requires an embedding model; all other functions require a completions model.
  • Latency and throughput depend on the model provider's API response time.
Was this helpful?