Coverage Report

Created: 2026-04-17 11:34

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
be/src/exprs/function/function_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
// This file is copied from
18
// https://github.com/ClickHouse/ClickHouse/blob/master/src/Functions/FunctionHash.cpp
19
// and modified by Doris
20
21
#include "exprs/function/function_hash.h"
22
23
#include "common/status.h"
24
#include "core/assert_cast.h"
25
#include "core/column/column.h"
26
#include "core/column/column_const.h"
27
#include "core/column/column_string.h"
28
#include "core/column/column_varbinary.h"
29
#include "core/column/column_vector.h"
30
#include "core/data_type/data_type.h"
31
#include "core/data_type/data_type_number.h"
32
#include "core/field.h"
33
#include "exec/common/template_helpers.hpp"
34
#include "exprs/function/function_helpers.h"
35
#include "exprs/function/function_variadic_arguments.h"
36
#include "exprs/function/simple_function_factory.h"
37
#include "util/hash/murmur_hash3.h"
38
#include "util/hash_util.hpp"
39
40
namespace doris {
41
constexpr uint64_t emtpy_value = 0xe28dbde7fe22e41c;
42
43
template <PrimitiveType ReturnType, bool is_mmh64_v2 = false>
44
struct MurmurHash3Impl {
45
0
    static constexpr auto get_name() {
46
0
        if constexpr (ReturnType == TYPE_INT) {
47
0
            return "murmur_hash3_32";
48
0
        } else if constexpr (ReturnType == TYPE_LARGEINT) {
49
0
            return "murmur_hash3_u64_v2";
50
0
        } else if constexpr (is_mmh64_v2) {
51
0
            return "murmur_hash3_64_v2";
52
0
        } else {
53
0
            return "murmur_hash3_64";
54
0
        }
55
0
    }
56
    static constexpr auto name = get_name();
57
58
0
    static Status empty_apply(IColumn& icolumn, size_t input_rows_count) {
59
0
        ColumnVector<ReturnType>& vec_to = assert_cast<ColumnVector<ReturnType>&>(icolumn);
60
0
        vec_to.get_data().assign(
61
0
                input_rows_count,
62
0
                static_cast<typename PrimitiveTypeTraits<ReturnType>::CppType>(emtpy_value));
63
0
        return Status::OK();
64
0
    }
Unexecuted instantiation: _ZN5doris15MurmurHash3ImplILNS_13PrimitiveTypeE5ELb0EE11empty_applyERNS_7IColumnEm
Unexecuted instantiation: _ZN5doris15MurmurHash3ImplILNS_13PrimitiveTypeE6ELb0EE11empty_applyERNS_7IColumnEm
Unexecuted instantiation: _ZN5doris15MurmurHash3ImplILNS_13PrimitiveTypeE6ELb1EE11empty_applyERNS_7IColumnEm
Unexecuted instantiation: _ZN5doris15MurmurHash3ImplILNS_13PrimitiveTypeE7ELb1EE11empty_applyERNS_7IColumnEm
65
66
    static Status first_apply(const IDataType* type, const IColumn* column, size_t input_rows_count,
67
7
                              IColumn& icolumn) {
68
7
        return execute<true>(type, column, input_rows_count, icolumn);
69
7
    }
_ZN5doris15MurmurHash3ImplILNS_13PrimitiveTypeE5ELb0EE11first_applyEPKNS_9IDataTypeEPKNS_7IColumnEmRS6_
Line
Count
Source
67
3
                              IColumn& icolumn) {
68
3
        return execute<true>(type, column, input_rows_count, icolumn);
69
3
    }
_ZN5doris15MurmurHash3ImplILNS_13PrimitiveTypeE6ELb0EE11first_applyEPKNS_9IDataTypeEPKNS_7IColumnEmRS6_
Line
Count
Source
67
3
                              IColumn& icolumn) {
68
3
        return execute<true>(type, column, input_rows_count, icolumn);
69
3
    }
_ZN5doris15MurmurHash3ImplILNS_13PrimitiveTypeE6ELb1EE11first_applyEPKNS_9IDataTypeEPKNS_7IColumnEmRS6_
Line
Count
Source
67
1
                              IColumn& icolumn) {
68
1
        return execute<true>(type, column, input_rows_count, icolumn);
69
1
    }
Unexecuted instantiation: _ZN5doris15MurmurHash3ImplILNS_13PrimitiveTypeE7ELb1EE11first_applyEPKNS_9IDataTypeEPKNS_7IColumnEmRS6_
70
71
    static Status combine_apply(const IDataType* type, const IColumn* column,
72
6
                                size_t input_rows_count, IColumn& icolumn) {
73
6
        return execute<false>(type, column, input_rows_count, icolumn);
74
6
    }
_ZN5doris15MurmurHash3ImplILNS_13PrimitiveTypeE5ELb0EE13combine_applyEPKNS_9IDataTypeEPKNS_7IColumnEmRS6_
Line
Count
Source
72
3
                                size_t input_rows_count, IColumn& icolumn) {
73
3
        return execute<false>(type, column, input_rows_count, icolumn);
74
3
    }
_ZN5doris15MurmurHash3ImplILNS_13PrimitiveTypeE6ELb0EE13combine_applyEPKNS_9IDataTypeEPKNS_7IColumnEmRS6_
Line
Count
Source
72
3
                                size_t input_rows_count, IColumn& icolumn) {
73
3
        return execute<false>(type, column, input_rows_count, icolumn);
74
3
    }
