github-actions[bot] commented on code in PR #63143:
URL: https://github.com/apache/doris/pull/63143#discussion_r3271336774


##########
be/src/exprs/aggregate/aggregate_function_datasketches_hll_union_agg.h:
##########
@@ -0,0 +1,181 @@
+// 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.
+
+#pragma once
+#include <stddef.h>
+
+#include <DataSketches/hll.hpp>
+#include <boost/iterator/iterator_facade.hpp>
+#include <memory>
+#include <type_traits>
+#include <vector>
+
+#include "common/compiler_util.h" // IWYU pragma: keep
+#include "core/assert_cast.h"
+#include "core/column/column.h"
+#include "core/column/column_vector.h"
+#include "core/data_type/data_type_number.h"
+#include "core/data_type/define_primitive_type.h"
+#include "core/field.h"
+#include "core/string_ref.h"
+#include "core/types.h"
+#include "core/uint128.h"
+#include "exec/common/hash_table/hash.h"
+#include "exec/common/hash_table/phmap_fwd_decl.h"
+#include "exprs/aggregate/aggregate_function.h"
+#include "util/var_int.h"
+template <typename T>
+struct HashCRC32;
+namespace doris {
+class Arena;
+class BufferReadable;
+class BufferWritable;
+template <PrimitiveType T>
+class ColumnDecimal;
+/// datasketches_hll_union_agg
+template <PrimitiveType T>
+struct AggregateFunctionHllSketchData {
+    /** We set the default LgK to 12,
+      * as this value is used as a performance baseline in the relevant 
documentation.
+      * (https://datasketches.apache.org/docs/HLL/HllPerformance.html)
+      */
+    static const uint8_t DEFAULT_LOG_K = 12;
+    std::unique_ptr<datasketches::hll_union> hll_union_data;

Review Comment:
   This aggregate stores each group's DataSketches union in a `std::unique_ptr` 
constructed with the default allocator, so the union object and its internal 
HLL buffers are allocated on the process heap rather than through the aggregate 
`Arena`/Doris memory tracking. A query that groups many keys, especially when 
the input sketches use a large `lg_k`, can allocate substantial per-group 
memory without charging the query MemTracker, so memory limits may not trigger 
before process-level OOM. Please tie this state to Doris memory accounting, 
e.g. by using an allocator-aware DataSketches type or explicit MemTracker 
reservation/release for the union's owned memory.



##########
be/src/exprs/aggregate/aggregate_function_datasketches_hll_union_agg.h:
##########
@@ -0,0 +1,181 @@
+// 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.
+
+#pragma once
+#include <stddef.h>
+
+#include <DataSketches/hll.hpp>
+#include <boost/iterator/iterator_facade.hpp>
+#include <memory>
+#include <type_traits>
+#include <vector>
+
+#include "common/compiler_util.h" // IWYU pragma: keep
+#include "core/assert_cast.h"
+#include "core/column/column.h"
+#include "core/column/column_vector.h"
+#include "core/data_type/data_type_number.h"
+#include "core/data_type/define_primitive_type.h"
+#include "core/field.h"
+#include "core/string_ref.h"
+#include "core/types.h"
+#include "core/uint128.h"
+#include "exec/common/hash_table/hash.h"
+#include "exec/common/hash_table/phmap_fwd_decl.h"
+#include "exprs/aggregate/aggregate_function.h"
+#include "util/var_int.h"
+template <typename T>
+struct HashCRC32;
+namespace doris {
+class Arena;
+class BufferReadable;
+class BufferWritable;
+template <PrimitiveType T>
+class ColumnDecimal;
+/// datasketches_hll_union_agg
+template <PrimitiveType T>
+struct AggregateFunctionHllSketchData {
+    /** We set the default LgK to 12,
+      * as this value is used as a performance baseline in the relevant 
documentation.
+      * (https://datasketches.apache.org/docs/HLL/HllPerformance.html)
+      */
+    static const uint8_t DEFAULT_LOG_K = 12;
+    std::unique_ptr<datasketches::hll_union> hll_union_data;
+    static String get_name() { return "datasketches_hll_union_agg"; }
+    void merge(const datasketches::hll_sketch& sketch_data) {
+        if (hll_union_data == nullptr) {
+            hll_union_data =
+                    
std::make_unique<datasketches::hll_union>(sketch_data.get_lg_config_k());
+        }
+        try {
+            hll_union_data->update(sketch_data);
+        } catch (...) {
+            throw Exception(ErrorCode::INTERNAL_ERROR,
+                            "Internal error happened when update HLL sketch.");
+        }
+    }
+    void reset() {
+        if (hll_union_data != nullptr) {
+            hll_union_data->reset();
+        }
+        hll_union_data = nullptr;
+    }
+    void write_sketch(BufferWritable& buf, const datasketches::hll_sketch& sk) 
const {
+        auto serialized_bytes = sk.serialize_compact();
+        StringRef d(serialized_bytes.data(), serialized_bytes.size());
+        buf.write_binary(d);
+    }
+    void write(BufferWritable& buf) const {
+        if (hll_union_data == nullptr) {
+            /** Using DEFAULT_LOG_K(12) here is surely sufficient,
+              * because in this case the union that actually needs to be 
serialized should contain no data.
+              */
+            datasketches::hll_union u(DEFAULT_LOG_K);
+            write_sketch(buf, u.get_result());
+            return;
+        }
+        try {
+            auto cache = hll_union_data->get_result();
+            write_sketch(buf, cache);
+        } catch (...) {
+            throw Exception(ErrorCode::INTERNAL_ERROR,
+                            "Internal error happened when serialize HLL 
sketch.");
+        }
+    }
+    void read(BufferReadable& buf) {
+        StringRef d;
+        buf.read_binary(d);
+        try {
+            auto cache = datasketches::hll_sketch::deserialize(d.data, d.size);
+            merge(cache);
+        } catch (...) {
+            throw Exception(ErrorCode::CORRUPTION, "HLL sketch data corrupted 
when read.");
+        }
+    }
+    int64_t get_result() const {
+        if (hll_union_data != nullptr) {
+            try {
+                return static_cast<int64_t>(hll_union_data->get_estimate());
+            } catch (...) {
+                throw Exception(ErrorCode::INTERNAL_ERROR,
+                                "Internal error happened when get HLL sketch 
estimate.");
+            }
+        }
+        return 0;
+    }
+};
+namespace detail {
+/** The structure for the delegation work to add one element to the 
`datasketches_hll_union_agg` aggregate functions.
+  * Used for partial specialization to add strings.
+  */
+template <PrimitiveType T, typename Data>
+struct OneAdder {
+    static void ALWAYS_INLINE add(Data& data, const IColumn& column, size_t 
row_num) {
+        if constexpr (is_string_type(T) || is_varbinary(T)) {
+            StringRef value = column.get_data_at(row_num);
+            if (value.empty()) {

Review Comment:
   This silently drops zero-length strings before deserialization. `NULL` 
inputs are already skipped by the aggregate nullable handling, but an empty 
string is an actual input value and is not a valid serialized DataSketches HLL 
sketch. With this branch, a table containing `''` or a bad `from_base64()` 
result returns a cardinality computed from the remaining rows instead of 
failing with `CORRUPTION`, which hides corrupt data and undercounts. This is 
distinct from the earlier empty-state merge crash: the merge no longer 
dereferences null, but `add()` should still reject invalid empty serialized 
sketches (or explicitly document and test a valid empty-sketch encoding).



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to