ClickHouse/dbms/src/Functions/arrayEnumerateRanked.cpp

95 lines
3.4 KiB
C++
Raw Normal View History

2019-03-05 21:53:16 +00:00
#include <algorithm>
#include <Columns/ColumnConst.h>
#include "arrayEnumerateRanked.h"
2019-03-05 21:53:16 +00:00
namespace DB
{
2019-03-05 21:53:16 +00:00
ArraysDepths getArraysDepths(const ColumnsWithTypeAndName & arguments)
{
const size_t num_arguments = arguments.size();
2019-03-05 21:53:16 +00:00
DepthType clear_depth = 1;
DepthTypes depths;
2019-03-05 21:53:16 +00:00
/// function signature is the following:
/// f(c0, arr1, c1, arr2, c2, ...)
///
/// c0 is something called "clear_depth" here.
/// cN... - how deep to look into the corresponding arrN, (called "depths" here)
/// may be omitted - then it means "look at the full depth".
size_t array_num = 0;
2019-03-05 21:53:16 +00:00
DepthType prev_array_depth = 0;
for (size_t i = 0; i < num_arguments; ++i)
{
2019-03-05 21:53:16 +00:00
const DataTypePtr & type = arguments[i].type;
const DataTypeArray * type_array = typeid_cast<const DataTypeArray *>(type.get());
2019-03-05 21:53:16 +00:00
if (type_array)
{
2019-03-05 21:53:16 +00:00
if (depths.size() < array_num && prev_array_depth)
{
2019-03-05 21:53:16 +00:00
depths.emplace_back(prev_array_depth);
prev_array_depth = 0;
}
2019-03-05 21:53:16 +00:00
prev_array_depth = type_array->getNumberOfDimensions();
++array_num;
}
2019-03-05 21:53:16 +00:00
else
{
const auto & depth_column = arguments[i].column;
if (depth_column && depth_column->isColumnConst())
{
2019-03-05 21:53:16 +00:00
UInt64 value = static_cast<const ColumnConst &>(*depth_column).getValue<UInt64>();
if (!value)
2019-03-05 21:53:16 +00:00
throw Exception("Incorrect arguments for function arrayEnumerateUniqRanked or arrayEnumerateDenseRanked: depth ("
+ std::to_string(value) + ") cannot be less or equal 0.",
ErrorCodes::BAD_ARGUMENTS);
if (i == 0)
{
clear_depth = value;
}
else
{
if (depths.size() >= array_num)
2019-03-05 21:53:16 +00:00
throw Exception("Incorrect arguments for function arrayEnumerateUniqRanked or arrayEnumerateDenseRanked: depth ("
+ std::to_string(value) + ") for missing array.",
ErrorCodes::BAD_ARGUMENTS);
if (value > prev_array_depth)
2019-03-06 19:19:21 +00:00
throw Exception(
"Arguments for function arrayEnumerateUniqRanked/arrayEnumerateDenseRanked incorrect: depth="
+ std::to_string(value) + " for array with depth=" + std::to_string(prev_array_depth) + ".",
2019-03-06 19:19:21 +00:00
ErrorCodes::BAD_ARGUMENTS);
2019-03-05 21:53:16 +00:00
depths.emplace_back(value);
}
}
}
}
2019-03-06 19:19:21 +00:00
if (depths.size() < array_num)
2019-03-05 21:53:16 +00:00
depths.emplace_back(prev_array_depth);
if (depths.empty())
2019-03-05 21:53:16 +00:00
throw Exception("Incorrect arguments for function arrayEnumerateUniqRanked or arrayEnumerateDenseRanked: at least one array should be passed.",
ErrorCodes::BAD_ARGUMENTS);
2019-03-05 21:53:16 +00:00
DepthType max_array_depth = 0;
for (auto depth : depths)
max_array_depth = std::max(depth, max_array_depth);
if (clear_depth > max_array_depth)
2019-03-05 21:53:16 +00:00
throw Exception("Incorrect arguments for function arrayEnumerateUniqRanked or arrayEnumerateDenseRanked: clear_depth ("
+ std::to_string(clear_depth) + ") cant be larger than max_array_depth (" + std::to_string(max_array_depth) + ").",
ErrorCodes::BAD_ARGUMENTS);
return {clear_depth, depths, max_array_depth};
}
}