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

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


The following commit(s) were added to refs/heads/partial_agg by this push:
     new dd7e302  quantiles sketch partial aggregation support
dd7e302 is described below

commit dd7e3027b882527cc8d1099971642905a404c66c
Author: AlexanderSaydakov <[email protected]>
AuthorDate: Fri Apr 21 13:54:29 2023 -0700

    quantiles sketch partial aggregation support
---
 sql/datasketches_quantiles_double_sketch.sql | 122 +++++++++++-------
 src/quantiles_double_sketch_pg_functions.c   | 182 +++++++++++++++++----------
 2 files changed, 194 insertions(+), 110 deletions(-)

diff --git a/sql/datasketches_quantiles_double_sketch.sql 
b/sql/datasketches_quantiles_double_sketch.sql
index e8b9db2..f1f334c 100644
--- a/sql/datasketches_quantiles_double_sketch.sql
+++ b/sql/datasketches_quantiles_double_sketch.sql
@@ -19,11 +19,11 @@ CREATE TYPE quantiles_double_sketch;
 
 CREATE OR REPLACE FUNCTION quantiles_double_sketch_in(cstring) RETURNS 
quantiles_double_sketch
      AS '$libdir/datasketches', 'pg_sketch_in'
-     LANGUAGE C STRICT IMMUTABLE;
+     LANGUAGE C STRICT IMMUTABLE PARALLEL SAFE;
 
 CREATE OR REPLACE FUNCTION 
quantiles_double_sketch_out(quantiles_double_sketch) RETURNS cstring
      AS '$libdir/datasketches', 'pg_sketch_out'
