r/SQL • • 2d 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

2 Upvotes

3 comments sorted by

1

u/Artistic_Cow_9586 1d ago

sql as the glue for every cursed data source is exactly the kind of idea that starts as an edge case and turns into someone’s entire job, this looks pretty neat tho

0

u/actually_RevolutionP 2d ago

this looks super useful, the federated query stuff is what caught my eye. being able to join a parquet file from s3 against a postgres table without setting up a whole pipeline first is the kind of thing you don't realize you need until you're three hours deep into writing glue code

love that it's a single binary too, no dependency hell to wrestle with. will give it a spin on some of our staging data next week

the lack of windows support isn't a dealbreaker but it'd be nice eventually, half my team is stuck on it unfortunately

0

u/Discordbothost 2d ago

Put retry safety next to the exit-code guidance in the README. A transient failure tells a scheduler when to retry, but users also need to know whether a partially completed write can be rerun safely. Spell that out per write mode, including any cleanup required.