1
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
8
CONTENT="Modular DocBook HTML Stylesheet Version 1.79"><LINK
10
HREF="mailto:pgsql-docs@postgresql.org"><LINK
12
TITLE="PostgreSQL 9.3beta1 Documentation"
13
HREF="index.html"><LINK
15
TITLE="Type Conversion"
16
HREF="typeconv.html"><LINK
19
HREF="typeconv-oper.html"><LINK
22
HREF="typeconv-query.html"><LINK
25
HREF="stylesheet.css"><META
26
HTTP-EQUIV="Content-Type"
27
CONTENT="text/html; charset=ISO-8859-1"><META
29
CONTENT="2013-05-06T21:00:50"></HEAD
35
SUMMARY="Header navigation table"
47
>PostgreSQL 9.3beta1 Documentation</A
57
HREF="typeconv-oper.html"
74
>Chapter 10. Type Conversion</TD
81
HREF="typeconv-query.html"
99
> The specific function that is referenced by a function call
100
is determined using the following procedure.
106
>Function Type Resolution</B
113
>Select the functions to be considered from the
117
> system catalog. If a non-schema-qualified
118
function name was used, the functions
119
considered are those with the matching name and argument count that are
120
visible in the current search path (see <A
121
HREF="ddl-schemas.html#DDL-SCHEMAS-PATH"
124
If a qualified function name was given, only functions in the specified
125
schema are considered.</P
132
>If the search path finds multiple functions of identical argument types,
133
only the one appearing earliest in the path is considered. Functions of
134
different argument types are considered on an equal footing regardless of
135
search path position.</P
140
>If a function is declared with a <TT
143
> array parameter, and
144
the call does not use the <TT
147
> keyword, then the function
148
is treated as if the array parameter were replaced by one or more occurrences
149
of its element type, as needed to match the call. After such expansion the
150
function might have effective argument types identical to some non-variadic
151
function. In that case the function appearing earlier in the search path is
152
used, or if the two functions are in the same schema, the non-variadic one is
158
>Functions that have default values for parameters are considered to match any
159
call that omits zero or more of the defaultable parameter positions. If more
160
than one such function matches a call, the one appearing earliest in the
161
search path is used. If there are two or more such functions in the same
162
schema with identical parameter types in the non-defaulted positions (which is
163
possible if they have different sets of defaultable parameters), the system
164
will not be able to determine which to prefer, and so an <SPAN
168
> error will result if no better match to the call can be
176
>Check for a function accepting exactly the input argument types.
177
If one exists (there can be only one exact match in the set of
178
functions considered), use it.
182
> will never find a match at
188
>If no exact match is found, see if the function call appears
189
to be a special type conversion request. This happens if the function call
190
has just one argument and the function name is the same as the (internal)
191
name of some data type. Furthermore, the function argument must be either
192
an unknown-type literal, or a type that is binary-coercible to the named
193
data type, or a type that could be converted to the named data type by
194
applying that type's I/O functions (that is, the conversion is either to or
195
from one of the standard string types). When these conditions are met,
196
the function call is treated as a form of <TT
212
>Look for the best match.</P
219
>Discard candidate functions for which the input types do not match
220
and cannot be converted (using an implicit conversion) to match.
225
assumed to be convertible to anything for this purpose. If only one
226
candidate remains, use it; else continue to the next step.</P
231
>Run through all candidates and keep those with the most exact matches
232
on input types. (Domains are considered the same as their base type
233
for this purpose.) Keep all candidates if none have exact matches.
234
If only one candidate remains, use it; else continue to the next step.</P
239
>Run through all candidates and keep those that accept preferred types (of the
240
input data type's type category) at the most positions where type conversion
242
Keep all candidates if none accept preferred types.
243
If only one candidate remains, use it; else continue to the next step.</P
248
>If any input arguments are <TT
251
>, check the type categories
253
at those argument positions by the remaining candidates. At each position,
257
> category if any candidate accepts that category.
258
(This bias towards string
259
is appropriate since an unknown-type literal looks like a string.)
260
Otherwise, if all the remaining candidates accept the same type category,
261
select that category; otherwise fail because
262
the correct choice cannot be deduced without more clues.
263
Now discard candidates that do not accept the selected type category.
264
Furthermore, if any candidate accepts a preferred type in that category,
265
discard candidates that accept non-preferred types for that argument.
266
Keep all candidates if none survive these tests.
267
If only one candidate remains, use it; else continue to the next step.</P
272
>If there are both <TT
275
> and known-type arguments, and all
276
the known-type arguments have the same type, assume that the
280
> arguments are also of that type, and check which
281
candidates can accept that type at the <TT
285
positions. If exactly one candidate passes this test, use it.
296
> rules are identical for operator and
297
function type resolution.
298
Some examples follow.</P
306
>Example 10-5. Rounding Function Argument Type Resolution</B
309
>There is only one <CODE
312
> function that takes two
313
arguments; it takes a first argument of type <TT
317
a second argument of type <TT
321
So the following query automatically converts
322
the first argument of type <TT
341
That query is actually transformed by the parser to:
344
>SELECT round(CAST (4 AS numeric), 4);</PRE
347
>Since numeric constants with decimal points are initially assigned the
351
>, the following query will require no type
352
conversion and therefore might be slightly more efficient:
355
>SELECT round(4.0, 4);</PRE
365
>Example 10-6. Substring Function Type Resolution</B
368
>There are several <CODE
371
> functions, one of which
379
with a string constant of unspecified type, the system chooses the
380
candidate function that accepts an argument of the preferred category
384
> (namely of type <TT
391
>SELECT substr('1234', 3);
399
>If the string is declared to be of type <TT
402
>, as might be the case
403
if it comes from a table, then the parser will try to convert it to become <TT
409
>SELECT substr(varchar '1234', 3);
417
This is transformed by the parser to effectively become:
420
>SELECT substr(CAST (varchar '1234' AS text), 3);</PRE
430
>The parser learns from the <TT
441
are binary-compatible, meaning that one can be passed to a function that
442
accepts the other without doing any physical conversion. Therefore, no
443
type conversion call is really inserted in this case.</P
448
>And, if the function is called with an argument of type <TT
452
the parser will try to convert that to <TT
458
>SELECT substr(1234, 3);
459
ERROR: function substr(integer, integer) does not exist
460
HINT: No function matches the given name and argument types. You might need
461
to add explicit type casts.</PRE
464
This does not work because <TT
467
> does not have an implicit cast
471
>. An explicit cast will work, however:
474
>SELECT substr(CAST (1234 AS text), 3);
497
HREF="typeconv-func.html#AEN21298"
508
> The reason for this step is to support function-style cast specifications
509
in cases where there is not an actual cast function. If there is a cast
510
function, it is conventionally named after its output type, and so there
511
is no need to have a special case. See
513
HREF="sql-createcast.html"
516
for additional commentary.
526
SUMMARY="Footer navigation table"
537
HREF="typeconv-oper.html"
555
HREF="typeconv-query.html"
b'\\ No newline at end of file'