You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@datasketches.apache.org by al...@apache.org on 2019/06/15 01:17:29 UTC

[incubator-datasketches-postgresql] 01/01: renamed merge to union, added non-agg union

This is an automated email from the ASF dual-hosted git repository.

alsay pushed a commit to branch cpc_union
in repository https://gitbox.apache.org/repos/asf/incubator-datasketches-postgresql.git

commit 61ff0c47dc226af903007205f792ed83601ef8f8
Author: AlexanderSaydakov <Al...@users.noreply.github.com>
AuthorDate: Fri Jun 14 18:17:01 2019 -0700

    renamed merge to union, added non-agg union
---
 sql/datasketches_cpc_sketch.sql | 24 ++++++++++++++++--------
 src/cpc_sketch_pg_functions.c   | 39 ++++++++++++++++++++++++++++++++++++---
 2 files changed, 52 insertions(+), 11 deletions(-)

diff --git a/sql/datasketches_cpc_sketch.sql b/sql/datasketches_cpc_sketch.sql
index a6e9db3..b86453a 100644
--- a/sql/datasketches_cpc_sketch.sql
+++ b/sql/datasketches_cpc_sketch.sql
@@ -58,12 +58,12 @@ CREATE OR REPLACE FUNCTION cpc_sketch_to_string(cpc_sketch) RETURNS TEXT
     AS '$libdir/datasketches', 'pg_cpc_sketch_to_string'
     LANGUAGE C STRICT IMMUTABLE;
 
-CREATE OR REPLACE FUNCTION cpc_sketch_merge(internal, cpc_sketch) RETURNS internal
-    AS '$libdir/datasketches', 'pg_cpc_sketch_merge'
+CREATE OR REPLACE FUNCTION cpc_sketch_union_agg(internal, cpc_sketch) RETURNS internal
+    AS '$libdir/datasketches', 'pg_cpc_sketch_union_agg'
     LANGUAGE C IMMUTABLE;
 
-CREATE OR REPLACE FUNCTION cpc_sketch_merge(internal, cpc_sketch, int) RETURNS internal
-    AS '$libdir/datasketches', 'pg_cpc_sketch_merge'
+CREATE OR REPLACE FUNCTION cpc_sketch_union_agg(internal, cpc_sketch, int) RETURNS internal
+    AS '$libdir/datasketches', 'pg_cpc_sketch_union_agg'
     LANGUAGE C IMMUTABLE;
 
 CREATE OR REPLACE FUNCTION cpc_union_get_result(internal) RETURNS cpc_sketch
@@ -94,14 +94,22 @@ CREATE AGGREGATE cpc_sketch_build(anyelement, int) (
     finalfunc = cpc_sketch_from_internal
 );
 
