1#include <Functions/IFunctionImpl.h>
2#include <Functions/FunctionFactory.h>
3#include <Functions/GatherUtils/GatherUtils.h>
4#include <DataTypes/DataTypeArray.h>
5#include <DataTypes/DataTypesNumber.h>
6#include <DataTypes/DataTypeNothing.h>
7#include <DataTypes/DataTypeNullable.h>
8#include <DataTypes/getLeastSupertype.h>
9#include <Columns/ColumnArray.h>
10#include <Columns/ColumnsNumber.h>
11#include <Columns/ColumnConst.h>
12#include <Interpreters/castColumn.h>
13#include <Common/typeid_cast.h>
14#include <ext/range.h>
15
16
17namespace DB
18{
19
20namespace ErrorCodes
21{
22 extern const int LOGICAL_ERROR;
23 extern const int ILLEGAL_TYPE_OF_ARGUMENT;
24}
25
26
27class FunctionArrayHasAllAny : public IFunction
28{
29public:
30 FunctionArrayHasAllAny(const Context & context_, bool all_, const char * name_)
31 : context(context_), all(all_), name(name_) {}
32
33 String getName() const override { return name; }
34
35 bool isVariadic() const override { return false; }
36 size_t getNumberOfArguments() const override { return 2; }
37
38 DataTypePtr getReturnTypeImpl(const DataTypes & arguments) const override
39 {
40 for (auto i : ext::range(0, arguments.size()))
41 {
42 auto array_type = typeid_cast<const DataTypeArray *>(arguments[i].get());
43 if (!array_type)
44 throw Exception("Argument " + std::to_string(i) + " for function " + getName() + " must be an array but it has type "
45 + arguments[i]->getName() + ".", ErrorCodes::ILLEGAL_TYPE_OF_ARGUMENT);
46 }
47
48 return std::make_shared<DataTypeUInt8>();
49 }
50
51 void executeImpl(Block & block, const ColumnNumbers & arguments, size_t result, size_t input_rows_count) override
52 {
53 size_t rows = input_rows_count;
54 size_t num_args = arguments.size();
55
56 DataTypePtr common_type = nullptr;
57 auto commonType = [&common_type, &block, &arguments]()
58 {
59 if (common_type == nullptr)
60 {
61 DataTypes data_types;
62 data_types.reserve(arguments.size());
63 for (const auto & argument : arguments)
64 data_types.push_back(block.getByPosition(argument).type);
65
66 common_type = getLeastSupertype(data_types);
67 }
68
69 return common_type;
70 };
71
72 Columns preprocessed_columns(num_args);
73
74 for (size_t i = 0; i < num_args; ++i)
75 {
76 const auto & argument = block.getByPosition(arguments[i]);
77 ColumnPtr preprocessed_column = argument.column;
78
79 const auto argument_type = typeid_cast<const DataTypeArray *>(argument.type.get());
80 const auto & nested_type = argument_type->getNestedType();
81
82 /// Converts Array(Nothing) or Array(Nullable(Nothing) to common type. Example: hasAll([Null, 1], [Null]) -> 1
83 if (typeid_cast<const DataTypeNothing *>(removeNullable(nested_type).get()))
84 preprocessed_column = castColumn(argument, commonType(), context);
85
86 preprocessed_columns[i] = std::move(preprocessed_column);
87 }
88
89 std::vector<std::unique_ptr<GatherUtils::IArraySource>> sources;
90
91 for (auto & argument_column : preprocessed_columns)
92 {
93 bool is_const = false;
94
95 if (auto argument_column_const = typeid_cast<const ColumnConst *>(argument_column.get()))
96 {
97 is_const = true;
98 argument_column = argument_column_const->getDataColumnPtr();
99 }
100
101 if (auto argument_column_array = typeid_cast<const ColumnArray *>(argument_column.get()))
102 sources.emplace_back(GatherUtils::createArraySource(*argument_column_array, is_const, rows));
103 else
104 throw Exception{"Arguments for function " + getName() + " must be arrays.", ErrorCodes::LOGICAL_ERROR};
105 }
106
107 auto result_column = ColumnUInt8::create(rows);
108 auto result_column_ptr = typeid_cast<ColumnUInt8 *>(result_column.get());
109 GatherUtils::sliceHas(*sources[0], *sources[1], all, *result_column_ptr);
110
111 block.getByPosition(result).column = std::move(result_column);
112 }
113
114 bool useDefaultImplementationForConstants() const override { return true; }
115
116private:
117 const Context & context;
118 bool all;
119 const char * name;
120};
121
122}
123