From 5187deb2db0edf4b0823f5d9102f34f501b9b2f1 Mon Sep 17 00:00:00 2001
From: Alvaro Herrera <alvherre@alvh.no-ip.org>
Date: Tue, 26 Oct 2010 11:40:06 -0300
Subject: [PATCH] Change syntax to add a new enum value to ALTER TYPE ADD ELEMENT

Per discussion.
---
 doc/src/sgml/ref/alter_type.sgml   |    8 ++--
 src/backend/parser/gram.y          |   19 +++++----
 src/include/parser/kwlist.h        |    1 +
 src/test/regress/expected/enum.out |   74 ++++++++++++++++++------------------
 src/test/regress/sql/enum.sql      |   74 ++++++++++++++++++------------------
 5 files changed, 89 insertions(+), 87 deletions(-)

diff --git a/doc/src/sgml/ref/alter_type.sgml b/doc/src/sgml/ref/alter_type.sgml
index 90de2e8..1fbade1 100644
--- a/doc/src/sgml/ref/alter_type.sgml
+++ b/doc/src/sgml/ref/alter_type.sgml
@@ -28,7 +28,7 @@ ALTER TYPE <replaceable class="PARAMETER">name</replaceable> OWNER TO <replaceab
 ALTER TYPE <replaceable class="PARAMETER">name</replaceable> RENAME ATTRIBUTE <replaceable class="PARAMETER">attribute_name</replaceable> TO <replaceable class="PARAMETER">new_attribute_name</replaceable>
 ALTER TYPE <replaceable class="PARAMETER">name</replaceable> RENAME TO <replaceable class="PARAMETER">new_name</replaceable>
 ALTER TYPE <replaceable class="PARAMETER">name</replaceable> SET SCHEMA <replaceable class="PARAMETER">new_schema</replaceable>
-ALTER TYPE <replaceable class="PARAMETER">name</replaceable> ADD <replaceable class="PARAMETER">new_enum_value</replaceable> [ { BEFORE | AFTER } <replaceable class="PARAMETER">existing_enum_value</replaceable> ]
+ALTER TYPE <replaceable class="PARAMETER">name</replaceable> ADD ELEMENT <replaceable class="PARAMETER">new_enum_value</replaceable> [ { BEFORE | AFTER } <replaceable class="PARAMETER">existing_enum_value</replaceable> ]
 
 <phrase>where <replaceable class="PARAMETER">action</replaceable> is one of:</phrase>
 
@@ -106,7 +106,7 @@ ALTER TYPE <replaceable class="PARAMETER">name</replaceable> ADD <replaceable cl
    </varlistentry>
 
    <varlistentry>
-    <term><literal>ADD [ BEFORE | AFTER ]</literal></term>
+    <term><literal>ADD ELEMENT [ BEFORE | AFTER ]</literal></term>
     <listitem>
      <para>
       This form adds a new value to an enum type. If the new value's place in
@@ -238,7 +238,7 @@ ALTER TYPE <replaceable class="PARAMETER">name</replaceable> ADD <replaceable cl
   <title>Notes</title>
 
   <para>
