| 106 | } |
| 107 | |
| 108 | QueryPlanPtr PlanCacheManager::getPlanFromCache(UInt128 query_hash, ContextMutablePtr & context) |
| 109 | { |
| 110 | if (!context->getPlanCacheManager()) |
| 111 | throw Exception("plan cache has to be initialized", ErrorCodes::LOGICAL_ERROR); |
| 112 | |
| 113 | auto & cached = context->getPlanCacheManager()->instance(); |
| 114 | try |
| 115 | { |
| 116 | auto plan_object = cached.get(query_hash); |
| 117 | if (!plan_object || !plan_object->plan_root) |
| 118 | return nullptr; |
| 119 | |
| 120 | // check statistic version |
| 121 | for (auto & item : plan_object->query_info->stats_version) |
| 122 | { |
| 123 | Statistics::StatsTableIdentifier table_identifier{item.first}; |
| 124 | auto version_value = Statistics::getVersion(context, table_identifier); |
| 125 | Int64 version = version_value.has_value() ? version_value.value().convertTo<Int64>() : 0; |
| 126 | if (version != item.second) |
| 127 | { |
| 128 | cached.remove(query_hash); |
| 129 | return nullptr; |
| 130 | } |
| 131 | } |
| 132 | PlanNodeId max_id; |
| 133 | auto root = PlanCacheManager::getNewPlanNode(plan_object->plan_root, context, false, max_id); |
| 134 | CTEInfo cte_info; |
| 135 | for (auto & cte : plan_object->cte_map) |
| 136 | cte_info.add(cte.first, PlanCacheManager::getNewPlanNode(cte.second, context, false, max_id)); |
| 137 | |
| 138 | if (plan_object->query_info && context->hasQueryContext()) |
| 139 | { |
| 140 | for (auto & [database, table_info] : plan_object->query_info->query_access_info) |
| 141 | { |
| 142 | for (auto & [table, columns] : table_info) |
| 143 | context->addQueryAccessInfo(database, table, columns); |
| 144 | } |
| 145 | } |
| 146 | |
| 147 | context->createPlanNodeIdAllocator(max_id+1); |
| 148 | return std::make_unique<QueryPlan>(root, cte_info, context->getPlanNodeIdAllocator()); |
| 149 | } |
| 150 | catch (...) |
| 151 | { |
| 152 | cached.remove(query_hash); |
| 153 | return nullptr; |
| 154 | } |
| 155 | } |
| 156 | |
| 157 | bool PlanCacheManager::addPlanToCache(UInt128 query_hash, QueryPlanPtr & plan, AnalysisPtr analysis, ContextMutablePtr & context) |
| 158 | { |
nothing calls this directly
no test coverage detected