fix usage of index with array columns and ARRAY JOIN

This commit is contained in:
Anton Popov 2021-06-21 15:34:05 +03:00
parent 8ba6a5393f
commit ffa56bde24
4 changed files with 26 additions and 0 deletions

View File

@ -423,6 +423,9 @@ KeyCondition::KeyCondition(
*/ */
Block block_with_constants = getBlockWithConstants(query_info.query, query_info.syntax_analyzer_result, context); Block block_with_constants = getBlockWithConstants(query_info.query, query_info.syntax_analyzer_result, context);
for (const auto & [name, _] : query_info.syntax_analyzer_result->array_join_result_to_source)
array_joined_columns.insert(name);
const ASTSelectQuery & select = query_info.query->as<ASTSelectQuery &>(); const ASTSelectQuery & select = query_info.query->as<ASTSelectQuery &>();
if (select.where() || select.prewhere()) if (select.where() || select.prewhere())
{ {
@ -610,6 +613,10 @@ bool KeyCondition::canConstantBeWrappedByMonotonicFunctions(
DataTypePtr & out_type) DataTypePtr & out_type)
{ {
String expr_name = node->getColumnNameWithoutAlias(); String expr_name = node->getColumnNameWithoutAlias();
if (array_joined_columns.count(expr_name))
return false;
if (key_subexpr_names.count(expr_name) == 0) if (key_subexpr_names.count(expr_name) == 0)
return false; return false;
@ -714,6 +721,9 @@ bool KeyCondition::canConstantBeWrappedByFunctions(
{ {
String expr_name = ast->getColumnNameWithoutAlias(); String expr_name = ast->getColumnNameWithoutAlias();
if (array_joined_columns.count(expr_name))
return false;
if (key_subexpr_names.count(expr_name) == 0) if (key_subexpr_names.count(expr_name) == 0)
{ {
/// Let's check another one case. /// Let's check another one case.
@ -1075,6 +1085,9 @@ bool KeyCondition::isKeyPossiblyWrappedByMonotonicFunctionsImpl(
// Key columns should use canonical names for index analysis // Key columns should use canonical names for index analysis
String name = node->getColumnNameWithoutAlias(); String name = node->getColumnNameWithoutAlias();
if (array_joined_columns.count(name))
return false;
auto it = key_columns.find(name); auto it = key_columns.find(name);
if (key_columns.end() != it) if (key_columns.end() != it)
{ {

View File

@ -459,6 +459,8 @@ private:
const ExpressionActionsPtr key_expr; const ExpressionActionsPtr key_expr;
/// All intermediate columns are used to calculate key_expr. /// All intermediate columns are used to calculate key_expr.
const NameSet key_subexpr_names; const NameSet key_subexpr_names;
NameSet array_joined_columns;
PreparedSets prepared_sets; PreparedSets prepared_sets;
// If true, always allow key_expr to be wrapped by function // If true, always allow key_expr to be wrapped by function

View File

@ -0,0 +1 @@
a c

View File

@ -0,0 +1,10 @@
DROP TABLE IF EXISTS t_array_index;
CREATE TABLE t_array_index (n Nested(key String, value String))
ENGINE = MergeTree ORDER BY n.key;
INSERT INTO t_array_index VALUES (['a', 'b'], ['c', 'd']);
SELECT * FROM t_array_index ARRAY JOIN n WHERE n.key = 'a';
DROP TABLE IF EXISTS t_array_index;