forked from docs/doc-exports
Reviewed-by: Pruthi, Vineet <vineet.pruthi@t-systems.com> Co-authored-by: Lu, Huayi <luhuayi@huawei.com> Co-committed-by: Lu, Huayi <luhuayi@huawei.com>
98 lines
15 KiB
HTML
98 lines
15 KiB
HTML
<a name="EN-US_TOPIC_0000001233430141"></a><a name="EN-US_TOPIC_0000001233430141"></a>
|
|
|
|
<h1 class="topictitle1">Functions</h1>
|
|
<div id="body8662426"><div class="section" id="EN-US_TOPIC_0000001233430141__s4608fb91376045baace30cd93134844a"><h4 class="sectiontitle"><strong id="EN-US_TOPIC_0000001233430141__b20205355321945">Function Type Resolution</strong></h4><ol id="EN-US_TOPIC_0000001233430141__of54a2f75e2084cfcb4a37ca78a447219"><li id="EN-US_TOPIC_0000001233430141__ldc39edeaf0304a47978b18f4cc7fecfe">Select the functions to be considered from the <strong id="EN-US_TOPIC_0000001233430141__b1635015449114">PG_PROC</strong> 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.<p id="EN-US_TOPIC_0000001233430141__a3485d2fa843e40699a9ebb395265f79b">If the search path finds multiple functions of different argument types, a proper function in the path is considered.</p>
|
|
</li><li id="EN-US_TOPIC_0000001233430141__lc94f3b17a00a424ba188f9125a46ef2d">Check for a function accepting exactly the input argument types. If the function exists, use it. Cases involving <strong id="EN-US_TOPIC_0000001233430141__b84235270615515">unknown</strong> will never find a match at this step.</li><li id="EN-US_TOPIC_0000001233430141__lca649359a4994a03853ab42f6286697b">If no exact match is found, see if the function call appears to be a special type conversion request.</li><li id="EN-US_TOPIC_0000001233430141__lb23c6180bf0f4b959c4317cb80dd6cd3">Look for the best match.<ol type="a" id="EN-US_TOPIC_0000001233430141__o0a4f8d4ee7e8461280c42648cb334b55"><li id="EN-US_TOPIC_0000001233430141__l23f01b78d8a34ab98ea2ed72c98a8097">Discard candidate functions for which the input types do not match and cannot be converted (using an implicit conversion) to match. <strong id="EN-US_TOPIC_0000001233430141__b842352706122954">unknown</strong> literals are assumed to be convertible to anything for this purpose. If only one candidate remains, use it; else continue to the next step.</li><li id="EN-US_TOPIC_0000001233430141__la03694be1fc14e4481f041bb4b1c6f21">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.</li><li id="EN-US_TOPIC_0000001233430141__l4d4f0ab0edc04ab4af121000c2c9c520">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.</li><li id="EN-US_TOPIC_0000001233430141__l6083f5cfd3994ab08fec08a25ebf7627">If any input arguments are of <strong id="EN-US_TOPIC_0000001233430141__b842352706151357">unknown</strong> 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.</li><li id="EN-US_TOPIC_0000001233430141__l5471329bc88e4aa1b4458fdc7eed5d56">If there are both <strong id="EN-US_TOPIC_0000001233430141__b842352706123559">unknown</strong> and known-type arguments, and all the known-type arguments have the same type, assume that the <strong id="EN-US_TOPIC_0000001233430141__b842352706123619">unknown</strong> arguments are also of that type, and check which candidates can accept that type at the <strong id="EN-US_TOPIC_0000001233430141__b842352706151947">unknown</strong>-argument positions. If exactly one candidate passes this test, use it. Otherwise, fail.</li></ol>
|
|
</li></ol>
|
|
</div>
|
|
<div class="section" id="EN-US_TOPIC_0000001233430141__sf21205f9e86e482db5ef1f8707ab0b81"><h4 class="sectiontitle">Examples</h4><p id="EN-US_TOPIC_0000001233430141__a1965eeadeaeb474a918f2ce07b4c33f4">Example 1: Use the rounding function argument type resolution as the first example. There is only one <strong id="EN-US_TOPIC_0000001233430141__b842352706162518">round</strong> function that takes two arguments; it takes a first argument of type <strong id="EN-US_TOPIC_0000001233430141__b842352706162458">numeric</strong> and a second argument of type <strong id="EN-US_TOPIC_0000001233430141__b84235270616258">integer</strong>. So the following query automatically converts the first argument of type <strong id="EN-US_TOPIC_0000001233430141__b198160369121945">integer</strong> to <strong id="EN-US_TOPIC_0000001233430141__b5199225421945">numeric</strong>:</p>
|
|
<div class="codecoloring" codetype="Sql" id="EN-US_TOPIC_0000001233430141__s871c0b8b85e048e798990c4eb9b6bfb5"><div class="highlight"><table class="highlighttable"><tr><td class="linenos"><div class="linenodiv"><pre><span class="normal">1</span>
|
|
<span class="normal">2</span>
|
|
<span class="normal">3</span>
|
|
<span class="normal">4</span>
|
|
<span class="normal">5</span></pre></div></td><td class="code"><div><pre><span></span><span class="k">SELECT</span><span class="w"> </span><span class="n">round</span><span class="p">(</span><span class="mi">4</span><span class="p">,</span><span class="w"> </span><span class="mi">4</span><span class="p">);</span>
|
|
<span class="w"> </span><span class="n">round</span>
|
|
<span class="c1">--------</span>
|
|
<span class="w"> </span><span class="mi">4</span><span class="p">.</span><span class="mi">0000</span>
|
|
<span class="p">(</span><span class="mi">1</span><span class="w"> </span><span class="k">row</span><span class="p">)</span>
|
|
</pre></div></td></tr></table></div>
|
|
|
|
</div>
|
|
<p id="EN-US_TOPIC_0000001233430141__a2cfc62b91e0d4eed84a7741c46d86cea">That query is converted by the parser to:</p>
|
|
<div class="codecoloring" codetype="Sql" id="EN-US_TOPIC_0000001233430141__sa58a201ef823404dab3945bcb95e3b69"><div class="highlight"><table class="highlighttable"><tr><td class="linenos"><div class="linenodiv"><pre><span class="normal">1</span></pre></div></td><td class="code"><div><pre><span></span><span class="k">SELECT</span><span class="w"> </span><span class="n">round</span><span class="p">(</span><span class="k">CAST</span><span class="w"> </span><span class="p">(</span><span class="mi">4</span><span class="w"> </span><span class="k">AS</span><span class="w"> </span><span class="nb">numeric</span><span class="p">),</span><span class="w"> </span><span class="mi">4</span><span class="p">);</span>
|
|
</pre></div></td></tr></table></div>
|
|
|
|
</div>
|
|
<p id="EN-US_TOPIC_0000001233430141__a50531605c8214824a1ff995d135340fd">Since numeric constants with decimal points are initially assigned the type <strong id="EN-US_TOPIC_0000001233430141__b197437546821945">numeric</strong>, the following query will require no type conversion and therefore might be slightly more efficient:</p>
|
|
<div class="codecoloring" codetype="Sql" id="EN-US_TOPIC_0000001233430141__s35613437a8614b28b63885617146c805"><div class="highlight"><table class="highlighttable"><tr><td class="linenos"><div class="linenodiv"><pre><span class="normal">1</span></pre></div></td><td class="code"><div><pre><span></span><span class="k">SELECT</span><span class="w"> </span><span class="n">round</span><span class="p">(</span><span class="mi">4</span><span class="p">.</span><span class="mi">0</span><span class="p">,</span><span class="w"> </span><span class="mi">4</span><span class="p">);</span>
|
|
</pre></div></td></tr></table></div>
|
|
|
|
</div>
|
|
<p id="EN-US_TOPIC_0000001233430141__ac91b3dc1edba4688939332129bc5081d">Example 2: Use the substring function type resolution as the second example. There are several <strong id="EN-US_TOPIC_0000001233430141__b84235270616284">substr</strong> functions, one of which takes types <strong id="EN-US_TOPIC_0000001233430141__b842352706162811">text</strong> and <strong id="EN-US_TOPIC_0000001233430141__b842352706162815">integer</strong>. If called with a string constant of unspecified type, the system chooses the candidate function that accepts an argument of the preferred category <strong id="EN-US_TOPIC_0000001233430141__b72159267121945">string</strong> (namely of type <strong id="EN-US_TOPIC_0000001233430141__b180354385921945">text</strong>). </p>
|
|
<div class="codecoloring" codetype="Sql" id="EN-US_TOPIC_0000001233430141__saddf0f3219934b8791c74c5c33907f0c"><div class="highlight"><table class="highlighttable"><tr><td class="linenos"><div class="linenodiv"><pre><span class="normal">1</span>
|
|
<span class="normal">2</span>
|
|
<span class="normal">3</span>
|
|
<span class="normal">4</span>
|
|
<span class="normal">5</span></pre></div></td><td class="code"><div><pre><span></span><span class="k">SELECT</span><span class="w"> </span><span class="n">substr</span><span class="p">(</span><span class="s1">'1234'</span><span class="p">,</span><span class="w"> </span><span class="mi">3</span><span class="p">);</span>
|
|
<span class="w"> </span><span class="n">substr</span>
|
|
<span class="c1">--------</span>
|
|
<span class="w"> </span><span class="mi">34</span>
|
|
<span class="p">(</span><span class="mi">1</span><span class="w"> </span><span class="k">row</span><span class="p">)</span>
|
|
</pre></div></td></tr></table></div>
|
|
|
|
</div>
|
|
<p id="EN-US_TOPIC_0000001233430141__af214be4b554b4e4a8d8476d132a8139c">If the string is declared to be of type <strong id="EN-US_TOPIC_0000001233430141__b32940488321945">varchar</strong>, as might be the case if it comes from a table, then the parser will try to convert it to become <strong id="EN-US_TOPIC_0000001233430141__b146615892621945">text</strong>:</p>
|
|
<div class="codecoloring" codetype="Sql" id="EN-US_TOPIC_0000001233430141__s44a3ca5982c849d7b4f36d5535ac27ff"><div class="highlight"><table class="highlighttable"><tr><td class="linenos"><div class="linenodiv"><pre><span class="normal">1</span>
|
|
<span class="normal">2</span>
|
|
<span class="normal">3</span>
|
|
<span class="normal">4</span>
|
|
<span class="normal">5</span></pre></div></td><td class="code"><div><pre><span></span><span class="k">SELECT</span><span class="w"> </span><span class="n">substr</span><span class="p">(</span><span class="nb">varchar</span><span class="w"> </span><span class="s1">'1234'</span><span class="p">,</span><span class="w"> </span><span class="mi">3</span><span class="p">);</span>
|
|
<span class="w"> </span><span class="n">substr</span>
|
|
<span class="c1">--------</span>
|
|
<span class="w"> </span><span class="mi">34</span>
|
|
<span class="p">(</span><span class="mi">1</span><span class="w"> </span><span class="k">row</span><span class="p">)</span>
|
|
</pre></div></td></tr></table></div>
|
|
|
|
</div>
|
|
<p id="EN-US_TOPIC_0000001233430141__aa67fa9b5ab374b218ceed93f266659bd">This is transformed by the parser to effectively become:</p>
|
|
<div class="codecoloring" codetype="Sql" id="EN-US_TOPIC_0000001233430141__s0e460f7b339b43e4ba35b1fcd98695d8"><div class="highlight"><table class="highlighttable"><tr><td class="linenos"><div class="linenodiv"><pre><span class="normal">1</span></pre></div></td><td class="code"><div><pre><span></span><span class="k">SELECT</span><span class="w"> </span><span class="n">substr</span><span class="p">(</span><span class="k">CAST</span><span class="w"> </span><span class="p">(</span><span class="nb">varchar</span><span class="w"> </span><span class="s1">'1234'</span><span class="w"> </span><span class="k">AS</span><span class="w"> </span><span class="nb">text</span><span class="p">),</span><span class="w"> </span><span class="mi">3</span><span class="p">);</span>
|
|
</pre></div></td></tr></table></div>
|
|
|
|
</div>
|
|
<div class="note" id="EN-US_TOPIC_0000001233430141__nfbd51094707e498ea3f559d1e2ea3fba"><img src="public_sys-resources/note_3.0-en-us.png"><span class="notetitle"> </span><div class="notebody"><p id="EN-US_TOPIC_0000001233430141__a043eeec66c0248389ec67f45bd6cdb12">The parser learns from the <strong id="EN-US_TOPIC_0000001233430141__b16894184401212">PG_CAST</strong> 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 inserted in this case.</p>
|
|
</div></div>
|
|
<p id="EN-US_TOPIC_0000001233430141__a21b5b1366dbd40348497e6af7764db59">And, if the function is called with an argument of type <strong id="EN-US_TOPIC_0000001233430141__b14481113321945">integer</strong>, the parser will try to convert that to <strong id="EN-US_TOPIC_0000001233430141__b173950146521945">text</strong>:</p>
|
|
<div class="codecoloring" codetype="Sql" id="EN-US_TOPIC_0000001233430141__sb9f74012a18d40e18bfbbb016018d069"><div class="highlight"><table class="highlighttable"><tr><td class="linenos"><div class="linenodiv"><pre><span class="normal">1</span>
|
|
<span class="normal">2</span>
|
|
<span class="normal">3</span>
|
|
<span class="normal">4</span>
|
|
<span class="normal">5</span></pre></div></td><td class="code"><div><pre><span></span><span class="k">SELECT</span><span class="w"> </span><span class="n">substr</span><span class="p">(</span><span class="mi">1234</span><span class="p">,</span><span class="w"> </span><span class="mi">3</span><span class="p">);</span>
|
|
<span class="n">substr</span>
|
|
<span class="c1">--------</span>
|
|
<span class="w"> </span><span class="mi">34</span>
|
|
<span class="p">(</span><span class="mi">1</span><span class="w"> </span><span class="k">row</span><span class="p">)</span>
|
|
</pre></div></td></tr></table></div>
|
|
|
|
</div>
|
|
<p id="EN-US_TOPIC_0000001233430141__a0d7ebee91ea345fa9a34d78f4fe845c1">This is transformed by the parser to effectively become:</p>
|
|
<div class="codecoloring" codetype="Sql" id="EN-US_TOPIC_0000001233430141__s5f5d0f49ba5945b3971b53305c57918d"><div class="highlight"><table class="highlighttable"><tr><td class="linenos"><div class="linenodiv"><pre><span class="normal">1</span>
|
|
<span class="normal">2</span>
|
|
<span class="normal">3</span>
|
|
<span class="normal">4</span>
|
|
<span class="normal">5</span></pre></div></td><td class="code"><div><pre><span></span><span class="k">SELECT</span><span class="w"> </span><span class="n">substr</span><span class="p">(</span><span class="k">CAST</span><span class="w"> </span><span class="p">(</span><span class="mi">1234</span><span class="w"> </span><span class="k">AS</span><span class="w"> </span><span class="nb">text</span><span class="p">),</span><span class="w"> </span><span class="mi">3</span><span class="p">);</span>
|
|
<span class="w"> </span><span class="n">substr</span>
|
|
<span class="c1">--------</span>
|
|
<span class="w"> </span><span class="mi">34</span>
|
|
<span class="p">(</span><span class="mi">1</span><span class="w"> </span><span class="k">row</span><span class="p">)</span>
|
|
</pre></div></td></tr></table></div>
|
|
|
|
</div>
|
|
</div>
|
|
</div>
|
|
<div>
|
|
<div class="familylinks">
|
|
<div class="parentlink"><strong>Parent topic:</strong> <a href="dws_06_0075.html">Type Conversion</a></div>
|
|
</div>
|
|
</div>
|
|
|