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/09/17 05:56:48 UTC

[impala] 01/02: IMPALA-10901 cleaner and faster operations with datasketches

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 c925807b1a3eacf1ca3d0e78f6a0fa043b350174
Author: AlexanderSaydakov <al...@apache.org>
AuthorDate: Mon Aug 30 17:03:04 2021 -0700

    IMPALA-10901 cleaner and faster operations with datasketches
    
    - serialize using bytes instead of stream
    - avoid unnecessary constructor during deserialization
    - simplified code slightly
    - added original exception message to re-thrown generic message
    
    Change-Id: I306a2489dac0f4d2d475e8f9987cd58bf95474bb
    Reviewed-on: http://gerrit.cloudera.org:8080/17818
    Reviewed-by: Impala Public Jenkins <im...@cloudera.com>
    Tested-by: Impala Public Jenkins <im...@cloudera.com>
---
 be/src/exprs/aggregate-functions-ir.cc             | 229 +++++++------------
 be/src/exprs/datasketches-common.cc                |  76 ++-----
 be/src/exprs/datasketches-common.h                 |  21 +-
 be/src/exprs/datasketches-functions-ir.cc          | 247 ++++++++++-----------
 .../queries/QueryTest/datasketches-cpc.test        |  10 +-
 .../queries/QueryTest/datasketches-hll.test        |  10 +-
 .../queries/QueryTest/datasketches-kll.test        |  10 +-
 .../queries/QueryTest/datasketches-theta.test      |  29 +--
 8 files changed, 258 insertions(+), 374 deletions(-)

