diff --git a/dbms/src/Interpreters/ExpressionAnalyzer.cpp b/dbms/src/Interpreters/ExpressionAnalyzer.cpp index b00ce36f73a..e60655b3419 100644 --- a/dbms/src/Interpreters/ExpressionAnalyzer.cpp +++ b/dbms/src/Interpreters/ExpressionAnalyzer.cpp @@ -356,6 +356,16 @@ void ExpressionAnalyzer::translateQualifiedNamesImpl(ASTPtr & ast, const std::ve } else { + /// If the WHERE clause or HAVING consists of a single quailified column, the reference must be translated not only in children, but also in where_expression and having_expression. + if (ASTSelectQuery * select = typeid_cast(ast.get())) + { + if (select->prewhere_expression) + translateQualifiedNamesImpl(select->prewhere_expression, tables); + if (select->where_expression) + translateQualifiedNamesImpl(select->where_expression, tables); + if (select->having_expression) + translateQualifiedNamesImpl(select->having_expression, tables); + } for (auto & child : ast->children) { /// Do not go to FROM, JOIN, subqueries. diff --git a/dbms/tests/queries/0_stateless/00702_where_with_quailified_names.reference b/dbms/tests/queries/0_stateless/00702_where_with_quailified_names.reference new file mode 100644 index 00000000000..d00491fd7e5 --- /dev/null +++ b/dbms/tests/queries/0_stateless/00702_where_with_quailified_names.reference @@ -0,0 +1 @@ +1 diff --git a/dbms/tests/queries/0_stateless/00702_where_with_quailified_names.sql b/dbms/tests/queries/0_stateless/00702_where_with_quailified_names.sql new file mode 100644 index 00000000000..91948d720fd --- /dev/null +++ b/dbms/tests/queries/0_stateless/00702_where_with_quailified_names.sql @@ -0,0 +1,7 @@ +USE test; +DROP TABLE IF EXISTS where_qualified; +CREATE TABLE where_qualified(a UInt32, b UInt8) ENGINE = Memory; +INSERT INTO where_qualified VALUES(1, 1); +INSERT INTO where_qualified VALUES(2, 0); +SELECT a from where_qualified WHERE where_qualified.b; +DROP TABLE where_qualified;