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/07/29 21:49:28 UTC
[incubator-datasketches-postgresql] branch kll_cdf created (now
4b9bfeb)
This is an automated email from the ASF dual-hosted git repository.
alsay pushed a change to branch kll_cdf
in repository https://gitbox.apache.org/repos/asf/incubator-datasketches-postgresql.git.
at 4b9bfeb added function to get CDF
This branch includes the following new commits:
new 4b9bfeb added function to get CDF
The 1 revisions listed above as "new" are entirely new to this
repository and will be described in separate emails. The revisions
listed as "add" were already present in the repository and have only
been added to this reference.
---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscribe@datasketches.apache.org
For additional commands, e-mail: commits-help@datasketches.apache.org
[incubator-datasketches-postgresql] 01/01: added function to get CDF
Posted by al...@apache.org.
This is an automated email from the ASF dual-hosted git repository.
alsay pushed a commit to branch kll_cdf
in repository https://gitbox.apache.org/repos/asf/incubator-datasketches-postgresql.git
commit 4b9bfeb463d3bb1d1e09ed667712bb5fda94df69
Author: AlexanderSaydakov <Al...@users.noreply.github.com>
AuthorDate: Mon Jul 29 14:43:08 2019 -0700
added function to get CDF
---
sql/datasketches_kll_float_sketch.sql | 4 +++
src/kll_float_sketch_c_adapter.cpp | 6 ++--
src/kll_float_sketch_c_adapter.h | 2 +-
src/kll_float_sketch_pg_functions.c | 58 +++++++++++++++++++++++++++++++++--
4 files changed, 64 insertions(+), 6 deletions(-)
diff --git a/sql/datasketches_kll_float_sketch.sql b/sql/datasketches_kll_float_sketch.sql
index 704e2b8..b179a75 100644
--- a/sql/datasketches_kll_float_sketch.sql
+++ b/sql/datasketches_kll_float_sketch.sql
@@ -98,6 +98,10 @@ CREATE OR REPLACE FUNCTION kll_float_sketch_get_pmf(kll_float_sketch, real[]) RE
AS '$libdir/datasketches', 'pg_kll_float_sketch_get_pmf'
LANGUAGE C STRICT IMMUTABLE;
+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;
+
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;
diff --git a/src/kll_float_sketch_c_adapter.cpp b/src/kll_float_sketch_c_adapter.cpp
index c9db67c..ef81b6d 100644
--- a/src/kll_float_sketch_c_adapter.cpp
+++ b/src/kll_float_sketch_c_adapter.cpp
@@ -129,9 +129,11 @@ unsigned kll_float_sketch_get_serialized_size_bytes(const void* sketchptr) {
pg_unreachable();
}
-Datum* kll_float_sketch_get_pmf(const void* sketchptr, const float* split_points, unsigned num_split_points) {
+Datum* kll_float_sketch_get_pmf_or_cdf(const void* sketchptr, const float* split_points, unsigned num_split_points, bool is_cdf) {
try {
- auto ptr = static_cast<const kll_float_sketch*>(sketchptr)->get_PMF(split_points, num_split_points);
+ auto ptr = is_cdf ?
+ static_cast<const kll_float_sketch*>(sketchptr)->get_CDF(split_points, num_split_points) :
+ static_cast<const kll_float_sketch*>(sketchptr)->get_PMF(split_points, num_split_points);
Datum* pmf = (Datum*) palloc(sizeof(Datum) * (num_split_points + 1));
for (unsigned i = 0; i < num_split_points + 1; i++) {
pmf[i] = pg_float8_get_datum(ptr[i]);
diff --git a/src/kll_float_sketch_c_adapter.h b/src/kll_float_sketch_c_adapter.h
index 9aefc91..75dd62b 100644
--- a/src/kll_float_sketch_c_adapter.h
+++ b/src/kll_float_sketch_c_adapter.h
@@ -43,7 +43,7 @@ struct ptr_with_size kll_float_sketch_serialize(const void* sketchptr, unsigned
void* kll_float_sketch_deserialize(const char* buffer, unsigned length);
unsigned kll_float_sketch_get_serialized_size_bytes(const void* sketchptr);
-void** kll_float_sketch_get_pmf(const void* sketchptr, const float* split_points, unsigned num_split_points);
+void** kll_float_sketch_get_pmf_or_cdf(const void* sketchptr, const float* split_points, unsigned num_split_points, bool is_cdf);
void** kll_float_sketch_get_quantiles(const void* sketchptr, const double* fractions, unsigned num_fractions);
#ifdef __cplusplus
diff --git a/src/kll_float_sketch_pg_functions.c b/src/kll_float_sketch_pg_functions.c
index 3284bb4..14cc3c1 100644
--- a/src/kll_float_sketch_pg_functions.c
+++ b/src/kll_float_sketch_pg_functions.c
@@ -36,6 +36,7 @@ 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);
/* function declarations */
@@ -49,6 +50,7 @@ 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);
static const unsigned DEFAULT_K = 200;
@@ -204,7 +206,7 @@ Datum pg_kll_float_sketch_get_pmf(PG_FUNCTION_ARGS) {
float* split_points;
// output array of fractions
- Datum* pmf;
+ Datum* result;
ArrayType* arr_out;
int16 elmlen_out;
bool elmbyval_out;
@@ -225,13 +227,63 @@ Datum pg_kll_float_sketch_get_pmf(PG_FUNCTION_ARGS) {
for (i = 0; i < arr_len_in; i++) {
split_points[i] = DatumGetFloat4(data_in[i]);
}
- pmf = (Datum*) kll_float_sketch_get_pmf(sketchptr, split_points, arr_len_in);
+ result = (Datum*) kll_float_sketch_get_pmf_or_cdf(sketchptr, split_points, arr_len_in, false);
pfree(split_points);
// construct output array of fractions
arr_len_out = arr_len_in + 1; // N split points devide the number line into N+1 intervals
get_typlenbyvalalign(FLOAT8OID, &elmlen_out, &elmbyval_out, &elmalign_out);
- arr_out = construct_array(pmf, arr_len_out, FLOAT8OID, elmlen_out, elmbyval_out, elmalign_out);
+ arr_out = construct_array(result, arr_len_out, FLOAT8OID, elmlen_out, elmbyval_out, elmalign_out);
+
+ kll_float_sketch_delete(sketchptr);
+
+ PG_RETURN_ARRAYTYPE_P(arr_out);
+}
+
+Datum pg_kll_float_sketch_get_cdf(PG_FUNCTION_ARGS) {
+ const bytea* bytes_in;
+ void* sketchptr;
+
+ // input array of split points
+ ArrayType* arr_in;
+ Oid elmtype_in;
+ int16 elmlen_in;
+ bool elmbyval_in;
+ char elmalign_in;
+ Datum* data_in;
+ bool* nulls_in;
+ int arr_len_in;
+ float* split_points;
+
+ // output array of fractions
+ Datum* result;
+ ArrayType* arr_out;
+ int16 elmlen_out;
+ bool elmbyval_out;
+ char elmalign_out;
+ int arr_len_out;
+
+ int i;
+
+ bytes_in = PG_GETARG_BYTEA_P(0);
+ sketchptr = kll_float_sketch_deserialize(VARDATA(bytes_in), VARSIZE(bytes_in) - VARHDRSZ);
+
+ arr_in = PG_GETARG_ARRAYTYPE_P(1);
+ elmtype_in = ARR_ELEMTYPE(arr_in);
+ get_typlenbyvalalign(elmtype_in, &elmlen_in, &elmbyval_in, &elmalign_in);
+ deconstruct_array(arr_in, elmtype_in, elmlen_in, elmbyval_in, elmalign_in, &data_in, &nulls_in, &arr_len_in);
+
+ split_points = palloc(sizeof(float) * arr_len_in);
+ for (i = 0; i < arr_len_in; i++) {
+ split_points[i] = DatumGetFloat4(data_in[i]);
+ }
+ result = (Datum*) kll_float_sketch_get_pmf_or_cdf(sketchptr, split_points, arr_len_in, true);
+ pfree(split_points);
+
+ // construct output array of fractions
+ arr_len_out = arr_len_in + 1; // N split points devide the number line into N+1 intervals
+ get_typlenbyvalalign(FLOAT8OID, &elmlen_out, &elmbyval_out, &elmalign_out);
+ arr_out = construct_array(result, arr_len_out, FLOAT8OID, elmlen_out, elmbyval_out, elmalign_out);
kll_float_sketch_delete(sketchptr);
---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscribe@datasketches.apache.org
For additional commands, e-mail: commits-help@datasketches.apache.org