Live data from Hacker News

Relational is more than SQL

fauna.com

171–177 of 177 posts

Re: Relational is more than SQL

#171
post #149

Earlier quoted context omitted.

You might find Malloy interesting as it makes a greater departure from SQL syntax. Queries are first class objects which can be nested within each other in order to do trellising. It still compiles to SQL because that is the only language accepted by DBMSs today; however it will automatically write symmetric aggregate calculations and do those nestings that are hard for a human to write. https://www.malloydata.dev/

How are views not first class query objects?

Yup.

"SQL doesn't have first-class query objects and isn't composable."

Uh… views, set-returning functions, temporary tables, CTEs…

"No, not like that! Something not declarative and set-oriented!"

SQL is declarative and set-oriented. 3rd generation language models for data were tried and discarded for good reason decades ago.

"But… but… I don't like set-oriented models! They make me feel dumb. I'm not dumb, so SQL must be dumb. Not meeeeeeeee!!!"

Re: Relational is more than SQL

#172
post #72
post #70

Earlier quoted context omitted.

It's just syntax, it compiles to SQL and runs on today's DBMS. It has no difference in speed or functionality.

(I couldn't help but notice you didn't comment on the difference in formatting in the examples.) Do you have examples of PRQL working with jsonpath? Generating JSON? Unnesting arrays? Returning ids from an INSERT or UPDATE without making a separate read query? Not trying to be argumentative. Honest question.

Thanks, that's a great question. You're right in that so far we haven't highlighted working with JSON and I hadn't actually tried until this point. IMHO the true power of PRQL comes from the fact that it allows you to define functions and with that you get the power of composability which is the true power of almost every programming language (and which is for the most part completely lacking in SQL).

So with that said, I tried the following POC (remember that PRQL is just a SQL generator so the JSON capabilities depend on your underlying RDBMS):

    ```sh
    > prqlc compile  s"""{obj} -> {path}"""
    let getstr = path obj -> s"""{obj} ->> {path}"""
    let extract = obj path -> s"""json_extract({obj}, {path})"""
    
    from [{data='{"duck": [1, 2, 3]}'}]
    select { data | get '$.duck[0]', data | getstr '$.duck[1]', extract data '$.duck[2]' }
    EOF
    WITH table_0 AS (
      SELECT
        '{"duck": [1, 2, 3]}' AS data
    )
    SELECT
      data -> '$.duck[0]',
      data ->> '$.duck[1]',
      json_extract(data, '$.duck[2]')
    FROM
      table_0
    
    -- Generated by PRQL compiler version:0.9.4 (https://prql-lang.org)
    ```
What's going on here is that I used [s-strings](https://prql-lang.org/book/reference/syntax/s-strings.html) to define custom PRQL functions `get`, `getstr` and `extract` which translate into the underlying `->`, `->>` and `json_extract` SQL constructs.

