Claude Code for Trino: Distributed SQL Query Engine — Claude Skills 360 Blog
Blog / AI / Claude Code for Trino: Distributed SQL Query Engine
AI

Claude Code for Trino: Distributed SQL Query Engine

Published: August 18, 2027
Read time: 5 min read
By: Claude Skills October

Trino is the distributed SQL query engine for data lakes — federated queries across any data source. Python: import trino.dbapi as trino_db, conn = trino_db.connect(host="trino-host", port=443, user="user", auth=trino.auth.JWTAuthentication(TOKEN), catalog="iceberg", schema="analytics"). cursor = conn.cursor(), cursor.execute("SELECT * FROM orders WHERE dt >= current_date - INTERVAL '7' DAY"), cursor.fetchall() returns rows. Catalogs define connectors: iceberg catalog queries Iceberg tables on S3, hive queries Hive metastore tables, postgresql queries Postgres directly. Federated query: SELECT o.order_id, u.email FROM iceberg.analytics.orders o JOIN postgresql.public.users u ON o.user_id = u.id — joins across lakes and OLTP in one SQL. JDBC: jdbc:trino://host:443/iceberg/analytics?SSL=true&user=user&password=TOKEN. REST API: POST /v1/statement with SQL body returns { nextUri, data, columns, stats } — poll nextUri until status != "RUNNING". trino_credentials.header for Basic auth. Cost-based optimizer (CBO): ANALYZE table collects statistics for better query plans. Partition pruning: filter on partition columns (dt, region) to skip data files. EXPLAIN (FORMAT JSON) shows query plan including join strategy and pushdowns. session_properties: hash_partition_count=64, join_distribution_type=BROADCAST for small dimension tables. USE catalog.schema sets default. SHOW CATALOGS, SHOW SCHEMAS FROM iceberg, SHOW TABLES FROM iceberg.analytics. Claude Code generates Trino Python clients, federated SQL queries, REST API polling clients, and catalog configurations.

CLAUDE.md for Trino

## Trino Stack
- Python: trino (PyPI) >= 0.328 — trino.dbapi.connect(host, port=443, auth=JWTAuthentication(TOKEN))
- Auth: JWTAuthentication(TOKEN) or BasicAuthentication(user, password) or KerberosAuthentication
- Catalog: specify in connect() or USE catalog.schema in SQL
- Federated: SELECT from iceberg.db.table JOIN postgresql.public.table — one SQL, multiple sources
- REST: POST /v1/statement → poll nextUri until state == "FINISHED" or "FAILED"
- EXPLAIN: EXPLAIN (FORMAT JSON) SELECT ... — analyze query plan for optimization

Trino Python Client

# lib/trino/client.py — Trino query client
import os
import time
from typing import Any, Optional
import trino
import trino.dbapi
import trino.auth
import pandas as pd


def get_connection(
    catalog: str = "iceberg",
    schema:  str = "analytics",
) -> trino.dbapi.Connection:
    """Create a Trino connection using environment config."""
    host  = os.environ["TRINO_HOST"]
    port  = int(os.environ.get("TRINO_PORT", "443"))
    user  = os.environ.get("TRINO_USER", "query-service")
    token = os.environ.get("TRINO_TOKEN")

    auth = trino.auth.JWTAuthentication(token) if token else trino.auth.BasicAuthentication(
        username=user,
        password=os.environ.get("TRINO_PASSWORD", ""),
    )

    return trino.dbapi.connect(
        host=host,
        port=port,
        user=user,
        auth=auth,
        catalog=catalog,
        schema=schema,
        http_scheme="https" if port == 443 else "http",
        session_properties={
            "query_max_execution_time": "10m",
            "join_distribution_type":  "AUTOMATIC",
        },
    )


