1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149
|
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Chapter10.Type Conversion</title>
<link rel="stylesheet" href="stylesheet.css" type="text/css">
<link rev="made" href="pgsql-docs@postgresql.org">
<meta name="generator" content="DocBook XSL Stylesheets V1.70.0">
<link rel="start" href="index.html" title="PostgreSQL 8.1.4 Documentation">
<link rel="up" href="sql.html" title="PartII.The SQL Language">
<link rel="prev" href="functions-admin.html" title="9.20.System Administration Functions">
<link rel="next" href="typeconv-oper.html" title="10.2.Operators">
<link rel="copyright" href="ln-legalnotice.html" title="Legal Notice">
</head>
<body bgcolor="white" text="black" link="#0000FF" vlink="#840084" alink="#0000FF"><div class="chapter" lang="en" id="typeconv">
<div class="titlepage"><div><div><h2 class="title">
<a name="typeconv"></a>Chapter10.Type Conversion</h2></div></div></div>
<div class="toc">
<p><b>Table of Contents</b></p>
<dl>
<dt><span class="sect1"><a href="typeconv.html#typeconv-overview">10.1. Overview</a></span></dt>
<dt><span class="sect1"><a href="typeconv-oper.html">10.2. Operators</a></span></dt>
<dt><span class="sect1"><a href="typeconv-func.html">10.3. Functions</a></span></dt>
<dt><span class="sect1"><a href="typeconv-query.html">10.4. Value Storage</a></span></dt>
<dt><span class="sect1"><a href="typeconv-union-case.html">10.5. <code class="literal">UNION</code>, <code class="literal">CASE</code>, and Related Constructs</a></span></dt>
</dl>
</div>
<a name="id625906"></a><p><acronym class="acronym">SQL</acronym> statements can, intentionally or not, require
mixing of different data types in the same expression.
<span class="productname">PostgreSQL</span> has extensive facilities for
evaluating mixed-type expressions.</p>
<p>In many cases a user will not need
to understand the details of the type conversion mechanism.
However, the implicit conversions done by <span class="productname">PostgreSQL</span>
can affect the results of a query. When necessary, these results
can be tailored by using <span class="emphasis"><em>explicit</em></span> type conversion.</p>
<p>This chapter introduces the <span class="productname">PostgreSQL</span>
type conversion mechanisms and conventions.
Refer to the relevant sections in <a href="datatype.html" title="Chapter8.Data Types">Chapter8, <i>Data Types</i></a> and <a href="functions.html" title="Chapter9.Functions and Operators">Chapter9, <i>Functions and Operators</i></a>
for more information on specific data types and allowed functions and
operators.</p>
<div class="sect1" lang="en">
<div class="titlepage"><div><div><h2 class="title" style="clear: both">
<a name="typeconv-overview"></a>10.1.Overview</h2></div></div></div>
<p><acronym class="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.
<span class="productname">PostgreSQL</span> has an extensible type system that is
much more general and flexible than other <acronym class="acronym">SQL</acronym> implementations.
Hence, most type conversion behavior in <span class="productname">PostgreSQL</span>
is governed by general rules rather than by <span class="foreignphrase"><em class="foreignphrase">ad hoc</em></span>
heuristics. This allows
mixed-type expressions to be meaningful even with user-defined types.</p>
<p>The <span class="productname">PostgreSQL</span> scanner/parser divides lexical
elements into only five fundamental categories: integers, non-integer numbers,
strings, identifiers, and key words. Constants of most non-numeric types are
first classified as strings. The <acronym class="acronym">SQL</acronym> language definition
allows specifying type names with strings, and this mechanism can be used in
<span class="productname">PostgreSQL</span> to start the parser down the correct
path. For example, the query
</p>
<pre class="screen">SELECT text 'Origin' AS "label", point '(0,0)' AS "value";
label | value
--------+-------
Origin | (0,0)
(1 row)</pre>
<p>
has two literal constants, of type <code class="type">text</code> and <code class="type">point</code>.
If a type is not specified for a string literal, then the placeholder type
<code class="type">unknown</code> is assigned initially, to be resolved in later
stages as described below.</p>
<p>There are four fundamental <acronym class="acronym">SQL</acronym> constructs requiring
distinct type conversion rules in the <span class="productname">PostgreSQL</span>
parser:
</p>
<div class="variablelist"><dl>
<dt><span class="term">Function calls</span></dt>
<dd><p>Much of the <span class="productname">PostgreSQL</span> type system is built around a
rich set of functions. Functions can have one or more arguments.
Since <span class="productname">PostgreSQL</span> permits function
overloading, the function name alone does not uniquely identify the function
to be called; the parser must select the right function based on the data
types of the supplied arguments.</p></dd>
<dt><span class="term">Operators</span></dt>
<dd><p><span class="productname">PostgreSQL</span> allows expressions with
prefix and postfix unary (one-argument) operators,
as well as binary (two-argument) operators. Like functions, operators can
be overloaded, and so the same problem of selecting the right operator
exists.</p></dd>
<dt><span class="term">Value Storage</span></dt>
<dd><p><acronym class="acronym">SQL</acronym> <code class="command">INSERT</code> and <code class="command">UPDATE</code> statements place the results of
expressions into a table. The expressions in the statement must be matched up
with, and perhaps converted to, the types of the target columns.</p></dd>
<dt><span class="term"><code class="literal">UNION</code>, <code class="literal">CASE</code>, and related constructs</span></dt>
<dd><p>Since all query results from a unionized <code class="command">SELECT</code> statement
must appear in a single set of columns, the types of the results of each
<code class="command">SELECT</code> clause must be matched up and converted to a uniform set.
Similarly, the result expressions of a <code class="literal">CASE</code> construct must be
converted to a common type so that the <code class="literal">CASE</code> expression as a whole
has a known output type. The same holds for <code class="literal">ARRAY</code> constructs,
and for the <code class="function">GREATEST</code> and <code class="function">LEAST</code> functions.</p></dd>
</dl></div>
<p>The system catalogs store information about which conversions, called
<em class="firstterm">casts</em>, between data types are valid, and how to
perform those conversions. Additional casts can be added by the user
with the <code class="command">CREATE CAST</code> command. (This is usually
done in conjunction with defining new data types. The set of casts
between the built-in types has been carefully crafted and is best not
altered.)</p>
<a name="id626246"></a><p>An additional heuristic is provided in the parser to allow better guesses
at proper behavior for <acronym class="acronym">SQL</acronym> standard types. There are
several basic <em class="firstterm">type categories</em> defined: <code class="type">boolean</code>,
<code class="type">numeric</code>, <code class="type">string</code>, <code class="type">bitstring</code>, <code class="type">datetime</code>, <code class="type">timespan</code>, <code class="type">geometric</code>, <code class="type">network</code>,
and user-defined. Each category, with the exception of user-defined, has
one or more <em class="firstterm">preferred types</em> which are 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 therefore 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.</p>
<p>All type conversion rules are designed with several principles in mind:
</p>
<div class="itemizedlist"><ul type="disc">
<li><p>Implicit conversions should never have surprising or unpredictable outcomes.</p></li>
<li><p>User-defined types, of which the parser has no <span class="foreignphrase"><em class="foreignphrase">a priori</em></span> knowledge, should be
“<span class="quote">higher</span>” in the type hierarchy. In mixed-type expressions, native types shall always
be converted to a user-defined type (of course, only if conversion is necessary).</p></li>
<li><p>User-defined types are not related. Currently, <span class="productname">PostgreSQL</span>
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 and casts.</p></li>
<li>
<p>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
calls into the query.</p>
<p>Additionally, if a query usually requires an implicit conversion for a function, and
if then the user defines a new 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.</p>
</li>
</ul></div>
</div>
</div></body>
</html>
|