Merge pull request #10207 from ClickHouse/fix-join-constants

Fix column names of constants inside JOIN.
This commit is contained in:
alexey-milovidov 2020-04-12 22:01:43 +03:00 committed by GitHub
commit 96f2a8e2a4
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 32 additions and 3 deletions

View File

@ -88,12 +88,14 @@ void RequiredSourceColumnsMatcher::visit(const ASTPtr & ast, Data & data)
visit(*t, ast, data); visit(*t, ast, data);
return; return;
} }
if (auto * t = ast->as<ASTSelectQuery>()) if (auto * t = ast->as<ASTSelectQuery>())
{ {
data.addTableAliasIfAny(*ast); data.addTableAliasIfAny(*ast);
visit(*t, ast, data); visit(*t, ast, data);
return; return;
} }
if (ast->as<ASTSubquery>()) if (ast->as<ASTSubquery>())
{ {
data.addTableAliasIfAny(*ast); data.addTableAliasIfAny(*ast);

View File

@ -5,6 +5,8 @@
#include <Core/Settings.h> #include <Core/Settings.h>
#include <Core/Block.h> #include <Core/Block.h>
#include <Common/StringUtils/StringUtils.h>
#include <DataTypes/DataTypeNullable.h> #include <DataTypes/DataTypeNullable.h>
@ -79,7 +81,9 @@ void TableJoin::deduplicateAndQualifyColumnNames(const NameSet & left_table_colu
dedup_columns.push_back(column); dedup_columns.push_back(column);
auto & inserted = dedup_columns.back(); auto & inserted = dedup_columns.back();
if (left_table_columns.count(column.name)) /// Also qualify unusual column names - that does not look like identifiers.
if (left_table_columns.count(column.name) || !isValidIdentifierBegin(column.name.at(0)))
inserted.name = right_table_prefix + column.name; inserted.name = right_table_prefix + column.name;
original_names[inserted.name] = column.name; original_names[inserted.name] = column.name;

View File

@ -5,6 +5,7 @@
#include <Interpreters/AsteriskSemantic.h> #include <Interpreters/AsteriskSemantic.h>
#include <Common/typeid_cast.h> #include <Common/typeid_cast.h>
#include <Common/StringUtils/StringUtils.h>
#include <Core/Names.h> #include <Core/Names.h>
#include <Parsers/ASTIdentifier.h> #include <Parsers/ASTIdentifier.h>
@ -107,8 +108,9 @@ void TranslateQualifiedNamesMatcher::visit(ASTIdentifier & identifier, ASTPtr &,
IdentifierSemantic::setMembership(identifier, table_pos); IdentifierSemantic::setMembership(identifier, table_pos);
/// In case if column from the joined table are in source columns, change it's name to qualified. /// In case if column from the joined table are in source columns, change it's name to qualified.
/// Also always leave unusual identifiers qualified.
auto & table = data.tables[table_pos].table; auto & table = data.tables[table_pos].table;
if (table_pos && data.hasColumn(short_name)) if (table_pos && (data.hasColumn(short_name) || !isValidIdentifierBegin(short_name.at(0))))
IdentifierSemantic::setColumnLongName(identifier, table); IdentifierSemantic::setColumnLongName(identifier, table);
else else
IdentifierSemantic::setColumnShortName(identifier, table); IdentifierSemantic::setColumnShortName(identifier, table);
@ -128,7 +130,7 @@ void TranslateQualifiedNamesMatcher::visit(ASTFunction & node, const ASTPtr &, D
func_arguments->children.clear(); func_arguments->children.clear();
} }
void TranslateQualifiedNamesMatcher::visit(const ASTQualifiedAsterisk & , const ASTPtr & ast, Data & data) void TranslateQualifiedNamesMatcher::visit(const ASTQualifiedAsterisk &, const ASTPtr & ast, Data & data)
{ {
if (ast->children.size() != 1) if (ast->children.size() != 1)
throw Exception("Logical error: qualified asterisk must have exactly one child", ErrorCodes::LOGICAL_ERROR); throw Exception("Logical error: qualified asterisk must have exactly one child", ErrorCodes::LOGICAL_ERROR);

View File

@ -0,0 +1,2 @@
1 hello 1 world world 1
2 hello 0 world 1

View File

@ -0,0 +1,17 @@
SELECT
t1.*,
t2.*,
'world',
isConstant('world')
FROM
(
SELECT
arrayJoin([1, 2]) AS k,
'hello'
) AS t1
LEFT JOIN
(
SELECT
arrayJoin([1, 3]) AS k,
'world'
) AS t2 ON t1.k = t2.k;

View File

@ -0,0 +1,2 @@
select cast(1, 'UInt8') from (select arrayJoin([1, 2]) as a) t1 left join (select 1 as b) t2 on b = ignore('UInt8');
select isConstant('UInt8'), toFixedString('hello', toUInt8(substring('UInt8', 5, 1))) from (select arrayJoin([1, 2]) as a) t1 left join (select 1 as b) t2 on b = ignore('UInt8');