diff --git a/be/src/exprs/aggregate-functions-ir.cc b/be/src/exprs/aggregate-functions-ir.cc
index a5833ae..50fc26b 100644
--- a/be/src/exprs/aggregate-functions-ir.cc
+++ b/be/src/exprs/aggregate-functions-ir.cc
@@ -1623,9 +1623,10 @@ BigIntVal AggregateFunctions::HllFinalize(FunctionContext* ctx, const StringVal&
 /// functionality into the header.
 StringVal SerializeCompactDsHllSketch(FunctionContext* ctx,
     const datasketches::hll_sketch& sketch) {
-  std::stringstream serialized_input;
-  sketch.serialize_compact(serialized_input);
-  return StringStreamToStringVal(ctx, serialized_input);
+  auto bytes = sketch.serialize_compact();
+  StringVal result(ctx, bytes.size());
+  memcpy(result.ptr, bytes.data(), bytes.size());
+  return result;
 }
 
 /// Auxiliary function that receives a hll_union, gets the underlying HLL sketch from the
@@ -1634,22 +1635,10 @@ StringVal SerializeCompactDsHllSketch(FunctionContext* ctx,
 /// functionality into the header.
 StringVal SerializeDsHllUnion(FunctionContext* ctx,
     const datasketches::hll_union& ds_union) {
-  std::stringstream serialized_input;
   datasketches::hll_sketch sketch = ds_union.get_result(DS_HLL_TYPE);
   return SerializeCompactDsHllSketch(ctx, sketch);
 }
 
-/// Auxiliary function that receives a cpc_sketch and returns the serialized version of
-/// it wrapped into a StringVal.
-/// Introducing this function in the .cc to avoid including the whole DataSketches CPC
-/// functionality into the header.
-StringVal SerializeDsCpcSketch(
-    FunctionContext* ctx, const datasketches::cpc_sketch& sketch) {
-  std::stringstream serialized_input;
-  sketch.serialize(serialized_input);
-  return StringStreamToStringVal(ctx, serialized_input);
-}
-
 /// Auxiliary function that receives a cpc_union, gets the underlying CPC sketch from the
 /// union object and returns the serialized, CPC sketch wrapped into StringVal.
 /// Introducing this function in the .cc to avoid including the whole DataSketches CPC
@@ -1657,18 +1646,7 @@ StringVal SerializeDsCpcSketch(
 StringVal SerializeDsCpcUnion(
     FunctionContext* ctx, const datasketches::cpc_union& ds_union) {
   datasketches::cpc_sketch sketch = ds_union.get_result();
-  return SerializeDsCpcSketch(ctx, sketch);
-}
-
-/// Auxiliary function that receives a theta_sketch and returns the serialized version of
-/// it wrapped into a StringVal.
-/// Introducing this function in the .cc to avoid including the whole DataSketches Theta
-/// functionality into the header.
-StringVal SerializeDsThetaSketch(
-    FunctionContext* ctx, const datasketches::compact_theta_sketch& sketch) {
-  std::stringstream serialized_input(std::ios::in | std::ios::out | std::ios::binary);
-  sketch.serialize(serialized_input);
-  return StringStreamToStringVal(ctx, serialized_input);
+  return SerializeDsSketch(ctx, sketch);
 }
 
 /// Auxiliary function that receives a theta_union, gets the underlying Theta sketch from
@@ -1678,7 +1656,7 @@ StringVal SerializeDsThetaSketch(
 StringVal SerializeDsThetaUnion(
     FunctionContext* ctx, const datasketches::theta_union& ds_union) {
   datasketches::compact_theta_sketch sketch = ds_union.get_result();
-  return SerializeDsThetaSketch(ctx, sketch);
+  return SerializeDsSketch(ctx, sketch);
 }
 
 /// Auxiliary function that receives a theta_intersection, gets the underlying Theta
@@ -1688,26 +1666,14 @@ StringVal SerializeDsThetaUnion(
 /// functionality into the header.
 StringVal SerializeDsThetaIntersection(
     FunctionContext* ctx, const datasketches::theta_intersection& ds_intersection) {
-  std::stringstream serialized_input;
   // Calling get_result() before calling update() is undefined, so you need to check.
   if (ds_intersection.has_result()) {
     datasketches::compact_theta_sketch sketch = ds_intersection.get_result();
-    return SerializeDsThetaSketch(ctx, sketch);
+    return SerializeDsSketch(ctx, sketch);
   }
   return StringVal::null();
 }
 
-/// Auxiliary function that receives a kll_sketch<float> and returns the serialized
-/// version of it wrapped into a StringVal.
-/// Introducing this function in the .cc to avoid including the whole DataSketches HLL
-/// functionality into the header
-StringVal SerializeDsKllSketch(FunctionContext* ctx,
-    const datasketches::kll_sketch<float>& sketch) {
-  std::stringstream serialized_sketch;
-  sketch.serialize(serialized_sketch);
-  return StringStreamToStringVal(ctx, serialized_sketch);
-}
-
 void AggregateFunctions::DsHllInit(FunctionContext* ctx, StringVal* dst) {
   AllocBuffer(ctx, dst, sizeof(datasketches::hll_sketch));
   if (UNLIKELY(dst->is_null)) {
@@ -1814,16 +1780,12 @@ void AggregateFunctions::DsHllUnionUpdate(FunctionContext* ctx, const StringVal&
   if (src.is_null) return;
   DCHECK(!dst->is_null);
   DCHECK_EQ(dst->len, sizeof(datasketches::hll_union));
-  // These parameters might be overwritten by DeserializeDsSketch() to use the settings
-  // from the deserialized sketch from 'src'.
-  datasketches::hll_sketch src_sketch(DS_SKETCH_CONFIG, DS_HLL_TYPE);
-  if (!DeserializeDsSketch(src, &src_sketch)) {
-    LogSketchDeserializationError(ctx);
-    return;
+  try {
+    reinterpret_cast<datasketches::hll_union*>(dst->ptr)
+        ->update(datasketches::hll_sketch::deserialize(src.ptr, src.len));
+  } catch (const std::exception& e) {
+    LogSketchDeserializationError(ctx, e);
   }
-  datasketches::hll_union* union_ptr =
-      reinterpret_cast<datasketches::hll_union*>(dst->ptr);
-  union_ptr->update(src_sketch);
 }
 
 StringVal AggregateFunctions::DsHllUnionSerialize(FunctionContext* ctx,
@@ -1842,18 +1804,13 @@ void AggregateFunctions::DsHllUnionMerge(
   DCHECK(!src.is_null);
   DCHECK(!dst->is_null);
   DCHECK_EQ(dst->len, sizeof(datasketches::hll_union));
-
   // Note, 'src' is a serialized hll_sketch and not a serialized hll_union.
-  datasketches::hll_sketch src_sketch(DS_SKETCH_CONFIG, DS_HLL_TYPE);
-  if (!DeserializeDsSketch(src, &src_sketch)) {
-    LogSketchDeserializationError(ctx);
-    return;
+  try {
+    reinterpret_cast<datasketches::hll_union*>(dst->ptr)
+        ->update(datasketches::hll_sketch::deserialize(src.ptr, src.len));
+  } catch (const std::exception& e) {
+    LogSketchDeserializationError(ctx, e);
   }
-
-  datasketches::hll_union* dst_union_ptr =
-      reinterpret_cast<datasketches::hll_union*>(dst->ptr);
-
-  dst_union_ptr->update(src_sketch);
 }
 
 StringVal AggregateFunctions::DsHllUnionFinalize(FunctionContext* ctx,
@@ -1913,7 +1870,7 @@ StringVal AggregateFunctions::DsCpcSerialize(FunctionContext* ctx, const StringV
   DCHECK_EQ(src.len, sizeof(datasketches::cpc_sketch));
   datasketches::cpc_sketch* sketch_ptr =
       reinterpret_cast<datasketches::cpc_sketch*>(src.ptr);
-  StringVal dst = SerializeDsCpcSketch(ctx, *sketch_ptr);
+  StringVal dst = SerializeDsSketch(ctx, *sketch_ptr);
   ctx->Free(src.ptr);
   return dst;
 }
@@ -1923,19 +1880,17 @@ void AggregateFunctions::DsCpcMerge(
   DCHECK(!src.is_null);
   DCHECK(!dst->is_null);
   DCHECK_EQ(dst->len, sizeof(datasketches::cpc_sketch));
-  datasketches::cpc_sketch src_sketch;
-  if (!DeserializeDsSketch(src, &src_sketch)) {
-    LogSketchDeserializationError(ctx);
+
+  datasketches::cpc_union union_sketch(DS_CPC_SKETCH_CONFIG);
+  try {
+    union_sketch.update(datasketches::cpc_sketch::deserialize(src.ptr, src.len));
+  } catch (const std::exception& e) {
+    LogSketchDeserializationError(ctx, e);
     return;
   }
-
   datasketches::cpc_sketch* dst_sketch_ptr =
       reinterpret_cast<datasketches::cpc_sketch*>(dst->ptr);
-
-  datasketches::cpc_union union_sketch(DS_CPC_SKETCH_CONFIG);
-  union_sketch.update(src_sketch);
   union_sketch.update(*dst_sketch_ptr);
-
   *dst_sketch_ptr = union_sketch.get_result();
 }
 
@@ -1957,7 +1912,7 @@ StringVal AggregateFunctions::DsCpcFinalizeSketch(
       reinterpret_cast<datasketches::cpc_sketch*>(src.ptr);
   StringVal result_str = StringVal::null();
   if (sketch_ptr->get_estimate() > 0.0) {
-    result_str = SerializeDsCpcSketch(ctx, *sketch_ptr);
+    result_str = SerializeDsSketch(ctx, *sketch_ptr);
   }
   ctx->Free(src.ptr);
   return result_str;
@@ -1979,16 +1934,12 @@ void AggregateFunctions::DsCpcUnionUpdate(
   if (src.is_null) return;
   DCHECK(!dst->is_null);
   DCHECK_EQ(dst->len, sizeof(datasketches::cpc_union));
-  // These parameters might be overwritten by DeserializeDsSketch() to use the settings
-  // from the deserialized sketch from 'src'.
-  datasketches::cpc_sketch src_sketch(DS_CPC_SKETCH_CONFIG);
-  if (!DeserializeDsSketch(src, &src_sketch)) {
-    LogSketchDeserializationError(ctx);
-    return;
+  try {
+    reinterpret_cast<datasketches::cpc_union*>(dst->ptr)
+        ->update(datasketches::cpc_sketch::deserialize(src.ptr, src.len));
+  } catch (const std::exception& e) {
+    LogSketchDeserializationError(ctx, e);
   }
-  datasketches::cpc_union* union_ptr =
-      reinterpret_cast<datasketches::cpc_union*>(dst->ptr);
-  union_ptr->update(src_sketch);
 }
 
 StringVal AggregateFunctions::DsCpcUnionSerialize(
@@ -2007,18 +1958,13 @@ void AggregateFunctions::DsCpcUnionMerge(
   DCHECK(!src.is_null);
   DCHECK(!dst->is_null);
   DCHECK_EQ(dst->len, sizeof(datasketches::cpc_union));
-
   // Note, 'src' is a serialized Cpc_sketch and not a serialized Cpc_union.
-  datasketches::cpc_sketch src_sketch(DS_CPC_SKETCH_CONFIG);
-  if (!DeserializeDsSketch(src, &src_sketch)) {
-    LogSketchDeserializationError(ctx);
-    return;
+  try {
+    reinterpret_cast<datasketches::cpc_union*>(dst->ptr)
+        ->update(datasketches::cpc_sketch::deserialize(src.ptr, src.len));
+  } catch (const std::exception& e) {
+    LogSketchDeserializationError(ctx, e);
   }
-
-  datasketches::cpc_union* dst_union_ptr =
-      reinterpret_cast<datasketches::cpc_union*>(dst->ptr);
-
-  dst_union_ptr->update(src_sketch);
 }
 
 StringVal AggregateFunctions::DsCpcUnionFinalize(
@@ -2032,7 +1978,7 @@ StringVal AggregateFunctions::DsCpcUnionFinalize(
     ctx->Free(src.ptr);
     return StringVal::null();
   }
-  StringVal result = SerializeDsCpcSketch(ctx, sketch);
+  StringVal result = SerializeDsSketch(ctx, sketch);
   ctx->Free(src.ptr);
   return result;
 }
@@ -2084,7 +2030,7 @@ StringVal AggregateFunctions::DsThetaSerialize(
   StringVal dst;
   if (src.len == sizeof(datasketches::update_theta_sketch)) {
     auto sketch_ptr = reinterpret_cast<datasketches::update_theta_sketch*>(src.ptr);
-    dst = SerializeDsThetaSketch(ctx, sketch_ptr->compact());
+    dst = SerializeDsSketch(ctx, sketch_ptr->compact());
   } else {
     auto union_ptr = reinterpret_cast<datasketches::theta_union*>(src.ptr);
     dst = SerializeDsThetaUnion(ctx, *union_ptr);
@@ -2101,21 +2047,27 @@ void AggregateFunctions::DsThetaMerge(
       or dst->len == sizeof(datasketches::theta_union));
 
   // Note, 'src' is a serialized compact_theta_sketch.
-  std::unique_ptr<datasketches::compact_theta_sketch> src_sketch;
-  if (!DeserializeDsSketch(src, &src_sketch)) {
-    LogSketchDeserializationError(ctx);
-    return;
-  }
-
   if (dst->len == sizeof(datasketches::theta_union)) {
     auto dst_union_ptr = reinterpret_cast<datasketches::theta_union*>(dst->ptr);
-    dst_union_ptr->update(*src_sketch);
+    try {
+      dst_union_ptr->update(datasketches::compact_theta_sketch::deserialize(src.ptr,
+          src.len));
+    } catch (const std::exception& e) {
+      LogSketchDeserializationError(ctx, e);
+      return;
+    }
   } else if (dst->len == sizeof(datasketches::update_theta_sketch)) {
     auto dst_sketch_ptr = reinterpret_cast<datasketches::update_theta_sketch*>(dst->ptr);
 
     datasketches::theta_union union_sketch = datasketches::theta_union::builder().build();
-    union_sketch.update(*src_sketch);
     union_sketch.update(*dst_sketch_ptr);
+    try {
+      union_sketch.update(datasketches::compact_theta_sketch::deserialize(src.ptr,
+          src.len));
+    } catch (const std::exception& e) {
+      LogSketchDeserializationError(ctx, e);
+      return;
+    }
 
     // theta_union.get_result() returns a compact sketch, does not support updating, and
     // is inconsistent with the initial underlying type of dst. This is different from
@@ -2157,7 +2109,7 @@ StringVal AggregateFunctions::DsThetaFinalizeSketch(
   StringVal result;
   if (src.len == sizeof(datasketches::update_theta_sketch)) {
     auto sketch_ptr = reinterpret_cast<datasketches::update_theta_sketch*>(src.ptr);
-    result = SerializeDsThetaSketch(ctx, sketch_ptr->compact());
+    result = SerializeDsSketch(ctx, sketch_ptr->compact());
   } else {
     auto union_ptr = reinterpret_cast<datasketches::theta_union*>(src.ptr);
     result = SerializeDsThetaUnion(ctx, *union_ptr);
@@ -2183,14 +2135,12 @@ void AggregateFunctions::DsThetaUnionUpdate(
   if (src.is_null) return;
   DCHECK(!dst->is_null);
   DCHECK_EQ(dst->len, sizeof(datasketches::theta_union));
-  std::unique_ptr<datasketches::compact_theta_sketch> src_sketch;
-  if (!DeserializeDsSketch(src, &src_sketch)) {
-    LogSketchDeserializationError(ctx);
-    return;
+  try {
+    reinterpret_cast<datasketches::theta_union*>(dst->ptr)
+        ->update(datasketches::compact_theta_sketch::deserialize(src.ptr, src.len));
+  } catch (const std::exception& e) {
+    LogSketchDeserializationError(ctx, e);
   }
-  datasketches::theta_union* union_ptr =
-      reinterpret_cast<datasketches::theta_union*>(dst->ptr);
-  union_ptr->update(*src_sketch);
 }
 
 StringVal AggregateFunctions::DsThetaUnionSerialize(
@@ -2209,18 +2159,13 @@ void AggregateFunctions::DsThetaUnionMerge(
   DCHECK(!src.is_null);
   DCHECK(!dst->is_null);
   DCHECK_EQ(dst->len, sizeof(datasketches::theta_union));
-
   // Note, 'src' is a serialized compact_theta_sketch and not a serialized theta_union.
-  std::unique_ptr<datasketches::compact_theta_sketch> src_sketch;
-  if (!DeserializeDsSketch(src, &src_sketch)) {
-    LogSketchDeserializationError(ctx);
-    return;
+  try {
+    reinterpret_cast<datasketches::theta_union*>(dst->ptr)
+        ->update(datasketches::compact_theta_sketch::deserialize(src.ptr, src.len));
+  } catch (const std::exception& e) {
+    LogSketchDeserializationError(ctx, e);
   }
-
-  datasketches::theta_union* dst_union_ptr =
-      reinterpret_cast<datasketches::theta_union*>(dst->ptr);
-
-  dst_union_ptr->update(*src_sketch);
 }
 
 StringVal AggregateFunctions::DsThetaUnionFinalize(
@@ -2234,7 +2179,7 @@ StringVal AggregateFunctions::DsThetaUnionFinalize(
     ctx->Free(src.ptr);
     return StringVal::null();
   }
-  StringVal result = SerializeDsThetaSketch(ctx, sketch);
+  StringVal result = SerializeDsSketch(ctx, sketch);
   ctx->Free(src.ptr);
   return result;
 }
@@ -2255,14 +2200,12 @@ void AggregateFunctions::DsThetaIntersectUpdate(
   if (src.is_null) return;
   DCHECK(!dst->is_null);
   DCHECK_EQ(dst->len, sizeof(datasketches::theta_intersection));
-  std::unique_ptr<datasketches::compact_theta_sketch> src_sketch;
-  if (!DeserializeDsSketch(src, &src_sketch)) {
-    LogSketchDeserializationError(ctx);
-    return;
+  try {
+    reinterpret_cast<datasketches::theta_intersection*>(dst->ptr)
+        ->update(datasketches::compact_theta_sketch::deserialize(src.ptr, src.len));
+  } catch (const std::exception& e) {
+    LogSketchDeserializationError(ctx, e);
   }
-  datasketches::theta_intersection* intersection_ptr =
-      reinterpret_cast<datasketches::theta_intersection*>(dst->ptr);
-  intersection_ptr->update(*src_sketch);
 }
 
 StringVal AggregateFunctions::DsThetaIntersectSerialize(
@@ -2281,19 +2224,14 @@ void AggregateFunctions::DsThetaIntersectMerge(
   if (src.is_null) return;
   DCHECK(!dst->is_null);
   DCHECK_EQ(dst->len, sizeof(datasketches::theta_intersection));
-
   // Note, 'src' is a serialized compact_theta_sketch and not a serialized
   // theta_intersection.
-  std::unique_ptr<datasketches::compact_theta_sketch> src_sketch;
-  if (!DeserializeDsSketch(src, &src_sketch)) {
-    LogSketchDeserializationError(ctx);
-    return;
+  try {
+    reinterpret_cast<datasketches::theta_intersection*>(dst->ptr)
+        ->update(datasketches::compact_theta_sketch::deserialize(src.ptr, src.len));
+  } catch (const std::exception& e) {
+    LogSketchDeserializationError(ctx, e);
   }
-
-  datasketches::theta_intersection* dst_intersection_ptr =
-      reinterpret_cast<datasketches::theta_intersection*>(dst->ptr);
-
-  dst_intersection_ptr->update(*src_sketch);
 }
 
 StringVal AggregateFunctions::DsThetaIntersectFinalize(
@@ -2307,7 +2245,7 @@ StringVal AggregateFunctions::DsThetaIntersectFinalize(
     return StringVal::null();
   }
   auto sketch = intersection_ptr->get_result();
-  StringVal result = SerializeDsThetaSketch(ctx, sketch);
+  StringVal result = SerializeDsSketch(ctx, sketch);
   ctx->Free(src.ptr);
   return result;
 }
@@ -2332,7 +2270,7 @@ StringVal AggregateFunctions::DsKllSerializeHelper(FunctionContext* ctx,
   DCHECK_EQ(src.len, sizeof(datasketches::kll_sketch<float>));
   datasketches::kll_sketch<float>* sketch_ptr =
       reinterpret_cast<datasketches::kll_sketch<float>*>(src.ptr);
-  StringVal dst = SerializeDsKllSketch(ctx, *sketch_ptr);
+  StringVal dst = SerializeDsSketch(ctx, *sketch_ptr);
   ctx->Free(src.ptr);
   return dst;
 }
@@ -2342,17 +2280,11 @@ void AggregateFunctions::DsKllMergeHelper(FunctionContext* ctx, const StringVal&
   DCHECK(!src.is_null);
   DCHECK(!dst->is_null);
   DCHECK_EQ(dst->len, sizeof(datasketches::kll_sketch<float>));
-  datasketches::kll_sketch<float> src_sketch;
-  if (!DeserializeDsSketch(src, &src_sketch)) {
-    LogSketchDeserializationError(ctx);
-    return;
-  }
 
   datasketches::kll_sketch<float>* dst_sketch_ptr =
       reinterpret_cast<datasketches::kll_sketch<float>*>(dst->ptr);
-
   try {
-    dst_sketch_ptr->merge(src_sketch);
+    dst_sketch_ptr->merge(datasketches::kll_sketch<float>::deserialize(src.ptr, src.len));
   } catch (const std::exception& e) {
     ctx->SetError(Substitute("Error while merging DataSketches KLL sketches. "
         "Message: $0", e.what()).c_str());
@@ -2370,7 +2302,7 @@ StringVal AggregateFunctions::DsKllFinalizeHelper(FunctionContext* ctx,
     ctx->Free(src.ptr);
     return StringVal::null();
   }
-  StringVal dst = SerializeDsKllSketch(ctx, *sketch_ptr);
+  StringVal dst = SerializeDsSketch(ctx, *sketch_ptr);
   ctx->Free(src.ptr);
   return dst;
 }
@@ -2416,15 +2348,10 @@ void AggregateFunctions::DsKllUnionUpdate(FunctionContext* ctx, const StringVal&
   if (src.is_null) return;
   DCHECK(!dst->is_null);
   DCHECK_EQ(dst->len, sizeof(datasketches::kll_sketch<float>));
-  datasketches::kll_sketch<float> src_sketch;
-  if (!DeserializeDsSketch(src, &src_sketch)) {
-    LogSketchDeserializationError(ctx);
-    return;
-  }
   datasketches::kll_sketch<float>* dst_sketch =
       reinterpret_cast<datasketches::kll_sketch<float>*>(dst->ptr);
   try {
-    dst_sketch->merge(src_sketch);
+    dst_sketch->merge(datasketches::kll_sketch<float>::deserialize(src.ptr, src.len));
   } catch (const std::exception& e) {
     ctx->SetError(Substitute("Error while merging DataSketches KLL sketches. "
         "Message: $0", e.what()).c_str());
diff --git a/be/src/exprs/datasketches-common.cc b/be/src/exprs/datasketches-common.cc
index c3e0b3b..bc26cb2 100644
--- a/be/src/exprs/datasketches-common.cc
+++ b/be/src/exprs/datasketches-common.cc
@@ -19,6 +19,7 @@
 
 #include "common/logging.h"
 #include "udf/udf-internal.h"
+#include "gutil/strings/substitute.h"
 #include "thirdparty/datasketches/cpc_sketch.hpp"
 #include "thirdparty/datasketches/kll_sketch.hpp"
 #include "thirdparty/datasketches/theta_sketch.hpp"
@@ -34,49 +35,11 @@ using impala_udf::StringVal;
 using std::stringstream;
 using std::vector;
 
-void LogSketchDeserializationError(FunctionContext* ctx) {
-  ctx->SetError("Unable to deserialize sketch.");
+void LogSketchDeserializationError(FunctionContext* ctx, const std::exception& e) {
+  ctx->SetError(strings::Substitute("Unable to deserialize sketch: $0",
+      e.what()).c_str());
 }
 
-template<class T>
-bool DeserializeDsSketch(const StringVal& serialized_sketch, T* sketch) {
-  DCHECK(sketch != nullptr);
-  if (serialized_sketch.is_null || serialized_sketch.len == 0) return false;
-  try {
-    *sketch = T::deserialize((void*)serialized_sketch.ptr, serialized_sketch.len);
-    return true;
-  } catch (const std::exception&) {
-    // One reason of throwing from deserialization is that the input string is not a
-    // serialized sketch.
-    return false;
-  }
-}
-
-// This is a specialization of the template DeserializeDsSketch() for theta sketches.
-template <>
-bool DeserializeDsSketch(const StringVal& serialized_sketch,
-    std::unique_ptr<compact_theta_sketch>* sketch_ptr) {
-  DCHECK(sketch_ptr->get() == nullptr);
-  if (serialized_sketch.is_null || serialized_sketch.len == 0) return false;
-  try {
-    auto sketch = compact_theta_sketch::deserialize(
-        (void*)serialized_sketch.ptr, serialized_sketch.len);
-    *sketch_ptr = std::make_unique<compact_theta_sketch>(sketch);
-    return true;
-  } catch (const std::exception&) {
-    // One reason of throwing from deserialization is that the input string is not a
-    // serialized sketch.
-    return false;
-  }
-}
-
-template bool DeserializeDsSketch(const StringVal& serialized_sketch,
-    hll_sketch* sketch);
-template bool DeserializeDsSketch(const StringVal& serialized_sketch,
-    cpc_sketch* sketch);
-template bool DeserializeDsSketch(const StringVal& serialized_sketch,
-    kll_sketch<float>* sketch);
-
 StringVal StringStreamToStringVal(FunctionContext* ctx, const stringstream& str_stream) {
   string str = str_stream.str();
   StringVal dst(ctx, str.size());
@@ -87,12 +50,13 @@ StringVal StringStreamToStringVal(FunctionContext* ctx, const stringstream& str_
 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);
+    try {
+      union_sketch.update(datasketches::cpc_sketch::deserialize(serialized_sketch.ptr,
+          serialized_sketch.len));
+    } catch (const std::exception& e) {
+      LogSketchDeserializationError(ctx, e);
       return false;
     }
-    union_sketch.update(sketch);
   }
   return true;
 }
@@ -100,12 +64,13 @@ bool update_sketch_to_cpc_union(FunctionContext* ctx,
 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) {
-    std::unique_ptr<datasketches::compact_theta_sketch> sketch_ptr;
-    if (!DeserializeDsSketch(serialized_sketch, &sketch_ptr)) {
-      LogSketchDeserializationError(ctx);
+    try {
+      sketch.update(datasketches::compact_theta_sketch::deserialize(serialized_sketch.ptr,
+          serialized_sketch.len));
+    } catch (const std::exception& e) {
+      LogSketchDeserializationError(ctx, e);
       return false;
     }
-    sketch.update(*sketch_ptr);
   }
   return true;
 }
@@ -113,13 +78,13 @@ bool update_sketch_to_theta_union(FunctionContext* ctx,
 bool update_sketch_to_theta_intersection(FunctionContext* ctx,
     const StringVal& serialized_sketch, datasketches::theta_intersection& sketch) {
   if (!serialized_sketch.is_null && serialized_sketch.len > 0) {
-    std::unique_ptr<datasketches::compact_theta_sketch> sketch_ptr;
-    if (!DeserializeDsSketch(serialized_sketch, &sketch_ptr)) {
-      LogSketchDeserializationError(ctx);
-      return false;
+    try {
+      sketch.update(datasketches::compact_theta_sketch::deserialize(serialized_sketch.ptr,
+          serialized_sketch.len));
+      return true;
+    } catch (const std::exception& e) {
+      LogSketchDeserializationError(ctx, e);
     }
-    sketch.update(*sketch_ptr);
-    return true;
   }
   return false;
 }
@@ -159,4 +124,3 @@ template bool RaiseErrorForNullOrNaNInput(FunctionContext* ctx, int num_args,
     const FloatVal* args);
 
 }
-
diff --git a/be/src/exprs/datasketches-common.h b/be/src/exprs/datasketches-common.h
index 0cf5273..0965ed4 100644
--- a/be/src/exprs/datasketches-common.h
+++ b/be/src/exprs/datasketches-common.h
@@ -50,16 +50,17 @@ const int DS_CPC_SKETCH_CONFIG = 11;
 const int DS_DEFAULT_KAPPA = 2;
 
 /// Logs a common error message saying that sketch deserialization failed.
-void LogSketchDeserializationError(FunctionContext* ctx);
-
-/// Receives a serialized DataSketches sketch (either Hll or KLL) in
-/// 'serialized_sketch', deserializes it and puts the deserialized sketch into 'sketch'.
-/// The outgoing 'sketch' will hold the same configs as 'serialized_sketch' regardless of
-/// what was provided when it was constructed before this function call. Returns false if
-/// the deserialization fails, true otherwise.
-template<class T>
-bool DeserializeDsSketch(const StringVal& serialized_sketch, T* sketch)
-    WARN_UNUSED_RESULT;
+void LogSketchDeserializationError(FunctionContext* ctx, const std::exception& e);
+
+/// Auxiliary function that receives a sketch and returns the serialized version of
+/// it wrapped into a StringVal.
+template<typename Sketch>
+StringVal SerializeDsSketch(FunctionContext* ctx, const Sketch& sketch) {
+  auto bytes = sketch.serialize();
+  StringVal result(ctx, bytes.size());
+  memcpy(result.ptr, bytes.data(), bytes.size());
+  return result;
+}
 
 /// Helper function that receives an std::stringstream and converts it to StringVal. Uses
 /// 'ctx' for memory allocation.
diff --git a/be/src/exprs/datasketches-functions-ir.cc b/be/src/exprs/datasketches-functions-ir.cc
index 4315fa3..6e516e9 100644
--- a/be/src/exprs/datasketches-functions-ir.cc
+++ b/be/src/exprs/datasketches-functions-ir.cc
@@ -36,12 +36,14 @@ using strings::Substitute;
 BigIntVal DataSketchesFunctions::DsHllEstimate(FunctionContext* ctx,
     const StringVal& serialized_sketch) {
   if (serialized_sketch.is_null || serialized_sketch.len == 0) return BigIntVal::null();
-  datasketches::hll_sketch sketch(DS_SKETCH_CONFIG, DS_HLL_TYPE);
-  if (!DeserializeDsSketch(serialized_sketch, &sketch)) {
-    LogSketchDeserializationError(ctx);
+  try {
+    auto sketch = datasketches::hll_sketch::deserialize(serialized_sketch.ptr,
+        serialized_sketch.len);
+    return sketch.get_estimate();
+  } catch (const std::exception& e) {
+    LogSketchDeserializationError(ctx, e);
     return BigIntVal::null();
   }
-  return sketch.get_estimate();
 }
 
 StringVal DataSketchesFunctions::DsHllEstimateBoundsAsString(
@@ -57,82 +59,89 @@ StringVal DataSketchesFunctions::DsHllEstimateBoundsAsString(
     ctx->SetError("Kappa must be 1, 2 or 3");
     return StringVal::null();
   }
-  datasketches::hll_sketch sketch(DS_SKETCH_CONFIG, DS_HLL_TYPE);
-  if (!DeserializeDsSketch(serialized_sketch, &sketch)) {
-    LogSketchDeserializationError(ctx);
+  try {
+    auto sketch = datasketches::hll_sketch::deserialize(serialized_sketch.ptr,
+        serialized_sketch.len);
+    std::stringstream buffer;
+    buffer << sketch.get_estimate() << "," << sketch.get_lower_bound(kappa.val) << ","
+           << sketch.get_upper_bound(kappa.val);
+    return StringStreamToStringVal(ctx, buffer);
+  } catch (const std::exception& e) {
+    LogSketchDeserializationError(ctx, e);
     return StringVal::null();
   }
-  std::stringstream buffer;
-  buffer << sketch.get_estimate() << "," << sketch.get_lower_bound(kappa.val) << ","
-         << sketch.get_upper_bound(kappa.val);
-  return StringStreamToStringVal(ctx, buffer);
 }
 
 StringVal DataSketchesFunctions::DsHllUnionF(FunctionContext* ctx,
-    const StringVal& first_serialized_sketch, const StringVal& second_serialized_sketch) {
-  // Union
+    const StringVal& serialized_sketch1, const StringVal& serialized_sketch2) {
   datasketches::hll_union union_sketch(DS_SKETCH_CONFIG);
-  // Deserialize two sketch
-  if (!first_serialized_sketch.is_null && first_serialized_sketch.len > 0) {
-    datasketches::hll_sketch first_sketch(DS_SKETCH_CONFIG, DS_HLL_TYPE);
-    if (!DeserializeDsSketch(first_serialized_sketch, &first_sketch)) {
-      LogSketchDeserializationError(ctx);
+  if (!serialized_sketch1.is_null && serialized_sketch1.len > 0) {
+    try {
+      union_sketch.update(datasketches::hll_sketch::deserialize(serialized_sketch1.ptr,
+          serialized_sketch1.len));
+    } catch (const std::exception& e) {
+      LogSketchDeserializationError(ctx, e);
       return StringVal::null();
     }
-    union_sketch.update(first_sketch);
   }
-  if (!second_serialized_sketch.is_null && second_serialized_sketch.len > 0) {
-    datasketches::hll_sketch second_sketch(DS_SKETCH_CONFIG, DS_HLL_TYPE);
-    if (!DeserializeDsSketch(second_serialized_sketch, &second_sketch)) {
-      LogSketchDeserializationError(ctx);
+  if (!serialized_sketch2.is_null && serialized_sketch2.len > 0) {
+    try {
+      union_sketch.update(datasketches::hll_sketch::deserialize(serialized_sketch2.ptr,
+          serialized_sketch2.len));
+    } catch (const std::exception& e) {
+      LogSketchDeserializationError(ctx, e);
       return StringVal::null();
     }
-    union_sketch.update(second_sketch);
   }
-  //  Result
-  datasketches::hll_sketch sketch = union_sketch.get_result(DS_HLL_TYPE);
-  std::stringstream serialized_input;
-  sketch.serialize_compact(serialized_input);
-  return StringStreamToStringVal(ctx, serialized_input);
+  auto bytes = union_sketch.get_result(DS_HLL_TYPE).serialize_compact();
+  StringVal result(ctx, bytes.size());
+  memcpy(result.ptr, bytes.data(), bytes.size());
+  return result;
 }
 
 StringVal DataSketchesFunctions::DsHllStringify(FunctionContext* ctx,
     const StringVal& serialized_sketch) {
   if (serialized_sketch.is_null || serialized_sketch.len == 0) return StringVal::null();
-  datasketches::hll_sketch sketch(DS_SKETCH_CONFIG, DS_HLL_TYPE);
-  if (!DeserializeDsSketch(serialized_sketch, &sketch)) {
-    LogSketchDeserializationError(ctx);
+  try {
+    auto sketch = datasketches::hll_sketch::deserialize(serialized_sketch.ptr,
+        serialized_sketch.len);
+    string str = sketch.to_string(true, false, false, false);
+    StringVal dst(ctx, str.size());
+    memcpy(dst.ptr, str.c_str(), str.size());
+    return dst;
+  } catch (const std::exception& e) {
+    LogSketchDeserializationError(ctx, e);
     return StringVal::null();
   }
-  string str = sketch.to_string(true, false, false, false);
-  StringVal dst(ctx, str.size());
-  memcpy(dst.ptr, str.c_str(), str.size());
-  return dst;
 }
 
 BigIntVal DataSketchesFunctions::DsCpcEstimate(
     FunctionContext* ctx, const StringVal& serialized_sketch) {
   if (serialized_sketch.is_null || serialized_sketch.len == 0) return BigIntVal::null();
-  datasketches::cpc_sketch sketch(DS_CPC_SKETCH_CONFIG);
-  if (!DeserializeDsSketch(serialized_sketch, &sketch)) {
-    LogSketchDeserializationError(ctx);
+  try {
+    auto sketch = datasketches::cpc_sketch::deserialize(serialized_sketch.ptr,
+        serialized_sketch.len);
+    return sketch.get_estimate();
+  } catch (const std::exception& e) {
+    LogSketchDeserializationError(ctx, e);
     return BigIntVal::null();
   }
-  return sketch.get_estimate();
 }
 
 StringVal DataSketchesFunctions::DsCpcStringify(
     FunctionContext* ctx, const StringVal& serialized_sketch) {
   if (serialized_sketch.is_null || serialized_sketch.len == 0) return StringVal::null();
-  datasketches::cpc_sketch sketch(DS_CPC_SKETCH_CONFIG);
-  if (!DeserializeDsSketch(serialized_sketch, &sketch)) {
-    LogSketchDeserializationError(ctx);
+  try {
+    auto sketch = datasketches::cpc_sketch::deserialize(serialized_sketch.ptr,
+        serialized_sketch.len);
+    string str = sketch.to_string();
+    StringVal dst(ctx, str.size());
+    memcpy(dst.ptr, str.c_str(), str.size());
+    return dst;
+  } catch (const std::exception& e) {
+    LogSketchDeserializationError(ctx, e);
     return StringVal::null();
   }
-  string str = sketch.to_string();
-  StringVal dst(ctx, str.size());
-  memcpy(dst.ptr, str.c_str(), str.size());
-  return dst;
 }
 
 StringVal DataSketchesFunctions::DsCpcUnionF(FunctionContext* ctx,
@@ -145,56 +154,50 @@ StringVal DataSketchesFunctions::DsCpcUnionF(FunctionContext* ctx,
   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);
+  auto bytes = union_sketch.get_result().serialize();
+  StringVal result(ctx, bytes.size());
+  memcpy(result.ptr, bytes.data(), bytes.size());
+  return result;
 }
 
 BigIntVal DataSketchesFunctions::DsThetaEstimate(
     FunctionContext* ctx, const StringVal& serialized_sketch) {
   if (serialized_sketch.is_null || serialized_sketch.len == 0) return 0;
-  std::unique_ptr<datasketches::compact_theta_sketch> sketch;
-  if (!DeserializeDsSketch(serialized_sketch, &sketch)) {
-    LogSketchDeserializationError(ctx);
+  try {
+    auto sketch = datasketches::compact_theta_sketch::deserialize(serialized_sketch.ptr,
+        serialized_sketch.len);
+    return sketch.get_estimate();
+  } catch (const std::exception& e) {
+    LogSketchDeserializationError(ctx, e);
     return BigIntVal::null();
   }
-  return sketch->get_estimate();
 }
 
 StringVal DataSketchesFunctions::DsThetaExclude(FunctionContext* ctx,
-    const StringVal& first_serialized_sketch, const StringVal& second_serialized_sketch) {
-  datasketches::theta_a_not_b a_not_b;
-  // Deserialize two sketches
-  std::unique_ptr<datasketches::compact_theta_sketch> first_sketch_ptr;
-  if (!first_serialized_sketch.is_null && first_serialized_sketch.len > 0) {
-    if (!DeserializeDsSketch(first_serialized_sketch, &first_sketch_ptr)) {
-      LogSketchDeserializationError(ctx);
-      return StringVal::null();
-    }
-  }
-  std::unique_ptr<datasketches::compact_theta_sketch> second_sketch_ptr;
-  if (!second_serialized_sketch.is_null && second_serialized_sketch.len > 0) {
-    if (!DeserializeDsSketch(second_serialized_sketch, &second_sketch_ptr)) {
-      LogSketchDeserializationError(ctx);
-      return StringVal::null();
-    }
-  }
+    const StringVal& serialized_sketch1, const StringVal& serialized_sketch2) {
   // Note, A and B refer to the two input sketches in the order A-not-B.
   // if A is null return null.
   // if A is not null, B is null return copyA.
   // other return A-not-B.
-  if (first_sketch_ptr) {
-    if (!second_sketch_ptr) {
-      return StringVal::CopyFrom(
-          ctx, first_serialized_sketch.ptr, first_serialized_sketch.len);
+  if (!serialized_sketch1.is_null && serialized_sketch1.len > 0) {
+    if (serialized_sketch2.is_null || serialized_sketch2.len == 0) {
+      return StringVal::CopyFrom(ctx, serialized_sketch1.ptr, serialized_sketch1.len);
     }
     // A and B are not null, call a_not_b.compute()
-    auto result = a_not_b.compute(*first_sketch_ptr, *second_sketch_ptr);
-    std::stringstream serialized_input;
-    result.serialize(serialized_input);
-    return StringStreamToStringVal(ctx, serialized_input);
+    datasketches::theta_a_not_b a_not_b;
+    try {
+      auto bytes = a_not_b.compute(
+        datasketches::compact_theta_sketch::deserialize(serialized_sketch1.ptr,
+            serialized_sketch1.len),
+        datasketches::compact_theta_sketch::deserialize(serialized_sketch2.ptr,
+            serialized_sketch2.len)
+      ).serialize();
+      StringVal result(ctx, bytes.size());
+      memcpy(result.ptr, bytes.data(), bytes.size());
+      return result;
+    } catch (const std::exception& e) {
+      LogSketchDeserializationError(ctx, e);
+    }
   }
   return StringVal::null();
 }
@@ -209,11 +212,10 @@ StringVal DataSketchesFunctions::DsThetaUnionF(FunctionContext* ctx,
   if (!update_sketch_to_theta_union(ctx, second_serialized_sketch, union_sketch)) {
     return StringVal::null();
   }
-  //  Result
-  datasketches::compact_theta_sketch sketch = union_sketch.get_result();
-  std::stringstream serialized_input;
-  sketch.serialize(serialized_input);
-  return StringStreamToStringVal(ctx, serialized_input);
+  auto bytes = union_sketch.get_result().serialize();
+  StringVal result(ctx, bytes.size());
+  memcpy(result.ptr, bytes.data(), bytes.size());
+  return result;
 }
 
 StringVal DataSketchesFunctions::DsThetaIntersectF(FunctionContext* ctx,
@@ -229,10 +231,10 @@ StringVal DataSketchesFunctions::DsThetaIntersectF(FunctionContext* ctx,
           ctx, second_serialized_sketch, intersection_sketch)) {
     return StringVal::null();
   }
-  datasketches::compact_theta_sketch sketch = intersection_sketch.get_result();
-  std::stringstream serialized_input;
-  sketch.serialize(serialized_input);
-  return StringStreamToStringVal(ctx, serialized_input);
+  auto bytes = intersection_sketch.get_result().serialize();
+  StringVal result(ctx, bytes.size());
+  memcpy(result.ptr, bytes.data(), bytes.size());
+  return result;
 }
 
 FloatVal DataSketchesFunctions::DsKllQuantile(FunctionContext* ctx,
@@ -242,12 +244,9 @@ FloatVal DataSketchesFunctions::DsKllQuantile(FunctionContext* ctx,
     ctx->SetError("Rank parameter should be in the range of [0,1]");
     return FloatVal::null();
   }
-  datasketches::kll_sketch<float> sketch;
-  if (!DeserializeDsSketch(serialized_sketch, &sketch)) {
-    LogSketchDeserializationError(ctx);
-    return FloatVal::null();
-  }
   try {
+    auto sketch = datasketches::kll_sketch<float>::deserialize(serialized_sketch.ptr,
+        serialized_sketch.len);
     return sketch.get_quantile(rank.val);
   } catch (const std::exception& e) {
     ctx->SetError(Substitute("Error while getting quantile from DataSketches KLL. "
@@ -259,23 +258,27 @@ FloatVal DataSketchesFunctions::DsKllQuantile(FunctionContext* ctx,
 BigIntVal DataSketchesFunctions::DsKllN(FunctionContext* ctx,
     const StringVal& serialized_sketch) {
   if (serialized_sketch.is_null || serialized_sketch.len == 0) return BigIntVal::null();
-  datasketches::kll_sketch<float> sketch;
-  if (!DeserializeDsSketch(serialized_sketch, &sketch)) {
-    LogSketchDeserializationError(ctx);
+  try {
+    auto sketch = datasketches::kll_sketch<float>::deserialize(serialized_sketch.ptr,
+        serialized_sketch.len);
+    return sketch.get_n();
+  } catch (const std::exception& e) {
+    LogSketchDeserializationError(ctx, e);
     return BigIntVal::null();
   }
-  return sketch.get_n();
 }
 
 DoubleVal DataSketchesFunctions::DsKllRank(FunctionContext* ctx,
     const StringVal& serialized_sketch, const FloatVal& probe_value) {
   if (serialized_sketch.is_null || serialized_sketch.len == 0) return DoubleVal::null();
-  datasketches::kll_sketch<float> sketch;
-  if (!DeserializeDsSketch(serialized_sketch, &sketch)) {
-    LogSketchDeserializationError(ctx);
+  try {
+    auto sketch = datasketches::kll_sketch<float>::deserialize(serialized_sketch.ptr,
+        serialized_sketch.len);
+    return sketch.get_rank(probe_value.val);
+  } catch (const std::exception& e) {
+    LogSketchDeserializationError(ctx, e);
     return DoubleVal::null();
   }
-  return sketch.get_rank(probe_value.val);
 }
 
 StringVal DataSketchesFunctions::DsKllQuantilesAsString(FunctionContext* ctx,
@@ -284,14 +287,11 @@ StringVal DataSketchesFunctions::DsKllQuantilesAsString(FunctionContext* ctx,
   if (args == nullptr) return StringVal::null();
   if (serialized_sketch.is_null || serialized_sketch.len == 0) return StringVal::null();
   if (RaiseErrorForNullOrNaNInput(ctx, num_args, args)) return StringVal::null();
-  datasketches::kll_sketch<float> sketch;
-  if (!DeserializeDsSketch(serialized_sketch, &sketch)) {
-    LogSketchDeserializationError(ctx);
-    return StringVal::null();
-  }
-  double quantiles_input[(unsigned int)num_args];
-  for (int i = 0; i < num_args; ++i) quantiles_input[i] = args[i].val;
   try {
+    auto sketch = datasketches::kll_sketch<float>::deserialize(serialized_sketch.ptr,
+        serialized_sketch.len);
+    double quantiles_input[(unsigned int)num_args];
+    for (int i = 0; i < num_args; ++i) quantiles_input[i] = args[i].val;
     std::vector<float> results = sketch.get_quantiles(quantiles_input, num_args);
     return DsKllVectorResultToStringVal(ctx, results);
   } catch(const std::exception& e) {
@@ -308,14 +308,11 @@ StringVal DataSketchesFunctions::GetDsKllPMFOrCDF(FunctionContext* ctx,
   if (args == nullptr || args->is_null) return StringVal::null();
   if (serialized_sketch.is_null || serialized_sketch.len == 0) return StringVal::null();
   if (RaiseErrorForNullOrNaNInput(ctx, num_args, args)) return StringVal::null();
-  datasketches::kll_sketch<float> sketch;
-  if (!DeserializeDsSketch(serialized_sketch, &sketch)) {
-    LogSketchDeserializationError(ctx);
-    return StringVal::null();
-  }
-  float input_ranges[(unsigned int)num_args];
-  for (int i = 0; i < num_args; ++i) input_ranges[i] = args[i].val;
   try {
+    auto sketch = datasketches::kll_sketch<float>::deserialize(serialized_sketch.ptr,
+        serialized_sketch.len);
+    float input_ranges[(unsigned int)num_args];
+    for (int i = 0; i < num_args; ++i) input_ranges[i] = args[i].val;
     std::vector<double> results = (mode == PMF) ?
         sketch.get_PMF(input_ranges, num_args) : sketch.get_CDF(input_ranges, num_args);
     return DsKllVectorResultToStringVal(ctx, results);
@@ -340,15 +337,17 @@ StringVal DataSketchesFunctions::DsKllCDFAsString(FunctionContext* ctx,
 StringVal DataSketchesFunctions::DsKllStringify( FunctionContext* ctx,
     const StringVal& serialized_sketch) {
   if (serialized_sketch.is_null || serialized_sketch.len == 0) return StringVal::null();
-  datasketches::kll_sketch<float> sketch;
-  if (!DeserializeDsSketch(serialized_sketch, &sketch)) {
-    LogSketchDeserializationError(ctx);
+  try {
+    auto sketch = datasketches::kll_sketch<float>::deserialize(serialized_sketch.ptr,
+        serialized_sketch.len);
+    string str = sketch.to_string(false, false);
+    StringVal dst(ctx, str.size());
+    memcpy(dst.ptr, str.c_str(), str.size());
+    return dst;
+  } catch (const std::exception& e) {
+    LogSketchDeserializationError(ctx, e);
     return StringVal::null();
   }
-  string str = sketch.to_string(false, false);
-  StringVal dst(ctx, str.size());
-  memcpy(dst.ptr, str.c_str(), str.size());
-  return dst;
 }
 
 }
diff --git a/testdata/workloads/functional-query/queries/QueryTest/datasketches-cpc.test b/testdata/workloads/functional-query/queries/QueryTest/datasketches-cpc.test
index 1ee5d53..757e352 100644
--- a/testdata/workloads/functional-query/queries/QueryTest/datasketches-cpc.test
+++ b/testdata/workloads/functional-query/queries/QueryTest/datasketches-cpc.test
@@ -140,7 +140,7 @@ INT,INT,BIGINT,BIGINT
 # Check that ds_cpc_estimate returns error for strings that are not serialized sketches.
 select ds_cpc_estimate(date_string_col) from functional_parquet.alltypestiny;
 ---- CATCH
-UDF ERROR: Unable to deserialize sketch.
+UDF ERROR: Unable to deserialize sketch: Insufficient buffer size detected: bytes available 8, minimum needed 192
 ====
 ---- QUERY
 # Check that ds_cpc_estimate returns null for null and empty string inputs.
@@ -219,7 +219,7 @@ STRING
 # ds_cpc_union() returns an error if it receives an invalid serialized sketch.
 select ds_cpc_union(date_string_col) from functional_parquet.alltypestiny where id=1;
 ---- CATCH
-UDF ERROR: Unable to deserialize sketch.
+UDF ERROR: Unable to deserialize sketch: Insufficient buffer size detected: bytes available 8, minimum needed 192
 ====
 ---- QUERY
 # Get the same sketches from Impala and Hive and put them into the same table. When we
@@ -266,7 +266,7 @@ STRING
 # Check that ds_cpc_stringify() returns error for strings that are not serialized sketches.
 select ds_cpc_stringify(date_string_col) from functional_parquet.alltypestiny;
 ---- CATCH
-UDF ERROR: Unable to deserialize sketch.
+UDF ERROR: Unable to deserialize sketch: Insufficient buffer size detected: bytes available 8, minimum needed 192
 ====
 ---- QUERY
 # Check that ds_cpc_stringify() works on sketches created by Hive.
@@ -320,14 +320,14 @@ BIGINT
 select ds_cpc_union_f(null, date_string_col) from functional_parquet.alltypestiny
 where id=1;
 ---- CATCH
-UDF ERROR: Unable to deserialize sketch.
+UDF ERROR: Unable to deserialize sketch: Insufficient buffer size detected: bytes available 8, minimum needed 192
 ====
 ---- 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.
+UDF ERROR: Unable to deserialize sketch: Insufficient buffer size detected: bytes available 8, minimum needed 192
 ====
 ---- QUERY
 # Get the same sketches from Impala and Hive and put them into the same table (different
diff --git a/testdata/workloads/functional-query/queries/QueryTest/datasketches-hll.test b/testdata/workloads/functional-query/queries/QueryTest/datasketches-hll.test
index f6b188b..c5ed91a 100644
--- a/testdata/workloads/functional-query/queries/QueryTest/datasketches-hll.test
+++ b/testdata/workloads/functional-query/queries/QueryTest/datasketches-hll.test
@@ -138,7 +138,7 @@ INT,INT,BIGINT,BIGINT
 # Check that ds_hll_estimate returns error for strings that are not serialized sketches.
 select ds_hll_estimate(date_string_col) from functional_parquet.alltypestiny;
 ---- CATCH
-UDF ERROR: Unable to deserialize sketch.
+UDF ERROR: Unable to deserialize sketch: Attempt to deserialize unknown object type
 ====
 ---- QUERY
 # Check that ds_hll_estimate returns null for null and empty string inputs.
@@ -198,7 +198,7 @@ STRING
 # ds_hll_union() returns an error if it receives an invalid serialized sketch.
 select ds_hll_union(date_string_col) from functional_parquet.alltypestiny where id=1;
 ---- CATCH
-UDF ERROR: Unable to deserialize sketch.
+UDF ERROR: Unable to deserialize sketch: Attempt to deserialize unknown object type
 ====
 ---- QUERY
 # Get the same sketches from Impala and Hive and put them into the same table. When we
@@ -291,7 +291,7 @@ STRING
 # serialized sketches.
 select ds_hll_estimate_bounds_as_string(date_string_col) from functional_parquet.alltypestiny;
 ---- CATCH
-UDF ERROR: Unable to deserialize sketch.
+UDF ERROR: Unable to deserialize sketch: Attempt to deserialize unknown object type
 ====
 ---- QUERY
 # Check that ds_hll_estimate_bounds_as_string() returns error for kappa is not 1, 2, 3.
@@ -379,14 +379,14 @@ BIGINT
 select ds_hll_union_f(null, date_string_col) from functional_parquet.alltypestiny
 where id=1;
 ---- CATCH
-UDF ERROR: Unable to deserialize sketch.
+UDF ERROR: Unable to deserialize sketch: Attempt to deserialize unknown object type
 ====
 ---- QUERY
 # ds_hll_union_f() returns an error if it receives an invalid serialized sketch.
 select ds_hll_union_f(date_string_col, null) from functional_parquet.alltypestiny
 where id=1;
 ---- CATCH
-UDF ERROR: Unable to deserialize sketch.
+UDF ERROR: Unable to deserialize sketch: Attempt to deserialize unknown object type
 ====
 ---- QUERY
 # Get the same sketches from Impala and Hive and put them into the same table (different
diff --git a/testdata/workloads/functional-query/queries/QueryTest/datasketches-kll.test b/testdata/workloads/functional-query/queries/QueryTest/datasketches-kll.test
index 197218f..5ec403b 100644
--- a/testdata/workloads/functional-query/queries/QueryTest/datasketches-kll.test
+++ b/testdata/workloads/functional-query/queries/QueryTest/datasketches-kll.test
@@ -3,7 +3,7 @@
 # Check that ds_kll_quantile returns error for strings that are not serialized sketches.
 select ds_kll_quantile(date_string_col, 0.5) from functional_parquet.alltypestiny;
 ---- CATCH
-UDF ERROR: Unable to deserialize sketch
+UDF ERROR: Error while getting quantile from DataSketches KLL. Message: Possible corruption: M must be 8: 48
 ====
 ---- QUERY
 select ds_kll_quantile(ds_kll_sketch(float_col), -0.1) from functional_parquet.alltypestiny;
@@ -266,7 +266,7 @@ STRING
 # ds_kll_union() returns an error if it receives an invalid serialized sketch.
 select ds_kll_union(date_string_col) from functional_parquet.alltypestiny where id=1;
 ---- CATCH
-UDF ERROR: Unable to deserialize sketch.
+UDF ERROR: Error while merging DataSketches KLL sketches. Message: Possible corruption: M must be 8: 48
 ====
 ---- QUERY
 # Get the same sketches from Impala and Hive and put them into the same table. When we
@@ -313,7 +313,7 @@ select
     ds_kll_quantiles_as_string(date_string_col, 0.5)
 from functional_parquet.alltypestiny;
 ---- CATCH
-UDF ERROR: Unable to deserialize sketch
+UDF ERROR: Error while getting quantiles from DataSketches KLL. Message: Possible corruption: M must be 8: 48
 ====
 ---- QUERY
 # Check error when rank is out of range
@@ -430,7 +430,7 @@ STRING
 # sketches.
 select ds_kll_pmf_as_string(date_string_col, 0.5) from functional_parquet.alltypestiny;
 ---- CATCH
-UDF ERROR: Unable to deserialize sketch
+UDF ERROR: Error while running DataSketches KLL function. Message: Possible corruption: M must be 8: 48
 ====
 ---- QUERY
 select
@@ -507,7 +507,7 @@ STRING
 # sketches.
 select ds_kll_cdf_as_string(date_string_col, 0.5) from functional_parquet.alltypestiny;
 ---- CATCH
-UDF ERROR: Unable to deserialize sketch
+UDF ERROR: Error while running DataSketches KLL function. Message: Possible corruption: M must be 8: 48
 ====
 ---- QUERY
 select
diff --git a/testdata/workloads/functional-query/queries/QueryTest/datasketches-theta.test b/testdata/workloads/functional-query/queries/QueryTest/datasketches-theta.test
index cc03b22..e535695 100644
--- a/testdata/workloads/functional-query/queries/QueryTest/datasketches-theta.test
+++ b/testdata/workloads/functional-query/queries/QueryTest/datasketches-theta.test
@@ -138,17 +138,17 @@ INT,INT,BIGINT,BIGINT
 # Check that ds_theta_estimate returns error for strings that are not serialized sketches.
 select ds_theta_estimate(date_string_col) from functional_parquet.alltypestiny;
 ---- CATCH
-UDF ERROR: Unable to deserialize sketch.
+UDF ERROR: Unable to deserialize sketch: sketch type mismatch: expected 3, actual 47
 ====
 ---- QUERY
-# Check that ds_theta_estimate returns error for HLL serialized sketches .
+# Check that ds_theta_estimate returns error for HLL serialized sketches.
 create table hll_sketch_store (date_sketch string) stored as parquet;
 insert into hll_sketch_store
     select ds_hll_sketch(date_string_col)
     from functional_parquet.alltypessmall;
 select ds_theta_estimate(date_sketch) from hll_sketch_store;
 ---- CATCH
-UDF ERROR: Unable to deserialize sketch.
+UDF ERROR: Unable to deserialize sketch: sketch type mismatch: expected 3, actual 7
 ====
 ---- QUERY
 # Check that ds_theta_estimate returns null for null and empty string inputs.
@@ -208,7 +208,7 @@ STRING
 # ds_theta_union() returns an error if it receives an invalid serialized sketch.
 select ds_theta_union(date_string_col) from functional_parquet.alltypestiny where id=1;
 ---- CATCH
-UDF ERROR: Unable to deserialize sketch.
+UDF ERROR: Unable to deserialize sketch: sketch type mismatch: expected 3, actual 47
 ====
 ---- QUERY
 # Get the same sketches from Impala and Hive and put them into the same table. When we
@@ -265,7 +265,7 @@ STRING
 select ds_theta_intersect(date_string_col) from functional_parquet.alltypestiny
 where id=1;
 ---- CATCH
-UDF ERROR: Unable to deserialize sketch.
+UDF ERROR: Unable to deserialize sketch: sketch type mismatch: expected 3, actual 47
 ====
 ---- QUERY
 # Check that sketches made by Hive for intersection and estimation, which is consistent
@@ -354,17 +354,10 @@ BIGINT
 ====
 ---- QUERY
 # ds_theta_exclude() returns an error if it receives an invalid serialized sketch.
-select ds_theta_exclude(null, date_string_col) from functional_parquet.alltypestiny
+select ds_theta_exclude(date_string_col, date_string_col) from functional_parquet.alltypestiny
 where id=1;
 ---- CATCH
-UDF ERROR: Unable to deserialize sketch.
-====
----- QUERY
-# ds_theta_exclude() returns an error if it receives an invalid serialized sketch.
-select ds_theta_exclude(date_string_col, null) from functional_parquet.alltypestiny
-where id=1;
----- CATCH
-UDF ERROR: Unable to deserialize sketch.
+UDF ERROR: Unable to deserialize sketch: sketch type mismatch: expected 3, actual 47
 ====
 ---- QUERY
 # Get the same sketches from Impala and Hive and put them into the same table (different
@@ -446,14 +439,14 @@ BIGINT
 select ds_theta_union_f(null, date_string_col) from functional_parquet.alltypestiny
 where id=1;
 ---- CATCH
-UDF ERROR: Unable to deserialize sketch.
+UDF ERROR: Unable to deserialize sketch: sketch type mismatch: expected 3, actual 47
 ====
 ---- QUERY
 # ds_theta_union_f() returns an error if it receives an invalid serialized sketch.
 select ds_theta_union_f(date_string_col, null) from functional_parquet.alltypestiny
 where id=1;
 ---- CATCH
-UDF ERROR: Unable to deserialize sketch.
+UDF ERROR: Unable to deserialize sketch: sketch type mismatch: expected 3, actual 47
 ====
 ---- QUERY
 # Unions the sketches from theta_sketches_impala_hive2 and checks if the union produces
@@ -520,7 +513,7 @@ BIGINT
 select ds_theta_intersect_f(date_string_col, null) from functional_parquet.alltypestiny
 where id=1;
 ---- CATCH
-UDF ERROR: Unable to deserialize sketch.
+UDF ERROR: Unable to deserialize sketch: sketch type mismatch: expected 3, actual 47
 ====
 ---- QUERY
 # ds_theta_intersect_f() returns an error if it receives an invalid serialized sketch.
@@ -529,7 +522,7 @@ select ds_theta_sketch(float_col) sketch1, max(date_string_col) sketch2 from
 functional_parquet.alltypestiny
 ) t
 ---- CATCH
-UDF ERROR: Unable to deserialize sketch.
+UDF ERROR: Unable to deserialize sketch: sketch type mismatch: expected 3, actual 47
 ====
 ---- QUERY
 # Intersect the sketches from theta_sketches_impala_hive2 and checks if the intersect