Skip to content

Commit

Permalink
Merge pull request #70868 from ClickHouse/cherrypick/24.7/688c9717db9…
Browse files Browse the repository at this point in the history
…0cf1c8f242c3fe583d6add0a86718

Cherry pick #70103 to 24.7: Avoid reusing columns among different named tuples
  • Loading branch information
robot-clickhouse authored Oct 21, 2024
2 parents 0cbe01a + 4cfc569 commit f3a6430
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 0 deletions.
31 changes: 31 additions & 0 deletions src/Planner/PlannerActionsVisitor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,10 @@

namespace DB
{
namespace Setting
{
extern const SettingsBool enable_named_columns_in_function_tuple;
}

namespace ErrorCodes
{
Expand Down Expand Up @@ -178,6 +182,33 @@ class ActionNodeNameHelper
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()))
Expand Down
Original file line number Diff line number Diff line change
@@ -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"}}
22 changes: 22 additions & 0 deletions tests/queries/0_stateless/03240_insert_select_named_tuple.sql
Original file line number Diff line number Diff line change
@@ -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;

0 comments on commit f3a6430

Please sign in to comment.