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/12 00:23:39 UTC

[incubator-datasketches-postgresql] branch theta updated: get estimate and bounds

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 56acf85  get estimate and bounds
56acf85 is described below

commit 56acf8563c3da4bebd6171c36f7a450878d06e16
Author: AlexanderSaydakov <Al...@users.noreply.github.com>
AuthorDate: Tue Jun 11 17:23:11 2019 -0700

    get estimate and bounds
---
 sql/datasketches_theta_sketch.sql |  8 ++++++++
 src/theta_sketch_c_adapter.cpp    | 12 ++++++++++++
 src/theta_sketch_c_adapter.h      |  3 +++
 src/theta_sketch_pg_functions.c   | 29 +++++++++++++++++++++++++++++
 4 files changed, 52 insertions(+)

diff --git a/sql/datasketches_theta_sketch.sql b/sql/datasketches_theta_sketch.sql
index 4927795..2d1f7f0 100644
--- a/sql/datasketches_theta_sketch.sql
+++ b/sql/datasketches_theta_sketch.sql
@@ -36,6 +36,14 @@ CREATE OR REPLACE FUNCTION theta_sketch_get_estimate(theta_sketch) RETURNS doubl
     AS '$libdir/datasketches', 'pg_theta_sketch_get_estimate'
     LANGUAGE C STRICT IMMUTABLE;
 
+CREATE OR REPLACE FUNCTION theta_sketch_get_estimate_and_bounds(theta_sketch) RETURNS double precision[]
+    AS '$libdir/datasketches', 'pg_theta_sketch_get_estimate_and_bounds'
+    LANGUAGE C STRICT IMMUTABLE;
+
+CREATE OR REPLACE FUNCTION theta_sketch_get_estimate_and_bounds(theta_sketch, int) RETURNS double precision[]
+    AS '$libdir/datasketches', 'pg_theta_sketch_get_estimate_and_bounds'
+    LANGUAGE C STRICT IMMUTABLE;
+
 CREATE OR REPLACE FUNCTION theta_sketch_from_internal(internal) RETURNS theta_sketch
     AS '$libdir/datasketches', 'pg_theta_sketch_from_internal'
     LANGUAGE C STRICT IMMUTABLE;
diff --git a/src/theta_sketch_c_adapter.cpp b/src/theta_sketch_c_adapter.cpp
index 76843ad..b99485c 100644
--- a/src/theta_sketch_c_adapter.cpp
+++ b/src/theta_sketch_c_adapter.cpp
@@ -84,6 +84,18 @@ double theta_sketch_get_estimate(const void* sketchptr) {
   }
 }
 
