Coverage Report

Created: 2026-03-14 18:30

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
#include "common/compile_check_begin.h"
42
constexpr uint64_t emtpy_value = 0xe28dbde7fe22e41c;
43
44
template <PrimitiveType ReturnType, bool is_mmh64_v2 = false>
45
struct MurmurHash3Impl {
46
3
    static constexpr auto get_name() {
47
3
        if constexpr (ReturnType == TYPE_INT) {
48
1
            return "murmur_hash3_32";
49
1
        } else if constexpr (is_mmh64_v2) {
50
1
            return "murmur_hash3_64_v2";
51
1
        } else {
52
1
            return "murmur_hash3_64";
53
1
        }
54
3
    }
_ZN5doris15MurmurHash3ImplILNS_13PrimitiveTypeE5ELb0EE8get_nameEv
Line
Count
Source
46
1
    static constexpr auto get_name() {
47
1
        if constexpr (ReturnType == TYPE_INT) {
48
1
            return "murmur_hash3_32";
49
        } else if constexpr (is_mmh64_v2) {
50
            return "murmur_hash3_64_v2";
51
        } else {
52
            return "murmur_hash3_64";
53
        }
54
1
    }
_ZN5doris15MurmurHash3ImplILNS_13PrimitiveTypeE6ELb0EE8get_nameEv
Line
Count
Source
46
1
    static constexpr auto get_name() {
47
        if constexpr (ReturnType == TYPE_INT) {
48
            return "murmur_hash3_32";
49
        } else if constexpr (is_mmh64_v2) {
50
            return "murmur_hash3_64_v2";
51
1
        } else {
52
1
            return "murmur_hash3_64";
53
1
        }
54
1
    }
_ZN5doris15MurmurHash3ImplILNS_13PrimitiveTypeE6ELb1EE8get_nameEv
Line
Count
Source
46
1
    static constexpr auto get_name() {
47
        if constexpr (ReturnType == TYPE_INT) {
48
            return "murmur_hash3_32";
49
1
        } else if constexpr (is_mmh64_v2) {
50
1
            return "murmur_hash3_64_v2";
51
        } else {
52
            return "murmur_hash3_64";
53
        }
54
1
    }
55
    static constexpr auto name = get_name();
56
57
0
    static Status empty_apply(IColumn& icolumn, size_t input_rows_count) {
58
0
        ColumnVector<ReturnType>& vec_to = assert_cast<ColumnVector<ReturnType>&>(icolumn);
59
0
        vec_to.get_data().assign(
60
0
                input_rows_count,
61
0
                static_cast<typename PrimitiveTypeTraits<ReturnType>::CppType>(emtpy_value));
62
0
        return Status::OK();
63
0
    }
Unexecuted instantiation: _ZN5doris15MurmurHash3ImplILNS_13PrimitiveTypeE5ELb0EE11empty_applyERNS_7IColumnEm
Unexecuted instantiation: _ZN5doris15MurmurHash3ImplILNS_13PrimitiveTypeE6ELb0EE11empty_applyERNS_7IColumnEm
Unexecuted instantiation: _ZN5doris15MurmurHash3ImplILNS_13PrimitiveTypeE6ELb1EE11empty_applyERNS_7IColumnEm
64
65
    static Status first_apply(const IDataType* type, const IColumn* column, size_t input_rows_count,
66
7
                              IColumn& icolumn) {
67
7
        return execute<true>(type, column, input_rows_count, icolumn);
68
7
    }
_ZN5doris15MurmurHash3ImplILNS_13PrimitiveTypeE5ELb0EE11first_applyEPKNS_9IDataTypeEPKNS_7IColumnEmRS6_
Line
Count
Source
66
3
                              IColumn& icolumn) {
67
3
        return execute<true>(type, column, input_rows_count, icolumn);
68
3
    }
_ZN5doris15MurmurHash3ImplILNS_13PrimitiveTypeE6ELb0EE11first_applyEPKNS_9IDataTypeEPKNS_7IColumnEmRS6_
Line
Count
Source
66
3
                              IColumn& icolumn) {
67
3
        return execute<true>(type, column, input_rows_count, icolumn);
68
3
    }
_ZN5doris15MurmurHash3ImplILNS_13PrimitiveTypeE6ELb1EE11first_applyEPKNS_9IDataTypeEPKNS_7IColumnEmRS6_
Line
Count
Source
66
1
                              IColumn& icolumn) {
67
1
        return execute<true>(type, column, input_rows_count, icolumn);
68
1
    }
69
70
    static Status combine_apply(const IDataType* type, const IColumn* column,
71
6
                                size_t input_rows_count, IColumn& icolumn) {
72
6
        return execute<false>(type, column, input_rows_count, icolumn);
73
6
    }
_ZN5doris15MurmurHash3ImplILNS_13PrimitiveTypeE5ELb0EE13combine_applyEPKNS_9IDataTypeEPKNS_7IColumnEmRS6_
Line
Count
Source
71
3
                                size_t input_rows_count, IColumn& icolumn) {
72
3
        return execute<false>(type, column, input_rows_count, icolumn);
73
3
    }
_ZN5doris15MurmurHash3ImplILNS_13PrimitiveTypeE6ELb0EE13combine_applyEPKNS_9IDataTypeEPKNS_7IColumnEmRS6_
Line
Count
Source
71
3
                                size_t input_rows_count, IColumn& icolumn) {
72
3
        return execute<false>(type, column, input_rows_count, icolumn);
73
3
    }