-CREATE AGGREGATE cpc_sketch_merge(cpc_sketch) (
-    sfunc = cpc_sketch_merge,
+CREATE AGGREGATE cpc_sketch_union(cpc_sketch) (
+    sfunc = cpc_sketch_union_agg,
     stype = internal,
     finalfunc = cpc_union_get_result
 );
 
-CREATE AGGREGATE cpc_sketch_merge(cpc_sketch, int) (
-    sfunc = cpc_sketch_merge,
+CREATE AGGREGATE cpc_sketch_union(cpc_sketch, int) (
+    sfunc = cpc_sketch_union_agg,
     stype = internal,
     finalfunc = cpc_union_get_result
 );
+
+CREATE OR REPLACE FUNCTION cpc_sketch_union(cpc_sketch, cpc_sketch) RETURNS cpc_sketch
+    AS '$libdir/datasketches', 'pg_cpc_sketch_union'
+    LANGUAGE C IMMUTABLE;
+
+CREATE OR REPLACE FUNCTION cpc_sketch_union(cpc_sketch, cpc_sketch, int) RETURNS cpc_sketch
+    AS '$libdir/datasketches', 'pg_cpc_sketch_union'
+    LANGUAGE C IMMUTABLE;
diff --git a/src/cpc_sketch_pg_functions.c b/src/cpc_sketch_pg_functions.c
index d4e6e35..f37cde8 100644
--- a/src/cpc_sketch_pg_functions.c
+++ b/src/cpc_sketch_pg_functions.c
@@ -31,10 +31,11 @@ const unsigned DEFAULT_LG_K = 11;
 PG_FUNCTION_INFO_V1(pg_cpc_sketch_add_item);
 PG_FUNCTION_INFO_V1(pg_cpc_sketch_get_estimate);
 PG_FUNCTION_INFO_V1(pg_cpc_sketch_to_string);
-PG_FUNCTION_INFO_V1(pg_cpc_sketch_merge);
+PG_FUNCTION_INFO_V1(pg_cpc_sketch_union_agg);
 PG_FUNCTION_INFO_V1(pg_cpc_sketch_from_internal);
 PG_FUNCTION_INFO_V1(pg_cpc_sketch_get_estimate_from_internal);
 PG_FUNCTION_INFO_V1(pg_cpc_union_get_result);
+PG_FUNCTION_INFO_V1(pg_cpc_sketch_union);
 
 /* function declarations */
 Datum pg_cpc_sketch_recv(PG_FUNCTION_ARGS);
@@ -42,10 +43,11 @@ Datum pg_cpc_sketch_send(PG_FUNCTION_ARGS);
 Datum pg_cpc_sketch_add_item(PG_FUNCTION_ARGS);
 Datum pg_cpc_sketch_get_estimate(PG_FUNCTION_ARGS);
 Datum pg_cpc_sketch_to_string(PG_FUNCTION_ARGS);
-Datum pg_cpc_sketch_merge(PG_FUNCTION_ARGS);
+Datum pg_cpc_sketch_union_agg(PG_FUNCTION_ARGS);
 Datum pg_cpc_sketch_from_internal(PG_FUNCTION_ARGS);
 Datum pg_cpc_sketch_get_estimate_from_internal(PG_FUNCTION_ARGS);
 Datum pg_cpc_union_get_result(PG_FUNCTION_ARGS);
+Datum pg_cpc_sketch_union(PG_FUNCTION_ARGS);
 
 Datum pg_cpc_sketch_add_item(PG_FUNCTION_ARGS) {
   void* sketchptr;
@@ -120,7 +122,7 @@ Datum pg_cpc_sketch_to_string(PG_FUNCTION_ARGS) {
   PG_RETURN_TEXT_P(cstring_to_text(str));
 }
 
-Datum pg_cpc_sketch_merge(PG_FUNCTION_ARGS) {
+Datum pg_cpc_sketch_union_agg(PG_FUNCTION_ARGS) {
   void* unionptr;
   bytea* sketch_bytes;
   void* sketchptr;
@@ -228,3 +230,34 @@ Datum pg_cpc_union_get_result(PG_FUNCTION_ARGS) {
 
   PG_RETURN_BYTEA_P(bytes_out);
 }
+
+Datum pg_cpc_sketch_union(PG_FUNCTION_ARGS) {
+  const bytea* bytes_in1;
+  const bytea* bytes_in2;
+  void* sketchptr1;
+  void* sketchptr2;
+  void* unionptr;
+  void* sketchptr;
+  bytea* bytes_out;
+  int lg_k;
+
+  lg_k = PG_GETARG_INT32(2);
+  unionptr = cpc_union_new(lg_k ? lg_k : DEFAULT_LG_K);
+  if (!PG_ARGISNULL(0)) {
+    bytes_in1 = PG_GETARG_BYTEA_P(0);
+    sketchptr1 = cpc_sketch_deserialize(VARDATA(bytes_in1), VARSIZE(bytes_in1) - VARHDRSZ);
+    cpc_union_update(unionptr, sketchptr1);
+    cpc_sketch_delete(sketchptr1);
+  }
+  if (!PG_ARGISNULL(1)) {
+    bytes_in2 = PG_GETARG_BYTEA_P(1);
+    sketchptr2 = cpc_sketch_deserialize(VARDATA(bytes_in2), VARSIZE(bytes_in2) - VARHDRSZ);
+    cpc_union_update(unionptr, sketchptr2);
+    cpc_sketch_delete(sketchptr2);
+  }
+  sketchptr = cpc_union_get_result(unionptr);
+  cpc_union_delete(unionptr);
+  bytes_out = cpc_sketch_serialize(sketchptr);
+  cpc_sketch_delete(sketchptr);
+  PG_RETURN_BYTEA_P(bytes_out);
+}


---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscribe@datasketches.apache.org
For additional commands, e-mail: commits-help@datasketches.apache.org