Sophie

Sophie

distrib > Mageia > 4 > x86_64 > by-pkgid > 977b9e43ddbf791a68788d984b14383d > files > 1173

postgresql9.3-docs-9.3.9-1.mga4.noarch.rpm

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<HTML
><HEAD
><TITLE
>Operators</TITLE
><META
NAME="GENERATOR"
CONTENT="Modular DocBook HTML Stylesheet Version 1.79"><LINK
REV="MADE"
HREF="mailto:pgsql-docs@postgresql.org"><LINK
REL="HOME"
TITLE="PostgreSQL 9.3.9 Documentation"
HREF="index.html"><LINK
REL="UP"
TITLE="Type Conversion"
HREF="typeconv.html"><LINK
REL="PREVIOUS"
TITLE="Overview"
HREF="typeconv-overview.html"><LINK
REL="NEXT"
TITLE="Functions"
HREF="typeconv-func.html"><LINK
REL="STYLESHEET"
TYPE="text/css"
HREF="stylesheet.css"><META
HTTP-EQUIV="Content-Type"
CONTENT="text/html; charset=ISO-8859-1"><META
NAME="creation"
CONTENT="2015-06-13T20:07:22"></HEAD
><BODY
CLASS="SECT1"
><DIV
CLASS="NAVHEADER"
><TABLE
SUMMARY="Header navigation table"
WIDTH="100%"
BORDER="0"
CELLPADDING="0"
CELLSPACING="0"
><TR
><TH
COLSPAN="5"
ALIGN="center"
VALIGN="bottom"
><A
HREF="index.html"
>PostgreSQL 9.3.9 Documentation</A
></TH
></TR
><TR
><TD
WIDTH="10%"
ALIGN="left"
VALIGN="top"
><A
TITLE="Overview"
HREF="typeconv-overview.html"
ACCESSKEY="P"
>Prev</A
></TD
><TD
WIDTH="10%"
ALIGN="left"
VALIGN="top"
><A
HREF="typeconv.html"
ACCESSKEY="U"
>Up</A
></TD
><TD
WIDTH="60%"
ALIGN="center"
VALIGN="bottom"
>Chapter 10. Type Conversion</TD
><TD
WIDTH="20%"
ALIGN="right"
VALIGN="top"
><A
TITLE="Functions"
HREF="typeconv-func.html"
ACCESSKEY="N"
>Next</A
></TD
></TR
></TABLE
><HR
ALIGN="LEFT"
WIDTH="100%"></DIV
><DIV
CLASS="SECT1"
><H1
CLASS="SECT1"
><A
NAME="TYPECONV-OPER"
>10.2. Operators</A
></H1
><P
>   The specific operator that is referenced by an operator expression
   is determined using the following procedure.
   Note that this procedure is indirectly affected
   by the precedence of the operators involved, since that will determine
   which sub-expressions are taken to be the inputs of which operators.
   See <A
HREF="sql-syntax-lexical.html#SQL-PRECEDENCE"
>Section 4.1.6</A
> for more information.
  </P
