fix tuples in 'CREATE .. AS SELECT' queries

This commit is contained in:
Anton Popov 2021-05-24 23:15:04 +03:00
parent 83ef34374c
commit 6c0ac12dbb
3 changed files with 29 additions and 5 deletions

View File

@ -307,26 +307,30 @@ SerializationPtr DataTypeTuple::getSubcolumnSerialization(
SerializationPtr DataTypeTuple::doGetDefaultSerialization() const SerializationPtr DataTypeTuple::doGetDefaultSerialization() const
{ {
SerializationTuple::ElementSerializations serializations(elems.size()); SerializationTuple::ElementSerializations serializations(elems.size());
bool use_explicit_names = have_explicit_names && serialize_names;
for (size_t i = 0; i < elems.size(); ++i) for (size_t i = 0; i < elems.size(); ++i)
{ {
String elem_name = use_explicit_names ? names[i] : toString(i + 1);
auto serialization = elems[i]->getDefaultSerialization(); auto serialization = elems[i]->getDefaultSerialization();
serializations[i] = std::make_shared<SerializationTupleElement>(serialization, names[i]); serializations[i] = std::make_shared<SerializationTupleElement>(serialization, elem_name);
} }
return std::make_shared<SerializationTuple>(std::move(serializations), have_explicit_names); return std::make_shared<SerializationTuple>(std::move(serializations), use_explicit_names);
} }
SerializationPtr DataTypeTuple::getSerialization(const String & column_name, const StreamExistenceCallback & callback) const SerializationPtr DataTypeTuple::getSerialization(const String & column_name, const StreamExistenceCallback & callback) const
{ {
SerializationTuple::ElementSerializations serializations(elems.size()); SerializationTuple::ElementSerializations serializations(elems.size());
bool use_explicit_names = have_explicit_names && serialize_names;
for (size_t i = 0; i < elems.size(); ++i) for (size_t i = 0; i < elems.size(); ++i)
{ {
auto subcolumn_name = Nested::concatenateName(column_name, names[i]); String elem_name = use_explicit_names ? names[i] : toString(i + 1);
auto subcolumn_name = Nested::concatenateName(column_name, elem_name);
auto serializaion = elems[i]->getSerialization(subcolumn_name, callback); auto serializaion = elems[i]->getSerialization(subcolumn_name, callback);
serializations[i] = std::make_shared<SerializationTupleElement>(serializaion, names[i]); serializations[i] = std::make_shared<SerializationTupleElement>(serializaion, elem_name);
} }
return std::make_shared<SerializationTuple>(std::move(serializations), have_explicit_names); return std::make_shared<SerializationTuple>(std::move(serializations), use_explicit_names);
} }
static DataTypePtr create(const ASTPtr & arguments) static DataTypePtr create(const ASTPtr & arguments)

View File

@ -0,0 +1,6 @@
0 [('string',0)]
1 [('string',1)]
2 [('string',2)]
0 [('string',0)]
1 [('string',1)]
2 [('string',2)]

View File

@ -0,0 +1,14 @@
DROP TABLE IF EXISTS t_create_as_tuple;
CREATE TABLE t_create_as_tuple ENGINE = MergeTree()
ORDER BY number AS
SELECT number, [('string',number)] AS array FROM numbers(3);
SELECT * FROM t_create_as_tuple ORDER BY number;
DETACH TABLE t_create_as_tuple;
ATTACH TABLE t_create_as_tuple;
SELECT * FROM t_create_as_tuple ORDER BY number;
DROP TABLE t_create_as_tuple;