correct index analysis of WITH aliases

This commit is contained in:
Amos Bird 2021-01-10 17:40:47 +08:00
parent 68ccdc8ca2
commit 44758935df
No known key found for this signature in database
GPG Key ID: 80D430DCBECFEDB4
7 changed files with 37 additions and 4 deletions

View File

@ -48,4 +48,9 @@ void ASTWithAlias::appendColumnName(WriteBuffer & ostr) const
appendColumnNameImpl(ostr);
}
void ASTWithAlias::appendColumnNameWithoutAlias(WriteBuffer & ostr) const
{
appendColumnNameImpl(ostr);
}
}

View File

@ -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; }

View File

@ -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)

View File

@ -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(); }

View File

@ -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)

View File

@ -0,0 +1 @@
3 4

View 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;