1#include <algorithm>
2#include <Columns/ColumnConst.h>
3#include <Common/assert_cast.h>
4#include "arrayEnumerateRanked.h"
5
6
7namespace DB
8{
9
10namespace ErrorCodes
11{
12 extern const int BAD_ARGUMENTS;
13}
14
15ArraysDepths getArraysDepths(const ColumnsWithTypeAndName & arguments)
16{
17 const size_t num_arguments = arguments.size();
18
19 DepthType clear_depth = 1;
20 DepthTypes depths;
21
22 /// function signature is the following:
23 /// f(c0, arr1, c1, arr2, c2, ...)
24 ///
25 /// c0 is something called "clear_depth" here.
26 /// cN... - how deep to look into the corresponding arrN, (called "depths" here)
27 /// may be omitted - then it means "look at the full depth".
28
29 size_t array_num = 0;
30 DepthType prev_array_depth = 0;
31 for (size_t i = 0; i < num_arguments; ++i)
32 {
33 const DataTypePtr & type = arguments[i].type;
34 const DataTypeArray * type_array = typeid_cast<const DataTypeArray *>(type.get());
35
36 if (type_array)
37 {
38 if (depths.size() < array_num && prev_array_depth)
39 {
40 depths.emplace_back(prev_array_depth);
41 prev_array_depth = 0;
42 }
43
44 prev_array_depth = type_array->getNumberOfDimensions();
45 ++array_num;
46 }
47 else
48 {
49 const auto & depth_column = arguments[i].column;
50
51 if (depth_column && isColumnConst(*depth_column))
52 {
53 UInt64 value = assert_cast<const ColumnConst &>(*depth_column).getValue<UInt64>();
54 if (!value)
55 throw Exception("Incorrect arguments for function arrayEnumerateUniqRanked or arrayEnumerateDenseRanked: depth ("
56 + std::to_string(value) + ") cannot be less or equal 0.",
57 ErrorCodes::BAD_ARGUMENTS);
58
59 if (i == 0)
60 {
61 clear_depth = value;
62 }
63 else
64 {
65 if (depths.size() >= array_num)
66 throw Exception("Incorrect arguments for function arrayEnumerateUniqRanked or arrayEnumerateDenseRanked: depth ("
67 + std::to_string(value) + ") for missing array.",
68 ErrorCodes::BAD_ARGUMENTS);
69 if (value > prev_array_depth)
70 throw Exception(
71 "Arguments for function arrayEnumerateUniqRanked/arrayEnumerateDenseRanked incorrect: depth="
72 + std::to_string(value) + " for array with depth=" + std::to_string(prev_array_depth) + ".",
73 ErrorCodes::BAD_ARGUMENTS);
74
75 depths.emplace_back(value);
76 }
77 }
78 }
79 }
80
81 if (depths.size() < array_num)
82 depths.emplace_back(prev_array_depth);
83
84 if (depths.empty())
85 throw Exception("Incorrect arguments for function arrayEnumerateUniqRanked or arrayEnumerateDenseRanked: at least one array should be passed.",
86 ErrorCodes::BAD_ARGUMENTS);
87
88 DepthType max_array_depth = 0;
89 for (auto depth : depths)
90 max_array_depth = std::max(depth, max_array_depth);
91
92 if (clear_depth > max_array_depth)
93 throw Exception("Incorrect arguments for function arrayEnumerateUniqRanked or arrayEnumerateDenseRanked: clear_depth ("
94 + std::to_string(clear_depth) + ") cant be larger than max_array_depth (" + std::to_string(max_array_depth) + ").",
95 ErrorCodes::BAD_ARGUMENTS);
96
97 return {clear_depth, depths, max_array_depth};
98}
99
100}
101