Unexecuted instantiation: _ZN5doris15MurmurHash3ImplILNS_13PrimitiveTypeE6ELb1EE13combine_applyEPKNS_9IDataTypeEPKNS_7IColumnEmRS6_
Unexecuted instantiation: _ZN5doris15MurmurHash3ImplILNS_13PrimitiveTypeE7ELb1EE13combine_applyEPKNS_9IDataTypeEPKNS_7IColumnEmRS6_
75
76
    template <bool first>
77
    static Status execute(const IDataType* type, const IColumn* column, size_t input_rows_count,
78
13
                          IColumn& col_to) {
79
13
        auto& to_column = assert_cast<ColumnVector<ReturnType>&>(col_to);
80
13
        if constexpr (first) {
81
7
            if constexpr (ReturnType == TYPE_INT) {
82
3
                to_column.insert_many_vals(static_cast<Int32>(HashUtil::MURMUR3_32_SEED),
83
3
                                           input_rows_count);
84
4
            } else {
85
4
                to_column.insert_many_defaults(input_rows_count);
86
4
            }
87
7
        }
88
13
        auto& col_to_data = to_column.get_data();
89
13
        if (const auto* col_from = check_and_get_column<ColumnString>(column)) {
90
13
            const typename ColumnString::Chars& data = col_from->get_chars();
91
13
            const typename ColumnString::Offsets& offsets = col_from->get_offsets();
92
13
            size_t size = offsets.size();
93
13
            ColumnString::Offset current_offset = 0;
94
40
            for (size_t i = 0; i < size; ++i) {
95
27
                if constexpr (ReturnType == TYPE_INT) {
96
12
                    col_to_data[i] = HashUtil::murmur_hash3_32(
97
12
                            reinterpret_cast<const char*>(&data[current_offset]),
98
12
                            offsets[i] - current_offset, col_to_data[i]);
99
15
                } else {
100
15
                    col_to_data[i] = HashUtil::murmur_hash3_64<is_mmh64_v2>(
101
15
                            reinterpret_cast<const char*>(&data[current_offset]),
102
15
                            offsets[i] - current_offset, static_cast<uint64_t>(col_to_data[i]));
103
15
                }
104
27
                current_offset = offsets[i];
105
27
            }
106
13
        } else if (const ColumnConst* col_from_const =
107
0
                           check_and_get_column_const_string_or_fixedstring(column)) {
108
0
            auto value = col_from_const->get_value<TYPE_STRING>();
109
0
            for (size_t i = 0; i < input_rows_count; ++i) {
110
0
                if constexpr (ReturnType == TYPE_INT) {
111
0
                    col_to_data[i] =
112
0
                            HashUtil::murmur_hash3_32(value.data(), value.size(), col_to_data[i]);
113
0
                } else {
114
0
                    col_to_data[i] = HashUtil::murmur_hash3_64<is_mmh64_v2>(
115
0
                            value.data(), value.size(), static_cast<uint64_t>(col_to_data[i]));
116
0
                }
117
0
            }
118
0
        } else {
119
0
            DCHECK(false);
120
0
            return Status::NotSupported("Illegal column {} of argument of function {}",
121
0
                                        column->get_name(), name);
122
0
        }
123
13
        return Status::OK();
124
13
    }
_ZN5doris15MurmurHash3ImplILNS_13PrimitiveTypeE5ELb0EE7executeILb1EEENS_6StatusEPKNS_9IDataTypeEPKNS_7IColumnEmRS8_
Line
Count
Source
78
3
                          IColumn& col_to) {
79
3
        auto& to_column = assert_cast<ColumnVector<ReturnType>&>(col_to);
80
3
        if constexpr (first) {
81
3
            if constexpr (ReturnType == TYPE_INT) {
82
3
                to_column.insert_many_vals(static_cast<Int32>(HashUtil::MURMUR3_32_SEED),
83
3
                                           input_rows_count);
84
            } else {
85
                to_column.insert_many_defaults(input_rows_count);
86
            }
87
3
        }
88
3
        auto& col_to_data = to_column.get_data();
89
3
        if (const auto* col_from = check_and_get_column<ColumnString>(column)) {
90
3
            const typename ColumnString::Chars& data = col_from->get_chars();
91
3
            const typename ColumnString::Offsets& offsets = col_from->get_offsets();
92
3
            size_t size = offsets.size();
93
3
            ColumnString::Offset current_offset = 0;
94
9
            for (size_t i = 0; i < size; ++i) {
95
6
                if constexpr (ReturnType == TYPE_INT) {
96
6
                    col_to_data[i] = HashUtil::murmur_hash3_32(
97
6
                            reinterpret_cast<const char*>(&data[current_offset]),
98
6
                            offsets[i] - current_offset, col_to_data[i]);
99
                } else {
100
                    col_to_data[i] = HashUtil::murmur_hash3_64<is_mmh64_v2>(
101
                            reinterpret_cast<const char*>(&data[current_offset]),
102
                            offsets[i] - current_offset, static_cast<uint64_t>(col_to_data[i]));
103
                }
104
6
                current_offset = offsets[i];
105
6
            }
106
3
        } else if (const ColumnConst* col_from_const =
107
0
                           check_and_get_column_const_string_or_fixedstring(column)) {
108
0
            auto value = col_from_const->get_value<TYPE_STRING>();
109
0
            for (size_t i = 0; i < input_rows_count; ++i) {
110
0
                if constexpr (ReturnType == TYPE_INT) {
111
0
                    col_to_data[i] =
112
0
                            HashUtil::murmur_hash3_32(value.data(), value.size(), col_to_data[i]);
113
                } else {
114
                    col_to_data[i] = HashUtil::murmur_hash3_64<is_mmh64_v2>(
115
                            value.data(), value.size(), static_cast<uint64_t>(col_to_data[i]));
116
                }
117
0
            }
118
0
        } else {
119
0
            DCHECK(false);
120
0
            return Status::NotSupported("Illegal column {} of argument of function {}",
121
0
                                        column->get_name(), name);
122
0
        }
123
3
        return Status::OK();
124
3
    }
