Hi,
I have created a small patch to postgres source (in particular the
psql part of it) that accepts trailing comma at the end of list in
SELECT statement.

The idea is to be able to say both (with the same result):
SELECT a, b, c from t;
SELECT a, b, c, from t;

Attached you can find a patch containing regression test (incorporated
into the serial_schedule).
My patch is relative to origin/REL9_4_STABLE branch as that is the one
I started from.

My plea is to have this change merged into the main stream so that it
becomes available in upcoming releases.

This modification does not require any interaction with user.
It does not create any backward compatibility issues.
Not does it have any performance impact.

regards
bogdan
>From 450c339b4284887782b30e154766a0ee90d6f7ee Mon Sep 17 00:00:00 2001
From: Bogdan Pilch <bogdan.pi...@opensynergy.com>
Date: Sat, 16 Aug 2014 19:42:29 +0200
Subject: [PATCH 1/3] BPI: Added support for ignoring the trailing comma in
 select statement

---
 src/backend/parser/gram.y | 1 +
 1 file changed, 1 insertion(+)

diff --git a/src/backend/parser/gram.y b/src/backend/parser/gram.y
index 7b9895d..345c6cb 100644
--- a/src/backend/parser/gram.y
+++ b/src/backend/parser/gram.y
@@ -12470,6 +12470,7 @@ ctext_row: '(' ctext_expr_list ')'					{ $$ = $2; }
  *****************************************************************************/
 
 opt_target_list: target_list						{ $$ = $1; }
+			| target_list ','				{ $$ = $1; }
 			| /* EMPTY */							{ $$ = NIL; }
 		;
 
-- 
1.9.1


>From 9faf5eec4975eb99ad7c8901e30742ba92c0c4cb Mon Sep 17 00:00:00 2001
From: Bogdan Pilch <bogdan.pi...@opensynergy.com>
Date: Sun, 28 Sep 2014 13:12:24 +0200
Subject: [PATCH 3/3] Added regression test for trailing comma select feature.

---
 .../regress/expected/select_trailing_comma.out     | 53 ++++++++++++++++++++++
 src/test/regress/serial_schedule                   |  1 +
 src/test/regress/sql/select_trailing_comma.sql     | 16 +++++++
 3 files changed, 70 insertions(+)
 create mode 100644 src/test/regress/expected/select_trailing_comma.out
 create mode 100644 src/test/regress/sql/select_trailing_comma.sql

diff --git a/src/test/regress/expected/select_trailing_comma.out b/src/test/regress/expected/select_trailing_comma.out
new file mode 100644
index 0000000..f84938c
--- /dev/null
+++ b/src/test/regress/expected/select_trailing_comma.out
@@ -0,0 +1,53 @@
+--
+-- SELECT WITH TRAILING COMMA
+--
+CREATE TEMP TABLE primes (p1 int, p2 int, p3 int);
+INSERT INTO primes VALUES (13, 7927, 7);
+SELECT * FROM primes;
+ p1 |  p2  | p3 
+----+------+----
+ 13 | 7927 |  7
+(1 row)
+
+SELECT *, FROM primes;
+ p1 |  p2  | p3 
+----+------+----
+ 13 | 7927 |  7
+(1 row)
+
+SELECT p1 FROM primes;
+ p1 
+----
+ 13
+(1 row)
+
+SELECT p1, FROM primes;
+ p1 
+----
+ 13
+(1 row)
+
+SELECT p1, p2 FROM primes;
+ p1 |  p2  
+----+------
+ 13 | 7927
+(1 row)
+
+SELECT p1, p2, FROM primes;
+ p1 |  p2  
+----+------
+ 13 | 7927
+(1 row)
+
+SELECT p1, p2, p3 FROM primes;
+ p1 |  p2  | p3 
+----+------+----
+ 13 | 7927 |  7
+(1 row)
+
+SELECT p1, p2, p3, FROM primes;
+ p1 |  p2  | p3 
+----+------+----
+ 13 | 7927 |  7
+(1 row)
+
diff --git a/src/test/regress/serial_schedule b/src/test/regress/serial_schedule
index 16a1905..3571d14 100644
--- a/src/test/regress/serial_schedule
+++ b/src/test/regress/serial_schedule
@@ -79,6 +79,7 @@ test: select_distinct
 test: select_distinct_on
 test: select_implicit
 test: select_having
+test: select_trailing_comma
 test: subselect
 test: union
 test: case
diff --git a/src/test/regress/sql/select_trailing_comma.sql b/src/test/regress/sql/select_trailing_comma.sql
new file mode 100644
index 0000000..a2c922f
--- /dev/null
+++ b/src/test/regress/sql/select_trailing_comma.sql
@@ -0,0 +1,16 @@
+--
+-- SELECT WITH TRAILING COMMA
+--
+
+CREATE TEMP TABLE primes (p1 int, p2 int, p3 int);
+
+INSERT INTO primes VALUES (13, 7927, 7);
+
+SELECT * FROM primes;
+SELECT *, FROM primes;
+SELECT p1 FROM primes;
+SELECT p1, FROM primes;
+SELECT p1, p2 FROM primes;
+SELECT p1, p2, FROM primes;
+SELECT p1, p2, p3 FROM primes;
+SELECT p1, p2, p3, FROM primes;
-- 
1.9.1

-- 
Sent via pgsql-hackers mailing list (pgsql-hackers@postgresql.org)
To make changes to your subscription:
http://www.postgresql.org/mailpref/pgsql-hackers

Reply via email to