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 66cac95 KLL sketch partial aggregation support
66cac95 is described below
commit 66cac9573f5217d3cab81db84413c78ae9332edf
Author: AlexanderSaydakov <[email protected]>
AuthorDate: Fri Apr 21 12:43:31 2023 -0700
KLL sketch partial aggregation support
---
sql/datasketches_kll_double_sketch.sql | 122 +++++++++++++---------
sql/datasketches_kll_float_sketch.sql | 122 +++++++++++++---------
src/kll_double_sketch_pg_functions.c | 182 +++++++++++++++++++++------------
src/kll_float_sketch_pg_functions.c | 182 +++++++++++++++++++++------------
4 files changed, 388 insertions(+), 220 deletions(-)
diff --git a/sql/datasketches_kll_double_sketch.sql
b/sql/datasketches_kll_double_sketch.sql
index b606814..630143d 100644
--- a/sql/datasketches_kll_double_sketch.sql
+++ b/sql/datasketches_kll_double_sketch.sql
@@ -19,11 +19,11 @@ CREATE TYPE kll_double_sketch;
CREATE OR REPLACE FUNCTION kll_double_sketch_in(cstring) RETURNS
kll_double_sketch
AS '$libdir/datasketches', 'pg_sketch_in'
- LANGUAGE C STRICT IMMUTABLE;
+ LANGUAGE C STRICT IMMUTABLE PARALLEL SAFE;
CREATE OR REPLACE FUNCTION kll_double_sketch_out(kll_double_sketch) RETURNS
cstring
AS '$libdir/datasketches', 'pg_sketch_out'
- LANGUAGE C STRICT IMMUTABLE;
+ LANGUAGE C STRICT IMMUTABLE PARALLEL SAFE;
CREATE TYPE kll_double_sketch (
INPUT = kll_double_sketch_in,
@@ -34,82 +34,110 @@ CREATE TYPE kll_double_sketch (
CREATE CAST (bytea as kll_double_sketch) WITHOUT FUNCTION AS ASSIGNMENT;
CREATE CAST (kll_double_sketch as bytea) WITHOUT FUNCTION AS ASSIGNMENT;
-CREATE OR REPLACE FUNCTION kll_double_sketch_add_item(internal, double
precision) RETURNS internal
- AS '$libdir/datasketches', 'pg_kll_double_sketch_add_item'
- LANGUAGE C IMMUTABLE;
+CREATE OR REPLACE FUNCTION kll_double_sketch_build_agg(internal, double
precision) RETURNS internal
+ AS '$libdir/datasketches', 'pg_kll_double_sketch_build_agg'
+ LANGUAGE C IMMUTABLE PARALLEL SAFE;
-CREATE OR REPLACE FUNCTION kll_double_sketch_add_item(internal, double
precision, int) RETURNS internal
- AS '$libdir/datasketches', 'pg_kll_double_sketch_add_item'
- LANGUAGE C IMMUTABLE;
+CREATE OR REPLACE FUNCTION kll_double_sketch_build_agg(internal, double
precision, int) RETURNS internal
+ AS '$libdir/datasketches', 'pg_kll_double_sketch_build_agg'
+ LANGUAGE C IMMUTABLE PARALLEL SAFE;
-CREATE OR REPLACE FUNCTION kll_double_sketch_get_rank(kll_double_sketch,
double precision) RETURNS double precision
- AS '$libdir/datasketches', 'pg_kll_double_sketch_get_rank'
- LANGUAGE C STRICT IMMUTABLE;
+CREATE OR REPLACE FUNCTION kll_double_sketch_merge_agg(internal,
kll_double_sketch) RETURNS internal
+ AS '$libdir/datasketches', 'pg_kll_double_sketch_merge_agg'
+ LANGUAGE C IMMUTABLE PARALLEL SAFE;
-CREATE OR REPLACE FUNCTION kll_double_sketch_get_quantile(kll_double_sketch,
double precision) RETURNS double precision
- AS '$libdir/datasketches', 'pg_kll_double_sketch_get_quantile'
- LANGUAGE C STRICT IMMUTABLE;
+CREATE OR REPLACE FUNCTION kll_double_sketch_merge_agg(internal,
kll_double_sketch, int) RETURNS internal
+ AS '$libdir/datasketches', 'pg_kll_double_sketch_merge_agg'
+ LANGUAGE C IMMUTABLE PARALLEL SAFE;
-CREATE OR REPLACE FUNCTION kll_double_sketch_get_n(kll_double_sketch) RETURNS
bigint
- AS '$libdir/datasketches', 'pg_kll_double_sketch_get_n'
- LANGUAGE C STRICT IMMUTABLE;
+CREATE OR REPLACE FUNCTION kll_double_sketch_serialize(internal) RETURNS bytea
+ AS '$libdir/datasketches', 'pg_kll_double_sketch_serialize'
+ LANGUAGE C STRICT IMMUTABLE PARALLEL SAFE;
-CREATE OR REPLACE FUNCTION kll_double_sketch_to_string(kll_double_sketch)
RETURNS TEXT
- AS '$libdir/datasketches', 'pg_kll_double_sketch_to_string'
- LANGUAGE C STRICT IMMUTABLE;
-
-CREATE OR REPLACE FUNCTION kll_double_sketch_merge(internal,
kll_double_sketch) RETURNS internal
- AS '$libdir/datasketches', 'pg_kll_double_sketch_merge'
- LANGUAGE C IMMUTABLE;
+CREATE OR REPLACE FUNCTION kll_double_sketch_deserialize(bytea, internal)
RETURNS internal
+ AS '$libdir/datasketches', 'pg_kll_double_sketch_deserialize'
+ LANGUAGE C STRICT IMMUTABLE PARALLEL SAFE;
-CREATE OR REPLACE FUNCTION kll_double_sketch_merge(internal,
kll_double_sketch, int) RETURNS internal
- AS '$libdir/datasketches', 'pg_kll_double_sketch_merge'
- LANGUAGE C IMMUTABLE;
+CREATE OR REPLACE FUNCTION kll_double_sketch_combine(internal, internal)
RETURNS internal
+ AS '$libdir/datasketches', 'pg_kll_double_sketch_combine'
+ LANGUAGE C IMMUTABLE PARALLEL SAFE;
-CREATE OR REPLACE FUNCTION kll_double_sketch_from_internal(internal) RETURNS
kll_double_sketch
- AS '$libdir/datasketches', 'pg_kll_double_sketch_from_internal'
- LANGUAGE C STRICT IMMUTABLE;
+CREATE OR REPLACE FUNCTION kll_double_sketch_finalize(internal) RETURNS
kll_double_sketch
+ AS '$libdir/datasketches', 'pg_kll_double_sketch_serialize'
+ LANGUAGE C STRICT IMMUTABLE PARALLEL SAFE;
CREATE AGGREGATE kll_double_sketch_build(double precision) (
- sfunc = kll_double_sketch_add_item,
- stype = internal,
- finalfunc = kll_double_sketch_from_internal
+ STYPE = internal,
+ SFUNC = kll_double_sketch_build_agg,
+ COMBINEFUNC = kll_double_sketch_combine,
+ SERIALFUNC = kll_double_sketch_serialize,
+ DESERIALFUNC = kll_double_sketch_deserialize,
+ FINALFUNC = kll_double_sketch_finalize,
+ PARALLEL = SAFE
);
CREATE AGGREGATE kll_double_sketch_build(double precision, int) (
- sfunc = kll_double_sketch_add_item,
- stype = internal,
- finalfunc = kll_double_sketch_from_internal
+ STYPE = internal,
+ SFUNC = kll_double_sketch_build_agg,
+ COMBINEFUNC = kll_double_sketch_combine,
+ SERIALFUNC = kll_double_sketch_serialize,
+ DESERIALFUNC = kll_double_sketch_deserialize,
+ FINALFUNC = kll_double_sketch_finalize,
+ PARALLEL = SAFE
);
CREATE AGGREGATE kll_double_sketch_merge(kll_double_sketch) (
- sfunc = kll_double_sketch_merge,
- stype = internal,
- finalfunc = kll_double_sketch_from_internal
+ STYPE = internal,
+ SFUNC = kll_double_sketch_merge_agg,
+ COMBINEFUNC = kll_double_sketch_combine,
+ SERIALFUNC = kll_double_sketch_serialize,
+ DESERIALFUNC = kll_double_sketch_deserialize,
+ FINALFUNC = kll_double_sketch_finalize,
+ PARALLEL = SAFE
);
CREATE AGGREGATE kll_double_sketch_merge(kll_double_sketch, int) (
- sfunc = kll_double_sketch_merge,
- stype = internal,
- finalfunc = kll_double_sketch_from_internal
+ STYPE = internal,
+ SFUNC = kll_double_sketch_merge_agg,
+ COMBINEFUNC = kll_double_sketch_combine,
+ SERIALFUNC = kll_double_sketch_serialize,
+ DESERIALFUNC = kll_double_sketch_deserialize,
+ FINALFUNC = kll_double_sketch_finalize,
+ PARALLEL = SAFE
);
+CREATE OR REPLACE FUNCTION kll_double_sketch_get_rank(kll_double_sketch,
double precision) RETURNS double precision
+ AS '$libdir/datasketches', 'pg_kll_double_sketch_get_rank'
+ LANGUAGE C STRICT IMMUTABLE PARALLEL SAFE;
+
+CREATE OR REPLACE FUNCTION kll_double_sketch_get_quantile(kll_double_sketch,
double precision) RETURNS double precision
+ AS '$libdir/datasketches', 'pg_kll_double_sketch_get_quantile'
+ LANGUAGE C STRICT IMMUTABLE PARALLEL SAFE;
+
+CREATE OR REPLACE FUNCTION kll_double_sketch_get_n(kll_double_sketch) RETURNS
bigint
+ AS '$libdir/datasketches', 'pg_kll_double_sketch_get_n'
+ LANGUAGE C STRICT IMMUTABLE PARALLEL SAFE;
+
+CREATE OR REPLACE FUNCTION kll_double_sketch_to_string(kll_double_sketch)
RETURNS TEXT
+ AS '$libdir/datasketches', 'pg_kll_double_sketch_to_string'
+ LANGUAGE C STRICT IMMUTABLE PARALLEL SAFE;
+
CREATE OR REPLACE FUNCTION kll_double_sketch_get_pmf(kll_double_sketch, double
precision[]) RETURNS double precision[]
AS '$libdir/datasketches', 'pg_kll_double_sketch_get_pmf'
- LANGUAGE C STRICT IMMUTABLE;
+ LANGUAGE C STRICT IMMUTABLE PARALLEL SAFE;
CREATE OR REPLACE FUNCTION kll_double_sketch_get_cdf(kll_double_sketch, double
precision[]) RETURNS double precision[]
AS '$libdir/datasketches', 'pg_kll_double_sketch_get_cdf'
- LANGUAGE C STRICT IMMUTABLE;
+ LANGUAGE C STRICT IMMUTABLE PARALLEL SAFE;
CREATE OR REPLACE FUNCTION kll_double_sketch_get_quantiles(kll_double_sketch,
double precision[]) RETURNS double precision[]
AS '$libdir/datasketches', 'pg_kll_double_sketch_get_quantiles'
- LANGUAGE C STRICT IMMUTABLE;
+ LANGUAGE C STRICT IMMUTABLE PARALLEL SAFE;
CREATE OR REPLACE FUNCTION kll_double_sketch_get_histogram(kll_double_sketch)
RETURNS double precision[]
AS '$libdir/datasketches', 'pg_kll_double_sketch_get_histogram'
- LANGUAGE C STRICT IMMUTABLE;
+ LANGUAGE C STRICT IMMUTABLE PARALLEL SAFE;
CREATE OR REPLACE FUNCTION kll_double_sketch_get_histogram(kll_double_sketch,
int) RETURNS double precision[]
AS '$libdir/datasketches', 'pg_kll_double_sketch_get_histogram'
- LANGUAGE C STRICT IMMUTABLE;
+ LANGUAGE C STRICT IMMUTABLE PARALLEL SAFE;
diff --git a/sql/datasketches_kll_float_sketch.sql
b/sql/datasketches_kll_float_sketch.sql
index 332496f..426b16b 100644
--- a/sql/datasketches_kll_float_sketch.sql
+++ b/sql/datasketches_kll_float_sketch.sql
@@ -19,11 +19,11 @@ CREATE TYPE kll_float_sketch;
CREATE OR REPLACE FUNCTION kll_float_sketch_in(cstring) RETURNS
kll_float_sketch
AS '$libdir/datasketches', 'pg_sketch_in'
- LANGUAGE C STRICT IMMUTABLE;
+ LANGUAGE C STRICT IMMUTABLE PARALLEL SAFE;
CREATE OR REPLACE FUNCTION kll_float_sketch_out(kll_float_sketch) RETURNS
cstring
AS '$libdir/datasketches', 'pg_sketch_out'
- LANGUAGE C STRICT IMMUTABLE;
+ LANGUAGE C STRICT IMMUTABLE PARALLEL SAFE;
CREATE TYPE kll_float_sketch (
INPUT = kll_float_sketch_in,
@@ -34,82 +34,110 @@ CREATE TYPE kll_float_sketch (
CREATE CAST (bytea as kll_float_sketch) WITHOUT FUNCTION AS ASSIGNMENT;
CREATE CAST (kll_float_sketch as bytea) WITHOUT FUNCTION AS ASSIGNMENT;
-CREATE OR REPLACE FUNCTION kll_float_sketch_add_item(internal, real) RETURNS
internal
- AS '$libdir/datasketches', 'pg_kll_float_sketch_add_item'
- LANGUAGE C IMMUTABLE;
+CREATE OR REPLACE FUNCTION kll_float_sketch_build_agg(internal, real) RETURNS
internal
+ AS '$libdir/datasketches', 'pg_kll_float_sketch_build_agg'
+ LANGUAGE C IMMUTABLE PARALLEL SAFE;
-CREATE OR REPLACE FUNCTION kll_float_sketch_add_item(internal, real, int)
RETURNS internal
- AS '$libdir/datasketches', 'pg_kll_float_sketch_add_item'
- LANGUAGE C IMMUTABLE;
+CREATE OR REPLACE FUNCTION kll_float_sketch_build_agg(internal, real, int)
RETURNS internal
+ AS '$libdir/datasketches', 'pg_kll_float_sketch_build_agg'
+ LANGUAGE C IMMUTABLE PARALLEL SAFE;
-CREATE OR REPLACE FUNCTION kll_float_sketch_get_rank(kll_float_sketch, real)
RETURNS double precision
- AS '$libdir/datasketches', 'pg_kll_float_sketch_get_rank'
- LANGUAGE C STRICT IMMUTABLE;
+CREATE OR REPLACE FUNCTION kll_float_sketch_merge_agg(internal,
kll_float_sketch) RETURNS internal
+ AS '$libdir/datasketches', 'pg_kll_float_sketch_merge_agg'
+ LANGUAGE C IMMUTABLE PARALLEL SAFE;
-CREATE OR REPLACE FUNCTION kll_float_sketch_get_quantile(kll_float_sketch,
double precision) RETURNS real
- AS '$libdir/datasketches', 'pg_kll_float_sketch_get_quantile'
- LANGUAGE C STRICT IMMUTABLE;
+CREATE OR REPLACE FUNCTION kll_float_sketch_merge_agg(internal,
kll_float_sketch, int) RETURNS internal
+ AS '$libdir/datasketches', 'pg_kll_float_sketch_merge_agg'
+ LANGUAGE C IMMUTABLE PARALLEL SAFE;
-CREATE OR REPLACE FUNCTION kll_float_sketch_get_n(kll_float_sketch) RETURNS
bigint
- AS '$libdir/datasketches', 'pg_kll_float_sketch_get_n'
- LANGUAGE C STRICT IMMUTABLE;
+CREATE OR REPLACE FUNCTION kll_float_sketch_serialize(internal) RETURNS bytea
+ AS '$libdir/datasketches', 'pg_kll_float_sketch_serialize'
+ LANGUAGE C STRICT IMMUTABLE PARALLEL SAFE;
-CREATE OR REPLACE FUNCTION kll_float_sketch_to_string(kll_float_sketch)
RETURNS TEXT
- AS '$libdir/datasketches', 'pg_kll_float_sketch_to_string'
- LANGUAGE C STRICT IMMUTABLE;
-
-CREATE OR REPLACE FUNCTION kll_float_sketch_merge(internal, kll_float_sketch)
RETURNS internal
- AS '$libdir/datasketches', 'pg_kll_float_sketch_merge'
- LANGUAGE C IMMUTABLE;
+CREATE OR REPLACE FUNCTION kll_float_sketch_deserialize(bytea, internal)
RETURNS internal
+ AS '$libdir/datasketches', 'pg_kll_float_sketch_deserialize'
+ LANGUAGE C STRICT IMMUTABLE PARALLEL SAFE;
-CREATE OR REPLACE FUNCTION kll_float_sketch_merge(internal, kll_float_sketch,
int) RETURNS internal
- AS '$libdir/datasketches', 'pg_kll_float_sketch_merge'
- LANGUAGE C IMMUTABLE;
+CREATE OR REPLACE FUNCTION kll_float_sketch_combine(internal, internal)
RETURNS internal
+ AS '$libdir/datasketches', 'pg_kll_float_sketch_combine'
+ LANGUAGE C IMMUTABLE PARALLEL SAFE;
-CREATE OR REPLACE FUNCTION kll_float_sketch_from_internal(internal) RETURNS
kll_float_sketch
- AS '$libdir/datasketches', 'pg_kll_float_sketch_from_internal'
- LANGUAGE C STRICT IMMUTABLE;
+CREATE OR REPLACE FUNCTION kll_float_sketch_finalize(internal) RETURNS
kll_float_sketch
+ AS '$libdir/datasketches', 'pg_kll_float_sketch_serialize'
+ LANGUAGE C STRICT IMMUTABLE PARALLEL SAFE;
CREATE AGGREGATE kll_float_sketch_build(real) (
- sfunc = kll_float_sketch_add_item,
- stype = internal,
- finalfunc = kll_float_sketch_from_internal
+ STYPE = internal,
+ SFUNC = kll_float_sketch_build_agg,
+ COMBINEFUNC = kll_float_sketch_combine,
+ SERIALFUNC = kll_float_sketch_serialize,
+ DESERIALFUNC = kll_float_sketch_deserialize,
+ FINALFUNC = kll_float_sketch_finalize,
+ PARALLEL = SAFE
);
CREATE AGGREGATE kll_float_sketch_build(real, int) (
- sfunc = kll_float_sketch_add_item,
- stype = internal,
- finalfunc = kll_float_sketch_from_internal
+ STYPE = internal,
+ SFUNC = kll_float_sketch_build_agg,
+ COMBINEFUNC = kll_float_sketch_combine,
+ SERIALFUNC = kll_float_sketch_serialize,
+ DESERIALFUNC = kll_float_sketch_deserialize,
+ FINALFUNC = kll_float_sketch_finalize,
+ PARALLEL = SAFE
);
CREATE AGGREGATE kll_float_sketch_merge(kll_float_sketch) (
- sfunc = kll_float_sketch_merge,
- stype = internal,
- finalfunc = kll_float_sketch_from_internal
+ STYPE = internal,
+ SFUNC = kll_float_sketch_merge_agg,
+ COMBINEFUNC = kll_float_sketch_combine,
+ SERIALFUNC = kll_float_sketch_serialize,
+ DESERIALFUNC = kll_float_sketch_deserialize,
+ FINALFUNC = kll_float_sketch_finalize,
+ PARALLEL = SAFE
);
CREATE AGGREGATE kll_float_sketch_merge(kll_float_sketch, int) (
- sfunc = kll_float_sketch_merge,
- stype = internal,
- finalfunc = kll_float_sketch_from_internal
+ STYPE = internal,
+ SFUNC = kll_float_sketch_merge_agg,
+ COMBINEFUNC = kll_float_sketch_combine,
+ SERIALFUNC = kll_float_sketch_serialize,
+ DESERIALFUNC = kll_float_sketch_deserialize,
+ FINALFUNC = kll_float_sketch_finalize,
+ PARALLEL = SAFE
);
+CREATE OR REPLACE FUNCTION kll_float_sketch_get_rank(kll_float_sketch, real)
RETURNS double precision
+ AS '$libdir/datasketches', 'pg_kll_float_sketch_get_rank'
+ LANGUAGE C STRICT IMMUTABLE PARALLEL SAFE;
+
+CREATE OR REPLACE FUNCTION kll_float_sketch_get_quantile(kll_float_sketch,
double precision) RETURNS real
+ AS '$libdir/datasketches', 'pg_kll_float_sketch_get_quantile'
+ LANGUAGE C STRICT IMMUTABLE PARALLEL SAFE;
+
+CREATE OR REPLACE FUNCTION kll_float_sketch_get_n(kll_float_sketch) RETURNS
bigint
+ AS '$libdir/datasketches', 'pg_kll_float_sketch_get_n'
+ LANGUAGE C STRICT IMMUTABLE PARALLEL SAFE;
+
+CREATE OR REPLACE FUNCTION kll_float_sketch_to_string(kll_float_sketch)
RETURNS TEXT
+ AS '$libdir/datasketches', 'pg_kll_float_sketch_to_string'
+ LANGUAGE C STRICT IMMUTABLE PARALLEL SAFE;
+
CREATE OR REPLACE FUNCTION kll_float_sketch_get_pmf(kll_float_sketch, real[])
RETURNS double precision[]
AS '$libdir/datasketches', 'pg_kll_float_sketch_get_pmf'
- LANGUAGE C STRICT IMMUTABLE;
+ LANGUAGE C STRICT IMMUTABLE PARALLEL SAFE;
CREATE OR REPLACE FUNCTION kll_float_sketch_get_cdf(kll_float_sketch, real[])
RETURNS double precision[]
AS '$libdir/datasketches', 'pg_kll_float_sketch_get_cdf'
- LANGUAGE C STRICT IMMUTABLE;
+ LANGUAGE C STRICT IMMUTABLE PARALLEL SAFE;
CREATE OR REPLACE FUNCTION kll_float_sketch_get_quantiles(kll_float_sketch,
double precision[]) RETURNS real[]
AS '$libdir/datasketches', 'pg_kll_float_sketch_get_quantiles'
- LANGUAGE C STRICT IMMUTABLE;
+ LANGUAGE C STRICT IMMUTABLE PARALLEL SAFE;
CREATE OR REPLACE FUNCTION kll_float_sketch_get_histogram(kll_float_sketch)
RETURNS double precision[]
AS '$libdir/datasketches', 'pg_kll_float_sketch_get_histogram'
- LANGUAGE C STRICT IMMUTABLE;
+ LANGUAGE C STRICT IMMUTABLE PARALLEL SAFE;
CREATE OR REPLACE FUNCTION kll_float_sketch_get_histogram(kll_float_sketch,
int) RETURNS double precision[]
AS '$libdir/datasketches', 'pg_kll_float_sketch_get_histogram'
- LANGUAGE C STRICT IMMUTABLE;
+ LANGUAGE C STRICT IMMUTABLE PARALLEL SAFE;
diff --git a/src/kll_double_sketch_pg_functions.c
b/src/kll_double_sketch_pg_functions.c
index 4810839..8942a98 100644
--- a/src/kll_double_sketch_pg_functions.c
+++ b/src/kll_double_sketch_pg_functions.c
@@ -25,31 +25,32 @@
#include <catalog/pg_type.h>
#include "kll_double_sketch_c_adapter.h"
-#include "base64.h"
/* PG_FUNCTION_INFO_V1 macro to pass functions to postgres */
-PG_FUNCTION_INFO_V1(pg_kll_double_sketch_add_item);
+PG_FUNCTION_INFO_V1(pg_kll_double_sketch_build_agg);
+PG_FUNCTION_INFO_V1(pg_kll_double_sketch_merge_agg);
+PG_FUNCTION_INFO_V1(pg_kll_double_sketch_serialize);
+PG_FUNCTION_INFO_V1(pg_kll_double_sketch_deserialize);
+PG_FUNCTION_INFO_V1(pg_kll_double_sketch_combine);
PG_FUNCTION_INFO_V1(pg_kll_double_sketch_get_rank);
PG_FUNCTION_INFO_V1(pg_kll_double_sketch_get_quantile);
PG_FUNCTION_INFO_V1(pg_kll_double_sketch_get_n);
PG_FUNCTION_INFO_V1(pg_kll_double_sketch_to_string);
-PG_FUNCTION_INFO_V1(pg_kll_double_sketch_merge);
-PG_FUNCTION_INFO_V1(pg_kll_double_sketch_from_internal);
PG_FUNCTION_INFO_V1(pg_kll_double_sketch_get_pmf);
PG_FUNCTION_INFO_V1(pg_kll_double_sketch_get_cdf);
PG_FUNCTION_INFO_V1(pg_kll_double_sketch_get_quantiles);
PG_FUNCTION_INFO_V1(pg_kll_double_sketch_get_histogram);
/* function declarations */
-Datum pg_kll_double_sketch_recv(PG_FUNCTION_ARGS);
-Datum pg_kll_double_sketch_send(PG_FUNCTION_ARGS);
-Datum pg_kll_double_sketch_add_item(PG_FUNCTION_ARGS);
+Datum pg_kll_double_sketch_build_agg(PG_FUNCTION_ARGS);
+Datum pg_kll_double_sketch_merge_agg(PG_FUNCTION_ARGS);
+Datum pg_kll_double_sketch_serialize(PG_FUNCTION_ARGS);
+Datum pg_kll_double_sketch_deserialize(PG_FUNCTION_ARGS);
+Datum pg_kll_double_sketch_combine(PG_FUNCTION_ARGS);
Datum pg_kll_double_sketch_get_rank(PG_FUNCTION_ARGS);
Datum pg_kll_double_sketch_get_quantile(PG_FUNCTION_ARGS);
Datum pg_kll_double_sketch_get_n(PG_FUNCTION_ARGS);
Datum pg_kll_double_sketch_to_string(PG_FUNCTION_ARGS);
-Datum pg_kll_double_sketch_merge(PG_FUNCTION_ARGS);
-Datum pg_kll_double_sketch_from_internal(PG_FUNCTION_ARGS);
Datum pg_kll_double_sketch_get_pmf(PG_FUNCTION_ARGS);
Datum pg_kll_double_sketch_get_cdf(PG_FUNCTION_ARGS);
Datum pg_kll_double_sketch_get_quantiles(PG_FUNCTION_ARGS);
@@ -57,7 +58,7 @@ Datum pg_kll_double_sketch_get_histogram(PG_FUNCTION_ARGS);
static const unsigned DEFAULT_NUM_BINS = 10;
-Datum pg_kll_double_sketch_add_item(PG_FUNCTION_ARGS) {
+Datum pg_kll_double_sketch_build_agg(PG_FUNCTION_ARGS) {
void* sketchptr;
double value;
int k;
@@ -72,7 +73,7 @@ Datum pg_kll_double_sketch_add_item(PG_FUNCTION_ARGS) {
}
if (!AggCheckCallContext(fcinfo, &aggcontext)) {
- elog(ERROR, "kll_double_sketch_add_item called in non-aggregate context");
+ elog(ERROR, "kll_double_sketch_build_agg called in non-aggregate context");
}
oldcontext = MemoryContextSwitchTo(aggcontext);
@@ -91,55 +92,7 @@ Datum pg_kll_double_sketch_add_item(PG_FUNCTION_ARGS) {
PG_RETURN_POINTER(sketchptr);
}
-Datum pg_kll_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 = kll_double_sketch_deserialize(VARDATA(bytes_in),
VARSIZE(bytes_in) - VARHDRSZ);
- value = PG_GETARG_FLOAT8(1);
- rank = kll_double_sketch_get_rank(sketchptr, value);
- kll_double_sketch_delete(sketchptr);
- PG_RETURN_FLOAT8(rank);
-}
-
-Datum pg_kll_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 = kll_double_sketch_deserialize(VARDATA(bytes_in),
VARSIZE(bytes_in) - VARHDRSZ);
- rank = PG_GETARG_FLOAT8(1);
- value = kll_double_sketch_get_quantile(sketchptr, rank);
- kll_double_sketch_delete(sketchptr);
- PG_RETURN_FLOAT8(value);
-}
-
-Datum pg_kll_double_sketch_get_n(PG_FUNCTION_ARGS) {
- const bytea* bytes_in;
- void* sketchptr;
- uint64 n;
- bytes_in = PG_GETARG_BYTEA_P(0);
- sketchptr = kll_double_sketch_deserialize(VARDATA(bytes_in),
VARSIZE(bytes_in) - VARHDRSZ);
- n = kll_double_sketch_get_n(sketchptr);
- kll_double_sketch_delete(sketchptr);
- PG_RETURN_INT64(n);
-}
-
-Datum pg_kll_double_sketch_to_string(PG_FUNCTION_ARGS) {
- const bytea* bytes_in;
- void* sketchptr;
- char* str;
- bytes_in = PG_GETARG_BYTEA_P(0);
- sketchptr = kll_double_sketch_deserialize(VARDATA(bytes_in),
VARSIZE(bytes_in) - VARHDRSZ);
- str = kll_double_sketch_to_string(sketchptr);
- kll_double_sketch_delete(sketchptr);
- PG_RETURN_TEXT_P(cstring_to_text(str));
-}
-
-Datum pg_kll_double_sketch_merge(PG_FUNCTION_ARGS) {
+Datum pg_kll_double_sketch_merge_agg(PG_FUNCTION_ARGS) {
void* unionptr;
bytea* sketch_bytes;
void* sketchptr;
@@ -155,7 +108,7 @@ Datum pg_kll_double_sketch_merge(PG_FUNCTION_ARGS) {
}
if (!AggCheckCallContext(fcinfo, &aggcontext)) {
- elog(ERROR, "kll_double_sketch_merge called in non-aggregate context");
+ elog(ERROR, "kll_double_sketch_merge_agg called in non-aggregate context");
}
oldcontext = MemoryContextSwitchTo(aggcontext);
@@ -176,14 +129,14 @@ Datum pg_kll_double_sketch_merge(PG_FUNCTION_ARGS) {
PG_RETURN_POINTER(unionptr);
}
-Datum pg_kll_double_sketch_from_internal(PG_FUNCTION_ARGS) {
+Datum pg_kll_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, "kll_double_sketch_from_internal called in non-aggregate
context");
+ elog(ERROR, "kll_double_sketch_serialize called in non-aggregate context");
}
sketchptr = PG_GETARG_POINTER(0);
bytes_out = kll_double_sketch_serialize(sketchptr, VARHDRSZ);
@@ -192,6 +145,109 @@ Datum
pg_kll_double_sketch_from_internal(PG_FUNCTION_ARGS) {
PG_RETURN_BYTEA_P(bytes_out.ptr);
}
+Datum pg_kll_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, "kll_double_sketch_deserialize called in non-aggregate
context");
+ }
+ oldcontext = MemoryContextSwitchTo(aggcontext);
+
+ bytes_in = PG_GETARG_BYTEA_P(0);
+ sketchptr = kll_double_sketch_deserialize(VARDATA(bytes_in),
VARSIZE(bytes_in) - VARHDRSZ);
+
+ MemoryContextSwitchTo(oldcontext);
+
+ PG_RETURN_POINTER(sketchptr);
+}
+
+Datum pg_kll_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, "kll_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) {
+ kll_double_sketch_merge(sketchptr, sketchptr2);
+ }
+ kll_double_sketch_delete(sketchptr2);
+ } else {
+ sketchptr = sketchptr2;
+ }
+
+ MemoryContextSwitchTo(oldcontext);
+
+ PG_RETURN_POINTER(sketchptr);
+}
+
+Datum pg_kll_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 = kll_double_sketch_deserialize(VARDATA(bytes_in),
VARSIZE(bytes_in) - VARHDRSZ);
+ value = PG_GETARG_FLOAT8(1);
+ rank = kll_double_sketch_get_rank(sketchptr, value);
+ kll_double_sketch_delete(sketchptr);
+ PG_RETURN_FLOAT8(rank);
+}
+
+Datum pg_kll_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 = kll_double_sketch_deserialize(VARDATA(bytes_in),
VARSIZE(bytes_in) - VARHDRSZ);
+ rank = PG_GETARG_FLOAT8(1);
+ value = kll_double_sketch_get_quantile(sketchptr, rank);
+ kll_double_sketch_delete(sketchptr);
+ PG_RETURN_FLOAT8(value);
+}
+
+Datum pg_kll_double_sketch_get_n(PG_FUNCTION_ARGS) {
+ const bytea* bytes_in;
+ void* sketchptr;
+ uint64 n;
+ bytes_in = PG_GETARG_BYTEA_P(0);
+ sketchptr = kll_double_sketch_deserialize(VARDATA(bytes_in),
VARSIZE(bytes_in) - VARHDRSZ);
+ n = kll_double_sketch_get_n(sketchptr);
+ kll_double_sketch_delete(sketchptr);
+ PG_RETURN_INT64(n);
+}
+
+Datum pg_kll_double_sketch_to_string(PG_FUNCTION_ARGS) {
+ const bytea* bytes_in;
+ void* sketchptr;
+ char* str;
+ bytes_in = PG_GETARG_BYTEA_P(0);
+ sketchptr = kll_double_sketch_deserialize(VARDATA(bytes_in),
VARSIZE(bytes_in) - VARHDRSZ);
+ str = kll_double_sketch_to_string(sketchptr);
+ kll_double_sketch_delete(sketchptr);
+ PG_RETURN_TEXT_P(cstring_to_text(str));
+}
+
Datum pg_kll_double_sketch_get_pmf(PG_FUNCTION_ARGS) {
const bytea* bytes_in;
void* sketchptr;
diff --git a/src/kll_float_sketch_pg_functions.c
b/src/kll_float_sketch_pg_functions.c
index 308b91b..8714147 100644
--- a/src/kll_float_sketch_pg_functions.c
+++ b/src/kll_float_sketch_pg_functions.c
@@ -25,31 +25,32 @@
#include <catalog/pg_type.h>
#include "kll_float_sketch_c_adapter.h"
-#include "base64.h"
/* PG_FUNCTION_INFO_V1 macro to pass functions to postgres */
-PG_FUNCTION_INFO_V1(pg_kll_float_sketch_add_item);
+PG_FUNCTION_INFO_V1(pg_kll_float_sketch_build_agg);
+PG_FUNCTION_INFO_V1(pg_kll_float_sketch_merge_agg);
+PG_FUNCTION_INFO_V1(pg_kll_float_sketch_serialize);
+PG_FUNCTION_INFO_V1(pg_kll_float_sketch_deserialize);
+PG_FUNCTION_INFO_V1(pg_kll_float_sketch_combine);
PG_FUNCTION_INFO_V1(pg_kll_float_sketch_get_rank);
PG_FUNCTION_INFO_V1(pg_kll_float_sketch_get_quantile);
PG_FUNCTION_INFO_V1(pg_kll_float_sketch_get_n);
PG_FUNCTION_INFO_V1(pg_kll_float_sketch_to_string);
-PG_FUNCTION_INFO_V1(pg_kll_float_sketch_merge);
-PG_FUNCTION_INFO_V1(pg_kll_float_sketch_from_internal);
PG_FUNCTION_INFO_V1(pg_kll_float_sketch_get_pmf);
PG_FUNCTION_INFO_V1(pg_kll_float_sketch_get_cdf);
PG_FUNCTION_INFO_V1(pg_kll_float_sketch_get_quantiles);
PG_FUNCTION_INFO_V1(pg_kll_float_sketch_get_histogram);
/* function declarations */
-Datum pg_kll_float_sketch_recv(PG_FUNCTION_ARGS);
-Datum pg_kll_float_sketch_send(PG_FUNCTION_ARGS);
-Datum pg_kll_float_sketch_add_item(PG_FUNCTION_ARGS);
+Datum pg_kll_float_sketch_build_agg(PG_FUNCTION_ARGS);
+Datum pg_kll_float_sketch_merge_agg(PG_FUNCTION_ARGS);
+Datum pg_kll_float_sketch_serialize(PG_FUNCTION_ARGS);
+Datum pg_kll_float_sketch_deserialize(PG_FUNCTION_ARGS);
+Datum pg_kll_float_sketch_combine(PG_FUNCTION_ARGS);
Datum pg_kll_float_sketch_get_rank(PG_FUNCTION_ARGS);
Datum pg_kll_float_sketch_get_quantile(PG_FUNCTION_ARGS);
Datum pg_kll_float_sketch_get_n(PG_FUNCTION_ARGS);
Datum pg_kll_float_sketch_to_string(PG_FUNCTION_ARGS);
-Datum pg_kll_float_sketch_merge(PG_FUNCTION_ARGS);
-Datum pg_kll_float_sketch_from_internal(PG_FUNCTION_ARGS);
Datum pg_kll_float_sketch_get_pmf(PG_FUNCTION_ARGS);
Datum pg_kll_float_sketch_get_cdf(PG_FUNCTION_ARGS);
Datum pg_kll_float_sketch_get_quantiles(PG_FUNCTION_ARGS);
@@ -57,7 +58,7 @@ Datum pg_kll_float_sketch_get_histogram(PG_FUNCTION_ARGS);
static const unsigned DEFAULT_NUM_BINS = 10;
-Datum pg_kll_float_sketch_add_item(PG_FUNCTION_ARGS) {
+Datum pg_kll_float_sketch_build_agg(PG_FUNCTION_ARGS) {
void* sketchptr;
float value;
int k;
@@ -72,7 +73,7 @@ Datum pg_kll_float_sketch_add_item(PG_FUNCTION_ARGS) {
}
if (!AggCheckCallContext(fcinfo, &aggcontext)) {
- elog(ERROR, "kll_float_sketch_add_item called in non-aggregate context");
+ elog(ERROR, "kll_float_sketch_build_agg called in non-aggregate context");
}
oldcontext = MemoryContextSwitchTo(aggcontext);
@@ -91,55 +92,7 @@ Datum pg_kll_float_sketch_add_item(PG_FUNCTION_ARGS) {
PG_RETURN_POINTER(sketchptr);
}
-Datum pg_kll_float_sketch_get_rank(PG_FUNCTION_ARGS) {
- const bytea* bytes_in;
- void* sketchptr;
- float value;
- double rank;
- bytes_in = PG_GETARG_BYTEA_P(0);
- sketchptr = kll_float_sketch_deserialize(VARDATA(bytes_in),
VARSIZE(bytes_in) - VARHDRSZ);
- value = PG_GETARG_FLOAT4(1);
- rank = kll_float_sketch_get_rank(sketchptr, value);
- kll_float_sketch_delete(sketchptr);
- PG_RETURN_FLOAT8(rank);
-}
-
-Datum pg_kll_float_sketch_get_quantile(PG_FUNCTION_ARGS) {
- const bytea* bytes_in;
- void* sketchptr;
- float value;
- double rank;
- bytes_in = PG_GETARG_BYTEA_P(0);
- sketchptr = kll_float_sketch_deserialize(VARDATA(bytes_in),
VARSIZE(bytes_in) - VARHDRSZ);
- rank = PG_GETARG_FLOAT8(1);
- value = kll_float_sketch_get_quantile(sketchptr, rank);
- kll_float_sketch_delete(sketchptr);
- PG_RETURN_FLOAT4(value);
-}
-
-Datum pg_kll_float_sketch_get_n(PG_FUNCTION_ARGS) {
- const bytea* bytes_in;
- void* sketchptr;
- uint64 n;
- bytes_in = PG_GETARG_BYTEA_P(0);
- sketchptr = kll_float_sketch_deserialize(VARDATA(bytes_in),
VARSIZE(bytes_in) - VARHDRSZ);
- n = kll_float_sketch_get_n(sketchptr);
- kll_float_sketch_delete(sketchptr);
- PG_RETURN_INT64(n);
-}
-
-Datum pg_kll_float_sketch_to_string(PG_FUNCTION_ARGS) {
- const bytea* bytes_in;
- void* sketchptr;
- char* str;
- bytes_in = PG_GETARG_BYTEA_P(0);
- sketchptr = kll_float_sketch_deserialize(VARDATA(bytes_in),
VARSIZE(bytes_in) - VARHDRSZ);
- str = kll_float_sketch_to_string(sketchptr);
- kll_float_sketch_delete(sketchptr);
- PG_RETURN_TEXT_P(cstring_to_text(str));
-}
-
-Datum pg_kll_float_sketch_merge(PG_FUNCTION_ARGS) {
+Datum pg_kll_float_sketch_merge_agg(PG_FUNCTION_ARGS) {
void* unionptr;
bytea* sketch_bytes;
void* sketchptr;
@@ -155,7 +108,7 @@ Datum pg_kll_float_sketch_merge(PG_FUNCTION_ARGS) {
}
if (!AggCheckCallContext(fcinfo, &aggcontext)) {
- elog(ERROR, "kll_float_sketch_merge called in non-aggregate context");
+ elog(ERROR, "kll_float_sketch_merge_agg called in non-aggregate context");
}
oldcontext = MemoryContextSwitchTo(aggcontext);
@@ -176,14 +129,14 @@ Datum pg_kll_float_sketch_merge(PG_FUNCTION_ARGS) {
PG_RETURN_POINTER(unionptr);
}
-Datum pg_kll_float_sketch_from_internal(PG_FUNCTION_ARGS) {
+Datum pg_kll_float_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, "kll_float_sketch_from_internal called in non-aggregate
context");
+ elog(ERROR, "kll_float_sketch_serialize called in non-aggregate context");
}
sketchptr = PG_GETARG_POINTER(0);
bytes_out = kll_float_sketch_serialize(sketchptr, VARHDRSZ);
@@ -192,6 +145,109 @@ Datum pg_kll_float_sketch_from_internal(PG_FUNCTION_ARGS)
{
PG_RETURN_BYTEA_P(bytes_out.ptr);
}
+Datum pg_kll_float_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, "kll_float_sketch_deserialize called in non-aggregate
context");
+ }
+ oldcontext = MemoryContextSwitchTo(aggcontext);
+
+ bytes_in = PG_GETARG_BYTEA_P(0);
+ sketchptr = kll_float_sketch_deserialize(VARDATA(bytes_in),
VARSIZE(bytes_in) - VARHDRSZ);
+
+ MemoryContextSwitchTo(oldcontext);
+
+ PG_RETURN_POINTER(sketchptr);
+}
+
+Datum pg_kll_float_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, "kll_float_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) {
+ kll_float_sketch_merge(sketchptr, sketchptr2);
+ }
+ kll_float_sketch_delete(sketchptr2);
+ } else {
+ sketchptr = sketchptr2;
+ }
+
+ MemoryContextSwitchTo(oldcontext);
+
+ PG_RETURN_POINTER(sketchptr);
+}
+
+Datum pg_kll_float_sketch_get_rank(PG_FUNCTION_ARGS) {
+ const bytea* bytes_in;
+ void* sketchptr;
+ float value;
+ double rank;
+ bytes_in = PG_GETARG_BYTEA_P(0);
+ sketchptr = kll_float_sketch_deserialize(VARDATA(bytes_in),
VARSIZE(bytes_in) - VARHDRSZ);
+ value = PG_GETARG_FLOAT4(1);
+ rank = kll_float_sketch_get_rank(sketchptr, value);
+ kll_float_sketch_delete(sketchptr);
+ PG_RETURN_FLOAT8(rank);
+}
+
+Datum pg_kll_float_sketch_get_quantile(PG_FUNCTION_ARGS) {
+ const bytea* bytes_in;
+ void* sketchptr;
+ float value;
+ double rank;
+ bytes_in = PG_GETARG_BYTEA_P(0);
+ sketchptr = kll_float_sketch_deserialize(VARDATA(bytes_in),
VARSIZE(bytes_in) - VARHDRSZ);
+ rank = PG_GETARG_FLOAT8(1);
+ value = kll_float_sketch_get_quantile(sketchptr, rank);
+ kll_float_sketch_delete(sketchptr);
+ PG_RETURN_FLOAT4(value);
+}
+
+Datum pg_kll_float_sketch_get_n(PG_FUNCTION_ARGS) {
+ const bytea* bytes_in;
+ void* sketchptr;
+ uint64 n;
+ bytes_in = PG_GETARG_BYTEA_P(0);
+ sketchptr = kll_float_sketch_deserialize(VARDATA(bytes_in),
VARSIZE(bytes_in) - VARHDRSZ);
+ n = kll_float_sketch_get_n(sketchptr);
+ kll_float_sketch_delete(sketchptr);
+ PG_RETURN_INT64(n);
+}
+
+Datum pg_kll_float_sketch_to_string(PG_FUNCTION_ARGS) {
+ const bytea* bytes_in;
+ void* sketchptr;
+ char* str;
+ bytes_in = PG_GETARG_BYTEA_P(0);
+ sketchptr = kll_float_sketch_deserialize(VARDATA(bytes_in),
VARSIZE(bytes_in) - VARHDRSZ);
+ str = kll_float_sketch_to_string(sketchptr);
+ kll_float_sketch_delete(sketchptr);
+ PG_RETURN_TEXT_P(cstring_to_text(str));
+}
+
Datum pg_kll_float_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]