def query_to_df(
    sql:     str,
    catalog: str = "iceberg",
    schema:  str = "analytics",
    params:  Optional[list] = None,
) -> pd.DataFrame:
    """Execute SQL and return a DataFrame."""
    conn   = get_connection(catalog=catalog, schema=schema)
    cur    = conn.cursor()
    cur.execute(sql, params)
    rows   = cur.fetchall()
    cols   = [desc[0] for desc in cur.description] if cur.description else []
    return pd.DataFrame(rows, columns=cols)


def query_rows(
    sql:     str,
    catalog: str = "iceberg",
    schema:  str = "analytics",
) -> list[dict[str, Any]]:
    """Execute SQL and return list of dicts."""
    df = query_to_df(sql, catalog=catalog, schema=schema)
    return df.to_dict("records")


def explain_query(sql: str) -> dict:
    """Get Trino query plan as JSON for optimization analysis."""
    conn = get_connection()
    cur  = conn.cursor()
    cur.execute(f"EXPLAIN (FORMAT JSON) {sql}")
    row  = cur.fetchone()
    import json
    return json.loads(row[0]) if row else {}

Federated Query Patterns

# lib/trino/queries.py — common federated query patterns
from .client import query_rows, query_to_df
from datetime import date, timedelta


def join_lake_and_oltp(start_date: date, end_date: date):
    """Join Iceberg data lake orders with Postgres OLTP users."""
    sql = f"""
        SELECT
            o.order_id,
            o.amount,
            o.status,
            o.created_at,
            u.email,
            u.plan,
            u.country
        FROM iceberg.analytics.orders o
        JOIN postgresql.public.users u
            ON o.user_id = u.id
        WHERE
            o.dt >= DATE '{start_date.isoformat()}'  -- partition pruning
            AND o.dt <= DATE '{end_date.isoformat()}'
            AND o.status = 'completed'
        ORDER BY o.created_at DESC
        LIMIT 10000
    """
    return query_rows(sql, catalog="iceberg", schema="analytics")


def revenue_by_country_and_plan(lookback_days: int = 30):
    """Cross-catalog aggregation with GROUP BY rollup."""
    sql = f"""
        WITH order_data AS (
            SELECT
                o.user_id,
                o.amount,
                o.created_at
            FROM iceberg.analytics.orders o
            WHERE
                o.dt >= DATE_ADD('day', -{lookback_days}, current_date)
                AND o.status = 'completed'
        )
        SELECT
            u.country,
            u.plan,
            count(*)            AS order_count,
            sum(od.amount)      AS total_revenue,
            avg(od.amount)      AS avg_order_value,
            count(DISTINCT od.user_id) AS unique_buyers
        FROM order_data od
        JOIN postgresql.public.users u
            ON od.user_id = u.id
        GROUP BY GROUPING SETS (
            (u.country, u.plan),
            (u.country),
            (u.plan),
            ()
        )
        ORDER BY total_revenue DESC NULLS LAST
    """
    return query_to_df(sql, catalog="iceberg")


def incremental_stats_refresh(as_of_date: date):
    """Read from Kafka connector for near-realtime event stats."""
    sql = f"""
        SELECT
            json_extract_scalar("_message", '$.event_type') AS event_type,
            count(*) AS event_count,
            date_trunc('hour', from_unixtime(
                CAST(json_extract_scalar("_message", '$.timestamp') AS BIGINT) / 1000
            )) AS event_hour
        FROM kafka.default.app_events
        WHERE _timestamp >= TIMESTAMP '{as_of_date} 00:00:00'
        GROUP BY 3, 1
        ORDER BY 3 DESC
        LIMIT 500
    """
    return query_rows(sql, catalog="kafka", schema="default")

Trino REST API Client (TypeScript)

// lib/trino/rest-client.ts — Trino REST API polling client
const TRINO_HOST  = process.env.TRINO_HOST!
const TRINO_TOKEN = process.env.TRINO_TOKEN!
const TRINO_USER  = process.env.TRINO_USER ?? "query-service"