Unexecuted instantiation: _ZN5doris15MurmurHash3ImplILNS_13PrimitiveTypeE6ELb1EE13combine_applyEPKNS_9IDataTypeEPKNS_7IColumnEmRS6_
74
75
    template <bool first>
76
    static Status execute(const IDataType* type, const IColumn* column, size_t input_rows_count,
77
13
                          IColumn& col_to) {
78
13
        auto& to_column = assert_cast<ColumnVector<ReturnType>&>(col_to);
79
13
        if constexpr (first) {
80
7
            if constexpr (ReturnType == TYPE_INT) {
81
3
                to_column.insert_many_vals(static_cast<Int32>(HashUtil::MURMUR3_32_SEED),
82
3
                                           input_rows_count);
83
4
            } else {
84
4
                to_column.insert_many_defaults(input_rows_count);
85
4
            }
86
7
        }
87
13
        auto& col_to_data = to_column.get_data();
88
13
        if (const auto* col_from = check_and_get_column<ColumnString>(column)) {
89
13
            const typename ColumnString::Chars& data = col_from->get_chars();
90
13
            const typename ColumnString::Offsets& offsets = col_from->get_offsets();
91
13
            size_t size = offsets.size();
92
13
            ColumnString::Offset current_offset = 0;
93
40
            for (size_t i = 0; i < size; ++i) {
94
27
                if constexpr (ReturnType == TYPE_INT) {
95
12
                    col_to_data[i] = HashUtil::murmur_hash3_32(
96
12
                            reinterpret_cast<const char*>(&data[current_offset]),
97
12
                            offsets[i] - current_offset, col_to_data[i]);
98
15
                } else {
99
15
                    col_to_data[i] = HashUtil::murmur_hash3_64<is_mmh64_v2>(
100
15
                            reinterpret_cast<const char*>(&data[current_offset]),
101
15
                            offsets[i] - current_offset, col_to_data[i]);
102
15
                }
103
27
                current_offset = offsets[i];
104
27
            }
105
13
        } else if (const ColumnConst* col_from_const =
106
0
                           check_and_get_column_const_string_or_fixedstring(column)) {
107
0
            auto value = col_from_const->get_value<TYPE_STRING>();
108
0
            for (size_t i = 0; i < input_rows_count; ++i) {
109
0
                if constexpr (ReturnType == TYPE_INT) {
110
0
                    col_to_data[i] =
111
0
                            HashUtil::murmur_hash3_32(value.data(), value.size(), col_to_data[i]);
112
0
                } else {
113
0
                    col_to_data[i] = HashUtil::murmur_hash3_64<is_mmh64_v2>(
114
0
                            value.data(), value.size(), col_to_data[i]);
115
0
                }
116
0
            }
117
0
        } else {
118
0
            DCHECK(false);
119
0
            return Status::NotSupported("Illegal column {} of argument of function {}",
120
0
                                        column->get_name(), name);
121
0
        }
122
13
        return Status::OK();
123
13
    }
_ZN5doris15MurmurHash3ImplILNS_13PrimitiveTypeE5ELb0EE7executeILb1EEENS_6StatusEPKNS_9IDataTypeEPKNS_7IColumnEmRS8_
Line
Count
Source
77
3
                          IColumn& col_to) {
78
3
        auto& to_column = assert_cast<ColumnVector<ReturnType>&>(col_to);
79
3
        if constexpr (first) {
80
3
            if constexpr (ReturnType == TYPE_INT) {
81
3
                to_column.insert_many_vals(static_cast<Int32>(HashUtil::MURMUR3_32_SEED),
82
3
                                           input_rows_count);
83
            } else {
84
                to_column.insert_many_defaults(input_rows_count);
85
            }
86
3
        }
87
3
        auto& col_to_data = to_column.get_data();
88
3
        if (const auto* col_from = check_and_get_column<ColumnString>(column)) {
89
3
            const typename ColumnString::Chars& data = col_from->get_chars();
90
3
            const typename ColumnString::Offsets& offsets = col_from->get_offsets();
91
3
            size_t size = offsets.size();
92
3
            ColumnString::Offset current_offset = 0;
93
9
            for (size_t i = 0; i < size; ++i) {
94
6
                if constexpr (ReturnType == TYPE_INT) {
95
6
                    col_to_data[i] = HashUtil::murmur_hash3_32(
96
6
                            reinterpret_cast<const char*>(&data[current_offset]),
97
6
                            offsets[i] - current_offset, col_to_data[i]);
98
                } else {
99
                    col_to_data[i] = HashUtil::murmur_hash3_64<is_mmh64_v2>(
100
                            reinterpret_cast<const char*>(&data[current_offset]),
101
                            offsets[i] - current_offset, col_to_data[i]);
102
                }
103
6
                current_offset = offsets[i];
104
6
            }
105
3
        } else if (const ColumnConst* col_from_const =
106
0
                           check_and_get_column_const_string_or_fixedstring(column)) {
107
0
            auto value = col_from_const->get_value<TYPE_STRING>();
108
0
            for (size_t i = 0; i < input_rows_count; ++i) {
109
0
                if constexpr (ReturnType == TYPE_INT) {
110
0
                    col_to_data[i] =
111
0
                            HashUtil::murmur_hash3_32(value.data(), value.size(), col_to_data[i]);
112
                } else {
113
                    col_to_data[i] = HashUtil::murmur_hash3_64<is_mmh64_v2>(
114
                            value.data(), value.size(), col_to_data[i]);
115
                }
116
0
            }
117
0
        } else {
118
0
            DCHECK(false);
119
0
            return Status::NotSupported("Illegal column {} of argument of function {}",
120
0
                                        column->get_name(), name);
121
0
        }
122
3
        return Status::OK();
123
3
    }
