Integrations · Data and spreadsheets
Validate email addresses in ClickHouse
Two SQL functions for ClickHouse: validate_email(email) returns the status of an address (valid, invalid, catch_all, unknown or disposable), and validate_email_details(email) returns the reason, the suggested action and, for a row that could not be checked, an error code instead of a status, as JSON text.
SELECT email, validate_email(email) AS status FROM customers;
SELECT email, JSONExtractString(validate_email_details(email), 'reason') AS reason FROM customers;validate_email_details is an executable user-defined function: ClickHouse runs a small Python script on the server, which checks each address with our API. It is for self-managed ClickHouse, where you can put files on the server. Download email-validation-clickhouse.zip: it unpacks into a folder email-validation-clickhouse holding the script (user_scripts/ev_validate.py, Python 3, standard library only), the function's config (config/), the SQL scripts (sql/) and LICENSE (MIT). It has not run in a real ClickHouse server yet: it was built from ClickHouse's documentation and tested against a stand-in.
What it costs
Every non-NULL row a query passes to these functions is checked each time the query runs, and a definite result costs one credit; the same address twice in one chunk is checked once. unknown results, malformed addresses and test addresses on sandbox.email-validation.org are free. A check of an address checked before also costs a credit (repeat checks).
A query over a million rows can spend a million credits, and running it again spends them again. A view that calls the functions checks again whenever it is read. So check each address once into a table and join to it (below), and give the key a daily credit cap.
Set up
Key. Create an API key on the API keys page with the permission Validate addresses and a daily credit cap. Save it in a file only the
clickhouseuser can read:sudo install -m 0400 -o clickhouse -g clickhouse /dev/null /etc/clickhouse-server/email-validation.key sudo nano /etc/clickhouse-server/email-validation.key # paste the key, saveScript and config. From the unzipped folder, put the script in ClickHouse's
user_scriptsfolder and the config next to the server's config:cd email-validation-clickhouse sudo install -m 0755 -o root user_scripts/ev_validate.py /var/lib/clickhouse/user_scripts/ sudo install -m 0644 -o root config/email_validation_function.xml /etc/clickhouse-server/Those are the default places: the server's
user_scripts_pathnames the first, and the config file's name must match itsuser_defined_executable_functions_configpattern (*_function.*ml). The server needspython3.Load it. Run
SYSTEM RELOAD FUNCTIONS, thensql/1-functions.sql: it createsvalidate_emailon top of the details and ends with a free test on test addresses.
For EU data residency, set EV_REGION = "eu" at the top of the script before you install it. Every user who can run queries on the server can call the functions and spend credits: the key's daily credit cap is the limit.
Our API allows 20 validation requests a second and 100 in flight until your account has made a purchase, and 500 a second after one. The config runs 4 copies of the script, each with 8 checks in flight at most: 32 in all.
Check each address once
sql/2-check-once.sql creates the table ev_email_results and, on each run, checks only the distinct addresses that have no result there yet, at most 10,000 per run (the LIMIT caps what one run spends), in chunks of 100 rows, and keeps the status, reason, action and error as columns. Replace customers and email with your table and column, and run it again when new addresses arrive. Read the results with a join that costs nothing:
SELECT c.*, r.status AS ev_status, r.reason AS ev_reason, r.action AS ev_action
FROM customers AS c
LEFT JOIN ev_email_results AS r ON r.email = c.email;Start with a small LIMIT and raise it once a run succeeds: a run that fails keeps nothing of what it checked.
Try it free
SELECT validate_email('no_mailbox@sandbox.email-validation.org'); -- invalid
SELECT validate_email('disposable@sandbox.email-validation.org'); -- disposableWhat the functions return
validate_email(email) returns one of the five statuses, or NULL for a NULL or empty address and for a row that could not be checked. validate_email_details(email) returns a JSON object as text, with the same keys on every row ({status, reason, action, did_you_mean, domain, free_email, role_account, disposable, catch_all_domain, mx_found, processed_at, credits_used, request_id, error, error_detail}): read a key with JSONExtractString. Statuses and reasons explains each value.
When a row is not checked
A row that could not be checked never gets an invented status: its status is NULL and error says why.
error | Meaning | Charged |
|---|---|---|
rate_limited, abuse_throttled | The account's request rate limit | No |
service_unavailable, upstream_capacity | A short outage on our side | No |
insufficient_credits, key_daily_cap | No credits left, or the key's daily credit cap reached | No |
invalid_request, internal_error | The API refused or failed this one address | No |
"no_answer" | Sent, but no answer arrived in time | Maybe |
unexpected_response | An answer that is neither a result nor our error | Maybe |
"not_sent" | Not sent: the chunk stopped after one of the codes above, or ran out of time | No |
After a rate limit, an outage, the daily cap or no credits, no further address of that chunk is sent. The check-once script checks rows without a status again on its next run, except "no_answer" and unexpected_response rows, which may have been charged (one commented line in the script includes them). If the key is wrong, lacks Validate addresses, or its file cannot be read, the query fails with a message that names the problem (and the error code and request id), never the key.
Privacy
The script sends each address only in the body of a POST to https://api.email-validation.org (or https://api-eu.email-validation.org), with the key in the Authorization header, never to another host, and follows no redirect or proxy setting. It logs nothing. What keeps addresses is under your control: ClickHouse's query log (any address written into the SQL) and ev_email_results.
Licence
The download is under the MIT licence (LICENSE in the zip): you may use, change and share it.