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:47 UTC

[impala] branch master updated (3850d49 -> 35b2108)

This is an automated email from the ASF dual-hosted git repository.

stigahuang pushed a change to branch master
in repository https://gitbox.apache.org/repos/asf/impala.git.


    from 3850d49  IMPALA-9662,IMPALA-2019(part-3): Support UTF-8 mode in mask functions
     new c925807  IMPALA-10901 cleaner and faster operations with datasketches
     new 35b2108  IMPALA-6505: Min-Max predicate push down in ORC scanner

The 2 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.


Summary of changes:
 be/src/exec/hdfs-orc-scanner.cc                    | 245 +++++++++-
 be/src/exec/hdfs-orc-scanner.h                     |  26 +
 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 +++++-----
 be/src/exprs/scalar-expr.h                         |   3 +
 be/src/service/query-options.cc                    |   4 +
 be/src/service/query-options.h                     |   4 +-
 bin/impala-config.sh                               |   4 +-
 common/thrift/ImpalaService.thrift                 |   3 +
 common/thrift/Query.thrift                         |   3 +
 .../org/apache/impala/planner/HdfsScanNode.java    |  48 +-
 .../queries/PlannerTest/acid-scans.test            |  12 +-
 .../queries/QueryTest/datasketches-cpc.test        |  10 +-
 .../queries/QueryTest/datasketches-hll.test        |  10 +-
 .../queries/QueryTest/datasketches-kll.test        |  10 +-
 .../queries/QueryTest/datasketches-theta.test      |  29 +-
 .../queries/QueryTest/orc-stats.test               | 542 +++++++++++++++++++++
 tests/query_test/test_nested_types.py              |   3 +-
 .../test_set.py => query_test/test_orc_stats.py}   |  31 +-
 21 files changed, 1148 insertions(+), 412 deletions(-)
 create mode 100644 testdata/workloads/functional-query/queries/QueryTest/orc-stats.test
 copy tests/{metadata/test_set.py => query_test/test_orc_stats.py} (58%)

[impala] 02/02: IMPALA-6505: Min-Max predicate push down in ORC scanner

Posted by st...@apache.org.
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 35b21083b1866b7056e3810ae5a8daf7bc77ddda
Author: norbert.luksa <no...@cloudera.com>
AuthorDate: Tue Mar 10 12:13:00 2020 +0100

    IMPALA-6505: Min-Max predicate push down in ORC scanner
    
    In planning phase, the planner collects and generates min-max predicates
    that can be evaluated on parquet file statistics. We can easily extend
    this on ORC tables.
    
    This commit implements min/max predicate pushdown for the ORC scanner
    leveraging on the external ORC library's search arguments. We build
    the search arguments when we open the scanner as we need not to
    modify them later.
    
    Also added a new query option orc_read_statistics, similar to
    parquet_read_statistics. If the option is set to true (it is by default)
    predicate pushdown will take effect, otherwise it will be skipped. The
    predicates will be evaluated at ORC row group level, i.e. by default for
    every 10,000 rows.
    
    Limitations:
     - Min-max predicates on CHAR/VARCHAR types are not pushed down due to
       inconsistent behaviors on padding/truncating between Hive and Impala.
       (IMPALA-10882)
     - Min-max predicates on TIMESTAMP are not pushed down (IMPALA-10915).
     - Min-max predicates having different arg types are not pushed down
       (IMPALA-10916).
     - Min-max predicates with non-literal const exprs are not pushed down
       since SearchArgument interfaces only accept literals. This only
       happens when expr rewrites are disabled thus constant folding is
       disabled.
    
    Tests:
     - Add e2e tests similar to test_parquet_stats to verify that
       predicates are pushed down.
     - Run CORE tests
     - Run TPCH benchmark, there is no improvement, nor regression.
       On the other hand, certain selective queries gained significant
       speed-up, e.g. select count(*) from lineitem where l_orderkey = 1.
    
    Change-Id: I136622413db21e0941d238ab6aeea901a6464845
    Reviewed-on: http://gerrit.cloudera.org:8080/15403
    Reviewed-by: Csaba Ringhofer <cs...@cloudera.com>
    Reviewed-by: Qifan Chen <qc...@cloudera.com>
    Tested-by: Impala Public Jenkins <im...@cloudera.com>
---
 be/src/exec/hdfs-orc-scanner.cc                    | 245 +++++++++-
 be/src/exec/hdfs-orc-scanner.h                     |  26 +
 be/src/exprs/scalar-expr.h                         |   3 +
 be/src/service/query-options.cc                    |   4 +
 be/src/service/query-options.h                     |   4 +-
 bin/impala-config.sh                               |   4 +-
 common/thrift/ImpalaService.thrift                 |   3 +
 common/thrift/Query.thrift                         |   3 +
 .../org/apache/impala/planner/HdfsScanNode.java    |  48 +-
 .../queries/PlannerTest/acid-scans.test            |  12 +-
 .../queries/QueryTest/orc-stats.test               | 542 +++++++++++++++++++++
 tests/query_test/test_nested_types.py              |   3 +-
 tests/query_test/test_orc_stats.py                 |  40 ++
 13 files changed, 915 insertions(+), 22 deletions(-)

diff --git a/be/src/exec/hdfs-orc-scanner.cc b/be/src/exec/hdfs-orc-scanner.cc
index d26839c..ed86477 100644
--- a/be/src/exec/hdfs-orc-scanner.cc
+++ b/be/src/exec/hdfs-orc-scanner.cc
@@ -24,6 +24,7 @@
 #include "exec/scanner-context.inline.h"
 #include "exec/scratch-tuple-batch.h"
 #include "exprs/expr.h"
+#include "exprs/scalar-expr.h"
 #include "runtime/collection-value-builder.h"
 #include "runtime/exec-env.h"
 #include "runtime/io/request-context.h"
@@ -138,6 +139,7 @@ HdfsOrcScanner::HdfsOrcScanner(HdfsScanNodeBase* scan_node, RuntimeState* state)
   : HdfsColumnarScanner(scan_node, state),
     dictionary_pool_(new MemPool(scan_node->mem_tracker())),
     data_batch_pool_(new MemPool(scan_node->mem_tracker())),
+    search_args_pool_(new MemPool(scan_node->mem_tracker())),
     assemble_rows_timer_(scan_node_->materialize_tuple_timer()) {
   assemble_rows_timer_.Stop();
 }