_ZN5doris15MurmurHash3ImplILNS_13PrimitiveTypeE5ELb0EE7executeILb0EEENS_6StatusEPKNS_9IDataTypeEPKNS_7IColumnEmRS8_
Line
Count
Source
78
3
                          IColumn& col_to) {
79
3
        auto& to_column = assert_cast<ColumnVector<ReturnType>&>(col_to);
80
        if constexpr (first) {
81
            if constexpr (ReturnType == TYPE_INT) {
82
                to_column.insert_many_vals(static_cast<Int32>(HashUtil::MURMUR3_32_SEED),
83
                                           input_rows_count);
84
            } else {
85
                to_column.insert_many_defaults(input_rows_count);
86
            }
87
        }
88
3
        auto& col_to_data = to_column.get_data();
89
3
        if (const auto* col_from = check_and_get_column<ColumnString>(column)) {
90
3
            const typename ColumnString::Chars& data = col_from->get_chars();
91
3
            const typename ColumnString::Offsets& offsets = col_from->get_offsets();
92
3
            size_t size = offsets.size();
93
3
            ColumnString::Offset current_offset = 0;
94
9
            for (size_t i = 0; i < size; ++i) {
95
6
                if constexpr (ReturnType == TYPE_INT) {
96
6
                    col_to_data[i] = HashUtil::murmur_hash3_32(
97
6
                            reinterpret_cast<const char*>(&data[current_offset]),
98
6
                            offsets[i] - current_offset, col_to_data[i]);
99
                } else {
100
                    col_to_data[i] = HashUtil::murmur_hash3_64<is_mmh64_v2>(
101
                            reinterpret_cast<const char*>(&data[current_offset]),
102
                            offsets[i] - current_offset, static_cast<uint64_t>(col_to_data[i]));
103
                }
104
6
                current_offset = offsets[i];
105
6
            }
106
3
        } else if (const ColumnConst* col_from_const =
107
0
                           check_and_get_column_const_string_or_fixedstring(column)) {
108
0
            auto value = col_from_const->get_value<TYPE_STRING>();
109
0
            for (size_t i = 0; i < input_rows_count; ++i) {
110
0
                if constexpr (ReturnType == TYPE_INT) {
111
0
                    col_to_data[i] =
112
0
                            HashUtil::murmur_hash3_32(value.data(), value.size(), col_to_data[i]);
113
                } else {
114
                    col_to_data[i] = HashUtil::murmur_hash3_64<is_mmh64_v2>(
115
                            value.data(), value.size(), static_cast<uint64_t>(col_to_data[i]));
116
                }
117
0
            }
118
0
        } else {
119
0
            DCHECK(false);
120
0
            return Status::NotSupported("Illegal column {} of argument of function {}",
121
0
                                        column->get_name(), name);
122
0
        }
123
3
        return Status::OK();
124
3
    }
_ZN5doris15MurmurHash3ImplILNS_13PrimitiveTypeE6ELb0EE7executeILb1EEENS_6StatusEPKNS_9IDataTypeEPKNS_7IColumnEmRS8_
Line
Count
Source
78
3
                          IColumn& col_to) {
79
3
        auto& to_column = assert_cast<ColumnVector<ReturnType>&>(col_to);
80
3
        if constexpr (first) {
81
            if constexpr (ReturnType == TYPE_INT) {
82
                to_column.insert_many_vals(static_cast<Int32>(HashUtil::MURMUR3_32_SEED),
83
                                           input_rows_count);
84
3
            } else {
85
3
                to_column.insert_many_defaults(input_rows_count);
86
3
            }
87
3
        }
88
3
        auto& col_to_data = to_column.get_data();
89
3
        if (const auto* col_from = check_and_get_column<ColumnString>(column)) {
90
3
            const typename ColumnString::Chars& data = col_from->get_chars();
91
3
            const typename ColumnString::Offsets& offsets = col_from->get_offsets();
92
3
            size_t size = offsets.size();
93
3
            ColumnString::Offset current_offset = 0;
94
9
            for (size_t i = 0; i < size; ++i) {
95
                if constexpr (ReturnType == TYPE_INT) {
96
                    col_to_data[i] = HashUtil::murmur_hash3_32(
97
                            reinterpret_cast<const char*>(&data[current_offset]),
98
                            offsets[i] - current_offset, col_to_data[i]);
99
6
                } else {
100
6
                    col_to_data[i] = HashUtil::murmur_hash3_64<is_mmh64_v2>(
101
6
                            reinterpret_cast<const char*>(&data[current_offset]),
102
6
                            offsets[i] - current_offset, static_cast<uint64_t>(col_to_data[i]));
103
6
                }
104
6
                current_offset = offsets[i];
105
6
            }
106
3
        } else if (const ColumnConst* col_from_const =
107
0
                           check_and_get_column_const_string_or_fixedstring(column)) {
108
0
            auto value = col_from_const->get_value<TYPE_STRING>();
109
0
            for (size_t i = 0; i < input_rows_count; ++i) {
110
                if constexpr (ReturnType == TYPE_INT) {
111
                    col_to_data[i] =
112
                            HashUtil::murmur_hash3_32(value.data(), value.size(), col_to_data[i]);
113
0
                } else {
114
0
                    col_to_data[i] = HashUtil::murmur_hash3_64<is_mmh64_v2>(
115
0
                            value.data(), value.size(), static_cast<uint64_t>(col_to_data[i]));
116
0
                }
117
0
            }
118
0
        } else {
119
0
            DCHECK(false);
120
0
            return Status::NotSupported("Illegal column {} of argument of function {}",
121
0
                                        column->get_name(), name);
122
0
        }
123
3
        return Status::OK();
124
3
    }
