# <img className="page-icon" src="/img/icons/trino.svg" alt="" /> Trino

Test data in Trino.

## 1. Install

```bash
uv tool install --python python3.11 --upgrade 'datacontract-cli[trino]'
```

See [Installation](../installation.md) for pip, pipx, and Docker.

## 2. Authenticate

Create a `.env` file in your working directory (or export the variables):

```bash
# .env
DATACONTRACT_TRINO_USERNAME=trino
DATACONTRACT_TRINO_PASSWORD=mysecretpassword
```

The default is `basic` auth, which connects over HTTPS whenever a password is set, so the server must serve TLS. JWT and OAuth2 are also supported — see the [Trino Reference](../reference/trino.md).

## 3. Create a contract from your tables

Import the table metadata directly from the catalog. This also generates a ready-to-test `servers` block:

```bash
datacontract import trino \
  --source localhost \
  --port 8443 \
  --catalog my_catalog \
  --schema my_schema \
  --table orders \
  --output datacontract.yaml
```

Repeat `--table` for multiple tables, or omit it to import every table in the schema.

Only have a DDL script? `datacontract import sql --source orders.sql --dialect postgres` works too — Trino's ANSI-style DDL generally parses with that dialect — but writes a `servers` block with placeholder values that you have to fill in by hand.

## 4. Test the actual data

```bash
datacontract test datacontract.yaml
```

```
Testing datacontract.yaml
Server: trino (type=trino, host=localhost, port=8443, schema=my_schema, catalog=my_catalog)
╭────────┬─────────────────────────────────────────────────────┬──────────┬─────────╮
│ Result │ Check                                               │ Field    │ Details │
├────────┼─────────────────────────────────────────────────────┼──────────┼─────────┤
│  ...   │                                                     │          │         │
│ passed │ Check that field 'order_id' is present              │ order_id │         │
│ passed │ Check that field order_id has physical type varchar │ order_id │         │
│  ...   │                                                     │          │         │
╰────────┴─────────────────────────────────────────────────────┴──────────┴─────────╯
🟢 Data contract is valid. Ran 24 checks. Took 1.9 seconds.
```

## 5. Let it catch a violation

The contract becomes valuable when it detects drift. Tighten an expectation — for example, add a quality rule to a schema in `datacontract.yaml`:

```yaml
schema:
  - name: orders
    # ...
    quality:
      - type: sql
        description: No order has a negative total
        query: SELECT COUNT(*) FROM orders WHERE order_total < 0
        mustBe: 0
```

Run `datacontract test datacontract.yaml` again: every violation is listed as an error, and the command exits with code `1` — ready for [CI/CD and scheduled runs](../scheduling/index.md) so you catch drift before your consumers do.

## Metadata checks

Instead of the data, you can check what Trino records about it. Point a server at the catalog (`schema: information_schema` of the server's `catalog`); its schemas then name the catalog views, and every check works on them as on a table:

```yaml
servers:
  - server: catalog
    type: trino
    host: localhost
    port: 8080
    catalog: memory
    schema: information_schema
schema:
  - name: columns
    quality:
      - type: sql
        description: my_schema.my_table has exactly three columns
        query: SELECT COUNT(*) FROM columns WHERE table_schema = 'my_schema' AND table_name = 'my_table'
        mustBe: 3
    properties:
      - name: column_name
        logicalType: string
        required: true
        logicalTypeOptions:
          pattern: "^[a-z_]+$"
```

## Reference

All authentication options (basic, JWT, OAuth2) and the data type handling: **[Trino Reference](../reference/trino.md)**.

## Troubleshooting

- **`401 Unauthorized`** — check the auth mode: password-protected clusters need `basic` credentials over HTTPS; token-based setups need `DATACONTRACT_TRINO_AUTHENTICATION=jwt` and `DATACONTRACT_TRINO_JWT_TOKEN`.
- **`Catalog ... does not exist` / `Schema ... does not exist`** — `catalog` and `schema` in the `servers` block must match `SHOW CATALOGS` / `SHOW SCHEMAS FROM <catalog>`.
