|
1 | | -<Chapter Id="xoper"> |
2 | | -<Title>Extending <Acronym>SQL</Acronym>: Operators</Title> |
3 | | - |
4 | | -<Para> |
5 | | - <ProductName>Postgres</ProductName> supports left unary, right unary and binary |
6 | | - operators. Operators can be overloaded, or re-used |
7 | | - with different numbers and types of arguments. If |
8 | | - there is an ambiguous situation and the system cannot |
9 | | - determine the correct operator to use, it will return |
10 | | - an error and you may have to typecast the left and/or |
11 | | - right operands to help it understand which operator you |
12 | | - meant to use. |
13 | | - To create an operator for adding two complex numbers |
14 | | - can be done as follows. First we need to create a |
15 | | - function to add the new types. Then, we can create the |
16 | | - operator with the function. |
17 | | - |
18 | | -<ProgramListing> |
19 | | - CREATE FUNCTION complex_add(complex, complex) |
20 | | - RETURNS complex |
21 | | - AS '$PWD/obj/complex.so' |
22 | | - LANGUAGE 'c'; |
23 | | - |
24 | | - CREATE OPERATOR + ( |
25 | | - leftarg = complex, |
26 | | - rightarg = complex, |
27 | | - procedure = complex_add, |
28 | | - commutator = + |
29 | | - ); |
30 | | -</ProgramListing> |
31 | | -</Para> |
32 | | - |
33 | | -<Para> |
34 | | - We've shown how to create a binary operator here. To |
35 | | - create unary operators, just omit one of leftarg (for |
36 | | - left unary) or rightarg (for right unary). |
37 | | - If we give the system enough type information, it can |
38 | | - automatically figure out which operators to use. |
| 1 | + <Chapter Id="xoper"> |
| 2 | + <Title>Extending <Acronym>SQL</Acronym>: Operators</Title> |
| 3 | + |
| 4 | + <Para> |
| 5 | + <ProductName>Postgres</ProductName> supports left unary, |
| 6 | + right unary and binary |
| 7 | + operators. Operators can be overloaded, or re-used |
| 8 | + with different numbers and types of arguments. If |
| 9 | + there is an ambiguous situation and the system cannot |
| 10 | + determine the correct operator to use, it will return |
| 11 | + an error and you may have to typecast the left and/or |
| 12 | + right operands to help it understand which operator you |
| 13 | + meant to use. |
| 14 | + To create an operator for adding two complex numbers |
| 15 | + can be done as follows. First we need to create a |
| 16 | + function to add the new types. Then, we can create the |
| 17 | + operator with the function. |
| 18 | + |
| 19 | + <ProgramListing> |
| 20 | +CREATE FUNCTION complex_add(complex, complex) |
| 21 | + RETURNS complex |
| 22 | + AS '$PWD/obj/complex.so' |
| 23 | + LANGUAGE 'c'; |
| 24 | + |
| 25 | +CREATE OPERATOR + ( |
| 26 | + leftarg = complex, |
| 27 | + rightarg = complex, |
| 28 | + procedure = complex_add, |
| 29 | + commutator = + |
| 30 | +); |
| 31 | + </ProgramListing> |
| 32 | + </Para> |
| 33 | + |
| 34 | + <Para> |
| 35 | + We've shown how to create a binary operator here. To |
| 36 | + create unary operators, just omit one of leftarg (for |
| 37 | + left unary) or rightarg (for right unary). |
| 38 | + If we give the system enough type information, it can |
| 39 | + automatically figure out which operators to use. |
39 | 40 |
|
40 | | -<ProgramListing> |
41 | | - SELECT (a + b) AS c FROM test_complex; |
42 | | - |
43 | | - +----------------+ |
44 | | - |c | |
45 | | - +----------------+ |
46 | | - |(5.2,6.05) | |
47 | | - +----------------+ |
48 | | - |(133.42,144.95) | |
49 | | - +----------------+ |
50 | | -</ProgramListing> |
51 | | -</Para> |
52 | | -</Chapter> |
| 41 | + <ProgramListing> |
| 42 | +SELECT (a + b) AS c FROM test_complex; |
| 43 | + |
| 44 | ++----------------+ |
| 45 | +|c | |
| 46 | ++----------------+ |
| 47 | +|(5.2,6.05) | |
| 48 | ++----------------+ |
| 49 | +|(133.42,144.95) | |
| 50 | ++----------------+ |
| 51 | + </ProgramListing> |
| 52 | + </Para> |
| 53 | + |
| 54 | + <sect1> |
| 55 | + <title>Hash Join Operators</title> |
| 56 | + |
| 57 | + <note> |
| 58 | + <title>Author</title> |
| 59 | + <para> |
| 60 | + Written by Tom Lane. |
| 61 | + </para> |
| 62 | + </note> |
| 63 | + |
| 64 | + <para> |
| 65 | + The assumption underlying hash join is that two values that will be |
| 66 | + considered equal by the comparison operator will always have the same |
| 67 | + hash value. If two values get put in different hash buckets, the join |
| 68 | + will never compare them at all, so they are necessarily treated as |
| 69 | + unequal. |
| 70 | + </para> |
| 71 | + |
| 72 | + <para> |
| 73 | + But we have a number of datatypes for which the "=" operator is not |
| 74 | + a straight bitwise comparison. For example, intervaleq is not bitwise |
| 75 | + at all; it considers two time intervals equal if they have the same |
| 76 | + duration, whether or not their endpoints are identical. What this means |
| 77 | + is that a join using "=" between interval fields will yield different |
| 78 | + results if implemented as a hash join than if implemented another way, |
| 79 | + because a large fraction of the pairs that should match will hash to |
| 80 | + different values and will never be compared. |
| 81 | + </para> |
| 82 | + |
| 83 | + <para> |
| 84 | + I believe the same problem exists for float data; for example, on |
| 85 | + IEEE-compliant machines, minus zero and plus zero have different bit |
| 86 | + patterns (hence different hash values) but should be considered equal. |
| 87 | + A hashjoin will get it wrong. |
| 88 | + </para> |
| 89 | + |
| 90 | + <para> |
| 91 | + I will go through pg_operator and remove the hashable flag from |
| 92 | + operators that are not safely hashable, but I see no way to |
| 93 | + automatically check for this sort of mistake. The only long-term |
| 94 | + answer is to raise the consciousness of datatype creators about what |
| 95 | + it means to set the oprcanhash flag. Don't do it unless your equality |
| 96 | + operator can be implemented as memcmp()! |
| 97 | + </para> |
| 98 | + </sect1> |
| 99 | + </Chapter> |
| 100 | + |
| 101 | +<!-- Keep this comment at the end of the file |
| 102 | +Local variables: |
| 103 | +mode: sgml |
| 104 | +sgml-omittag:nil |
| 105 | +sgml-shorttag:t |
| 106 | +sgml-minimize-attributes:nil |
| 107 | +sgml-always-quote-attributes:t |
| 108 | +sgml-indent-step:1 |
| 109 | +sgml-indent-data:t |
| 110 | +sgml-parent-document:nil |
| 111 | +sgml-default-dtd-file:"./reference.ced" |
| 112 | +sgml-exposed-tags:nil |
| 113 | +sgml-local-catalogs:"/usr/lib/sgml/CATALOG" |
| 114 | +sgml-local-ecat-files:nil |
| 115 | +End: |
| 116 | +--> |