Functions
Function Type Resolution
- Select the functions to be considered from the pg_proc system catalog. If a non-schema-qualified function name was used, the functions in the current search path are considered. If a qualified function name was given, only functions in the specified schema are considered.
If the search path finds multiple functions of different argument types, a proper function in the path is considered.
- Check for a function accepting exactly the input argument types. If the function exists, use it. Cases involving unknown will never find a match at this step.
- If no exact match is found, see if the function call appears to be a special type conversion request.
- Look for the best match.
- Discard candidate functions for which the input types do not match and cannot be converted (using an implicit conversion) to match. unknown literals are assumed to be convertible to anything for this purpose. If only one candidate remains, use it; else continue to the next step.
- 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 has exact matches. If only one candidate remains, use it; else continue to the next step.
- Run through all candidates and keep those that accept preferred types at the most positions where type conversion will be required. Keep all candidates if none accepts preferred types. If only one candidate remains, use it; else continue to the next step.
- If any input arguments are of unknown types, check the type categories accepted at those argument positions by the remaining candidates. At each position, select the string category if any candidate accepts that category. (This bias towards string is appropriate since an unknown-type literal looks 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 in that category, discard candidates that accept non-preferred types for that argument. Keep all candidates if none survives these tests. If only one candidate remains, use it; else continue to the next step.
- If there are both unknown and known-type arguments, and all the known-type arguments have the same type, assume that the unknown arguments are also of that type, and check which candidates can accept that type at the unknown-argument positions. If exactly one candidate passes this test, use it. Otherwise, fail.
Examples
Example 1: Use the rounding function argument type resolution as the first example. There is only one round function that takes two arguments; it takes a first argument of type numeric and a second argument of type integer. So the following query automatically converts the first argument of type integer to numeric:
1 2 3 4 5 |
openGauss=# SELECT round(4, 4); round -------- 4.0000 (1 row) |
That query is actually transformed by the parser to:
1
|
openGauss=# SELECT round(CAST (4 AS numeric), 4); |
Since numeric constants with decimal points are initially assigned the type numeric, the following query will require no type conversion and therefore might be slightly more efficient:
1
|
openGauss=# SELECT round(4.0, 4); |
Example 2: Use the substring function type resolution as the second example. There are several substr functions, one of which takes types text and integer. If called with a string constant of unspecified type, the system chooses the candidate function that accepts an argument of the preferred category string (namely of type text).
1 2 3 4 5 |
openGauss=# SELECT substr('1234', 3); substr -------- 34 (1 row) |
If the string is declared to be of type varchar, as might be the case if it comes from a table, then the parser will try to convert it to become text:
1 2 3 4 5 |
openGauss=# SELECT substr(varchar '1234', 3); substr -------- 34 (1 row) |
This is transformed by the parser to effectively become:
1
|
openGauss=# SELECT substr(CAST (varchar '1234' AS text), 3); |
The parser learns from the pg_cast catalog that text and varchar are binary-compatible, meaning that one can be passed to a function that accepts the other without doing any physical conversion. Therefore, no type conversion is really inserted in this case.
And, if the function is called with an argument of type integer, the parser will try to convert that to text:
1 2 3 4 5 |
openGauss=# SELECT substr(1234, 3); substr -------- 34 (1 row) |
This is transformed by the parser to effectively become:
1 2 3 4 5 |
openGauss=# SELECT substr(CAST (1234 AS text), 3); substr -------- 34 (1 row) |
Feedback
Was this page helpful?
Provide feedbackThank you very much for your feedback. We will continue working to improve the documentation.See the reply and handling status in My Cloud VOC.
For any further questions, feel free to contact us through the chatbot.
Chatbot