From 255c7669a96b99eeb2a1b2feb269e26102a09d0f Mon Sep 17 00:00:00 2001 From: Alexey Arno Date: Wed, 24 Dec 2014 14:24:43 +0300 Subject: [PATCH] dbms: Server: modify columns renaming rules [#METR-14099] --- dbms/include/DB/Parsers/ASTSelectQuery.h | 17 +++++++++-------- .../queries/0_stateless/00098_1_union_all.sql | 5 +++++ .../queries/0_stateless/00098_9_union_all.sql | 2 +- .../queries/0_stateless/00098_b_union_all.sql | 2 +- .../0_stateless/00098_d_union_all.reference | 5 +++++ .../queries/0_stateless/00098_d_union_all.sql | 1 + .../0_stateless/00098_e_union_all.reference | 5 +++++ .../queries/0_stateless/00098_e_union_all.sql | 1 + .../0_stateless/00098_f_union_all.reference | 5 +++++ .../queries/0_stateless/00098_f_union_all.sql | 1 + 10 files changed, 34 insertions(+), 10 deletions(-) create mode 100644 dbms/tests/queries/0_stateless/00098_d_union_all.reference create mode 100644 dbms/tests/queries/0_stateless/00098_d_union_all.sql create mode 100644 dbms/tests/queries/0_stateless/00098_e_union_all.reference create mode 100644 dbms/tests/queries/0_stateless/00098_e_union_all.sql create mode 100644 dbms/tests/queries/0_stateless/00098_f_union_all.reference create mode 100644 dbms/tests/queries/0_stateless/00098_f_union_all.sql diff --git a/dbms/include/DB/Parsers/ASTSelectQuery.h b/dbms/include/DB/Parsers/ASTSelectQuery.h index 68702ff08ab..a4bc4d4c47a 100644 --- a/dbms/include/DB/Parsers/ASTSelectQuery.h +++ b/dbms/include/DB/Parsers/ASTSelectQuery.h @@ -52,7 +52,7 @@ public: return false; } - /// Переименовать столбцы в такие же имена, как в source. + /// Переименовать столбцы запроса в такие же имена, как в исходном запросе. void renameColumns(const ASTSelectQuery & source) { const ASTs & from = source.select_expression_list->children; @@ -64,14 +64,15 @@ public: for (size_t i = 0; i < from.size(); ++i) { - const auto & from_alias = from[i]->tryGetAlias(); - const auto & to_alias = to[i]->tryGetAlias(); - if (!to_alias.empty() && !from_alias.empty()) - if (to_alias != from_alias) - throw Exception("Column alias mismatch in UNION ALL chain", + /// Если столбец имеет алиас, то он должен совпадать с названием исходного столбца. + /// В противном случае мы ему присваиваем алиас, если требуется. + if (!to[i]->tryGetAlias().empty()) + { + if (to[i]->tryGetAlias() != from[i]->getAliasOrColumnName()) + throw Exception("Column alias mismatch in UNION ALL chain", DB::ErrorCodes::UNION_ALL_COLUMN_ALIAS_MISMATCH); - - if (to[i]->getAliasOrColumnName() != from[i]->getAliasOrColumnName()) + } + else if (to[i]->getColumnName() != from[i]->getAliasOrColumnName()) to[i]->setAlias(from[i]->getAliasOrColumnName()); } } diff --git a/dbms/tests/queries/0_stateless/00098_1_union_all.sql b/dbms/tests/queries/0_stateless/00098_1_union_all.sql index 619371a6496..7c05af6de98 100644 --- a/dbms/tests/queries/0_stateless/00098_1_union_all.sql +++ b/dbms/tests/queries/0_stateless/00098_1_union_all.sql @@ -1,8 +1,10 @@ DROP TABLE IF EXISTS data2013; DROP TABLE IF EXISTS data2014; +DROP TABLE IF EXISTS data2015; CREATE TABLE data2013 (name String, value UInt32) ENGINE = Memory; CREATE TABLE data2014 (name String, value UInt32) ENGINE = Memory; +CREATE TABLE data2015 (data_name String, data_value UInt32) ENGINE = Memory; INSERT INTO data2013(name,value) VALUES('Alice', 1000); INSERT INTO data2013(name,value) VALUES('Bob', 2000); @@ -12,6 +14,9 @@ INSERT INTO data2014(name,value) VALUES('Alice', 2000); INSERT INTO data2014(name,value) VALUES('Bob', 2000); INSERT INTO data2014(name,value) VALUES('Dennis', 35000); +INSERT INTO data2015(data_name, data_value) VALUES('Foo', 42); +INSERT INTO data2015(data_name, data_value) VALUES('Bar', 1); + SELECT val FROM (SELECT value AS val FROM data2013 WHERE name = 'Alice' UNION ALL diff --git a/dbms/tests/queries/0_stateless/00098_9_union_all.sql b/dbms/tests/queries/0_stateless/00098_9_union_all.sql index de61afcb518..781556ffa7b 100644 --- a/dbms/tests/queries/0_stateless/00098_9_union_all.sql +++ b/dbms/tests/queries/0_stateless/00098_9_union_all.sql @@ -1 +1 @@ -SELECT * FROM (SELECT 1 UNION ALL SELECT 2 AS X) ORDER BY 1 ASC; +SELECT * FROM (SELECT 1 UNION ALL SELECT 2) ORDER BY 1 ASC; diff --git a/dbms/tests/queries/0_stateless/00098_b_union_all.sql b/dbms/tests/queries/0_stateless/00098_b_union_all.sql index 6ab601fb1de..837f0b50da4 100644 --- a/dbms/tests/queries/0_stateless/00098_b_union_all.sql +++ b/dbms/tests/queries/0_stateless/00098_b_union_all.sql @@ -1 +1 @@ -SELECT * FROM (SELECT 1 UNION ALL SELECT 2 AS X UNION ALL SELECT 3 AS Y) ORDER BY 1 ASC; +SELECT * FROM (SELECT 1 AS X UNION ALL SELECT 2 UNION ALL SELECT 3 AS X) ORDER BY X ASC; diff --git a/dbms/tests/queries/0_stateless/00098_d_union_all.reference b/dbms/tests/queries/0_stateless/00098_d_union_all.reference new file mode 100644 index 00000000000..9f0145f0f0b --- /dev/null +++ b/dbms/tests/queries/0_stateless/00098_d_union_all.reference @@ -0,0 +1,5 @@ +Alice +Bar +Bob +Carol +Foo diff --git a/dbms/tests/queries/0_stateless/00098_d_union_all.sql b/dbms/tests/queries/0_stateless/00098_d_union_all.sql new file mode 100644 index 00000000000..282d46e81a6 --- /dev/null +++ b/dbms/tests/queries/0_stateless/00098_d_union_all.sql @@ -0,0 +1 @@ +SELECT name FROM (SELECT name FROM data2013 UNION ALL SELECT data_name FROM data2015) ORDER BY name ASC; diff --git a/dbms/tests/queries/0_stateless/00098_e_union_all.reference b/dbms/tests/queries/0_stateless/00098_e_union_all.reference new file mode 100644 index 00000000000..9f0145f0f0b --- /dev/null +++ b/dbms/tests/queries/0_stateless/00098_e_union_all.reference @@ -0,0 +1,5 @@ +Alice +Bar +Bob +Carol +Foo diff --git a/dbms/tests/queries/0_stateless/00098_e_union_all.sql b/dbms/tests/queries/0_stateless/00098_e_union_all.sql new file mode 100644 index 00000000000..68b26eac5d9 --- /dev/null +++ b/dbms/tests/queries/0_stateless/00098_e_union_all.sql @@ -0,0 +1 @@ +SELECT X FROM (SELECT name AS X FROM data2013 UNION ALL SELECT data_name FROM data2015) ORDER BY X ASC; diff --git a/dbms/tests/queries/0_stateless/00098_f_union_all.reference b/dbms/tests/queries/0_stateless/00098_f_union_all.reference new file mode 100644 index 00000000000..9f0145f0f0b --- /dev/null +++ b/dbms/tests/queries/0_stateless/00098_f_union_all.reference @@ -0,0 +1,5 @@ +Alice +Bar +Bob +Carol +Foo diff --git a/dbms/tests/queries/0_stateless/00098_f_union_all.sql b/dbms/tests/queries/0_stateless/00098_f_union_all.sql new file mode 100644 index 00000000000..561d2398869 --- /dev/null +++ b/dbms/tests/queries/0_stateless/00098_f_union_all.sql @@ -0,0 +1 @@ +SELECT name FROM (SELECT name FROM data2013 UNION ALL SELECT data_name AS name FROM data2015) ORDER BY name ASC;