IT

Data Quality with ClickHouse and Airflow: Magnum e-Commerce Case Study

A green DAG doesn't guarantee data correctness. Magnum's CoreDataTeam built a custom DQ platform: 4 core SQL checks, Airflow integration, and targeted alerts — all on their existing stack.

·3 min read
Data Quality with ClickHouse and Airflow: Magnum e-Commerce Case Study

Green DAG ≠ Correct Data Every data engineer knows this morning: the load completed without errors, fresh rows sit in ClickHouse, then business messages arrive: "No sales for one warehouse." An hour later: "Purchase prices disappeared." Both times the ETL process ran successfully. Magnum e-Commerce's CoreDataTeam faced this constantly: "When did data update? Did all records arrive? Can we trust the report?" They started wanting a simple notification system and ended up building a full Data Quality platform: custom UI and SQL-configurable checks.

Four Quality Dimensions to Validate Data Quality means controlling whether data fits its consumers. Not a subjective "good/bad" glance, but a set of conditions verifiable by query. For a daily sales mart, four checks are essential: - **Freshness** — is data present for the expected day? (max(dt) = yesterday()) - **Uniqueness** — no duplicates on the business key? (date + warehouse + order_id) - **Not-null** — mandatory fields populated? (amount, customer) - **Completeness** — all expected entities present? (all 3 warehouses delivered data) Plus **business correctness (biz_sql)** — domain rules: receipt existence, price validity, reference matching. Critical distinction: technical load monitoring ("did load finish?") and content validation ("is data as expected?") are separate. Successful ETL can write partial rows; a failed load may leave correct yesterday's data intact.

Practice: Four SQL Tests on ClickHouse The authors use a simplified `orders` mart (dt, warehouse_id, order_id, amount) with three injected issues: duplicate order, NULL amount, missing warehouse 3 for yesterday. Max date looks fine. 1. **Freshness:** `SELECT max(dt) = yesterday() AS ok FROM orders` → returns true, but warehouse 3 absent. Lesson: one fresh row ≠ completeness. For tables with future periods, check window presence, not max(dt) equality. Timezone drift near midnight matters. 2. **Uniqueness:** `SELECT count() - count(DISTINCT (dt, warehouse_id, order_id)) AS extra_rows FROM orders` → 1 extra row. MergeTree's ORDER BY doesn't enforce uniqueness. If granularity is order line items, add item_id to the key. 3. **Not-null:** `SELECT count() FROM orders WHERE amount IS NULL` → 1 violation. For strings, also check `trim(toString(col)) = ''`. Zero amount isn't automatically an error (free items, compensation). 4. **Completeness:** `SELECT arrayJoin([1,2,3]) AS wh WHERE wh NOT IN (SELECT DISTINCT warehouse_id FROM orders WHERE dt = yesterday())` → returns 3. In production, replace hardcoded list with active warehouse reference table including operating periods.

Airflow Integration & Custom DQ Platform As marts grow — each with own keys, expected dates, mandatory fields, exceptions, alert recipients — script sprawl becomes unmanageable. Magnum had ClickHouse and Airflow, plus capacity for a custom engine, so they skipped external DQ products (Great Expectations, Monte Carlo, Soda) and built their own pragmatic solution. The platform covers three linked tasks: 1. **Load monitoring** — technical DAG status (Airflow). 2. **Content validation** — SQL rules in ClickHouse, executed via Python. 3. **Targeted delivery** — alerts to Telegram, email, web UI. Failure causes and consumers differ, so they don't mix in one status. Rules, load registry, results, alert configs live in ClickHouse. Management via web app. This fits their infrastructure — not a claim of universal superiority, but a working reference pattern for Russian companies on ClickHouse + Airflow.

Takeaway for Data Teams Core lesson: green freshness doesn't rule out other issues. Four basic queries are easy; scaling needs systematic approach: centralized rule storage, separation of technical vs business monitoring layers, flexible alert routing. Magnum demonstrated how to do this on existing stack without buying external platforms. A ready architectural pattern for teams on ClickHouse + Airflow.

Source: Все публикации подряд на Хабре

Related articles