You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@impala.apache.org by st...@apache.org on 2021/06/08 10:03:52 UTC
[impala] 01/02: IMPALA-10689: Implement ds_cpc_union_f() function.
This is an automated email from the ASF dual-hosted git repository.
stigahuang pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/impala.git
commit ce21fe74b806757a1c9e52c614db95a76283c78c
Author: Fucun Chu <ch...@hotmail.com>
AuthorDate: Wed May 12 16:15:38 2021 +0800
IMPALA-10689: Implement ds_cpc_union_f() function.
This function receives two strings that are serialized Apache
DataSketches CPC sketches. Union two sketches and returns the
resulting sketch of union.
Example:
select ds_cpc_estimate(ds_cpc_union_f(sketch1, sketch2))
from sketch_tbl;
+---------------------------------------------------+
| ds_cpc_estimate(ds_cpc_union_f(sketch1, sketch2)) |
+---------------------------------------------------+
| 15 |
+---------------------------------------------------+
Change-Id: Ib5c616316bf2bf2ff437678e9a44a15339920150
Reviewed-on: http://gerrit.cloudera.org:8080/17440
Reviewed-by: Impala Public Jenkins <im...@cloudera.com>
Tested-by: Impala Public Jenkins <im...@cloudera.com>
---
be/src/exprs/datasketches-common.cc | 13 +++
be/src/exprs/datasketches-common.h | 8 ++
be/src/exprs/datasketches-functions-ir.cc | 18 +++++
be/src/exprs/datasketches-functions.h | 7 ++
common/function-registry/impala_functions.py | 2 +
.../queries/QueryTest/datasketches-cpc.test | 93 +++++++++++++++++++++-
6 files changed, 140 insertions(+), 1 deletion(-)
diff --git a/be/src/exprs/datasketches-common.cc b/be/src/exprs/datasketches-common.cc
index cfb5a68..c3e0b3b 100644
--- a/be/src/exprs/datasketches-common.cc
+++ b/be/src/exprs/datasketches-common.cc
@@ -84,6 +84,19 @@ StringVal StringStreamToStringVal(FunctionContext* ctx, const stringstream& str_
return dst;
}
+bool update_sketch_to_cpc_union(FunctionContext* ctx,
+ const StringVal& serialized_sketch, datasketches::cpc_union& union_sketch) {
+ if (!serialized_sketch.is_null && serialized_sketch.len > 0) {
+ datasketches::cpc_sketch sketch(DS_CPC_SKETCH_CONFIG);
+ if (!DeserializeDsSketch(serialized_sketch, &sketch)) {
+ LogSketchDeserializationError(ctx);
+ return false;
+ }
+ union_sketch.update(sketch);
+ }
+ return true;
+}
+
bool update_sketch_to_theta_union(FunctionContext* ctx,
const StringVal& serialized_sketch, datasketches::theta_union& sketch) {
if (!serialized_sketch.is_null && serialized_sketch.len > 0) {
diff --git a/be/src/exprs/datasketches-common.h b/be/src/exprs/datasketches-common.h
index 7187399..0cf5273 100644
--- a/be/src/exprs/datasketches-common.h
+++ b/be/src/exprs/datasketches-common.h
@@ -22,6 +22,7 @@
#include "common/status.h"
#include "thirdparty/datasketches/hll.hpp"
+#include "thirdparty/datasketches/cpc_union.hpp"
#include "thirdparty/datasketches/theta_union.hpp"
#include "thirdparty/datasketches/theta_intersection.hpp"
#include "udf/udf.h"
@@ -65,6 +66,13 @@ bool DeserializeDsSketch(const StringVal& serialized_sketch, T* sketch)
StringVal StringStreamToStringVal(FunctionContext* ctx,
const std::stringstream& str_stream);
+/// Helper function that receives a serialized DataSketches CPC sketch in
+/// 'serialized_sketch', deserializes it and update the deserialized sketch to 'sketch'.
+/// Returns false if the deserialization fails (the error log will be written),
+/// true otherwise.
+bool update_sketch_to_cpc_union(FunctionContext* ctx, const StringVal& serialized_sketch,
+ datasketches::cpc_union& sketch);
+
/// Helper function that receives a serialized DataSketches Theta sketch in
/// 'serialized_sketch', deserializes it and update the deserialized sketch to 'sketch'.
/// Returns false if the deserialization fails (the error log will be written),
diff --git a/be/src/exprs/datasketches-functions-ir.cc b/be/src/exprs/datasketches-functions-ir.cc
index f741edc..4315fa3 100644
--- a/be/src/exprs/datasketches-functions-ir.cc
+++ b/be/src/exprs/datasketches-functions-ir.cc
@@ -21,6 +21,7 @@
#include "gutil/strings/substitute.h"
#include "thirdparty/datasketches/hll.hpp"
#include "thirdparty/datasketches/cpc_sketch.hpp"
+#include "thirdparty/datasketches/cpc_union.hpp"
#include "thirdparty/datasketches/theta_sketch.hpp"
#include "thirdparty/datasketches/theta_union.hpp"
#include "thirdparty/datasketches/theta_intersection.hpp"
@@ -134,6 +135,23 @@ StringVal DataSketchesFunctions::DsCpcStringify(
return dst;
}
+StringVal DataSketchesFunctions::DsCpcUnionF(FunctionContext* ctx,
+ const StringVal& first_serialized_sketch, const StringVal& second_serialized_sketch) {
+ datasketches::cpc_union union_sketch;
+ // Update two sketches to cpc_union
+ if (!update_sketch_to_cpc_union(ctx, first_serialized_sketch, union_sketch)) {
+ return StringVal::null();
+ }
+ if (!update_sketch_to_cpc_union(ctx, second_serialized_sketch, union_sketch)) {
+ return StringVal::null();
+ }
+ // Result
+ datasketches::cpc_sketch sketch = union_sketch.get_result();
+ std::stringstream serialized_input;
+ sketch.serialize(serialized_input);
+ return StringStreamToStringVal(ctx, serialized_input);
+}
+
BigIntVal DataSketchesFunctions::DsThetaEstimate(
FunctionContext* ctx, const StringVal& serialized_sketch) {
if (serialized_sketch.is_null || serialized_sketch.len == 0) return 0;
diff --git a/be/src/exprs/datasketches-functions.h b/be/src/exprs/datasketches-functions.h
index 2477189..3f44793 100644
--- a/be/src/exprs/datasketches-functions.h
+++ b/be/src/exprs/datasketches-functions.h
@@ -76,6 +76,13 @@ public:
static StringVal DsCpcStringify(
FunctionContext* ctx, const StringVal& serialized_sketch);
+ /// 'first_serialized_sketch' and 'second_serialized_sketch' are both expected as
+ /// serialized Apache DataSketches CPC sketches. If they are not, then the query
+ /// fails. Union two sketches and returns the resulting sketch of union.
+ static StringVal DsCpcUnionF(FunctionContext* ctx,
+ const StringVal& first_serialized_sketch,
+ const StringVal& second_serialized_sketch);
+
/// 'serialized_sketch' is expected as a serialized Apache DataSketches Theta sketch.
/// If it is not, then the query fails. Otherwise, returns the count(distinct) estimate
/// from the sketch.
diff --git a/common/function-registry/impala_functions.py b/common/function-registry/impala_functions.py
index 7c36995..a6b4914 100644
--- a/common/function-registry/impala_functions.py
+++ b/common/function-registry/impala_functions.py
@@ -1011,6 +1011,8 @@ visible_functions = [
'_ZN6impala21DataSketchesFunctions13DsCpcEstimateEPN10impala_udf15FunctionContextERKNS1_9StringValE'],
[['ds_cpc_stringify'], 'STRING', ['STRING'],
'_ZN6impala21DataSketchesFunctions14DsCpcStringifyEPN10impala_udf15FunctionContextERKNS1_9StringValE'],
+ [['ds_cpc_union_f'], 'STRING', ['STRING', 'STRING'],
+ '_ZN6impala21DataSketchesFunctions11DsCpcUnionFEPN10impala_udf15FunctionContextERKNS1_9StringValES6_'],
[['ds_theta_estimate'], 'BIGINT', ['STRING'],
'_ZN6impala21DataSketchesFunctions15DsThetaEstimateEPN10impala_udf15FunctionContextERKNS1_9StringValE'],
[['ds_theta_exclude'], 'STRING', ['STRING', 'STRING'],
diff --git a/testdata/workloads/functional-query/queries/QueryTest/datasketches-cpc.test b/testdata/workloads/functional-query/queries/QueryTest/datasketches-cpc.test
index 4c1ce48..1ee5d53 100644
--- a/testdata/workloads/functional-query/queries/QueryTest/datasketches-cpc.test
+++ b/testdata/workloads/functional-query/queries/QueryTest/datasketches-cpc.test
@@ -282,4 +282,95 @@ select ds_cpc_stringify(ds_cpc_sketch(float_col)) from functional_parquet.alltyp
row_regex: .*### CPC sketch summary:.*lg_k.*seed hash.*C.*flavor.*### End sketch summary.*
---- TYPES
STRING
-====
\ No newline at end of file
+====
+---- QUERY
+# Checks that unioning a valid sketch with a null value result the valid sketch being returned.
+select
+ ds_cpc_estimate(ds_cpc_union_f(date_sketch, null)),
+ ds_cpc_estimate(ds_cpc_union_f(null, float_sketch))
+from cpc_sketch_store;
+---- TYPES
+BIGINT,BIGINT
+---- RESULTS
+3,10
+3,10
+3,10
+3,10
+====
+---- QUERY
+# Check that ds_cpc_union_f() returns an empty sketch for an empty sketch.
+select ds_cpc_estimate(ds_cpc_union_f(ds_cpc_sketch(cast(f2 as float)), null))
+from functional_parquet.emptytable;
+---- TYPES
+BIGINT
+---- RESULTS
+0
+====
+---- QUERY
+# Checks that ds_cpc_union_f() returns an empty sketch for NULL inputs.
+select ds_cpc_estimate(ds_cpc_union_f(null_str, some_nulls)) from functional_parquet.nullrows
+where id='b';
+---- TYPES
+BIGINT
+---- RESULTS
+0
+====
+---- QUERY
+# ds_cpc_union_f() returns an error if it receives an invalid serialized sketch.
+select ds_cpc_union_f(null, date_string_col) from functional_parquet.alltypestiny
+where id=1;
+---- CATCH
+UDF ERROR: Unable to deserialize sketch.
+====
+---- QUERY
+# ds_cpc_union_f() returns an error if it receives an invalid serialized sketch.
+select ds_cpc_union_f(date_string_col, null) from functional_parquet.alltypestiny
+where id=1;
+---- CATCH
+UDF ERROR: Unable to deserialize sketch.
+====
+---- QUERY
+# Get the same sketches from Impala and Hive and put them into the same table (different
+# column prefix). When we get the estimates from the unions of these sketches the expectation
+# is to get the same results as if these sketches were used separately to get the estimates.
+# However, for string types (STRING, CHAR, VARCHAR) we see the numbers doubling up because
+# of the difference between how Impala and Hive uses these types. Like IMPALA-9939.
+create table cpc_sketches_impala_hive2 (
+i_ti string,i_i string,i_bi string,i_f string,i_d string,i_s string,i_c string,i_v string,
+i_nc string,
+h_ti string,h_i string,h_bi string,h_f string,h_d string,h_s string,h_c string,h_v string,
+h_nc string) stored as parquet;
+insert overwrite cpc_sketches_impala_hive2 select
+i.ti i_ti, i.i i_i, i.bi i_bi, i.f i_f, i.d i_d, i.s i_s, i.c i_c, i.v i_v,i.nc i_nc,
+h.ti h_ti, h.i h_i, h.bi h_bi, h.f h_f, h.d h_d, h.s h_s, h.c h_c, h.v h_v,h.nc h_nc
+from cpc_sketches_from_impala i, cpc_sketches_from_hive h;
+select
+ ds_cpc_estimate(ds_cpc_union_f(i_ti, h_ti)) as ti,
+ ds_cpc_estimate(ds_cpc_union_f(i_i, h_i)) as i,
+ ds_cpc_estimate(ds_cpc_union_f(i_bi, h_bi)) as bi,
+ ds_cpc_estimate(ds_cpc_union_f(i_f, h_f)) as f,
+ ds_cpc_estimate(ds_cpc_union_f(i_d, h_d)) as d,
+ ds_cpc_estimate(ds_cpc_union_f(i_s, h_s)) as s,
+ ds_cpc_estimate(ds_cpc_union_f(i_c, h_c)) as c,
+ ds_cpc_estimate(ds_cpc_union_f(i_v, h_v)) as v,
+ ds_cpc_estimate(ds_cpc_union_f(i_nc, h_nc)) as nc
+from cpc_sketches_impala_hive2;
+---- TYPES
+BIGINT,BIGINT,BIGINT,BIGINT,BIGINT,BIGINT,BIGINT,BIGINT,BIGINT
+---- RESULTS
+5,7,6,6,7,8,8,6,0
+====
+---- QUERY
+# Union two sketches from different columns of sketch_intermediate.
+create table sketch_input (id1 int, id2 int);
+insert into table sketch_input values
+ (1, 2), (2, 4), (3, 6), (4, 8), (5, 10), (6, 12), (7, 14), (8, 16), (9, 18), (10, 20);
+create table sketch_intermediate (sketch1 string, sketch2 string) stored as parquet;
+insert overwrite sketch_intermediate
+ select ds_cpc_sketch(id1), ds_cpc_sketch(id2) from sketch_input;
+select ds_cpc_estimate(ds_cpc_union_f(sketch1, sketch2)) from sketch_intermediate;
+---- TYPES
+BIGINT
+---- RESULTS
+15
+====