><DIV
CLASS="PROCEDURE"
><P
><B
>Operator Type Resolution</B
></P
><OL
TYPE="1"
><LI
CLASS="STEP"
><A
NAME="OP-RESOL-SELECT"
></A
><P
>Select the operators to be considered from the
<CODE
CLASS="CLASSNAME"
>pg_operator</CODE
> system catalog.  If a non-schema-qualified
operator name was used (the usual case), the operators
considered are those with the matching name and argument count that are
visible in the current search path (see <A
HREF="ddl-schemas.html#DDL-SCHEMAS-PATH"
>Section 5.7.3</A
>).
If a qualified operator name was given, only operators in the specified
schema are considered.</P
><OL
CLASS="SUBSTEPS"
TYPE="a"
><LI
CLASS="STEP"
><P
>If the search path finds multiple operators with identical argument types,
only the one appearing earliest in the path is considered.  Operators with
different argument types are considered on an equal footing regardless of
search path position.</P
></LI
></OL
></LI
><LI
CLASS="STEP"
><A
NAME="OP-RESOL-EXACT-MATCH"
></A
><P
>Check for an operator accepting exactly the input argument types.
If one exists (there can be only one exact match in the set of
operators considered), use it.</P
><OL
CLASS="SUBSTEPS"
TYPE="a"
><LI
CLASS="STEP"
><A
NAME="OP-RESOL-EXACT-UNKNOWN"
></A
><P
>If one argument of a binary operator invocation is of the <TT
CLASS="TYPE"
>unknown</TT
> type,
then assume it is the same type as the other argument for this check.
Invocations involving two <TT
CLASS="TYPE"
>unknown</TT
> inputs, or a unary operator
with an <TT
CLASS="TYPE"
>unknown</TT
> input, will never find a match at this step.</P
></LI
><LI
CLASS="STEP"
><A
NAME="OP-RESOL-EXACT-DOMAIN"
></A
><P
>If one argument of a binary operator invocation is of the <TT
CLASS="TYPE"
>unknown</TT
>
type and the other is of a domain type, next check to see if there is an
operator accepting exactly the domain's base type on both sides; if so, use it.</P
></LI
></OL
></LI
><LI
CLASS="STEP"
><A
NAME="OP-RESOL-BEST-MATCH"
></A
><P
>Look for the best match.</P
><OL
CLASS="SUBSTEPS"
TYPE="a"
><LI
CLASS="STEP"
><P
>Discard candidate operators for which the input types do not match
and cannot be converted (using an implicit conversion) to match.
<TT
CLASS="TYPE"
>unknown</TT
> 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
CLASS="STEP"
><P
>If any input argument is of a domain type, treat it as being of the
domain's base type for all subsequent steps.  This ensures that domains
act like their base types for purposes of ambiguous-operator resolution.</P
></LI
><LI
CLASS="STEP"
><P
>Run through all candidates and keep those with the most exact matches
on input types.  Keep all candidates if none have exact matches.
If only one candidate remains, use it; else continue to the next step.</P
></LI
><LI
CLASS="STEP"
><P
>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 accept preferred types.
If only one candidate remains, use it; else continue to the next step.</P
></LI
><LI
CLASS="STEP"
><P
>If any input arguments are <TT
CLASS="TYPE"
>unknown</TT
>, check the type
categories accepted at those argument positions by the remaining
candidates.  At each position, select the <TT
CLASS="TYPE"
>string</TT
> 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 survive these tests.
If only one candidate remains, use it; else continue to the next step.</P
></LI
><LI
CLASS="STEP"
><A
NAME="OP-RESOL-LAST-UNKNOWN"
></A
><P
>If there are both <TT
CLASS="TYPE"
>unknown</TT
> and known-type arguments, and all
the known-type arguments have the same type, assume that the
<TT
CLASS="TYPE"
>unknown</TT
> arguments are also of that type, and check which
candidates can accept that type at the <TT
CLASS="TYPE"
>unknown</TT
>-argument
positions.  If exactly one candidate passes this test, use it.
Otherwise, fail.</P
></LI
></OL
></LI
></OL
></DIV
><P
>Some examples follow.</P
><DIV
CLASS="EXAMPLE"
><A
NAME="AEN21355"
></A
><P
><B
>Example 10-1. Factorial Operator Type Resolution</B
></P
><P
>There is only one factorial operator (postfix <TT
CLASS="LITERAL"
>!</TT
>)
defined in the standard catalog, and it takes an argument of type
<TT
CLASS="TYPE"
>bigint</TT
>.
The scanner assigns an initial type of <TT
CLASS="TYPE"
>integer</TT
> to the argument
in this query expression:
</P><PRE
CLASS="SCREEN"
>SELECT 40 ! AS "40 factorial";

                   40 factorial
--------------------------------------------------
 815915283247897734345611269596115894272000000000
(1 row)</PRE
><P>

So the parser does a type conversion on the operand and the query
is equivalent to:

</P><PRE
CLASS="SCREEN"
>SELECT CAST(40 AS bigint) ! AS "40 factorial";</PRE
><P></P
></DIV
><DIV
CLASS="EXAMPLE"
><A
NAME="AEN21363"
></A
><P
><B
>Example 10-2. String Concatenation Operator Type Resolution</B
></P
><P
>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.</P
><P
>An example with one unspecified argument:
</P><PRE
CLASS="SCREEN"
>SELECT text 'abc' || 'def' AS "text and unknown";

 text and unknown
------------------
 abcdef
