accept int2 and int8 from hash function
authorMarko Kreen <markokr@gmail.com>
Fri, 27 Jun 2008 08:55:19 +0000 (08:55 +0000)
committerMarko Kreen <markokr@gmail.com>
Fri, 27 Jun 2008 08:55:19 +0000 (08:55 +0000)
expected/plproxy_test.out
sql/plproxy_test.sql
src/execute.c

index 9cc596d3e047f50322d7da1e51d811720bf22cfb..b79f5ca1c75bd1938a9f5a33d9bc2771588dacfd 100644 (file)
@@ -264,3 +264,49 @@ select * from "testQuoting"('user', '1', 'dat');
      1 | BazOoka
 (1 row)
 
+-- test hash types function
+create or replace function t_hash16(int4) returns int2 as $$
+declare
+    res int2;
+begin
+    res = $1::int2;
+    return res;
+end;
+$$ language plpgsql;
+create or replace function t_hash64(int4) returns int8 as $$
+declare
+    res int8;
+begin
+    res = $1;
+    return res;
+end;
+$$ language plpgsql;
+create function test_hash16(id integer, data text)
+returns text as $$ cluster 'testcluster'; run on t_hash16(id); select data; $$ language plproxy;
+select * from test_hash16('0', 'hash16');
+ test_hash16 
+-------------
+ hash16
+(1 row)
+
+create function test_hash64(id integer, data text)
+returns text as $$ cluster 'testcluster'; run on t_hash64(id); select data; $$ language plproxy;
+select * from test_hash64('0', 'hash64');
+ test_hash64 
+-------------
+ hash64
+(1 row)
+
+-- test argument difference
+\c test_part
+create function test_difftypes(username text, out val1 int2, out val2 float8)
+as $$ begin val1 = 1; val2 = 3;return; end; $$ language plpgsql;
+\c regression
+create function test_difftypes(username text, out val1 int4, out val2 float4)
+as $$ cluster 'testcluster'; run on 0; $$ language plproxy;
+select * from test_difftypes('types');
+ val1 | val2 
+------+------
+    1 |    3
+(1 row)
+
index 43cd7d475de32023dc45de9ee1f7b3c0b3cd4f89..5fb025b9503098ad059f0f40f605d7939f907650 100644 (file)
@@ -162,3 +162,39 @@ returns "RetWeird" as $$ select 1::int4, 'BazOoka'::text $$ language sql;
 \c regression
 select * from "testQuoting"('user', '1', 'dat');
 
+-- test hash types function
+create or replace function t_hash16(int4) returns int2 as $$
+declare
+    res int2;
+begin
+    res = $1::int2;
+    return res;
+end;
+$$ language plpgsql;
+
+create or replace function t_hash64(int4) returns int8 as $$
+declare
+    res int8;
+begin
+    res = $1;
+    return res;
+end;
+$$ language plpgsql;
+
+create function test_hash16(id integer, data text)
+returns text as $$ cluster 'testcluster'; run on t_hash16(id); select data; $$ language plproxy;
+select * from test_hash16('0', 'hash16');
+
+create function test_hash64(id integer, data text)
+returns text as $$ cluster 'testcluster'; run on t_hash64(id); select data; $$ language plproxy;
+select * from test_hash64('0', 'hash64');
+
+-- test argument difference
+\c test_part
+create function test_difftypes(username text, out val1 int2, out val2 float8)
+as $$ begin val1 = 1; val2 = 3;return; end; $$ language plpgsql;
+\c regression
+create function test_difftypes(username text, out val1 int4, out val2 float4)
+as $$ cluster 'testcluster'; run on 0; $$ language plproxy;
+select * from test_difftypes('types');
+
index 0e75c6aefe158519b7500b3d19c9572d068d9882..5bba3a881d0c6bb7d477020db35fbdf6b651283c 100644 (file)
@@ -619,6 +619,7 @@ tag_hash_partitions(ProxyFunction *func, FunctionCallInfo fcinfo)
 {
        int                     i;
        TupleDesc       desc;
+       Oid                     htype;
        ProxyCluster *cluster = func->cur_cluster;
 
        /* execute cached plan */
@@ -626,22 +627,29 @@ tag_hash_partitions(ProxyFunction *func, FunctionCallInfo fcinfo)
 
        /* get header */
        desc = SPI_tuptable->tupdesc;
-
-       /* check if type is ok */
-       if (SPI_gettypeid(desc, 1) != INT4OID)
-               plproxy_error(func, "Hash result must be int4");
+       htype = SPI_gettypeid(desc, 1);
 
        /* tag connections */
        for (i = 0; i < SPI_processed; i++)
        {
                bool            isnull;
-               int                     hashval;
+               uint32          hashval = 0;
                HeapTuple       row = SPI_tuptable->vals[i];
                Datum           val = SPI_getbinval(row, desc, 1, &isnull);
 
                if (isnull)
                        plproxy_error(func, "Hash function returned NULL");
-               hashval = DatumGetInt32(val) & cluster->part_mask;
+
+               if (htype == INT4OID)
+                       hashval = DatumGetInt32(val);
+               else if (htype == INT8OID)
+                       hashval = DatumGetInt64(val);
+               else if (htype == INT2OID)
+                       hashval = DatumGetInt16(val);
+               else
+                       plproxy_error(func, "Hash result must be int2, int4 or int8");
+
+               hashval &= cluster->part_mask;
                cluster->part_map[hashval]->run_on = 1;
        }