_ZN5doris15MurmurHash3ImplILNS_13PrimitiveTypeE6ELb0EE7executeILb0EEENS_6StatusEPKNS_9IDataTypeEPKNS_7IColumnEmRS8_
Line
Count
Source
78
3
                          IColumn& col_to) {
79
3
        auto& to_column = assert_cast<ColumnVector<ReturnType>&>(col_to);
80
        if constexpr (first) {
81
            if constexpr (ReturnType == TYPE_INT) {
82
                to_column.insert_many_vals(static_cast<Int32>(HashUtil::MURMUR3_32_SEED),
83
                                           input_rows_count);
84
            } else {
85
                to_column.insert_many_defaults(input_rows_count);
86
            }
87
        }
88
3
        auto& col_to_data = to_column.get_data();
89
3
        if (const auto* col_from = check_and_get_column<ColumnString>(column)) {
90
3
            const typename ColumnString::Chars& data = col_from->get_chars();
91
3
            const typename ColumnString::Offsets& offsets = col_from->get_offsets();
92
3
            size_t size = offsets.size();
93
3
            ColumnString::Offset current_offset = 0;
94
9
            for (size_t i = 0; i < size; ++i) {
95
                if constexpr (ReturnType == TYPE_INT) {
96
                    col_to_data[i] = HashUtil::murmur_hash3_32(
97
                            reinterpret_cast<const char*>(&data[current_offset]),
98
                            offsets[i] - current_offset, col_to_data[i]);
99
6
                } else {
100
6
                    col_to_data[i] = HashUtil::murmur_hash3_64<is_mmh64_v2>(
101
6
                            reinterpret_cast<const char*>(&data[current_offset]),
102
6
                            offsets[i] - current_offset, static_cast<uint64_t>(col_to_data[i]));
103
6
                }
104
6
                current_offset = offsets[i];
105
6
            }
106
3
        } else if (const ColumnConst* col_from_const =
107
0
                           check_and_get_column_const_string_or_fixedstring(column)) {
108
0
            auto value = col_from_const->get_value<TYPE_STRING>();
109
0
            for (size_t i = 0; i < input_rows_count; ++i) {
110
                if constexpr (ReturnType == TYPE_INT) {
111
                    col_to_data[i] =
112
                            HashUtil::murmur_hash3_32(value.data(), value.size(), col_to_data[i]);
113
0
                } else {
114
0
                    col_to_data[i] = HashUtil::murmur_hash3_64<is_mmh64_v2>(
115
0
                            value.data(), value.size(), static_cast<uint64_t>(col_to_data[i]));
116
0
                }
117
0
            }
118
0
        } else {
119
0
            DCHECK(false);
120
0
            return Status::NotSupported("Illegal column {} of argument of function {}",
121
0
                                        column->get_name(), name);
122
0
        }
123
3
        return Status::OK();
124
3
    }
_ZN5doris15MurmurHash3ImplILNS_13PrimitiveTypeE6ELb1EE7executeILb1EEENS_6StatusEPKNS_9IDataTypeEPKNS_7IColumnEmRS8_
Line
Count
Source
78
1
                          IColumn& col_to) {
79
1
        auto& to_column = assert_cast<ColumnVector<ReturnType>&>(col_to);
80
1
        if constexpr (first) {
81
            if constexpr (ReturnType == TYPE_INT) {
82
                to_column.insert_many_vals(static_cast<Int32>(HashUtil::MURMUR3_32_SEED),
83
                                           input_rows_count);
84
1
            } else {
85
1
                to_column.insert_many_defaults(input_rows_count);
86
1
            }
87
1
        }
88
1
        auto& col_to_data = to_column.get_data();
89
1
        if (const auto* col_from = check_and_get_column<ColumnString>(column)) {
90
1
            const typename ColumnString::Chars& data = col_from->get_chars();
91
1
            const typename ColumnString::Offsets& offsets = col_from->get_offsets();
92
1
            size_t size = offsets.size();
93
1
            ColumnString::Offset current_offset = 0;
94
4
            for (size_t i = 0; i < size; ++i) {
95
                if constexpr (ReturnType == TYPE_INT) {
96
                    col_to_data[i] = HashUtil::murmur_hash3_32(
97
                            reinterpret_cast<const char*>(&data[current_offset]),
98
                            offsets[i] - current_offset, col_to_data[i]);
99
3
                } else {
100
3
                    col_to_data[i] = HashUtil::murmur_hash3_64<is_mmh64_v2>(
101
3
                            reinterpret_cast<const char*>(&data[current_offset]),
102
3
                            offsets[i] - current_offset, static_cast<uint64_t>(col_to_data[i]));
103
3
                }
104
3
                current_offset = offsets[i];
105
3
            }
106
1
        } else if (const ColumnConst* col_from_const =
107
0
                           check_and_get_column_const_string_or_fixedstring(column)) {
108
0
            auto value = col_from_const->get_value<TYPE_STRING>();
109
0
            for (size_t i = 0; i < input_rows_count; ++i) {
110
                if constexpr (ReturnType == TYPE_INT) {
111
                    col_to_data[i] =
112
                            HashUtil::murmur_hash3_32(value.data(), value.size(), col_to_data[i]);
113
0
                } else {
114
0
                    col_to_data[i] = HashUtil::murmur_hash3_64<is_mmh64_v2>(
115
0
                            value.data(), value.size(), static_cast<uint64_t>(col_to_data[i]));
116
0
                }
117
0
            }
118
0
        } else {
119
0
            DCHECK(false);
120
0
            return Status::NotSupported("Illegal column {} of argument of function {}",
121
0
                                        column->get_name(), name);
122
0
        }
123
1
        return Status::OK();
124
1
    }