(1 row)</PRE
><P></P
><P
>In this case the parser looks to see if there is an operator taking <TT
CLASS="TYPE"
>text</TT
>
for both arguments. Since there is, it assumes that the second argument should
be interpreted as type <TT
CLASS="TYPE"
>text</TT
>.</P
><P
>Here is a concatenation of two values of unspecified types:
</P><PRE
CLASS="SCREEN"
>SELECT 'abc' || 'def' AS "unspecified";

 unspecified
-------------
 abcdef
(1 row)</PRE
><P></P
><P
>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, <TT
CLASS="TYPE"
>text</TT
>, is used as the specific
type to resolve the unknown-type literals as.</P
></DIV
><DIV
CLASS="EXAMPLE"
><A
NAME="AEN21375"
></A
><P
><B
>Example 10-3. Absolute-Value and Negation Operator Type Resolution</B
></P
><P
>The <SPAN
CLASS="PRODUCTNAME"
>PostgreSQL</SPAN
> operator catalog has several
entries for the prefix operator <TT
CLASS="LITERAL"
>@</TT
>, all of which implement
absolute-value operations for various numeric data types.  One of these
entries is for type <TT
CLASS="TYPE"
>float8</TT
>, which is the preferred type in
the numeric category.  Therefore, <SPAN
CLASS="PRODUCTNAME"
>PostgreSQL</SPAN
>
will use that entry when faced with an <TT
CLASS="TYPE"
>unknown</TT
> input:
</P><PRE
CLASS="SCREEN"
>SELECT @ '-4.5' AS "abs";
 abs
-----
 4.5
(1 row)</PRE
><P>
Here the system has implicitly resolved the unknown-type literal as type
<TT
CLASS="TYPE"
>float8</TT
> before applying the chosen operator.  We can verify that
<TT
CLASS="TYPE"
>float8</TT
> and not some other type was used:
</P><PRE
CLASS="SCREEN"
>SELECT @ '-4.5e500' AS "abs";

ERROR:  "-4.5e500" is out of range for type double precision</PRE
><P></P
><P
>On the other hand, the prefix operator <TT
CLASS="LITERAL"
>~</TT
> (bitwise negation)
is defined only for integer data types, not for <TT
CLASS="TYPE"
>float8</TT
>.  So, if we
try a similar case with <TT
CLASS="LITERAL"
>~</TT
>, we get:
</P><PRE
CLASS="SCREEN"
>SELECT ~ '20' AS "negation";

ERROR:  operator is not unique: ~ "unknown"
HINT:  Could not choose a best candidate operator. You might need to add
explicit type casts.</PRE
><P>
This happens because the system cannot decide which of the several
possible <TT
CLASS="LITERAL"
>~</TT
> operators should be preferred.  We can help
it out with an explicit cast:
</P><PRE
CLASS="SCREEN"
>SELECT ~ CAST('20' AS int8) AS "negation";

 negation
----------
      -21
(1 row)</PRE
><P></P
></DIV
><DIV
CLASS="EXAMPLE"
><A
NAME="AEN21394"
></A
><P
><B
>Example 10-4. Array Inclusion Operator Type Resolution</B
></P
><P
>Here is another example of resolving an operator with one known and one
unknown input:
</P><PRE
CLASS="SCREEN"
>SELECT array[1,2] &lt;@ '{1,2,3}' as "is subset";

 is subset
-----------
 t