-     LANGUAGE C STRICT IMMUTABLE;
+     LANGUAGE C STRICT IMMUTABLE PARALLEL SAFE;
 
 CREATE TYPE quantiles_double_sketch (
     INPUT = quantiles_double_sketch_in,
@@ -34,82 +34,110 @@ CREATE TYPE quantiles_double_sketch (
 CREATE CAST (bytea as quantiles_double_sketch) WITHOUT FUNCTION AS ASSIGNMENT;
 CREATE CAST (quantiles_double_sketch as bytea) WITHOUT FUNCTION AS ASSIGNMENT;
 
-CREATE OR REPLACE FUNCTION quantiles_double_sketch_add_item(internal, double 
precision) RETURNS internal
-    AS '$libdir/datasketches', 'pg_quantiles_double_sketch_add_item'
-    LANGUAGE C IMMUTABLE;
+CREATE OR REPLACE FUNCTION quantiles_double_sketch_build_agg(internal, double 
precision) RETURNS internal
+    AS '$libdir/datasketches', 'pg_quantiles_double_sketch_build_agg'
+    LANGUAGE C IMMUTABLE PARALLEL SAFE;
 
-CREATE OR REPLACE FUNCTION quantiles_double_sketch_add_item(internal, double 
precision, int) RETURNS internal
-    AS '$libdir/datasketches', 'pg_quantiles_double_sketch_add_item'
-    LANGUAGE C IMMUTABLE;
+CREATE OR REPLACE FUNCTION quantiles_double_sketch_build_agg(internal, double 
precision, int) RETURNS internal
+    AS '$libdir/datasketches', 'pg_quantiles_double_sketch_build_agg'
+    LANGUAGE C IMMUTABLE PARALLEL SAFE;
 
-CREATE OR REPLACE FUNCTION 
quantiles_double_sketch_get_rank(quantiles_double_sketch, double precision) 
RETURNS double precision
-    AS '$libdir/datasketches', 'pg_quantiles_double_sketch_get_rank'
-    LANGUAGE C STRICT IMMUTABLE;
+CREATE OR REPLACE FUNCTION quantiles_double_sketch_merge_agg(internal, 
quantiles_double_sketch) RETURNS internal
+    AS '$libdir/datasketches', 'pg_quantiles_double_sketch_merge_agg'
+    LANGUAGE C IMMUTABLE PARALLEL SAFE;
 
-CREATE OR REPLACE FUNCTION 
quantiles_double_sketch_get_quantile(quantiles_double_sketch, double precision) 
RETURNS double precision
-    AS '$libdir/datasketches', 'pg_quantiles_double_sketch_get_quantile'
-    LANGUAGE C STRICT IMMUTABLE;
+CREATE OR REPLACE FUNCTION quantiles_double_sketch_merge_agg(internal, 
quantiles_double_sketch, int) RETURNS internal
+    AS '$libdir/datasketches', 'pg_quantiles_double_sketch_merge_agg'
+    LANGUAGE C IMMUTABLE PARALLEL SAFE;
 
-CREATE OR REPLACE FUNCTION 
quantiles_double_sketch_get_n(quantiles_double_sketch) RETURNS bigint
-    AS '$libdir/datasketches', 'pg_quantiles_double_sketch_get_n'
-    LANGUAGE C STRICT IMMUTABLE;
+CREATE OR REPLACE FUNCTION quantiles_double_sketch_serialize(internal) RETURNS 
bytea
+    AS '$libdir/datasketches', 'pg_quantiles_double_sketch_serialize'
+    LANGUAGE C STRICT IMMUTABLE PARALLEL SAFE;
 
-CREATE OR REPLACE FUNCTION 
quantiles_double_sketch_to_string(quantiles_double_sketch) RETURNS TEXT
-    AS '$libdir/datasketches', 'pg_quantiles_double_sketch_to_string'
-    LANGUAGE C STRICT IMMUTABLE;
-
-CREATE OR REPLACE FUNCTION quantiles_double_sketch_merge(internal, 
quantiles_double_sketch) RETURNS internal
-    AS '$libdir/datasketches', 'pg_quantiles_double_sketch_merge'
-    LANGUAGE C IMMUTABLE;
+CREATE OR REPLACE FUNCTION quantiles_double_sketch_deserialize(bytea, 
internal) RETURNS internal
+    AS '$libdir/datasketches', 'pg_quantiles_double_sketch_deserialize'
+    LANGUAGE C STRICT IMMUTABLE PARALLEL SAFE;
 
-CREATE OR REPLACE FUNCTION quantiles_double_sketch_merge(internal, 
quantiles_double_sketch, int) RETURNS internal
-    AS '$libdir/datasketches', 'pg_quantiles_double_sketch_merge'
-    LANGUAGE C IMMUTABLE;
+CREATE OR REPLACE FUNCTION quantiles_double_sketch_combine(internal, internal) 
RETURNS internal
+    AS '$libdir/datasketches', 'pg_quantiles_double_sketch_combine'
+    LANGUAGE C IMMUTABLE PARALLEL SAFE;
 
-CREATE OR REPLACE FUNCTION quantiles_double_sketch_from_internal(internal) 
RETURNS quantiles_double_sketch
-    AS '$libdir/datasketches', 'pg_quantiles_double_sketch_from_internal'
-    LANGUAGE C STRICT IMMUTABLE;
+CREATE OR REPLACE FUNCTION quantiles_double_sketch_finalize(internal) RETURNS 
quantiles_double_sketch
+    AS '$libdir/datasketches', 'pg_quantiles_double_sketch_serialize'
+    LANGUAGE C STRICT IMMUTABLE PARALLEL SAFE;
 
 CREATE AGGREGATE quantiles_double_sketch_build(double precision) (
-    sfunc = quantiles_double_sketch_add_item,
-    stype = internal,
-    finalfunc = quantiles_double_sketch_from_internal
+    STYPE = internal,
+    SFUNC = quantiles_double_sketch_build_agg,
+    COMBINEFUNC = quantiles_double_sketch_combine,
+    SERIALFUNC = quantiles_double_sketch_serialize,
+    DESERIALFUNC = quantiles_double_sketch_deserialize, 
+    FINALFUNC = quantiles_double_sketch_finalize,
+    PARALLEL = SAFE
 );
 
 CREATE AGGREGATE quantiles_double_sketch_build(double precision, int) (
-    sfunc = quantiles_double_sketch_add_item,
-    stype = internal,
-    finalfunc = quantiles_double_sketch_from_internal
+    STYPE = internal,
+    SFUNC = quantiles_double_sketch_build_agg,
+    COMBINEFUNC = quantiles_double_sketch_combine,
+    SERIALFUNC = quantiles_double_sketch_serialize,
+    DESERIALFUNC = quantiles_double_sketch_deserialize, 
+    FINALFUNC = quantiles_double_sketch_finalize,
+    PARALLEL = SAFE
 );
 
 CREATE AGGREGATE quantiles_double_sketch_merge(quantiles_double_sketch) (
-    sfunc = quantiles_double_sketch_merge,
-    stype = internal,
-    finalfunc = quantiles_double_sketch_from_internal
+    STYPE = internal,
+    SFUNC = quantiles_double_sketch_merge_agg,
+    COMBINEFUNC = quantiles_double_sketch_combine,
+    SERIALFUNC = quantiles_double_sketch_serialize,
+    DESERIALFUNC = quantiles_double_sketch_deserialize, 
+    FINALFUNC = quantiles_double_sketch_finalize,
+    PARALLEL = SAFE
 );
 
 CREATE AGGREGATE quantiles_double_sketch_merge(quantiles_double_sketch, int) (
-    sfunc = quantiles_double_sketch_merge,
-    stype = internal,
-    finalfunc = quantiles_double_sketch_from_internal
+    STYPE = internal,
+    SFUNC = quantiles_double_sketch_merge_agg,
+    COMBINEFUNC = quantiles_double_sketch_combine,
+    SERIALFUNC = quantiles_double_sketch_serialize,
+    DESERIALFUNC = quantiles_double_sketch_deserialize, 
+    FINALFUNC = quantiles_double_sketch_finalize,
+    PARALLEL = SAFE
 );
 
+CREATE OR REPLACE FUNCTION 
quantiles_double_sketch_get_rank(quantiles_double_sketch, double precision) 
RETURNS double precision
+    AS '$libdir/datasketches', 'pg_quantiles_double_sketch_get_rank'
+    LANGUAGE C STRICT IMMUTABLE PARALLEL SAFE;
+
+CREATE OR REPLACE FUNCTION 
quantiles_double_sketch_get_quantile(quantiles_double_sketch, double precision) 
RETURNS double precision
+    AS '$libdir/datasketches', 'pg_quantiles_double_sketch_get_quantile'
+    LANGUAGE C STRICT IMMUTABLE PARALLEL SAFE;
+
+CREATE OR REPLACE FUNCTION 
quantiles_double_sketch_get_n(quantiles_double_sketch) RETURNS bigint
+    AS '$libdir/datasketches', 'pg_quantiles_double_sketch_get_n'
+    LANGUAGE C STRICT IMMUTABLE PARALLEL SAFE;
+
+CREATE OR REPLACE FUNCTION 
quantiles_double_sketch_to_string(quantiles_double_sketch) RETURNS TEXT
+    AS '$libdir/datasketches', 'pg_quantiles_double_sketch_to_string'
+    LANGUAGE C STRICT IMMUTABLE PARALLEL SAFE;
+
 CREATE OR REPLACE FUNCTION 
quantiles_double_sketch_get_pmf(quantiles_double_sketch, double precision[]) 
RETURNS double precision[]
     AS '$libdir/datasketches', 'pg_quantiles_double_sketch_get_pmf'
-    LANGUAGE C STRICT IMMUTABLE;
+    LANGUAGE C STRICT IMMUTABLE PARALLEL SAFE;
 
 CREATE OR REPLACE FUNCTION 
quantiles_double_sketch_get_cdf(quantiles_double_sketch, double precision[]) 
RETURNS double precision[]
     AS '$libdir/datasketches', 'pg_quantiles_double_sketch_get_cdf'
-    LANGUAGE C STRICT IMMUTABLE;
+    LANGUAGE C STRICT IMMUTABLE PARALLEL SAFE;
 
 CREATE OR REPLACE FUNCTION 
quantiles_double_sketch_get_quantiles(quantiles_double_sketch, double 
precision[]) RETURNS double precision[]
     AS '$libdir/datasketches', 'pg_quantiles_double_sketch_get_quantiles'
-    LANGUAGE C STRICT IMMUTABLE;
+    LANGUAGE C STRICT IMMUTABLE PARALLEL SAFE;
 
 CREATE OR REPLACE FUNCTION 
quantiles_double_sketch_get_histogram(quantiles_double_sketch) RETURNS double 
precision[]
     AS '$libdir/datasketches', 'pg_quantiles_double_sketch_get_histogram'
-    LANGUAGE C STRICT IMMUTABLE;
+    LANGUAGE C STRICT IMMUTABLE PARALLEL SAFE;
 
 CREATE OR REPLACE FUNCTION 
quantiles_double_sketch_get_histogram(quantiles_double_sketch, int) RETURNS 
double precision[]
     AS '$libdir/datasketches', 'pg_quantiles_double_sketch_get_histogram'
-    LANGUAGE C STRICT IMMUTABLE;
+    LANGUAGE C STRICT IMMUTABLE PARALLEL SAFE;
diff --git a/src/quantiles_double_sketch_pg_functions.c 
b/src/quantiles_double_sketch_pg_functions.c
index 713e54e..707224e 100644
--- a/src/quantiles_double_sketch_pg_functions.c
+++ b/src/quantiles_double_sketch_pg_functions.c
@@ -25,31 +25,32 @@
 #include <catalog/pg_type.h>
 
 #include "quantiles_double_sketch_c_adapter.h"
-#include "base64.h"
 
 /* PG_FUNCTION_INFO_V1 macro to pass functions to postgres */
-PG_FUNCTION_INFO_V1(pg_quantiles_double_sketch_add_item);
+PG_FUNCTION_INFO_V1(pg_quantiles_double_sketch_build_agg);
+PG_FUNCTION_INFO_V1(pg_quantiles_double_sketch_merge_agg);
+PG_FUNCTION_INFO_V1(pg_quantiles_double_sketch_serialize);
+PG_FUNCTION_INFO_V1(pg_quantiles_double_sketch_deserialize);
+PG_FUNCTION_INFO_V1(pg_quantiles_double_sketch_combine);
 PG_FUNCTION_INFO_V1(pg_quantiles_double_sketch_get_rank);
 PG_FUNCTION_INFO_V1(pg_quantiles_double_sketch_get_quantile);
 PG_FUNCTION_INFO_V1(pg_quantiles_double_sketch_get_n);
 PG_FUNCTION_INFO_V1(pg_quantiles_double_sketch_to_string);
-PG_FUNCTION_INFO_V1(pg_quantiles_double_sketch_merge);
-PG_FUNCTION_INFO_V1(pg_quantiles_double_sketch_from_internal);
 PG_FUNCTION_INFO_V1(pg_quantiles_double_sketch_get_pmf);
 PG_FUNCTION_INFO_V1(pg_quantiles_double_sketch_get_cdf);
 PG_FUNCTION_INFO_V1(pg_quantiles_double_sketch_get_quantiles);
 PG_FUNCTION_INFO_V1(pg_quantiles_double_sketch_get_histogram);
 
 /* function declarations */
-Datum pg_quantiles_double_sketch_recv(PG_FUNCTION_ARGS);
-Datum pg_quantiles_double_sketch_send(PG_FUNCTION_ARGS);
-Datum pg_quantiles_double_sketch_add_item(PG_FUNCTION_ARGS);
+Datum pg_quantiles_double_sketch_build_agg(PG_FUNCTION_ARGS);
+Datum pg_quantiles_double_sketch_merge_agg(PG_FUNCTION_ARGS);
+Datum pg_quantiles_double_sketch_serialize(PG_FUNCTION_ARGS);
+Datum pg_quantiles_double_sketch_deserialize(PG_FUNCTION_ARGS);
+Datum pg_quantiles_double_sketch_combine(PG_FUNCTION_ARGS);
 Datum pg_quantiles_double_sketch_get_rank(PG_FUNCTION_ARGS);
 Datum pg_quantiles_double_sketch_get_quantile(PG_FUNCTION_ARGS);
 Datum pg_quantiles_double_sketch_get_n(PG_FUNCTION_ARGS);
 Datum pg_quantiles_double_sketch_to_string(PG_FUNCTION_ARGS);
-Datum pg_quantiles_double_sketch_merge(PG_FUNCTION_ARGS);
-Datum pg_quantiles_double_sketch_from_internal(PG_FUNCTION_ARGS);
 Datum pg_quantiles_double_sketch_get_pmf(PG_FUNCTION_ARGS);
 Datum pg_quantiles_double_sketch_get_cdf(PG_FUNCTION_ARGS);
 Datum pg_quantiles_double_sketch_get_quantiles(PG_FUNCTION_ARGS);
@@ -57,7 +58,7 @@ Datum 
pg_quantiles_double_sketch_get_histogram(PG_FUNCTION_ARGS);
 
 static const unsigned DEFAULT_NUM_BINS = 10;
 
-Datum pg_quantiles_double_sketch_add_item(PG_FUNCTION_ARGS) {
+Datum pg_quantiles_double_sketch_build_agg(PG_FUNCTION_ARGS) {
   void* sketchptr;
   double value;
   int k;
@@ -72,7 +73,7 @@ Datum pg_quantiles_double_sketch_add_item(PG_FUNCTION_ARGS) {
   }
 
   if (!AggCheckCallContext(fcinfo, &aggcontext)) {
-    elog(ERROR, "quantiles_double_sketch_add_item called in non-aggregate 
context");
+    elog(ERROR, "quantiles_double_sketch_build_agg called in non-aggregate 
context");
   }
   oldcontext = MemoryContextSwitchTo(aggcontext);
 
@@ -91,55 +92,7 @@ Datum pg_quantiles_double_sketch_add_item(PG_FUNCTION_ARGS) {
   PG_RETURN_POINTER(sketchptr);
 }
 
-Datum pg_quantiles_double_sketch_get_rank(PG_FUNCTION_ARGS) {
-  const bytea* bytes_in;
-  void* sketchptr;
-  double value;
-  double rank;
-  bytes_in = PG_GETARG_BYTEA_P(0);
-  sketchptr = quantiles_double_sketch_deserialize(VARDATA(bytes_in), 
VARSIZE(bytes_in) - VARHDRSZ);
-  value = PG_GETARG_FLOAT8(1);
-  rank = quantiles_double_sketch_get_rank(sketchptr, value);
-  quantiles_double_sketch_delete(sketchptr);
-  PG_RETURN_FLOAT8(rank);
-}
-
-Datum pg_quantiles_double_sketch_get_quantile(PG_FUNCTION_ARGS) {
-  const bytea* bytes_in;
-  void* sketchptr;
-  double value;
-  double rank;
-  bytes_in = PG_GETARG_BYTEA_P(0);
-  sketchptr = quantiles_double_sketch_deserialize(VARDATA(bytes_in), 
VARSIZE(bytes_in) - VARHDRSZ);
-  rank = PG_GETARG_FLOAT8(1);
-  value = quantiles_double_sketch_get_quantile(sketchptr, rank);
-  quantiles_double_sketch_delete(sketchptr);
-  PG_RETURN_FLOAT8(value);
-}
-
-Datum pg_quantiles_double_sketch_get_n(PG_FUNCTION_ARGS) {
-  const bytea* bytes_in;
-  void* sketchptr;
-  uint64 n;
-  bytes_in = PG_GETARG_BYTEA_P(0);
-  sketchptr = quantiles_double_sketch_deserialize(VARDATA(bytes_in), 
VARSIZE(bytes_in) - VARHDRSZ);
-  n = quantiles_double_sketch_get_n(sketchptr);
-  quantiles_double_sketch_delete(sketchptr);
-  PG_RETURN_INT64(n);
-}
-
-Datum pg_quantiles_double_sketch_to_string(PG_FUNCTION_ARGS) {
-  const bytea* bytes_in;
-  void* sketchptr;
-  char* str;
-  bytes_in = PG_GETARG_BYTEA_P(0);
-  sketchptr = quantiles_double_sketch_deserialize(VARDATA(bytes_in), 
VARSIZE(bytes_in) - VARHDRSZ);
-  str = quantiles_double_sketch_to_string(sketchptr);
-  quantiles_double_sketch_delete(sketchptr);
-  PG_RETURN_TEXT_P(cstring_to_text(str));
-}
-
-Datum pg_quantiles_double_sketch_merge(PG_FUNCTION_ARGS) {
+Datum pg_quantiles_double_sketch_merge_agg(PG_FUNCTION_ARGS) {
   void* unionptr;
   bytea* sketch_bytes;
   void* sketchptr;
@@ -155,7 +108,7 @@ Datum pg_quantiles_double_sketch_merge(PG_FUNCTION_ARGS) {
   }
 
   if (!AggCheckCallContext(fcinfo, &aggcontext)) {
-    elog(ERROR, "quantiles_double_sketch_merge called in non-aggregate 
context");
+    elog(ERROR, "quantiles_double_sketch_merge_agg called in non-aggregate 
context");
   }
   oldcontext = MemoryContextSwitchTo(aggcontext);
 
@@ -176,14 +129,14 @@ Datum pg_quantiles_double_sketch_merge(PG_FUNCTION_ARGS) {
   PG_RETURN_POINTER(unionptr);
 }
 
-Datum pg_quantiles_double_sketch_from_internal(PG_FUNCTION_ARGS) {
+Datum pg_quantiles_double_sketch_serialize(PG_FUNCTION_ARGS) {
   void* sketchptr;
   struct ptr_with_size bytes_out;
   MemoryContext aggcontext;
 
   if (PG_ARGISNULL(0)) PG_RETURN_NULL();
   if (!AggCheckCallContext(fcinfo, &aggcontext)) {
-    elog(ERROR, "quantiles_double_sketch_from_internal called in non-aggregate 
context");
+    elog(ERROR, "quantiles_double_sketch_serialize called in non-aggregate 
context");
   }
   sketchptr = PG_GETARG_POINTER(0);
   bytes_out = quantiles_double_sketch_serialize(sketchptr, VARHDRSZ);
@@ -192,6 +145,109 @@ Datum 
pg_quantiles_double_sketch_from_internal(PG_FUNCTION_ARGS) {
   PG_RETURN_BYTEA_P(bytes_out.ptr);
 }
 
+Datum pg_quantiles_double_sketch_deserialize(PG_FUNCTION_ARGS) {
+  const bytea* bytes_in;
+  void* sketchptr;
+
+  MemoryContext oldcontext;
+  MemoryContext aggcontext;
+
+  if (PG_ARGISNULL(0)) PG_RETURN_NULL();
+
+  if (!AggCheckCallContext(fcinfo, &aggcontext)) {
+    elog(ERROR, "quantiles_double_sketch_deserialize called in non-aggregate 
context");
+  }
+  oldcontext = MemoryContextSwitchTo(aggcontext);
+
+  bytes_in = PG_GETARG_BYTEA_P(0);
+  sketchptr = quantiles_double_sketch_deserialize(VARDATA(bytes_in), 
VARSIZE(bytes_in) - VARHDRSZ);
+
+  MemoryContextSwitchTo(oldcontext);
+
+  PG_RETURN_POINTER(sketchptr);
+}
+
+Datum pg_quantiles_double_sketch_combine(PG_FUNCTION_ARGS) {
+  void* sketchptr1;
+  void* sketchptr2;
+  void* sketchptr;
+
+  MemoryContext oldcontext;
+  MemoryContext aggcontext;
+
+  if (PG_ARGISNULL(0) && PG_ARGISNULL(1)) PG_RETURN_NULL();
+
+  if (!AggCheckCallContext(fcinfo, &aggcontext)) {
+    elog(ERROR, "quantiles_double_sketch_combine called in non-aggregate 
context");
+  }
+  oldcontext = MemoryContextSwitchTo(aggcontext);
+
+  sketchptr1 = PG_GETARG_POINTER(0);
+  sketchptr2 = PG_GETARG_POINTER(1);
+
+  if (sketchptr1) {
+    sketchptr = sketchptr1;
+    if (sketchptr2) {
+      quantiles_double_sketch_merge(sketchptr, sketchptr2);
+    }
+    quantiles_double_sketch_delete(sketchptr2);
+  } else {
+    sketchptr = sketchptr2;
+  }
+
+  MemoryContextSwitchTo(oldcontext);
+
+  PG_RETURN_POINTER(sketchptr);
+}
+
+Datum pg_quantiles_double_sketch_get_rank(PG_FUNCTION_ARGS) {
+  const bytea* bytes_in;
+  void* sketchptr;
+  double value;
+  double rank;
+  bytes_in = PG_GETARG_BYTEA_P(0);
+  sketchptr = quantiles_double_sketch_deserialize(VARDATA(bytes_in), 
VARSIZE(bytes_in) - VARHDRSZ);
+  value = PG_GETARG_FLOAT8(1);
+  rank = quantiles_double_sketch_get_rank(sketchptr, value);
+  quantiles_double_sketch_delete(sketchptr);
+  PG_RETURN_FLOAT8(rank);
+}
+
+Datum pg_quantiles_double_sketch_get_quantile(PG_FUNCTION_ARGS) {
+  const bytea* bytes_in;
+  void* sketchptr;
+  double value;
+  double rank;
+  bytes_in = PG_GETARG_BYTEA_P(0);
+  sketchptr = quantiles_double_sketch_deserialize(VARDATA(bytes_in), 
VARSIZE(bytes_in) - VARHDRSZ);
+  rank = PG_GETARG_FLOAT8(1);
+  value = quantiles_double_sketch_get_quantile(sketchptr, rank);
+  quantiles_double_sketch_delete(sketchptr);
+  PG_RETURN_FLOAT8(value);
+}
+
+Datum pg_quantiles_double_sketch_get_n(PG_FUNCTION_ARGS) {
+  const bytea* bytes_in;
+  void* sketchptr;
+  uint64 n;
+  bytes_in = PG_GETARG_BYTEA_P(0);
+  sketchptr = quantiles_double_sketch_deserialize(VARDATA(bytes_in), 
VARSIZE(bytes_in) - VARHDRSZ);
+  n = quantiles_double_sketch_get_n(sketchptr);
+  quantiles_double_sketch_delete(sketchptr);
+  PG_RETURN_INT64(n);
+}
+
+Datum pg_quantiles_double_sketch_to_string(PG_FUNCTION_ARGS) {
+  const bytea* bytes_in;
+  void* sketchptr;
+  char* str;
+  bytes_in = PG_GETARG_BYTEA_P(0);
+  sketchptr = quantiles_double_sketch_deserialize(VARDATA(bytes_in), 
VARSIZE(bytes_in) - VARHDRSZ);
+  str = quantiles_double_sketch_to_string(sketchptr);
+  quantiles_double_sketch_delete(sketchptr);
+  PG_RETURN_TEXT_P(cstring_to_text(str));
+}
+
 Datum pg_quantiles_double_sketch_get_pmf(PG_FUNCTION_ARGS) {
   const bytea* bytes_in;
   void* sketchptr;


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to