Unexecuted instantiation: _ZN5doris15MurmurHash3ImplILNS_13PrimitiveTypeE6ELb1EE7executeILb0EEENS_6StatusEPKNS_9IDataTypeEPKNS_7IColumnEmRS8_
Unexecuted instantiation: _ZN5doris15MurmurHash3ImplILNS_13PrimitiveTypeE7ELb1EE7executeILb1EEENS_6StatusEPKNS_9IDataTypeEPKNS_7IColumnEmRS8_
Unexecuted instantiation: _ZN5doris15MurmurHash3ImplILNS_13PrimitiveTypeE7ELb1EE7executeILb0EEENS_6StatusEPKNS_9IDataTypeEPKNS_7IColumnEmRS8_
125
};
126
127
using FunctionMurmurHash3_32 =
128
        FunctionVariadicArgumentsBase<DataTypeInt32, MurmurHash3Impl<TYPE_INT>>;
129
using FunctionMurmurHash3_64 =
130
        FunctionVariadicArgumentsBase<DataTypeInt64, MurmurHash3Impl<TYPE_BIGINT>>;
131
using FunctionMurmurHash3_64_V2 =
132
        FunctionVariadicArgumentsBase<DataTypeInt64, MurmurHash3Impl<TYPE_BIGINT, true>>;
133
using FunctionMurmurHash3U64V2 =
134
        FunctionVariadicArgumentsBase<DataTypeInt128, MurmurHash3Impl<TYPE_LARGEINT, true>>;
135
136
#ifdef BE_TEST
137
const char* murmur_hash3_get_name_type_int_for_test() {
138
    return MurmurHash3Impl<TYPE_INT>::get_name();
139
}
140
141
const char* murmur_hash3_get_name_type_bigint_for_test() {
142
    return MurmurHash3Impl<TYPE_BIGINT>::get_name();
143
}
144
145
const char* murmur_hash3_get_name_type_bigint_v2_for_test() {
146
    return MurmurHash3Impl<TYPE_BIGINT, true>::get_name();
147
}
148
#endif
149
150
template <PrimitiveType ReturnType>
151
struct XxHashImpl {
152
    static constexpr auto name = ReturnType == TYPE_INT ? "xxhash_32" : "xxhash_64";
153
154
0
    static Status empty_apply(IColumn& icolumn, size_t input_rows_count) {
155
0
        ColumnVector<ReturnType>& vec_to = assert_cast<ColumnVector<ReturnType>&>(icolumn);
156
0
        vec_to.get_data().assign(
157
0
                input_rows_count,
158
0
                static_cast<typename PrimitiveTypeTraits<ReturnType>::CppType>(emtpy_value));
159
0
        return Status::OK();
160
0
    }
Unexecuted instantiation: _ZN5doris10XxHashImplILNS_13PrimitiveTypeE5EE11empty_applyERNS_7IColumnEm
Unexecuted instantiation: _ZN5doris10XxHashImplILNS_13PrimitiveTypeE6EE11empty_applyERNS_7IColumnEm
161
162
    static Status first_apply(const IDataType* type, const IColumn* column, size_t input_rows_count,
163
511
                              IColumn& icolumn) {
164
511
        return execute<true>(type, column, input_rows_count, icolumn);
165
511
    }
_ZN5doris10XxHashImplILNS_13PrimitiveTypeE5EE11first_applyEPKNS_9IDataTypeEPKNS_7IColumnEmRS6_
Line
Count
Source
163
10
                              IColumn& icolumn) {
164
10
        return execute<true>(type, column, input_rows_count, icolumn);
165
10
    }
_ZN5doris10XxHashImplILNS_13PrimitiveTypeE6EE11first_applyEPKNS_9IDataTypeEPKNS_7IColumnEmRS6_
Line
Count
Source
163
501
                              IColumn& icolumn) {
164
501
        return execute<true>(type, column, input_rows_count, icolumn);
165
501
    }
166
167
    static Status combine_apply(const IDataType* type, const IColumn* column,
168
16
                                size_t input_rows_count, IColumn& icolumn) {
169
16
        return execute<false>(type, column, input_rows_count, icolumn);
170
16
    }
_ZN5doris10XxHashImplILNS_13PrimitiveTypeE5EE13combine_applyEPKNS_9IDataTypeEPKNS_7IColumnEmRS6_
Line
Count
Source
168
8
                                size_t input_rows_count, IColumn& icolumn) {
169
8
        return execute<false>(type, column, input_rows_count, icolumn);
170
8
    }
_ZN5doris10XxHashImplILNS_13PrimitiveTypeE6EE13combine_applyEPKNS_9IDataTypeEPKNS_7IColumnEmRS6_
Line
Count
Source
168
8
                                size_t input_rows_count, IColumn& icolumn) {
169
8
        return execute<false>(type, column, input_rows_count, icolumn);
170
8
    }
171
172
    template <bool first>
