dbms: Server: modify columns renaming rules [#METR-14099]

This commit is contained in:
Alexey Arno 2014-12-24 14:24:43 +03:00
parent 9c9db14d95
commit 255c7669a9
10 changed files with 34 additions and 10 deletions

View File

@ -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());
}
}

View File

@ -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

View File

@ -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;

View File

@ -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;

View File

@ -0,0 +1,5 @@
Alice
Bar
Bob
Carol
Foo

View File

@ -0,0 +1 @@
SELECT name FROM (SELECT name FROM data2013 UNION ALL SELECT data_name FROM data2015) ORDER BY name ASC;

View File

@ -0,0 +1,5 @@
Alice
Bar
Bob
Carol
Foo

View File

@ -0,0 +1 @@
SELECT X FROM (SELECT name AS X FROM data2013 UNION ALL SELECT data_name FROM data2015) ORDER BY X ASC;

View File

@ -0,0 +1,5 @@
Alice
Bar
Bob
Carol
Foo

View File

@ -0,0 +1 @@
SELECT name FROM (SELECT name FROM data2013 UNION ALL SELECT data_name AS name FROM data2015) ORDER BY name ASC;