_ZN5doris15MurmurHash3ImplILNS_13PrimitiveTypeE5ELb0EE7executeILb0EEENS_6StatusEPKNS_9IDataTypeEPKNS_7IColumnEmRS8_
Line
Count
Source
77
3
                          IColumn& col_to) {
78
3
        auto& to_column = assert_cast<ColumnVector<ReturnType>&>(col_to);
79
        if constexpr (first) {
80
            if constexpr (ReturnType == TYPE_INT) {
81
                to_column.insert_many_vals(static_cast<Int32>(HashUtil::MURMUR3_32_SEED),
82
                                           input_rows_count);
83
            } else {
84
                to_column.insert_many_defaults(input_rows_count);
85
            }
86
        }
87
3
        auto& col_to_data = to_column.get_data();
88
3
        if (const auto* col_from = check_and_get_column<ColumnString>(column)) {
89
3
            const typename ColumnString::Chars& data = col_from->get_chars();
90
3
            const typename ColumnString::Offsets& offsets = col_from->get_offsets();
91
3
            size_t size = offsets.size();
92
3
            ColumnString::Offset current_offset = 0;
93
9
            for (size_t i = 0; i < size; ++i) {
94
6
                if constexpr (ReturnType == TYPE_INT) {
95
6
                    col_to_data[i] = HashUtil::murmur_hash3_32(
96
6
                            reinterpret_cast<const char*>(&data[current_offset]),
97
6
                            offsets[i] - current_offset, col_to_data[i]);
98
                } else {
99
                    col_to_data[i] = HashUtil::murmur_hash3_64<is_mmh64_v2>(
100
                            reinterpret_cast<const char*>(&data[current_offset]),
101
                            offsets[i] - current_offset, col_to_data[i]);
102
                }
103
6
                current_offset = offsets[i];
104
6
            }
105
3
        } else if (const ColumnConst* col_from_const =
106
0
                           check_and_get_column_const_string_or_fixedstring(column)) {
107
0
            auto value = col_from_const->get_value<TYPE_STRING>();
108
0
            for (size_t i = 0; i < input_rows_count; ++i) {
109
0
                if constexpr (ReturnType == TYPE_INT) {
110
0
                    col_to_data[i] =
111
0
                            HashUtil::murmur_hash3_32(value.data(), value.size(), col_to_data[i]);
112
                } else {
113
                    col_to_data[i] = HashUtil::murmur_hash3_64<is_mmh64_v2>(
114
                            value.data(), value.size(), col_to_data[i]);
115
                }
116
0
            }
117
0
        } else {
118
0
            DCHECK(false);
119
0
            return Status::NotSupported("Illegal column {} of argument of function {}",
120
0
                                        column->get_name(), name);
121
0
        }
122
3
        return Status::OK();
123
3
    }
_ZN5doris15MurmurHash3ImplILNS_13PrimitiveTypeE6ELb0EE7executeILb1EEENS_6StatusEPKNS_9IDataTypeEPKNS_7IColumnEmRS8_
Line
Count
Source
77
3
                          IColumn& col_to) {
78
3
        auto& to_column = assert_cast<ColumnVector<ReturnType>&>(col_to);
79
3
        if constexpr (first) {
80
            if constexpr (ReturnType == TYPE_INT) {
81
                to_column.insert_many_vals(static_cast<Int32>(HashUtil::MURMUR3_32_SEED),
82
                                           input_rows_count);
83
3
            } else {
84
3
                to_column.insert_many_defaults(input_rows_count);
85
3
            }
86
3
        }
87
3
        auto& col_to_data = to_column.get_data();
88
3
        if (const auto* col_from = check_and_get_column<ColumnString>(column)) {
89
3
            const typename ColumnString::Chars& data = col_from->get_chars();
90
3
            const typename ColumnString::Offsets& offsets = col_from->get_offsets();
91
3
            size_t size = offsets.size();
92
3
            ColumnString::Offset current_offset = 0;
93
9
            for (size_t i = 0; i < size; ++i) {
94
                if constexpr (ReturnType == TYPE_INT) {
95
                    col_to_data[i] = HashUtil::murmur_hash3_32(
96
                            reinterpret_cast<const char*>(&data[current_offset]),
97
                            offsets[i] - current_offset, col_to_data[i]);
98
6
                } else {
99
6
                    col_to_data[i] = HashUtil::murmur_hash3_64<is_mmh64_v2>(
100
6
                            reinterpret_cast<const char*>(&data[current_offset]),
101
6
                            offsets[i] - current_offset, col_to_data[i]);
102
6
                }
103
6
                current_offset = offsets[i];
104
6
            }
105
3
        } else if (const ColumnConst* col_from_const =
106
0
                           check_and_get_column_const_string_or_fixedstring(column)) {
107
0
            auto value = col_from_const->get_value<TYPE_STRING>();
108
0
            for (size_t i = 0; i < input_rows_count; ++i) {
109
                if constexpr (ReturnType == TYPE_INT) {
110
                    col_to_data[i] =
111
                            HashUtil::murmur_hash3_32(value.data(), value.size(), col_to_data[i]);
112
0
                } else {
113
0
                    col_to_data[i] = HashUtil::murmur_hash3_64<is_mmh64_v2>(
114
0
                            value.data(), value.size(), col_to_data[i]);
115
0
                }
116
0
            }
117
0
        } else {
118
0
            DCHECK(false);
119
0
            return Status::NotSupported("Illegal column {} of argument of function {}",
120
0
                                        column->get_name(), name);
121
0
        }
122
3
        return Status::OK();
123
3
    }
