Fix scalar subquery index analysis

This commit is contained in:
Amos Bird 2021-03-16 14:07:30 +08:00
parent 1b2ed51ff5
commit f00e108410
No known key found for this signature in database
GPG Key ID: 80D430DCBECFEDB4
2 changed files with 9 additions and 4 deletions

View File

@ -444,7 +444,8 @@ 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->getColumnNameWithoutAlias();
// Constant expr should use alias names if any
String column_name = expr->getColumnName();
if (const auto * lit = expr->as<ASTLiteral>())
{
@ -607,7 +608,8 @@ bool KeyCondition::canConstantBeWrappedByMonotonicFunctions(
if (strict)
return false;
String expr_name = node->getColumnNameWithoutAlias();
// Constant expr should use alias names if any
String expr_name = node->getColumnName();
const auto & sample_block = key_expr->getSampleBlock();
if (!sample_block.has(expr_name))
return false;
@ -675,7 +677,8 @@ bool KeyCondition::canConstantBeWrappedByFunctions(
if (strict)
return false;
String expr_name = ast->getColumnNameWithoutAlias();
// Constant expr should use alias names if any
String expr_name = ast->getColumnName();
const auto & sample_block = key_expr->getSampleBlock();
if (!sample_block.has(expr_name))
return false;
@ -1011,6 +1014,8 @@ bool KeyCondition::isKeyPossiblyWrappedByMonotonicFunctionsImpl(
* Therefore, use the full name of the expression for search.
*/
const auto & sample_block = key_expr->getSampleBlock();
// Key columns should use canonical names for index analysis
String name = node->getColumnNameWithoutAlias();
auto it = key_columns.find(name);

View File

@ -6,6 +6,6 @@ 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;
with i as k select * from alias_key_condition where k = (select i from alias_key_condition where i = 3);
drop table if exists alias_key_condition;