MCPcopy Create free account
hub / github.com/ClickHouse/ClickHouse / shouldUseQueryCacheForSubquery

Function shouldUseQueryCacheForSubquery

src/Planner/Planner.cpp:2063–2091  ·  view source on GitHub ↗

Returns true if the Planner-level query result cache (with `is_subquery = true` key) should be used. For actual subqueries (is_subquery=true): explicit SETTINGS `use_query_cache` on the node wins. For all Planner invocations: `query_cache_for_subqueries` propagation from outer query. By default, `use_query_cache` does NOT propagate. Regardless of opt-in, never use the cache for execution kinds wh

Source from the content-addressed store, hash-verified

2061/// per-replica or per-internal entries that the outer query already manages or that bypass the
2062/// safety gate `executeQuery` applies via `Context::setCanUseQueryResultCache`.
2063static bool shouldUseQueryCacheForSubquery(
2064 const QueryNode & query_node, bool outer_can_use_cache, const Settings & settings, bool is_subquery, const ContextPtr & query_context)
2065{
2066 /// Gate every Planner-level cache use by the query kind, so explicit `SETTINGS use_query_cache`
2067 /// on a subquery cannot bypass the safety check that `executeQuery` performs for the outer query.
2068 if (query_context->isInternalQuery()
2069 || query_context->getClientInfo().query_kind != ClientInfo::QueryKind::INITIAL_QUERY)
2070 return false;
2071
2072 /// Only check explicit per-node `use_query_cache` for actual subqueries.
2073 /// For the top-level query, this setting is handled by `executeQuery` (with `is_subquery = false` key).
2074 if (is_subquery && query_node.hasSettingsChanges())
2075 {
2076 for (const auto & change : query_node.getSettingsChanges())
2077 {
2078 if (change.name == "use_query_cache")
2079 return change.value.safeGet<bool>();
2080 }
2081 }
2082
2083 /// `query_cache_for_subqueries` enables the Planner-level (`is_subquery = true`) cache
2084 /// only for actual subqueries. The top-level query is cached separately by `executeQuery`
2085 /// using the `is_subquery = false` key; allowing it through this path here would write
2086 /// a duplicate entry under the subquery namespace.
2087 if (is_subquery && settings[Setting::query_cache_for_subqueries] && outer_can_use_cache)
2088 return true;
2089
2090 return false;
2091}
2092
2093void Planner::buildPlanForQueryNode()
2094{

Callers 1

buildPlanForQueryNodeMethod · 0.85

Calls 3

isInternalQueryMethod · 0.80
getSettingsChangesMethod · 0.80
hasSettingsChangesMethod · 0.45

Tested by

no test coverage detected