type TrinoQueryState = "QUEUED" | "PLANNING" | "STARTING" | "RUNNING" | "FINISHED" | "FAILED" | "CANCELED"

type TrinoResponse = {
  id:        string
  nextUri?:  string
  data?:     unknown[][]
  columns?:  Array<{ name: string; type: string }>
  stats:     { state: TrinoQueryState; totalRows?: number }
  error?:    { message: string; errorCode: number }
}

const headers = {
  "Authorization":          `Bearer ${TRINO_TOKEN}`,
  "X-Trino-User":           TRINO_USER,
  "X-Trino-Catalog":        "iceberg",
  "X-Trino-Schema":         "analytics",
  "X-Trino-Time-Zone":      "UTC",
  "Content-Type":           "application/json",
}

export async function runTrinoQuery(sql: string): Promise<Array<Record<string, unknown>>> {
  // Submit query
  const submitRes = await fetch(`https://${TRINO_HOST}/v1/statement`, {
    method:  "POST",
    headers,
    body:    sql,
  })

  if (!submitRes.ok) throw new Error(`Trino submit error ${submitRes.status}: ${await submitRes.text()}`)

  let response: TrinoResponse = await submitRes.json()
  const allRows: unknown[][]  = []
  let columns: Array<{ name: string }> | undefined

  // Poll until finished
  while (response.nextUri || response.stats.state === "RUNNING") {
    if (response.data)    allRows.push(...response.data)
    if (response.columns) columns = response.columns

    if (response.error)   throw new Error(`Trino query error: ${response.error.message}`)
    if (!response.nextUri) break

    await new Promise((r) => setTimeout(r, 500))

    const pollRes = await fetch(response.nextUri, { headers })
    if (!pollRes.ok) throw new Error(`Trino poll error: ${pollRes.status}`)
    response = await pollRes.json()
  }

  if (response.data)    allRows.push(...response.data)
  if (response.columns) columns = response.columns
  if (response.error)   throw new Error(`Trino failed: ${response.error.message}`)

  // Map rows to objects
  if (!columns) return []
  return allRows.map((row) =>
    Object.fromEntries(columns!.map((col, i) => [col.name, row[i]]))
  )
}

For the Presto alternative when operating in a Meta/Facebook environment or using PrestoDB — Trino was forked from Presto Original (PrestoSQL), is more actively maintained with faster quarterly releases, and is the de facto community version while PrestoDB is Meta’s internal fork. Athena runs Trino under the hood for serverless federated queries on S3 without cluster management. For the Apache Spark alternative when needing batch processing with large shuffles, MLlib integration for machine learning, Structured Streaming for micro-batch streaming, and a cluster managed by Databricks or EMR — Spark is the standard for complex multi-step data pipelines while Trino is optimized for interactive ad-hoc SQL analytics across heterogeneous data sources with sub-second to sub-minute latency. The Claude Skills 360 bundle includes Trino skill sets covering Python client, federated queries, REST API polling, and catalog configuration. Start with the free tier to try distributed SQL generation.

Keep Reading

AI

Claude Code for email.contentmanager: Python Email Content Accessors

Read and write EmailMessage body content with Python's email.contentmanager module and Claude Code — email contentmanager ContentManager for the class that maps content types to get and set handler functions allowing EmailMessage to support get_content and set_content with type-specific behaviour, email contentmanager raw_data_manager for the ContentManager instance that handles raw bytes and str payloads without any conversion, email contentmanager content_manager for the standard ContentManager instance used by email.policy.default that intelligently handles text plain text html multipart and binary content types, email contentmanager get_content_text for the handler that returns the decoded text payload of a text-star message part as a str, email contentmanager get_content_binary for the handler that returns the raw decoded bytes payload of a non-text message part, email contentmanager get_data_manager for the get-handler lookup used by EmailMessage get_content to find the right reader function for the content type, email contentmanager set_content text for the handler that creates and sets a text part correctly choosing charset and transfer encoding, email contentmanager set_content bytes for the handler that creates and sets a binary part with base64 encoding and optional filename Content-Disposition, email contentmanager EmailMessage get_content for the method that reads the message body using the registered content manager handlers, email contentmanager EmailMessage set_content for the method that sets the message body and MIME headers in one call, email contentmanager EmailMessage make_alternative make_mixed make_related for the methods that convert a simple message into a multipart container, email contentmanager EmailMessage add_attachment for the method that attaches a file or bytes to a multipart message, and email contentmanager integration with email.message and email.policy and email.mime and io for building high-level email readers attachment extractors text body accessors HTML readers and policy-aware MIME construction pipelines.