_ZN5doris15MurmurHash3ImplILNS_13PrimitiveTypeE6ELb0EE7executeILb0EEENS_6StatusEPKNS_9IDataTypeEPKNS_7IColumnEmRS8_
Line
Count
Source
77
3
                          IColumn& col_to) {
78
3
        auto& to_column = assert_cast<ColumnVector<ReturnType>&>(col_to);
79
        if constexpr (first) {
80
            if constexpr (ReturnType == TYPE_INT) {
81
                to_column.insert_many_vals(static_cast<Int32>(HashUtil::MURMUR3_32_SEED),
82
                                           input_rows_count);
83
            } else {
84
                to_column.insert_many_defaults(input_rows_count);
85
            }
86
        }
87
3
        auto& col_to_data = to_column.get_data();
88
3
        if (const auto* col_from = check_and_get_column<ColumnString>(column)) {
89
3
            const typename ColumnString::Chars& data = col_from->get_chars();
90
3
            const typename ColumnString::Offsets& offsets = col_from->get_offsets();
91
3
            size_t size = offsets.size();
92
3
            ColumnString::Offset current_offset = 0;
93
9
            for (size_t i = 0; i < size; ++i) {
94
                if constexpr (ReturnType == TYPE_INT) {
95
                    col_to_data[i] = HashUtil::murmur_hash3_32(
96
                            reinterpret_cast<const char*>(&data[current_offset]),
97
                            offsets[i] - current_offset, col_to_data[i]);
98
6
                } else {
99
6
                    col_to_data[i] = HashUtil::murmur_hash3_64<is_mmh64_v2>(
100
6
                            reinterpret_cast<const char*>(&data[current_offset]),
101
6
                            offsets[i] - current_offset, col_to_data[i]);
102
6
                }
103
6
                current_offset = offsets[i];
104
6
            }
105
3
        } else if (const ColumnConst* col_from_const =
106
0
                           check_and_get_column_const_string_or_fixedstring(column)) {
107
0
            auto value = col_from_const->get_value<TYPE_STRING>();
108
0
            for (size_t i = 0; i < input_rows_count; ++i) {
109
                if constexpr (ReturnType == TYPE_INT) {
110
                    col_to_data[i] =
111
                            HashUtil::murmur_hash3_32(value.data(), value.size(), col_to_data[i]);
112
0
                } else {
113
0
                    col_to_data[i] = HashUtil::murmur_hash3_64<is_mmh64_v2>(
114
0
                            value.data(), value.size(), col_to_data[i]);
115
0
                }
116
0
            }
117
0
        } else {
118
0
            DCHECK(false);
119
0
            return Status::NotSupported("Illegal column {} of argument of function {}",
120
0
                                        column->get_name(), name);
121
0
        }
122
3
        return Status::OK();
123
3
    }
_ZN5doris15MurmurHash3ImplILNS_13PrimitiveTypeE6ELb1EE7executeILb1EEENS_6StatusEPKNS_9IDataTypeEPKNS_7IColumnEmRS8_
Line
Count
Source
77
1
                          IColumn& col_to) {
78
1
        auto& to_column = assert_cast<ColumnVector<ReturnType>&>(col_to);
79
1
        if constexpr (first) {
80
            if constexpr (ReturnType == TYPE_INT) {
81
                to_column.insert_many_vals(static_cast<Int32>(HashUtil::MURMUR3_32_SEED),
82
                                           input_rows_count);
83
1
            } else {
84
1
                to_column.insert_many_defaults(input_rows_count);
85
1
            }
86
1
        }
87
1
        auto& col_to_data = to_column.get_data();
88
1
        if (const auto* col_from = check_and_get_column<ColumnString>(column)) {
89
1
            const typename ColumnString::Chars& data = col_from->get_chars();
90
1
            const typename ColumnString::Offsets& offsets = col_from->get_offsets();
91
1
            size_t size = offsets.size();
92
1
            ColumnString::Offset current_offset = 0;
93
4
            for (size_t i = 0; i < size; ++i) {
94
                if constexpr (ReturnType == TYPE_INT) {
95
                    col_to_data[i] = HashUtil::murmur_hash3_32(
96
                            reinterpret_cast<const char*>(&data[current_offset]),
97
                            offsets[i] - current_offset, col_to_data[i]);
98
3
                } else {
99
3
                    col_to_data[i] = HashUtil::murmur_hash3_64<is_mmh64_v2>(
100
3
                            reinterpret_cast<const char*>(&data[current_offset]),
101
3
                            offsets[i] - current_offset, col_to_data[i]);
102
3
                }
103
3
                current_offset = offsets[i];
104
3
            }
105
1
        } else if (const ColumnConst* col_from_const =
106
0
                           check_and_get_column_const_string_or_fixedstring(column)) {
107
0
            auto value = col_from_const->get_value<TYPE_STRING>();
108
0
            for (size_t i = 0; i < input_rows_count; ++i) {
109
                if constexpr (ReturnType == TYPE_INT) {
110
                    col_to_data[i] =
111
                            HashUtil::murmur_hash3_32(value.data(), value.size(), col_to_data[i]);
112
0
                } else {
113
0
                    col_to_data[i] = HashUtil::murmur_hash3_64<is_mmh64_v2>(
114
0
                            value.data(), value.size(), col_to_data[i]);
115
0
                }
116
0
            }
117
0
        } else {
118
0
            DCHECK(false);
119
0
            return Status::NotSupported("Illegal column {} of argument of function {}",
120
0
                                        column->get_name(), name);
121
0
        }
122
1
        return Status::OK();
123
1
    }
