doc-exports/docs/dws/dev/dws_06_0077.html
Lu, Huayi e6fa411af0 DWS DEV 830.201 version
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>
2024-05-16 07:24:04 +00:00

98 lines
16 KiB
HTML

<a name="EN-US_TOPIC_0000001233708695"></a><a name="EN-US_TOPIC_0000001233708695"></a>
<h1 class="topictitle1">Operators</h1>
<div id="body8662426"><div class="section" id="EN-US_TOPIC_0000001233708695__s6316cc6a8bea4039bc9aa13bed01efc8"><h4 class="sectiontitle">Operator Type Resolution</h4><ol id="EN-US_TOPIC_0000001233708695__od90246f04aed40c0b06d71b7624fdbcb"><li id="EN-US_TOPIC_0000001233708695__lb4b9532c5f6549feb9513c0553704478">Select the operators to be considered from the <strong id="EN-US_TOPIC_0000001233708695__b84235270695922">pg_operator</strong> system catalog. Considered operators are those with the matching name and argument count. If the search path finds multiple available operators, only the most suitable one is considered.</li><li id="EN-US_TOPIC_0000001233708695__lf2cf42e08e384d26b5ae9fd2bcdc8268">Look for the best match.<ol type="a" id="EN-US_TOPIC_0000001233708695__ol2040116425254"><li id="EN-US_TOPIC_0000001233708695__li18401542122514"><p id="EN-US_TOPIC_0000001233708695__p140194220257"><a name="EN-US_TOPIC_0000001233708695__li18401542122514"></a><a name="li18401542122514"></a>Discard candidate operators for which the input types do not match and cannot be converted (using an implicit conversion) to match. <strong id="EN-US_TOPIC_0000001233708695__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.</p>
</li><li id="EN-US_TOPIC_0000001233708695__li17401144222512"><p id="EN-US_TOPIC_0000001233708695__p3401642162515"><a name="EN-US_TOPIC_0000001233708695__li17401144222512"></a><a name="li17401144222512"></a>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 there are no exact matches. If only one candidate remains, use it; else continue to the next step.</p>
</li><li id="EN-US_TOPIC_0000001233708695__li9401842172515"><p id="EN-US_TOPIC_0000001233708695__p11401342142518"><a name="EN-US_TOPIC_0000001233708695__li9401842172515"></a><a name="li9401842172515"></a>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 accepts preferred types. If only one candidate remains, use it; else continue to the next step.</p>
</li><li id="EN-US_TOPIC_0000001233708695__li1240120424252"><p id="EN-US_TOPIC_0000001233708695__p20401174210258"><a name="EN-US_TOPIC_0000001233708695__li1240120424252"></a><a name="li1240120424252"></a>If any input arguments are of <strong id="EN-US_TOPIC_0000001233708695__b84235270610844">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.</p>
</li><li id="EN-US_TOPIC_0000001233708695__li14011242172516"><p id="EN-US_TOPIC_0000001233708695__p16401742162510"><a name="EN-US_TOPIC_0000001233708695__li14011242172516"></a><a name="li14011242172516"></a>If there are both <strong id="EN-US_TOPIC_0000001233708695__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_0000001233708695__b842352706123619">unknown</strong> 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, an error is reported.</p>
</li></ol>
</li></ol>
</div>
<div class="section" id="EN-US_TOPIC_0000001233708695__s0a8516bfe4cf48ce86ef9520a5900b6b"><h4 class="sectiontitle">Examples</h4><p id="EN-US_TOPIC_0000001233708695__a435d88f328184691a9b263401e4a9303">Example 1: factorial operator type resolution. There is only one factorial operator (postfix !) defined in the system catalog, and it takes an argument of type <strong id="EN-US_TOPIC_0000001233708695__b842352706124029">bigint</strong>. The scanner assigns an initial type of <strong id="EN-US_TOPIC_0000001233708695__b842352706124227">bigint</strong> to the argument in this query expression:</p>
<div class="codecoloring" codetype="Sql" id="EN-US_TOPIC_0000001233708695__s03f968627371421bbdc66e05b3ddadfe"><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>
<span class="normal">6</span></pre></div></td><td class="code"><div><pre><span></span><span class="k">SELECT</span><span class="w"> </span><span class="mi">40</span><span class="w"> </span><span class="o">!</span><span class="w"> </span><span class="k">AS</span><span class="w"> </span><span class="ss">&quot;40 factorial&quot;</span><span class="p">;</span>
<span class="w"> </span><span class="mi">40</span><span class="w"> </span><span class="n">factorial</span>
<span class="c1">--------------------------------------------------</span>
<span class="w"> </span><span class="mi">815915283247897734345611269596115894272000000000</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_0000001233708695__a14233ddd743f406fb7b37ca2a7e305cc">So the parser does a type conversion on the operand and the query is equivalent to:</p>
<div class="codecoloring" codetype="Sql" id="EN-US_TOPIC_0000001233708695__s39a41f878e8b48cf91c8e70b469a7ee2"><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="k">CAST</span><span class="p">(</span><span class="mi">40</span><span class="w"> </span><span class="k">AS</span><span class="w"> </span><span class="nb">bigint</span><span class="p">)</span><span class="w"> </span><span class="o">!</span><span class="w"> </span><span class="k">AS</span><span class="w"> </span><span class="ss">&quot;40 factorial&quot;</span><span class="p">;</span>
</pre></div></td></tr></table></div>
</div>
<p id="EN-US_TOPIC_0000001233708695__acc69a3f910de47a09f213d0b6c6cda4f">Example 2: string concatenation operator type resolution. A string-like syntax is used for working with string types and for working with complex extension types. Strings with unspecified type are matched with likely operator candidates. An example with one unspecified argument:</p>
<div class="codecoloring" codetype="Sql" id="EN-US_TOPIC_0000001233708695__s919a9e0e5717442b865d66304d371c36"><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="nb">text</span><span class="w"> </span><span class="s1">'abc'</span><span class="w"> </span><span class="o">||</span><span class="w"> </span><span class="s1">'def'</span><span class="w"> </span><span class="k">AS</span><span class="w"> </span><span class="ss">&quot;text and unknown&quot;</span><span class="p">;</span>
<span class="w"> </span><span class="nb">text</span><span class="w"> </span><span class="k">and</span><span class="w"> </span><span class="k">unknown</span>
<span class="c1">------------------</span>
<span class="w"> </span><span class="n">abcdef</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_0000001233708695__aa4d297d0229f4cf293e806daf6900fe9">In this example, the parser looks for an operator whose parameters are of the text type. Such an operator is found.</p>
<p id="EN-US_TOPIC_0000001233708695__a7d276885342146dc9dcb4121583c4cfd">Here is a concatenation of two values of unspecified types:</p>
<div class="codecoloring" codetype="Sql" id="EN-US_TOPIC_0000001233708695__s83995962dc44452192107b35284313a9"><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="s1">'abc'</span><span class="w"> </span><span class="o">||</span><span class="w"> </span><span class="s1">'def'</span><span class="w"> </span><span class="k">AS</span><span class="w"> </span><span class="ss">&quot;unspecified&quot;</span><span class="p">;</span>
<span class="w"> </span><span class="n">unspecified</span>
<span class="c1">-------------</span>
<span class="w"> </span><span class="n">abcdef</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 class="note" id="EN-US_TOPIC_0000001233708695__n3129fbd9797a468cbd6037c9d7ecbd21"><img src="public_sys-resources/note_3.0-en-us.png"><span class="notetitle"> </span><div class="notebody"><p id="EN-US_TOPIC_0000001233708695__a1f470b293b1640059fc68c8da26f6b08">In this case there is no initial hint for which type to use, since no types are specified in the query. So, the parser looks for all candidate operators and finds that there are candidates accepting both string-category and bit-string-category inputs. Since string category is preferred when available, that category is selected, and then the preferred type for strings, <strong id="EN-US_TOPIC_0000001233708695__b842352706144017">text</strong>, is used as the specific type to resolve the unknown-type literals.</p>
</div></div>
<p id="EN-US_TOPIC_0000001233708695__ae25728f8aa1b4348b43b9d44f1c84f16">Example 3: absolute-value and negation operator type resolution. The <span id="EN-US_TOPIC_0000001233708695__text731098051">GaussDB(DWS)</span> operator catalog has several entries for the prefix operator @. All the entries implement absolute-value operations for various numeric data types. One of these entries is for type <strong id="EN-US_TOPIC_0000001233708695__b842352706144237">float8</strong>, which is the preferred type in the numeric category. Therefore, <span id="EN-US_TOPIC_0000001233708695__text1444940381">GaussDB(DWS)</span> will use that entry when faced with an <strong id="EN-US_TOPIC_0000001233708695__en-us_topic_0085032966_en-us_topic_0058966145_b842352706144323">unknown</strong> input: </p>
<div class="codecoloring" codetype="Sql" id="EN-US_TOPIC_0000001233708695__s07fdf266d90146948eba751fa3e409ad"><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="o">@</span><span class="w"> </span><span class="s1">'-4.5'</span><span class="w"> </span><span class="k">AS</span><span class="w"> </span><span class="ss">&quot;abs&quot;</span><span class="p">;</span>
<span class="w"> </span><span class="k">abs</span>
<span class="c1">-----</span>
<span class="w"> </span><span class="mi">4</span><span class="p">.</span><span class="mi">5</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_0000001233708695__a6278344ff904433c9de1dfe4d17e6ea6">Here the system has implicitly resolved the unknown-type literal as type <strong id="EN-US_TOPIC_0000001233708695__b842352706144418">float8</strong> before applying the chosen operator.</p>
<p id="EN-US_TOPIC_0000001233708695__a63e3cce43f0e4f28a4c45b0b5da7004d">Example 4: array inclusion operator type resolution. The following is an example of resolving an operator with one known and one unknown input:</p>
<div class="codecoloring" codetype="Sql" id="EN-US_TOPIC_0000001233708695__sbd882a4df5e84282a037459c7a1c4e13"><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="nb">array</span><span class="p">[</span><span class="mi">1</span><span class="p">,</span><span class="mi">2</span><span class="p">]</span><span class="w"> </span><span class="o">&lt;@</span><span class="w"> </span><span class="s1">'{1,2,3}'</span><span class="w"> </span><span class="k">as</span><span class="w"> </span><span class="ss">&quot;is subset&quot;</span><span class="p">;</span>
<span class="w"> </span><span class="k">is</span><span class="w"> </span><span class="n">subset</span>
<span class="c1">-----------</span>
<span class="w"> </span><span class="n">t</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 class="note" id="EN-US_TOPIC_0000001233708695__nb86bc9be527e4c23877742332a485ea7"><img src="public_sys-resources/note_3.0-en-us.png"><span class="notetitle"> </span><div class="notebody"><p id="EN-US_TOPIC_0000001233708695__aeb8da23e96fd40b0b82d5afe1b10393c">In the <strong id="EN-US_TOPIC_0000001233708695__b13307125520319">pg_operator</strong> table of <span id="EN-US_TOPIC_0000001233708695__text430713550318">GaussDB(DWS)</span>, several entries correspond to the infix operator &lt;@, but the only two that may accept an integer array on the left-hand side are array inclusion (<strong id="EN-US_TOPIC_0000001233708695__b83074553315">anyarray &lt;@ anyarray</strong>) and range inclusion (<strong id="EN-US_TOPIC_0000001233708695__b03087555319">anyelement &lt;@ anyrange</strong>). Because none of these polymorphic pseudo-types (see <a href="dws_06_0023.html">Pseudo-Types</a>) is considered preferred, the parser cannot resolve the ambiguity on that basis. However, <a href="#EN-US_TOPIC_0000001233708695__li14011242172516">2.e</a> tells it to assume that the unknown-type literal is of the same type as the other input, that is, integer array. Now only one of the two operators can match, so array inclusion is selected. (If you select range inclusion, an error will be reported because the string does not have the right format to be a range literal.)</p>
</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>