From ef3102b09cda18d11fdd7649ae0b3cb1895a8adb Mon Sep 17 00:00:00 2001 From: Amos Bird Date: Mon, 7 Oct 2024 19:31:31 +0800 Subject: [PATCH] Address review --- src/Planner/PlannerActionsVisitor.cpp | 34 +++++++++++-------- .../03240_insert_select_named_tuple.reference | 2 ++ .../03240_insert_select_named_tuple.sql | 9 +++++ 3 files changed, 30 insertions(+), 15 deletions(-) diff --git a/src/Planner/PlannerActionsVisitor.cpp b/src/Planner/PlannerActionsVisitor.cpp index 3c09d8fc0e9..d666e30123a 100644 --- a/src/Planner/PlannerActionsVisitor.cpp +++ b/src/Planner/PlannerActionsVisitor.cpp @@ -38,6 +38,7 @@ namespace DB { namespace Setting { + extern const SettingsBool enable_named_columns_in_function_tuple; extern const SettingsBool transform_null_in; } @@ -182,26 +183,29 @@ public: break; } - /// Function "tuple" which generates named tuple should use argument aliases to construct its name. - if (function_node.getFunctionName() == "tuple") + if (planner_context.getQueryContext()->getSettingsRef()[Setting::enable_named_columns_in_function_tuple]) { - if (const DataTypeTuple * type_tuple = typeid_cast(function_node.getResultType().get())) + /// Function "tuple" which generates named tuple should use argument aliases to construct its name. + if (function_node.getFunctionName() == "tuple") { - if (type_tuple->haveExplicitNames()) + if (const DataTypeTuple * type_tuple = typeid_cast(function_node.getResultType().get())) { - const auto & names = type_tuple->getElementNames(); - size_t size = names.size(); - WriteBufferFromOwnString s; - s << "tuple("; - for (size_t i = 0; i < size; ++i) + if (type_tuple->haveExplicitNames()) { - if (i != 0) - s << ", "; - s << names[i]; + 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 << names[i]; + } + s << ")"; + result = s.str(); + break; } - s << ")"; - result = s.str(); - break; } } } diff --git a/tests/queries/0_stateless/03240_insert_select_named_tuple.reference b/tests/queries/0_stateless/03240_insert_select_named_tuple.reference index e9909adc3ec..8c3cb5b8425 100644 --- a/tests/queries/0_stateless/03240_insert_select_named_tuple.reference +++ b/tests/queries/0_stateless/03240_insert_select_named_tuple.reference @@ -1 +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"}} diff --git a/tests/queries/0_stateless/03240_insert_select_named_tuple.sql b/tests/queries/0_stateless/03240_insert_select_named_tuple.sql index fd3a556e26c..43e2df74af6 100644 --- a/tests/queries/0_stateless/03240_insert_select_named_tuple.sql +++ b/tests/queries/0_stateless/03240_insert_select_named_tuple.sql @@ -1,3 +1,5 @@ +SET enable_named_columns_in_function_tuple = 1; + DROP TABLE IF EXISTS src; DROP TABLE IF EXISTS dst; @@ -10,3 +12,10 @@ 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;