2017-12-23 04:29:34 +00:00
|
|
|
#include <string.h>
|
|
|
|
|
2017-07-13 16:49:09 +00:00
|
|
|
#include <Common/typeid_cast.h>
|
2019-08-21 02:28:04 +00:00
|
|
|
#include <Common/assert_cast.h>
|
2018-01-15 19:07:47 +00:00
|
|
|
#include <Common/StringUtils/StringUtils.h>
|
2013-07-11 17:35:56 +00:00
|
|
|
|
2017-04-01 09:19:00 +00:00
|
|
|
#include <DataTypes/DataTypeArray.h>
|
2017-12-23 04:29:34 +00:00
|
|
|
#include <DataTypes/DataTypeTuple.h>
|
2017-12-25 18:58:39 +00:00
|
|
|
#include <DataTypes/NestedUtils.h>
|
2018-01-02 09:11:13 +00:00
|
|
|
|
|
|
|
#include <Columns/ColumnArray.h>
|
|
|
|
#include <Columns/ColumnTuple.h>
|
|
|
|
#include <Columns/ColumnConst.h>
|
2017-07-16 04:08:20 +00:00
|
|
|
|
|
|
|
#include <Parsers/IAST.h>
|
2013-08-14 16:52:40 +00:00
|
|
|
|
2013-07-11 17:35:56 +00:00
|
|
|
|
|
|
|
namespace DB
|
|
|
|
{
|
|
|
|
|
2018-09-12 21:08:55 +00:00
|
|
|
namespace ErrorCodes
|
|
|
|
{
|
|
|
|
extern const int ILLEGAL_COLUMN;
|
|
|
|
extern const int SIZES_OF_ARRAYS_DOESNT_MATCH;
|
|
|
|
}
|
|
|
|
|
2017-12-25 18:58:39 +00:00
|
|
|
namespace Nested
|
|
|
|
{
|
2013-07-11 17:35:56 +00:00
|
|
|
|
2017-12-25 18:58:39 +00:00
|
|
|
std::string concatenateName(const std::string & nested_table_name, const std::string & nested_field_name)
|
2013-07-16 14:55:01 +00:00
|
|
|
{
|
2017-04-01 07:20:54 +00:00
|
|
|
return nested_table_name + "." + nested_field_name;
|
2013-07-16 14:55:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2018-01-02 05:54:34 +00:00
|
|
|
/** Name can be treated as compound if and only if both parts are simple identifiers.
|
|
|
|
*/
|
|
|
|
std::pair<std::string, std::string> splitName(const std::string & name)
|
2013-07-16 14:55:01 +00:00
|
|
|
{
|
2018-01-02 05:54:34 +00:00
|
|
|
const char * begin = name.data();
|
|
|
|
const char * pos = begin;
|
|
|
|
const char * end = begin + name.size();
|
|
|
|
|
|
|
|
if (pos >= end || !isValidIdentifierBegin(*pos))
|
|
|
|
return {name, {}};
|
|
|
|
|
|
|
|
++pos;
|
|
|
|
|
|
|
|
while (pos < end && isWordCharASCII(*pos))
|
|
|
|
++pos;
|
|
|
|
|
|
|
|
if (pos >= end || *pos != '.')
|
|
|
|
return {name, {}};
|
|
|
|
|
|
|
|
const char * first_end = pos;
|
|
|
|
++pos;
|
|
|
|
const char * second_begin = pos;
|
|
|
|
|
|
|
|
if (pos >= end || !isValidIdentifierBegin(*pos))
|
|
|
|
return {name, {}};
|
|
|
|
|
|
|
|
++pos;
|
|
|
|
|
|
|
|
while (pos < end && isWordCharASCII(*pos))
|
|
|
|
++pos;
|
|
|
|
|
|
|
|
if (pos != end)
|
|
|
|
return {name, {}};
|
|
|
|
|
|
|
|
return {{ begin, first_end }, { second_begin, end }};
|
2013-07-16 14:55:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2018-01-02 05:54:34 +00:00
|
|
|
std::string extractTableName(const std::string & nested_name)
|
2013-07-30 17:25:46 +00:00
|
|
|
{
|
2018-01-02 05:54:34 +00:00
|
|
|
auto splitted = splitName(nested_name);
|
|
|
|
return splitted.first;
|
2013-07-30 17:25:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2018-01-02 09:11:13 +00:00
|
|
|
Block flatten(const Block & block)
|
|
|
|
{
|
|
|
|
Block res;
|
|
|
|
|
|
|
|
for (const auto & elem : block)
|
|
|
|
{
|
|
|
|
if (const DataTypeArray * type_arr = typeid_cast<const DataTypeArray *>(elem.type.get()))
|
|
|
|
{
|
|
|
|
if (const DataTypeTuple * type_tuple = typeid_cast<const DataTypeTuple *>(type_arr->getNestedType().get()))
|
|
|
|
{
|
|
|
|
const DataTypes & element_types = type_tuple->getElements();
|
|
|
|
const Strings & names = type_tuple->getElementNames();
|
|
|
|
size_t tuple_size = element_types.size();
|
|
|
|
|
2019-06-27 19:28:52 +00:00
|
|
|
bool is_const = isColumnConst(*elem.column);
|
2018-01-02 09:11:13 +00:00
|
|
|
const ColumnArray * column_array;
|
|
|
|
if (is_const)
|
2019-08-21 02:28:04 +00:00
|
|
|
column_array = typeid_cast<const ColumnArray *>(&assert_cast<const ColumnConst &>(*elem.column).getDataColumn());
|
2018-01-02 09:11:13 +00:00
|
|
|
else
|
|
|
|
column_array = typeid_cast<const ColumnArray *>(elem.column.get());
|
|
|
|
|
|
|
|
const ColumnPtr & column_offsets = column_array->getOffsetsPtr();
|
|
|
|
|
|
|
|
const ColumnTuple & column_tuple = typeid_cast<const ColumnTuple &>(column_array->getData());
|
2019-03-25 01:43:54 +00:00
|
|
|
const auto & element_columns = column_tuple.getColumns();
|
2018-01-02 09:11:13 +00:00
|
|
|
|
|
|
|
for (size_t i = 0; i < tuple_size; ++i)
|
|
|
|
{
|
|
|
|
String nested_name = concatenateName(elem.name, names[i]);
|
|
|
|
ColumnPtr column_array_of_element = ColumnArray::create(element_columns[i], column_offsets);
|
|
|
|
|
|
|
|
res.insert(ColumnWithTypeAndName(
|
|
|
|
is_const
|
|
|
|
? ColumnConst::create(std::move(column_array_of_element), block.rows())
|
|
|
|
: std::move(column_array_of_element),
|
|
|
|
std::make_shared<DataTypeArray>(element_types[i]),
|
|
|
|
nested_name));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
res.insert(elem);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
res.insert(elem);
|
|
|
|
}
|
|
|
|
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2017-12-25 21:57:29 +00:00
|
|
|
NamesAndTypesList collect(const NamesAndTypesList & names_and_types)
|
2017-12-25 21:10:46 +00:00
|
|
|
{
|
2018-01-02 05:54:34 +00:00
|
|
|
NamesAndTypesList res;
|
|
|
|
|
|
|
|
std::map<std::string, NamesAndTypesList> nested;
|
|
|
|
for (const auto & name_type : names_and_types)
|
|
|
|
{
|
|
|
|
bool collected = false;
|
|
|
|
if (const DataTypeArray * type_arr = typeid_cast<const DataTypeArray *>(name_type.type.get()))
|
|
|
|
{
|
|
|
|
auto splitted = splitName(name_type.name);
|
|
|
|
if (!splitted.second.empty())
|
|
|
|
{
|
|
|
|
nested[splitted.first].emplace_back(splitted.second, type_arr->getNestedType());
|
|
|
|
collected = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!collected)
|
|
|
|
res.push_back(name_type);
|
|
|
|
}
|
|
|
|
|
|
|
|
for (const auto & name_elems : nested)
|
|
|
|
res.emplace_back(name_elems.first, std::make_shared<DataTypeArray>(
|
|
|
|
std::make_shared<DataTypeTuple>(name_elems.second.getTypes(), name_elems.second.getNames())));
|
|
|
|
|
|
|
|
return res;
|
2013-08-14 16:52:40 +00:00
|
|
|
}
|
2015-10-04 03:17:36 +00:00
|
|
|
|
2018-09-12 21:08:55 +00:00
|
|
|
|
|
|
|
void validateArraySizes(const Block & block)
|
|
|
|
{
|
|
|
|
/// Nested prefix -> position of first column in block.
|
|
|
|
std::map<std::string, size_t> nested;
|
|
|
|
|
|
|
|
for (size_t i = 0, size = block.columns(); i < size; ++i)
|
|
|
|
{
|
|
|
|
const auto & elem = block.getByPosition(i);
|
|
|
|
|
2018-09-12 22:44:42 +00:00
|
|
|
if (isArray(elem.type))
|
2018-09-12 21:08:55 +00:00
|
|
|
{
|
|
|
|
if (!typeid_cast<const ColumnArray *>(elem.column.get()))
|
|
|
|
throw Exception("Column with Array type is not represented by ColumnArray column: " + elem.column->dumpStructure(), ErrorCodes::ILLEGAL_COLUMN);
|
|
|
|
|
|
|
|
auto splitted = splitName(elem.name);
|
|
|
|
|
|
|
|
/// Is it really a column of Nested data structure.
|
|
|
|
if (!splitted.second.empty())
|
|
|
|
{
|
|
|
|
auto [it, inserted] = nested.emplace(splitted.first, i);
|
|
|
|
|
|
|
|
/// It's not the first column of Nested data structure.
|
|
|
|
if (!inserted)
|
|
|
|
{
|
2019-08-21 02:28:04 +00:00
|
|
|
const ColumnArray & first_array_column = assert_cast<const ColumnArray &>(*block.getByPosition(it->second).column);
|
|
|
|
const ColumnArray & another_array_column = assert_cast<const ColumnArray &>(*elem.column);
|
2018-09-12 21:08:55 +00:00
|
|
|
|
|
|
|
if (!first_array_column.hasEqualOffsets(another_array_column))
|
|
|
|
throw Exception("Elements '" + block.getByPosition(it->second).name
|
|
|
|
+ "' and '" + elem.name
|
|
|
|
+ "' of Nested data structure '" + splitted.first
|
|
|
|
+ "' (Array columns) have different array sizes.", ErrorCodes::SIZES_OF_ARRAYS_DOESNT_MATCH);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-07-16 04:08:20 +00:00
|
|
|
}
|
|
|
|
|
2013-07-11 17:35:56 +00:00
|
|
|
}
|