๐Ÿ“Š Data Engineering

Trino

Distributed SQL Query Engine

A distributed SQL query engine. Federated queries across various data sources with standard SQL. Optimized for OLAP analytics.

Detailed Explanation

Trino (formerly PrestoSQL) is an open-source distributed SQL query engine that was developed at Facebook and later became independent. It can query petabyte-scale data at interactive speed (in seconds), and its core strength is Federated Query, which joins multiple data sources with a single SQL statement without moving data.

Trino's architecture consists of a Coordinator (query parsing, planning, scheduling) and Workers (actual data processing). It executes queries in a distributed manner using MPP (Massively Parallel Processing), and it is fast because pipelined processing means intermediate results are not written to disk.

Through its Connector architecture, it can connect to dozens of data sources, including Hive, Delta Lake, Iceberg, PostgreSQL, MySQL, MongoDB, and Elasticsearch. It is especially optimized for OLAP queries on S3- and HDFS-based data lakes, and it minimizes I/O through table format optimization (Parquet, ORC) and pushdown (Predicate Pushdown).

AWS Athena is a serverless service based on Trino, and Starburst is a commercial version that adds enterprise features. It is widely used as the analytics layer in a Data Lakehouse architecture, and it is also used for ELT transformation workloads in conjunction with dbt.

Code Example

-- Trino ๋ถ„์‚ฐ SQL JOIN ์ฟผ๋ฆฌ ์˜ˆ์ œ

-- 1. ์—ฌ๋Ÿฌ ๋ฐ์ดํ„ฐ ์†Œ์Šค ์—ฐํ•ฉ ์ฟผ๋ฆฌ (Federated Query)
-- S3 ๋ฐ์ดํ„ฐ ๋ ˆ์ดํฌ + PostgreSQL + Elasticsearch ์กฐ์ธ
SELECT
    o.order_id,
    o.order_date,
    c.customer_name,
    c.email,
    p.product_name,
    p.category,
    o.quantity,
    o.total_amount,
    r.review_score,
    r.review_text
FROM hive.sales.orders o                      -- S3/Hive ๋ฐ์ดํ„ฐ ๋ ˆ์ดํฌ
JOIN postgres.crm.customers c                 -- PostgreSQL CRM
    ON o.customer_id = c.customer_id
JOIN iceberg.catalog.products p               -- Iceberg ํ…Œ์ด๋ธ”
    ON o.product_id = p.product_id
LEFT JOIN elasticsearch.reviews.product_reviews r  -- Elasticsearch
    ON o.product_id = r.product_id
WHERE o.order_date >= DATE '2024-01-01'
  AND o.region = 'APAC'
ORDER BY o.total_amount DESC
LIMIT 1000;

-- 2. ์œˆ๋„์šฐ ํ•จ์ˆ˜๋ฅผ ํ™œ์šฉํ•œ ๋ถ„์„ ์ฟผ๋ฆฌ
WITH daily_metrics AS (
    SELECT
        DATE_TRUNC('day', event_time) AS event_date,
        user_id,
        COUNT(*) AS event_count,
        SUM(revenue) AS daily_revenue
    FROM delta.analytics.user_events
    WHERE event_time >= CURRENT_DATE - INTERVAL '90' DAY
    GROUP BY 1, 2
)
SELECT
    event_date,
    user_id,
    daily_revenue,
    SUM(daily_revenue) OVER (
        PARTITION BY user_id
        ORDER BY event_date
        ROWS BETWEEN 6 PRECEDING AND CURRENT ROW
    ) AS rolling_7d_revenue,
    RANK() OVER (
        PARTITION BY event_date
        ORDER BY daily_revenue DESC
    ) AS daily_rank
FROM daily_metrics
ORDER BY event_date DESC, daily_rank;

-- 3. ๋Œ€์šฉ๋Ÿ‰ ์ง‘๊ณ„ ์ตœ์ ํ™” (Approximate Functions)
SELECT
    region,
    APPROX_DISTINCT(user_id) AS unique_users,
    APPROX_PERCENTILE(latency_ms, 0.95) AS p95_latency,
    APPROX_PERCENTILE(latency_ms, 0.99) AS p99_latency
FROM hive.logs.api_requests
WHERE request_date = CURRENT_DATE
GROUP BY region;

How It's Said in Practice

Senior: "I need to join the log data in S3 with the customer information in PostgreSQL for analysis, and if I run a federated query in Trino, I can do it right away without moving any data."

Junior: "How is it different from Spark SQL?"

Senior: "Trino is optimized for interactive queries, so results come back in seconds. Spark is better suited to complex ETL batch jobs."

Interviewer: "What is Trino's Federated Query, and what are its advantages?"

Candidate: "It is a feature that joins different data sources with a single SQL statement. Because you do not have to copy the data to one place, you can avoid storage costs and synchronization problems."

Take a mock interview on this topic โ†’ WIA SOOM AI Interview ยท Start for free

Senior: "COUNT(DISTINCT user_id) is too slow on a table with 500 million rows. If you use APPROX_DISTINCT, it is much faster with HyperLogLog, within a 2% error."

Junior: "If an exact value isn't strictly necessary, an approximate function would be better."

Cautions

Learn More