1#include <DataTypes/DataTypesNumber.h>
2#include <Columns/ColumnsNumber.h>
3#include "FunctionArrayMapped.h"
4#include <Functions/FunctionFactory.h>
5
6
7namespace DB
8{
9
10/** arrayAll(x1,...,xn -> expression, array1,...,arrayn) - is the expression true for all elements of the array.
11 * An overload of the form f(array) is available, which works in the same way as f(x -> x, array).
12 */
13struct ArrayAllImpl
14{
15 static bool needBoolean() { return true; }
16 static bool needExpression() { return false; }
17 static bool needOneArray() { return false; }
18
19 static DataTypePtr getReturnType(const DataTypePtr & /*expression_return*/, const DataTypePtr & /*array_element*/)
20 {
21 return std::make_shared<DataTypeUInt8>();
22 }
23
24 static ColumnPtr execute(const ColumnArray & array, ColumnPtr mapped)
25 {
26 const ColumnUInt8 * column_filter = typeid_cast<const ColumnUInt8 *>(&*mapped);
27
28 if (!column_filter)
29 {
30 auto column_filter_const = checkAndGetColumnConst<ColumnUInt8>(&*mapped);
31
32 if (!column_filter_const)
33 throw Exception("Unexpected type of filter column", ErrorCodes::ILLEGAL_COLUMN);
34
35 if (column_filter_const->getValue<UInt8>())
36 return DataTypeUInt8().createColumnConst(array.size(), 1u);
37 else
38 {
39 const IColumn::Offsets & offsets = array.getOffsets();
40 auto out_column = ColumnUInt8::create(offsets.size());
41 ColumnUInt8::Container & out_all = out_column->getData();
42
43 size_t pos = 0;
44 for (size_t i = 0; i < offsets.size(); ++i)
45 {
46 out_all[i] = offsets[i] == pos;
47 pos = offsets[i];
48 }
49
50 return out_column;
51 }
52 }
53
54 const IColumn::Filter & filter = column_filter->getData();
55 const IColumn::Offsets & offsets = array.getOffsets();
56 auto out_column = ColumnUInt8::create(offsets.size());
57 ColumnUInt8::Container & out_all = out_column->getData();
58
59 size_t pos = 0;
60 for (size_t i = 0; i < offsets.size(); ++i)
61 {
62 UInt8 all = 1;
63 for (; pos < offsets[i]; ++pos)
64 {
65 if (!filter[pos])
66 {
67 all = 0;
68 pos = offsets[i];
69 break;
70 }
71 }
72 out_all[i] = all;
73 }
74
75 return out_column;
76 }
77};
78
79struct NameArrayAll { static constexpr auto name = "arrayAll"; };
80using FunctionArrayAll = FunctionArrayMapped<ArrayAllImpl, NameArrayAll>;
81
82void registerFunctionArrayAll(FunctionFactory & factory)
83{
84 factory.registerFunction<FunctionArrayAll>();
85}
86
87}
88
89
90