@@ -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 theinvolved 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
+ <stepid="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
+ <stepid="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
+ <stepid="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
+ <stepid="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>
@@ -376,7 +391,7 @@ be interpreted as type <type>text</type>.
376
391
</para>
377
392
378
393
<para>
379
- Here is a concatenationon unspecified types:
394
+ Here is a concatenationof two values of unspecified types:
380
395
<screen>
381
396
SELECT 'abc' || 'def' AS "unspecified";
382
397
@@ -394,7 +409,7 @@ and finds that there are candidates accepting both string-category and
394
409
bit-string-category inputs. Since string category is preferred when available,
395
410
that category is selected, and then the
396
411
preferred type for strings, <type>text</type>, is used as the specific
397
- type to resolve the unknown literals as.
412
+ type to resolve the unknown-type literals as.
398
413
</para>
399
414
</example>
400
415
@@ -450,6 +465,45 @@ SELECT ~ CAST('20' AS int8) AS "negation";
450
465
</para>
451
466
</example>
452
467
468
+ <example>
469
+ <title>Custom Operator on a Domain Type</title>
470
+
471
+ <para>
472
+ Users sometimes try to declare operators applying just to a domain type.
473
+ This is possible but is not nearly as useful as it might seem, because the
474
+ operator resolution rules are designed to select operators applying to the
475
+ domain's base type. As an example consider
476
+ <screen>
477
+ CREATE DOMAIN mytext AS text CHECK(...);
478
+ CREATE FUNCTION mytext_eq_text (mytext, text) RETURNS boolean AS ...;
479
+ CREATE OPERATOR = (procedure=mytext_eq_text, leftarg=mytext, rightarg=text);
480
+ CREATE TABLE mytable (val mytext);
481
+
482
+ SELECT * FROM mytable WHERE val = 'foo';
483
+ </screen>
484
+ This query will not use the custom operator. The parser will first see if
485
+ there is a <type>mytext</> <literal>=</> <type>mytext</> operator
486
+ (<xref linkend="op-resol-exact-unknown">), which there is not;
487
+ then it will consider the domain's base type <type>text</>, and see if
488
+ there is a <type>text</> <literal>=</> <type>text</> operator
489
+ (<xref linkend="op-resol-exact-domain">), which there is;
490
+ so it resolves the <type>unknown</>-type literal as <type>text</> and
491
+ uses the <type>text</> <literal>=</> <type>text</> operator.
492
+ The only way to get the custom operator to be used is to explicitly cast
493
+ the literal:
494
+ <screen>
495
+ SELECT * FROM mytable WHERE val = text 'foo';
496
+ </screen>
497
+ so that the <type>mytext</> <literal>=</> <type>text</> operator is found
498
+ immediately according to the exact-match rule. If the best-match rules
499
+ are reached, they actively discriminate against operators on domain types.
500
+ If they did not, such an operator would create too many ambiguous-operator
501
+ failures, because the casting rules always consider a domain as castable
502
+ to or from its base type, and so the domain operator would be considered
503
+ usable in all the same cases as a similarly-named operator on the base type.
504
+ </para>
505
+ </example>
506
+
453
507
</sect1>
454
508
455
509
<sect1 id="typeconv-func">
@@ -565,9 +619,15 @@ candidate remains, use it; else continue to the next step.
565
619
</step>
566
620
<step performance="required">
567
621
<para>
622
+ If any input argument is of a domain type, treat it as being of the
623
+ domain's base type for all subsequent steps. This ensures that domains
624
+ act like their base types for purposes of ambiguous-function resolution.
625
+ </para>
626
+ </step>
627
+ <step performance="required">
628
+ <para>
568
629
Run through all candidates and keep those with the most exact matches
569
- on input types. (Domains are considered the same as their base type
570
- for this purpose.) Keep all candidates if none have exact matches.
630
+ on input types. Keep all candidates if none have exact matches.
571
631
If only one candidate remains, use it; else continue to the next step.
572
632
</para>
573
633
</step>
@@ -858,8 +918,23 @@ and Related Constructs</title>
858
918
<step performance="required">
859
919
<para>
860
920
If all inputs are of the same type, and it is not <type>unknown</type>,
861
- resolve as that type. Otherwise, replace any domain types in the list with
862
- their underlying base types.
921
+ resolve as that type.
922
+ </para>
923
+ </step>
924
+
925
+ <step performance="required">
926
+ <para>
927
+ If any input is of a domain type, treat it as being of the
928
+ domain's base type for all subsequent steps.
929
+ <footnote>
930
+ <para>
931
+ Somewhat like the treatment of domain inputs for operators and
932
+ functions, this behavior allows a domain type to be preserved through
933
+ a <literal>UNION</> or similar construct, so long as the user is
934
+ careful to ensure that all inputs are implicitly or explicitly of that
935
+ exact type. Otherwise the domain's base type will be preferred.
936
+ </para>
937
+ </footnote>
863
938
</para>
864
939
</step>
865
940