diff --git a/dbms/src/Interpreters/ExpressionAnalyzer.cpp b/dbms/src/Interpreters/ExpressionAnalyzer.cpp index 2393c95afb2..0f64d9586cf 100644 --- a/dbms/src/Interpreters/ExpressionAnalyzer.cpp +++ b/dbms/src/Interpreters/ExpressionAnalyzer.cpp @@ -1185,11 +1185,8 @@ void ExpressionAnalyzer::optimizeLimitBy() for (const auto & elem : elems) { - if (const auto id = typeid_cast(elem.get())) - { - if (elems_set.emplace(id->getColumnName()).second) - unique_elems.emplace_back(elem); - } + if (elems_set.emplace(elem->getColumnName()).second) + unique_elems.emplace_back(elem); } if (unique_elems.size() < elems.size()) diff --git a/dbms/src/Interpreters/InterpreterSelectQuery.cpp b/dbms/src/Interpreters/InterpreterSelectQuery.cpp index d0f744c6530..4e30c3f5e90 100644 --- a/dbms/src/Interpreters/InterpreterSelectQuery.cpp +++ b/dbms/src/Interpreters/InterpreterSelectQuery.cpp @@ -1185,7 +1185,7 @@ void InterpreterSelectQuery::executePreLimit() void InterpreterSelectQuery::executeLimitBy() { - if (!query.limit_by_value) + if (!query.limit_by_value || !query.limit_by_expression_list) return; Names columns; diff --git a/dbms/src/Parsers/ASTSelectQuery.cpp b/dbms/src/Parsers/ASTSelectQuery.cpp index 70079fe7b94..0ca0b4979dc 100644 --- a/dbms/src/Parsers/ASTSelectQuery.cpp +++ b/dbms/src/Parsers/ASTSelectQuery.cpp @@ -291,7 +291,9 @@ void ASTSelectQuery::formatImpl(const FormatSettings & s, FormatState & state, F if (limit_by_value) { - s.ostr << (s.hilite ? hilite_keyword : "") << s.nl_or_ws << indent_str << "LIMIT BY " << (s.hilite ? hilite_none : ""); + s.ostr << (s.hilite ? hilite_keyword : "") << s.nl_or_ws << indent_str << "LIMIT " << (s.hilite ? hilite_none : ""); + limit_by_value->formatImpl(s, state, frame); + s.ostr << (s.hilite ? hilite_keyword : "") << " BY " << (s.hilite ? hilite_none : ""); s.one_line ? limit_by_expression_list->formatImpl(s, state, frame) : typeid_cast(*limit_by_expression_list).formatImplMultiline(s, state, frame); diff --git a/dbms/tests/queries/0_stateless/00409_limit_by.reference b/dbms/tests/queries/0_stateless/00409_limit_by.reference index e037feaf637..f0f6c4e48ad 100644 --- a/dbms/tests/queries/0_stateless/00409_limit_by.reference +++ b/dbms/tests/queries/0_stateless/00409_limit_by.reference @@ -15,3 +15,7 @@ 1 John 3 Mary 4 Mary +0 +0 +0 +1 diff --git a/dbms/tests/queries/0_stateless/00409_limit_by.sql b/dbms/tests/queries/0_stateless/00409_limit_by.sql index 8501acebbdb..dab31b99235 100644 --- a/dbms/tests/queries/0_stateless/00409_limit_by.sql +++ b/dbms/tests/queries/0_stateless/00409_limit_by.sql @@ -23,4 +23,10 @@ SELECT Num, count(*) FROM test.limit_by GROUP BY Num ORDER BY Num LIMIT 2 BY Num -- LIMIT BY can be combined with LIMIT SELECT Num, Name FROM test.limit_by ORDER BY Num LIMIT 1 BY Num, Name LIMIT 3; +-- Distributed LIMIT BY +SELECT dummy FROM remote('127.0.0.{1,2}', system.one) LIMIT 1 BY dummy; +SELECT dummy FROM remote('127.0.0.{1,2}', system.one) LIMIT 2 BY dummy; + +SELECT 1 as one FROM remote('127.0.0.{1,2}', system.one) LIMIT 1 BY one; + DROP TABLE IF EXISTS test.limit_by;