mirror of
https://github.com/ClickHouse/ClickHouse.git
synced 2024-11-23 16:12:01 +00:00
correct index analysis of WITH aliases
This commit is contained in:
parent
68ccdc8ca2
commit
44758935df
@ -48,4 +48,9 @@ void ASTWithAlias::appendColumnName(WriteBuffer & ostr) const
|
||||
appendColumnNameImpl(ostr);
|
||||
}
|
||||
|
||||
void ASTWithAlias::appendColumnNameWithoutAlias(WriteBuffer & ostr) const
|
||||
{
|
||||
appendColumnNameImpl(ostr);
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -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; }
|
||||
|
@ -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)
|
||||
|
@ -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(); }
|
||||
|
||||
|
@ -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<ASTLiteral>())
|
||||
{
|
||||
@ -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)
|
||||
|
@ -0,0 +1 @@
|
||||
3 4
|
11
tests/queries/0_stateless/01649_with_alias_key_condition.sql
Normal file
11
tests/queries/0_stateless/01649_with_alias_key_condition.sql
Normal file
@ -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;
|
Loading…
Reference in New Issue
Block a user