11<!--
2- $Header: /cvsroot/pgsql/doc/src/sgml/typeconv.sgml,v 1.30 2003/03/25 16:15:38 petere Exp $
2+ $Header: /cvsroot/pgsql/doc/src/sgml/typeconv.sgml,v 1.31 2003/05/26 00:11:27 tgl Exp $
33-->
44
55<chapter Id="typeconv">
@@ -45,7 +45,7 @@ mixed-type expressions to be meaningful even with user-defined types.
4545<para>
4646The <productname>PostgreSQL</productname> scanner/parser decodes lexical
4747elements into only five fundamental categories: integers, floating-point numbers, strings,
48- names, and key words.Most extended types are first classified as
48+ names, and key words.Constants of most non-numeric types are first classified as
4949strings. The <acronym>SQL</acronym> language definition allows specifying type
5050names with strings, and this mechanism can be used in
5151<productname>PostgreSQL</productname> to start the parser down the correct
@@ -134,8 +134,8 @@ The system catalogs store information about which conversions, called
134134perform those conversions. Additional casts can be added by the user
135135with the <command>CREATE CAST</command> command. (This is usually
136136done in conjunction with defining new data types. The set of casts
137- between the built-in types has been carefully crafted andshould not
138- be altered.)
137+ between the built-in types has been carefully crafted andis best not
138+ altered.)
139139</para>
140140
141141<para>
@@ -144,8 +144,8 @@ at proper behavior for <acronym>SQL</acronym> standard types. There are
144144several basic <firstterm>type categories</firstterm> defined: <type>boolean</type>,
145145<type>numeric</type>, <type>string</type>, <type>bitstring</type>, <type>datetime</type>, <type>timespan</type>, <type>geometric</type>, <type>network</type>,
146146and user-defined. Each category, with the exception of user-defined, has
147- a <firstterm>preferredtype </firstterm> whichis preferentially selected
148- when there is ambiguity.
147+ one or more <firstterm>preferredtypes </firstterm> whichare preferentially
148+ selected when there is ambiguity.
149149In the user-defined category, each type is its own preferred type.
150150Ambiguous expressions (those with multiple candidate parsing solutions)
151151can therefore often be resolved when there are multiple possible built-in types, but
@@ -175,7 +175,8 @@ be converted to a user-defined type (of course, only if conversion is necessary)
175175<para>
176176User-defined types are not related. Currently, <productname>PostgreSQL</productname>
177177does not have information available to it on relationships between types, other than
178- hardcoded heuristics for built-in types and implicit relationships based on available functions.
178+ hardcoded heuristics for built-in types and implicit relationships based on
179+ available functions and casts.
179180</para>
180181</listitem>
181182
@@ -203,14 +204,15 @@ should use this new function and will no longer do the implicit conversion using
203204<title>Operators</title>
204205
205206 <para>
206- The operand types of an operator invocation are resolved following
207+ The specific operator to be used in an operator invocation is determined
208+ by following
207209 the procedure below. Note that this procedure is indirectly affected
208210 by the precedence of the involved operators. See <xref
209211 linkend="sql-precedence"> for more information.
210212 </para>
211213
212214<procedure>
213- <title>Operand Type Resolution</title>
215+ <title>Operator Type Resolution</title>
214216
215217<step performance="required">
216218<para>
@@ -271,22 +273,16 @@ candidate remains, use it; else continue to the next step.
271273<step performance="required">
272274<para>
273275Run through all candidates and keep those with the most exact matches
274- on input types. Keep all candidates if none have any exact matches.
276+ on input types. (Domain types are considered the same as their base type
277+ for this purpose.) Keep all candidates if none have any exact matches.
275278If only one candidate remains, use it; else continue to the next step.
276279</para>
277280</step>
278281<step performance="required">
279282<para>
280- Run through all candidates and keep those with the most exact or
281- binary-compatible matches on input types. Keep all candidates if none have
282- any exact or binary-compatible matches.
283- If only one candidate remains, use it; else continue to the next step.
284- </para>
285- </step>
286- <step performance="required">
287- <para>
288- Run through all candidates and keep those that accept preferred types at
289- the most positions where type conversion will be required.
283+ Run through all candidates and keep those that accept preferred types (of the
284+ input datatype's type category) at the most positions where type conversion
285+ will be required.
290286Keep all candidates if none accept preferred types.
291287If only one candidate remains, use it; else continue to the next step.
292288</para>
@@ -295,12 +291,13 @@ If only one candidate remains, use it; else continue to the next step.
295291<para>
296292If any input arguments are <type>unknown</type>, check the type
297293categories accepted at those argument positions by the remaining
298- candidates. At each position, select the <literal>string</literal> category if any
294+ candidates. At each position, select the <type>string</type> category
295+ if any
299296candidate accepts that category. (This bias towards string is appropriate
300297since an unknown-type literal does look like a string.) Otherwise, if
301298all the remaining candidates accept the same type category, select that
302299category; otherwise fail because the correct choice cannot be deduced
303- without more clues. Now discard operator
300+ without more clues. Now discard
304301candidates that do not accept the selected type category. Furthermore,
305302if any candidate accepts a preferred type at a given argument position,
306303discard candidates that accept non-preferred types for that argument.
@@ -455,12 +452,12 @@ SELECT CAST('20' AS int8) ! AS "factorial";
455452<title>Functions</title>
456453
457454 <para>
458- Theargument types of function calls are resolved according to the
459- following steps.
455+ Thespecific function to be used in a function invocation is determined
456+ according to the following steps.
460457 </para>
461458
462459<procedure>
463- <title>FunctionArgument Type Resolution</title>
460+ <title>Function Type Resolution</title>
464461
465462<step performance="required">
466463<para>
@@ -523,38 +520,33 @@ candidate remains, use it; else continue to the next step.
523520<step performance="required">
524521<para>
525522Run through all candidates and keep those with the most exact matches
526- on input types. Keep all candidates if none have any exact matches.
527- If only one candidate remains, use it; else continue to the next step.
528- </para>
529- </step>
530- <step performance="required">
531- <para>
532- Run through all candidates and keep those with the most exact or
533- binary-compatible matches on input types. Keep all candidates if none have
534- any exact or binary-compatible matches.
523+ on input types. (Domain types are considered the same as their base type
524+ for this purpose.) Keep all candidates if none have any exact matches.
535525If only one candidate remains, use it; else continue to the next step.
536526</para>
537527</step>
538528<step performance="required">
539529<para>
540- Run through all candidates and keep those that accept preferred types at
541- the most positions where type conversion will be required.
530+ Run through all candidates and keep those that accept preferred types (of the
531+ input datatype's type category) at the most positions where type conversion
532+ will be required.
542533Keep all candidates if none accept preferred types.
543534If only one candidate remains, use it; else continue to the next step.
544535</para>
545536</step>
546537<step performance="required">
547538<para>
548- If any input arguments are <type>unknown</type>, check the type categories accepted
539+ If any input arguments are <type>unknown</type>, check the type categories
540+ accepted
549541at those argument positions by the remaining candidates. At each position,
550542select the <type>string</type> category if any candidate accepts that category.
551543(This bias towards string
552544is appropriate since an unknown-type literal does look like a string.)
553545Otherwise, if all the remaining candidates accept the same type category,
554546select that category; otherwise fail because
555547the correct choice cannot be deduced without more clues.
556- Now discard candidates that do not accept the selected type category;
557- furthermore , if any candidate accepts a preferred type at a given argument
548+ Now discard candidates that do not accept the selected type category.
549+ Furthermore , if any candidate accepts a preferred type at a given argument
558550position, discard candidates that accept non-preferred types for that
559551argument.
560552</para>
@@ -571,6 +563,8 @@ then fail.
571563</procedure>
572564
573565<para>
566+ Note that the <quote>best match</> rules are identical for operator and
567+ function type resolution.
574568Some examples follow.
575569</para>
576570
@@ -649,7 +643,8 @@ SELECT substr(CAST (varchar '1234' AS text), 3);
649643<para>
650644<note>
651645<para>
652- The parser is aware that <type>text</type> and <type>varchar</type>
646+ The parser learns from the <structname>pg_cast</> catalog that
647+ <type>text</type> and <type>varchar</type>
653648are binary-compatible, meaning that one can be passed to a function that
654649accepts the other without doing any physical conversion. Therefore, no
655650explicit type conversion call is really inserted in this case.