Federated Queries
One query can read tables of several connectors: join a Trino table with a local CSV file, your own L-Store tables, a Snowflake table, or Slack, without copying data by hand. Each connector runs the part of the query that reads its own tables, and the Wvlet engine joins the results.
Think of Trino and Snowflake as remote warehouses and the Wvlet engine as your local engine: a query whose data all sits in one warehouse is sent there as SQL, and a query that mixes sites reads only what it needs from each.
Federated queries work in the JVM wvlet CLI, the REPL, and wvlet server. The npm and native CLIs run a query on one engine only (see Limitations).
Set up a profile
List every connector you want to query in one profile of ~/.wvlet/profiles.json. Here td is a Trino warehouse, sf a Snowflake account, local an in-memory DuckDB, and the Wvlet engine is the default (see Working with Multiple Connectors for the connector options):
{
"profiles": [
{
"name": "dev",
"connectors": [
{ "name": "wvlet", "type": "wvlet", "default": true },
{
"name": "td",
"type": "trino",
"host": "api-presto.treasuredata.com",
"user": "${TD_API_KEY}",
"catalog": "td",
"schema": "sample_datasets"
},
{
"name": "sf",
"type": "snowflake",
"host": "myorg-myaccount",
"user": "${SNOWFLAKE_USER}",
"catalog": "ANALYTICS",
"schema": "PUBLIC",
"properties": { "private_key_file": "/path/to/rsa_key.p8", "warehouse": "COMPUTE_WH" }
},
{ "name": "local", "type": "duckdb" }
]
}
]
}
Start the REPL with wvlet repl -p dev, or run a file with wvlet run -f report.wv -p dev.
A first federated query
Name each table through its connector: <connector>.<table>, or <connector>.<schema>.<table> for another schema. This joins a Trino table with a local CSV file:
from td.www_access as a
join 'data/plans.csv' as p on a.user_id = p.user_id
where a.time > 1700000000
group by p.plan
agg _.count as pv
You don't need use first, and the query reads the same whichever connector is active. Since data/plans.csv is small, Wvlet sends its rows into the Trino query, so Trino runs the join and the aggregation and returns one row per plan.
Common tasks
Enrich warehouse data with a local file
Lookup tables, labels, and mappings often live in a spreadsheet export rather than in the warehouse. Read the file next to the warehouse table:
from td.www_access as a
join 'data/campaigns.csv' as c on a.path = c.landing_page
where a.time > 1700000000
group by c.campaign
agg _.count as visits
A file of up to 1,000 rows is sent into the warehouse query as VALUES, so the warehouse runs the join and returns only its result. A file of up to 10,000 rows stays on the Wvlet engine, which sends its join keys to the warehouse as where <key> in (…), so the warehouse returns only rows that can match.
Join your own tables with warehouse tables
Keep your own data in an L-Store database on the Wvlet engine, and join it with the warehouse. import works whichever connector is active:
import database 'data/crm' as crm
from td.www_access as a
join crm.main.customers as c on a.user_id = c.user_id
where a.path = '/cart'
group by c.segment
agg _.count as carts
After use td, the same query works, and crm.main.customers still refers to the L-Store table. See Wvlet Engine Tables to create the database and its tables.
Join two warehouses
Join a Trino table with a Snowflake table. Each warehouse filters and reads its own rows, and the Wvlet engine joins them:
from sf.orders as o
join td.www_access as a on o.user_id = a.user_id
where a.time > 1700000000 and o.amount > 100
group by a.path
agg o.amount.sum as revenue
Every row a warehouse returns travels to your machine, so filter both sides. A query that would read more than 1,000,000 rows from a Trino or Snowflake connector fails before it runs (see Keep remote reads small).
Add Slack data
With a Slack connector in the profile, its tables join like any other table; the Wvlet engine reads them:
from slack.messages as m
join slack.users as u on m.user = u.id
join td.employees as e on u.real_name = e.name
group by e.department
agg _.count as messages
Slack rows aren't sent into a warehouse's queries unless the Slack connector allows it (see The data boundary).
Run part of a query in a warehouse as written
By default Wvlet decides where each part of a query runs. To decide yourself, pin raw SQL or a model to a connector with in <connector>. Use raw SQL for warehouse features that Wvlet can't express, such as Trino's tablesample:
from sql"select user_id, count(*) as n from www_access tablesample bernoulli (1) group by 1" in td as s
join crm.main.customers as c on s.user_id = c.user_id
select c.segment, s.n
A pinned model runs its whole body on the connector, compiled for its SQL dialect, so connector-specific functions such as td_time_string work:
model daily_pv in td = {
from www_access
group by td_time_string(time, 'd!') as day
agg _.count as pv
}
from daily_pv as d
join 'data/holidays.csv' as h on d.day = h.day
wvlet build creates a pinned model's table on its connector. If the model reads tables from elsewhere, Wvlet sends them into the model's query; see Temporary copies for pinned models for large inputs. A stored table already runs on its own connector, so from sf.orders in td is an error. See Run a Model on a Connector.
Move data between connectors in a flow
In a flow, each stage can run on a different connector with stage <name> in <connector>. A stage reads the earlier stages' results wherever they ran:
flow daily_report = {
stage recent = from td.www_access | where time > 1700000000
stage summary in local = from recent | group by path | agg _.count as pv
}
Check a query before you run it
Prefix a query with explain to see where it runs before it reads anything. For a federated query, it prints the SQL each connector gets, with an estimate of its rows, and then the plan the Wvlet engine runs over the results:
explain
from td.www_access as a
join sf.orders as o on a.user_id = o.user_id
where o.amount > 100
group by a.path
agg o.amount.sum as revenue
-- federated: the wvlet engine joins the parts below
-- part 1 on td (~5000000 rows):
select user_id, path
from td.sample_datasets.www_access
-- part 2 on sf:
select user_id, amount
from ANALYTICS.PUBLIC.orders as o
where o.amount > 100
-- engine plan:
...
What to look for:
-- runs whole on <connector>: every table is on one connector, which runs the query.-- part N on <connector> (~rows): the SQL a connector runs and about how many rows it returns. A large estimate on a warehouse part means a missing filter. Snowflake parts show no estimate.-- ships <input> into <connector>: rows that will be written into that connector's query text (see The data boundary).
To estimate rows, explain asks Trino (SHOW STATS) and DuckDB (EXPLAIN) about their parts, but runs no part.
Keep remote reads small
Rows that travel from a warehouse to the Wvlet engine cost transfer time, and on Trino and Snowflake also warehouse resources. Wvlet keeps them few by sending each connector its share of the query: filters, the columns used, limit, and joins and aggregations over that connector's tables. You help it by:
- Filtering on the warehouse table's columns.
where a.time > 1700000000runs in Trino. Filters comparingfloatordoublevalues or calling functions run on the Wvlet engine after the rows arrive, so add a plain comparison next to them when you can (the reference lists what stays local). - Aggregating early.
group byover one warehouse's tables runs in that warehouse, and the sums, counts, minimums, and maximums below a cross-connector join are partly computed there too. - Keeping the small side small. A join input of up to 1,000 rows from your machine goes into the warehouse query, so the warehouse runs the join.
A statement reads at most 1,000,000 rows from each Trino or Snowflake connector. When a connector's estimate is over the limit, the query fails before it starts; without an estimate, it fails once that many rows have arrived:
The statement would read about 3200000 rows from connector 'td', more than its limit of 1000000.
Add a filter, or raise maxRemoteRows in the connector's profile entry
Add a filter, or raise the limit for that connector with "properties": { "maxRemoteRows": 5000000 } (0 turns it off). DuckDB and other connectors have no limit unless you set one.
The data boundary
To let a warehouse run a join, Wvlet writes small inputs into its query as VALUES, and pinned models may copy larger inputs into temporary tables. Those rows end up in the warehouse's query history, and copies in its storage.
- Engine tables, imported databases, local files, and
valuesare sent by default. - Rows of Slack or of another warehouse are sent only when their connector's
propertiesset"shipRows": true.
Check the -- ships <input> into <connector> lines of explain before running a query over sensitive local data.
Results and consistency
Results don't depend on where each part runs: the same query returns the same rows as one engine holding all the tables. Two things differ from a single engine:
- Each connector's part is a separate query, so parts don't share a snapshot: two parts reading the same connector may see different versions of a table that changes while the query runs.
limitwithoutorder bymay return other rows than a single-engine run.
Learn more
- Federated query reference: what each connector runs, the size limits for sending inputs, connector properties, turning steps off, and current limitations.
- Working with Multiple Connectors: profiles, connector names, and
use. - Trino and Snowflake: connector setup.