ISSUES-2312 fix nested view & joined view without database

This commit is contained in:
zhang2014 2018-10-17 14:35:16 +08:00
parent 36f5ce728d
commit 133363e8e3
5 changed files with 61 additions and 33 deletions

View File

@ -395,7 +395,7 @@ ASTs PredicateExpressionsOptimizer::evaluateAsterisk(ASTSelectQuery * select_que
{
const auto database_and_table_ast = static_cast<ASTIdentifier*>(table_expression->database_and_table_name.get());
const auto database_and_table_name = getDatabaseAndTableNameFromIdentifier(*database_and_table_ast);
storage = context.tryGetTable(database_and_table_name.first, database_and_table_name.second);
storage = context.getTable(database_and_table_name.first, database_and_table_name.second);
}
const auto block = storage->getSampleBlock();

View File

@ -7,6 +7,7 @@
#include <Parsers/ASTSubquery.h>
#include <Parsers/ASTTablesInSelectQuery.h>
#include <Common/typeid_cast.h>
#include "ASTSelectWithUnionQuery.h"
namespace DB
@ -339,25 +340,41 @@ const ASTTablesInSelectQueryElement * ASTSelectQuery::join() const
void ASTSelectQuery::setDatabaseIfNeeded(const String & database_name)
{
ASTTableExpression * table_expression = getFirstTableExpression(*this);
if (!table_expression)
if (!tables)
return;
if (!table_expression->database_and_table_name)
return;
ASTTablesInSelectQuery & tables_in_select_query = static_cast<ASTTablesInSelectQuery &>(*tables);
if (table_expression->database_and_table_name->children.empty())
for (auto & child : tables_in_select_query.children)
{
ASTPtr database = ASTIdentifier::createSpecial(database_name);
ASTPtr table = table_expression->database_and_table_name;
const auto & tables_element = static_cast<ASTTablesInSelectQueryElement &>(*child);
if (tables_element.table_expression)
{
const auto table_expression = static_cast<ASTTableExpression *>(tables_element.table_expression.get());
const String & old_name = static_cast<ASTIdentifier &>(*table_expression->database_and_table_name).name;
table_expression->database_and_table_name = ASTIdentifier::createSpecial(database_name + "." + old_name);
table_expression->database_and_table_name->children = {database, table};
}
else if (table_expression->database_and_table_name->children.size() != 2)
{
throw Exception("Logical error: more than two components in table expression", ErrorCodes::LOGICAL_ERROR);
if (!table_expression->database_and_table_name && !table_expression->subquery)
continue;
if (table_expression->subquery)
{
const auto subquery = static_cast<const ASTSubquery *>(table_expression->subquery.get());
const auto select_with_union_query = static_cast<ASTSelectWithUnionQuery *>(subquery->children[0].get());
select_with_union_query->setDatabaseIfNeeded(database_name);
}
else if (table_expression->database_and_table_name->children.empty())
{
ASTPtr database = ASTIdentifier::createSpecial(database_name);
ASTPtr table = table_expression->database_and_table_name;
const String & old_name = static_cast<ASTIdentifier &>(*table_expression->database_and_table_name).name;
table_expression->database_and_table_name = ASTIdentifier::createSpecial(database_name + "." + old_name);
table_expression->database_and_table_name->children = {database, table};
}
else if (table_expression->database_and_table_name->children.size() != 2)
{
throw Exception("Logical error: more than two components in table expression", ErrorCodes::LOGICAL_ERROR);
}
}
}
}

View File

@ -0,0 +1,6 @@
1
1
1 1
1
1
1 1

View File

@ -0,0 +1,23 @@
DROP TABLE IF EXISTS test.test;
DROP TABLE IF EXISTS test.test_view;
DROP TABLE IF EXISTS test.test_nested_view;
DROP TABLE IF EXISTS test.test_joined_view;
USE test;
CREATE VIEW test AS SELECT 1 AS N;
CREATE VIEW test_view AS SELECT * FROM test;
CREATE VIEW test_nested_view AS SELECT * FROM (SELECT * FROM test);
CREATE VIEW test_joined_view AS SELECT * FROM test ANY LEFT JOIN test USING N;
SELECT * FROM test_view;
SELECT * FROM test_nested_view;
SELECT * FROM test_joined_view;
USE default;
SELECT * FROM test.test_view;
SELECT * FROM test.test_nested_view;
SELECT * FROM test.test_joined_view;
DROP TABLE IF EXISTS test.test;
DROP TABLE IF EXISTS test.test_view;
DROP TABLE IF EXISTS test.test_nested_view;

View File

@ -1,18 +0,0 @@
DROP TABLE IF EXISTS test.whoami;
DROP TABLE IF EXISTS test.tellme;
DROP TABLE IF EXISTS test.tellme_nested;
use test;
create view whoami as select 1 as n;
create view tellme as select * from whoami;
create view tellme_nested as select * from (select * from whoami);
select * from tellme;
select * from tellme_nested;
use default;
select * from test.tellme;
select * from test.tellme_nested;
DROP TABLE test.whoami;
DROP TABLE test.tellme;
DROP TABLE test.tellme_nested;