ClickHouse

Форк
0
183 строки · 7.6 Кб
1
#include <Functions/IFunction.h>
2
#include <Functions/FunctionFactory.h>
3
#include <Functions/FunctionHelpers.h>
4
#include <DataTypes/DataTypeTuple.h>
5
#include <DataTypes/DataTypesNumber.h>
6
#include <Columns/ColumnConst.h>
7
#include <Columns/ColumnsNumber.h>
8
#include <Columns/ColumnTuple.h>
9
#include <Columns/ColumnSet.h>
10
#include <Interpreters/Set.h>
11

12

13
namespace DB
14
{
15
namespace ErrorCodes
16
{
17
    extern const int ILLEGAL_COLUMN;
18
    extern const int LOGICAL_ERROR;
19
}
20

21
namespace
22
{
23

24
/** in(x, set) - function for evaluating the IN
25
  * notIn(x, set) - and NOT IN.
26
  */
27

28
template <bool negative, bool global, bool null_is_skipped, bool ignore_set>
29
struct FunctionInName;
30

31
template <> struct FunctionInName<false, false, true, false> { static constexpr auto name = "in"; };
32
template <> struct FunctionInName<false, true, true, false> { static constexpr auto name = "globalIn"; };
33
template <> struct FunctionInName<true, false, true, false> { static constexpr auto name = "notIn"; };
34
template <> struct FunctionInName<true, true, true, false> { static constexpr auto name = "globalNotIn"; };
35
template <> struct FunctionInName<false, false, false, false> { static constexpr auto name = "nullIn"; };
36
template <> struct FunctionInName<false, true, false, false> { static constexpr auto name = "globalNullIn"; };
37
template <> struct FunctionInName<true, false, false, false> { static constexpr auto name = "notNullIn"; };
38
template <> struct FunctionInName<true, true, false, false> { static constexpr auto name = "globalNotNullIn"; };
39
template <> struct FunctionInName<false, false, true, true> { static constexpr auto name = "inIgnoreSet"; };
40
template <> struct FunctionInName<false, true, true, true> { static constexpr auto name = "globalInIgnoreSet"; };
41
template <> struct FunctionInName<true, false, true, true> { static constexpr auto name = "notInIgnoreSet"; };
42
template <> struct FunctionInName<true, true, true, true> { static constexpr auto name = "globalNotInIgnoreSet"; };
43
template <> struct FunctionInName<false, false, false, true> { static constexpr auto name = "nullInIgnoreSet"; };
44
template <> struct FunctionInName<false, true, false, true> { static constexpr auto name = "globalNullInIgnoreSet"; };
45
template <> struct FunctionInName<true, false, false, true> { static constexpr auto name = "notNullInIgnoreSet"; };
46
template <> struct FunctionInName<true, true, false, true> { static constexpr auto name = "globalNotNullInIgnoreSet"; };
47

48
template <bool negative, bool global, bool null_is_skipped, bool ignore_set>
49
class FunctionIn : public IFunction
50
{
51
public:
52
    /// ignore_set flag means that we don't use set from the second argument, just return zero column.
53
    /// It is needed to perform type analysis without creation of set.
54
    static constexpr auto name = FunctionInName<negative, global, null_is_skipped, ignore_set>::name;
55

56
    static FunctionPtr create(ContextPtr)
57
    {
58
        return std::make_shared<FunctionIn>();
59
    }
60

61
    String getName() const override
62
    {
63
        return name;
64
    }
65

66
    size_t getNumberOfArguments() const override
67
    {
68
        return 2;
69
    }
70

71
    DataTypePtr getReturnTypeImpl(const DataTypes & /*arguments*/) const override
72
    {
73
        return std::make_shared<DataTypeUInt8>();
74
    }
75

76
    bool useDefaultImplementationForConstants() const override
77
    {
78
        /// Never return constant for -IgnoreSet functions to avoid constant folding.
79
        return !ignore_set;
80
    }
81

82
    bool useDefaultImplementationForNulls() const override { return null_is_skipped; }
83

84
    bool isSuitableForShortCircuitArgumentsExecution(const DataTypesWithConstInfo & /*arguments*/) const override { return false; }
85

86
    ColumnPtr executeImpl(const ColumnsWithTypeAndName & arguments, const DataTypePtr &, [[maybe_unused]] size_t input_rows_count) const override
87
    {
88
        if constexpr (ignore_set)
89
            return ColumnUInt8::create(input_rows_count, 0u);
90
        if (input_rows_count == 0)
91
            return ColumnUInt8::create();
92

93
        /// Second argument must be ColumnSet.
94
        ColumnPtr column_set_ptr = arguments[1].column;
95
        const ColumnSet * column_set = checkAndGetColumnConstData<const ColumnSet>(column_set_ptr.get());
96
        if (!column_set)
97
            column_set = checkAndGetColumn<const ColumnSet>(column_set_ptr.get());
98
        if (!column_set)
99
            throw Exception(ErrorCodes::ILLEGAL_COLUMN, "Second argument for function '{}' must be Set; found {}",
100
                getName(), column_set_ptr->getName());
101

102
        ColumnsWithTypeAndName columns_of_key_columns;
103

104
        /// First argument may be a tuple or a single column.
105
        const ColumnWithTypeAndName & left_arg = arguments[0];
106
        const ColumnTuple * tuple = typeid_cast<const ColumnTuple *>(left_arg.column.get());
107
        const ColumnConst * const_tuple = checkAndGetColumnConst<ColumnTuple>(left_arg.column.get());
108
        const DataTypeTuple * type_tuple = typeid_cast<const DataTypeTuple *>(left_arg.type.get());
109

110
        ColumnPtr materialized_tuple;
111
        if (const_tuple)
112
        {
113
            materialized_tuple = const_tuple->convertToFullColumn();
114
            tuple = typeid_cast<const ColumnTuple *>(materialized_tuple.get());
115
        }
116

117
        auto future_set = column_set->getData();
118
        if (!future_set)
119
            throw Exception(ErrorCodes::LOGICAL_ERROR, "No Set is passed as the second argument for function '{}'", getName());
120

121
        auto set = future_set->get();
122
        if (!set)
123
            throw Exception(ErrorCodes::LOGICAL_ERROR, "Not-ready Set is passed as the second argument for function '{}'", getName());
124

125
        auto set_types = set->getDataTypes();
126

127
        if (tuple && set_types.size() != 1 && set_types.size() == tuple->tupleSize())
128
        {
129
            const auto & tuple_columns = tuple->getColumns();
130
            const DataTypes & tuple_types = type_tuple->getElements();
131
            size_t tuple_size = tuple_columns.size();
132
            for (size_t i = 0; i < tuple_size; ++i)
133
                columns_of_key_columns.emplace_back(tuple_columns[i], tuple_types[i], "_" + toString(i));
134
        }
135
        else
136
            columns_of_key_columns.emplace_back(left_arg);
137

138
        bool is_const = false;
139
        if (columns_of_key_columns.size() == 1)
140
        {
141
            auto & arg = columns_of_key_columns.at(0);
142
            const auto * col = arg.column.get();
143
            if (const auto * const_col = typeid_cast<const ColumnConst *>(col))
144
            {
145
                col = &const_col->getDataColumn();
146
                is_const = true;
147
            }
148
        }
149

150
        auto res = set->execute(columns_of_key_columns, negative);
151

152
        if (is_const)
153
            res = ColumnUInt8::create(input_rows_count, res->getUInt(0));
154

155
        if (res->size() != input_rows_count)
156
            throw Exception(ErrorCodes::LOGICAL_ERROR, "Output size is different from input size, expect {}, get {}", input_rows_count, res->size());
157

158
        return res;
159
    }
160
};
161

162
template<bool ignore_set>
163
void registerFunctionsInImpl(FunctionFactory & factory)
164
{
165
    factory.registerFunction<FunctionIn<false, false, true, ignore_set>>();
166
    factory.registerFunction<FunctionIn<false, true, true, ignore_set>>();
167
    factory.registerFunction<FunctionIn<true, false, true, ignore_set>>();
168
    factory.registerFunction<FunctionIn<true, true, true, ignore_set>>();
169
    factory.registerFunction<FunctionIn<false, false, false, ignore_set>>();
170
    factory.registerFunction<FunctionIn<false, true, false, ignore_set>>();
171
    factory.registerFunction<FunctionIn<true, false, false, ignore_set>>();
172
    factory.registerFunction<FunctionIn<true, true, false, ignore_set>>();
173
}
174

175
}
176

177
REGISTER_FUNCTION(In)
178
{
179
    registerFunctionsInImpl<false>(factory);
180
    registerFunctionsInImpl<true>(factory);
181
}
182

183
}
184

Использование cookies

Мы используем файлы cookie в соответствии с Политикой конфиденциальности и Политикой использования cookies.

Нажимая кнопку «Принимаю», Вы даете АО «СберТех» согласие на обработку Ваших персональных данных в целях совершенствования нашего веб-сайта и Сервиса GitVerse, а также повышения удобства их использования.

Запретить использование cookies Вы можете самостоятельно в настройках Вашего браузера.