From 2d4db3675fa7a2f4831b755bc98242421901042f Mon Sep 17 00:00:00 2001 From: Tom Lane Date: Wed, 6 Jun 2007 23:00:50 +0000 Subject: Fix up text concatenation so that it accepts all the reasonable cases that were accepted by prior Postgres releases. This takes care of the loose end left by the preceding patch to downgrade implicit casts-to-text. To avoid breaking desirable behavior for array concatenation, introduce a new polymorphic pseudo-type "anynonarray" --- the added concatenation operators are actually text || anynonarray and anynonarray || text. --- doc/src/sgml/datatype.sgml | 16 +++++++++++++--- doc/src/sgml/extend.sgml | 18 +++++++++++++++--- doc/src/sgml/func.sgml | 46 +++++++++++++++++++++++++++++++++++++--------- doc/src/sgml/plpgsql.sgml | 8 ++++---- doc/src/sgml/xfunc.sgml | 8 +++++--- 5 files changed, 74 insertions(+), 22 deletions(-) (limited to 'doc/src') diff --git a/doc/src/sgml/datatype.sgml b/doc/src/sgml/datatype.sgml index 3802aa24dff..c64b040e457 100644 --- a/doc/src/sgml/datatype.sgml +++ b/doc/src/sgml/datatype.sgml @@ -1,4 +1,4 @@ - + Data Types @@ -3676,12 +3676,16 @@ SELECT * FROM pg_attribute any + + anyelement + + anyarray - anyelement + anynonarray @@ -3760,6 +3764,12 @@ SELECT * FROM pg_attribute ). + + anynonarray + Indicates that a function accepts any non-array data type + (see ). + + cstring Indicates that a function accepts or returns a null-terminated C string. @@ -3813,7 +3823,7 @@ SELECT * FROM pg_attribute only void and record as a result type (plus trigger when the function is used as a trigger). Some also support polymorphic functions using the types anyarray, - anyelement and anyenum. + anyelement, anyenum, and anynonarray. diff --git a/doc/src/sgml/extend.sgml b/doc/src/sgml/extend.sgml index bb5834e74a9..28bdfb85322 100644 --- a/doc/src/sgml/extend.sgml +++ b/doc/src/sgml/extend.sgml @@ -1,4 +1,4 @@ - + Extending <acronym>SQL</acronym> @@ -193,8 +193,8 @@ - Three pseudo-types of special interest are anyelement, - anyarray, and anyenum, + Four pseudo-types of special interest are anyelement, + anyarray, anynonarray, and anyenum, which are collectively called polymorphic types. Any function declared using these types is said to be a polymorphic function. A polymorphic function can @@ -216,6 +216,9 @@ anyelement, the actual array type in the anyarray positions must be an array whose elements are the same type appearing in the anyelement positions. + anynonarray is treated exactly the same as anyelement, + but adds the additional constraint that the actual type must not be + an array type. anyenum is treated exactly the same as anyelement, but adds the additional constraint that the actual type must be an enum type. @@ -242,6 +245,15 @@ is that a function declared as f(anyarray) returns anyenum will only accept arrays of enum types. + + + Note that anynonarray and anyenum do not represent + separate type variables; they are the same type as + anyelement, just with an additional constraint. For + example, declaring a function as f(anyelement, anyenum) + is equivalent to declaring it as f(anyenum, anyenum): + both actual arguments have to be the same enum type. + diff --git a/doc/src/sgml/func.sgml b/doc/src/sgml/func.sgml index c2f3371dc48..387c4e81c8f 100644 --- a/doc/src/sgml/func.sgml +++ b/doc/src/sgml/func.sgml @@ -1,4 +1,4 @@ - + Functions and Operators @@ -986,24 +986,36 @@ This section describes functions and operators for examining and manipulating string values. Strings in this context include values - of all the types character, character - varying, and text. Unless otherwise noted, all + of the types character, character varying, + and text. Unless otherwise noted, all of the functions listed below work on all of these types, but be - wary of potential effects of the automatic padding when using the - character type. Generally, the functions described - here also work on data of non-string types by converting that data - to a string representation first. Some functions also exist + wary of potential effects of automatic space-padding when using the + character type. Some functions also exist natively for the bit-string types. - SQL defines some string functions with a special syntax where - certain key words rather than commas are used to separate the + SQL defines some string functions with a special syntax + wherein certain key words rather than commas are used to separate the arguments. Details are in . These functions are also implemented using the regular syntax for function invocation. (See .) + + + Before PostgreSQL 8.3, these functions would + silently accept values of several non-string data types as well, due to + the presence of implicit coercions from those data types to + text. Those coercions have been removed because they frequently + caused surprising behaviors. However, the string concatenation operator + (||) still accepts non-string input, so long as at least one + input is of a string type, as shown in . For other cases, insert an explicit + coercion to text if you need to duplicate the previous behavior. + + + bit_length @@ -1064,6 +1076,22 @@ PostgreSQL + + + string || + non-string + or + non-string || + string + + text + + String concatenation with one non-string input + + 'Value: ' || 42 + Value: 42 + + bit_length(string) int diff --git a/doc/src/sgml/plpgsql.sgml b/doc/src/sgml/plpgsql.sgml index c1f57ddf4f8..d14519b6133 100644 --- a/doc/src/sgml/plpgsql.sgml +++ b/doc/src/sgml/plpgsql.sgml @@ -1,4 +1,4 @@ - + <application>PL/pgSQL</application> - <acronym>SQL</acronym> Procedural Language @@ -210,8 +210,8 @@ $$ LANGUAGE plpgsql; PL/pgSQL functions can also be declared to accept and return the polymorphic types - anyelement, anyarray, and anyenum. - The actual + anyelement, anyarray, anynonarray, + and anyenum. The actual data types handled by a polymorphic function can vary from call to call, as discussed in . An example is shown in . @@ -700,7 +700,7 @@ $$ LANGUAGE plpgsql; When the return type of a PL/pgSQL function is declared as a polymorphic type (anyelement, - anyarray, or anyenum), + anyarray, anynonarray, or anyenum), a special parameter $0 is created. Its data type is the actual return type of the function, as deduced from the actual input types (see + User-Defined Functions @@ -718,7 +718,8 @@ SELECT name, listchildren(name) FROM nodes; SQL functions can be declared to accept and return the polymorphic types anyelement, - anyarray, and anyenum. See anyarray, anynonarray, and + anyenum. See for a more detailed explanation of polymorphic functions. Here is a polymorphic function make_array that builds up an array @@ -2831,7 +2832,8 @@ CREATE OR REPLACE FUNCTION retcomposite(IN integer, IN integer, C-language functions can be declared to accept and return the polymorphic types - anyelement, anyarray, and anyenum. + anyelement, anyarray, anynonarray, + and anyenum. See for a more detailed explanation of polymorphic functions. When function arguments or return types are defined as polymorphic types, the function author cannot know -- cgit v1.2.3