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 2020/09/14 22:03:28 UTC
[incubator-datasketches-postgresql] branch aod_sketch updated:
means and variances
This is an automated email from the ASF dual-hosted git repository.
alsay pushed a commit to branch aod_sketch
in repository https://gitbox.apache.org/repos/asf/incubator-datasketches-postgresql.git
The following commit(s) were added to refs/heads/aod_sketch by this push:
new 8855903 means and variances
8855903 is described below
commit 8855903a6004521450f816a722cbb45c78525300
Author: AlexanderSaydakov <Al...@users.noreply.github.com>
AuthorDate: Mon Sep 14 15:02:59 2020 -0700
means and variances
---
sql/datasketches_aod_sketch.sql | 8 ++++++
src/aod_sketch_c_adapter.cpp | 52 ++++++++++++++++++++++++++++++++++
src/aod_sketch_c_adapter.h | 4 ++-
src/aod_sketch_pg_functions.c | 62 +++++++++++++++++++++++++++++++++++++++++
4 files changed, 125 insertions(+), 1 deletion(-)
diff --git a/sql/datasketches_aod_sketch.sql b/sql/datasketches_aod_sketch.sql
index bda7560..b737519 100644
--- a/sql/datasketches_aod_sketch.sql
+++ b/sql/datasketches_aod_sketch.sql
@@ -181,3 +181,11 @@ CREATE OR REPLACE FUNCTION aod_sketch_to_kll_float_sketch(aod_sketch, int, int)
CREATE OR REPLACE FUNCTION aod_sketch_students_t_test(aod_sketch, aod_sketch) RETURNS double precision[]
AS '$libdir/datasketches', 'pg_aod_sketch_students_t_test'
LANGUAGE C STRICT IMMUTABLE;
+
+CREATE OR REPLACE FUNCTION aod_sketch_to_means(aod_sketch) RETURNS double precision[]
+ AS '$libdir/datasketches', 'pg_aod_sketch_to_means'
+ LANGUAGE C STRICT IMMUTABLE;
+
+CREATE OR REPLACE FUNCTION aod_sketch_to_variances(aod_sketch) RETURNS double precision[]
+ AS '$libdir/datasketches', 'pg_aod_sketch_to_variances'
+ LANGUAGE C STRICT IMMUTABLE;
diff --git a/src/aod_sketch_c_adapter.cpp b/src/aod_sketch_c_adapter.cpp
index a7f268e..69d2022 100644
--- a/src/aod_sketch_c_adapter.cpp
+++ b/src/aod_sketch_c_adapter.cpp
@@ -341,3 +341,55 @@ Datum* aod_sketch_students_t_test(const void* sketchptr1, const void* sketchptr2
}
pg_unreachable();
}
+
+Datum* aod_sketch_to_means(const void* sketchptr, unsigned* arr_len_out) {
+ try {
+ const auto& sketch = *static_cast<const compact_aod_sketch_pg*>(sketchptr);
+ unsigned num_values = sketch.get_num_values();
+ Datum* means = (Datum*) palloc(sizeof(Datum) * num_values);
+ *arr_len_out = num_values;
+
+ using namespace boost::accumulators;
+ using Accum = accumulator_set<double, stats<tag::mean>>;
+
+ std::vector<Accum, palloc_allocator<Accum>> stats(num_values);
+ for (const auto& entry: sketch) {
+ for (unsigned i = 0; i < num_values; ++i) stats[i](entry.second[i]);
+ }
+
+ for (unsigned i = 0; i < num_values; ++i) {
+ means[i] = pg_float8_get_datum(mean(stats[i]));
+ }
+
+ return means;
+ } catch (std::exception& e) {
+ pg_error(e.what());
+ }
+ pg_unreachable();
+}
+
+Datum* aod_sketch_to_variances(const void* sketchptr, unsigned* arr_len_out) {
+ try {
+ const auto& sketch = *static_cast<const compact_aod_sketch_pg*>(sketchptr);
+ unsigned num_values = sketch.get_num_values();
+ Datum* variances = (Datum*) palloc(sizeof(Datum) * num_values);
+ *arr_len_out = num_values;
+
+ using namespace boost::accumulators;
+ using Accum = accumulator_set<double, stats<tag::variance>>;
+
+ std::vector<Accum, palloc_allocator<Accum>> stats(num_values);
+ for (const auto& entry: sketch) {
+ for (unsigned i = 0; i < num_values; ++i) stats[i](entry.second[i]);
+ }
+
+ for (unsigned i = 0; i < num_values; ++i) {
+ variances[i] = pg_float8_get_datum(variance(stats[i]));
+ }
+
+ return variances;
+ } catch (std::exception& e) {
+ pg_error(e.what());
+ }
+ pg_unreachable();
+}
diff --git a/src/aod_sketch_c_adapter.h b/src/aod_sketch_c_adapter.h
index 5bfa3b0..9f36b10 100644
--- a/src/aod_sketch_c_adapter.h
+++ b/src/aod_sketch_c_adapter.h
@@ -58,7 +58,9 @@ void* aod_a_not_b(const void* sketchptr1, const void* sketchptr2);
void* aod_sketch_to_kll_float_sketch(const void* sketchptr, unsigned column_index, unsigned k);
-void** aod_sketch_students_t_test(const void* sketch1ptr, const void* sketch2ptr, unsigned* arr_len_out);
+void** aod_sketch_students_t_test(const void* sketchptr1, const void* sketchptr2, unsigned* arr_len_out);
+void** aod_sketch_to_means(const void* sketchptr, unsigned* arr_len_out);
+void** aod_sketch_to_variances(const void* sketchptr, unsigned* arr_len_out);
#ifdef __cplusplus
}
diff --git a/src/aod_sketch_pg_functions.c b/src/aod_sketch_pg_functions.c
index 93254a4..98766ef 100644
--- a/src/aod_sketch_pg_functions.c
+++ b/src/aod_sketch_pg_functions.c
@@ -44,6 +44,8 @@ PG_FUNCTION_INFO_V1(pg_aod_sketch_intersection);
PG_FUNCTION_INFO_V1(pg_aod_sketch_a_not_b);
PG_FUNCTION_INFO_V1(pg_aod_sketch_to_kll_float_sketch);
PG_FUNCTION_INFO_V1(pg_aod_sketch_students_t_test);
+PG_FUNCTION_INFO_V1(pg_aod_sketch_to_means);
+PG_FUNCTION_INFO_V1(pg_aod_sketch_to_variances);
/* function declarations */
Datum pg_aod_sketch_recv(PG_FUNCTION_ARGS);
@@ -63,6 +65,8 @@ Datum pg_aod_sketch_intersection(PG_FUNCTION_ARGS);
Datum pg_aod_sketch_a_not_b(PG_FUNCTION_ARGS);
Datum pg_aod_sketch_to_kll_float_sketch(PG_FUNCTION_ARGS);
Datum pg_aod_sketch_students_t_test(PG_FUNCTION_ARGS);
+Datum pg_aod_sketch_to_means(PG_FUNCTION_ARGS);
+Datum pg_aod_sketch_to_variances(PG_FUNCTION_ARGS);
Datum pg_aod_sketch_add_item(PG_FUNCTION_ARGS) {
void* sketchptr;
@@ -523,3 +527,61 @@ Datum pg_aod_sketch_students_t_test(PG_FUNCTION_ARGS) {
PG_RETURN_ARRAYTYPE_P(arr_out);
}
+
+Datum pg_aod_sketch_to_means(PG_FUNCTION_ARGS) {
+ const bytea* bytes_in;
+ void* sketchptr;
+
+ // output array
+ Datum* means;
+ ArrayType* arr_out;
+ int16 elmlen_out;
+ bool elmbyval_out;
+ char elmalign_out;
+ unsigned arr_len_out;
+
+ if (PG_ARGISNULL(0)) {
+ elog(ERROR, "aod_sketch_to_means expects a valid aod sketch");
+ }
+
+ bytes_in = PG_GETARG_BYTEA_P(0);
+ sketchptr = aod_sketch_deserialize(VARDATA(bytes_in), VARSIZE(bytes_in) - VARHDRSZ);
+
+ means = (Datum*) aod_sketch_to_means(sketchptr, &arr_len_out);
+ compact_aod_sketch_delete(sketchptr);
+
+ // construct output array
+ get_typlenbyvalalign(FLOAT8OID, &elmlen_out, &elmbyval_out, &elmalign_out);
+ arr_out = construct_array(means, arr_len_out, FLOAT8OID, elmlen_out, elmbyval_out, elmalign_out);
+
+ PG_RETURN_ARRAYTYPE_P(arr_out);
+}
+
+Datum pg_aod_sketch_to_variances(PG_FUNCTION_ARGS) {
+ const bytea* bytes_in;
+ void* sketchptr;
+
+ // output array
+ Datum* variances;
+ ArrayType* arr_out;
+ int16 elmlen_out;
+ bool elmbyval_out;
+ char elmalign_out;
+ unsigned arr_len_out;
+
+ if (PG_ARGISNULL(0)) {
+ elog(ERROR, "aod_sketch_to_variances expects a valid aod sketch");
+ }
+
+ bytes_in = PG_GETARG_BYTEA_P(0);
+ sketchptr = aod_sketch_deserialize(VARDATA(bytes_in), VARSIZE(bytes_in) - VARHDRSZ);
+
+ variances = (Datum*) aod_sketch_to_variances(sketchptr, &arr_len_out);
+ compact_aod_sketch_delete(sketchptr);
+
+ // construct output array
+ get_typlenbyvalalign(FLOAT8OID, &elmlen_out, &elmbyval_out, &elmalign_out);
+ arr_out = construct_array(variances, arr_len_out, FLOAT8OID, elmlen_out, elmbyval_out, elmalign_out);
+
+ PG_RETURN_ARRAYTYPE_P(arr_out);
+}
---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscribe@datasketches.apache.org
For additional commands, e-mail: commits-help@datasketches.apache.org