Unexecuted instantiation: _ZN5doris15MurmurHash3ImplILNS_13PrimitiveTypeE6ELb1EE7executeILb0EEENS_6StatusEPKNS_9IDataTypeEPKNS_7IColumnEmRS8_
124
};
125
126
using FunctionMurmurHash3_32 =
127
        FunctionVariadicArgumentsBase<DataTypeInt32, MurmurHash3Impl<TYPE_INT>>;
128
using FunctionMurmurHash3_64 =
129
        FunctionVariadicArgumentsBase<DataTypeInt64, MurmurHash3Impl<TYPE_BIGINT>>;
130
using FunctionMurmurHash3_64_V2 =
131
        FunctionVariadicArgumentsBase<DataTypeInt64, MurmurHash3Impl<TYPE_BIGINT, true>>;
132
133
#ifdef BE_TEST
134
1
const char* murmur_hash3_get_name_type_int_for_test() {
135
1
    return MurmurHash3Impl<TYPE_INT>::get_name();
136
1
}
137
138
1
const char* murmur_hash3_get_name_type_bigint_for_test() {
139
1
    return MurmurHash3Impl<TYPE_BIGINT>::get_name();
140
1
}
141
142
1
const char* murmur_hash3_get_name_type_bigint_v2_for_test() {
143
1
    return MurmurHash3Impl<TYPE_BIGINT, true>::get_name();
144
1
}
145
#endif
146
147
template <PrimitiveType ReturnType>
148
struct XxHashImpl {
149
    static constexpr auto name = ReturnType == TYPE_INT ? "xxhash_32" : "xxhash_64";
150
151
0
    static Status empty_apply(IColumn& icolumn, size_t input_rows_count) {
152
0
        ColumnVector<ReturnType>& vec_to = assert_cast<ColumnVector<ReturnType>&>(icolumn);
153
0
        vec_to.get_data().assign(
154
0
                input_rows_count,
155
0
                static_cast<typename PrimitiveTypeTraits<ReturnType>::CppType>(emtpy_value));
156
0
        return Status::OK();
157
0
    }
Unexecuted instantiation: _ZN5doris10XxHashImplILNS_13PrimitiveTypeE5EE11empty_applyERNS_7IColumnEm
Unexecuted instantiation: _ZN5doris10XxHashImplILNS_13PrimitiveTypeE6EE11empty_applyERNS_7IColumnEm
158
159
    static Status first_apply(const IDataType* type, const IColumn* column, size_t input_rows_count,
160
12
                              IColumn& icolumn) {
161
12
        return execute<true>(type, column, input_rows_count, icolumn);
162
12
    }
_ZN5doris10XxHashImplILNS_13PrimitiveTypeE5EE11first_applyEPKNS_9IDataTypeEPKNS_7IColumnEmRS6_
Line
Count
Source
160
6
                              IColumn& icolumn) {
161
6
        return execute<true>(type, column, input_rows_count, icolumn);
162
6
    }
_ZN5doris10XxHashImplILNS_13PrimitiveTypeE6EE11first_applyEPKNS_9IDataTypeEPKNS_7IColumnEmRS6_
Line
Count
Source
160
6
                              IColumn& icolumn) {
161
6
        return execute<true>(type, column, input_rows_count, icolumn);
162
6
    }
163
164
    static Status combine_apply(const IDataType* type, const IColumn* column,
165
12
                                size_t input_rows_count, IColumn& icolumn) {
166
12
        return execute<false>(type, column, input_rows_count, icolumn);
167
12
    }
_ZN5doris10XxHashImplILNS_13PrimitiveTypeE5EE13combine_applyEPKNS_9IDataTypeEPKNS_7IColumnEmRS6_
Line
Count
Source
165
6
                                size_t input_rows_count, IColumn& icolumn) {
166
6
        return execute<false>(type, column, input_rows_count, icolumn);
167
6
    }
_ZN5doris10XxHashImplILNS_13PrimitiveTypeE6EE13combine_applyEPKNS_9IDataTypeEPKNS_7IColumnEmRS6_
Line
Count
Source
165
6
                                size_t input_rows_count, IColumn& icolumn) {
166
6
        return execute<false>(type, column, input_rows_count, icolumn);
167
6
    }
168
169
    template <bool first>
