diff --git a/src/Interpreters/InterpreterSelectQuery.cpp b/src/Interpreters/InterpreterSelectQuery.cpp index 32df599bc32..3e607ad8909 100644 --- a/src/Interpreters/InterpreterSelectQuery.cpp +++ b/src/Interpreters/InterpreterSelectQuery.cpp @@ -687,8 +687,8 @@ static std::pair getLimitLengthAndOffset(const ASTSelectQuery & static UInt64 getLimitForSorting(const ASTSelectQuery & query, const Context & context) { - /// Partial sort can be done if there is LIMIT but no DISTINCT or LIMIT BY. - if (!query.distinct && !query.limitBy() && !query.limit_with_ties) + /// Partial sort can be done if there is LIMIT but no DISTINCT or LIMIT BY, neither ARRAY JOIN. + if (!query.distinct && !query.limitBy() && !query.limit_with_ties && !query.arrayJoinExpressionList()) { auto [limit_length, limit_offset] = getLimitLengthAndOffset(query, context); return limit_length + limit_offset; diff --git a/tests/queries/1_stateful/00094_order_by_array_join_limit.reference b/tests/queries/1_stateful/00094_order_by_array_join_limit.reference new file mode 100644 index 00000000000..a8be94b5470 --- /dev/null +++ b/tests/queries/1_stateful/00094_order_by_array_join_limit.reference @@ -0,0 +1,8 @@ +[''] +[''] + + +[] +[] +[''] +[''] diff --git a/tests/queries/1_stateful/00094_order_by_array_join_limit.sql b/tests/queries/1_stateful/00094_order_by_array_join_limit.sql new file mode 100644 index 00000000000..1c2d9f0164e --- /dev/null +++ b/tests/queries/1_stateful/00094_order_by_array_join_limit.sql @@ -0,0 +1,9 @@ +SELECT `ParsedParams.Key2` AS x +FROM test.hits +ARRAY JOIN ParsedParams AS PP +ORDER BY x ASC +LIMIT 2; + +SELECT arrayJoin(`ParsedParams.Key2`) AS x FROM test.hits ORDER BY x ASC LIMIT 2; +WITH arrayJoin(`ParsedParams.Key2`) AS pp SELECT ParsedParams.Key2 AS x FROM test.hits ORDER BY x ASC LIMIT 2; +WITH arrayJoin(`ParsedParams.Key2`) AS pp SELECT ParsedParams.Key2 AS x FROM test.hits WHERE NOT ignore(pp) ORDER BY x ASC LIMIT 2;