SQL Has Problems. We Can Fix Them: Pipe Syntax In SQL (research.google)
from LPThinker@lemmy.world to programming@programming.dev on 25 Aug 2024 01:42
https://lemmy.world/post/19021165

Really intriguing article about a SQL syntax extension that has apparently already been trialed at Google.

As someone who works with SQL for hours every week, this makes me hopeful for potential improvements, although the likelihood of any changes to SQL arriving in my sector before I retire seems slim.

#programming

threaded - newest

lowleveldata@programming.dev on 25 Aug 2024 02:02 next collapse

So like LINQ?

MajorHavoc@programming.dev on 25 Aug 2024 12:25 next collapse

I just threw up in my mouth a little. Do we have content warnings here? We should get content warnings here. It’s been three blessed years since I thought about the pile of crap that is LINQ. Here’s to three more!

gedhrel@lemmy.world on 25 Aug 2024 13:34 collapse

“Maybe our friend doesn’t like monads.”

Kissaki@programming.dev on 25 Aug 2024 13:42 collapse

I find LINQ query syntax less readable than SQL. I like LINQ method syntax for simple, linear queries.

The linear method syntax is somewhat like the idea of piping SQL operations.

xmunk@sh.itjust.works on 25 Aug 2024 03:07 next collapse

The reorganization of statements is excellent but the pipe operator itself is unnecessary and annoying. It’d be far better to just rearrange the clauses and call it a day, relying on the keywords that are still present to signify clause termination…

Especially once we get into subqueries and CTES, I never want to write:

|> LEFT JOIN |> FROM foo |> GROUP BY clusterid |> SELECT clusterid, COUNT(*)
      ON cluster.id = foo.clusterid

And I’m also not splitting out a trivial subselect like that into four lines because I respect my reader.

tal@lemmy.today on 25 Aug 2024 03:36 next collapse

I find dplyr in R to be pretty reasonable.

cran.r-project.org/web/packages/…/dplyr.html

I don’t know if that’s what the article is referring to by “other data languages”.

frezik@midwest.social on 25 Aug 2024 21:22 collapse

What about respecting the reader of the diff when there’s a change in the middle?

xmunk@sh.itjust.works on 25 Aug 2024 21:40 collapse

If this is something likely to change I’d space it out - but mid-line diffs are usually pretty readable in most clients.

As always, expression should cater to readability and shouldn’t be limited by syntax rules.

frezik@midwest.social on 26 Aug 2024 11:45 collapse

No matter which tool you’re using, this:

- |> LEFT JOIN |> FROM foo |> GROUP BY clusterid |> SELECT clusterid, COUNT(*)
+ |> LEFT JOIN |> FROM foobar |> GROUP BY clusterid |> SELECT clusterid, COUNT(*)
      ON cluster.id = foo.clusterid

Is always less readable than:

  |> LEFT JOIN 
- |> FROM foo 
+ |> FROM foobar
  |> GROUP BY clusterid 
  |> SELECT clusterid, COUNT(*)
      ON cluster.id = foo.clusterid

And this isn’t even the worst example I’ve seen. That would be a file that had a bug due to duplicated entries in a list, and it became very obvious as soon as I converted it to something akin to the second version.

houseofleft@slrpnk.net on 25 Aug 2024 06:34 next collapse

I lile this a lot. This reminds me a lot of KQL (a microsoft query language that’s used for a bunch if azure logging).

I use a lot of python pandas/dask- I’ve definitely got used to viewing a table as a series of operations to perform rather than the kind of declarative queries you get in SQL.

At what point is it no longer SQL? If we’re changing fundamental stuff, I’d love a way of writing loops or if statements that isn’t painful too.

xmunk@sh.itjust.works on 25 Aug 2024 07:14 next collapse

SQL has pretty powerful conditional support support already and lateral joins are essentially loops if you’re unfamiliar with them.

leisesprecher@feddit.org on 25 Aug 2024 09:11 collapse

Stored Procedures have been a thing for literally decades. But they’re an absolute pain.

