# table.query.run

Runs SQL across CSV, JSON, NDJSON and Parquet files in a project store as if they were tables

# table.query.run

```
table.query.run --from TEXT… [--query TEXT] [--param KEY=EXPR…] [--limit N] [--format csv|json|ndjson|parquet] [--rows] [--folder TEXT] [--filename TEXT] [--path TEXT] [--store TEXT] [--on-conflict error|skip|overwrite] → query
```

Runs SQL across CSV, JSON, NDJSON and Parquet files in a project store as if they were tables. It has the GeoDataFusion `ST_*` functions. Each `--from "<source> as <name>"` binds one table: a store key, or a FileRef or rows (`"{{ input.data }} as t"`, or `$expr`). The SQL is the body after `--` (or `--query`), SELECT or WITH only; `--param 1=…` binds `$1` (a whole `{{ expr }}` keeps its type). Without a destination it adds `query: { rows, columns, row_count, truncated }`, as the db nodes do — `--limit` caps the rows (default 200, at most 5000). With `--folder` / `--filename` / `--path` the whole result (at most 10000 rows) is written as `--format` and `query` holds the file's FileRef fields with `row_count` (rows written), `columns` and `format`; `--rows` adds the first `--limit` rows and `truncated`. `--limit` caps the rows answered, not the work: every table query shares one memory pool, a quarter of this office's memory, and at most 2 run at once. A query past the pool fails with `FW_NODE_TABLE_QUERY_RUN_MEMORY` (exact COUNT(DISTINCT) and GROUP BY over columns with many distinct values are the usual cause: aggregate fewer columns per query, or use approx_distinct), and one that waits over 10 s for its turn fails with `FW_NODE_TABLE_QUERY_RUN_BUSY`. For database tables use [`postgres.query.run`](/nodes/postgres/postgres.query.run.md), [`sqlite.query.run`](/nodes/sqlite/sqlite.query.run.md) or [`sekejap.query.run`](/nodes/sekejap/sekejap.query.run.md).

## Flags

- `--from` (required): One table, repeated: "<source> as <name>" — a store key, or {{ expr }} (or $expr) giving a FileRef or rows.
- `--query`: The SQL (SELECT or WITH) statement (or the body after `--`). A literal or {{ expr }}.
- `--param`: One bind value, repeated: `1=…` binds $1, `2=…` binds $2. A literal is text; a whole {{ expr }} keeps its JSON type, so a number stays a number.
- `--limit`: Rows answered (default 200, at most 5000); `truncated` says more matched.
- `--format`: What the written file is: csv, json, ndjson or parquet (default: from the destination's extension, else csv).
- `--rows`: With a destination, the answer carries the rows as JSON as well as the file (without one it always does).
- `--folder`: Store folder for the written file (default: tables).
- `--filename`: Name of the written file (default: a UUID with the format's extension).
- `--path`: Exact store key for the written file; overrides --folder and --filename.
- `--store`: The project store to write to: `local`, or the id of an `s3` credential. Saved explicitly when the pipeline is registered (default: the project's default store at that moment)
- `--on-conflict`: When the target already exists: overwrite, skip or error (default: error)

## Examples

### Aggregate a CSV

```
| table.query.run --from "uploads/sales.csv as sales" -- "SELECT region, SUM(amount) AS total FROM sales GROUP BY region ORDER BY total DESC"
```

It answers:

```json
{
  "query": {
    "rows": [
      {
        "region": "AU",
        "total": 1200
      },
      {
        "region": "NZ",
        "total": 300
      }
    ],
    "columns": [
      "region",
      "total"
    ],
    "row_count": 2,
    "truncated": false
  }
}
```

### Join two files into Parquet

```
| table.query.run --from "datasets/orders.ndjson as o" --from "datasets/customers.csv as c" --path datasets/report.parquet -- "SELECT c.name, COUNT(*) AS orders FROM o JOIN c ON o.customer_id = c.id GROUP BY c.name"
```

The whole result is the file; `input.query` is its FileRef. Add `--rows` to answer the first --limit rows as well.

It answers:

```json
{
  "query": {
    "__zf_type": "file_ref",
    "backend": "zebfs",
    "store": "local",
    "ref": "datasets/report.parquet",
    "filename": "report.parquet",
    "mime": "application/vnd.apache.parquet",
    "kind": "parquet",
    "size": 2048,
    "sha256": "sha256:…",
    "lifecycle": "durable",
    "origin": "table.query.run",
    "trust": "generated",
    "row_count": 42,
    "columns": [
      "name",
      "orders"
    ],
    "format": "parquet"
  }
}
```

### Filter by a bound value

```
| table.query.run --from "datasets/posts.parquet as posts" --param "1={{ $trigger.params.slug }}" -- "SELECT id, title FROM posts WHERE slug = $1"
```
