this post was submitted on 25 Aug 2024
52 points (91.9% liked)

Programming

17443 readers
123 users here now

Welcome to the main community in programming.dev! Feel free to post anything relating to programming here!

Cross posting is strongly encouraged in the instance. If you feel your post or another person's post makes sense in another community cross post into it.

Hope you enjoy the instance!

Rules

Rules

  • Follow the programming.dev instance rules
  • Keep content related to programming in some way
  • If you're posting long videos try to add in some form of tldr for those who don't want to watch videos

Wormhole

Follow the wormhole through a path of communities !webdev@programming.dev



founded 1 year ago
MODERATORS
 

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.

top 25 comments
sorted by: hot top controversial new old
[–] atzanteol@sh.itjust.works 10 points 2 months ago (3 children)

"|>"? Why? That's such a difficult combination to type and it seems entirely unnecessary.

[–] frezik@midwest.social 5 points 2 months ago (2 children)

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

[–] xmunk@sh.itjust.works 5 points 2 months ago (1 children)

It's unnecessary, though - the keywords alone are sufficient. I dislike "clutter" syntax.

[–] frezik@midwest.social 0 points 2 months ago (2 children)

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.

[–] xmunk@sh.itjust.works 6 points 2 months ago

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.

[–] kogasa@programming.dev 4 points 2 months ago

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

[–] RagingHungryPanda@lemm.ee 4 points 2 months ago

F# also does that

[–] pkill@programming.dev 2 points 2 months ago* (last edited 2 months ago)

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]
   :limit 20})
[–] Kissaki@programming.dev 0 points 2 months ago

CTRL+ALT+<, SHIFT+<

🙃🤡

[–] lowleveldata@programming.dev 10 points 2 months ago (2 children)
[–] Kissaki@programming.dev 10 points 2 months ago* (last edited 2 months ago)

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.

[–] MajorHavoc@programming.dev 1 points 2 months ago (1 children)

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 4 points 2 months ago

"Maybe our friend doesn't like monads."

[–] xmunk@sh.itjust.works 6 points 2 months ago (2 children)

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 4 points 2 months ago

I find dplyr in R to be pretty reasonable.

https://cran.r-project.org/web/packages/dplyr/vignettes/dplyr.html

I don't know if that's what the article is referring to by "other data languages".

[–] frezik@midwest.social 2 points 2 months ago (1 children)

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

[–] xmunk@sh.itjust.works 2 points 2 months ago (1 children)

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 2 points 2 months ago* (last edited 2 months ago)

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 4 points 2 months ago (2 children)

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.

[–] leisesprecher@feddit.org 7 points 2 months ago

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).

[–] xmunk@sh.itjust.works 4 points 2 months ago

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

[–] FizzyOrange@programming.dev 3 points 2 months ago

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.

[–] Kissaki@programming.dev -2 points 2 months ago* (last edited 2 months ago) (2 children)

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 4 points 2 months ago

Delineated is a word and makes sense here