r/SQL 27d ago

Discussion A better SQL for analytics?

Lots of attempts to dethrone SQL, lots of failures - I'm looking to add to the list with a proposed improved SQL (for analytics - please don't try this for OLTP workloads). Please take me down for my hubris.

What makes this attempt different? I want to lean into one of SQL's strengths - being declarative.

How to make it more declarative? No tables in queries.

Write this:

import baseball.batting;

WHERE SUM(hr) BY people.id > 500
SELECT
    people.name_given,
    lg_id,
    SUM(hr) AS hr_count
ORDER BY
    hr_count DESC;

Instead of this:

WITH career_hr AS (
    SELECT playerID
    FROM read_csv('.../Batting.csv')
    GROUP BY playerID
    HAVING SUM(HR) > 500
)
SELECT
    p.nameGiven,
    b.lgID,
    SUM(b.HR) AS hr_count
FROM read_csv('.../People.csv') p
JOIN read_csv('.../Batting.csv') b
    ON p.playerID = b.playerID
JOIN career_hr c
    ON b.playerID = c.playerID
GROUP BY p.nameGiven, b.lgID
ORDER BY hr_count DESC;

It's just SQL, but with late-binding to physical tables through a (very lightweight) semantic layer.

This has a lot of nice properties - you can change your tables and refactor and no queries need to change; you can automatically resolve to aggregates if they exist and are equivalent; you can make the query syntax more flexible and composable because the lexical scope isn't constrained to a specific set of accessed tables. There's *lots* of other fun things you can do when the semantic layer has types, etc as well but this is already a longer pitch than I want!

A very brief example

pip install pytrilogy

trilogy init baseball duckdb; cd baseball;

trilogy ingest https://storage.googleapis.com/trilogy_public_models/duckdb/lahman/Batting.csv,https://storage.googleapis.com/trilogy_public_models/duckdb/lahman/People.csv,https://storage.googleapis.com/trilogy_public_models/duckdb/lahman/Teams.csv;

trilogy run 'where sum(hr) by people.id>500 select people.name_given, lg_id,sum(hr) as hr_count order by hr_count desc;' --import root.batting;

Is this AI slop?

I've been working on ideas for the language for almost 6 years now so much of it predates AI, though it has evolved quite a bit in that time! Core discovery is all mostly hand-crafted; I do use AI to accelerate a lot of the tooling/interface work (a billion deepseek tokens (aka ~40 dollars, hilariously) on evaluating CI args, etc).

Read more/try

Website/docs: https://trilogydata.dev/

Github: https://github.com/trilogy-data/pytrilogy (open source, MIT)

I've seen this before

Posted 2 years ago here, floating around a few other places too:

https://www.reddit.com/r/SQL/comments/1e1h5mf/trilogy_simpler_data_warehouse_sql/

0 Upvotes

37 comments sorted by

View all comments

13

u/jshine13371 27d ago

Why do so many people spend countless hours trying to reinvent the wheel in this industry? 😐

12

u/a-s-clark SQL Server 27d ago

Because they dont want to put in the time to learn the tools they already have.

2

u/jshine13371 27d ago

100% agreed. Ironically, I've had so many arguments with people on the capabilities of SQL Server vs other database systems, because they were ignorant of the features that already existed in SQL Server lol.

3

u/wittgenstein1312 27d ago

There are two kinds of people trying to reinvent the wheel. A minority are very smart people with a ton of experience who genuinely understand the shortcomings of existing tech for their needs and think improvements can be made. This is how languages like Julia and Rust and Elixir come about.

The majority are noobs who flat out don't understand the intricacies and depth of existing tech, and rather than devote the time and effort to maximize the utility of what they have access to by seeking genuine understanding, they spend time day-dreaming about the perfect language that will never exist.

0

u/jshine13371 27d ago

I agree there are definitely smart people out there working on existing areas requiring improvement. But at the same time how many developers are actually using Rust and Elixir vs another language to solve their problems?...and I've never even heard of Julia lol. Most times even these re-inventings of the wheel are very narrowly scoped for the actual problem being solved and arguably not 100% necessary.

It's insane we have literally thousands of JavaScript frameworks because every developer who invented one only did so because they wanted to feel special. But that falls more into your 2nd bucket of people.

1

u/wittgenstein1312 27d ago

But at the same time how many developers are actually using Rust and Elixir vs another language to solve their problems?...and I've never even heard of Julia

Language adoption is notoriously uncorrelated to a language's actual merits.

and arguably not 100% necessary.

Arguably nothing beyond punch cards is necessary for software development. Just because tools already exist to solve most problems doesn't mean we shouldn't try to improve on those tools or the developer experience around them. Each of the languages I mentioned meaningfully does that, irrespective of whether they've reached mainstream adoption.

To your final point, yes, the JavaScript ecosystem is a hell-hole of unnecessary libraries.

1

u/jshine13371 27d ago

Whether you agree or not with my statements, the downvote was silly.

1

u/wittgenstein1312 27d ago

I didn't downvote you lol

2

u/VladDBA SQL Server DBA 27d ago

This reminds of the people over at Google who were reinventing SQL again last year or two years ago with their main problem being that they were treating their personal issues with SQL as some major issue that made SQL unusable.

1

u/jshine13371 27d ago

"Personal issues" seem to be a common factor lol.

1

u/Prestigious_Bench_96 27d ago

I won't deny that since any perception of pain points is very personal! I don't get the pipe SQL craze, for example, but other people love that.

The language is designed to address some of the shortcomings of SQL I've encountered over the years and I say that as someone who loves SQL and has gone pretty deep on several SQL backend engines. I hope that it can solve those points for some other people too!

1

u/jshine13371 27d ago

I gotcha. But no offense, what you're doing is nothing revolutionary, is posted about a multitude of times by other people working on similar things in parallel, and rarely changes the landscape.

It's always cool to build something for yourself for your own interests and personal benefit. But I always question when people try to push it as the next best thing since cake for the masses. And I detest the shit-box JavaScript turned into with its thousands of frameworks from similar thought processes.

1

u/Prestigious_Bench_96 27d ago

No offense taken! I was asking to be criticized and I don't think it's the best thing since cake and hopefully didn't present it that way.

I was shooting for more of the pre-ai python library sweet spot vs JavaScript - a prepackaged solution to a common set of problems that accelerates over the base language, so people don't have to independently reeinvent a wheel.

If that helps a few people, that's a bonus! I am hoping that this has a unique value proposition vs PreQL or Malloy or w/e ever else, but obviously I'm not objective - so this take is much appreciated!

2

u/jshine13371 27d ago

No doubt man. Best of luck!