diff --git a/src/Parsers/ASTWithAlias.cpp b/src/Parsers/ASTWithAlias.cpp index 20f647fb575..88f6568a719 100644 --- a/src/Parsers/ASTWithAlias.cpp +++ b/src/Parsers/ASTWithAlias.cpp @@ -48,4 +48,9 @@ void ASTWithAlias::appendColumnName(WriteBuffer & ostr) const appendColumnNameImpl(ostr); } +void ASTWithAlias::appendColumnNameWithoutAlias(WriteBuffer & ostr) const +{ + appendColumnNameImpl(ostr); +} + } diff --git a/src/Parsers/ASTWithAlias.h b/src/Parsers/ASTWithAlias.h index 7a272a157e2..ea4419402b0 100644 --- a/src/Parsers/ASTWithAlias.h +++ b/src/Parsers/ASTWithAlias.h @@ -21,6 +21,7 @@ public: using IAST::IAST; void appendColumnName(WriteBuffer & ostr) const final; + void appendColumnNameWithoutAlias(WriteBuffer & ostr) const final; String getAliasOrColumnName() const override { return alias.empty() ? getColumnName() : alias; } String tryGetAlias() const override { return alias; } void setAlias(const String & to) override { alias = to; } diff --git a/src/Parsers/IAST.cpp b/src/Parsers/IAST.cpp index e223235b8e4..6451a9d0c0d 100644 --- a/src/Parsers/IAST.cpp +++ b/src/Parsers/IAST.cpp @@ -109,6 +109,14 @@ String IAST::getColumnName() const } +String IAST::getColumnNameWithoutAlias() const +{ + WriteBufferFromOwnString write_buffer; + appendColumnNameWithoutAlias(write_buffer); + return write_buffer.str(); +} + + void IAST::FormatSettings::writeIdentifier(const String & name) const { switch (identifier_quoting_style) diff --git a/src/Parsers/IAST.h b/src/Parsers/IAST.h index bed6c5bcdf9..54e08b2700e 100644 --- a/src/Parsers/IAST.h +++ b/src/Parsers/IAST.h @@ -41,11 +41,18 @@ public: /** Get the canonical name of the column if the element is a column */ String getColumnName() const; + /** Same as the above but ensure no alias names are used. This is for index analysis */ + String getColumnNameWithoutAlias() const; virtual void appendColumnName(WriteBuffer &) const { throw Exception("Trying to get name of not a column: " + getID(), ErrorCodes::LOGICAL_ERROR); } + virtual void appendColumnNameWithoutAlias(WriteBuffer &) const + { + throw Exception("Trying to get name of not a column: " + getID(), ErrorCodes::LOGICAL_ERROR); + } + /** Get the alias, if any, or the canonical name of the column, if it is not. */ virtual String getAliasOrColumnName() const { return getColumnName(); } diff --git a/src/Storages/MergeTree/KeyCondition.cpp b/src/Storages/MergeTree/KeyCondition.cpp index ead93a6e1d9..eb58b2911c9 100644 --- a/src/Storages/MergeTree/KeyCondition.cpp +++ b/src/Storages/MergeTree/KeyCondition.cpp @@ -444,7 +444,7 @@ bool KeyCondition::addCondition(const String & column, const Range & range) */ bool KeyCondition::getConstant(const ASTPtr & expr, Block & block_with_constants, Field & out_value, DataTypePtr & out_type) { - String column_name = expr->getColumnName(); + String column_name = expr->getColumnNameWithoutAlias(); if (const auto * lit = expr->as()) { @@ -607,7 +607,7 @@ bool KeyCondition::canConstantBeWrappedByMonotonicFunctions( if (strict) return false; - String expr_name = node->getColumnName(); + String expr_name = node->getColumnNameWithoutAlias(); const auto & sample_block = key_expr->getSampleBlock(); if (!sample_block.has(expr_name)) return false; @@ -675,7 +675,7 @@ bool KeyCondition::canConstantBeWrappedByFunctions( if (strict) return false; - String expr_name = ast->getColumnName(); + String expr_name = ast->getColumnNameWithoutAlias(); const auto & sample_block = key_expr->getSampleBlock(); if (!sample_block.has(expr_name)) return false; @@ -934,7 +934,7 @@ bool KeyCondition::isKeyPossiblyWrappedByMonotonicFunctionsImpl( * Therefore, use the full name of the expression for search. */ const auto & sample_block = key_expr->getSampleBlock(); - String name = node->getColumnName(); + String name = node->getColumnNameWithoutAlias(); auto it = key_columns.find(name); if (key_columns.end() != it) diff --git a/tests/queries/0_stateless/01649_with_alias_key_condition.reference b/tests/queries/0_stateless/01649_with_alias_key_condition.reference new file mode 100644 index 00000000000..5816b4eb49b --- /dev/null +++ b/tests/queries/0_stateless/01649_with_alias_key_condition.reference @@ -0,0 +1 @@ +3 4 diff --git a/tests/queries/0_stateless/01649_with_alias_key_condition.sql b/tests/queries/0_stateless/01649_with_alias_key_condition.sql new file mode 100644 index 00000000000..b813e6ee84f --- /dev/null +++ b/tests/queries/0_stateless/01649_with_alias_key_condition.sql @@ -0,0 +1,11 @@ +drop table if exists alias_key_condition; + +create table alias_key_condition ( i int, j int ) engine MergeTree order by i; + +insert into alias_key_condition values (1, 2), (3, 4); + +set force_primary_key = 1; + +with i as k select * from alias_key_condition where k = 3; + +drop table if exists alias_key_condition;