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/11 00:49:37 UTC

[incubator-datasketches-postgresql] branch theta updated: a-not-b

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

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


The following commit(s) were added to refs/heads/theta by this push:
     new e80b587  a-not-b
e80b587 is described below

commit e80b587b786d755e74b2ffb7584826d9ebd23eae
Author: AlexanderSaydakov <Al...@users.noreply.github.com>
AuthorDate: Mon Jun 10 17:49:18 2019 -0700

    a-not-b
---
 sql/datasketches_theta_sketch.sql |  4 ++++
 src/theta_sketch_c_adapter.cpp    | 22 ++++++++++++++++++----
 src/theta_sketch_c_adapter.h      |  6 ++++--
 src/theta_sketch_pg_functions.c   | 26 ++++++++++++++++++++++++++
 4 files changed, 52 insertions(+), 6 deletions(-)

diff --git a/sql/datasketches_theta_sketch.sql b/sql/datasketches_theta_sketch.sql
index c060c54..2a8cfe8 100644
--- a/sql/datasketches_theta_sketch.sql
+++ b/sql/datasketches_theta_sketch.sql
@@ -99,3 +99,7 @@ CREATE OR REPLACE FUNCTION theta_sketch_union(theta_sketch, theta_sketch) RETURN
 CREATE OR REPLACE FUNCTION theta_sketch_intersection(theta_sketch, theta_sketch) RETURNS theta_sketch
     AS '$libdir/datasketches', 'pg_theta_sketch_intersection'
     LANGUAGE C STRICT IMMUTABLE;