+Datum* theta_sketch_get_estimate_and_bounds(const void* sketchptr, unsigned num_std_devs) {
+  try {
+    Datum* est_and_bounds = (Datum*) palloc(sizeof(Datum) * 3);
+    est_and_bounds[0] = Float8GetDatum(static_cast<const theta_sketch_pg*>(sketchptr)->get_estimate());
+    est_and_bounds[1] = Float8GetDatum(static_cast<const theta_sketch_pg*>(sketchptr)->get_lower_bound(num_std_devs));
+    est_and_bounds[2] = Float8GetDatum(static_cast<const theta_sketch_pg*>(sketchptr)->get_upper_bound(num_std_devs));
+    return est_and_bounds;
+  } catch (std::exception& e) {
+    elog(ERROR, e.what());
+  }
+}
+
 void theta_sketch_to_string(const void* sketchptr, char* buffer, unsigned length) {
   try {
     std::stringstream s;
diff --git a/src/theta_sketch_c_adapter.h b/src/theta_sketch_c_adapter.h
index dfac5b4..75c7502 100644
--- a/src/theta_sketch_c_adapter.h
+++ b/src/theta_sketch_c_adapter.h
@@ -10,6 +10,8 @@
 extern "C" {
 #endif
 
+#include <postgres.h>
+
 void* theta_sketch_new_default();
 void* theta_sketch_new_lgk(unsigned lg_k);
 void* theta_sketch_new_lgk_p(unsigned lg_k, float p);
@@ -19,6 +21,7 @@ void theta_sketch_update(void* sketchptr, const void* data, unsigned length);
 void* theta_sketch_compact(void* sketchptr);
 void theta_sketch_union(void* sketchptr1, const void* sketchptr2);
 double theta_sketch_get_estimate(const void* sketchptr);
+Datum* theta_sketch_get_estimate_and_bounds(const void* sketchptr, unsigned num_std_devs);
 void theta_sketch_to_string(const void* sketchptr, char* buffer, unsigned length);
 
 void* theta_sketch_serialize(const void* sketchptr);
diff --git a/src/theta_sketch_pg_functions.c b/src/theta_sketch_pg_functions.c
index 20c1e42..899817a 100644
--- a/src/theta_sketch_pg_functions.c
+++ b/src/theta_sketch_pg_functions.c
@@ -7,6 +7,8 @@
 #include <fmgr.h>
 #include <utils/lsyscache.h>
 #include <utils/builtins.h>
+#include <utils/array.h>
+#include <catalog/pg_type.h>
 
 #include "theta_sketch_c_adapter.h"
 #include "base64.h"
@@ -14,6 +16,7 @@
 /* PG_FUNCTION_INFO_V1 macro to pass functions to postgres */
 PG_FUNCTION_INFO_V1(pg_theta_sketch_add_item);
 PG_FUNCTION_INFO_V1(pg_theta_sketch_get_estimate);
+PG_FUNCTION_INFO_V1(pg_theta_sketch_get_estimate_and_bounds);
 PG_FUNCTION_INFO_V1(pg_theta_sketch_to_string);
 PG_FUNCTION_INFO_V1(pg_theta_sketch_union_agg);
 PG_FUNCTION_INFO_V1(pg_theta_sketch_from_internal);
@@ -28,6 +31,7 @@ Datum pg_theta_sketch_recv(PG_FUNCTION_ARGS);
 Datum pg_theta_sketch_send(PG_FUNCTION_ARGS);
 Datum pg_theta_sketch_add_item(PG_FUNCTION_ARGS);
 Datum pg_theta_sketch_get_estimate(PG_FUNCTION_ARGS);
+Datum pg_theta_sketch_get_estimate_and_bounds(PG_FUNCTION_ARGS);
 Datum pg_theta_sketch_to_string(PG_FUNCTION_ARGS);
 Datum pg_theta_sketch_union_agg(PG_FUNCTION_ARGS);
 Datum pg_theta_sketch_from_internal(PG_FUNCTION_ARGS);
@@ -105,6 +109,31 @@ Datum pg_theta_sketch_get_estimate(PG_FUNCTION_ARGS) {
   PG_RETURN_FLOAT8(estimate);
 }
 
+Datum pg_theta_sketch_get_estimate_and_bounds(PG_FUNCTION_ARGS) {
+  const bytea* bytes_in;
+  void* sketchptr;
+  int num_std_devs;
+
+  // output array
+  Datum* est_and_bounds;
+  ArrayType* arr_out;
+  int16 elmlen_out;
+  bool elmbyval_out;
+  char elmalign_out;
+
+  bytes_in = PG_GETARG_BYTEA_P(0);
+  sketchptr = theta_sketch_deserialize(VARDATA(bytes_in), VARSIZE(bytes_in) - VARHDRSZ);
+  num_std_devs = PG_GETARG_INT32(1);
+  if (num_std_devs == 0) num_std_devs = 1; // default
+  est_and_bounds = theta_sketch_get_estimate_and_bounds(sketchptr, num_std_devs);
+  theta_sketch_delete(sketchptr);
+
+  // construct output array
+  get_typlenbyvalalign(FLOAT8OID, &elmlen_out, &elmbyval_out, &elmalign_out);
+  arr_out = construct_array(est_and_bounds, 3, FLOAT8OID, elmlen_out, elmbyval_out, elmalign_out);
+  PG_RETURN_ARRAYTYPE_P(arr_out);
+}
+
 Datum pg_theta_sketch_to_string(PG_FUNCTION_ARGS) {
   const bytea* bytes_in;
   void* sketchptr;


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