Live data from Hacker News

Show HN: PRQL 0.2 – a better SQL

github.com

71–80 of 166 posts

Re: Show HN: PRQL 0.2 – a better SQL

#71

I've thought about building a better query language too. I'd love the ability to model sum types in databases, something like: enum SchoolType { College { degrees: Vec }, HighSchool } It's such a common pattern and yet it's so annoying to model in a normal relational database. I wouldn't be surprised if the rise of NoSQL is tied to the inability of relational databases to model basic patterns like this. Part of me ha…

What is annoying about implementing something like this in a relational database?

It's not straightforward to do polymorphic joins: one common pattern is to have child tables for each case of the union, but there's no integrity constraint such that each parent must only have one child, e.g.

  CREATE TABLE schools (id SERIAL PRIMARY KEY);
  CREATE TABLE colleges (id INTEGER NOT NULL REFERENCES schools (id));
  CREATE TABLE high_schools (id INTEGER NOT NULL REFERENCES schools (id));
How can you ensure that a school is either a college or high_school but not both?

Another alternative is to make one big table with check constraints but that's also hairy in its own right:

  CREATE TYPE school_type AS ENUM ('college', 'high_school');
  CREATE TABLE schools (
    id SERIAL PRIMARY KEY,
    type school_type,
    /* college columns */,
    /* high school columns */,
    CHECK (type = 'college' AND /* college column constraints */),
    CHECK (type = 'high_school' AND /* high school column constraints */)
  );
The other thing in the grandparent's comment that's a constant pain in SQL is representing an ordered list: how do you insert items into the middle of the list? Depending on your database, it can also be painful to renumber the other items.

Re: Show HN: PRQL 0.2 – a better SQL

#73
Is it correct there's no CASE WHEN and instead you have to define a function using a ternary operator? CASE WHENs may be verbose but when you have a dozen of them they're more readable and the waterfall nature is far preferable to a giant block of ternary clauses.

Re: Show HN: PRQL 0.2 – a better SQL

#74
post #71

Earlier quoted context omitted.

What is annoying about implementing something like this in a relational database?

It's not straightforward to do polymorphic joins: one common pattern is to have child tables for each case of the union, but there's no integrity constraint such that each parent must only have one child, e.g. CREATE TABLE schools (id SERIAL PRIMARY KEY); CREATE TABLE colleges (id INTEGER NOT NULL REFERENCES schools (id)); CREATE TABLE high_schools (id INTEGER NOT NULL REFERENCES schools (id)); How can you ensure tha…

You add a xor non null check on the foreign keys?

Re: Show HN: PRQL 0.2 – a better SQL

#75

Thanks, I've frequently wanted a query language that was designed after the 70s. The ideas are sound, but a modernized syntax with variables to reuse subqueries would be lovely. This looks like it. I noticed one issue though... please don't copy the prefix of f-strings! That only exists because Python boxed itself in and it was literally the only ascii syntax left that could be used for string interpolation. It's mil…

Interesting suggestion. We added f-strings because we already had s-strings (pass trough to SQL) and r-strings (for raw multi-line text).

And would you rather see "My {name}" or "My ${name}"? I personally dislike the $ prefix for all variables and interpolations...

Re: Show HN: PRQL 0.2 – a better SQL

#76
post #43
post #15

Here's one suggestion: SQL tediously requires specifying the equality condition on joins, when 90% of the time you just want to join on the fk defined between the tables. from a join b should implicitly join on the FK if no condition is given. It would require knowledge of the schema. I don't know if this is possible in PRQL, or if the transpilation to SQL has to be stateless.

This is something you might end up regretting later. It’s annoying adding another foreign key later and then having previously working queries fail at runtime due to an ambiguous join condition.

Agree about not implicitly finding the join key. But as long as we're brainstorming imaginary features, then maybe as part of the schema, we could somehow declare the default join key to use, for any given two tables. In most cases it's pretty obvious what the best join key would be.

Re: Show HN: PRQL 0.2 – a better SQL

#77
post #15

Here's one suggestion: SQL tediously requires specifying the equality condition on joins, when 90% of the time you just want to join on the fk defined between the tables. from a join b should implicitly join on the FK if no condition is given. It would require knowledge of the schema. I don't know if this is possible in PRQL, or if the transpilation to SQL has to be stateless.

If you're willing to sacrifice economics elsewhere, repeating the table's name in the id column is one workaround: from a join b on b.a_id = a.a_id You can even use NATURAL JOIN if you can guarantee that the only fkey/pkey names will overlap between tables. An unreasonable way to achieve that is to put the table name in every column. A more palatable way is to write some clever functions in your schema to scan the in…

If you have identical field names, you can do in sql:

  Select * from a join b using (a_id)
Don't do this in Oracle though, pain follows when you try to touch an a_id column.

Re: Show HN: PRQL 0.2 – a better SQL

#78

I've thought about building a better query language too. I'd love the ability to model sum types in databases, something like: enum SchoolType { College { degrees: Vec }, HighSchool } It's such a common pattern and yet it's so annoying to model in a normal relational database. I wouldn't be surprised if the rise of NoSQL is tied to the inability of relational databases to model basic patterns like this. Part of me ha…

You'd have 1 table per sum type which requires extra data. Then polymorphic foreign key (aka a pair of fields school_type, school_id). (No foreign key constraints, but those are falling out of use in some cases due to inability to online migrate mysql schemas anyways.)

You can retain foreign key constraints by having one column per type of reference. It is also possible to ensure that exactly one column of several is NOT NULL, so that the columns can always be mapped to an enum in application code. Also, in PostgreSQL, the storage for the extra NULLs uses just one bit per column in a bitmap.

    CREATE TABLE dirents (
        parent         bigint   NOT NULL,
        child_dir      bigint,
        child_file     bigint,
        child_symlink  bigint,
        basename       text     NOT NULL,
    
        -- Ensure exactly one type of child is set
        CHECK (num_nonnulls(child_dir, child_file, child_symlink) = 1),
    
        CONSTRAINT dirents_child_dir_fkey     FOREIGN KEY (child_dir)     REFERENCES dirs (id),
        CONSTRAINT dirents_child_file_fkey    FOREIGN KEY (child_file)    REFERENCES files (id),
        CONSTRAINT dirents_child_symlink_fkey FOREIGN KEY (child_symlink) REFERENCES symlinks (id),
    
        PRIMARY KEY (parent, basename)
    );

Re: Show HN: PRQL 0.2 – a better SQL

#79

Earlier quoted context omitted.

The second part of your statement is fine, but the first part is just a complete fallacy. I can transpile a pure language exposing only `if`, `while`, and `for` with no standard library and no interop to C - that definitely does not make it "trivial" that it can do everything SQL can do.

I realized this after posting and edited the post. Thanks.

No problem. (I didn't downvote.)

Re: Show HN: PRQL 0.2 – a better SQL

#80

Earlier quoted context omitted.

I assume it can't do anything SQL can't, because they write "It can be used with any database that uses SQL, since it transpiles to SQL." Not sure about the reverse. I'm used to SQL syntax, but this has definite appeal. As a small example, I like that it starts with the "from" clause, so autocomplete is more viable.

Transpiling to SQL doesn't mean all the underlying SQL features are being exposed to you.

Yes, that's what I meant by "not sure about the reverse".
Post reply on HN