@@ -240,6 +242,8 @@ Status HdfsOrcScanner::Open(ScannerContext* context) {
   // blob more efficiently.
   row_reader_options_.setEnableLazyDecoding(true);
 
+  RETURN_IF_ERROR(PrepareSearchArguments());
+
   // To create OrcColumnReaders, we need the selected orc schema. It's a subset of the
   // file schema: a tree of selected orc types and can only be got from an orc::RowReader
   // (by orc::RowReader::getSelectedType).
@@ -290,6 +294,9 @@ void HdfsOrcScanner::Close(RowBatch* row_batch) {
     scratch_batch_->ReleaseResources(nullptr);
   }
   orc_root_batch_.reset(nullptr);
+  search_args_pool_->FreeAll();
+
+  ScalarExprEvaluator::Close(min_max_conjunct_evals_, state_);
 
   // Verify all resources (if any) have been transferred.
   DCHECK_EQ(template_tuple_pool_->total_allocated_bytes(), 0);
@@ -739,8 +746,7 @@ Status HdfsOrcScanner::NextStripe() {
       continue;
     }
 
-    // TODO: check if this stripe can be skipped by stats. e.g. IMPALA-6505 In that case,
-    // set the file row index in 'orc_root_reader_' accordingly.
+    // Set the file row index in 'orc_root_reader_' accordingly.
     if (first_invocation && acid_synthetic_rowid_ != nullptr) {
       orc_root_reader_->SetFileRowIndex(skipped_rows);
     }
@@ -906,4 +912,239 @@ Status HdfsOrcScanner::AssembleCollection(
   coll_items_read_counter_ += tuple_idx;
   return Status::OK();
 }
+
+/// T is the intended ORC primitive type and U is Impala internal primitive type.
+template<typename T, typename U>
+orc::Literal HdfsOrcScanner::GetOrcPrimitiveLiteral(
+    orc::PredicateDataType predicate_type, void* val) {
+  if (UNLIKELY(!val)) return orc::Literal(predicate_type);
+  T* val_dst = reinterpret_cast<T*>(val);
+  return orc::Literal(static_cast<U>(*val_dst));
+}
+
+orc::PredicateDataType HdfsOrcScanner::GetOrcPredicateDataType(const ColumnType& type) {
+  switch (type.type) {
+    case TYPE_BOOLEAN:
+      return orc::PredicateDataType::BOOLEAN;
+    case TYPE_TINYINT:
+    case TYPE_SMALLINT:
+    case TYPE_INT:
+    case TYPE_BIGINT:
+      return orc::PredicateDataType::LONG;
+    case TYPE_FLOAT:
+    case TYPE_DOUBLE:
+      return orc::PredicateDataType::FLOAT;
+    case TYPE_TIMESTAMP:
+      return orc::PredicateDataType::TIMESTAMP;
+    case TYPE_DATE:
+      return orc::PredicateDataType::DATE;
+    case TYPE_STRING:
+    case TYPE_VARCHAR:
+    case TYPE_CHAR:
+    case TYPE_FIXED_UDA_INTERMEDIATE:
+      return orc::PredicateDataType::STRING;
+    case TYPE_DECIMAL:
+      return orc::PredicateDataType::DECIMAL;
+    default:
+      DCHECK(false) << "Unsupported type: " << type.DebugString();
+      return orc::PredicateDataType::LONG;
+  }
+}
+
+orc::Literal HdfsOrcScanner::GetSearchArgumentLiteral(ScalarExprEvaluator* eval,
+    const ColumnType& dst_type, orc::PredicateDataType* predicate_type) {
+  DCHECK(eval->root().GetNumChildren() == 2);
+  ScalarExpr* literal_expr = eval->root().GetChild(1);
+  const ColumnType& type = literal_expr->type();
+  DCHECK(literal_expr->IsLiteral());
+  *predicate_type = GetOrcPredicateDataType(type);
+  // Since we want to get a literal value, the second parameter below is not used.
+  void* val = eval->GetValue(*literal_expr, nullptr);
+
+  switch (type.type) {
+    case TYPE_BOOLEAN:
+      return GetOrcPrimitiveLiteral<bool, bool>(*predicate_type, val);
+    case TYPE_TINYINT:
+      return GetOrcPrimitiveLiteral<int8_t, int64_t>(*predicate_type, val);
+    case TYPE_SMALLINT:
+      return GetOrcPrimitiveLiteral<int16_t, int64_t>(*predicate_type, val);
+    case TYPE_INT:
+      return GetOrcPrimitiveLiteral<int32_t, int64_t>(*predicate_type, val);
+    case TYPE_BIGINT:
+      return GetOrcPrimitiveLiteral<int64_t, int64_t>(*predicate_type, val);
+    case TYPE_FLOAT:
+      return GetOrcPrimitiveLiteral<float, double>(*predicate_type, val);
+    case TYPE_DOUBLE:
+      return GetOrcPrimitiveLiteral<double, double>(*predicate_type, val);
+    // Predicates on Timestamp are currently skipped in FE. We will focus on them in
+    // IMPALA-10915.
+    case TYPE_TIMESTAMP: {
+      DCHECK(false) << "Timestamp predicate is not supported: IMPALA-10915";
+      return orc::Literal(predicate_type);
+    }
+    case TYPE_DATE: {
+      if (UNLIKELY(!val)) return orc::Literal(predicate_type);
+      const DateValue* dv = reinterpret_cast<const DateValue*>(val);
+      int32_t value = 0;
+      // The date should be valid at this point.
+      DCHECK(dv->ToDaysSinceEpoch(&value));
+      return orc::Literal(*predicate_type, value);
+    }
+    case TYPE_STRING: {
+      if (UNLIKELY(!val)) return orc::Literal(predicate_type);
+      const StringValue* sv = reinterpret_cast<StringValue*>(val);
+      return orc::Literal(sv->ptr, sv->len);
+    }
+    // Predicates on CHAR/VARCHAR are currently skipped in FE. We will focus on them in
+    // IMPALA-10882.
+    case TYPE_VARCHAR: {
+      DCHECK(false) << "Varchar predicate is not supported: IMPALA-10882";
+      return orc::Literal(predicate_type);
+    }
+    case TYPE_CHAR: {
+      DCHECK(false) << "Char predicate is not supported: IMPALA-10882";
+      if (UNLIKELY(!val)) return orc::Literal(predicate_type);
+      const StringValue* sv = reinterpret_cast<StringValue*>(val);
+      char* dst_ptr;
+      if (dst_type.len > sv->len) {
+        dst_ptr = reinterpret_cast<char*>(search_args_pool_->TryAllocate(dst_type.len));
+        if (dst_ptr == nullptr) return orc::Literal(predicate_type);
+        memcpy(dst_ptr, sv->ptr, sv->len);
+        StringValue::PadWithSpaces(dst_ptr, dst_type.len, sv->len);
+      } else {
+        dst_ptr = sv->ptr;
+      }
+      return orc::Literal(dst_ptr, sv->len);
+    }
+    case TYPE_DECIMAL: {
+      if (!val) return orc::Literal(predicate_type);
+      orc::Int128 value;
+      switch (type.GetByteSize()) {
+        case 4: {
+          Decimal4Value* dv4 = reinterpret_cast<Decimal4Value*>(val);
+          value = orc::Int128(dv4->value());
+          break;
+        }
+        case 8: {
+          Decimal8Value* dv8 = reinterpret_cast<Decimal8Value*>(val);
+          value = orc::Int128(dv8->value());
+          break;
+        }
+        case 16: {
+          Decimal16Value* dv16 = reinterpret_cast<Decimal16Value*>(val);
+          value = orc::Int128(static_cast<int64_t>(dv16->value() >> 64), // higher bits
+              static_cast<uint64_t>(dv16->value())); // lower bits
+          break;
+        }
+        default:
+          DCHECK(false) << "Invalid byte size for decimal type: " << type.GetByteSize();
+      }
+      return orc::Literal(value, type.precision, type.scale);
+    }
+    default:
+      DCHECK(false) << "Invalid type";
+      return orc::Literal(orc::PredicateDataType::BOOLEAN);
+  }
+}
+
+Status HdfsOrcScanner::PrepareSearchArguments() {
+  if (!state_->query_options().orc_read_statistics) return Status::OK();
+
+  const TupleDescriptor* min_max_tuple_desc = scan_node_->min_max_tuple_desc();
+  if (!min_max_tuple_desc) return Status::OK();
+  // TODO(IMPALA-10894): pushing down predicates into the ORC reader will mess up the
+  //  synthetic(fake) row id, because the row index in the returned batch might differ
+  //  from the index in file (due to some rows are skipped).
+  if (acid_synthetic_rowid_ != nullptr) {
+    VLOG_FILE << "Skip pushing down predicates on non-ACID ORC files under an ACID "
+                 "table: " << filename();
+    return Status::OK();
+  }
+
+  // Clone the min/max statistics conjuncts.
+  RETURN_IF_ERROR(ScalarExprEvaluator::Clone(&obj_pool_, state_,
+      expr_perm_pool_.get(), context_->expr_results_pool(),
+      scan_node_->min_max_conjunct_evals(), &min_max_conjunct_evals_));
+
+  std::unique_ptr<orc::SearchArgumentBuilder> sarg =
+      orc::SearchArgumentFactory::newBuilder();
+  bool sargs_supported = false;
+
+  DCHECK_EQ(min_max_tuple_desc->slots().size(), min_max_conjunct_evals_.size());
+  for (int i = 0; i < min_max_conjunct_evals_.size(); ++i) {
+    SlotDescriptor* slot_desc = min_max_tuple_desc->slots()[i];
+    ScalarExprEvaluator* eval = min_max_conjunct_evals_[i];
+    ScalarExpr* const_expr = eval->root().GetChild(1);
+    // ORC reader only supports pushing down predicates that constant parts are literal.
+    // We could get non-literal expr if expr rewrites are disabled.
+    if (!const_expr->IsLiteral()) continue;
+    // TODO(IMPALA-10882): push down min-max predicates on CHAR/VARCHAR.
+    if (const_expr->type().type == TYPE_CHAR || const_expr->type().type == TYPE_VARCHAR
+        || slot_desc->type().type == TYPE_CHAR
+        || slot_desc->type().type == TYPE_VARCHAR) {
+      continue;
+    }
+    // TODO(IMPALA-10916): dealing with lhs that is a simple cast expr.
+    if (GetOrcPredicateDataType(slot_desc->type()) !=
+        GetOrcPredicateDataType(const_expr->type())) {
+      continue;
+    }
+
+    //Resolve column path to determine col idx.
+    const orc::Type* node = nullptr;
+    bool pos_field;
+    bool missing_field;
+    RETURN_IF_ERROR(schema_resolver_->ResolveColumn(slot_desc->col_path(),
+       &node, &pos_field, &missing_field));
+
+    if (pos_field || missing_field) continue;
+    // TODO(IMPALA-10882): push down min-max predicates on CHAR/VARCHAR.
+    if (node->getKind() == orc::CHAR || node->getKind() == orc::VARCHAR) continue;
+
+    const string& fn_name = eval->root().function_name();
+    orc::PredicateDataType predicate_type;
+    orc::Literal literal =
+        GetSearchArgumentLiteral(eval, slot_desc->type(), &predicate_type);
+    if (literal.isNull()) {
+      VLOG_QUERY << "Failed to push down predicate " << eval->root().DebugString();
+      continue;
+    }
+
+    if (fn_name == "lt") {
+      sarg->lessThan(node->getColumnId(), predicate_type, literal);
+    } else if (fn_name == "gt") {
+      sarg->startNot()
+          .lessThanEquals(node->getColumnId(), predicate_type, literal)
+          .end();
+    } else if (fn_name == "le") {
+      sarg->lessThanEquals(node->getColumnId(), predicate_type, literal);
+    } else if (fn_name == "ge") {
+      sarg->startNot()
+          .lessThan(node->getColumnId(), predicate_type, literal)
+          .end();
+    } else {
+      DCHECK(false) << "Invalid predicate: " << fn_name;
+      continue;
+    }
+    // If we have reached this far, we have a valid search arg that we can build later.
+    sargs_supported = true;
+  }
+  if (sargs_supported) {
+    try {
+      std::unique_ptr<orc::SearchArgument> final_sarg = sarg->build();
+      VLOG_FILE << "Built search arguments for ORC file: " << filename() << ": "
+          << final_sarg->toString() << ". File schema: " << reader_->getType().toString();
+      row_reader_options_.searchArgument(std::move(final_sarg));
+    } catch (std::exception& e) {
+      string msg = Substitute("Encountered parse error during building search arguments "
+          "in ORC file $0: $1", filename(), e.what());
+      parse_status_ = Status(msg);
+      return parse_status_;
+    }
+  }
+  // Free any expr result allocations accumulated during conjunct evaluation.
+  context_->expr_results_pool()->Clear();
+  return Status::OK();
+}
+
 }