170
    static Status execute(const IDataType* type, const IColumn* column, size_t input_rows_count,
171
24
                          IColumn& col_to) {
172
24
        auto& to_column = assert_cast<ColumnVector<ReturnType>&>(col_to);
173
24
        if constexpr (first) {
174
12
            to_column.insert_many_defaults(input_rows_count);
175
12
        }
176
24
        auto& col_to_data = to_column.get_data();
177
24
        if (const auto* col_from = check_and_get_column<ColumnString>(column)) {
178
12
            const typename ColumnString::Chars& data = col_from->get_chars();
179
12
            const typename ColumnString::Offsets& offsets = col_from->get_offsets();
180
12
            size_t size = offsets.size();
181
12
            ColumnString::Offset current_offset = 0;
182
36
            for (size_t i = 0; i < size; ++i) {
183
24
                if constexpr (ReturnType == TYPE_INT) {
184
12
                    col_to_data[i] = HashUtil::xxHash32WithSeed(
185
12
                            reinterpret_cast<const char*>(&data[current_offset]),
186
12
                            offsets[i] - current_offset, col_to_data[i]);
187
12
                } else {
188
12
                    col_to_data[i] = HashUtil::xxHash64WithSeed(
189
12
                            reinterpret_cast<const char*>(&data[current_offset]),
190
12
                            offsets[i] - current_offset, col_to_data[i]);
191
12
                }
192
24
                current_offset = offsets[i];
193
24
            }
194
12
        } else if (const ColumnConst* col_from_const =
195
12
                           check_and_get_column_const_string_or_fixedstring(column)) {
196
0
            auto value = col_from_const->get_value<TYPE_STRING>();
197
0
            for (size_t i = 0; i < input_rows_count; ++i) {
198
0
                if constexpr (ReturnType == TYPE_INT) {
199
0
                    col_to_data[i] =
200
0
                            HashUtil::xxHash32WithSeed(value.data(), value.size(), col_to_data[i]);
201
0
                } else {
202
0
                    col_to_data[i] =
203
0
                            HashUtil::xxHash64WithSeed(value.data(), value.size(), col_to_data[i]);
204
0
                }
205
0
            }
206
12
        } else if (const auto* vb_col = check_and_get_column<ColumnVarbinary>(column)) {
207
36
            for (size_t i = 0; i < input_rows_count; ++i) {
208
24
                auto data_ref = vb_col->get_data_at(i);
209
24
                if constexpr (ReturnType == TYPE_INT) {
210
12
                    col_to_data[i] = HashUtil::xxHash32WithSeed(data_ref.data, data_ref.size,
211
12
                                                                col_to_data[i]);
212
12
                } else {
213
12
                    col_to_data[i] = HashUtil::xxHash64WithSeed(data_ref.data, data_ref.size,
214
12
                                                                col_to_data[i]);
215
12
                }
216
24
            }
217
12
        } else {
218
0
            DCHECK(false);
219
0
            return Status::NotSupported("Illegal column {} of argument of function {}",
220
0
                                        column->get_name(), name);
221
0
        }
222
24
        return Status::OK();
223
24
    }
_ZN5doris10XxHashImplILNS_13PrimitiveTypeE5EE7executeILb1EEENS_6StatusEPKNS_9IDataTypeEPKNS_7IColumnEmRS8_
Line
Count
Source
171
6
                          IColumn& col_to) {
172
6
        auto& to_column = assert_cast<ColumnVector<ReturnType>&>(col_to);
173
6
        if constexpr (first) {
174
6
            to_column.insert_many_defaults(input_rows_count);
175
6
        }
176
6
        auto& col_to_data = to_column.get_data();
177
6
        if (const auto* col_from = check_and_get_column<ColumnString>(column)) {
178
3
            const typename ColumnString::Chars& data = col_from->get_chars();
179
3
            const typename ColumnString::Offsets& offsets = col_from->get_offsets();
180
3
            size_t size = offsets.size();
181
3
            ColumnString::Offset current_offset = 0;
182
9
            for (size_t i = 0; i < size; ++i) {
183
6
                if constexpr (ReturnType == TYPE_INT) {
184
6
                    col_to_data[i] = HashUtil::xxHash32WithSeed(
185
6
                            reinterpret_cast<const char*>(&data[current_offset]),
186
6
                            offsets[i] - current_offset, col_to_data[i]);
187
                } else {
188
                    col_to_data[i] = HashUtil::xxHash64WithSeed(
189
                            reinterpret_cast<const char*>(&data[current_offset]),
190
                            offsets[i] - current_offset, col_to_data[i]);
191
                }
192
6
                current_offset = offsets[i];
193
6
            }
194
3
        } else if (const ColumnConst* col_from_const =
195
3
                           check_and_get_column_const_string_or_fixedstring(column)) {
196
0
            auto value = col_from_const->get_value<TYPE_STRING>();
197
0
            for (size_t i = 0; i < input_rows_count; ++i) {
198
0
                if constexpr (ReturnType == TYPE_INT) {
199
0
                    col_to_data[i] =
200
0
                            HashUtil::xxHash32WithSeed(value.data(), value.size(), col_to_data[i]);
201
                } else {
202
                    col_to_data[i] =
203
                            HashUtil::xxHash64WithSeed(value.data(), value.size(), col_to_data[i]);
204
                }
205
0
            }
206
3
        } else if (const auto* vb_col = check_and_get_column<ColumnVarbinary>(column)) {
207
9
            for (size_t i = 0; i < input_rows_count; ++i) {
208
6
                auto data_ref = vb_col->get_data_at(i);
209
6
                if constexpr (ReturnType == TYPE_INT) {
210
6
                    col_to_data[i] = HashUtil::xxHash32WithSeed(data_ref.data, data_ref.size,
211
6
                                                                col_to_data[i]);
212
                } else {
213
                    col_to_data[i] = HashUtil::xxHash64WithSeed(data_ref.data, data_ref.size,
214
                                                                col_to_data[i]);
215
                }
216
6
            }
217
3
        } else {
218
0
            DCHECK(false);
219
0
            return Status::NotSupported("Illegal column {} of argument of function {}",
220
0
                                        column->get_name(), name);
221
0
        }
222
6
        return Status::OK();
223
6
    }
