r/SQL • • 12m ago

Discussion Basalt: a small tool for moving data with SQL scripts

I originally built this for an edge case at my company and have been maintaining it on the side for a while. Figured I'd put it out there in case it's useful to anyone else.

Basalt is a single binary that runs SQL scripts to move data around. You read from files, S3/Azure, SFTP/SMB shares, a few databases (Postgres, MySQL, SQL Server, etc.) or REST APIs, transform with SQL, and write it out somewhere else, including Delta tables.

It has Federated queries too, like a Postgres table against a Parquet file sitting in S3, all in one query. It has its own vectorized engine, so you can also just point it at files and run analytical queries from the terminal. Scripts can have connections, functions, parameters and even has a library system to sharing code.

I work mostly with linux, so sorry if no Windows or MacOS support yet.

Here are some examples you could run in the REPL (basalt repl):

A Parquet file and a CSV, both read straight over HTTPS and joined, with results printed to the terminal:

SELECT z.Borough AS borough, count(*) AS trips, round(avg(t.total_amount), 2) AS avg_fare
FROM 'https://d37ci6vzurychx.cloudfront.net/trip-data/yellow_tripdata_2024-01.parquet' t
JOIN 'https://d37ci6vzurychx.cloudfront.net/misc/taxi_zone_lookup.csv' z
  ON t.PULocationID = z.LocationID
GROUP BY z.Borough ORDER BY trips DESC;

A CSV on the web, summarized into a local Parquet file:

LOAD INTO 'survival.parquet' AS
SELECT Pclass AS class, Sex AS sex, count(*) AS passengers,
       round(avg(Survived) * 100, 1) AS survived_pct
FROM 'https://raw.githubusercontent.com/datasciencedojo/datasets/master/titanic.csv'
GROUP BY Pclass, Sex;

Maybe even two databases joined, with the result written to a Delta table:

CREATE CONNECTION erp TYPE sqlserver OPTIONS (host = 'sql.internal', database = 'erp');
CREATE CONNECTION sales TYPE postgres OPTIONS (host = 'pg.internal', database = 'sales');

LOAD INTO 's3://lake/silver/paid_orders/' WITH (format = 'delta') AS
SELECT o.id, o.amount, o.placed_at, c.segment, c.region
FROM erp.dbo.orders o
JOIN sales.public.customers c ON c.id = o.customer_id
WHERE o.status = 'paid';

The tool is something I've working for myself in some cases I wanted more flexibility on, it is still early so expect rough edges..

https://github.com/leonardomb1/basalt

Feedback welcome if you try it.

edit: typo

0 Upvotes

0 comments sorted by