-   <command>ALTER TYPE ... ADD</> (the form that adds a new value to an
+   <command>ALTER TYPE ... ADD ELEMENT</> (the form that adds a new value to an
    enum type) cannot be executed inside a transaction block.
   </para>
 
@@ -292,7 +292,7 @@ ALTER TYPE compfoo ADD ATTRIBUTE f3 int;
   <para>
    To add a new value to an enum type in a particular sort position:
 <programlisting>
-ALTER TYPE colors ADD 'orange' AFTER 'red';
+ALTER TYPE colors ADD ELEMENT 'orange' AFTER 'red';
 </programlisting>
   </para>
  </refsect1>
diff --git a/src/backend/parser/gram.y b/src/backend/parser/gram.y
index 1394b21..287ff20 100644
--- a/src/backend/parser/gram.y
+++ b/src/backend/parser/gram.y
@@ -483,7 +483,7 @@ static RangeVar *makeRangeVarFromAnyName(List *names, int position, core_yyscan_
 	DEFERRABLE DEFERRED DEFINER DELETE_P DELIMITER DELIMITERS DESC
 	DICTIONARY DISABLE_P DISCARD DISTINCT DO DOCUMENT_P DOMAIN_P DOUBLE_P DROP
 
-	EACH ELSE ENABLE_P ENCODING ENCRYPTED END_P ENUM_P ESCAPE EXCEPT
+	EACH ELEMENT ELSE ENABLE_P ENCODING ENCRYPTED END_P ENUM_P ESCAPE EXCEPT
 	EXCLUDE EXCLUDING EXCLUSIVE EXECUTE EXISTS EXPLAIN EXTERNAL EXTRACT
 
 	FALSE_P FAMILY FETCH FIRST_P FLOAT_P FOLLOWING FOR FORCE FOREIGN FORWARD
@@ -3871,30 +3871,30 @@ enum_val_list:	Sconst
  *****************************************************************************/
 
 AlterEnumStmt:
-         ALTER TYPE_P any_name ADD_P Sconst
+         ALTER TYPE_P any_name ADD_P ELEMENT Sconst
 			 {
 				 AlterEnumStmt *n = makeNode(AlterEnumStmt);
 				 n->typeName = $3;
-				 n->newVal = $5;
+				 n->newVal = $6;
 				 n->newValNeighbor = NULL;
 				 n->newValIsAfter = true;
 				 $$ = (Node *) n;
 			 }
-		 | ALTER TYPE_P any_name ADD_P Sconst BEFORE Sconst
+		 | ALTER TYPE_P any_name ADD_P ELEMENT Sconst BEFORE Sconst
 			 {
 				 AlterEnumStmt *n = makeNode(AlterEnumStmt);
 				 n->typeName = $3;
-				 n->newVal = $5;
-				 n->newValNeighbor = $7;
+				 n->newVal = $6;
+				 n->newValNeighbor = $8;
 				 n->newValIsAfter = false;
 				 $$ = (Node *) n;
 			 }
-		 | ALTER TYPE_P any_name ADD_P Sconst AFTER Sconst
+		 | ALTER TYPE_P any_name ADD_P ELEMENT Sconst AFTER Sconst
 			 {
 				 AlterEnumStmt *n = makeNode(AlterEnumStmt);
 				 n->typeName = $3;
-				 n->newVal = $5;
-				 n->newValNeighbor = $7;
+				 n->newVal = $6;
+				 n->newValNeighbor = $8;
 				 n->newValIsAfter = true;
 				 $$ = (Node *) n;
 			 }
@@ -11141,6 +11141,7 @@ unreserved_keyword:
 			| DOUBLE_P
 			| DROP
 			| EACH
+			| ELEMENT
 			| ENABLE_P
 			| ENCODING
 			| ENCRYPTED
diff --git a/src/include/parser/kwlist.h b/src/include/parser/kwlist.h
index 2c44cf7..f1bd388 100644
--- a/src/include/parser/kwlist.h
+++ b/src/include/parser/kwlist.h
@@ -136,6 +136,7 @@ PG_KEYWORD("domain", DOMAIN_P, UNRESERVED_KEYWORD)
 PG_KEYWORD("double", DOUBLE_P, UNRESERVED_KEYWORD)
 PG_KEYWORD("drop", DROP, UNRESERVED_KEYWORD)
 PG_KEYWORD("each", EACH, UNRESERVED_KEYWORD)
+PG_KEYWORD("element", ELEMENT, UNRESERVED_KEYWORD)
 PG_KEYWORD("else", ELSE, RESERVED_KEYWORD)
 PG_KEYWORD("enable", ENABLE_P, UNRESERVED_KEYWORD)
 PG_KEYWORD("encoding", ENCODING, UNRESERVED_KEYWORD)
diff --git a/src/test/regress/expected/enum.out b/src/test/regress/expected/enum.out
index b1ba3f1..9f1faed 100644
--- a/src/test/regress/expected/enum.out
+++ b/src/test/regress/expected/enum.out
@@ -39,7 +39,7 @@ ORDER BY 2;
  mars      |             3
 (3 rows)
 
-ALTER TYPE planets ADD 'uranus';
+ALTER TYPE planets ADD ELEMENT 'uranus';
 SELECT enumlabel, enumsortorder
 FROM pg_enum
 WHERE enumtypid = 'planets'::regtype
@@ -52,10 +52,10 @@ ORDER BY 2;
  uranus    |             4
 (4 rows)
 
-ALTER TYPE planets ADD 'mercury' BEFORE 'venus';
-ALTER TYPE planets ADD 'saturn' BEFORE 'uranus';
-ALTER TYPE planets ADD 'jupiter' AFTER 'mars';
-ALTER TYPE planets ADD 'neptune' AFTER 'uranus';
+ALTER TYPE planets ADD ELEMENT 'mercury' BEFORE 'venus';
+ALTER TYPE planets ADD ELEMENT 'saturn' BEFORE 'uranus';
+ALTER TYPE planets ADD ELEMENT 'jupiter' AFTER 'mars';
+ALTER TYPE planets ADD ELEMENT 'neptune' AFTER 'uranus';
 SELECT enumlabel, enumsortorder
 FROM pg_enum
 WHERE enumtypid = 'planets'::regtype
@@ -89,46 +89,46 @@ ORDER BY enumlabel::planets;
 (8 rows)
 
 -- errors for adding labels
-ALTER TYPE planets ADD
+ALTER TYPE planets ADD ELEMENT
   'plutoplutoplutoplutoplutoplutoplutoplutoplutoplutoplutoplutoplutopluto';
 ERROR:  invalid enum label "plutoplutoplutoplutoplutoplutoplutoplutoplutoplutoplutoplutoplutopluto"
 DETAIL:  Labels must be 63 characters or less.
-ALTER TYPE planets ADD 'pluto' AFTER 'zeus';
+ALTER TYPE planets ADD ELEMENT 'pluto' AFTER 'zeus';
 ERROR:  "zeus" is not an existing enum label
 --
 -- Test inserting so many values that we have to renumber
 --
 create type insenum as enum ('L1', 'L2');
-alter type insenum add 'i1' before 'L2';
-alter type insenum add 'i2' before 'L2';
-alter type insenum add 'i3' before 'L2';
-alter type insenum add 'i4' before 'L2';
-alter type insenum add 'i5' before 'L2';
-alter type insenum add 'i6' before 'L2';
-alter type insenum add 'i7' before 'L2';
-alter type insenum add 'i8' before 'L2';
-alter type insenum add 'i9' before 'L2';
-alter type insenum add 'i10' before 'L2';
-alter type insenum add 'i11' before 'L2';
-alter type insenum add 'i12' before 'L2';
-alter type insenum add 'i13' before 'L2';
-alter type insenum add 'i14' before 'L2';
-alter type insenum add 'i15' before 'L2';
-alter type insenum add 'i16' before 'L2';
-alter type insenum add 'i17' before 'L2';
-alter type insenum add 'i18' before 'L2';
-alter type insenum add 'i19' before 'L2';
-alter type insenum add 'i20' before 'L2';
-alter type insenum add 'i21' before 'L2';
-alter type insenum add 'i22' before 'L2';
-alter type insenum add 'i23' before 'L2';
-alter type insenum add 'i24' before 'L2';
-alter type insenum add 'i25' before 'L2';
-alter type insenum add 'i26' before 'L2';
-alter type insenum add 'i27' before 'L2';
-alter type insenum add 'i28' before 'L2';
-alter type insenum add 'i29' before 'L2';
-alter type insenum add 'i30' before 'L2';
+alter type insenum add element 'i1' before 'L2';
+alter type insenum add element 'i2' before 'L2';
+alter type insenum add element 'i3' before 'L2';
+alter type insenum add element 'i4' before 'L2';
+alter type insenum add element 'i5' before 'L2';
+alter type insenum add element 'i6' before 'L2';
+alter type insenum add element 'i7' before 'L2';
+alter type insenum add element 'i8' before 'L2';
+alter type insenum add element 'i9' before 'L2';
+alter type insenum add element 'i10' before 'L2';
+alter type insenum add element 'i11' before 'L2';
+alter type insenum add element 'i12' before 'L2';
+alter type insenum add element 'i13' before 'L2';
+alter type insenum add element 'i14' before 'L2';
+alter type insenum add element 'i15' before 'L2';
+alter type insenum add element 'i16' before 'L2';
+alter type insenum add element 'i17' before 'L2';
+alter type insenum add element 'i18' before 'L2';
+alter type insenum add element 'i19' before 'L2';
+alter type insenum add element 'i20' before 'L2';
+alter type insenum add element 'i21' before 'L2';
+alter type insenum add element 'i22' before 'L2';
+alter type insenum add element 'i23' before 'L2';
+alter type insenum add element 'i24' before 'L2';
+alter type insenum add element 'i25' before 'L2';
+alter type insenum add element 'i26' before 'L2';
+alter type insenum add element 'i27' before 'L2';
+alter type insenum add element 'i28' before 'L2';
+alter type insenum add element 'i29' before 'L2';
+alter type insenum add element 'i30' before 'L2';
 -- The exact values of enumsortorder will now depend on the local properties
 -- of float4, but in any reasonable implementation we should get at least
 -- 20 splits before having to renumber; so only hide values > 20.
diff --git a/src/test/regress/sql/enum.sql b/src/test/regress/sql/enum.sql
index 70bf8c5..2d24038 100644
--- a/src/test/regress/sql/enum.sql
+++ b/src/test/regress/sql/enum.sql
@@ -26,17 +26,17 @@ FROM pg_enum
 WHERE enumtypid = 'planets'::regtype
 ORDER BY 2;
 
-ALTER TYPE planets ADD 'uranus';
+ALTER TYPE planets ADD ELEMENT 'uranus';
 
 SELECT enumlabel, enumsortorder
 FROM pg_enum
 WHERE enumtypid = 'planets'::regtype
 ORDER BY 2;
 
-ALTER TYPE planets ADD 'mercury' BEFORE 'venus';
-ALTER TYPE planets ADD 'saturn' BEFORE 'uranus';
-ALTER TYPE planets ADD 'jupiter' AFTER 'mars';
-ALTER TYPE planets ADD 'neptune' AFTER 'uranus';
+ALTER TYPE planets ADD ELEMENT 'mercury' BEFORE 'venus';
+ALTER TYPE planets ADD ELEMENT 'saturn' BEFORE 'uranus';
+ALTER TYPE planets ADD ELEMENT 'jupiter' AFTER 'mars';
+ALTER TYPE planets ADD ELEMENT 'neptune' AFTER 'uranus';
 
 SELECT enumlabel, enumsortorder
 FROM pg_enum
@@ -49,10 +49,10 @@ WHERE enumtypid = 'planets'::regtype
 ORDER BY enumlabel::planets;
 
 -- errors for adding labels
-ALTER TYPE planets ADD
+ALTER TYPE planets ADD ELEMENT
   'plutoplutoplutoplutoplutoplutoplutoplutoplutoplutoplutoplutoplutopluto';
 
-ALTER TYPE planets ADD 'pluto' AFTER 'zeus';
+ALTER TYPE planets ADD ELEMENT 'pluto' AFTER 'zeus';
 
 --
 -- Test inserting so many values that we have to renumber
@@ -60,36 +60,36 @@ ALTER TYPE planets ADD 'pluto' AFTER 'zeus';
 
 create type insenum as enum ('L1', 'L2');
 
-alter type insenum add 'i1' before 'L2';
-alter type insenum add 'i2' before 'L2';
-alter type insenum add 'i3' before 'L2';
-alter type insenum add 'i4' before 'L2';
-alter type insenum add 'i5' before 'L2';
-alter type insenum add 'i6' before 'L2';
-alter type insenum add 'i7' before 'L2';
-alter type insenum add 'i8' before 'L2';
-alter type insenum add 'i9' before 'L2';
-alter type insenum add 'i10' before 'L2';
-alter type insenum add 'i11' before 'L2';
-alter type insenum add 'i12' before 'L2';
-alter type insenum add 'i13' before 'L2';
-alter type insenum add 'i14' before 'L2';
-alter type insenum add 'i15' before 'L2';
-alter type insenum add 'i16' before 'L2';
-alter type insenum add 'i17' before 'L2';
-alter type insenum add 'i18' before 'L2';
-alter type insenum add 'i19' before 'L2';
-alter type insenum add 'i20' before 'L2';
-alter type insenum add 'i21' before 'L2';
-alter type insenum add 'i22' before 'L2';
-alter type insenum add 'i23' before 'L2';
-alter type insenum add 'i24' before 'L2';
-alter type insenum add 'i25' before 'L2';
-alter type insenum add 'i26' before 'L2';
-alter type insenum add 'i27' before 'L2';
-alter type insenum add 'i28' before 'L2';
-alter type insenum add 'i29' before 'L2';
-alter type insenum add 'i30' before 'L2';
+alter type insenum add element 'i1' before 'L2';
+alter type insenum add element 'i2' before 'L2';
+alter type insenum add element 'i3' before 'L2';
+alter type insenum add element 'i4' before 'L2';
+alter type insenum add element 'i5' before 'L2';
+alter type insenum add element 'i6' before 'L2';
+alter type insenum add element 'i7' before 'L2';
+alter type insenum add element 'i8' before 'L2';
+alter type insenum add element 'i9' before 'L2';
+alter type insenum add element 'i10' before 'L2';
+alter type insenum add element 'i11' before 'L2';
+alter type insenum add element 'i12' before 'L2';
+alter type insenum add element 'i13' before 'L2';
+alter type insenum add element 'i14' before 'L2';
+alter type insenum add element 'i15' before 'L2';
+alter type insenum add element 'i16' before 'L2';
+alter type insenum add element 'i17' before 'L2';
+alter type insenum add element 'i18' before 'L2';
+alter type insenum add element 'i19' before 'L2';
+alter type insenum add element 'i20' before 'L2';
+alter type insenum add element 'i21' before 'L2';
+alter type insenum add element 'i22' before 'L2';
+alter type insenum add element 'i23' before 'L2';
+alter type insenum add element 'i24' before 'L2';
+alter type insenum add element 'i25' before 'L2';
+alter type insenum add element 'i26' before 'L2';
+alter type insenum add element 'i27' before 'L2';
+alter type insenum add element 'i28' before 'L2';
+alter type insenum add element 'i29' before 'L2';
+alter type insenum add element 'i30' before 'L2';
 
 -- The exact values of enumsortorder will now depend on the local properties
 -- of float4, but in any reasonable implementation we should get at least
-- 
1.7.1