_ZN5doris10XxHashImplILNS_13PrimitiveTypeE5EE7executeILb0EEENS_6StatusEPKNS_9IDataTypeEPKNS_7IColumnEmRS8_
Line
Count
Source
171
6
                          IColumn& col_to) {
172
6
        auto& to_column = assert_cast<ColumnVector<ReturnType>&>(col_to);
173
        if constexpr (first) {
174
            to_column.insert_many_defaults(input_rows_count);
175
        }
176
6
        auto& col_to_data = to_column.get_data();
177
6
        if (const auto* col_from = check_and_get_column<ColumnString>(column)) {
178
3
            const typename ColumnString::Chars& data = col_from->get_chars();
179
3
            const typename ColumnString::Offsets& offsets = col_from->get_offsets();
180
3
            size_t size = offsets.size();
181
3
            ColumnString::Offset current_offset = 0;
182
9
            for (size_t i = 0; i < size; ++i) {
183
6
                if constexpr (ReturnType == TYPE_INT) {
184
6
                    col_to_data[i] = HashUtil::xxHash32WithSeed(
185
6
                            reinterpret_cast<const char*>(&data[current_offset]),
186
6
                            offsets[i] - current_offset, col_to_data[i]);
187
                } else {
188
                    col_to_data[i] = HashUtil::xxHash64WithSeed(
189
                            reinterpret_cast<const char*>(&data[current_offset]),
190
                            offsets[i] - current_offset, col_to_data[i]);
191
                }
192
6
                current_offset = offsets[i];
193
6
            }
194
3
        } else if (const ColumnConst* col_from_const =
195
3
                           check_and_get_column_const_string_or_fixedstring(column)) {
196
0
            auto value = col_from_const->get_value<TYPE_STRING>();
197
0
            for (size_t i = 0; i < input_rows_count; ++i) {
198
0
                if constexpr (ReturnType == TYPE_INT) {
199
0
                    col_to_data[i] =
200
0
                            HashUtil::xxHash32WithSeed(value.data(), value.size(), col_to_data[i]);
201
                } else {
202
                    col_to_data[i] =
203
                            HashUtil::xxHash64WithSeed(value.data(), value.size(), col_to_data[i]);
204
                }
205
0
            }
206
3
        } else if (const auto* vb_col = check_and_get_column<ColumnVarbinary>(column)) {
207
9
            for (size_t i = 0; i < input_rows_count; ++i) {
208
6
                auto data_ref = vb_col->get_data_at(i);
209
6
                if constexpr (ReturnType == TYPE_INT) {
210
6
                    col_to_data[i] = HashUtil::xxHash32WithSeed(data_ref.data, data_ref.size,
211
6
                                                                col_to_data[i]);
212
                } else {
213
                    col_to_data[i] = HashUtil::xxHash64WithSeed(data_ref.data, data_ref.size,
214
                                                                col_to_data[i]);
215
                }
216
6
            }
217
3
        } else {
218
0
            DCHECK(false);
219
0
            return Status::NotSupported("Illegal column {} of argument of function {}",
220
0
                                        column->get_name(), name);
221
0
        }
222
6
        return Status::OK();
223
6
    }
_ZN5doris10XxHashImplILNS_13PrimitiveTypeE6EE7executeILb1EEENS_6StatusEPKNS_9IDataTypeEPKNS_7IColumnEmRS8_
Line
Count
Source
171
6
                          IColumn& col_to) {
172
6
        auto& to_column = assert_cast<ColumnVector<ReturnType>&>(col_to);
173
6
        if constexpr (first) {
174
6
            to_column.insert_many_defaults(input_rows_count);
175
6
        }
176
6
        auto& col_to_data = to_column.get_data();
177
6
        if (const auto* col_from = check_and_get_column<ColumnString>(column)) {
178
3
            const typename ColumnString::Chars& data = col_from->get_chars();
179
3
            const typename ColumnString::Offsets& offsets = col_from->get_offsets();
180
3
            size_t size = offsets.size();
181
3
            ColumnString::Offset current_offset = 0;
182
9
            for (size_t i = 0; i < size; ++i) {
183
                if constexpr (ReturnType == TYPE_INT) {
184
                    col_to_data[i] = HashUtil::xxHash32WithSeed(
185
                            reinterpret_cast<const char*>(&data[current_offset]),
186
                            offsets[i] - current_offset, col_to_data[i]);
187
6
                } else {
188
6
                    col_to_data[i] = HashUtil::xxHash64WithSeed(
189
6
                            reinterpret_cast<const char*>(&data[current_offset]),
190
6
                            offsets[i] - current_offset, col_to_data[i]);
191
6
                }
192
6
                current_offset = offsets[i];
193
6
            }
194
3
        } else if (const ColumnConst* col_from_const =
195
3
                           check_and_get_column_const_string_or_fixedstring(column)) {
196
0
            auto value = col_from_const->get_value<TYPE_STRING>();
197
0
            for (size_t i = 0; i < input_rows_count; ++i) {
198
                if constexpr (ReturnType == TYPE_INT) {
199
                    col_to_data[i] =
200
                            HashUtil::xxHash32WithSeed(value.data(), value.size(), col_to_data[i]);
201
0
                } else {
202
0
                    col_to_data[i] =
203
0
                            HashUtil::xxHash64WithSeed(value.data(), value.size(), col_to_data[i]);
204
0
                }
205
0
            }
206
3
        } else if (const auto* vb_col = check_and_get_column<ColumnVarbinary>(column)) {
207
9
            for (size_t i = 0; i < input_rows_count; ++i) {
208
6
                auto data_ref = vb_col->get_data_at(i);
209
                if constexpr (ReturnType == TYPE_INT) {
210
                    col_to_data[i] = HashUtil::xxHash32WithSeed(data_ref.data, data_ref.size,
211
                                                                col_to_data[i]);
212
6
                } else {
213
6
                    col_to_data[i] = HashUtil::xxHash64WithSeed(data_ref.data, data_ref.size,
214
6
                                                                col_to_data[i]);
215
6
                }
216
6
            }
217
3
        } else {
218
0
            DCHECK(false);
219
0
            return Status::NotSupported("Illegal column {} of argument of function {}",
220
0
                                        column->get_name(), name);
221
0
        }
222
6
        return Status::OK();
223
6
    }
