be/src/exprs/function/function_json_hash.cpp
Line | Count | Source |
1 | | // Licensed to the Apache Software Foundation (ASF) under one |
2 | | // or more contributor license agreements. See the NOTICE file |
3 | | // distributed with this work for additional information |
4 | | // regarding copyright ownership. The ASF licenses this file |
5 | | // to you under the Apache License, Version 2.0 (the |
6 | | // "License"); you may not use this file except in compliance |
7 | | // with the License. You may obtain a copy of the License at |
8 | | // |
9 | | // http://www.apache.org/licenses/LICENSE-2.0 |
10 | | // |
11 | | // Unless required by applicable law or agreed to in writing, |
12 | | // software distributed under the License is distributed on an |
13 | | // "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
14 | | // KIND, either express or implied. See the License for the |
15 | | // specific language governing permissions and limitations |
16 | | // under the License. |
17 | | |
18 | | #include "core/data_type/data_type_number.h" |
19 | | #include "core/data_type/primitive_type.h" |
20 | | #include "exprs/function/simple_function_factory.h" |
21 | | #include "util/jsonb_document.h" |
22 | | |
23 | | namespace doris { |
24 | | |
25 | 137 | void json_hash(const JsonbValue* jsonb_value, size_t& hash_value) { |
26 | 176 | auto update_hash = [&hash_value](const void* data, size_t size) { |
27 | 176 | hash_value = HashUtil::hash64(data, (uint32_t)size, hash_value); |
28 | 176 | }; |
29 | | |
30 | 137 | if (jsonb_value->isObject()) { |
31 | 44 | hash_value ^= (size_t)JsonbType::T_Object; |
32 | 44 | const auto* obj_val = jsonb_value->unpack<ObjectVal>(); |
33 | 44 | const auto ordered = obj_val->get_ordered_key_value_pairs(); |
34 | 92 | for (const auto& [key, value] : ordered) { |
35 | 92 | update_hash(key.data, key.size); |
36 | 92 | json_hash(value, hash_value); |
37 | 92 | } |
38 | 93 | } else if (jsonb_value->isArray()) { |
39 | 9 | hash_value ^= (size_t)JsonbType::T_Array; |
40 | 9 | const auto* array_val = jsonb_value->unpack<ArrayVal>(); |
41 | 33 | for (auto it = array_val->begin(); it != array_val->end(); ++it) { |
42 | 24 | json_hash(&*it, hash_value); |
43 | 24 | } |
44 | 84 | } else { |
45 | | // Similar to the code below |
46 | | // bool writeValue(const JsonbValue* value) { |
47 | | // ... |
48 | | // os_->write((char*)value, value->numPackedBytes()); |
49 | | // ... |
50 | | // } |
51 | | // The hash value of the whole structure is directly calculated here, and the Type of Jsonb is included. |
52 | 84 | update_hash((const char*)jsonb_value, jsonb_value->numPackedBytes()); |
53 | 84 | } |
54 | 137 | } |
55 | | |
56 | | class FunctionJsonHash : public IFunction { |
57 | | public: |
58 | | static constexpr auto name = "json_hash"; |
59 | | |
60 | 21 | static FunctionPtr create() { return std::make_shared<FunctionJsonHash>(); } |
61 | | |
62 | 1 | String get_name() const override { return name; } |
63 | | |
64 | 12 | DataTypePtr get_return_type_impl(const DataTypes& arguments) const override { |
65 | 12 | return std::make_shared<DataTypeInt64>(); |
66 | 12 | } |
67 | | |
68 | 12 | size_t get_number_of_arguments() const override { return 1; } |
69 | | |
70 | | Status execute_impl(FunctionContext* context, Block& block, const ColumnNumbers& arguments, |
71 | 16 | uint32_t result, size_t size) const override { |
72 | 16 | auto input_column = block.get_by_position(arguments[0]).column; |
73 | 16 | auto to_column = ColumnInt64::create(size); |
74 | 16 | auto& to_column_data = to_column->get_data(); |
75 | | |
76 | 16 | const auto& input_jsonb_column = assert_cast<const ColumnString&>(*input_column); |
77 | | |
78 | 37 | for (size_t i = 0; i < size; ++i) { |
79 | 21 | StringRef val = input_jsonb_column.get_data_at(i); |
80 | 21 | const JsonbDocument* doc = nullptr; |
81 | 21 | auto st = JsonbDocument::checkAndCreateDocument(val.data, val.size, &doc); |
82 | 21 | if (!st.ok() || !doc || !doc->getValue()) [[unlikely]] { |
83 | | // mayby be invalid jsonb, just insert default |
84 | | // invalid jsonb value may be caused by the default null processing |
85 | 0 | continue; |
86 | 0 | } |
87 | | |
88 | 21 | size_t hash_value = 0; |
89 | 21 | json_hash(doc->getValue(), hash_value); |
90 | | |
91 | 21 | to_column_data[i] = static_cast<int64_t>(hash_value); |
92 | 21 | } |
93 | 16 | block.get_by_position(result).column = std::move(to_column); |
94 | 16 | return Status::OK(); |
95 | 16 | } |
96 | | }; |
97 | | |
98 | 8 | void register_function_json_hash(SimpleFunctionFactory& factory) { |
99 | 8 | factory.register_function<FunctionJsonHash>(); |
100 | | |
101 | 8 | factory.register_alias(FunctionJsonHash::name, "jsonb_hash"); |
102 | 8 | } |
103 | | |
104 | | } // namespace doris |