@@ -185,7 +185,9 @@ That is, if a query is well-formed and the types already match, then the query s
185
185
without spending extra time in the parser and without introducing unnecessary implicit conversion
186
186
calls in the query.
187
187
</para>
188
+ </listitem>
188
189
190
+ <listitem>
189
191
<para>
190
192
Additionally, if a query usually requires an implicit conversion for a function, and
191
193
if 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
209
211
The specific operator that is referenced by an operator expression
210
212
is determined using the following procedure.
211
213
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
213
215
which sub-expressions are taken to be the inputs of which operators.
214
216
See <xref linkend="sql-precedence"> for more information.
215
217
</para>
216
218
217
219
<procedure>
218
220
<title>Operator Type Resolution</title>
219
221
220
- <step performance="required">
222
+ <step id="op-resol-select" performance="required">
221
223
<para>
222
224
Select the operators to be considered from the
223
225
<classname>pg_operator</classname> system catalog. If a non-schema-qualified
@@ -240,26 +242,33 @@ search path position.
240
242
</substeps>
241
243
</step>
242
244
243
- <step performance="required">
245
+ <step id="op-resol-exact-match" performance="required">
244
246
<para>
245
247
Check for an operator accepting exactly the input argument types.
246
248
If one exists (there can be only one exact match in the set of
247
249
operators considered), use it.
248
250
</para>
249
251
250
252
<substeps>
251
- <step performance="optional">
253
+ <step id="op-resol-exact-unknown" performance="optional">
252
254
<para>
253
255
If one argument of a binary operator invocation is of the <type>unknown</type> type,
254
256
then assume it is the same type as the other argument for this check.
255
257
Invocations involving two <type>unknown</type> inputs, or a unary operator
256
258
with an <type>unknown</type> input, will never find a match at this step.
257
259
</para>
258
260
</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>
259
268
</substeps>
260
269
</step>
261
270
262
- <step performance="required">
271
+ <step id="op-resol-best-match" performance="required">
263
272
<para>
264
273
Look for the best match.
265
274
</para>
@@ -275,9 +284,15 @@ candidate remains, use it; else continue to the next step.
275
284
</step>
276
285
<step performance="required">
277
286
<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>
278
294
Run 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.
281
296
If only one candidate remains, use it; else continue to the next step.
282
297
</para>
283
298
</step>
@@ -308,7 +323,7 @@ Keep all candidates if none survive these tests.
308
323
If only one candidate remains, use it; else continue to the next step.
309
324
</para>
310
325
</step>
311
- <step performance="required">
326
+ <step id="op-resol-last-unknown" performance="required">
312
327
<para>
313
328
If there are both <type>unknown</type> and known-type arguments, and all
314
329
the known-type arguments have the same type, assume that the
@@ -476,7 +491,8 @@ array inclusion (<type>anyarray</> <literal><@</> <type>anyarray</>)
476
491
and range inclusion (<type>anyelement</> <literal><@</> <type>anyrange</>).
477
492
Since none of these polymorphic pseudo-types (see <xref
478
493
linkend="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
480
496
it to assume that the unknown-type literal is of the same type as the other
481
497
input, that is, integer array. Now only one of the two operators can match,
482
498
so array inclusion is selected. (Had range inclusion been selected, we would
@@ -485,6 +501,45 @@ a range literal.)
485
501
</para>
486
502
</example>
487
503
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
+
488
543
</sect1>
489
544
490
545
<sect1 id="typeconv-func">
@@ -600,9 +655,15 @@ candidate remains, use it; else continue to the next step.
600
655
</step>
601
656
<step performance="required">
602
657
<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>
603
665
Run 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.
606
667
If only one candidate remains, use it; else continue to the next step.
607
668
</para>
608
669
</step>
@@ -898,8 +959,23 @@ and Related Constructs</title>
898
959
<step performance="required">
899
960
<para>
900
961
If 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>
903
979
</para>
904
980
</step>
905
981
0 commit comments