Allowing additional commas between columns, and at the end of the SELECT clause

2024-05-13 Thread Artur Formella

Hello!
I have created a patch to allow additional commas between columns, and 
at the end of the SELECT clause.


Motivation:
Commas of this type are allowed in many programming languages, in some 
it is even recommended to use them at the ends of lists or objects. A 
new generation of programmers expects a more forgiving language just as 
our generation enjoyed LIMIT and the ability to write `select` in lowercase.


Accepted:
    SELECT 1,;
    SELECT 1,;
    SELECT *, from information_schema.sql_features;
    (...) RETURNING a,,b,c,;

Not accepted:
    SELECT ,;
    SELECT ,1;
    SELECT ,,,;

Advantages:
- simplifies the creation and debugging of queries by reducing the most 
common syntax error,
- eliminates the need to use the popular `1::int as dummy` at the end of 
a SELECT list,

- simplifies query generators,
- the query is still deterministic,

Disadvantages:
- counting of returned columns can be difficult,
- syntax checkers will still report errors,
- probably not SQL standard compliant,
- functionality can be controversial,

I attach the patch along with the tests.

What do you think?

Your opinions are very much welcome!

diff --git a/src/backend/parser/gram.y b/src/backend/parser/gram.y
index e8b619926e..8bf0a2690e 100644
--- a/src/backend/parser/gram.y
+++ b/src/backend/parser/gram.y
@@ -17174,6 +17174,7 @@ opt_target_list: target_list
{ $$ = $1; }
 target_list:
target_el   
{ $$ = list_make1($1); }
| target_list ',' target_el 
{ $$ = lappend($1, $3); }
+   | target_list ','   
/* Handling trailing comma */
;
 
 target_el: a_expr AS ColLabel
diff --git a/src/test/regress/expected/returning.out 
b/src/test/regress/expected/returning.out
index cb51bb8687..8604e50e18 100644
--- a/src/test/regress/expected/returning.out
+++ b/src/test/regress/expected/returning.out
@@ -238,7 +238,7 @@ CREATE OR REPLACE RULE voo_u AS ON UPDATE TO voo DO INSTEAD
   UPDATE foo SET f1 = new.f1, f2 = new.f2 WHERE f1 = old.f1
   RETURNING f1, f2;
 update voo set f1 = f1 + 1 where f2 = 'zoo2';
-update voo set f1 = f1 + 1 where f2 = 'zoo2' RETURNING *, f1*2;
+update voo set f1 = f1 + 1 where f2 = 'zoo2' RETURNING *, f1*2,;
  f1 |  f2  | ?column? 
 +--+--
  16 | zoo2 |   32
@@ -264,7 +264,7 @@ SELECT * FROM voo;
 
 CREATE OR REPLACE RULE voo_d AS ON DELETE TO voo DO INSTEAD
   DELETE FROM foo WHERE f1 = old.f1
-  RETURNING f1, f2;
+  RETURNING f1, , , , f2;
 DELETE FROM foo WHERE f1 = 13;
 DELETE FROM foo WHERE f2 = 'zit' RETURNING *;
  f1 | f2  | f3 | f4 
diff --git a/src/test/regress/expected/select.out 
b/src/test/regress/expected/select.out
index 33a6dceb0e..e4b9e916da 100644
--- a/src/test/regress/expected/select.out
+++ b/src/test/regress/expected/select.out
@@ -968,3 +968,50 @@ explain (costs off) select * from list_parted_tbl;
 (2 rows)
 
 drop table list_parted_tbl;
+-- Handling trailing comma
+SELECT *, FROM onek
+   WHERE onek.unique1 < 10
+   ORDER BY onek.unique1;
+ unique1 | unique2 | two | four | ten | twenty | hundred | thousand | 
twothousand | fivethous | tenthous | odd | even | stringu1 | stringu2 | string4 
+-+-+-+--+-++-+--+-+---+--+-+--+--+--+-
+   0 | 998 |   0 |0 |   0 |  0 |   0 |0 |  
 0 | 0 |0 |   0 |1 | AA   | KMBAAA   | xx
+   1 | 214 |   1 |1 |   1 |  1 |   1 |1 |  
 1 | 1 |1 |   2 |3 | BA   | GI   | xx
+   2 | 326 |   0 |2 |   2 |  2 |   2 |2 |  
 2 | 2 |2 |   4 |5 | CA   | OM   | xx
+   3 | 431 |   1 |3 |   3 |  3 |   3 |3 |  
 3 | 3 |3 |   6 |7 | DA   | PQ   | xx
+   4 | 833 |   0 |0 |   4 |  4 |   4 |4 |  
 4 | 4 |4 |   8 |9 | EA   | BGBAAA   | xx
+   5 | 541 |   1 |1 |   5 |  5 |   5 |5 |  
 5 | 5 |5 |  10 |   11 | FA   | VU   | xx
+   6 | 978 |   0 |2 |   6 |  6 |   6 |6 |  
 6 | 6 |6 |  12 |   13 | GA   | QLBAAA   | xx
+   7 | 647 |   1 |3 |   7 |  7 |   7 |7 |  
 7 | 7 |7 |  14 |   15 | HA   | XY   | xx
+   8 | 653 |   0 |0 |   8 |  8 |   8 |8 |  
 8 | 8 |8 |  16 |   17 | IA   | DZ   | xx
+   9 |  49 |   1 |1 |   9 |  9 |   9 |9 |  
 9 | 9 |9 |  18 |   19 | JA   

Re: Allowing additional commas between columns, and at the end of the SELECT clause

2024-05-14 Thread Artur Formella

On 13.05.2024 11:24, Matthias van de Meent wrote:

On Mon, 13 May 2024 at 10:42, Artur Formella  wrote:

Motivation:
Commas of this type are allowed in many programming languages, in some
it is even recommended to use them at the ends of lists or objects.

Single trailing commas are a feature that's more and more common in
languages, yes, but arbitrary excess commas is new to me. Could you
provide some examples of popular languages which have that, as I can't
think of any.

Thank for your comment.
I meant commas are recommended at the end of the list. Sorry for the 
lack of precision.
Typescript has a popular directive "rules": { "trailing-comma": false } 
in the tslint.json file, which forces trailing commas. Popular Airbnb 
coding style require trailing commas by eslint 
(https://github.com/airbnb/javascript?tab=readme-ov-file#functions--signature-invocation-indentation).



This is the first time I've heard of this `1 as dummy`.


dummy column is a popular way to end SELECT list on R&D phase to avoid 
the most common syntax error. This way you don't have to pay attention 
to commas.


SELECT  , 1::int AS ignoreme FROM 


- simplifies query generators,
- the query is still deterministic,

What part of a query would (or would not) be deterministic? I don't
think I understand the potential concern here. Is it about whether the
statement can be parsed deterministically?


Bison doesn't report error or conflict.


I'd argue you better raise this with the standard committee if this
isn't compliant. I don't see enough added value to break standard
compliance here, especially when the standard may at some point allow
only a single trailing comma (and not arbitrarily many).


Do you expect `SELECT 1,,,` to have an equivalent query identifier
to `SELECT 1;` in pg_stat_statements? Why, or why not?
I don't know, I have a feeling that the queries are equivalent, but I 
don't know the mechanism.

Overall, I don't think unlimited commas is a good feature. A trailing
comma in the select list would be less problematic, but I'd still want
to follow the standard first and foremost.


I will prepare a patch with trailing comma only tomorrow.

Thank you.

Artur