5 min read Feb 12, 2029
AI

Claude Code for email.charset: Python Email Charset Encoding

Control header and body encoding for international email with Python's email.charset module and Claude Code — email charset Charset for the class that wraps a character set name with the encoding rules for header encoding and body encoding describing how to encode text for that charset in email messages, email charset Charset header_encoding for the attribute specifying whether headers using this charset should use QP quoted-printable encoding BASE64 encoding or no encoding, email charset Charset body_encoding for the attribute specifying the Content-Transfer-Encoding to use for message bodies in this charset such as QP or BASE64, email charset Charset output_codec for the attribute giving the Python codec name used to encode the string to bytes for the wire format, email charset Charset input_codec for the attribute giving the Python codec name used to decode incoming bytes to str, email charset Charset get_output_charset for returning the output charset name, email charset Charset header_encode for encoding a header string using the charset's header_encoding method, email charset Charset body_encode for encoding body content using the charset's body_encoding, email charset Charset convert for converting a string from the input_codec to the output_codec, email charset add_charset for registering a new charset with custom encoding rules in the global charset registry, email charset add_alias for adding an alias name that maps to an existing registered charset, email charset add_codec for registering a codec name mapping for use by the charset machinery, and email charset integration with email.message and email.mime and email.policy and email.encoders for building international email senders non-ASCII header encoders Content-Transfer-Encoding selectors charset-aware message constructors and MIME encoding pipelines.

5 min read Feb 11, 2029
AI

Claude Code for email.utils: Python Email Address and Header Utilities

Parse and format RFC 2822 email addresses and dates with Python's email.utils module and Claude Code — email utils parseaddr for splitting a display-name plus angle-bracket address string into a realname and email address tuple, email utils formataddr for combining a realname and address string into a properly quoted RFC 2822 address with angle brackets, email utils getaddresses for parsing a list of raw address header strings each potentially containing multiple comma-separated addresses into a list of realname address tuples, email utils parsedate for parsing an RFC 2822 date string into a nine-tuple compatible with time.mktime, email utils parsedate_tz for parsing an RFC 2822 date string into a ten-tuple that includes the UTC offset timezone in seconds, email utils parsedate_to_datetime for parsing an RFC 2822 date string into an aware datetime object with timezone, email utils formatdate for formatting a POSIX timestamp or the current time as an RFC 2822 date string with optional usegmt and localtime flags, email utils format_datetime for formatting a datetime object as an RFC 2822 date string, email utils make_msgid for generating a globally unique Message-ID string with optional idstring and domain components, email utils decode_rfc2231 for decoding an RFC 2231 encoded parameter value into a tuple of charset language and value, email utils encode_rfc2231 for encoding a string as an RFC 2231 encoded parameter value, email utils collapse_rfc2231_value for collapsing a decoded RFC 2231 tuple to a Unicode string, and email utils integration with email.message and email.headerregistry and datetime and time for building address parsers date formatters message-id generators header extractors and RFC-compliant email construction utilities.

5 min read Feb 10, 2029

Put these ideas into practice

Claude Skills 360 gives you production-ready skills for everything in this article — and 2,350+ more. Start free or go all-in.

Back to Blog

Get 360 skills free