Filters rows by evaluating text against a natural-language condition using a Large Language Model (LLM). Returns true if the condition is met, false otherwise.
Syntax
AI_FILTER(<text>)
AI_FILTER(<resource_name>, <text>)Use the first form when no specific model resource is configured. Use the second form to target a named resource.
Parameters
| Parameter | Required | Description |
|---|---|---|
<text> | Yes | The text to evaluate. Typically a string that combines a natural-language condition with column data. |
<resource_name> | No | The name of the model resource to use. |
Return value
Returns a
Booleanvalue:trueif the condition is met,falseotherwise.Returns
NULLif the input isNULL.Results are non-deterministic. Because the LLM generates each result independently, the same input may return different values across runs.
Usage notes
The quality of filtering depends on how clearly the condition is expressed. Follow these guidelines to get more accurate results:
Be specific. Instead of
'positive comment', use'This is a positive customer review of a delivery service'.Use question form. Phrasing the condition as a question often improves accuracy, for example:
'Does this comment express satisfaction with the delivery service?'Avoid NULL inputs. Filter out NULL values before passing them to
AI_FILTERto prevent unexpected NULL returns.
Examples
Quick test
Run this without any table setup to verify the function works:
SELECT AI_FILTER('resource_name', 'Is this a positive statement: Absolutely fantastic service!');Filter rows in a table
The following example filters customer comments from a courier company.
Create the table:
CREATE TABLE user_comments (
id INT,
comment VARCHAR(500)
) DUPLICATE KEY(id)
DISTRIBUTED BY HASH(id) BUCKETS 10
PROPERTIES (
"replication_num" = "1"
);Query positive comments:
SELECT id, comment FROM user_comments
WHERE AI_FILTER('resource_name', CONCAT('This is a positive comment: ', comment));Output:
+------+--------------------------------------------+
| id | comment |
+------+--------------------------------------------+
| 1 | Absolutely fantastic, highly recommend it. |
| 3 | This product is amazing and I love it. |
+------+--------------------------------------------+