diff --git a/be/src/exec/hdfs-orc-scanner.h b/be/src/exec/hdfs-orc-scanner.h
index 3af37de..6f2aacf 100644
--- a/be/src/exec/hdfs-orc-scanner.h
+++ b/be/src/exec/hdfs-orc-scanner.h
@@ -177,6 +177,12 @@ class HdfsOrcScanner : public HdfsColumnarScanner {
   /// Pool to copy non-dictionary buffer into. This pool is responsible for handling
   /// vector batches that do not necessarily fit into one row batch.
   boost::scoped_ptr<MemPool> data_batch_pool_;
+  /// Pool to copy values into when building search arguments. Freed on Close().
+  boost::scoped_ptr<MemPool> search_args_pool_;
+
+  /// Clone of Min/max statistics conjunct evaluators. Has the same lifetime as
+  /// the scanner. Stored in 'obj_pool_'.
+  vector<ScalarExprEvaluator*> min_max_conjunct_evals_;
 
   std::unique_ptr<OrcSchemaResolver> schema_resolver_ = nullptr;
 
@@ -311,6 +317,26 @@ class HdfsOrcScanner : public HdfsColumnarScanner {
 
   void SetSyntheticAcidFieldForOriginalFile(const SlotDescriptor* slot_desc,
       Tuple* template_tuple);
+
+  /// Clones the min/max conjucts into min_max_conjunct_evals_, then builds ORC search
+  /// arguments from the conjuncts. The search arguments will exist for the lifespan of
+  /// the scanner and need not to be updated.
+  Status PrepareSearchArguments() WARN_UNUSED_RESULT;
+
+  /// Helper function for GetLiteralSearchArguments. The template parameter T is the
+  /// type of val, and U is the destination type that the constructor of orc::Literal
+  /// accepts. The conversion is required here, since otherwise multiple implicit
+  /// conversions would be possible.
+  template<typename T, typename U>
+  orc::Literal GetOrcPrimitiveLiteral(orc::PredicateDataType predicate_type, void* val);
+
+  /// Helper function for mapping ColumnType to orc::PredicateDataType.
+  static orc::PredicateDataType GetOrcPredicateDataType(const ColumnType& type);
+
+  /// Returns the literal from the min/max conjucts, with the assumption that the
+  /// evaluator has exactly two children, where the second is a literal.
+  orc::Literal GetSearchArgumentLiteral(ScalarExprEvaluator* eval,
+      const ColumnType& dst_type, orc::PredicateDataType* predicate_type);
 };
 
 } // namespace impala
