From 4adbdf364e64a20001925528dd1f4aeddbec577a Mon Sep 17 00:00:00 2001 From: hexiaoting Date: Thu, 17 Dec 2020 17:33:47 +0800 Subject: [PATCH] Fix bug for orderby withfill with limit clause --- src/Interpreters/InterpreterSelectQuery.cpp | 15 ++++++++++++++- .../01614_with_fill_with_limit.reference | 4 ++++ .../0_stateless/01614_with_fill_with_limit.sql | 15 +++++++++++++++ 3 files changed, 33 insertions(+), 1 deletion(-) create mode 100644 tests/queries/0_stateless/01614_with_fill_with_limit.reference create mode 100644 tests/queries/0_stateless/01614_with_fill_with_limit.sql diff --git a/src/Interpreters/InterpreterSelectQuery.cpp b/src/Interpreters/InterpreterSelectQuery.cpp index 38cc19a00d6..5e895bf732b 100644 --- a/src/Interpreters/InterpreterSelectQuery.cpp +++ b/src/Interpreters/InterpreterSelectQuery.cpp @@ -1029,10 +1029,23 @@ void InterpreterSelectQuery::executeImpl(QueryPlan & query_plan, const BlockInpu /** Optimization - if there are several sources and there is LIMIT, then first apply the preliminary LIMIT, * limiting the number of rows in each up to `offset + limit`. */ + bool has_withfill = false; + if (query.orderBy()) + { + SortDescription order_descr = getSortDescription(query, *context); + for (auto & desc : order_descr) + if (desc.with_fill) + { + has_withfill = true; + break; + } + } + bool has_prelimit = false; if (!to_aggregation_stage && query.limitLength() && !query.limit_with_ties && !hasWithTotalsInAnySubqueryInFromClause(query) && - !query.arrayJoinExpressionList() && !query.distinct && !expressions.hasLimitBy() && !settings.extremes) + !query.arrayJoinExpressionList() && !query.distinct && !expressions.hasLimitBy() && !settings.extremes && + !has_withfill) { executePreLimit(query_plan, false); has_prelimit = true; diff --git a/tests/queries/0_stateless/01614_with_fill_with_limit.reference b/tests/queries/0_stateless/01614_with_fill_with_limit.reference new file mode 100644 index 00000000000..451e076552e --- /dev/null +++ b/tests/queries/0_stateless/01614_with_fill_with_limit.reference @@ -0,0 +1,4 @@ +1 original +2 +1 original +2 diff --git a/tests/queries/0_stateless/01614_with_fill_with_limit.sql b/tests/queries/0_stateless/01614_with_fill_with_limit.sql new file mode 100644 index 00000000000..119117af287 --- /dev/null +++ b/tests/queries/0_stateless/01614_with_fill_with_limit.sql @@ -0,0 +1,15 @@ +SELECT + toFloat32(number % 10) AS n, + 'original' AS source +FROM numbers(10) +WHERE (number % 3) = 1 +ORDER BY n ASC WITH FILL STEP 1 +LIMIT 2; + +SELECT + toFloat32(number % 10) AS n, + 'original' AS source +FROM numbers(10) +WHERE (number % 3) = 1 +ORDER BY n ASC WITH FILL STEP 1 +LIMIT 2 WITH TIES;