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 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176
|
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>10.3.Functions</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="typeconv.html" title="Chapter10.Type Conversion">
<link rel="prev" href="typeconv-oper.html" title="10.2.Operators">
<link rel="next" href="typeconv-query.html" title="10.4.Value Storage">
<link rel="copyright" href="ln-legalnotice.html" title="Legal Notice">
</head>
<body bgcolor="white" text="black" link="#0000FF" vlink="#840084" alink="#0000FF"><div class="sect1" lang="en">
<div class="titlepage"><div><div><h2 class="title" style="clear: both">
<a name="typeconv-func"></a>10.3.Functions</h2></div></div></div>
<a name="id626772"></a><p> The specific function to be used in a function invocation is determined
according to the following steps.
</p>
<div class="procedure">
<a name="id626791"></a><p class="title"><b>Function Type Resolution</b></p>
<ol type="1">
<li>
<p>Select the functions to be considered from the
<code class="classname">pg_proc</code> system catalog. If an unqualified
function name was used, the functions
considered are those of the right name and argument count that are
visible in the current search path (see <a href="ddl-schemas.html#ddl-schemas-path" title="5.7.3.The Schema Search Path">Section5.7.3, “The Schema Search Path”</a>).
If a qualified function name was given, only functions in the specified
schema are considered.</p>
<ol type="a"><li><p>If the search path finds multiple functions of identical argument types,
only the one appearing earliest in the path is considered. But functions of
different argument types are considered on an equal footing regardless of
search path position.</p></li></ol>
</li>
<li><p>Check for a function accepting exactly the input argument types.
If one exists (there can be only one exact match in the set of
functions considered), use it.
(Cases involving <code class="type">unknown</code> will never find a match at
this step.)</p></li>
<li><p>If no exact match is found, see whether the function call appears
to be a trivial type conversion request. This happens if the function call
has just one argument and the function name is the same as the (internal)
name of some data type. Furthermore, the function argument must be either
an unknown-type literal or a type that is binary-compatible with the named
data type. When these conditions are met, the function argument is converted
to the named data type without any actual function call.</p></li>
<li>
<p>Look for the best match.</p>
<ol type="a">
<li><p>Discard candidate functions for which the input types do not match
and cannot be converted (using an implicit conversion) to match.
<code class="type">unknown</code> literals are
assumed to be convertible to anything for this purpose. If only one
candidate remains, use it; else continue to the next step.</p></li>
<li><p>Run through all candidates and keep those with the most exact matches
on input types. (Domains are considered the same as their base type
for this purpose.) Keep all candidates if none have any exact matches.
If only one candidate remains, use it; else continue to the next step.</p></li>
<li><p>Run through all candidates and keep those that accept preferred types (of the
input data type's type category) at the most positions where type conversion
will be required.
Keep all candidates if none accept preferred types.
If only one candidate remains, use it; else continue to the next step.</p></li>
<li><p>If any input arguments are <code class="type">unknown</code>, check the type categories
accepted
at those argument positions by the remaining candidates. At each position,
select the <code class="type">string</code> 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,
select that category; otherwise fail because
the correct choice cannot be deduced without more clues.
Now discard 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.</p></li>
<li><p>If only one candidate remains, use it. If no candidate or more than one
candidate remains,
then fail.</p></li>
</ol>
</li>
</ol>
</div>
<p>Note that the “<span class="quote">best match</span>” rules are identical for operator and
function type resolution.
Some examples follow.</p>
<div class="example">
<a name="id626952"></a><p class="title"><b>Example10.4.Rounding Function Argument Type Resolution</b></p>
<div class="example-contents">
<p>There is only one <code class="function">round</code> function with two
arguments. (The first is <code class="type">numeric</code>, the second is
<code class="type">integer</code>.) So the following query automatically converts
the first argument of type <code class="type">integer</code> to
<code class="type">numeric</code>:
</p>
<pre class="screen">SELECT round(4, 4);
round
--------
4.0000
(1 row)</pre>
<p>
That query is actually transformed by the parser to
</p>
<pre class="screen">SELECT round(CAST (4 AS numeric), 4);</pre>
<p>Since numeric constants with decimal points are initially assigned the
type <code class="type">numeric</code>, the following query will require no type
conversion and may therefore be slightly more efficient:
</p>
<pre class="screen">SELECT round(4.0, 4);</pre>
</div>
</div>
<br class="example-break"><div class="example">
<a name="id627013"></a><p class="title"><b>Example10.5.Substring Function Type Resolution</b></p>
<div class="example-contents">
<p>There are several <code class="function">substr</code> functions, one of which
takes types <code class="type">text</code> and <code class="type">integer</code>. If called
with a string constant of unspecified type, the system chooses the
candidate function that accepts an argument of the preferred category
<code class="literal">string</code> (namely of type <code class="type">text</code>).
</p>
<pre class="screen">SELECT substr('1234', 3);
substr
--------
34
(1 row)</pre>
<p>If the string is declared to be of type <code class="type">varchar</code>, as might be the case
if it comes from a table, then the parser will try to convert it to become <code class="type">text</code>:
</p>
<pre class="screen">SELECT substr(varchar '1234', 3);
substr
--------
34
(1 row)</pre>
<p>
This is transformed by the parser to effectively become
</p>
<pre class="screen">SELECT substr(CAST (varchar '1234' AS text), 3);</pre>
<div class="note" style="margin-left: 0.5in; margin-right: 0.5in;">
<h3 class="title">Note</h3>
<p>The parser learns from the <code class="structname">pg_cast</code> catalog that
<code class="type">text</code> and <code class="type">varchar</code>
are binary-compatible, meaning that one can be passed to a function that
accepts the other without doing any physical conversion. Therefore, no
explicit type conversion call is really inserted in this case.</p>
</div>
<p>And, if the function is called with an argument of type <code class="type">integer</code>, the parser will
try to convert that to <code class="type">text</code>:
</p>
<pre class="screen">SELECT substr(1234, 3);
substr
--------
34
(1 row)</pre>
<p>
This actually executes as
</p>
<pre class="screen">SELECT substr(CAST (1234 AS text), 3);</pre>
<p>
This automatic transformation can succeed because there is an
implicitly invocable cast from <code class="type">integer</code> to
<code class="type">text</code>.</p>
</div>
</div>
<br class="example-break">
</div></body>
</html>
|