173
    static Status execute(const IDataType* type, const IColumn* column, size_t input_rows_count,
174
527
                          IColumn& col_to) {
175
527
        auto& to_column = assert_cast<ColumnVector<ReturnType>&>(col_to);
176
527
        if constexpr (first) {
177
511
            to_column.insert_many_defaults(input_rows_count);
178
511
        }
179
527
        auto& col_to_data = to_column.get_data();
180
527
        if (const auto* col_from = check_and_get_column<ColumnString>(column)) {
181
509
            const typename ColumnString::Chars& data = col_from->get_chars();
182
509
            const typename ColumnString::Offsets& offsets = col_from->get_offsets();
183
509
            size_t size = offsets.size();
184
509
            ColumnString::Offset current_offset = 0;
185
38.7k
            for (size_t i = 0; i < size; ++i) {
186
38.2k
                if constexpr (ReturnType == TYPE_INT) {
187
27
                    col_to_data[i] = HashUtil::xxHash32WithSeed(
188
27
                            reinterpret_cast<const char*>(&data[current_offset]),
189
27
                            offsets[i] - current_offset, col_to_data[i]);
190
38.2k
                } else {
191
38.2k
                    col_to_data[i] = HashUtil::xxHash64WithSeed(
192
38.2k
                            reinterpret_cast<const char*>(&data[current_offset]),
193
38.2k
                            offsets[i] - current_offset, col_to_data[i]);
194
38.2k
                }
195
38.2k
                current_offset = offsets[i];
196
38.2k
            }
197
509
        } else if (const ColumnConst* col_from_const =
198
18
                           check_and_get_column_const_string_or_fixedstring(column)) {
199
0
            auto value = col_from_const->get_value<TYPE_STRING>();
200
0
            for (size_t i = 0; i < input_rows_count; ++i) {
201
0
                if constexpr (ReturnType == TYPE_INT) {
202
0
                    col_to_data[i] =
203
0
                            HashUtil::xxHash32WithSeed(value.data(), value.size(), col_to_data[i]);
204
0
                } else {
205
0
                    col_to_data[i] =
206
0
                            HashUtil::xxHash64WithSeed(value.data(), value.size(), col_to_data[i]);
207
0
                }
208
0
            }
209
18
        } else if (const auto* vb_col = check_and_get_column<ColumnVarbinary>(column)) {
210
72
            for (size_t i = 0; i < input_rows_count; ++i) {
211
54
                auto data_ref = vb_col->get_data_at(i);
212
54
                if constexpr (ReturnType == TYPE_INT) {
213
27
                    col_to_data[i] = HashUtil::xxHash32WithSeed(data_ref.data, data_ref.size,
214
27
                                                                col_to_data[i]);
215
27
                } else {
216
27
                    col_to_data[i] = HashUtil::xxHash64WithSeed(data_ref.data, data_ref.size,
217
27
                                                                col_to_data[i]);
218
27
                }
219
54
            }
220
18
        } else {
221
0
            DCHECK(false);
222
0
            return Status::NotSupported("Illegal column {} of argument of function {}",
223
0
                                        column->get_name(), name);
224
0
        }
225
527
        return Status::OK();
226
527
    }
_ZN5doris10XxHashImplILNS_13PrimitiveTypeE5EE7executeILb1EEENS_6StatusEPKNS_9IDataTypeEPKNS_7IColumnEmRS8_
Line
Count
Source
174
10
                          IColumn& col_to) {
175
10
        auto& to_column = assert_cast<ColumnVector<ReturnType>&>(col_to);
176
10
        if constexpr (first) {
177
10
            to_column.insert_many_defaults(input_rows_count);
178
10
        }
179
10
        auto& col_to_data = to_column.get_data();
180
10
        if (const auto* col_from = check_and_get_column<ColumnString>(column)) {
181
5
            const typename ColumnString::Chars& data = col_from->get_chars();
182
5
            const typename ColumnString::Offsets& offsets = col_from->get_offsets();
183
5
            size_t size = offsets.size();
184
5
            ColumnString::Offset current_offset = 0;
185
21
            for (size_t i = 0; i < size; ++i) {
186
16
                if constexpr (ReturnType == TYPE_INT) {
187
16
                    col_to_data[i] = HashUtil::xxHash32WithSeed(
188
16
                            reinterpret_cast<const char*>(&data[current_offset]),
189
16
                            offsets[i] - current_offset, col_to_data[i]);
190
                } else {
191
                    col_to_data[i] = HashUtil::xxHash64WithSeed(
192
                            reinterpret_cast<const char*>(&data[current_offset]),
193
                            offsets[i] - current_offset, col_to_data[i]);
194
                }
195
16
                current_offset = offsets[i];
196
16
            }
197
5
        } else if (const ColumnConst* col_from_const =
198
5
                           check_and_get_column_const_string_or_fixedstring(column)) {
199
0
            auto value = col_from_const->get_value<TYPE_STRING>();
200
0
            for (size_t i = 0; i < input_rows_count; ++i) {
201
0
                if constexpr (ReturnType == TYPE_INT) {
202
0
                    col_to_data[i] =
203
0
                            HashUtil::xxHash32WithSeed(value.data(), value.size(), col_to_data[i]);
204
                } else {
205
                    col_to_data[i] =
206
                            HashUtil::xxHash64WithSeed(value.data(), value.size(), col_to_data[i]);
207
                }
208
0
            }
209
5
        } else if (const auto* vb_col = check_and_get_column<ColumnVarbinary>(column)) {
210
21
            for (size_t i = 0; i < input_rows_count; ++i) {
211
16
                auto data_ref = vb_col->get_data_at(i);
212
16
                if constexpr (ReturnType == TYPE_INT) {
213
16
                    col_to_data[i] = HashUtil::xxHash32WithSeed(data_ref.data, data_ref.size,
214
16
                                                                col_to_data[i]);
215
                } else {
216
                    col_to_data[i] = HashUtil::xxHash64WithSeed(data_ref.data, data_ref.size,
217
                                                                col_to_data[i]);
218
                }
219
16
            }
220
5
        } else {
221
0
            DCHECK(false);
222
0
            return Status::NotSupported("Illegal column {} of argument of function {}",
223
0
                                        column->get_name(), name);
224
0
        }
225
10
        return Status::OK();
226
10
    }
