diff --git a/docs/en/operations/query-result-cache.md b/docs/en/operations/query-result-cache.md index 5dab8effe0b..e46839aa10e 100644 --- a/docs/en/operations/query-result-cache.md +++ b/docs/en/operations/query-result-cache.md @@ -71,7 +71,7 @@ To control how often a query needs to run until its result is cached, use settin To specify the validity period after which cache entries become stale, use setting [query_result_cache_keep_seconds_alive](settings/settings.md#query-result-cache-keep-seconds-alive). -Results of queries with non-deterministic functions such as `rand()` and `now()` are not cached by default. This behavior can be overruled using setting [query_result_cache_ignore_nondeterministic_functions](settings/settings.md#query-result-cache-ignore-nondeterministic-functions). +Results of queries with non-deterministic functions such as `rand()` and `now()` are not cached by default. This behavior can be overruled using setting [query_result_cache_store_results_of_queries_with_nondeterministic_functions](settings/settings.md#query-result-cache-store-results-of-queries-with-nondeterministic-functions). Finally, it is sometimes useful to cache query results of the same query multiple times with different validity periods. To identify different entries for the same query, users may pass configuration [query_result_cache_partition_key](settings/settings.md#query-result-cache-partition-key). diff --git a/docs/en/operations/settings/settings.md b/docs/en/operations/settings/settings.md index eb9fc983292..0e68b760008 100644 --- a/docs/en/operations/settings/settings.md +++ b/docs/en/operations/settings/settings.md @@ -1192,16 +1192,16 @@ Possible values: Default value: `0`. -## query_result_cache_ignore_nondeterministic_functions {#experimental-query-result-cache-ignore-nondeterministic-function} +## query_result_cache_store_results_of_queries_with_nondeterministic_functions {#query-result-cache-store-results-of-queries-with-nondeterministic-functions} -If turned on, then results of SELECT queries with non-deterministic functions (e.g. `rand()`, `now()`) are not cached in the [query result cache](../query-result-cache.md). +If turned on, then results of SELECT queries with non-deterministic functions (e.g. `rand()`, `now()`) can be cached in the [query result cache](../query-result-cache.md). Possible values: - 0 - Disabled - 1 - Enabled -Default value: `1`. +Default value: `0`. ## query_result_cache_min_query_runs {#query-result-cache-min-query-runs} diff --git a/src/Core/Settings.h b/src/Core/Settings.h index 3153fc01b0d..af0cbffe43d 100644 --- a/src/Core/Settings.h +++ b/src/Core/Settings.h @@ -673,7 +673,7 @@ static constexpr UInt64 operator""_GiB(unsigned long long value) M(Bool, optimize_sorting_by_input_stream_properties, true, "Optimize sorting by sorting properties of input stream", 0) \ M(Bool, enable_experimental_query_result_cache, false, "Store and retrieve results of SELECT queries in/from the query result cache", 0) \ M(Bool, enable_experimental_query_result_cache_passive_usage, false, "Only retrieve results of SELECT queries from the query result cache", 0) \ - M(Bool, query_result_cache_ignore_nondeterministic_functions, true, "Queries with non-deterministic functions (e.g. rand(), now()) are not stored in the query result cache", 0) \ + M(Bool, query_result_cache_store_results_of_queries_with_nondeterministic_functions, false, "Store results of queries with non-deterministic functions (e.g. rand(), now()) in the query result cache", 0) \ M(UInt64, query_result_cache_size, (1ull << 30), "Maximum size of the query result cache in bytes. 0 means disabled.", 0) \ M(UInt64, query_result_cache_min_query_runs, 0, "Minimum number a SELECT query must run before its result is stored in the query result cache", 0) \ M(UInt64, query_result_cache_max_entries, (1ull << 10), "Maximum number of SELECT query results in the query result cache", 0) \ diff --git a/src/Interpreters/Cache/QueryResultCache.cpp b/src/Interpreters/Cache/QueryResultCache.cpp index a2bde4b2c0d..c2e9776b527 100644 --- a/src/Interpreters/Cache/QueryResultCache.cpp +++ b/src/Interpreters/Cache/QueryResultCache.cpp @@ -17,11 +17,8 @@ namespace ProfileEvents namespace DB { -bool hasNonCacheableFunctions(ASTPtr ast, ContextPtr context) +bool astContainsNonDeterministicFunctions(ASTPtr ast, ContextPtr context) { - if (!context->getSettings().query_result_cache_ignore_nondeterministic_functions) - return false; - if (const auto * function = ast->as()) { const FunctionFactory & function_factory = FunctionFactory::instance(); @@ -34,7 +31,7 @@ bool hasNonCacheableFunctions(ASTPtr ast, ContextPtr context) bool has_non_cacheable_functions = false; for (const auto & child : ast->children) - has_non_cacheable_functions |= hasNonCacheableFunctions(child, context); + has_non_cacheable_functions |= astContainsNonDeterministicFunctions(child, context); return has_non_cacheable_functions; } diff --git a/src/Interpreters/Cache/QueryResultCache.h b/src/Interpreters/Cache/QueryResultCache.h index 97430ab054a..53d956502d8 100644 --- a/src/Interpreters/Cache/QueryResultCache.h +++ b/src/Interpreters/Cache/QueryResultCache.h @@ -10,7 +10,7 @@ namespace DB { /// Does AST contain non-deterministic functions like rand() and now()? -bool hasNonCacheableFunctions(ASTPtr ast, ContextPtr context); +bool astContainsNonDeterministicFunctions(ASTPtr ast, ContextPtr context); /// Maps queries to query results. Useful to avoid repeated query calculation. /// diff --git a/src/Interpreters/executeQuery.cpp b/src/Interpreters/executeQuery.cpp index 9b50d634284..7733dcb12fd 100644 --- a/src/Interpreters/executeQuery.cpp +++ b/src/Interpreters/executeQuery.cpp @@ -700,7 +700,8 @@ static std::tuple executeQueryImpl( } if ((settings.enable_experimental_query_result_cache_passive_usage || settings.enable_experimental_query_result_cache) - && query_result_cache != nullptr && res.pipeline.pulling() && !hasNonCacheableFunctions(ast, context)) + && query_result_cache != nullptr && res.pipeline.pulling() + && (settings.query_result_cache_store_results_of_queries_with_nondeterministic_functions || !astContainsNonDeterministicFunctions(ast, context))) { QueryResultCache::Key key{ ast, context->getUserName(), settings.query_result_cache_partition_key, res.pipeline.getHeader(), diff --git a/tests/queries/0_stateless/02494_query_result_cache_ignore_nondeterministic_functions.reference b/tests/queries/0_stateless/02494_query_result_cache_nondeterministic_functions.reference similarity index 82% rename from tests/queries/0_stateless/02494_query_result_cache_ignore_nondeterministic_functions.reference rename to tests/queries/0_stateless/02494_query_result_cache_nondeterministic_functions.reference index fe52d4fc50f..5b125838a68 100644 --- a/tests/queries/0_stateless/02494_query_result_cache_ignore_nondeterministic_functions.reference +++ b/tests/queries/0_stateless/02494_query_result_cache_nondeterministic_functions.reference @@ -7,7 +7,7 @@ SELECT COUNT(rand(1)) SETTINGS enable_experimental_query_result_cache = true; SELECT COUNT(*) FROM system.queryresult_cache; 0 -- force an entry -SELECT COUNT(RAND(1)) SETTINGS enable_experimental_query_result_cache = true, query_result_cache_ignore_nondeterministic_functions = false; +SELECT COUNT(RAND(1)) SETTINGS enable_experimental_query_result_cache = true, query_result_cache_store_results_of_queries_with_nondeterministic_functions = true; 1 SELECT COUNT(*) FROM system.queryresult_cache; 1 diff --git a/tests/queries/0_stateless/02494_query_result_cache_ignore_nondeterministic_functions.sql b/tests/queries/0_stateless/02494_query_result_cache_nondeterministic_functions.sql similarity index 82% rename from tests/queries/0_stateless/02494_query_result_cache_ignore_nondeterministic_functions.sql rename to tests/queries/0_stateless/02494_query_result_cache_nondeterministic_functions.sql index 487427a88d3..ceb903c21c2 100644 --- a/tests/queries/0_stateless/02494_query_result_cache_ignore_nondeterministic_functions.sql +++ b/tests/queries/0_stateless/02494_query_result_cache_nondeterministic_functions.sql @@ -7,7 +7,7 @@ SELECT COUNT(rand(1)) SETTINGS enable_experimental_query_result_cache = true; SELECT COUNT(*) FROM system.queryresult_cache; -- force an entry -SELECT COUNT(RAND(1)) SETTINGS enable_experimental_query_result_cache = true, query_result_cache_ignore_nondeterministic_functions = false; +SELECT COUNT(RAND(1)) SETTINGS enable_experimental_query_result_cache = true, query_result_cache_store_results_of_queries_with_nondeterministic_functions = true; SELECT COUNT(*) FROM system.queryresult_cache; SYSTEM DROP QUERY RESULT CACHE;