1998-07-29 08:50:04 +02:00
|
|
|
<chapter Id="typeconv">
|
1998-07-08 15:53:15 +02:00
|
|
|
<title>Type Conversion</title>
|
|
|
|
|
2001-09-15 02:48:59 +02:00
|
|
|
<sect1 id="typeconv-intro">
|
|
|
|
<title>Introduction</title>
|
|
|
|
|
1998-07-08 15:53:15 +02:00
|
|
|
<para>
|
|
|
|
<acronym>SQL</acronym> queries can, intentionally or not, require
|
|
|
|
mixing of different data types in the same expression.
|
2001-09-15 02:48:59 +02:00
|
|
|
<productname>PostgreSQL</productname> has extensive facilities for
|
1998-07-08 15:53:15 +02:00
|
|
|
evaluating mixed-type expressions.
|
1998-12-29 03:24:47 +01:00
|
|
|
</para>
|
1998-07-08 15:53:15 +02:00
|
|
|
|
|
|
|
<para>
|
|
|
|
In many cases a user will not need
|
|
|
|
to understand the details of the type conversion mechanism.
|
2001-09-15 02:48:59 +02:00
|
|
|
However, the implicit conversions done by <productname>PostgreSQL</productname>
|
2000-12-17 06:55:26 +01:00
|
|
|
can affect the results of a query. When necessary, these results
|
1998-07-08 15:53:15 +02:00
|
|
|
can be tailored by a user or programmer
|
2000-03-20 05:22:11 +01:00
|
|
|
using <emphasis>explicit</emphasis> type coercion.
|
1998-12-29 03:24:47 +01:00
|
|
|
</para>
|
1998-07-08 15:53:15 +02:00
|
|
|
|
|
|
|
<para>
|
2001-09-15 02:48:59 +02:00
|
|
|
This chapter introduces the <productname>PostgreSQL</productname>
|
2000-12-17 06:55:26 +01:00
|
|
|
type conversion mechanisms and conventions.
|
2001-09-15 02:48:59 +02:00
|
|
|
Refer to the relevant sections in the <xref linkend="datatype"> and <xref linkend="functions">
|
2000-12-17 06:55:26 +01:00
|
|
|
for more information on specific data types and allowed functions and
|
|
|
|
operators.
|
1998-12-29 03:24:47 +01:00
|
|
|
</para>
|
1998-07-08 15:53:15 +02:00
|
|
|
|
|
|
|
<para>
|
2001-09-15 02:48:59 +02:00
|
|
|
The <citetitle>Programmer's Guide</citetitle> has more details on the exact algorithms used for
|
2000-03-20 05:22:11 +01:00
|
|
|
implicit type conversion and coercion.
|
1998-12-29 03:24:47 +01:00
|
|
|
</para>
|
2001-09-15 02:48:59 +02:00
|
|
|
</sect1>
|
1998-07-08 15:53:15 +02:00
|
|
|
|
2000-09-29 22:21:34 +02:00
|
|
|
<sect1 id="typeconv-overview">
|
1998-07-08 15:53:15 +02:00
|
|
|
<title>Overview</title>
|
|
|
|
|
|
|
|
<para>
|
|
|
|
<acronym>SQL</acronym> is a strongly typed language. That is, every data item
|
|
|
|
has an associated data type which determines its behavior and allowed usage.
|
2001-09-15 02:48:59 +02:00
|
|
|
<productname>PostgreSQL</productname> has an extensible type system that is
|
1998-07-08 15:53:15 +02:00
|
|
|
much more general and flexible than other <acronym>RDBMS</acronym> implementations.
|
2001-09-15 02:48:59 +02:00
|
|
|
Hence, most type conversion behavior in <productname>PostgreSQL</productname>
|
1998-07-08 15:53:15 +02:00
|
|
|
should be governed by general rules rather than by ad-hoc heuristics to allow
|
|
|
|
mixed-type expressions to be meaningful, even with user-defined types.
|
1998-12-29 03:24:47 +01:00
|
|
|
</para>
|
1998-07-08 15:53:15 +02:00
|
|
|
|
|
|
|
<para>
|
2001-09-15 02:48:59 +02:00
|
|
|
The <productname>PostgreSQL</productname> scanner/parser decodes lexical
|
2000-12-17 06:55:26 +01:00
|
|
|
elements into only five fundamental categories: integers, floats, strings,
|
|
|
|
names, and keywords. Most extended types are first tokenized into
|
|
|
|
strings. The <acronym>SQL</acronym> language definition allows specifying type
|
|
|
|
names with strings, and this mechanism can be used in
|
2001-09-15 02:48:59 +02:00
|
|
|
<productname>PostgreSQL</productname> to start the parser down the correct
|
2000-12-17 06:55:26 +01:00
|
|
|
path. For example, the query
|
1998-07-08 15:53:15 +02:00
|
|
|
|
2001-09-15 02:48:59 +02:00
|
|
|
<screen>
|
1998-07-08 15:53:15 +02:00
|
|
|
tgl=> SELECT text 'Origin' AS "Label", point '(0,0)' AS "Value";
|
2000-03-26 20:32:30 +02:00
|
|
|
Label | Value
|
|
|
|
--------+-------
|
|
|
|
Origin | (0,0)
|
1998-07-08 15:53:15 +02:00
|
|
|
(1 row)
|
2001-09-15 02:48:59 +02:00
|
|
|
</screen>
|
1998-07-08 15:53:15 +02:00
|
|
|
|
|
|
|
has two strings, of type <type>text</type> and <type>point</type>.
|
2000-12-17 06:55:26 +01:00
|
|
|
If a type is not specified for a string, then the placeholder type
|
|
|
|
<firstterm>unknown</firstterm> is assigned initially, to be resolved in later
|
|
|
|
stages as described below.
|
1998-12-29 03:24:47 +01:00
|
|
|
</para>
|
1998-07-08 15:53:15 +02:00
|
|
|
|
|
|
|
<para>
|
|
|
|
There are four fundamental <acronym>SQL</acronym> constructs requiring
|
2001-09-15 02:48:59 +02:00
|
|
|
distinct type conversion rules in the <productname>PostgreSQL</productname>
|
1998-07-08 15:53:15 +02:00
|
|
|
parser:
|
1998-12-29 03:24:47 +01:00
|
|
|
</para>
|
1998-07-08 15:53:15 +02:00
|
|
|
|
|
|
|
<variablelist>
|
|
|
|
<varlistentry>
|
|
|
|
<term>
|
|
|
|
Operators
|
|
|
|
</term>
|
|
|
|
<listitem>
|
|
|
|
<para>
|
2001-09-15 02:48:59 +02:00
|
|
|
<productname>PostgreSQL</productname> allows expressions with
|
|
|
|
prefix and postfix unary (one argument) operators,
|
1998-07-08 15:53:15 +02:00
|
|
|
as well as binary (two argument) operators.
|
1998-12-29 03:24:47 +01:00
|
|
|
</para>
|
1998-07-08 15:53:15 +02:00
|
|
|
</listitem>
|
|
|
|
</varlistentry>
|
|
|
|
<varlistentry>
|
|
|
|
<term>
|
|
|
|
Function calls
|
|
|
|
</term>
|
|
|
|
<listitem>
|
|
|
|
<para>
|
2001-09-15 02:48:59 +02:00
|
|
|
Much of the <productname>PostgreSQL</productname> type system is built around a
|
2000-12-17 06:55:26 +01:00
|
|
|
rich set of functions. Function calls have one or more arguments which, for
|
|
|
|
any specific query, must be matched to the functions available in the system
|
2001-09-15 02:48:59 +02:00
|
|
|
catalog. Since <productname>PostgreSQL</productname> permits function
|
2000-12-17 06:55:26 +01:00
|
|
|
overloading, the function name alone does not uniquely identify the function
|
2001-09-15 02:48:59 +02:00
|
|
|
to be called; the parser must select the right function based on the data
|
2000-12-17 06:55:26 +01:00
|
|
|
types of the supplied arguments.
|
1998-12-29 03:24:47 +01:00
|
|
|
</para>
|
1998-07-08 15:53:15 +02:00
|
|
|
</listitem>
|
|
|
|
</varlistentry>
|
|
|
|
<varlistentry>
|
|
|
|
<term>
|
|
|
|
Query targets
|
|
|
|
</term>
|
|
|
|
<listitem>
|
|
|
|
<para>
|
2001-09-15 02:48:59 +02:00
|
|
|
<acronym>SQL</acronym> <command>INSERT</command> and <command>UPDATE</command> statements place the results of
|
2000-12-17 06:55:26 +01:00
|
|
|
expressions into a table. The expressions in the query must be matched up
|
|
|
|
with, and perhaps converted to, the types of the target columns.
|
1998-12-29 03:24:47 +01:00
|
|
|
</para>
|
1998-07-08 15:53:15 +02:00
|
|
|
</listitem>
|
|
|
|
</varlistentry>
|
|
|
|
<varlistentry>
|
|
|
|
<term>
|
2001-09-15 02:48:59 +02:00
|
|
|
<literal>UNION</literal> and <literal>CASE</literal> constructs
|
1998-07-08 15:53:15 +02:00
|
|
|
</term>
|
|
|
|
<listitem>
|
|
|
|
<para>
|
2001-09-15 02:48:59 +02:00
|
|
|
Since all select results from a unionized <literal>SELECT</literal> statement must appear in a single
|
2000-12-17 06:55:26 +01:00
|
|
|
set of columns, the types of the results
|
2001-09-15 02:48:59 +02:00
|
|
|
of each <literal>SELECT</> clause must be matched up and converted to a uniform set.
|
|
|
|
Similarly, the result expressions of a <literal>CASE</> construct must be coerced to
|
|
|
|
a common type so that the <literal>CASE</> expression as a whole has a known output type.
|
1998-12-29 03:24:47 +01:00
|
|
|
</para>
|
1998-07-08 15:53:15 +02:00
|
|
|
</listitem>
|
|
|
|
</varlistentry>
|
|
|
|
</variablelist>
|
|
|
|
|
|
|
|
<para>
|
|
|
|
Many of the general type conversion rules use simple conventions built on
|
2001-09-15 02:48:59 +02:00
|
|
|
the <productname>PostgreSQL</productname> function and operator system tables.
|
1998-07-08 15:53:15 +02:00
|
|
|
There are some heuristics included in the conversion rules to better support
|
2001-09-15 02:48:59 +02:00
|
|
|
conventions for the <acronym>SQL</acronym> standard native types such as
|
|
|
|
<type>smallint</type>, <type>integer</type>, and <type>real</type>.
|
1998-12-29 03:24:47 +01:00
|
|
|
</para>
|
1998-07-08 15:53:15 +02:00
|
|
|
|
|
|
|
<para>
|
2001-09-15 02:48:59 +02:00
|
|
|
The <productname>PostgreSQL</productname> parser uses the convention that all
|
1998-07-08 15:53:15 +02:00
|
|
|
type conversion functions take a single argument of the source type and are
|
2000-12-17 06:55:26 +01:00
|
|
|
named with the same name as the target type. Any function meeting these
|
1998-07-08 15:53:15 +02:00
|
|
|
criteria is considered to be a valid conversion function, and may be used
|
|
|
|
by the parser as such. This simple assumption gives the parser the power
|
|
|
|
to explore type conversion possibilities without hardcoding, allowing
|
|
|
|
extended user-defined types to use these same features transparently.
|
1998-12-29 03:24:47 +01:00
|
|
|
</para>
|
1998-07-08 15:53:15 +02:00
|
|
|
|
|
|
|
<para>
|
|
|
|
An additional heuristic is provided in the parser to allow better guesses
|
|
|
|
at proper behavior for <acronym>SQL</acronym> standard types. There are
|
2001-09-09 19:21:59 +02:00
|
|
|
several basic <firstterm>type categories</firstterm> defined: <type>boolean</type>,
|
|
|
|
<type>numeric</type>, <type>string</type>, <type>bitstring</type>, <type>datetime</type>, <type>timespan</type>, <type>geometric</type>, <type>network</type>,
|
1998-07-08 15:53:15 +02:00
|
|
|
and user-defined. Each category, with the exception of user-defined, has
|
2000-12-17 06:55:26 +01:00
|
|
|
a <firstterm>preferred type</firstterm> which is preferentially selected
|
|
|
|
when there is ambiguity.
|
|
|
|
In the user-defined category, each type is its own preferred type.
|
|
|
|
Ambiguous expressions (those with multiple candidate parsing solutions)
|
|
|
|
can often be resolved when there are multiple possible built-in types, but
|
|
|
|
they will raise an error when there are multiple choices for user-defined
|
|
|
|
types.
|
1998-12-29 03:24:47 +01:00
|
|
|
</para>
|
1998-07-08 15:53:15 +02:00
|
|
|
|
|
|
|
<para>
|
|
|
|
All type conversion rules are designed with several principles in mind:
|
|
|
|
|
|
|
|
<itemizedlist mark="bullet" spacing="compact">
|
|
|
|
<listitem>
|
|
|
|
<para>
|
2000-03-20 05:22:11 +01:00
|
|
|
Implicit conversions should never have surprising or unpredictable outcomes.
|
1998-12-29 03:24:47 +01:00
|
|
|
</para>
|
|
|
|
</listitem>
|
1998-07-08 15:53:15 +02:00
|
|
|
|
|
|
|
<listitem>
|
|
|
|
<para>
|
2000-03-20 05:22:11 +01:00
|
|
|
User-defined types, of which the parser has no a-priori knowledge, should be
|
2001-09-13 17:55:24 +02:00
|
|
|
<quote>higher</quote> in the type hierarchy. In mixed-type expressions, native types shall always
|
1998-07-08 15:53:15 +02:00
|
|
|
be converted to a user-defined type (of course, only if conversion is necessary).
|
1998-12-29 03:24:47 +01:00
|
|
|
</para>
|
|
|
|
</listitem>
|
1998-07-08 15:53:15 +02:00
|
|
|
|
|
|
|
<listitem>
|
|
|
|
<para>
|
2001-09-15 02:48:59 +02:00
|
|
|
User-defined types are not related. Currently, <productname>PostgreSQL</productname>
|
1998-07-08 15:53:15 +02:00
|
|
|
does not have information available to it on relationships between types, other than
|
|
|
|
hardcoded heuristics for built-in types and implicit relationships based on available functions
|
|
|
|
in the catalog.
|
1998-12-29 03:24:47 +01:00
|
|
|
</para>
|
|
|
|
</listitem>
|
1998-07-08 15:53:15 +02:00
|
|
|
|
|
|
|
<listitem>
|
|
|
|
<para>
|
|
|
|
There should be no extra overhead from the parser or executor
|
|
|
|
if a query does not need implicit type conversion.
|
|
|
|
That is, if a query is well formulated and the types already match up, then the query should proceed
|
|
|
|
without spending extra time in the parser and without introducing unnecessary implicit conversion
|
|
|
|
functions into the query.
|
1998-12-29 03:24:47 +01:00
|
|
|
</para>
|
1998-07-08 15:53:15 +02:00
|
|
|
|
|
|
|
<para>
|
|
|
|
Additionally, if a query usually requires an implicit conversion for a function, and
|
|
|
|
if then the user defines an explicit function with the correct argument types, the parser
|
|
|
|
should use this new function and will no longer do the implicit conversion using the old function.
|
1998-12-29 03:24:47 +01:00
|
|
|
</para>
|
|
|
|
</listitem>
|
1998-07-08 15:53:15 +02:00
|
|
|
</itemizedlist>
|
1998-12-29 03:24:47 +01:00
|
|
|
</para>
|
2001-09-15 02:48:59 +02:00
|
|
|
|
1998-12-29 03:24:47 +01:00
|
|
|
</sect1>
|
1998-07-08 15:53:15 +02:00
|
|
|
|
2000-09-29 22:21:34 +02:00
|
|
|
<sect1 id="typeconv-oper">
|
1998-07-08 15:53:15 +02:00
|
|
|
<title>Operators</title>
|
|
|
|
|
2001-09-15 02:48:59 +02:00
|
|
|
<para>
|
|
|
|
The operand types of an operator invocation are resolved following
|
|
|
|
to the procedure below. Note that this procedure is indirectly affected
|
|
|
|
by the precedence of the involved operators. See <xref
|
|
|
|
linkend="sql-precedence"> for more information.
|
|
|
|
</para>
|
|
|
|
|
1998-07-08 15:53:15 +02:00
|
|
|
<procedure>
|
2001-09-15 02:48:59 +02:00
|
|
|
<title>Operand Type Resolution</title>
|
1998-07-08 15:53:15 +02:00
|
|
|
|
|
|
|
<step performance="required">
|
|
|
|
<para>
|
2001-09-15 02:48:59 +02:00
|
|
|
Check for an exact match in the <classname>pg_operator</classname> system catalog.
|
1998-12-29 03:24:47 +01:00
|
|
|
</para>
|
1998-07-08 15:53:15 +02:00
|
|
|
|
|
|
|
<substeps>
|
|
|
|
<step performance="optional">
|
|
|
|
<para>
|
2000-12-17 06:55:26 +01:00
|
|
|
If one argument of a binary operator is <type>unknown</type> type,
|
|
|
|
then assume it is the same type as the other argument for this check.
|
|
|
|
Other cases involving <type>unknown</type> will never find a match at
|
|
|
|
this step.
|
1998-12-29 03:24:47 +01:00
|
|
|
</para>
|
|
|
|
</step>
|
1998-07-08 15:53:15 +02:00
|
|
|
</substeps>
|
1998-12-29 03:24:47 +01:00
|
|
|
</step>
|
1998-07-08 15:53:15 +02:00
|
|
|
|
|
|
|
<step performance="required">
|
|
|
|
<para>
|
|
|
|
Look for the best match.
|
1998-12-29 03:24:47 +01:00
|
|
|
</para>
|
1998-07-08 15:53:15 +02:00
|
|
|
<substeps>
|
2000-12-17 06:55:26 +01:00
|
|
|
<step performance="required">
|
1998-07-08 15:53:15 +02:00
|
|
|
<para>
|
2000-12-17 06:55:26 +01:00
|
|
|
Make a list of all operators of the same name for which the input types
|
|
|
|
match or can be coerced to match. (<type>unknown</type> literals are
|
|
|
|
assumed to be coercible to anything for this purpose.) If there is only
|
|
|
|
one, use it; else continue to the next step.
|
1998-12-29 03:24:47 +01:00
|
|
|
</para>
|
|
|
|
</step>
|
1998-07-08 15:53:15 +02:00
|
|
|
<step performance="required">
|
|
|
|
<para>
|
2000-12-17 06:55:26 +01:00
|
|
|
Run through all candidates and keep those with the most exact matches
|
|
|
|
on input types. Keep all candidates if none have any exact matches.
|
|
|
|
If only one candidate remains, use it; else continue to the next step.
|
|
|
|
</para>
|
2000-12-17 18:50:46 +01:00
|
|
|
</step>
|
2000-12-17 06:55:26 +01:00
|
|
|
<step performance="required">
|
|
|
|
<para>
|
|
|
|
Run through all candidates and keep those with the most exact or
|
|
|
|
binary-compatible matches on input types. Keep all candidates if none have
|
|
|
|
any exact or binary-compatible matches.
|
|
|
|
If only one candidate remains, use it; else continue to the next step.
|
1998-12-29 03:24:47 +01:00
|
|
|
</para>
|
|
|
|
</step>
|
1998-07-08 15:53:15 +02:00
|
|
|
<step performance="required">
|
|
|
|
<para>
|
2000-12-22 19:57:50 +01:00
|
|
|
Run through all candidates and keep those that accept preferred types at
|
2000-12-17 06:55:26 +01:00
|
|
|
the most positions where type coercion will be required.
|
|
|
|
Keep all candidates if none accept preferred types.
|
|
|
|
If only one candidate remains, use it; else continue to the next step.
|
1998-12-29 03:24:47 +01:00
|
|
|
</para>
|
|
|
|
</step>
|
1998-07-08 15:53:15 +02:00
|
|
|
<step performance="required">
|
|
|
|
<para>
|
2001-09-09 19:21:59 +02:00
|
|
|
If any input arguments are <quote>unknown</quote>, check the type categories accepted
|
2000-12-17 06:55:26 +01:00
|
|
|
at those argument positions by the remaining candidates. At each position,
|
|
|
|
select "string"
|
|
|
|
category if any candidate accepts that category (this bias towards string
|
|
|
|
is appropriate since an unknown-type literal does look like a string).
|
|
|
|
Otherwise, if all the remaining candidates accept the same type category,
|
2000-12-19 01:54:59 +01:00
|
|
|
select that category; otherwise fail because
|
2000-12-17 06:55:26 +01:00
|
|
|
the correct choice cannot be deduced without more clues. Also note whether
|
2001-09-09 19:21:59 +02:00
|
|
|
any of the candidates accept a preferred data type within the selected category.
|
2000-12-17 06:55:26 +01:00
|
|
|
Now discard operator candidates that do not accept the selected type category;
|
|
|
|
furthermore, if any candidate accepts a preferred type at a given argument
|
|
|
|
position, discard candidates that accept non-preferred types for that
|
|
|
|
argument.
|
1998-12-29 03:24:47 +01:00
|
|
|
</para>
|
|
|
|
</step>
|
1998-07-08 15:53:15 +02:00
|
|
|
<step performance="required">
|
|
|
|
<para>
|
2000-12-17 06:55:26 +01:00
|
|
|
If only one candidate remains, use it. If no candidate or more than one
|
|
|
|
candidate remains,
|
2000-12-19 01:54:59 +01:00
|
|
|
then fail.
|
1998-12-29 03:24:47 +01:00
|
|
|
</para>
|
|
|
|
</step>
|
1998-07-08 15:53:15 +02:00
|
|
|
</substeps>
|
1998-12-29 03:24:47 +01:00
|
|
|
</step>
|
1998-07-08 15:53:15 +02:00
|
|
|
</procedure>
|
|
|
|
|
2001-09-15 02:48:59 +02:00
|
|
|
<bridgehead renderas="sect2">Examples</bridgehead>
|
1998-07-08 15:53:15 +02:00
|
|
|
|
2001-09-15 02:48:59 +02:00
|
|
|
<example>
|
|
|
|
<title>Exponentiation Operator Type Resolution</title>
|
1998-07-08 15:53:15 +02:00
|
|
|
|
|
|
|
<para>
|
|
|
|
There is only one exponentiation
|
2001-01-20 21:59:29 +01:00
|
|
|
operator defined in the catalog, and it takes arguments of type
|
|
|
|
<type>double precision</type>.
|
2001-09-15 02:48:59 +02:00
|
|
|
The scanner assigns an initial type of <type>integer</type> to both arguments
|
1998-07-08 15:53:15 +02:00
|
|
|
of this query expression:
|
2001-09-15 02:48:59 +02:00
|
|
|
<screen>
|
1998-07-08 15:53:15 +02:00
|
|
|
tgl=> select 2 ^ 3 AS "Exp";
|
2000-03-26 20:32:30 +02:00
|
|
|
Exp
|
|
|
|
-----
|
|
|
|
8
|
1998-07-08 15:53:15 +02:00
|
|
|
(1 row)
|
2001-09-15 02:48:59 +02:00
|
|
|
</screen>
|
1998-07-08 15:53:15 +02:00
|
|
|
|
|
|
|
So the parser does a type conversion on both operands and the query
|
|
|
|
is equivalent to
|
|
|
|
|
2001-09-15 02:48:59 +02:00
|
|
|
<screen>
|
2001-01-20 21:59:29 +01:00
|
|
|
tgl=> select CAST(2 AS double precision) ^ CAST(3 AS double precision) AS "Exp";
|
2000-03-26 20:32:30 +02:00
|
|
|
Exp
|
|
|
|
-----
|
|
|
|
8
|
1998-07-08 15:53:15 +02:00
|
|
|
(1 row)
|
2001-09-15 02:48:59 +02:00
|
|
|
</screen>
|
1998-07-08 15:53:15 +02:00
|
|
|
|
|
|
|
or
|
|
|
|
|
2001-09-15 02:48:59 +02:00
|
|
|
<screen>
|
1998-07-08 15:53:15 +02:00
|
|
|
tgl=> select 2.0 ^ 3.0 AS "Exp";
|
2000-03-26 20:32:30 +02:00
|
|
|
Exp
|
|
|
|
-----
|
|
|
|
8
|
1998-07-08 15:53:15 +02:00
|
|
|
(1 row)
|
2001-09-15 02:48:59 +02:00
|
|
|
</screen>
|
1998-07-08 15:53:15 +02:00
|
|
|
|
|
|
|
<note>
|
|
|
|
<para>
|
|
|
|
This last form has the least overhead, since no functions are called to do
|
|
|
|
implicit type conversion. This is not an issue for small queries, but may
|
|
|
|
have an impact on the performance of queries involving large tables.
|
1998-12-29 03:24:47 +01:00
|
|
|
</para>
|
1998-07-08 15:53:15 +02:00
|
|
|
</note>
|
1998-12-29 03:24:47 +01:00
|
|
|
</para>
|
2001-09-15 02:48:59 +02:00
|
|
|
</example>
|
1998-07-08 15:53:15 +02:00
|
|
|
|
2001-09-15 02:48:59 +02:00
|
|
|
<example>
|
|
|
|
<title>String Concatenation Operator Type Resolution</title>
|
1998-07-08 15:53:15 +02:00
|
|
|
|
|
|
|
<para>
|
|
|
|
A string-like syntax is used for working with string types as well as for
|
|
|
|
working with complex extended types.
|
|
|
|
Strings with unspecified type are matched with likely operator candidates.
|
1998-12-29 03:24:47 +01:00
|
|
|
</para>
|
1998-07-08 15:53:15 +02:00
|
|
|
|
|
|
|
<para>
|
|
|
|
One unspecified argument:
|
2001-09-15 02:48:59 +02:00
|
|
|
<screen>
|
1998-07-08 15:53:15 +02:00
|
|
|
tgl=> SELECT text 'abc' || 'def' AS "Text and Unknown";
|
2000-03-26 20:32:30 +02:00
|
|
|
Text and Unknown
|
|
|
|
------------------
|
|
|
|
abcdef
|
1998-07-08 15:53:15 +02:00
|
|
|
(1 row)
|
2001-09-15 02:48:59 +02:00
|
|
|
</screen>
|
1998-12-29 03:24:47 +01:00
|
|
|
</para>
|
1998-07-08 15:53:15 +02:00
|
|
|
|
|
|
|
<para>
|
|
|
|
In this case the parser looks to see if there is an operator taking <type>text</type>
|
|
|
|
for both arguments. Since there is, it assumes that the second argument should
|
|
|
|
be interpreted as of type <type>text</type>.
|
1998-12-29 03:24:47 +01:00
|
|
|
</para>
|
1998-07-08 15:53:15 +02:00
|
|
|
|
|
|
|
<para>
|
|
|
|
Concatenation on unspecified types:
|
2001-09-15 02:48:59 +02:00
|
|
|
<screen>
|
1998-07-08 15:53:15 +02:00
|
|
|
tgl=> SELECT 'abc' || 'def' AS "Unspecified";
|
2000-03-26 20:32:30 +02:00
|
|
|
Unspecified
|
|
|
|
-------------
|
|
|
|
abcdef
|
1998-07-08 15:53:15 +02:00
|
|
|
(1 row)
|
2001-09-15 02:48:59 +02:00
|
|
|
</screen>
|
1998-12-29 03:24:47 +01:00
|
|
|
</para>
|
1998-07-08 15:53:15 +02:00
|
|
|
|
|
|
|
<para>
|
|
|
|
In this case there is no initial hint for which type to use, since no types
|
|
|
|
are specified in the query. So, the parser looks for all candidate operators
|
2000-12-17 06:55:26 +01:00
|
|
|
and finds that there are candidates accepting both string-category and
|
2001-09-09 19:21:59 +02:00
|
|
|
bit-string-category inputs. Since string category is preferred when available,
|
2000-12-17 06:55:26 +01:00
|
|
|
that category is selected, and then the
|
2001-09-13 17:55:24 +02:00
|
|
|
<quote>preferred type</quote> for strings, <type>text</type>, is used as the specific
|
2000-12-17 06:55:26 +01:00
|
|
|
type to resolve the unknown literals to.
|
1998-12-29 03:24:47 +01:00
|
|
|
</para>
|
2001-09-15 02:48:59 +02:00
|
|
|
</example>
|
1998-07-08 15:53:15 +02:00
|
|
|
|
2001-09-15 02:48:59 +02:00
|
|
|
<example>
|
|
|
|
<title>Factorial Operator Type Resolution</title>
|
1998-07-08 15:53:15 +02:00
|
|
|
|
|
|
|
<para>
|
|
|
|
This example illustrates an interesting result. Traditionally, the
|
2001-09-15 02:48:59 +02:00
|
|
|
factorial operator is defined for integers only. The <productname>PostgreSQL</productname>
|
1998-07-08 15:53:15 +02:00
|
|
|
operator catalog has only one entry for factorial, taking an integer operand.
|
2001-09-15 02:48:59 +02:00
|
|
|
If given a non-integer numeric argument, <productname>PostgreSQL</productname>
|
1998-07-08 15:53:15 +02:00
|
|
|
will try to convert that argument to an integer for evaluation of the
|
|
|
|
factorial.
|
|
|
|
|
2001-09-15 02:48:59 +02:00
|
|
|
<screen>
|
1998-07-08 15:53:15 +02:00
|
|
|
tgl=> select (4.3 !);
|
2000-03-26 20:32:30 +02:00
|
|
|
?column?
|
|
|
|
----------
|
|
|
|
24
|
1998-07-08 15:53:15 +02:00
|
|
|
(1 row)
|
2001-09-15 02:48:59 +02:00
|
|
|
</screen>
|
1998-07-08 15:53:15 +02:00
|
|
|
|
|
|
|
<note>
|
|
|
|
<para>
|
|
|
|
Of course, this leads to a mathematically suspect result,
|
|
|
|
since in principle the factorial of a non-integer is not defined.
|
|
|
|
However, the role of a database is not to teach mathematics, but
|
|
|
|
to be a tool for data manipulation. If a user chooses to take the
|
2001-09-15 02:48:59 +02:00
|
|
|
factorial of a floating point number, <productname>PostgreSQL</productname>
|
1998-07-08 15:53:15 +02:00
|
|
|
will try to oblige.
|
1998-12-29 03:24:47 +01:00
|
|
|
</para>
|
1998-07-08 15:53:15 +02:00
|
|
|
</note>
|
1998-12-29 03:24:47 +01:00
|
|
|
</para>
|
2001-09-15 02:48:59 +02:00
|
|
|
</example>
|
|
|
|
|
1998-12-29 03:24:47 +01:00
|
|
|
</sect1>
|
1998-07-08 15:53:15 +02:00
|
|
|
|
2000-09-29 22:21:34 +02:00
|
|
|
<sect1 id="typeconv-func">
|
1998-07-08 15:53:15 +02:00
|
|
|
<title>Functions</title>
|
|
|
|
|
2001-09-15 02:48:59 +02:00
|
|
|
<para>
|
|
|
|
The argument types of function calls are resolved according to the
|
|
|
|
following steps.
|
|
|
|
</para>
|
|
|
|
|
1998-07-08 15:53:15 +02:00
|
|
|
<procedure>
|
2001-09-15 02:48:59 +02:00
|
|
|
<title>Function Argument Type Resolution</title>
|
1998-07-08 15:53:15 +02:00
|
|
|
|
|
|
|
<step performance="required">
|
|
|
|
<para>
|
2001-09-09 19:21:59 +02:00
|
|
|
Check for an exact match in the <classname>pg_proc</classname> system catalog.
|
2000-12-17 06:55:26 +01:00
|
|
|
(Cases involving <type>unknown</type> will never find a match at
|
|
|
|
this step.)
|
1998-12-29 03:24:47 +01:00
|
|
|
</para></step>
|
1998-07-08 15:53:15 +02:00
|
|
|
<step performance="required">
|
|
|
|
<para>
|
|
|
|
Look for the best match.
|
1998-12-29 03:24:47 +01:00
|
|
|
</para>
|
1998-07-08 15:53:15 +02:00
|
|
|
<substeps>
|
|
|
|
<step performance="required">
|
|
|
|
<para>
|
2000-12-17 06:55:26 +01:00
|
|
|
Make a list of all functions of the same name with the same number of
|
|
|
|
arguments for which the input types
|
|
|
|
match or can be coerced to match. (<type>unknown</type> literals are
|
|
|
|
assumed to be coercible to anything for this purpose.) If there is only
|
|
|
|
one, use it; else continue to the next step.
|
|
|
|
</para>
|
|
|
|
</step>
|
1998-07-08 15:53:15 +02:00
|
|
|
<step performance="required">
|
|
|
|
<para>
|
2000-12-17 06:55:26 +01:00
|
|
|
Run through all candidates and keep those with the most exact matches
|
|
|
|
on input types. Keep all candidates if none have any exact matches.
|
|
|
|
If only one candidate remains, use it; else continue to the next step.
|
|
|
|
</para>
|
2000-12-17 18:50:46 +01:00
|
|
|
</step>
|
1998-07-08 15:53:15 +02:00
|
|
|
<step performance="required">
|
|
|
|
<para>
|
2000-12-17 06:55:26 +01:00
|
|
|
Run through all candidates and keep those with the most exact or
|
|
|
|
binary-compatible matches on input types. Keep all candidates if none have
|
|
|
|
any exact or binary-compatible matches.
|
|
|
|
If only one candidate remains, use it; else continue to the next step.
|
|
|
|
</para>
|
|
|
|
</step>
|
1998-07-08 15:53:15 +02:00
|
|
|
<step performance="required">
|
|
|
|
<para>
|
2000-12-22 19:57:50 +01:00
|
|
|
Run through all candidates and keep those that accept preferred types at
|
2000-12-17 06:55:26 +01:00
|
|
|
the most positions where type coercion will be required.
|
|
|
|
Keep all candidates if none accept preferred types.
|
|
|
|
If only one candidate remains, use it; else continue to the next step.
|
|
|
|
</para>
|
|
|
|
</step>
|
1998-07-08 15:53:15 +02:00
|
|
|
<step performance="required">
|
|
|
|
<para>
|
2001-09-13 17:55:24 +02:00
|
|
|
If any input arguments are <type>unknown</type>, check the type categories accepted
|
2000-12-17 06:55:26 +01:00
|
|
|
at those argument positions by the remaining candidates. At each position,
|
2001-09-13 17:55:24 +02:00
|
|
|
select <type>string</type>
|
2000-12-17 06:55:26 +01:00
|
|
|
category if any candidate accepts that category (this bias towards string
|
|
|
|
is appropriate since an unknown-type literal does look like a string).
|
|
|
|
Otherwise, if all the remaining candidates accept the same type category,
|
2000-12-19 01:54:59 +01:00
|
|
|
select that category; otherwise fail because
|
2000-12-17 06:55:26 +01:00
|
|
|
the correct choice cannot be deduced without more clues. Also note whether
|
2001-09-09 19:21:59 +02:00
|
|
|
any of the candidates accept a preferred data type within the selected category.
|
2000-12-17 06:55:26 +01:00
|
|
|
Now discard operator candidates that do not accept the selected type category;
|
|
|
|
furthermore, if any candidate accepts a preferred type at a given argument
|
|
|
|
position, discard candidates that accept non-preferred types for that
|
|
|
|
argument.
|
|
|
|
</para>
|
|
|
|
</step>
|
|
|
|
<step performance="required">
|
|
|
|
<para>
|
|
|
|
If only one candidate remains, use it. If no candidate or more than one
|
|
|
|
candidate remains,
|
2000-12-19 01:54:59 +01:00
|
|
|
then fail.
|
2000-12-17 06:55:26 +01:00
|
|
|
</para>
|
|
|
|
</step>
|
1998-07-08 15:53:15 +02:00
|
|
|
</substeps>
|
1998-12-29 03:24:47 +01:00
|
|
|
</step>
|
2000-12-19 01:54:59 +01:00
|
|
|
<step performance="required">
|
|
|
|
<para>
|
|
|
|
If no best match could be identified, see whether the function call appears
|
|
|
|
to be a trivial type coercion request. This happens if the function call
|
|
|
|
has just one argument and the function name is the same as the (internal)
|
2001-09-09 19:21:59 +02:00
|
|
|
name of some data type. Furthermore, the function argument must be either
|
2000-12-19 01:54:59 +01:00
|
|
|
an unknown-type literal or a type that is binary-compatible with the named
|
2001-09-09 19:21:59 +02:00
|
|
|
data type. When these conditions are met, the function argument is coerced
|
|
|
|
to the named data type.
|
2000-12-19 01:54:59 +01:00
|
|
|
</para>
|
|
|
|
</step>
|
1998-07-08 15:53:15 +02:00
|
|
|
</procedure>
|
2000-12-17 06:55:26 +01:00
|
|
|
|
2001-09-15 02:48:59 +02:00
|
|
|
<bridgehead renderas="sect2">Examples</bridgehead>
|
1998-07-08 15:53:15 +02:00
|
|
|
|
2001-09-15 02:48:59 +02:00
|
|
|
<example>
|
|
|
|
<title>Factorial Function Argument Type Resolution</title>
|
1998-07-08 15:53:15 +02:00
|
|
|
|
|
|
|
<para>
|
2001-09-09 19:21:59 +02:00
|
|
|
There is only one factorial function defined in the <classname>pg_proc</classname> catalog.
|
1998-07-08 15:53:15 +02:00
|
|
|
So the following query automatically converts the <type>int2</type> argument
|
|
|
|
to <type>int4</type>:
|
|
|
|
|
2001-09-15 02:48:59 +02:00
|
|
|
<screen>
|
1998-07-08 15:53:15 +02:00
|
|
|
tgl=> select int4fac(int2 '4');
|
2000-03-26 20:32:30 +02:00
|
|
|
int4fac
|
|
|
|
---------
|
|
|
|
24
|
1998-07-08 15:53:15 +02:00
|
|
|
(1 row)
|
2001-09-15 02:48:59 +02:00
|
|
|
</screen>
|
1998-07-08 15:53:15 +02:00
|
|
|
|
|
|
|
and is actually transformed by the parser to
|
2001-09-15 02:48:59 +02:00
|
|
|
<screen>
|
1998-07-08 15:53:15 +02:00
|
|
|
tgl=> select int4fac(int4(int2 '4'));
|
2000-03-26 20:32:30 +02:00
|
|
|
int4fac
|
|
|
|
---------
|
|
|
|
24
|
1998-07-08 15:53:15 +02:00
|
|
|
(1 row)
|
2001-09-15 02:48:59 +02:00
|
|
|
</screen>
|
1998-12-29 03:24:47 +01:00
|
|
|
</para>
|
2001-09-15 02:48:59 +02:00
|
|
|
</example>
|
1998-07-08 15:53:15 +02:00
|
|
|
|
2001-09-15 02:48:59 +02:00
|
|
|
<example>
|
|
|
|
<title>Substring Function Type Resolution</title>
|
1998-07-08 15:53:15 +02:00
|
|
|
|
|
|
|
<para>
|
2001-09-09 19:21:59 +02:00
|
|
|
There are two <function>substr</function> functions declared in <classname>pg_proc</classname>. However,
|
1998-07-08 15:53:15 +02:00
|
|
|
only one takes two arguments, of types <type>text</type> and <type>int4</type>.
|
1998-12-29 03:24:47 +01:00
|
|
|
</para>
|
1998-07-08 15:53:15 +02:00
|
|
|
|
|
|
|
<para>
|
|
|
|
If called with a string constant of unspecified type, the type is matched up
|
|
|
|
directly with the only candidate function type:
|
2001-09-15 02:48:59 +02:00
|
|
|
<screen>
|
1998-07-08 15:53:15 +02:00
|
|
|
tgl=> select substr('1234', 3);
|
2000-03-26 20:32:30 +02:00
|
|
|
substr
|
|
|
|
--------
|
|
|
|
34
|
1998-07-08 15:53:15 +02:00
|
|
|
(1 row)
|
2001-09-15 02:48:59 +02:00
|
|
|
</screen>
|
1998-12-29 03:24:47 +01:00
|
|
|
</para>
|
1998-07-08 15:53:15 +02:00
|
|
|
|
|
|
|
<para>
|
|
|
|
If the string is declared to be of type <type>varchar</type>, as might be the case
|
|
|
|
if it comes from a table, then the parser will try to coerce it to become <type>text</type>:
|
2001-09-15 02:48:59 +02:00
|
|
|
<screen>
|
1998-07-08 15:53:15 +02:00
|
|
|
tgl=> select substr(varchar '1234', 3);
|
2000-03-26 20:32:30 +02:00
|
|
|
substr
|
|
|
|
--------
|
|
|
|
34
|
1998-07-08 15:53:15 +02:00
|
|
|
(1 row)
|
2001-09-15 02:48:59 +02:00
|
|
|
</screen>
|
1998-07-08 15:53:15 +02:00
|
|
|
which is transformed by the parser to become
|
2001-09-15 02:48:59 +02:00
|
|
|
<screen>
|
1998-07-08 15:53:15 +02:00
|
|
|
tgl=> select substr(text(varchar '1234'), 3);
|
2000-03-26 20:32:30 +02:00
|
|
|
substr
|
|
|
|
--------
|
|
|
|
34
|
1998-07-08 15:53:15 +02:00
|
|
|
(1 row)
|
2001-09-15 02:48:59 +02:00
|
|
|
</screen>
|
1998-12-29 03:24:47 +01:00
|
|
|
</para>
|
2001-09-15 02:48:59 +02:00
|
|
|
<para>
|
1998-07-08 15:53:15 +02:00
|
|
|
<note>
|
|
|
|
<para>
|
2000-12-17 06:55:26 +01:00
|
|
|
Actually, the parser is aware that <type>text</type> and <type>varchar</type>
|
2001-09-13 17:55:24 +02:00
|
|
|
are <firstterm>binary compatible</>, meaning that one can be passed to a function that
|
2000-12-17 06:55:26 +01:00
|
|
|
accepts the other without doing any physical conversion. Therefore, no
|
|
|
|
explicit type conversion call is really inserted in this case.
|
1998-12-29 03:24:47 +01:00
|
|
|
</para>
|
1998-07-08 15:53:15 +02:00
|
|
|
</note>
|
2001-09-15 02:48:59 +02:00
|
|
|
</para>
|
1998-07-08 15:53:15 +02:00
|
|
|
|
|
|
|
<para>
|
|
|
|
And, if the function is called with an <type>int4</type>, the parser will
|
|
|
|
try to convert that to <type>text</type>:
|
2001-09-15 02:48:59 +02:00
|
|
|
<screen>
|
1998-07-08 15:53:15 +02:00
|
|
|
tgl=> select substr(1234, 3);
|
2000-03-26 20:32:30 +02:00
|
|
|
substr
|
|
|
|
--------
|
|
|
|
34
|
1998-07-08 15:53:15 +02:00
|
|
|
(1 row)
|
2001-09-15 02:48:59 +02:00
|
|
|
</screen>
|
1998-07-08 15:53:15 +02:00
|
|
|
actually executes as
|
2001-09-15 02:48:59 +02:00
|
|
|
<screen>
|
1998-07-08 15:53:15 +02:00
|
|
|
tgl=> select substr(text(1234), 3);
|
2000-03-26 20:32:30 +02:00
|
|
|
substr
|
|
|
|
--------
|
|
|
|
34
|
1998-07-08 15:53:15 +02:00
|
|
|
(1 row)
|
2001-09-15 02:48:59 +02:00
|
|
|
</screen>
|
2000-12-17 06:55:26 +01:00
|
|
|
This succeeds because there is a conversion function text(int4) in the
|
|
|
|
system catalog.
|
1998-12-29 03:24:47 +01:00
|
|
|
</para>
|
2001-09-15 02:48:59 +02:00
|
|
|
</example>
|
|
|
|
|
1998-12-29 03:24:47 +01:00
|
|
|
</sect1>
|
1998-07-08 15:53:15 +02:00
|
|
|
|
2000-09-29 22:21:34 +02:00
|
|
|
<sect1 id="typeconv-query">
|
1998-07-08 15:53:15 +02:00
|
|
|
<title>Query Targets</title>
|
|
|
|
|
|
|
|
<procedure>
|
2000-12-17 06:55:26 +01:00
|
|
|
<title>Query Target Type Resolution</title>
|
1998-07-08 15:53:15 +02:00
|
|
|
|
|
|
|
<step performance="required">
|
|
|
|
<para>
|
|
|
|
Check for an exact match with the target.
|
1998-12-29 03:24:47 +01:00
|
|
|
</para></step>
|
1998-07-08 15:53:15 +02:00
|
|
|
<step performance="required">
|
|
|
|
<para>
|
2000-12-17 06:55:26 +01:00
|
|
|
Otherwise, try to coerce the expression to the target type. This will succeed
|
|
|
|
if the two types are known binary-compatible, or if there is a conversion
|
|
|
|
function. If the expression is an unknown-type literal, the contents of
|
|
|
|
the literal string will be fed to the input conversion routine for the target
|
|
|
|
type.
|
1998-12-29 03:24:47 +01:00
|
|
|
</para></step>
|
1998-07-08 15:53:15 +02:00
|
|
|
|
|
|
|
<step performance="required">
|
|
|
|
<para>
|
|
|
|
If the target is a fixed-length type (e.g. <type>char</type> or <type>varchar</type>
|
2000-12-17 06:55:26 +01:00
|
|
|
declared with a length) then try to find a sizing function for the target
|
|
|
|
type. A sizing function is a function of the same name as the type,
|
|
|
|
taking two arguments of which the first is that type and the second is an
|
|
|
|
integer, and returning the same type. If one is found, it is applied,
|
|
|
|
passing the column's declared length as the second parameter.
|
1998-12-29 03:24:47 +01:00
|
|
|
</para></step>
|
1998-07-08 15:53:15 +02:00
|
|
|
|
|
|
|
</procedure>
|
|
|
|
|
2001-09-15 02:48:59 +02:00
|
|
|
<example>
|
|
|
|
<title><type>varchar</type> Storage Type Conversion</title>
|
1998-07-08 15:53:15 +02:00
|
|
|
|
|
|
|
<para>
|
|
|
|
For a target column declared as <type>varchar(4)</type> the following query
|
|
|
|
ensures that the target is sized correctly:
|
|
|
|
|
2001-09-15 02:48:59 +02:00
|
|
|
<screen>
|
1998-07-08 15:53:15 +02:00
|
|
|
tgl=> CREATE TABLE vv (v varchar(4));
|
|
|
|
CREATE
|
|
|
|
tgl=> INSERT INTO vv SELECT 'abc' || 'def';
|
|
|
|
INSERT 392905 1
|
2000-03-26 20:32:30 +02:00
|
|
|
tgl=> SELECT * FROM vv;
|
|
|
|
v
|
|
|
|
------
|
|
|
|
abcd
|
2000-12-17 06:55:26 +01:00
|
|
|
(1 row)
|
2001-09-15 02:48:59 +02:00
|
|
|
</screen>
|
2000-12-17 06:55:26 +01:00
|
|
|
|
2001-09-15 02:48:59 +02:00
|
|
|
What has really happened here is that the two unknown literals are resolved
|
|
|
|
to <type>text</type> by default, allowing the <literal>||</literal> operator to be
|
|
|
|
resolved as <type>text</type> concatenation. Then the <type>text</type> result of the operator
|
2001-09-09 19:21:59 +02:00
|
|
|
is coerced to <type>varchar</type> to match the target column type. (But, since the
|
2001-09-15 02:48:59 +02:00
|
|
|
parser knows that <type>text</type> and <type>varchar</type> are binary-compatible, this coercion
|
2000-12-17 06:55:26 +01:00
|
|
|
is implicit and does not insert any real function call.) Finally, the
|
2001-09-15 02:48:59 +02:00
|
|
|
sizing function <literal>varchar(varchar, integer)</literal> is found in the system
|
2000-12-17 06:55:26 +01:00
|
|
|
catalogs and applied to the operator's result and the stored column length.
|
|
|
|
This type-specific function performs the desired truncation.
|
1998-12-29 03:24:47 +01:00
|
|
|
</para>
|
2001-09-15 02:48:59 +02:00
|
|
|
</example>
|
1998-12-29 03:24:47 +01:00
|
|
|
</sect1>
|
1998-07-08 15:53:15 +02:00
|
|
|
|
2000-12-17 06:55:26 +01:00
|
|
|
<sect1 id="typeconv-union-case">
|
2001-09-15 02:48:59 +02:00
|
|
|
<title><literal>UNION</> and <literal>CASE</> Constructs</title>
|
1998-07-08 15:53:15 +02:00
|
|
|
|
|
|
|
<para>
|
2001-09-15 02:48:59 +02:00
|
|
|
The <literal>UNION</> and <literal>CASE</> constructs must match up possibly dissimilar types to
|
2000-12-17 06:55:26 +01:00
|
|
|
become a single result set. The resolution algorithm is applied separately to
|
2001-09-15 02:48:59 +02:00
|
|
|
each output column of a union. <literal>CASE</> uses the identical algorithm to match
|
2000-12-17 06:55:26 +01:00
|
|
|
up its result expressions.
|
1998-12-29 03:24:47 +01:00
|
|
|
</para>
|
1998-07-08 15:53:15 +02:00
|
|
|
<procedure>
|
2001-09-15 02:48:59 +02:00
|
|
|
<title><literal>UNION</> and <literal>CASE</> Type Resolution</title>
|
2000-12-17 06:55:26 +01:00
|
|
|
|
|
|
|
<step performance="required">
|
|
|
|
<para>
|
|
|
|
If all inputs are of type <type>unknown</type>, resolve as type
|
|
|
|
<type>text</type> (the preferred type for string category).
|
|
|
|
Otherwise, ignore the <type>unknown</type> inputs while choosing the type.
|
|
|
|
</para></step>
|
|
|
|
|
|
|
|
<step performance="required">
|
|
|
|
<para>
|
2000-12-19 01:54:59 +01:00
|
|
|
If the non-unknown inputs are not all of the same type category, fail.
|
2000-12-17 06:55:26 +01:00
|
|
|
</para></step>
|
1998-07-08 15:53:15 +02:00
|
|
|
|
|
|
|
<step performance="required">
|
|
|
|
<para>
|
2000-12-17 06:55:26 +01:00
|
|
|
If one or more non-unknown inputs are of a preferred type in that category,
|
|
|
|
resolve as that type.
|
1998-12-29 03:24:47 +01:00
|
|
|
</para></step>
|
1998-07-08 15:53:15 +02:00
|
|
|
|
|
|
|
<step performance="required">
|
|
|
|
<para>
|
2000-12-17 06:55:26 +01:00
|
|
|
Otherwise, resolve as the type of the first non-unknown input.
|
|
|
|
</para></step>
|
|
|
|
|
|
|
|
<step performance="required">
|
|
|
|
<para>
|
|
|
|
Coerce all inputs to the selected type.
|
1998-12-29 03:24:47 +01:00
|
|
|
</para></step>
|
1998-07-08 15:53:15 +02:00
|
|
|
</procedure>
|
|
|
|
|
2001-09-15 02:48:59 +02:00
|
|
|
<bridgehead renderas="sect2">Examples</bridgehead>
|
1998-07-08 15:53:15 +02:00
|
|
|
|
2001-09-15 02:48:59 +02:00
|
|
|
<example>
|
|
|
|
<title>Underspecified Types in a Union</title>
|
1998-07-08 15:53:15 +02:00
|
|
|
|
|
|
|
<para>
|
2001-09-15 02:48:59 +02:00
|
|
|
<screen>
|
1998-07-08 15:53:15 +02:00
|
|
|
tgl=> SELECT text 'a' AS "Text" UNION SELECT 'b';
|
2000-03-26 20:32:30 +02:00
|
|
|
Text
|
|
|
|
------
|
|
|
|
a
|
|
|
|
b
|
1998-07-08 15:53:15 +02:00
|
|
|
(2 rows)
|
2001-09-15 02:48:59 +02:00
|
|
|
</screen>
|
2001-09-13 17:55:24 +02:00
|
|
|
Here, the unknown-type literal <literal>'b'</literal> will be resolved as type text.
|
1998-12-29 03:24:47 +01:00
|
|
|
</para>
|
2001-09-15 02:48:59 +02:00
|
|
|
</example>
|
1998-07-08 15:53:15 +02:00
|
|
|
|
2001-09-15 02:48:59 +02:00
|
|
|
<example>
|
|
|
|
<title>Type Conversion in a Simple Union</title>
|
1998-07-08 15:53:15 +02:00
|
|
|
|
|
|
|
<para>
|
2001-09-15 02:48:59 +02:00
|
|
|
<screen>
|
2001-01-20 21:59:29 +01:00
|
|
|
tgl=> SELECT 1.2 AS "Double" UNION SELECT 1;
|
|
|
|
Double
|
2000-03-26 20:32:30 +02:00
|
|
|
--------
|
|
|
|
1
|
|
|
|
1.2
|
1998-07-08 15:53:15 +02:00
|
|
|
(2 rows)
|
2001-09-15 02:48:59 +02:00
|
|
|
</screen>
|
1998-12-29 03:24:47 +01:00
|
|
|
</para>
|
2001-09-15 02:48:59 +02:00
|
|
|
</example>
|
1998-07-08 15:53:15 +02:00
|
|
|
|
2001-09-15 02:48:59 +02:00
|
|
|
<example>
|
|
|
|
<title>Type Conversion in a Transposed Union</title>
|
1998-07-08 15:53:15 +02:00
|
|
|
|
|
|
|
<para>
|
2000-12-17 06:55:26 +01:00
|
|
|
Here the output type of the union is forced to match the type of
|
1998-07-08 15:53:15 +02:00
|
|
|
the first/top clause in the union:
|
|
|
|
|
2001-09-15 02:48:59 +02:00
|
|
|
<screen>
|
1998-07-08 15:53:15 +02:00
|
|
|
tgl=> SELECT 1 AS "All integers"
|
2001-01-20 21:59:29 +01:00
|
|
|
tgl-> UNION SELECT CAST('2.2' AS REAL);
|
2000-03-26 20:32:30 +02:00
|
|
|
All integers
|
|
|
|
--------------
|
|
|
|
1
|
|
|
|
2
|
2000-12-17 06:55:26 +01:00
|
|
|
(2 rows)
|
2001-09-15 02:48:59 +02:00
|
|
|
</screen>
|
1998-12-29 03:24:47 +01:00
|
|
|
</para>
|
1998-07-08 15:53:15 +02:00
|
|
|
<para>
|
2001-01-20 21:59:29 +01:00
|
|
|
Since <type>REAL</type> is not a preferred type, the parser sees no reason
|
|
|
|
to select it over <type>INTEGER</type> (which is what the 1 is), and instead
|
|
|
|
falls back on the use-the-first-alternative rule.
|
2000-12-17 06:55:26 +01:00
|
|
|
This example demonstrates that the preferred-type mechanism doesn't encode
|
|
|
|
as much information as we'd like. Future versions of
|
2001-09-15 02:48:59 +02:00
|
|
|
<productname>PostgreSQL</productname> may support a more general notion of
|
2000-12-17 06:55:26 +01:00
|
|
|
type preferences.
|
1998-12-29 03:24:47 +01:00
|
|
|
</para>
|
2001-09-15 02:48:59 +02:00
|
|
|
</example>
|
|
|
|
|
1998-12-29 03:24:47 +01:00
|
|
|
</sect1>
|
1998-07-08 15:53:15 +02:00
|
|
|
</chapter>
|
2000-05-02 22:02:03 +02:00
|
|
|
|
|
|
|
<!-- Keep this comment at the end of the file
|
|
|
|
Local variables:
|
|
|
|
mode:sgml
|
|
|
|
sgml-omittag:t
|
|
|
|
sgml-shorttag:t
|
|
|
|
sgml-minimize-attributes:nil
|
|
|
|
sgml-always-quote-attributes:t
|
|
|
|
sgml-indent-step:1
|
|
|
|
sgml-indent-data:t
|
|
|
|
sgml-parent-document:nil
|
|
|
|
sgml-default-dtd-file:"./reference.ced"
|
|
|
|
sgml-exposed-tags:nil
|
|
|
|
sgml-local-catalogs:("/usr/lib/sgml/catalog")
|
|
|
|
sgml-local-ecat-files:nil
|
|
|
|
End:
|
|
|
|
-->
|