SQL functions: AI, bot detection and masking

aiClassify and embedText, isLikelyBot, getTrafficType, getBotName and friends, and how restricted properties are masked in SQL.

Embed
Preview of the card
<a href="https://lucidace.com/help/sql-ai-bot-masking"><img src="https://lucidace.com/help/_api/public/orgs/af34862a-c837-4478-8335-0505efe40459/spaces/2f4b888b-c4e6-488b-81bd-6e838fb12d2e/pages/eaf97551-31a0-4175-8c4e-6ddb8925c43d/social-card.svg?v=3e7ae33eec8b" alt="SQL functions: AI, bot detection and masking" width="600" height="315"></a>

These are on top of the ClickHouse functions allowed in SQL over the API.

AI functions

The model never sees SQL, and ClickHouse never calls a model. Lucida finds the AI calls in your query, asks your project's AI provider for the answers first, then substitutes them into the SQL. AI must be configured for the project, or the call fails: 403 when AI is switched off, 429 over the budget or spend cap, 422 for bad input (see AI providers).

Function

What it returns

aiClassify(text, ['a', 'b', ...])

For each row, the one of your choices that fits text, or empty when none fits

embedText('literal')

A float array (vector) for one literal text; compare it with cosineDistance(v, embedText('...')) or L2Distance

SELECT aiClassify(properties.message, ['bug', 'billing', 'praise']) AS topic, count() AS n
FROM events
WHERE event = 'support_message_sent' AND timestamp >= now() - INTERVAL 7 DAY
GROUP BY topic

Limits, enforced when the query is compiled:

  • aiClassify takes 1 to 50 string literals as choices, must sit inside a SELECT with a FROM, and classifies at most 500 distinct inputs per call (taken from the same FROM and WHERE). Each input is cut to 8 KB before it goes to the model.

  • embedText takes a string literal only, not a column.

  • At most 3 AI calls in one query.

  • Calls use your provider and are subject to the project's AI settings and spend cap.

Bot and traffic functions

Each takes an optional properties column (default properties) and classifies the stored user agent against the built-in bot catalog at query time, so a catalog update also applies to old events. Events with no catalog hit use what ingestion stored (IP ranges, heuristics, your custom rules).

Function

Returns

isLikelyBot()

1 when the traffic is not regular, or the event carries $lc_bot

getTrafficType()

regular, ai_agent, bot or automation

getTrafficCategory()

The catalog category, for example ai_assistant or headless_browser

getBotName()

The bot's name

getBotOperator()

The operator behind it

Categories map to types: ai_assistant is ai_agent; http_client, headless_browser and no_user_agent are automation; empty or regular is regular; any other category is bot.

SELECT getTrafficType() AS type, getBotName() AS bot, count() AS n
FROM events
WHERE event = '$pageview' AND timestamp >= now() - INTERVAL 30 DAY
GROUP BY type, bot
ORDER BY n DESC

Masking

Masking is not a function you call. When a key's access rules hide a property (for example an email property) or a warehouse table, the events and persons tables (including person_properties and the hot property columns) under your SQL replace that property's value with [masked], and warehouse_rows leaves the hidden tables out. Keys are matched case-insensitively and the key itself stays visible. Because masking happens below your query, no projection, filter or aggregate can read the real value. Access rules are described in Access control.

Updated