1BRC in SQL with DuckDB #39
Replies: 5 comments 10 replies
-
Great way to expand the knowledge of everyone! Since the problem is well-defined, and we can see different approaches with different stacks, it is a nice way to get a few new techniques all around! |
Beta Was this translation helpful? Give feedback.
-
Nice post, @rmoff Doesn't change speed (I tested) but there's actually a way to use WITH src AS (SELECT station_name,
MIN(measurement) AS min_measurement,
CAST(AVG(measurement) AS DECIMAL(8,1)) AS mean_measurement,
MAX(measurement) AS max_measurement
FROM READ_CSV('measurements.txt', header=false, columns= {'station_name':'VARCHAR','measurement':'double'}, delim=';')
GROUP BY station_name)
SELECT '{' ||
ARRAY_TO_STRING(LIST(station_name || '=' || CONCAT_WS('/', min_measurement, mean_measurement, max_measurement) ORDER BY station_name), ', ') ||
'}' AS "1BRC"
FROM src; See nested I wouldn't wonder if the compiler uses the same operator anyhow. |
Beta Was this translation helpful? Give feedback.
-
beat me to it by a couple of hours! i was working on a similar approach, but hadn't gotten to the optimization steps you did. Thanks for putting this out. Try to convince Gunnar to put other implementations on the leaderboard! I've seen several Python + Polars, Go, and Rust implementations that are interesting. |
Beta Was this translation helpful? Give feedback.
-
wow! DuckDB is a game changer and (up to this moment) didn't know it integrates well with Python. Bye bye sqlite3 ;-)
Thank you @rmoff Below is what I have in Python. I have added the dict at the end just to print it in the format as specified by the challenge. Also having
|
Beta Was this translation helpful? Give feedback.
-
Hey @rmoff, this is great!! I am also trying with duckdb. Just curious, what are your M1 specs? Is it 8GB? Cause when I ran my command, it took ~ 3mins to load and output the results. Most of the time was spent in loading the file which is not surprising. My run: duckdb command:
duckdb config is all default for ex: |
Beta Was this translation helpful? Give feedback.
-
Not an official 1BRC entry because it's not Java, but I thought it'd be fun to try it out in SQL with DuckDB - output formatting and all!
👉 Read more: 1️⃣🐝🏎️🦆 (1BRC in SQL with DuckDB)
Beta Was this translation helpful? Give feedback.
All reactions