diff --git a/be/src/exprs/scalar-expr.h b/be/src/exprs/scalar-expr.h
index 341a467..a148cc5 100644
--- a/be/src/exprs/scalar-expr.h
+++ b/be/src/exprs/scalar-expr.h
@@ -235,6 +235,9 @@ class ScalarExpr : public Expr {
   friend class ScalarFnCall;
   friend class SlotRef;
 
+  // The ORC scanner builds search arguments based on the values it gets from Literal.
+  friend class HdfsOrcScanner;
+
   /// For BE tests
   friend class ExprTest;
   friend class ExprCodegenTest;
diff --git a/be/src/service/query-options.cc b/be/src/service/query-options.cc
index 4e431a2..db34c66 100644
--- a/be/src/service/query-options.cc
+++ b/be/src/service/query-options.cc
@@ -525,6 +525,10 @@ Status impala::SetQueryOption(const string& key, const string& value,
         query_options->__set_parquet_read_statistics(IsTrue(value));
         break;
       }
+      case TImpalaQueryOptions::ORC_READ_STATISTICS: {
+        query_options->__set_orc_read_statistics(IsTrue(value));
+        break;
+      }
       case TImpalaQueryOptions::DEFAULT_JOIN_DISTRIBUTION_MODE: {
         TJoinDistributionMode::type enum_type;
         RETURN_IF_ERROR(GetThriftEnum(value, "default join distribution mode",
diff --git a/be/src/service/query-options.h b/be/src/service/query-options.h
index 65113cb..fb1b670 100644
--- a/be/src/service/query-options.h
+++ b/be/src/service/query-options.h
@@ -47,7 +47,7 @@ typedef std::unordered_map<string, beeswax::TQueryOptionLevel::type>
 // time we add or remove a query option to/from the enum TImpalaQueryOptions.
 #define QUERY_OPTS_TABLE\
   DCHECK_EQ(_TImpalaQueryOptions_VALUES_TO_NAMES.size(),\
-      TImpalaQueryOptions::PARQUET_BLOOM_FILTER_WRITE + 1);\
+      TImpalaQueryOptions::ORC_READ_STATISTICS+ 1);\
   REMOVED_QUERY_OPT_FN(abort_on_default_limit_exceeded, ABORT_ON_DEFAULT_LIMIT_EXCEEDED)\
   QUERY_OPT_FN(abort_on_error, ABORT_ON_ERROR, TQueryOptionLevel::REGULAR)\
   REMOVED_QUERY_OPT_FN(allow_unsupported_formats, ALLOW_UNSUPPORTED_FORMATS)\
@@ -259,6 +259,8 @@ typedef std::unordered_map<string, beeswax::TQueryOptionLevel::type>
       TQueryOptionLevel::ADVANCED)\
   QUERY_OPT_FN(parquet_bloom_filter_write, PARQUET_BLOOM_FILTER_WRITE,\
       TQueryOptionLevel::ADVANCED)\
+  QUERY_OPT_FN(orc_read_statistics, ORC_READ_STATISTICS,\
+      TQueryOptionLevel::ADVANCED)
   ;
 
 /// Enforce practical limits on some query options to avoid undesired query state.
diff --git a/bin/impala-config.sh b/bin/impala-config.sh
index de3f037..a5d666a 100755
--- a/bin/impala-config.sh
+++ b/bin/impala-config.sh
@@ -68,7 +68,7 @@ fi
 # moving to a different build of the toolchain, e.g. when a version is bumped or a
 # compile option is changed. The build id can be found in the output of the toolchain
 # build jobs, it is constructed from the build number and toolchain git hash prefix.
-export IMPALA_TOOLCHAIN_BUILD_ID=21-dd7509fc38
+export IMPALA_TOOLCHAIN_BUILD_ID=32-edf8115953
 # Versions of toolchain dependencies.
 # -----------------------------------
 export IMPALA_AVRO_VERSION=1.7.4-p5
@@ -134,7 +134,7 @@ export IMPALA_OPENLDAP_VERSION=2.4.47
 unset IMPALA_OPENLDAP_URL
 export IMPALA_OPENSSL_VERSION=1.0.2l
 unset IMPALA_OPENSSL_URL
-export IMPALA_ORC_VERSION=1.6.2-p11
+export IMPALA_ORC_VERSION=2667f2996b75e879e52365edfd06b05da4eda941
 unset IMPALA_ORC_URL
 export IMPALA_PROTOBUF_VERSION=3.5.1
 unset IMPALA_PROTOBUF_URL
diff --git a/common/thrift/ImpalaService.thrift b/common/thrift/ImpalaService.thrift
index ad0d1fc..4d560d3 100644
--- a/common/thrift/ImpalaService.thrift
+++ b/common/thrift/ImpalaService.thrift
@@ -693,6 +693,9 @@ enum TImpalaQueryOptions {
   //     ALWAYS     - always write Parquet Bloom filters, even if the row group is fully
   //                  dictionary encoded
   PARQUET_BLOOM_FILTER_WRITE = 134
+
+  // Indicates whether to use ORC's search argument to push down predicates.
+  ORC_READ_STATISTICS = 135
 }
 
 // The summary of a DML statement.
diff --git a/common/thrift/Query.thrift b/common/thrift/Query.thrift
index f5c3cd0..849944d 100644
--- a/common/thrift/Query.thrift
+++ b/common/thrift/Query.thrift
@@ -546,6 +546,9 @@ struct TQueryOptions {
   // See comment in ImpalaService.thrift
   135: optional TParquetBloomFilterWrite parquet_bloom_filter_write =
       TParquetBloomFilterWrite.IF_NO_DICT;
+
+  // Indicates whether to use ORC's search argument to push down predicates.
+  136: optional bool orc_read_statistics = true;
 }
 
 // Impala currently has three types of sessions: Beeswax, HiveServer2 and external
diff --git a/fe/src/main/java/org/apache/impala/planner/HdfsScanNode.java b/fe/src/main/java/org/apache/impala/planner/HdfsScanNode.java
index f8dcb68..aac9502 100644
--- a/fe/src/main/java/org/apache/impala/planner/HdfsScanNode.java
+++ b/fe/src/main/java/org/apache/impala/planner/HdfsScanNode.java
@@ -421,6 +421,14 @@ public class HdfsScanNode extends ScanNode {
       }
     }
 
+    if (fileFormats_.contains(HdfsFileFormat.ORC)) {
+      // Compute min-max conjuncts only if the ORC_READ_STATISTICS query option is
+      // set to true.
+      if (analyzer.getQueryOptions().orc_read_statistics) {
+        computeMinMaxTupleAndConjuncts(analyzer);
+      }
+    }
+
     if (canApplyCountStarOptimization(analyzer, fileFormats_)) {
       Preconditions.checkState(desc_.getPath().destTable() != null);
       Preconditions.checkState(collectionConjuncts_.isEmpty());
@@ -544,10 +552,11 @@ public class HdfsScanNode extends ScanNode {
     SlotRef slotRef = binaryPred.getChild(0).unwrapSlotRef(true);
     if (slotRef == null) return;
 
+    SlotDescriptor slotDesc = slotRef.getDesc();
     // This node is a table scan, so this must be a scanning slot.
-    Preconditions.checkState(slotRef.getDesc().isScanSlot());
+    Preconditions.checkState(slotDesc.isScanSlot());
     // Skip the slot ref if it refers to an array's "pos" field.
-    if (slotRef.getDesc().isArrayPosRef()) return;
+    if (slotDesc.isArrayPosRef()) return;
 
     Expr constExpr = binaryPred.getChild(1);
     // Only constant exprs can be evaluated against parquet::Statistics. This includes
@@ -555,11 +564,18 @@ public class HdfsScanNode extends ScanNode {
     if (!constExpr.isConstant()) return;
     if (Expr.IS_NULL_VALUE.apply(constExpr)) return;
 
+    // TODO(IMPALA-10882): Push down Min-Max predicates of CHAR/VARCHAR to ORC reader
+    // TODO(IMPALA-10915): Push down Min-Max predicates of TIMESTAMP to ORC reader
+    if (fileFormats_.contains(HdfsFileFormat.ORC) &&
+        (slotDesc.getType() == Type.CHAR || slotDesc.getType() == Type.VARCHAR ||
+            slotDesc.getType() == Type.TIMESTAMP)) {
+      return;
+    }
     if (BinaryPredicate.IS_RANGE_PREDICATE.apply(binaryPred)) {
-      addMinMaxOriginalConjunct(slotRef.getDesc().getParent(), binaryPred);
+      addMinMaxOriginalConjunct(slotDesc.getParent(), binaryPred);
       buildStatsPredicate(analyzer, slotRef, binaryPred, binaryPred.getOp());
     } else if (BinaryPredicate.IS_EQ_PREDICATE.apply(binaryPred)) {
-      addMinMaxOriginalConjunct(slotRef.getDesc().getParent(), binaryPred);
+      addMinMaxOriginalConjunct(slotDesc.getParent(), binaryPred);
       // TODO: this could be optimized for boolean columns.
       buildStatsPredicate(analyzer, slotRef, binaryPred, BinaryPredicate.Operator.LE);
       buildStatsPredicate(analyzer, slotRef, binaryPred, BinaryPredicate.Operator.GE);
@@ -570,10 +586,18 @@ public class HdfsScanNode extends ScanNode {
     // Retrieve the left side of the IN predicate. It must be a simple slot to proceed.
     SlotRef slotRef = inPred.getBoundSlot();
     if (slotRef == null) return;
+    SlotDescriptor slotDesc = slotRef.getDesc();
     // This node is a table scan, so this must be a scanning slot.
-    Preconditions.checkState(slotRef.getDesc().isScanSlot());
+    Preconditions.checkState(slotDesc.isScanSlot());
     // Skip the slot ref if it refers to an array's "pos" field.
-    if (slotRef.getDesc().isArrayPosRef()) return;
+    if (slotDesc.isArrayPosRef()) return;
+    // TODO(IMPALA-10882): Push down Min-Max predicates of CHAR/VARCHAR to ORC reader
+    // TODO(IMPALA-10915): Push down Min-Max predicates of TIMESTAMP to ORC reader
+    if (fileFormats_.contains(HdfsFileFormat.ORC) &&
+        (slotDesc.getType() == Type.CHAR || slotDesc.getType() == Type.VARCHAR ||
+            slotDesc.getType() == Type.TIMESTAMP)) {
+      return;
+    }
     if (inPred.isNotIn()) return;
 
     List<Expr> children = inPred.getChildren();
@@ -599,7 +623,7 @@ public class HdfsScanNode extends ScanNode {
     BinaryPredicate maxBound = new BinaryPredicate(BinaryPredicate.Operator.LE,
         children.get(0).clone(), max.clone());
 
-    addMinMaxOriginalConjunct(slotRef.getDesc().getParent(), inPred);
+    addMinMaxOriginalConjunct(slotDesc.getParent(), inPred);
     buildStatsPredicate(analyzer, slotRef, minBound, minBound.getOp());
     buildStatsPredicate(analyzer, slotRef, maxBound, maxBound.getOp());
   }
@@ -1830,13 +1854,19 @@ public class HdfsScanNode extends ScanNode {
         minMaxOriginalConjuncts_.entrySet()) {
       TupleDescriptor tupleDesc = entry.getKey();
       List<Expr> exprs = entry.getValue();
+      String fileFormatStr;
+      if (hasParquet(fileFormats_) && fileFormats_.contains(HdfsFileFormat.ORC)) {
+        fileFormatStr = "parquet/orc";
+      } else {
+        fileFormatStr = hasParquet(fileFormats_) ? "parquet" : "orc";
+      }
       if (tupleDesc == getTupleDesc()) {
         output.append(prefix)
-        .append(String.format("parquet statistics predicates: %s\n",
+        .append(String.format("%s statistics predicates: %s\n", fileFormatStr,
             Expr.getExplainString(exprs, detailLevel)));
       } else {
         output.append(prefix)
-        .append(String.format("parquet statistics predicates on %s: %s\n",
+        .append(String.format("%s statistics predicates on %s: %s\n", fileFormatStr,
             tupleDesc.getAlias(), Expr.getExplainString(exprs, detailLevel)));
       }
     }
diff --git a/testdata/workloads/functional-planner/queries/PlannerTest/acid-scans.test b/testdata/workloads/functional-planner/queries/PlannerTest/acid-scans.test
index 4ed5b97..13bfb1f 100644
--- a/testdata/workloads/functional-planner/queries/PlannerTest/acid-scans.test
+++ b/testdata/workloads/functional-planner/queries/PlannerTest/acid-scans.test
@@ -296,7 +296,7 @@ PLAN-ROOT SINK
 |  |       columns: all
 |  |     extrapolated-rows=disabled max-scan-range-rows=unavailable
 |  |     mem-estimate=48.00MB mem-reservation=24.00KB thread-reservation=1
-|  |     tuple-ids=5 row-size=28B cardinality=419
+|  |     tuple-ids=7 row-size=28B cardinality=413
 |  |     in pipelines: 04(GETNEXT)
 |  |
 |  03:SCAN HDFS [functional_orc_def.alltypes_deleted_rows]
@@ -395,7 +395,7 @@ Per-Host Resources: mem-estimate=68.94MB mem-reservation=4.98MB thread-reservati
 |  |
 |  |--09:EXCHANGE [BROADCAST]
 |  |  |  mem-estimate=50.74KB mem-reservation=0B thread-reservation=0
-|  |  |  tuple-ids=5 row-size=28B cardinality=419
+|  |  |  tuple-ids=7 row-size=28B cardinality=413
 |  |  |  in pipelines: 04(GETNEXT)
 |  |  |
 |  |  F03:PLAN FRAGMENT [RANDOM] hosts=3 instances=3
@@ -408,7 +408,7 @@ Per-Host Resources: mem-estimate=68.94MB mem-reservation=4.98MB thread-reservati
 |  |       columns: all
 |  |     extrapolated-rows=disabled max-scan-range-rows=unavailable
 |  |     mem-estimate=48.00MB mem-reservation=24.00KB thread-reservation=1
-|  |     tuple-ids=5 row-size=28B cardinality=419
+|  |     tuple-ids=7 row-size=28B cardinality=413
 |  |     in pipelines: 04(GETNEXT)
 |  |
 |  03:SCAN HDFS [functional_orc_def.alltypes_deleted_rows, RANDOM]
@@ -495,7 +495,7 @@ Per-Host Resources: mem-estimate=212.88MB mem-reservation=6.89MB thread-reservat
   |  |     extrapolated-rows=disabled max-scan-range-rows=unavailable
   |  |     file formats: [ORC]
   |  |     mem-estimate=48.00MB mem-reservation=24.00KB thread-reservation=1
-  |  |     tuple-ids=3 row-size=28B cardinality=419
+  |  |     tuple-ids=5 row-size=28B cardinality=413
   |  |     in pipelines: 04(GETNEXT)
   |  |
   |  03:SCAN HDFS [functional_orc_def.alltypes_deleted_rows t2]
@@ -630,7 +630,7 @@ Per-Host Resources: mem-estimate=49.99MB mem-reservation=1.96MB thread-reservati
   |
   |--08:EXCHANGE [BROADCAST]
   |     mem-estimate=50.74KB mem-reservation=0B thread-reservation=0
-  |     tuple-ids=3 row-size=28B cardinality=419
+  |     tuple-ids=5 row-size=28B cardinality=413
   |     in pipelines: 04(GETNEXT)
   |
   03:SCAN HDFS [functional_orc_def.alltypes_deleted_rows t2, RANDOM]
@@ -658,7 +658,7 @@ Per-Host Resources: mem-estimate=48.00MB mem-reservation=24.00KB thread-reservat
      extrapolated-rows=disabled max-scan-range-rows=unavailable
      file formats: [ORC]
      mem-estimate=48.00MB mem-reservation=24.00KB thread-reservation=1
-     tuple-ids=3 row-size=28B cardinality=419
+     tuple-ids=5 row-size=28B cardinality=413
      in pipelines: 04(GETNEXT)
 ====
 # Do a join with itself, but the scanned partitions of 't2' don't have delete delta files.
diff --git a/testdata/workloads/functional-query/queries/QueryTest/orc-stats.test b/testdata/workloads/functional-query/queries/QueryTest/orc-stats.test
new file mode 100644
index 0000000..a53a7d8
--- /dev/null
+++ b/testdata/workloads/functional-query/queries/QueryTest/orc-stats.test
@@ -0,0 +1,542 @@
+====
+---- QUERY
+select id, bool_col from functional_orc_def.alltypessmall
+---- RUNTIME_PROFILE
+aggregation(SUM, RowsRead): 100
+====
+---- QUERY
+set explain_level=2;
+explain select count(*) from functional_orc_def.alltypessmall where tinyint_col = 10
+---- RESULTS: VERIFY_IS_SUBSET
+'   orc statistics predicates: tinyint_col = CAST(10 AS TINYINT)'
+====
+---- QUERY
+# Test on predicate x < min_val for tinyint. No rows should be read by the ORC reader.
+select count(*) from functional_orc_def.alltypessmall where tinyint_col < 0
+---- RESULTS
+0
+---- RUNTIME_PROFILE
+aggregation(SUM, RowsRead): 0
+====
+---- QUERY
+# Test on predicate x <= min_val for tinyint.
+select count(*) from functional_orc_def.alltypessmall where tinyint_col <= 0
+---- RESULTS
+12
+---- RUNTIME_PROFILE
+aggregation(SUM, RowsRead): 100
+====
+---- QUERY
+# Test on predicate x >= max_val for tinyint.
+select count(*) from functional_orc_def.alltypessmall where tinyint_col >= 9
+---- RESULTS
+8
+---- RUNTIME_PROFILE
+aggregation(SUM, RowsRead): 100
+====
+---- QUERY
+# Test on predicate x > max_val for tinyint. No rows should be read by the ORC reader.
+select count(*) from functional_orc_def.alltypessmall where tinyint_col > 9
+---- RESULTS
+0
+---- RUNTIME_PROFILE
+aggregation(SUM, RowsRead): 0
+====
+---- QUERY
+# Test EQ predicate with values outside the range.
+select count(*) from functional_orc_def.alltypessmall where tinyint_col = -1
+---- RESULTS
+0
+---- RUNTIME_PROFILE
+aggregation(SUM, RowsRead): 0
+====
+---- QUERY
+# Test EQ predicate with values outside the range.
+select count(*) from functional_orc_def.alltypessmall where tinyint_col = 10
+---- RESULTS
+0
+---- RUNTIME_PROFILE
+aggregation(SUM, RowsRead): 0
+====
+---- QUERY
+# Test on predicate x < min_val for smallint. No rows should be read by the ORC reader.
+select count(*) from functional_orc_def.alltypessmall where smallint_col < 0
+---- RESULTS
+0
+---- RUNTIME_PROFILE
+aggregation(SUM, RowsRead): 0
+====
+---- QUERY
+# Test on predicate x <= min_val for smallint.
+select count(*) from functional_orc_def.alltypessmall where smallint_col <= 0
+---- RESULTS
+12
+---- RUNTIME_PROFILE
+aggregation(SUM, RowsRead): 100
+====
+---- QUERY
+# Test on predicate x >= max_val for smallint.
+select count(*) from functional_orc_def.alltypessmall where smallint_col >= 9
+---- RESULTS
+8
+---- RUNTIME_PROFILE
+aggregation(SUM, RowsRead): 100
+====
+---- QUERY
+# Test on predicate x > max_val for smallint. No rows should be read by the ORC reader.
+select count(*) from functional_orc_def.alltypessmall where smallint_col > 9
+---- RESULTS
+0
+---- RUNTIME_PROFILE
+aggregation(SUM, RowsRead): 0
+====
+---- QUERY
+# Test EQ predicate with values outside the range.
+select count(*) from functional_orc_def.alltypessmall where smallint_col = -1
+---- RESULTS
+0
+---- RUNTIME_PROFILE
+aggregation(SUM, RowsRead): 0
+====
+---- QUERY
+# Test EQ predicate with values outside the range.
+select count(*) from functional_orc_def.alltypessmall where smallint_col = 10
+---- RESULTS
+0
+---- RUNTIME_PROFILE
+aggregation(SUM, RowsRead): 0
+====
+---- QUERY
+# Test on predicate x < min_val for int. No rows should be read by the ORC reader.
+select count(*) from functional_orc_def.alltypessmall where int_col < 0
+---- RESULTS
+0
+---- RUNTIME_PROFILE
+aggregation(SUM, RowsRead): 0
+====
+---- QUERY
+# Test on predicate x <= min_val for int.
+select count(*) from functional_orc_def.alltypessmall where int_col <= 0
+---- RESULTS
+12
+---- RUNTIME_PROFILE
+aggregation(SUM, RowsRead): 100
+====
+---- QUERY
+# Test on predicate x >= max_val for int.
+select count(*) from functional_orc_def.alltypessmall where int_col >= 9
+---- RESULTS
+8
+---- RUNTIME_PROFILE
+aggregation(SUM, RowsRead): 100
+====
+---- QUERY
+# Test on predicate x > max_val for int. No rows should be read by the ORC reader.
+select count(*) from functional_orc_def.alltypessmall where int_col > 9
+---- RESULTS
+0
+---- RUNTIME_PROFILE
+aggregation(SUM, RowsRead): 0
+====
+---- QUERY
+# Test EQ predicate with values outside the range.
+select count(*) from functional_orc_def.alltypessmall where int_col = -1
+---- RESULTS
+0
+---- RUNTIME_PROFILE
+aggregation(SUM, RowsRead): 0
+====
+---- QUERY
+# Test EQ predicate with values outside the range.
+select count(*) from functional_orc_def.alltypessmall where int_col = 10
+---- RESULTS
+0
+---- RUNTIME_PROFILE
+aggregation(SUM, RowsRead): 0
+====
+---- QUERY
+# Test on predicate x < min_val for bigint. No rows should be read by the ORC reader.
+select count(*) from functional_orc_def.alltypessmall where bigint_col < 0
+---- RESULTS
+0
+---- RUNTIME_PROFILE
+aggregation(SUM, RowsRead): 0
+====
+---- QUERY
+# Test on predicate x <= min_val for bigint.
+select count(*) from functional_orc_def.alltypessmall where bigint_col <= 0
+---- RESULTS
+12
+---- RUNTIME_PROFILE
+aggregation(SUM, RowsRead): 100
+====
+---- QUERY
+# Test on predicate x >= max_val for bigint.
+select count(*) from functional_orc_def.alltypessmall where bigint_col >= 90
+---- RESULTS
+8
+---- RUNTIME_PROFILE
+aggregation(SUM, RowsRead): 100
+====
+---- QUERY
+# Test on predicate x > max_val for bigint. No rows should be read by the ORC reader.
+select count(*) from functional_orc_def.alltypessmall where bigint_col > 90
+---- RESULTS
+0
+---- RUNTIME_PROFILE
+aggregation(SUM, RowsRead): 0
+====
+---- QUERY
+# Test EQ predicate with values outside the range.
+select count(*) from functional_orc_def.alltypessmall where bigint_col = -1
+---- RESULTS
+0
+---- RUNTIME_PROFILE
+aggregation(SUM, RowsRead): 0
+====
+---- QUERY
+# Test EQ predicate with values outside the range.
+select count(*) from functional_orc_def.alltypessmall where bigint_col = 100
+---- RESULTS
+0
+---- RUNTIME_PROFILE
+aggregation(SUM, RowsRead): 0
+====
+---- QUERY
+# Test on predicate x < min_val for float. No rows should be read by the ORC reader.
+select count(*) from functional_orc_def.alltypessmall where float_col < 0
+---- RESULTS
+0
+---- RUNTIME_PROFILE
+aggregation(SUM, RowsRead): 0
+====
+---- QUERY
+# Test on predicate x > max_val for float. No rows should be read by the ORC reader.
+select count(*) from functional_orc_def.alltypessmall where float_col > 9.9
+---- RESULTS
+0
+---- RUNTIME_PROFILE
+aggregation(SUM, RowsRead): 0
+====
+---- QUERY
+# Test on predicate x < min_val for double. No rows should be read by the ORC reader.
+select count(*) from functional_orc_def.alltypessmall where double_col < 0
+---- RESULTS
+0
+---- RUNTIME_PROFILE
+aggregation(SUM, RowsRead): 0
+====
+---- QUERY
+# Test on predicate x > max_val for double. No rows should be read by the ORC reader.
+select count(*) from functional_orc_def.alltypessmall where double_col > 99
+---- RESULTS
+0
+---- RUNTIME_PROFILE
+aggregation(SUM, RowsRead): 0
+====
+---- QUERY
+# Test on predicate x < min_val for string. No rows should be read by the ORC reader.
+select count(*) from functional_orc_def.alltypessmall where string_col < "0"
+---- RESULTS
+0
+---- RUNTIME_PROFILE
+aggregation(SUM, RowsRead): 0
+====
+---- QUERY
+# Test on predicate x <= min_val for string.
+select count(*) from functional_orc_def.alltypessmall where string_col <= "0"
+---- RESULTS
+12
+---- RUNTIME_PROFILE
+aggregation(SUM, RowsRead): 100
+====
+---- QUERY
+# Test on predicate x >= max_val for string.
+select count(*) from functional_orc_def.alltypessmall where string_col >= "9"
+---- RESULTS
+8
+---- RUNTIME_PROFILE
+aggregation(SUM, RowsRead): 100
+====
+---- QUERY
+# Test on predicate x > max_val for string. No rows should be read by the ORC reader.
+select count(*) from functional_orc_def.alltypessmall where string_col > "9"
+---- RESULTS
+0
+---- RUNTIME_PROFILE
+aggregation(SUM, RowsRead): 0
+====
+---- QUERY
+# Test on TIMESTAMP predicates. Currently they are not pushed down (IMPALA-10915).
+select count(*) from functional_orc_def.alltypessmall where timestamp_col < "2009-01-01 00:00:00"
+---- RESULTS
+0
+---- RUNTIME_PROFILE
+aggregation(SUM, RowsRead): 100
+====
+---- QUERY
+# Test on TIMESTAMP predicates. Currently they are not pushed down (IMPALA-10915).
+select count(*) from functional_orc_def.alltypessmall where timestamp_col > "2009-04-03 00:24:00.96"
+---- RESULTS
+0
+---- RUNTIME_PROFILE
+aggregation(SUM, RowsRead): 100
+====
+---- QUERY
+# Test on predicate x < min_val for decimal(9,0).
+# Due to ORC-517 not included in the current Hive version (3.1.3000.7.2.12.0-104),
+# the ORC files have wrong statistics on d1 column showing that its minimum is 0.
+# So we still see RowsRead=5 here.
+select count(*) from functional_orc_def.decimal_tbl where d1 < 1234
+---- RESULTS
+0
+---- RUNTIME_PROFILE
+aggregation(SUM, RowsRead): 5
+====
+---- QUERY
+select count(*) from functional_orc_def.decimal_tbl where d1 < 0
+---- RESULTS
+0
+---- RUNTIME_PROFILE
+aggregation(SUM, RowsRead): 0
+====
+---- QUERY
+# Test on predicate x > max_val for decimal(9,0). No rows should be read by the ORC reader.
+# Use 132842.0 instead of 132842 to workaround IMPALA-10916.
+select count(*) from functional_orc_def.decimal_tbl where d1 > 132842.0
+---- RESULTS
+0
+---- RUNTIME_PROFILE
+aggregation(SUM, RowsRead): 0
+====
+---- QUERY
+# Test on predicate x < min_val for decimal(20,10). No rows should be read by the ORC
+# reader.
+select count(*) from functional_orc_def.decimal_tbl where d3 < 1.23456789
+---- RESULTS
+0
+---- RUNTIME_PROFILE
+aggregation(SUM, RowsRead): 0
+====
+---- QUERY
+# Test on predicate x > max_val for decimal(20,10). No rows should be read by the ORC
+# reader.
+select count(*) from functional_orc_def.decimal_tbl where d3 > 12345.6789
+---- RESULTS
+0
+---- RUNTIME_PROFILE
+aggregation(SUM, RowsRead): 0
+====
+---- QUERY
+# Test on EQ predicate with a value out of the range.
+select count(*) from functional_orc_def.decimal_tbl where d3 = 1.23456788
+---- RESULTS
+0
+---- RUNTIME_PROFILE
+aggregation(SUM, RowsRead): 0
+====
+---- QUERY
+# Test on EQ predicate with a value out of the range.
+select count(*) from functional_orc_def.decimal_tbl where d3 = 12345.6799
+---- RESULTS
+0
+---- RUNTIME_PROFILE
+aggregation(SUM, RowsRead): 0
+====
+---- QUERY
+select count(*) from functional_orc_def.decimal_tbl where d4 > 0.123456789
+---- RESULTS
+0
+---- RUNTIME_PROFILE
+aggregation(SUM, RowsRead): 0
+====
+---- QUERY
+select count(*) from functional_orc_def.decimal_tbl where d4 < 0.123456789
+---- RESULTS
+0
+---- RUNTIME_PROFILE
+aggregation(SUM, RowsRead): 0
+====
+---- QUERY
+select count(*) from functional_orc_def.decimal_tbl where d4 >= 0.12345678
+---- RESULTS
+5
+---- RUNTIME_PROFILE
+aggregation(SUM, RowsRead): 5
+====
+---- QUERY
+select count(*) from functional_orc_def.decimal_tbl where d4 >= 0.12345679
+---- RESULTS
+0
+---- RUNTIME_PROFILE
+aggregation(SUM, RowsRead): 0
+====
+---- QUERY
+# Test with inverted predicate
+select id, bool_col from functional_orc_def.alltypessmall where -1 > int_col
+---- RESULTS
+---- RUNTIME_PROFILE
+aggregation(SUM, RowsRead): 0
+====
+---- QUERY
+# Mix with partitioning columns
+select count(*) from functional_orc_def.alltypessmall where int_col < 0 and year < 2012
+---- RESULTS
+0
+---- RUNTIME_PROFILE
+aggregation(SUM, RowsRead): 0
+====
+---- QUERY
+select id, bool_col from functional_orc_def.alltypessmall where int_col < 3 - 3
+---- RESULTS
+---- RUNTIME_PROFILE
+aggregation(SUM, RowsRead): 0
+====
+---- QUERY
+select id, bool_col from functional_orc_def.alltypessmall where int_col < 3 - 3
+---- RESULTS
+---- RUNTIME_PROFILE
+aggregation(SUM, RowsRead): 0
+====
+---- QUERY
+# Without expr rewrite and thus without constant folding, predicates with const exprs
+# won't be pushed down.
+set enable_expr_rewrites=0;
+select id, bool_col from functional_orc_def.alltypessmall where int_col < 3 - 3
+---- RESULTS
+---- RUNTIME_PROFILE
+aggregation(SUM, RowsRead): 100
+====
+---- QUERY
+select id, bool_col from functional_orc_def.alltypessmall where 5 + 5 < int_col
+---- RESULTS
+---- RUNTIME_PROFILE
+aggregation(SUM, RowsRead): 0
+====
+---- QUERY
+# Without expr rewrite and thus without constant folding, predicates with const exprs
+# won't be pushed down.
+set enable_expr_rewrites=0;
+select id, bool_col from functional_orc_def.alltypessmall where 5 + 5 < int_col
+---- RESULTS
+---- RUNTIME_PROFILE
+aggregation(SUM, RowsRead): 100
+====
+---- QUERY
+# Query that has an implicit cast to a larger integer type
+select count(*) from functional_orc_def.alltypessmall where tinyint_col > 1000000000000
+---- RESULTS
+0
+---- RUNTIME_PROFILE
+aggregation(SUM, RowsRead): 0
+====
+---- QUERY
+# Predicates with explicit casts are not supported when evaluating orc statistics.
+select count(*) from functional_orc_def.alltypessmall where '0' > cast(tinyint_col as string)
+---- RESULTS
+0
+---- RUNTIME_PROFILE
+aggregation(SUM, RowsRead): 100
+====
+---- QUERY
+# Explicit casts between numerical types can violate the transitivity of "min()", so they
+# are not supported when evaluating orc statistics.
+select count(*) from functional_orc_def.alltypes where cast(id as tinyint) < 10;
+---- RESULTS
+3878
+---- RUNTIME_PROFILE
+aggregation(SUM, RowsRead): 7300
+====
+---- QUERY
+# Predicates on array position can't be pushed down into the orc reader.
+select count(*) from functional_orc_def.complextypestbl.int_array where pos < 5;
+---- RESULTS
+9
+---- RUNTIME_PROFILE
+aggregation(SUM, RowsRead): 8
+====
+---- QUERY
+# Test the conversion of constant IN lists to min/max predicats
+select count(*) from functional_orc_def.alltypes where int_col in (-1,-2,-3,-4);
+---- RESULTS
+0
+---- RUNTIME_PROFILE
+aggregation(SUM, RowsRead): 0
+====
+---- QUERY
+select count(*) from functional_orc_def.alltypes where id IN (1,25,49);
+---- RESULTS
+3
+---- RUNTIME_PROFILE
+aggregation(SUM, RowsRead): 310
+====
+---- QUERY
+select count(*) from functional_orc_def.date_tbl
+where date_part in ("2017-11-27", "1399-06-27") and date_col < '0001-06-19';
+---- RESULTS
+0
+---- RUNTIME_PROFILE
+aggregation(SUM, RowsRead): 0
+====
+---- QUERY
+select count(*) from functional_orc_def.date_tbl
+where date_part in ("2017-11-27", "1399-06-27") and date_col > '2018-12-31';
+---- RESULTS
+0
+---- RUNTIME_PROFILE
+aggregation(SUM, RowsRead): 0
+====
+---- QUERY
+# Test predicates on CHAR type. They are not pushed down (IMPALA-10882). Just make sure
+# we don't hit DCHECKs.
+select count(*) from functional_orc_def.chars_tiny where cs < cast('1aaaa' as char(5));
+---- RESULTS
+0
+---- RUNTIME_PROFILE
+aggregation(SUM, RowsRead): 9
+====
+---- QUERY
+# Test predicates on CHAR type. They are not pushed down (IMPALA-10882). Just make sure
+# we don't hit DCHECKs.
+select count(*) from functional_orc_def.chars_tiny where cs > cast('a' as char(5));
+---- RESULTS
+0
+---- RUNTIME_PROFILE
+aggregation(SUM, RowsRead): 9
+====
+---- QUERY
+# Test predicates on VARCHAR type. They are not pushed down (IMPALA-10882). Just make sure
+# we don't hit DCHECKs.
+select count(*) from functional_orc_def.chars_tiny where vc < cast('1cccc' as varchar(32));
+---- RESULTS
+0
+---- RUNTIME_PROFILE
+aggregation(SUM, RowsRead): 9
+====
+---- QUERY
+# Test predicates on VARCHAR type. They are not pushed down (IMPALA-10882). Just make sure
+# we don't hit DCHECKs.
+select count(*) from functional_orc_def.chars_tiny where vc > cast('c' as varchar(32));
+---- RESULTS
+0
+---- RUNTIME_PROFILE
+aggregation(SUM, RowsRead): 9
+====
+---- QUERY
+# Test that stats support can be disabled using the orc_read_statistics query option.
+set orc_read_statistics=0;
+select count(*) from functional_orc_def.alltypes where id < 0;
+---- RESULTS
+0
+---- RUNTIME_PROFILE
+aggregation(SUM, RowsRead): 7300
+====
+---- QUERY
+# Test on a larger ORC file that has multiple stripes and each stripe has multiple row
+# groups.
+select count(*) from tpch_orc_def.lineitem where l_orderkey = 1609411;
+---- RESULTS
+7
+---- RUNTIME_PROFILE
+aggregation(SUM, RowsRead): 13501
+====
diff --git a/tests/query_test/test_nested_types.py b/tests/query_test/test_nested_types.py
index f00b697..f202843 100644
--- a/tests/query_test/test_nested_types.py
+++ b/tests/query_test/test_nested_types.py
@@ -236,8 +236,7 @@ class TestNestedTypesNoMtDop(ImpalaTestSuite):
   def test_parquet_stats(self, vector):
     """Queries that test evaluation of Parquet row group statistics."""
     if vector.get_value('table_format').file_format == 'orc':
-      pytest.skip('Predicate push down on ORC stripe statistics is not supported' +
-                  '(IMPALA-6505)')
+      pytest.skip('This test is specific to Parquet')
     self.run_test_case('QueryTest/nested-types-parquet-stats', vector)
 
   @SkipIfIsilon.hive
diff --git a/tests/query_test/test_orc_stats.py b/tests/query_test/test_orc_stats.py
new file mode 100644
index 0000000..25cb106
--- /dev/null
+++ b/tests/query_test/test_orc_stats.py
@@ -0,0 +1,40 @@
+# Licensed to the Apache Software Foundation (ASF) under one
+# or more contributor license agreements.  See the NOTICE file
+# distributed with this work for additional information
+# regarding copyright ownership.  The ASF licenses this file
+# to you under the Apache License, Version 2.0 (the
+# "License"); you may not use this file except in compliance
+# with the License.  You may obtain a copy of the License at
+#
+#   http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing,
+# software distributed under the License is distributed on an
+# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+# KIND, either express or implied.  See the License for the
+# specific language governing permissions and limitations
+# under the License.
+
+from tests.common.impala_test_suite import ImpalaTestSuite
+
+
+class TestOrcStats(ImpalaTestSuite):
+  """
+  This suite tests runtime optimizations based on ORC statistics.
+  """
+
+  @classmethod
+  def get_workload(cls):
+    return 'functional-query'
+
+  @classmethod
+  def add_test_dimensions(cls):
+    super(TestOrcStats, cls).add_test_dimensions()
+    cls.ImpalaTestMatrix.add_constraint(
+        lambda v: v.get_value('table_format').file_format == 'orc')
+
+  def test_orc_stats(self, vector, unique_database):
+    # The test makes assumptions about the number of rows read from the orc-reader
+    # inside a fragment, so we ensure that the tests run in a single fragment.
+    vector.get_value('exec_option')['num_nodes'] = 1
+    self.run_test_case('QueryTest/orc-stats', vector, use_db=unique_database)

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

Posted by st...@apache.org.
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