What would really improve the usefulness of databases are autoindexes and generally more “let me handle that for you”. I’d argue 90% of business apps essentially need a way to store objects and their relationships, but doing that in an efficient manner is really hard (at least if you’ve got a few more rows to handle).

atzanteol@sh.itjust.works on 25 Aug 2024 13:27 next collapse

“|>”? Why? That’s such a difficult combination to type and it seems entirely unnecessary.

Kissaki@programming.dev on 25 Aug 2024 13:40 next collapse

CTRL+ALT+<, SHIFT+<

🙃🤡

frezik@midwest.social on 25 Aug 2024 13:55 next collapse

It’s used that way in Elixir. I don’t find it a problem.

RagingHungryPanda@lemm.ee on 25 Aug 2024 15:16 next collapse

F# also does that

xmunk@sh.itjust.works on 25 Aug 2024 20:25 collapse

It’s unnecessary, though - the keywords alone are sufficient. I dislike “clutter” syntax.

frezik@midwest.social on 25 Aug 2024 20:38 collapse

Possibly unpopular opinion: more languages should embrace unicode symbols in their syntax with multi-character ascii equivalents like Raku did. I set my vim config to automatically replace the ascii version with unicode. It wasn’t hard, it makes the code a little more compact, and with good character choices, it stands out in an understandable way.

kogasa@programming.dev on 25 Aug 2024 21:10 next collapse

This should be done with font ligatures, not replacing character combinations with other characters that can’t be typed normally

xmunk@sh.itjust.works on 25 Aug 2024 21:28 collapse

I think that makes it harder to work in a language… you certainly can set up an editor autoreplacement but once a decade or so someone’s going to need to hotfix something in a strange environment and trying to force things into nano using alt codes is a real pain.

That said the intentionally hard to type symbols with ascii replacements actually make me less sad than things like this syntax that requires a pipe character… I don’t know if you’re a polyglot (or ever typed on a keyboard in quebec) but most of these languages’ symbol choices are convenient on an en-US keyboard with little consideration for international keyboard layouts and there are a lot of hard to type symbols on the spanish keyboard that are very common in programming languages.

pkill@programming.dev on 25 Aug 2024 23:08 collapse

In Clojure, -> is used for inserting the piped argument at the head position in the arguments of whatever it is passed to, while ->> is used for inserting it at the tail. This approach is great for working with immutable data in a series of approachable transformations, which I believe is one reason why so many Domain-Specific Languages for generative programming are written in that language, aside from its interactive REPL. Additionally, there is no need to worry about excessive copying, as this is generally well optimized.

This can be particularly useful with HoneySQL, which is more of a DSL for SQL rather than a typical ORM tool. For example:

(defn apply-filters [query filters]
"applies WHERE clauses to a query"
  (reduce (fn [q [column value]]
            (helpers/where q [:= column value]))
          query
          filters))

(defn build-dynamic-query [{:keys [table columns filters sort-by limit]}]
  (-> {}
      (helpers/select columns)
      (helpers/from table)
      (apply-filters filters)
      (helpers/order-by sort-by)
      (helpers/limit limit)
      sql/format))

;; Result - a super readable function call that resembles a natural language 
(build-dynamic-query 
  {:table :products 
   :columns [:id :name :price] 
   :filters {:category "electronics" :in-stock true}
   :sort-by [:price :desc]
   :
Kissaki@programming.dev on 25 Aug 2024 13:45 next collapse

Any query can have zero or more pipe operators as a suffix, delin-
eated with the pipe character “|>”.

“delineated”? Looks like a typo of delimited?

Or is that a play on neat and delimited?

PumaStoleMyBluff@lemmy.world on 25 Aug 2024 14:18 next collapse

Delineated is a word and makes sense here

BradleyUffner@lemmy.world on 25 Aug 2024 14:19 collapse
FizzyOrange@programming.dev on 25 Aug 2024 14:44 collapse

I’m not sure I’m convinced by their reasons for not creating a new language (i.e. PRQL). I used it a bit and it was fantastic. It has support for using raw SQL if you need to access really niche features.

Really the only problem is that it doesn’t support mutation, or database-specific features (but you can use the raw SQL escape hatch in that case).

Still, this does look like a great improvement.