Live data from Hacker News

How about trailing commas in SQL?

peter.eisentraut.org

51–60 of 272 posts

Re: How about trailing commas in SQL?

#51
post #13

Earlier quoted context omitted.

> it is annoying that the end of the list is always special. You can't just copy some line and move it there. Also when moving things around you always have to take extra care at the end. You have simply moved the "special" entry to the beginning rather than the end. Side remark: I've noticed that when it comes to code formatting and grammar it's almost like there are broadly two camps. There are some instances of co…

Adding something to the end is the most common thing to do. And changing the start is extremely rare - it's anyway special because you usually put it in the line with the SELECT.

> you usually put it in the line with the SELECT.

No, you usually don't. That would "bury" the first field so you don't immediately see it if you quickly glance at the code. I'll admit I was a bit surprised when I saw a fully formatted SQL query but it does look much better:

   SELECT
      a,
      b,
      c,
      d
   FROM
      Customers
   ORDER BY
      b,
      c DESC

Edit: I've just seen other comments here suggesting you return an extra "pad" value at the start so you can use leading commas without "losing" the first value visually. I hardly know where to start with that. It transmits more data on the wire and, to my eyes, looks horrific. That level of hackery is proof, as if it were needed, of how badly needed trailing commas are.

Re: How about trailing commas in SQL?

#52
post #6

I feel the problem. When coding (not only in SQL) you often have to add something to the end of a list, and it is annoying that the end of the list is always special. You can't just copy some line and move it there. Also when moving things around you always have to take extra care at the end. So, my solution for this was always SELECT a , b , c FROM ... instead of: SELECT a, b, c, -- people want to allow trailing com…

That's as bad as using regular expressions: now you have TWO problems. Why do you seem to think you've cleverly solved the problem, when you've just moved the problem somewhere else just as bad, by blithely messing with the standard formatting conventions universally used by most human written languages and programming languages in the world? Programming languages borrow commas from human written languages, and no hu…

>That's not a solution at all, and there's nothing clever about it.

It is a solution, and I'm not motivated by trying to be "clever". It just makes writing and reading the query easier for me.

>You've just pushed the problem to the beginning of the list

The beginning of the list is modified less often than the end. The two cases aren't symmetric.

>and now your code is also butt-ugly with totally non-standard formatting

"Ugly" is subjective. Personally I like how the commas line up vertically, so I can tell at a glance that I didn't miss one out. SQL doesn't have a standard formatting in any case. It's whitespace insensitive and I've seen people write it in all kinds of weird ways.

>which sane people don't recognize and editors and IDEs and linters don't support.

A difference in code-formatting taste is not "insanity". And it does not interfere with tooling at all.

>showboating displays of pointlessly creative cleverness

Where are you getting all of this from? You seem to be imagining a "type of guy" in your head, so that you can be mad at him.

I am reminded of Sayre's law: In any dispute the intensity of feeling is inversely proportional to the value of the issues at stake.

Re: How about trailing commas in SQL?

#53
post #37

Prql has trailing commas https://prql-lang.org/ if you're ok with running that transformation.

I was thinking about it again recently, but can't tell if it's worth. Have you used it in real life projects? How was it?

It's nice for more complex queries. Not a big difference for simple ones. I don't have a more nuanced take here - it works fine.

Re: How about trailing commas in SQL?

#54

Earlier quoted context omitted.

That is the haskell workaround, and it also sucks, because it still requires a special non-uniform first value. I do not want to write either of your snippets, I want to write SELECT a, b, c, FROM Because now selected values are uniform and I can move them around or add new ones with minimal changes no matter their position in the sequence . It’s also completely wonky in many contexts e.g. CREATE TABLE. Trailing comm…

I'd still want to have leading commas. If the items in the list are long IF(...), maybe uses several lines and maybe contain SELECTs it's hard to see a missing trailing comma. At the start they're all lined up well, and it's very hard to get them wrong.

> I'd still want to have leading commas.

Real happy for you. Trailing commas support don’t prevent you from doing that.

Re: How about trailing commas in SQL?

#56
post #13
post #6

I feel the problem. When coding (not only in SQL) you often have to add something to the end of a list, and it is annoying that the end of the list is always special. You can't just copy some line and move it there. Also when moving things around you always have to take extra care at the end. So, my solution for this was always SELECT a , b , c FROM ... instead of: SELECT a, b, c, -- people want to allow trailing com…

> it is annoying that the end of the list is always special. You can't just copy some line and move it there. Also when moving things around you always have to take extra care at the end. You have simply moved the "special" entry to the beginning rather than the end. Side remark: I've noticed that when it comes to code formatting and grammar it's almost like there are broadly two camps. There are some instances of co…

> Code ligature thing has something to do with just seeing the characters that are actually there rather than a smokescreen, which IMO impedes editability because I can't place the cursor half-way through a ligature and so on

Why wouldn't that be possible? (The cursor thing)

It's still two characters as far as your editor is concerned.

Re: How about trailing commas in SQL?

#57
I'd be happy with an query editor that silently removes the comma. I write way more ad hoc SQL than what I deploy, and this would be a great tool for faster exploration of data (ditto for "FROM table SELECT columnA, ....")

Re: How about trailing commas in SQL?

#58
post #2

Ugh. Why? To make copy/paste programming easier? To make query generation easier? When you're writing code to generate queries, it's worth doing it right. Just about every programming language has an easy way to take an array of strings and add a separator between each element. Like PHP's implode: implode(', ', ['foo', 'bar', 'baz']) == 'foo, bar, baz'. Every time I see a trailing comma, I think "is this a bug? did t…

That's what I was thinking. Sometimes it's called join, like Enum.join/2 in Elixir.

Either you're cuddling the strings with your hands and it'll take time and care anyway, or you're doing programmatic massage, in which case most DB-driver capable languages has a bunch of functions that solve issues like these.

Re: How about trailing commas in SQL?

#59
post #55

> this might be the most requested feature in SQL Don’t forget to take into account the people not requesting this feature. A lot of them don’t think this is worth the changeover.

> A lot of them don’t think this is worth the changeover.

I think nobody wants to make trailing commas mandatory to use

Post reply on HN