_ZN5doris10XxHashImplILNS_13PrimitiveTypeE5EE7executeILb0EEENS_6StatusEPKNS_9IDataTypeEPKNS_7IColumnEmRS8_
Line
Count
Source
174
8
                          IColumn& col_to) {
175
8
        auto& to_column = assert_cast<ColumnVector<ReturnType>&>(col_to);
176
        if constexpr (first) {
177
            to_column.insert_many_defaults(input_rows_count);
178
        }
179
8
        auto& col_to_data = to_column.get_data();
180
8
        if (const auto* col_from = check_and_get_column<ColumnString>(column)) {
181
4
            const typename ColumnString::Chars& data = col_from->get_chars();
182
4
            const typename ColumnString::Offsets& offsets = col_from->get_offsets();
183
4
            size_t size = offsets.size();
184
4
            ColumnString::Offset current_offset = 0;
185
15
            for (size_t i = 0; i < size; ++i) {
186
11
                if constexpr (ReturnType == TYPE_INT) {
187
11
                    col_to_data[i] = HashUtil::xxHash32WithSeed(
188
11
                            reinterpret_cast<const char*>(&data[current_offset]),
189
11
                            offsets[i] - current_offset, col_to_data[i]);
190
                } else {
191
                    col_to_data[i] = HashUtil::xxHash64WithSeed(
192
                            reinterpret_cast<const char*>(&data[current_offset]),
193
                            offsets[i] - current_offset, col_to_data[i]);
194
                }
195
11
                current_offset = offsets[i];
196
11
            }
197
4
        } else if (const ColumnConst* col_from_const =
198
4
                           check_and_get_column_const_string_or_fixedstring(column)) {
199
0
            auto value = col_from_const->get_value<TYPE_STRING>();
200
0
            for (size_t i = 0; i < input_rows_count; ++i) {
201
0
                if constexpr (ReturnType == TYPE_INT) {
202
0
                    col_to_data[i] =
203
0
                            HashUtil::xxHash32WithSeed(value.data(), value.size(), col_to_data[i]);
204
                } else {
205
                    col_to_data[i] =
206
                            HashUtil::xxHash64WithSeed(value.data(), value.size(), col_to_data[i]);
207
                }
208
0
            }
209
4
        } else if (const auto* vb_col = check_and_get_column<ColumnVarbinary>(column)) {
210
15
            for (size_t i = 0; i < input_rows_count; ++i) {
211
11
                auto data_ref = vb_col->get_data_at(i);
212
11
                if constexpr (ReturnType == TYPE_INT) {
213
11
                    col_to_data[i] = HashUtil::xxHash32WithSeed(data_ref.data, data_ref.size,
214
11
                                                                col_to_data[i]);
215
                } else {
216
                    col_to_data[i] = HashUtil::xxHash64WithSeed(data_ref.data, data_ref.size,
217
                                                                col_to_data[i]);
218
                }
219
11
            }
220
4
        } else {
221
0
            DCHECK(false);
222
0
            return Status::NotSupported("Illegal column {} of argument of function {}",
223
0
                                        column->get_name(), name);
224
0
        }
225
8
        return Status::OK();
226
8
    }
_ZN5doris10XxHashImplILNS_13PrimitiveTypeE6EE7executeILb1EEENS_6StatusEPKNS_9IDataTypeEPKNS_7IColumnEmRS8_
Line
Count
Source
174
501
                          IColumn& col_to) {
175
501
        auto& to_column = assert_cast<ColumnVector<ReturnType>&>(col_to);
176
501
        if constexpr (first) {
177
501
            to_column.insert_many_defaults(input_rows_count);
178
501
        }
179
501
        auto& col_to_data = to_column.get_data();
180
501
        if (const auto* col_from = check_and_get_column<ColumnString>(column)) {
181
496
            const typename ColumnString::Chars& data = col_from->get_chars();
182
496
            const typename ColumnString::Offsets& offsets = col_from->get_offsets();
183
496
            size_t size = offsets.size();
184
496
            ColumnString::Offset current_offset = 0;
185
38.6k
            for (size_t i = 0; i < size; ++i) {
186
                if constexpr (ReturnType == TYPE_INT) {
187
                    col_to_data[i] = HashUtil::xxHash32WithSeed(
188
                            reinterpret_cast<const char*>(&data[current_offset]),
189
                            offsets[i] - current_offset, col_to_data[i]);
190
38.1k
                } else {
191
38.1k
                    col_to_data[i] = HashUtil::xxHash64WithSeed(
192
38.1k
                            reinterpret_cast<const char*>(&data[current_offset]),
193
38.1k
                            offsets[i] - current_offset, col_to_data[i]);
194
38.1k
                }
195
38.1k
                current_offset = offsets[i];
196
38.1k
            }
197
496
        } else if (const ColumnConst* col_from_const =
198
5
                           check_and_get_column_const_string_or_fixedstring(column)) {
199
0
            auto value = col_from_const->get_value<TYPE_STRING>();
200
0
            for (size_t i = 0; i < input_rows_count; ++i) {
201
                if constexpr (ReturnType == TYPE_INT) {
202
                    col_to_data[i] =
203
                            HashUtil::xxHash32WithSeed(value.data(), value.size(), col_to_data[i]);
204
0
                } else {
205
0
                    col_to_data[i] =
206
0
                            HashUtil::xxHash64WithSeed(value.data(), value.size(), col_to_data[i]);
207
0
                }
208
0
            }
209
5
        } else if (const auto* vb_col = check_and_get_column<ColumnVarbinary>(column)) {
210
21
            for (size_t i = 0; i < input_rows_count; ++i) {
211
16
                auto data_ref = vb_col->get_data_at(i);
212
                if constexpr (ReturnType == TYPE_INT) {
213
                    col_to_data[i] = HashUtil::xxHash32WithSeed(data_ref.data, data_ref.size,
214
                                                                col_to_data[i]);
215
16
                } else {
216
16
                    col_to_data[i] = HashUtil::xxHash64WithSeed(data_ref.data, data_ref.size,
217
16
                                                                col_to_data[i]);
218
16
                }
219
16
            }
220
5
        } else {
221
0
            DCHECK(false);
222
0
            return Status::NotSupported("Illegal column {} of argument of function {}",
223
0
                                        column->get_name(), name);
224
0
        }
225
501
        return Status::OK();
226
501
    }
