gooderp18绿色标准版
Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.

58 lignes
4.8KB

  1. <?xml version="1.0" encoding="UTF-8" standalone="no"?>
  2. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html xmlns="http://www.w3.org/1999/xhtml"><head><meta http-equiv="Content-Type" content="text/html; charset=UTF-8" /><title>37.14. User-Defined Operators</title><link rel="stylesheet" type="text/css" href="stylesheet.css" /><link rev="made" href="pgsql-docs@lists.postgresql.org" /><meta name="generator" content="DocBook XSL Stylesheets V1.79.1" /><link rel="prev" href="xtypes.html" title="37.13. User-Defined Types" /><link rel="next" href="xoper-optimization.html" title="37.15. Operator Optimization Information" /></head><body><div xmlns="http://www.w3.org/TR/xhtml1/transitional" class="navheader"><table width="100%" summary="Navigation header"><tr><th colspan="5" align="center">37.14. User-Defined Operators</th></tr><tr><td width="10%" align="left"><a accesskey="p" href="xtypes.html" title="37.13. User-Defined Types">Prev</a> </td><td width="10%" align="left"><a accesskey="u" href="extend.html" title="Chapter 37. Extending SQL">Up</a></td><th width="60%" align="center">Chapter 37. Extending <acronym xmlns="http://www.w3.org/1999/xhtml" class="acronym">SQL</acronym></th><td width="10%" align="right"><a accesskey="h" href="index.html" title="PostgreSQL 12.4 Documentation">Home</a></td><td width="10%" align="right"> <a accesskey="n" href="xoper-optimization.html" title="37.15. Operator Optimization Information">Next</a></td></tr></table><hr></hr></div><div class="sect1" id="XOPER"><div class="titlepage"><div><div><h2 class="title" style="clear: both">37.14. User-Defined Operators</h2></div></div></div><a id="id-1.8.3.17.2" class="indexterm"></a><p>
  3. Every operator is <span class="quote">“<span class="quote">syntactic sugar</span>”</span> for a call to an
  4. underlying function that does the real work; so you must
  5. first create the underlying function before you can create
  6. the operator. However, an operator is <span class="emphasis"><em>not merely</em></span>
  7. syntactic sugar, because it carries additional information
  8. that helps the query planner optimize queries that use the
  9. operator. The next section will be devoted to explaining
  10. that additional information.
  11. </p><p>
  12. <span class="productname">PostgreSQL</span> supports left unary, right
  13. unary, and binary operators. Operators can be
  14. overloaded;<a id="id-1.8.3.17.4.2" class="indexterm"></a>
  15. that is, the same operator name can be used for different operators
  16. that have different numbers and types of operands. When a query is
  17. executed, the system determines the operator to call from the
  18. number and types of the provided operands.
  19. </p><p>
  20. Here is an example of creating an operator for adding two complex
  21. numbers. We assume we've already created the definition of type
  22. <code class="type">complex</code> (see <a class="xref" href="xtypes.html" title="37.13. User-Defined Types">Section 37.13</a>). First we need a
  23. function that does the work, then we can define the operator:
  24. </p><pre class="programlisting">
  25. CREATE FUNCTION complex_add(complex, complex)
  26. RETURNS complex
  27. AS '<em class="replaceable"><code>filename</code></em>', 'complex_add'
  28. LANGUAGE C IMMUTABLE STRICT;
  29. CREATE OPERATOR + (
  30. leftarg = complex,
  31. rightarg = complex,
  32. function = complex_add,
  33. commutator = +
  34. );
  35. </pre><p>
  36. </p><p>
  37. Now we could execute a query like this:
  38. </p><pre class="screen">
  39. SELECT (a + b) AS c FROM test_complex;
  40. c
  41. -----------------
  42. (5.2,6.05)
  43. (133.42,144.95)
  44. </pre><p>
  45. </p><p>
  46. We've shown how to create a binary operator here. To create unary
  47. operators, just omit one of <code class="literal">leftarg</code> (for left unary) or
  48. <code class="literal">rightarg</code> (for right unary). The <code class="literal">function</code>
  49. clause and the argument clauses are the only required items in
  50. <code class="command">CREATE OPERATOR</code>. The <code class="literal">commutator</code>
  51. clause shown in the example is an optional hint to the query
  52. optimizer. Further details about <code class="literal">commutator</code> and other
  53. optimizer hints appear in the next section.
  54. </p></div><div class="navfooter"><hr /><table width="100%" summary="Navigation footer"><tr><td width="40%" align="left"><a accesskey="p" href="xtypes.html">Prev</a> </td><td width="20%" align="center"><a accesskey="u" href="extend.html">Up</a></td><td width="40%" align="right"> <a accesskey="n" href="xoper-optimization.html">Next</a></td></tr><tr><td width="40%" align="left" valign="top">37.13. User-Defined Types </td><td width="20%" align="center"><a accesskey="h" href="index.html">Home</a></td><td width="40%" align="right" valign="top"> 37.15. Operator Optimization Information</td></tr></table></div></body></html>
上海开阖软件有限公司 沪ICP备12045867号-1