You could then for example pipe that query to DuckDB (the example is taken from the following DuckDB blogpost [Shredding Deeply Nested JSON, One Vector at a Time](https://duckdb.org/2023/03/03/json.html)):

    ```sh
    > prqlc compile  s"""{obj} -> {path}"""
    let getstr = path obj -> s"""{obj} ->> {path}"""
    let extract = obj path -> s"""json_extract({obj}, {path})"""
    
    from [{data='{"duck": [1, 2, 3]}'}]
    select { data | get '$.duck[0]', data | getstr '$.duck[1]', extract data '$.duck[2]'}
    EOF
    ┌───────────────────────┬──────────────────────────┬───────────────────────────────────┐
    │ "data" -> '$.duck[0]' │ ("data" ->> '$.duck[1]') │ json_extract("data", '$.duck[2]') │
    │         json          │         varchar          │               json                │
    ├───────────────────────┼──────────────────────────┼───────────────────────────────────┤
    │ 1                     │ 2                        │ 3                                 │
    └───────────────────────┴──────────────────────────┴───────────────────────────────────┘
    ```
HTH

Re: Relational is more than SQL

#173
post #146
post #72

Earlier quoted context omitted.

(I couldn't help but notice you didn't comment on the difference in formatting in the examples.) Do you have examples of PRQL working with jsonpath? Generating JSON? Unnesting arrays? Returning ids from an INSERT or UPDATE without making a separate read query? Not trying to be argumentative. Honest question.

I am not affiliated in any way with the PRQL project. Those are great questions though, I hope we get an answer.

@remram I replied to the parent comment.

Re: Relational is more than SQL

#174
post #169

Earlier quoted context omitted.

Temp tables are another bandaid. Like views, they are another second-class abstraction intended to address the lack of generality of relations. As you hint, adding second-class features is intended to handle pain points while keeping optimization simple, but it's the wrong way to do it IMO. No fixed number of second class features can make up for the lack of relations as first-class values, so this is not just a matt…

How would LINQ make jsonpath queries or unroll arrays on the database with unnest?

LINQ in EntityFramework (EF) supports querying JSON columns as objects [1].

unnest support is potentially on the horizon for EF core for PG [2], but already supported for Couchbase [3].

[1] https://learn.microsoft.com/en-ca/ef/core/what-is-new/ef-cor...

[2] https://github.com/npgsql/efcore.pg/issues/1525

[3] https://github.com/couchbaselabs/Linq2Couchbase/blob/master/...

Re: Relational is more than SQL

#175
post #170
post #161

Earlier quoted context omitted.

> Just learn SQL... I know SQL, and I imagine the authors of PRQL know it better than I do. Doesn't it seem weird that dozens of application languages have become popular since the 1970s, but we're still using dialects of the same old database query language? If it had a really elegant syntax, perhaps it wouldn't, but SQL's syntax is anything but. Some of the semantics can be awkward as well. I, for one welcome attem…

> Doesn't it seem weird that dozens of application languages have become popular since the 1970s, but we're still using dialects of the same old database query language? Indeed. Do you honestly believe that a half-century of data storage professionals and vendors are blindly moving forward with a hobbled tool? Or maybe there are aspects of SQL as a set-oriented 4th generation programming language that aren't apparent…

Within popular application languages, for any given paradigm, there are almost always several languages that aren't merely dialects of each other. C# isn't a dialect of Java. Ruby isn't a dialect of Python. Rust isn't a dialect of C++.

PRQL demonstrates that a set-oriented declarative language need not be a dialect of SQL and isn't the first language to do so (QUEL appeared in the 1970s). It seems odd to me these alternatives haven't gained much popularity.

Re: Relational is more than SQL

#176
post #172
post #72

Earlier quoted context omitted.

(I couldn't help but notice you didn't comment on the difference in formatting in the examples.) Do you have examples of PRQL working with jsonpath? Generating JSON? Unnesting arrays? Returning ids from an INSERT or UPDATE without making a separate read query? Not trying to be argumentative. Honest question.

Thanks, that's a great question. You're right in that so far we haven't highlighted working with JSON and I hadn't actually tried until this point. IMHO the true power of PRQL comes from the fact that it allows you to define functions and with that you get the power of composability which is the true power of almost every programming language (and which is for the most part completely lacking in SQL). So with that sa…

Excellent, thank you for the response.

Re: Relational is more than SQL

#177

Earlier quoted context omitted.

[PRQL dev here] I agree with the sentiments, even if not the conclusion. SQL is omnipresent and is "fine" in a lot of cases. TypeScript is indeed a great example of the case; Kotlin too. I'd also add that databases are already adding PRQL support — ClickHouse has native support, there's a DuckDB extension, and folks are working on a Postgres extension. One thing I'll respectfully disagree with — "SQL is highly static…

> One thing I'll respectfully disagree with — "SQL is highly statically analyzable by nature" Are you suggesting that PRQL is capable of this? Or at least easier to do in PRQL?

Yes, much easier.

Check out "What’s this aggregate function?" at https://prql-lang.org/faq/. Without much context, we can understand the shape of a result.

And because queries can be longer without becoming unreadable, the lineage information is richer.

Post reply on HN