_ZN5doris10XxHashImplILNS_13PrimitiveTypeE6EE7executeILb0EEENS_6StatusEPKNS_9IDataTypeEPKNS_7IColumnEmRS8_
Line
Count
Source
171
6
                          IColumn& col_to) {
172
6
        auto& to_column = assert_cast<ColumnVector<ReturnType>&>(col_to);
173
        if constexpr (first) {
174
            to_column.insert_many_defaults(input_rows_count);
175
        }
176
6
        auto& col_to_data = to_column.get_data();
177
6
        if (const auto* col_from = check_and_get_column<ColumnString>(column)) {
178
3
            const typename ColumnString::Chars& data = col_from->get_chars();
179
3
            const typename ColumnString::Offsets& offsets = col_from->get_offsets();
180
3
            size_t size = offsets.size();
181
3
            ColumnString::Offset current_offset = 0;
182
9
            for (size_t i = 0; i < size; ++i) {
183
                if constexpr (ReturnType == TYPE_INT) {
184
                    col_to_data[i] = HashUtil::xxHash32WithSeed(
185
                            reinterpret_cast<const char*>(&data[current_offset]),
186
                            offsets[i] - current_offset, col_to_data[i]);
187
6
                } else {
188
6
                    col_to_data[i] = HashUtil::xxHash64WithSeed(
189
6
                            reinterpret_cast<const char*>(&data[current_offset]),
190
6
                            offsets[i] - current_offset, col_to_data[i]);
191
6
                }
192
6
                current_offset = offsets[i];
193
6
            }
194
3
        } else if (const ColumnConst* col_from_const =
195
3
                           check_and_get_column_const_string_or_fixedstring(column)) {
196
0
            auto value = col_from_const->get_value<TYPE_STRING>();
197
0
            for (size_t i = 0; i < input_rows_count; ++i) {
198
                if constexpr (ReturnType == TYPE_INT) {
199
                    col_to_data[i] =
200
                            HashUtil::xxHash32WithSeed(value.data(), value.size(), col_to_data[i]);
201
0
                } else {
202
0
                    col_to_data[i] =
203
0
                            HashUtil::xxHash64WithSeed(value.data(), value.size(), col_to_data[i]);
204
0
                }
205
0
            }
206
3
        } else if (const auto* vb_col = check_and_get_column<ColumnVarbinary>(column)) {
207
9
            for (size_t i = 0; i < input_rows_count; ++i) {
208
6
                auto data_ref = vb_col->get_data_at(i);
209
                if constexpr (ReturnType == TYPE_INT) {
210
                    col_to_data[i] = HashUtil::xxHash32WithSeed(data_ref.data, data_ref.size,
211
                                                                col_to_data[i]);
212
6
                } else {
213
6
                    col_to_data[i] = HashUtil::xxHash64WithSeed(data_ref.data, data_ref.size,
214
6
                                                                col_to_data[i]);
215
6
                }
216
6
            }
217
3
        } else {
218
0
            DCHECK(false);
219
0
            return Status::NotSupported("Illegal column {} of argument of function {}",
220
0
                                        column->get_name(), name);
221
0
        }
222
6
        return Status::OK();
223
6
    }
224
};
225
226
using FunctionXxHash_32 = FunctionVariadicArgumentsBase<DataTypeInt32, XxHashImpl<TYPE_INT>>;
227
using FunctionXxHash_64 = FunctionVariadicArgumentsBase<DataTypeInt64, XxHashImpl<TYPE_BIGINT>>;
228
229
1
void register_function_hash(SimpleFunctionFactory& factory) {
230
1
    factory.register_function<FunctionMurmurHash3_32>();
231
1
    factory.register_function<FunctionMurmurHash3_64>();
232
1
    factory.register_function<FunctionMurmurHash3_64_V2>();
233
1
    factory.register_function<FunctionXxHash_32>();
234
1
    factory.register_function<FunctionXxHash_64>();
235
1
    factory.register_alias("xxhash_64", "xxhash3_64");
236
1
}
237
} // namespace doris