Re: Current enums patch - Mailing list pgsql-patches

From Andrew Dunstan
Subject Re: Current enums patch
Date
Msg-id 46116120.2040500@dunslane.net
Whole thread Raw
In response to Re: Current enums patch  (Tom Dunstan <pgsql@tomd.cc>)
Responses Re: Current enums patch
List pgsql-patches
Tom Dunstan wrote:
>> Let's just throw the error instead.  (I agree that enum_in can just fail
>> with "no such label", but CREATE TYPE ought to give a specific
>> error about it.)
>
> Agreed.
>
> Andrew, you said you had a mostly-working patch already?
>
>

Working patch attached. If everyone's happy I'll apply it.

cheers

andrew

Index: src/backend/commands/typecmds.c
===================================================================
RCS file: /cvsroot/pgsql/src/backend/commands/typecmds.c,v
retrieving revision 1.101
diff -c -r1.101 typecmds.c
*** src/backend/commands/typecmds.c    2 Apr 2007 03:49:38 -0000    1.101
--- src/backend/commands/typecmds.c    2 Apr 2007 19:57:40 -0000
***************
*** 949,954 ****
--- 949,955 ----
      Oid        enumNamespace;
      Oid        enumTypeOid;
      AclResult    aclresult;
+     ListCell    *lc;

      /* Convert list of names to a name and namespace */
      enumNamespace = QualifiedNameGetCreationNamespace(stmt->typename,
***************
*** 970,975 ****
--- 971,987 ----
                   errmsg("type names must be %d characters or less",
                          NAMEDATALEN - 2)));

+     foreach (lc, stmt->vals)
+     {
+         char *lab = strVal(lfirst(lc));
+         if (strlen(lab) > (NAMEDATALEN - 1))
+             ereport(ERROR,
+                     (errcode(ERRCODE_INVALID_NAME),
+                      errmsg("invalid enum label \"%s\", must be %d characters or less",
+                             lab,
+                             NAMEDATALEN - 1)));
+     }
+
      /* Create the pg_type entry */
      enumTypeOid =
          TypeCreate(enumName,        /* type name */
Index: src/backend/utils/adt/enum.c
===================================================================
RCS file: /cvsroot/pgsql/src/backend/utils/adt/enum.c,v
retrieving revision 1.1
diff -c -r1.1 enum.c
*** src/backend/utils/adt/enum.c    2 Apr 2007 03:49:39 -0000    1.1
--- src/backend/utils/adt/enum.c    2 Apr 2007 19:57:41 -0000
***************
*** 44,49 ****
--- 44,57 ----
  {
      HeapTuple tup;
      Oid enumoid;
+     size_t namelen = strlen(name);
+
+     if (namelen >= NAMEDATALEN)
+         ereport(ERROR,
+                 (errcode(ERRCODE_INVALID_TEXT_REPRESENTATION),
+                  errmsg("invalid input value for enum %s: \"%s\"",
+                         format_type_be(enumtypoid),
+                         name)));

      tup = SearchSysCache(ENUMTYPOIDNAME,
                           ObjectIdGetDatum(enumtypoid),
Index: src/test/regress/expected/enum.out
===================================================================
RCS file: /cvsroot/pgsql/src/test/regress/expected/enum.out,v
retrieving revision 1.1
diff -c -r1.1 enum.out
*** src/test/regress/expected/enum.out    2 Apr 2007 03:49:42 -0000    1.1
--- src/test/regress/expected/enum.out    2 Apr 2007 19:57:43 -0000
***************
*** 40,45 ****
--- 40,59 ----
  (6 rows)

  --
+ -- Name, Values too long
+ --
+ CREATE TYPE
+  abcdefghijklmnopqrsatuvwxyz0123456789abcdefghijklmnopqrsatuvwxyz0123456789
+  AS ENUM('a');
+ NOTICE:  identifier "abcdefghijklmnopqrsatuvwxyz0123456789abcdefghijklmnopqrsatuvwxyz0123456789" will be truncated to
"abcdefghijklmnopqrsatuvwxyz0123456789abcdefghijklmnopqrsatuvwxy"
+ ERROR:  type names must be 62 characters or less
+ CREATE TYPE toolong AS ENUM
+ ('abcdefghijklmnopqrsatuvwxyz0123456789abcdefghijklmnopqrsatuvwxyz0123456789');
+ ERROR:  invalid enum label "abcdefghijklmnopqrsatuvwxyz0123456789abcdefghijklmnopqrsatuvwxyz0123456789", must be 63
charactersor less 
+ INSERT INTO enumtest VALUES
+ ('abcdefghijklmnopqrsatuvwxyz0123456789abcdefghijklmnopqrsatuvwxyz0123456789');
+ ERROR:  invalid input value for enum rainbow:
"abcdefghijklmnopqrsatuvwxyz0123456789abcdefghijklmnopqrsatuvwxyz0123456789"
+ --
  -- Operators, no index
  --
  SELECT * FROM enumtest WHERE col = 'orange';
Index: src/test/regress/sql/enum.sql
===================================================================
RCS file: /cvsroot/pgsql/src/test/regress/sql/enum.sql,v
retrieving revision 1.1
diff -c -r1.1 enum.sql
*** src/test/regress/sql/enum.sql    2 Apr 2007 03:49:42 -0000    1.1
--- src/test/regress/sql/enum.sql    2 Apr 2007 19:57:44 -0000
***************
*** 27,32 ****
--- 27,45 ----
  SELECT * FROM enumtest;

  --
+ -- Name, Values too long
+ --
+ CREATE TYPE
+  abcdefghijklmnopqrsatuvwxyz0123456789abcdefghijklmnopqrsatuvwxyz0123456789
+  AS ENUM('a');
+
+ CREATE TYPE toolong AS ENUM
+ ('abcdefghijklmnopqrsatuvwxyz0123456789abcdefghijklmnopqrsatuvwxyz0123456789');
+
+ INSERT INTO enumtest VALUES
+ ('abcdefghijklmnopqrsatuvwxyz0123456789abcdefghijklmnopqrsatuvwxyz0123456789');
+
+ --
  -- Operators, no index
  --
  SELECT * FROM enumtest WHERE col = 'orange';

pgsql-patches by date:

Previous
From: Bruce Momjian
Date:
Subject: Re: [Fwd: Deferred Transactions, Transaction Guarantee and COMMITwithout waiting]
Next
From: David Fetter
Date:
Subject: Re: [HACKERS] Arrays of Complex Types