+
+CREATE OR REPLACE FUNCTION theta_sketch_a_not_b(theta_sketch, theta_sketch) RETURNS theta_sketch
+    AS '$libdir/datasketches', 'pg_theta_sketch_a_not_b'
+    LANGUAGE C STRICT IMMUTABLE;
diff --git a/src/theta_sketch_c_adapter.cpp b/src/theta_sketch_c_adapter.cpp
index b1bd47b..fb71142 100644
--- a/src/theta_sketch_c_adapter.cpp
+++ b/src/theta_sketch_c_adapter.cpp
@@ -15,12 +15,14 @@ extern "C" {
 #include <theta_sketch.hpp>
 #include <theta_union.hpp>
 #include <theta_intersection.hpp>
+#include <theta_a_not_b.hpp>
 
 typedef datasketches::theta_sketch_alloc<palloc_allocator<void>> theta_sketch_pg;
 typedef datasketches::update_theta_sketch_alloc<palloc_allocator<void>> update_theta_sketch_pg;
 typedef datasketches::compact_theta_sketch_alloc<palloc_allocator<void>> compact_theta_sketch_pg;
 typedef datasketches::theta_union_alloc<palloc_allocator<void>> theta_union_pg;
 typedef datasketches::theta_intersection_alloc<palloc_allocator<void>> theta_intersection_pg;
+typedef datasketches::theta_a_not_b_alloc<palloc_allocator<void>> theta_a_not_b_pg;
 
 void* theta_sketch_new_default() {
   try {
@@ -138,9 +140,9 @@ void theta_union_update(void* unionptr, const void* sketchptr) {
   }
 }
 
-void* theta_union_get_result(void* unionptr) {
+void* theta_union_get_result(const void* unionptr) {
   try {
-    return new (palloc(sizeof(compact_theta_sketch_pg))) compact_theta_sketch_pg(static_cast<theta_union_pg*>(unionptr)->get_result());
+    return new (palloc(sizeof(compact_theta_sketch_pg))) compact_theta_sketch_pg(static_cast<const theta_union_pg*>(unionptr)->get_result());
   } catch (std::exception& e) {
     elog(ERROR, e.what());
   }
@@ -171,9 +173,21 @@ void theta_intersection_update(void* interptr, const void* sketchptr) {
   }
 }
 
-void* theta_intersection_get_result(void* interptr) {
+void* theta_intersection_get_result(const void* interptr) {
   try {
-    return new (palloc(sizeof(compact_theta_sketch_pg))) compact_theta_sketch_pg(static_cast<theta_intersection_pg*>(interptr)->get_result());
+    return new (palloc(sizeof(compact_theta_sketch_pg))) compact_theta_sketch_pg(static_cast<const theta_intersection_pg*>(interptr)->get_result());
+  } catch (std::exception& e) {
+    elog(ERROR, e.what());
+  }
+}
+
+void* theta_a_not_b(const void* sketchptr1, const void* sketchptr2) {
+  try {
+    theta_a_not_b_pg a_not_b;
+    return new (palloc(sizeof(compact_theta_sketch_pg))) compact_theta_sketch_pg(a_not_b.compute(
+      *static_cast<const theta_sketch_pg*>(sketchptr1),
+      *static_cast<const theta_sketch_pg*>(sketchptr2)
+    ));
   } catch (std::exception& e) {
     elog(ERROR, e.what());
   }
diff --git a/src/theta_sketch_c_adapter.h b/src/theta_sketch_c_adapter.h
index 2cf9dc9..2082865 100644
--- a/src/theta_sketch_c_adapter.h
+++ b/src/theta_sketch_c_adapter.h
@@ -27,12 +27,14 @@ void* theta_union_new_default();
 void* theta_union_new(unsigned lg_k);
 void theta_union_delete(void* unionptr);
 void theta_union_update(void* unionptr, const void* sketchptr);
-void* theta_union_get_result(void* unionptr);
+void* theta_union_get_result(const void* unionptr);
 
 void* theta_intersection_new_default();
 void theta_intersection_delete(void* interptr);
 void theta_intersection_update(void* interptr, const void* sketchptr);
-void* theta_intersection_get_result(void* interptr);
+void* theta_intersection_get_result(const void* interptr);
+
+void* theta_a_not_b(const void* sketchptr1, const void* sketchptr2);
 
 #ifdef __cplusplus
 }
diff --git a/src/theta_sketch_pg_functions.c b/src/theta_sketch_pg_functions.c
index 7de5107..5961c7f 100644
--- a/src/theta_sketch_pg_functions.c
+++ b/src/theta_sketch_pg_functions.c
@@ -21,6 +21,7 @@ PG_FUNCTION_INFO_V1(pg_theta_sketch_get_estimate_from_internal);
 PG_FUNCTION_INFO_V1(pg_theta_union_get_result);
 PG_FUNCTION_INFO_V1(pg_theta_sketch_union);
 PG_FUNCTION_INFO_V1(pg_theta_sketch_intersection);
+PG_FUNCTION_INFO_V1(pg_theta_sketch_a_not_b);
 
 /* function declarations */
 Datum pg_theta_sketch_recv(PG_FUNCTION_ARGS);
@@ -34,6 +35,7 @@ Datum pg_theta_sketch_get_estimate_from_internal(PG_FUNCTION_ARGS);
 Datum pg_theta_union_get_result(PG_FUNCTION_ARGS);
 Datum pg_theta_sketch_union(PG_FUNCTION_ARGS);
 Datum pg_theta_sketch_intersection(PG_FUNCTION_ARGS);
+Datum pg_theta_sketch_a_not_b(PG_FUNCTION_ARGS);
 
 Datum pg_theta_sketch_add_item(PG_FUNCTION_ARGS) {
   void* sketchptr;
@@ -275,3 +277,27 @@ Datum pg_theta_sketch_intersection(PG_FUNCTION_ARGS) {
   theta_sketch_delete(sketchptr);
   PG_RETURN_BYTEA_P(bytes_out);
 }
+
+Datum pg_theta_sketch_a_not_b(PG_FUNCTION_ARGS) {
+  const bytea* bytes_in1;
+  const bytea* bytes_in2;
+  void* sketchptr1;
+  void* sketchptr2;
+  void* sketchptr;
+  bytea* bytes_out;
+
+  if (PG_ARGISNULL(0) || PG_ARGISNULL(1)) {
+    elog(ERROR, "theta_a_not_b expects two valid theta sketches");
+  }
+
+  bytes_in1 = PG_GETARG_BYTEA_P(0);
+  sketchptr1 = theta_sketch_deserialize(VARDATA(bytes_in1), VARSIZE(bytes_in1) - VARHDRSZ);
+  bytes_in2 = PG_GETARG_BYTEA_P(1);
+  sketchptr2 = theta_sketch_deserialize(VARDATA(bytes_in2), VARSIZE(bytes_in2) - VARHDRSZ);
+  sketchptr = theta_a_not_b(sketchptr1, sketchptr2);
+  theta_sketch_delete(sketchptr1);
+  theta_sketch_delete(sketchptr2);
+  bytes_out = theta_sketch_serialize(sketchptr);
+  theta_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