_ZN5doris10XxHashImplILNS_13PrimitiveTypeE6EE7executeILb0EEENS_6StatusEPKNS_9IDataTypeEPKNS_7IColumnEmRS8_
Line
Count
Source
174
8
                          IColumn& col_to) {
175
8
        auto& to_column = assert_cast<ColumnVector<ReturnType>&>(col_to);
176
        if constexpr (first) {
177
            to_column.insert_many_defaults(input_rows_count);
178
        }
179
8
        auto& col_to_data = to_column.get_data();
180
8
        if (const auto* col_from = check_and_get_column<ColumnString>(column)) {
181
4
            const typename ColumnString::Chars& data = col_from->get_chars();
182
4
            const typename ColumnString::Offsets& offsets = col_from->get_offsets();
183
4
            size_t size = offsets.size();
184
4
            ColumnString::Offset current_offset = 0;
185
15
            for (size_t i = 0; i < size; ++i) {
186
                if constexpr (ReturnType == TYPE_INT) {
187
                    col_to_data[i] = HashUtil::xxHash32WithSeed(
188
                            reinterpret_cast<const char*>(&data[current_offset]),
189
                            offsets[i] - current_offset, col_to_data[i]);
190
11
                } else {
191
11
                    col_to_data[i] = HashUtil::xxHash64WithSeed(
192
11
                            reinterpret_cast<const char*>(&data[current_offset]),
193
11
                            offsets[i] - current_offset, col_to_data[i]);
194
11
                }
195
11
                current_offset = offsets[i];
196
11
            }
197
4
        } else if (const ColumnConst* col_from_const =
198
4
                           check_and_get_column_const_string_or_fixedstring(column)) {
199
0
            auto value = col_from_const->get_value<TYPE_STRING>();
200
0
            for (size_t i = 0; i < input_rows_count; ++i) {
201
                if constexpr (ReturnType == TYPE_INT) {
202
                    col_to_data[i] =
203
                            HashUtil::xxHash32WithSeed(value.data(), value.size(), col_to_data[i]);
204
0
                } else {
205
0
                    col_to_data[i] =
206
0
                            HashUtil::xxHash64WithSeed(value.data(), value.size(), col_to_data[i]);
207
0
                }
208
0
            }
209
4
        } else if (const auto* vb_col = check_and_get_column<ColumnVarbinary>(column)) {
210
15
            for (size_t i = 0; i < input_rows_count; ++i) {
211
11
                auto data_ref = vb_col->get_data_at(i);
212
                if constexpr (ReturnType == TYPE_INT) {
213
                    col_to_data[i] = HashUtil::xxHash32WithSeed(data_ref.data, data_ref.size,
214
                                                                col_to_data[i]);
215
11
                } else {
216
11
                    col_to_data[i] = HashUtil::xxHash64WithSeed(data_ref.data, data_ref.size,
217
11
                                                                col_to_data[i]);
218
11
                }
219
11
            }
220
4
        } else {
221
0
            DCHECK(false);
222
0
            return Status::NotSupported("Illegal column {} of argument of function {}",
223
0
                                        column->get_name(), name);
224
0
        }
225
8
        return Status::OK();
226
8
    }
227
};
228
229
using FunctionXxHash_32 = FunctionVariadicArgumentsBase<DataTypeInt32, XxHashImpl<TYPE_INT>>;
230
using FunctionXxHash_64 = FunctionVariadicArgumentsBase<DataTypeInt64, XxHashImpl<TYPE_BIGINT>>;
231
232
6
void register_function_hash(SimpleFunctionFactory& factory) {
233
6
    factory.register_function<FunctionMurmurHash3_32>();
234
6
    factory.register_function<FunctionMurmurHash3_64>();
235
6
    factory.register_function<FunctionMurmurHash3_64_V2>();
236
6
    factory.register_function<FunctionMurmurHash3U64V2>();
237
6
    factory.register_function<FunctionXxHash_32>();
238
6
    factory.register_function<FunctionXxHash_64>();
239
6
    factory.register_alias("xxhash_64", "xxhash3_64");
240
6
}
241
} // namespace doris