Backport #70103 to 24.9: Avoid reusing columns among different named tuples

This commit is contained in:
robot-clickhouse 2024-10-21 04:08:54 +00:00
parent 18c6380ccb
commit b11bf72b71
3 changed files with 53 additions and 0 deletions

View File

@ -38,6 +38,7 @@ namespace DB
{
namespace Setting
{
extern const SettingsBool enable_named_columns_in_function_tuple;
extern const SettingsBool transform_null_in;
}
@ -182,6 +183,33 @@ public:
break;
}
if (planner_context.getQueryContext()->getSettingsRef()[Setting::enable_named_columns_in_function_tuple])
{
/// Function "tuple" which generates named tuple should use argument aliases to construct its name.
if (function_node.getFunctionName() == "tuple")
{
if (const DataTypeTuple * type_tuple = typeid_cast<const DataTypeTuple *>(function_node.getResultType().get()))
{
if (type_tuple->haveExplicitNames())
{
const auto & names = type_tuple->getElementNames();
size_t size = names.size();
WriteBufferFromOwnString s;
s << "tuple(";
for (size_t i = 0; i < size; ++i)
{
if (i != 0)
s << ", ";
s << backQuoteIfNeed(names[i]);
}
s << ")";
result = s.str();
break;
}
}
}
}
String in_function_second_argument_node_name;
if (isNameOfInFunction(function_node.getFunctionName()))

View File

@ -0,0 +1,3 @@
1 ('dete','ok') ('dete','ok')
{"id":1,"a":{"col_a":"dete","type":"ok"},"b":{"col_b":"dete","type":"ok"}}
{"id":1,"a":{"col_a":"dete","type":"ok"},"b":{"col_b":"dete","type":"ok"}}

View File

@ -0,0 +1,22 @@
SET enable_analyzer = 1;
SET enable_named_columns_in_function_tuple = 1;
DROP TABLE IF EXISTS src;
DROP TABLE IF EXISTS dst;
CREATE TABLE src (id UInt32, type String, data String) ENGINE=MergeTree ORDER BY tuple();
CREATE TABLE dst (id UInt32, a Tuple (col_a Nullable(String), type String), b Tuple (col_b Nullable(String), type String)) ENGINE = MergeTree ORDER BY id;
INSERT INTO src VALUES (1, 'ok', 'data');
INSERT INTO dst (id, a, b) SELECT id, tuple(replaceAll(data, 'a', 'e') AS col_a, type) AS a, tuple(replaceAll(data, 'a', 'e') AS col_b, type) AS b FROM src;
SELECT * FROM dst;
DROP TABLE src;
DROP TABLE dst;
DROP TABLE IF EXISTS src;
CREATE TABLE src (id UInt32, type String, data String) ENGINE=MergeTree ORDER BY tuple();
INSERT INTO src VALUES (1, 'ok', 'data');
SELECT id, tuple(replaceAll(data, 'a', 'e') AS col_a, type) AS a, tuple(replaceAll(data, 'a', 'e') AS col_b, type) AS b FROM cluster(test_cluster_two_shards, currentDatabase(), src) SETTINGS prefer_localhost_replica=0 FORMAT JSONEachRow;
DROP TABLE src;