From 5fe5cc8611ba5c6ddea22538f47a5f650c28bf88 Mon Sep 17 00:00:00 2001 From: Robert Schulze Date: Thu, 12 Sep 2024 09:21:27 +0000 Subject: [PATCH] Disallow query cache for queries with non-throw overflow mode --- src/Interpreters/executeQuery.cpp | 13 +++++++++ .../02494_query_cache_bugs.reference | 21 ++++++++++++++ .../0_stateless/02494_query_cache_bugs.sql | 28 +++++++++++++++++++ 3 files changed, 62 insertions(+) diff --git a/src/Interpreters/executeQuery.cpp b/src/Interpreters/executeQuery.cpp index 6c22e71bccf..be9423852c1 100644 --- a/src/Interpreters/executeQuery.cpp +++ b/src/Interpreters/executeQuery.cpp @@ -1118,6 +1118,19 @@ static std::tuple executeQueryImpl( && settings.use_query_cache && !internal && client_info.query_kind == ClientInfo::QueryKind::INITIAL_QUERY + /// Bug 67476: Avoid that the query cache stores truncated results if the query ran with a non-THROW overflow mode and hit a limit. + /// This is more workaround than a fix ... unfortunately it is hard to detect from the perspective of the query cache that the + /// query result is truncated. + && (settings.read_overflow_mode == OverflowMode::THROW + && settings.read_overflow_mode_leaf == OverflowMode::THROW + && settings.group_by_overflow_mode == OverflowMode::THROW + && settings.sort_overflow_mode == OverflowMode::THROW + && settings.result_overflow_mode == OverflowMode::THROW + && settings.timeout_overflow_mode == OverflowMode::THROW + && settings.set_overflow_mode == OverflowMode::THROW + && settings.join_overflow_mode == OverflowMode::THROW + && settings.transfer_overflow_mode == OverflowMode::THROW + && settings.distinct_overflow_mode == OverflowMode::THROW) && (ast->as() || ast->as()); QueryCache::Usage query_cache_usage = QueryCache::Usage::None; diff --git a/tests/queries/0_stateless/02494_query_cache_bugs.reference b/tests/queries/0_stateless/02494_query_cache_bugs.reference index 448e1366ea7..ea9017d5394 100644 --- a/tests/queries/0_stateless/02494_query_cache_bugs.reference +++ b/tests/queries/0_stateless/02494_query_cache_bugs.reference @@ -22,3 +22,24 @@ Row 1: ────── x: 1 2 +-- Bug 67476: Queries with overflow mode != throw must not be cached by the query cache +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 diff --git a/tests/queries/0_stateless/02494_query_cache_bugs.sql b/tests/queries/0_stateless/02494_query_cache_bugs.sql index 74496e0f77a..423068aa646 100644 --- a/tests/queries/0_stateless/02494_query_cache_bugs.sql +++ b/tests/queries/0_stateless/02494_query_cache_bugs.sql @@ -36,4 +36,32 @@ SELECT count(*) FROM system.query_cache; DROP TABLE tab; +SELECT '-- Bug 67476: Queries with overflow mode != throw must not be cached by the query cache'; + +DROP TABLE IF EXISTS tab; + +CREATE TABLE tab(c UInt64) ENGINE = Memory; + +SYSTEM DROP QUERY CACHE; +SELECT sum(c) FROM tab SETTINGS read_overflow_mode = 'break', use_query_cache = 1; +SELECT count(*) from system.query_cache; +SELECT sum(c) FROM tab SETTINGS read_overflow_mode_leaf = 'break', use_query_cache = 1; +SELECT count(*) from system.query_cache; +SELECT sum(c) FROM tab SETTINGS group_by_overflow_mode = 'break', use_query_cache = 1; +SELECT count(*) from system.query_cache; +SELECT sum(c) FROM tab SETTINGS sort_overflow_mode = 'break', use_query_cache = 1; +SELECT count(*) from system.query_cache; +SELECT sum(c) FROM tab SETTINGS result_overflow_mode = 'break', use_query_cache = 1; +SELECT count(*) from system.query_cache; +SELECT sum(c) FROM tab SETTINGS timeout_overflow_mode = 'break', use_query_cache = 1; +SELECT count(*) from system.query_cache; +SELECT sum(c) FROM tab SETTINGS set_overflow_mode = 'break', use_query_cache = 1; +SELECT count(*) from system.query_cache; +SELECT sum(c) FROM tab SETTINGS join_overflow_mode = 'break', use_query_cache = 1; +SELECT count(*) from system.query_cache; +SELECT sum(c) FROM tab SETTINGS transfer_overflow_mode = 'break', use_query_cache = 1; +SELECT count(*) from system.query_cache; +SELECT sum(c) FROM tab SETTINGS distinct_overflow_mode = 'break', use_query_cache = 1; +SELECT count(*) from system.query_cache; + SYSTEM DROP QUERY CACHE;