@@ -185,7 +185,9 @@ That is, if a query is well-formed and the types already match, then the query s
185185without spending extra time in the parser and without introducing unnecessary implicit conversion
186186calls in the query.
187187</para>
188+ </listitem>
188189
190+ <listitem>
189191<para>
190192Additionally, if a query usually requires an implicit conversion for a function, and
191193if then the user defines a new function with the correct argument types, the parser
@@ -209,15 +211,15 @@ should use this new function and no longer do implicit conversion to use the old
209211 The specific operator that is referenced by an operator expression
210212 is determined using the following procedure.
211213 Note that this procedure is indirectly affected
212- by the precedence of the involved operators, since that will determine
214+ by the precedence of the operators involved , since that will determine
213215 which sub-expressions are taken to be the inputs of which operators.
214216 See <xref linkend="sql-precedence"> for more information.
215217 </para>
216218
217219<procedure>
218220<title>Operator Type Resolution</title>
219221
220- <step performance="required">
222+ <step id="op-resol-select" performance="required">
221223<para>
222224Select the operators to be considered from the
223225<classname>pg_operator</classname> system catalog. If a non-schema-qualified
@@ -240,26 +242,33 @@ search path position.
240242</substeps>
241243</step>
242244
243- <step performance="required">
245+ <step id="op-resol-exact-match" performance="required">
244246<para>
245247Check for an operator accepting exactly the input argument types.
246248If one exists (there can be only one exact match in the set of
247249operators considered), use it.
248250</para>
249251
250252<substeps>
251- <step performance="optional">
253+ <step id="op-resol-exact-unknown" performance="optional">
252254<para>
253255If one argument of a binary operator invocation is of the <type>unknown</type> type,
254256then assume it is the same type as the other argument for this check.
255257Invocations involving two <type>unknown</type> inputs, or a unary operator
256258with an <type>unknown</type> input, will never find a match at this step.
257259</para>
258260</step>
261+ <step id="op-resol-exact-domain" performance="optional">
262+ <para>
263+ If one argument of a binary operator invocation is of the <type>unknown</type>
264+ type and the other is of a domain type, next check to see if there is an
265+ operator accepting exactly the domain's base type on both sides; if so, use it.
266+ </para>
267+ </step>
259268</substeps>
260269</step>
261270
262- <step performance="required">
271+ <step id="op-resol-best-match" performance="required">
263272<para>
264273Look for the best match.
265274</para>
@@ -275,9 +284,15 @@ candidate remains, use it; else continue to the next step.
275284</step>
276285<step performance="required">
277286<para>
287+ If any input argument is of a domain type, treat it as being of the
288+ domain's base type for all subsequent steps. This ensures that domains
289+ act like their base types for purposes of ambiguous-operator resolution.
290+ </para>
291+ </step>
292+ <step performance="required">
293+ <para>
278294Run through all candidates and keep those with the most exact matches
279- on input types. (Domains are considered the same as their base type
280- for this purpose.) Keep all candidates if none have exact matches.
295+ on input types. Keep all candidates if none have exact matches.
281296If only one candidate remains, use it; else continue to the next step.
282297</para>
283298</step>
@@ -308,7 +323,7 @@ Keep all candidates if none survive these tests.
308323If only one candidate remains, use it; else continue to the next step.
309324</para>
310325</step>
311- <step performance="required">
326+ <step id="op-resol-last-unknown" performance="required">
312327<para>
313328If there are both <type>unknown</type> and known-type arguments, and all
314329the known-type arguments have the same type, assume that the
@@ -476,7 +491,8 @@ array inclusion (<type>anyarray</> <literal><@</> <type>anyarray</>)
476491and range inclusion (<type>anyelement</> <literal><@</> <type>anyrange</>).
477492Since none of these polymorphic pseudo-types (see <xref
478493linkend="datatype-pseudo">) are considered preferred, the parser cannot
479- resolve the ambiguity on that basis. However, the last resolution rule tells
494+ resolve the ambiguity on that basis.
495+ However, <xref linkend="op-resol-last-unknown"> tells
480496it to assume that the unknown-type literal is of the same type as the other
481497input, that is, integer array. Now only one of the two operators can match,
482498so array inclusion is selected. (Had range inclusion been selected, we would
@@ -485,6 +501,45 @@ a range literal.)
485501</para>
486502</example>
487503
504+ <example>
505+ <title>Custom Operator on a Domain Type</title>
506+
507+ <para>
508+ Users sometimes try to declare operators applying just to a domain type.
509+ This is possible but is not nearly as useful as it might seem, because the
510+ operator resolution rules are designed to select operators applying to the
511+ domain's base type. As an example consider
512+ <screen>
513+ CREATE DOMAIN mytext AS text CHECK(...);
514+ CREATE FUNCTION mytext_eq_text (mytext, text) RETURNS boolean AS ...;
515+ CREATE OPERATOR = (procedure=mytext_eq_text, leftarg=mytext, rightarg=text);
516+ CREATE TABLE mytable (val mytext);
517+
518+ SELECT * FROM mytable WHERE val = 'foo';
519+ </screen>
520+ This query will not use the custom operator. The parser will first see if
521+ there is a <type>mytext</> <literal>=</> <type>mytext</> operator
522+ (<xref linkend="op-resol-exact-unknown">), which there is not;
523+ then it will consider the domain's base type <type>text</>, and see if
524+ there is a <type>text</> <literal>=</> <type>text</> operator
525+ (<xref linkend="op-resol-exact-domain">), which there is;
526+ so it resolves the <type>unknown</>-type literal as <type>text</> and
527+ uses the <type>text</> <literal>=</> <type>text</> operator.
528+ The only way to get the custom operator to be used is to explicitly cast
529+ the literal:
530+ <screen>
531+ SELECT * FROM mytable WHERE val = text 'foo';
532+ </screen>
533+ so that the <type>mytext</> <literal>=</> <type>text</> operator is found
534+ immediately according to the exact-match rule. If the best-match rules
535+ are reached, they actively discriminate against operators on domain types.
536+ If they did not, such an operator would create too many ambiguous-operator
537+ failures, because the casting rules always consider a domain as castable
538+ to or from its base type, and so the domain operator would be considered
539+ usable in all the same cases as a similarly-named operator on the base type.
540+ </para>
541+ </example>
542+
488543</sect1>
489544
490545<sect1 id="typeconv-func">
@@ -600,9 +655,15 @@ candidate remains, use it; else continue to the next step.
600655</step>
601656<step performance="required">
602657<para>
658+ If any input argument is of a domain type, treat it as being of the
659+ domain's base type for all subsequent steps. This ensures that domains
660+ act like their base types for purposes of ambiguous-function resolution.
661+ </para>
662+ </step>
663+ <step performance="required">
664+ <para>
603665Run through all candidates and keep those with the most exact matches
604- on input types. (Domains are considered the same as their base type
605- for this purpose.) Keep all candidates if none have exact matches.
666+ on input types. Keep all candidates if none have exact matches.
606667If only one candidate remains, use it; else continue to the next step.
607668</para>
608669</step>
@@ -898,8 +959,23 @@ and Related Constructs</title>
898959<step performance="required">
899960<para>
900961If all inputs are of the same type, and it is not <type>unknown</type>,
901- resolve as that type. Otherwise, replace any domain types in the list with
902- their underlying base types.
962+ resolve as that type.
963+ </para>
964+ </step>
965+
966+ <step performance="required">
967+ <para>
968+ If any input is of a domain type, treat it as being of the
969+ domain's base type for all subsequent steps.
970+ <footnote>
971+ <para>
972+ Somewhat like the treatment of domain inputs for operators and
973+ functions, this behavior allows a domain type to be preserved through
974+ a <literal>UNION</> or similar construct, so long as the user is
975+ careful to ensure that all inputs are implicitly or explicitly of that
976+ exact type. Otherwise the domain's base type will be preferred.
977+ </para>
978+ </footnote>
903979</para>
904980</step>
905981
0 commit comments