(1 row)</PRE
><P>
The <SPAN
CLASS="PRODUCTNAME"
>PostgreSQL</SPAN
> operator catalog has several
entries for the infix operator <TT
CLASS="LITERAL"
>&lt;@</TT
>, but the only two that
could possibly accept an integer array on the left-hand side are
array inclusion (<TT
CLASS="TYPE"
>anyarray</TT
> <TT
CLASS="LITERAL"
>&lt;@</TT
> <TT
CLASS="TYPE"
>anyarray</TT
>)
and range inclusion (<TT
CLASS="TYPE"
>anyelement</TT
> <TT
CLASS="LITERAL"
>&lt;@</TT
> <TT
CLASS="TYPE"
>anyrange</TT
>).
Since none of these polymorphic pseudo-types (see <A
HREF="datatype-pseudo.html"
>Section 8.19</A
>) are considered preferred, the parser cannot
resolve the ambiguity on that basis.
However, <A
HREF="typeconv-oper.html#OP-RESOL-LAST-UNKNOWN"
>step 3.f</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.  (Had range inclusion been selected, we would
have gotten an error, because the string does not have the right format to be
a range literal.)</P
></DIV
><DIV
CLASS="EXAMPLE"
><A
NAME="AEN21408"
></A
><P
><B
>Example 10-5. Custom Operator on a Domain Type</B
></P
><P
>Users sometimes try to declare operators applying just to a domain type.
This is possible but is not nearly as useful as it might seem, because the
operator resolution rules are designed to select operators applying to the
domain's base type.  As an example consider
</P><PRE
CLASS="SCREEN"
>CREATE DOMAIN mytext AS text CHECK(...);
CREATE FUNCTION mytext_eq_text (mytext, text) RETURNS boolean AS ...;
CREATE OPERATOR = (procedure=mytext_eq_text, leftarg=mytext, rightarg=text);
CREATE TABLE mytable (val mytext);

SELECT * FROM mytable WHERE val = 'foo';</PRE
><P>
This query will not use the custom operator.  The parser will first see if
there is a <TT
CLASS="TYPE"
>mytext</TT
> <TT
CLASS="LITERAL"
>=</TT
> <TT
CLASS="TYPE"
>mytext</TT
> operator
(<A
HREF="typeconv-oper.html#OP-RESOL-EXACT-UNKNOWN"
>step 2.a</A
>), which there is not;
then it will consider the domain's base type <TT
CLASS="TYPE"
>text</TT
>, and see if
there is a <TT
CLASS="TYPE"
>text</TT
> <TT
CLASS="LITERAL"
>=</TT
> <TT
CLASS="TYPE"
>text</TT
> operator
(<A
HREF="typeconv-oper.html#OP-RESOL-EXACT-DOMAIN"
>step 2.b</A
>), which there is;
so it resolves the <TT
CLASS="TYPE"
>unknown</TT
>-type literal as <TT
CLASS="TYPE"
>text</TT
> and
uses the <TT
CLASS="TYPE"
>text</TT
> <TT
CLASS="LITERAL"
>=</TT
> <TT
CLASS="TYPE"
>text</TT
> operator.
The only way to get the custom operator to be used is to explicitly cast
the literal:
</P><PRE
CLASS="SCREEN"
>SELECT * FROM mytable WHERE val = text 'foo';</PRE
><P>
so that the <TT
CLASS="TYPE"
>mytext</TT
> <TT
CLASS="LITERAL"
>=</TT
> <TT
CLASS="TYPE"
>text</TT
> operator is found
immediately according to the exact-match rule.  If the best-match rules
are reached, they actively discriminate against operators on domain types.
If they did not, such an operator would create too many ambiguous-operator
failures, because the casting rules always consider a domain as castable
to or from its base type, and so the domain operator would be considered
usable in all the same cases as a similarly-named operator on the base type.</P
></DIV
></DIV
><DIV
CLASS="NAVFOOTER"
><HR
ALIGN="LEFT"
WIDTH="100%"><TABLE
SUMMARY="Footer navigation table"
WIDTH="100%"
BORDER="0"
CELLPADDING="0"
CELLSPACING="0"
><TR
><TD
WIDTH="33%"
ALIGN="left"
VALIGN="top"
><A
HREF="typeconv-overview.html"
ACCESSKEY="P"
>Prev</A
></TD
><TD
WIDTH="34%"
ALIGN="center"
VALIGN="top"
><A
HREF="index.html"
ACCESSKEY="H"
>Home</A
></TD
><TD
WIDTH="33%"
ALIGN="right"
VALIGN="top"
><A
HREF="typeconv-func.html"
ACCESSKEY="N"
>Next</A
></TD
></TR
><TR
><TD
WIDTH="33%"
ALIGN="left"
VALIGN="top"
>Overview</TD
><TD
WIDTH="34%"
ALIGN="center"
VALIGN="top"
><A
HREF="typeconv.html"
ACCESSKEY="U"
>Up</A
></TD
><TD
WIDTH="33%"
ALIGN="right"
VALIGN="top"
>Functions</TD
></TR
></TABLE
></DIV
></BODY
></HTML
>