From 6c0ac12dbb73a9139f5c88bf15269728567d3f70 Mon Sep 17 00:00:00 2001 From: Anton Popov Date: Mon, 24 May 2021 23:15:04 +0300 Subject: [PATCH] fix tuples in 'CREATE .. AS SELECT' queries --- src/DataTypes/DataTypeTuple.cpp | 14 +++++++++----- .../0_stateless/01881_create_as_tuple.reference | 6 ++++++ .../queries/0_stateless/01881_create_as_tuple.sql | 14 ++++++++++++++ 3 files changed, 29 insertions(+), 5 deletions(-) create mode 100644 tests/queries/0_stateless/01881_create_as_tuple.reference create mode 100644 tests/queries/0_stateless/01881_create_as_tuple.sql diff --git a/src/DataTypes/DataTypeTuple.cpp b/src/DataTypes/DataTypeTuple.cpp index b30efb163ab..9ebc061d6f9 100644 --- a/src/DataTypes/DataTypeTuple.cpp +++ b/src/DataTypes/DataTypeTuple.cpp @@ -307,26 +307,30 @@ SerializationPtr DataTypeTuple::getSubcolumnSerialization( SerializationPtr DataTypeTuple::doGetDefaultSerialization() const { SerializationTuple::ElementSerializations serializations(elems.size()); + bool use_explicit_names = have_explicit_names && serialize_names; 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(); - serializations[i] = std::make_shared(serialization, names[i]); + serializations[i] = std::make_shared(serialization, elem_name); } - return std::make_shared(std::move(serializations), have_explicit_names); + return std::make_shared(std::move(serializations), use_explicit_names); } SerializationPtr DataTypeTuple::getSerialization(const String & column_name, const StreamExistenceCallback & callback) const { SerializationTuple::ElementSerializations serializations(elems.size()); + bool use_explicit_names = have_explicit_names && serialize_names; 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); - serializations[i] = std::make_shared(serializaion, names[i]); + serializations[i] = std::make_shared(serializaion, elem_name); } - return std::make_shared(std::move(serializations), have_explicit_names); + return std::make_shared(std::move(serializations), use_explicit_names); } static DataTypePtr create(const ASTPtr & arguments) diff --git a/tests/queries/0_stateless/01881_create_as_tuple.reference b/tests/queries/0_stateless/01881_create_as_tuple.reference new file mode 100644 index 00000000000..76109462c59 --- /dev/null +++ b/tests/queries/0_stateless/01881_create_as_tuple.reference @@ -0,0 +1,6 @@ +0 [('string',0)] +1 [('string',1)] +2 [('string',2)] +0 [('string',0)] +1 [('string',1)] +2 [('string',2)] diff --git a/tests/queries/0_stateless/01881_create_as_tuple.sql b/tests/queries/0_stateless/01881_create_as_tuple.sql new file mode 100644 index 00000000000..cf370fe4633 --- /dev/null +++ b/tests/queries/0_stateless/01881_create_as_tuple.sql @@ -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;