icydb_core/db/session/query/
explain.rs1use crate::{
7 db::{
8 DbSession, Query, QueryError, QueryTracePlan, TraceExecutionFamily,
9 access::summarize_executable_access_plan,
10 executor::{EntityAuthority, ExecutionFamily},
11 query::builder::{AggregateExplain, ProjectionExplain},
12 query::explain::{
13 ExplainAggregateTerminalPlan, ExplainExecutionNodeDescriptor, ExplainPlan,
14 },
15 query::plan::{AccessPlannedQuery, QueryMode, VisibleIndexes},
16 schema::SchemaInfo,
17 session::query::{QueryPlanCacheAttribution, query_plan_cache_reuse_event},
18 },
19 traits::{CanisterKind, EntityKind, EntityValue},
20};
21
22const fn trace_execution_family_from_executor(family: ExecutionFamily) -> TraceExecutionFamily {
25 match family {
26 ExecutionFamily::PrimaryKey => TraceExecutionFamily::PrimaryKey,
27 ExecutionFamily::Ordered => TraceExecutionFamily::Ordered,
28 ExecutionFamily::Grouped => TraceExecutionFamily::Grouped,
29 }
30}
31
32impl<C: CanisterKind> DbSession<C> {
33 fn try_map_cached_logical_query_plan<E, T>(
36 &self,
37 query: &Query<E>,
38 map: impl FnOnce(&AccessPlannedQuery) -> Result<T, QueryError>,
39 ) -> Result<T, QueryError>
40 where
41 E: EntityKind<Canister = C>,
42 {
43 self.try_map_cached_shared_query_plan_ref_for_entity::<E, T>(query, |prepared_plan| {
44 map(prepared_plan.logical_plan())
45 })
46 }
47
48 fn cached_execution_explain_plan<E>(
52 &self,
53 query: &Query<E>,
54 visible_indexes: &VisibleIndexes<'_>,
55 ) -> Result<
56 (
57 AccessPlannedQuery,
58 EntityAuthority,
59 QueryPlanCacheAttribution,
60 ),
61 QueryError,
62 >
63 where
64 E: EntityKind<Canister = C>,
65 {
66 let (prepared_plan, cache_attribution) =
67 self.cached_shared_query_plan_for_entity::<E>(query)?;
68 let mut plan = prepared_plan.logical_plan().clone();
69 let accepted_schema = self
70 .ensure_accepted_schema_snapshot::<E>()
71 .map_err(QueryError::execute)?;
72 let schema_info = SchemaInfo::from_accepted_snapshot_for_model_with_expression_indexes(
73 query.structural().model(),
74 &accepted_schema,
75 true,
76 );
77
78 plan.finalize_access_choice_for_model_with_accepted_indexes_and_schema(
79 query.structural().model(),
80 visible_indexes.accepted_field_path_indexes(),
81 visible_indexes.accepted_expression_indexes(),
82 &schema_info,
83 );
84 let authority = Self::accepted_entity_authority_for_schema::<E>(&accepted_schema)
85 .map_err(QueryError::execute)?;
86
87 Ok((plan, authority, cache_attribution))
88 }
89
90 pub(in crate::db) fn explain_query_with_visible_indexes<E>(
92 &self,
93 query: &Query<E>,
94 ) -> Result<ExplainPlan, QueryError>
95 where
96 E: EntityKind<Canister = C>,
97 {
98 self.try_map_cached_logical_query_plan(query, |plan| Ok(plan.explain()))
99 }
100
101 pub(in crate::db) fn query_plan_hash_hex_with_visible_indexes<E>(
104 &self,
105 query: &Query<E>,
106 ) -> Result<String, QueryError>
107 where
108 E: EntityKind<Canister = C>,
109 {
110 self.try_map_cached_logical_query_plan(query, |plan| Ok(plan.fingerprint().to_string()))
111 }
112
113 pub(in crate::db) fn explain_query_execution_with_visible_indexes<E>(
116 &self,
117 query: &Query<E>,
118 ) -> Result<ExplainExecutionNodeDescriptor, QueryError>
119 where
120 E: EntityValue + EntityKind<Canister = C>,
121 {
122 self.with_query_visible_indexes(query, |query, visible_indexes| {
123 let (plan, authority, _) =
124 self.cached_execution_explain_plan::<E>(query, visible_indexes)?;
125
126 query
127 .structural()
128 .explain_execution_descriptor_from_plan_with_authority(&plan, &authority)
129 })
130 }
131
132 pub(in crate::db) fn explain_query_execution_verbose_with_visible_indexes<E>(
135 &self,
136 query: &Query<E>,
137 ) -> Result<String, QueryError>
138 where
139 E: EntityValue + EntityKind<Canister = C>,
140 {
141 self.with_query_visible_indexes(query, |query, visible_indexes| {
142 let (plan, authority, cache_attribution) =
143 self.cached_execution_explain_plan::<E>(query, visible_indexes)?;
144
145 query
146 .structural()
147 .finalized_execution_diagnostics_from_plan_with_authority_and_descriptor_mutator(
148 &plan,
149 &authority,
150 Some(query_plan_cache_reuse_event(cache_attribution)),
151 |_| {},
152 )
153 .map(|diagnostics| diagnostics.render_text_verbose())
154 })
155 }
156
157 pub(in crate::db) fn explain_query_prepared_aggregate_terminal_with_visible_indexes<E, S>(
160 &self,
161 query: &Query<E>,
162 strategy: &S,
163 ) -> Result<ExplainAggregateTerminalPlan, QueryError>
164 where
165 E: EntityValue + EntityKind<Canister = C>,
166 S: AggregateExplain,
167 {
168 let (plan, _) = self.cached_prepared_query_plan_for_entity::<E>(query)?;
169
170 plan.explain_prepared_aggregate_terminal(strategy)
171 }
172
173 pub(in crate::db) fn explain_query_bytes_by_with_visible_indexes<E>(
176 &self,
177 query: &Query<E>,
178 target_field: &str,
179 ) -> Result<ExplainExecutionNodeDescriptor, QueryError>
180 where
181 E: EntityValue + EntityKind<Canister = C>,
182 {
183 let (plan, _) = self.cached_prepared_query_plan_for_entity::<E>(query)?;
184
185 plan.explain_bytes_by_terminal(target_field)
186 }
187
188 pub(in crate::db) fn explain_query_prepared_projection_terminal_with_visible_indexes<E, S>(
191 &self,
192 query: &Query<E>,
193 strategy: &S,
194 ) -> Result<ExplainExecutionNodeDescriptor, QueryError>
195 where
196 E: EntityValue + EntityKind<Canister = C>,
197 S: ProjectionExplain,
198 {
199 let (plan, _) = self.cached_prepared_query_plan_for_entity::<E>(query)?;
200
201 plan.explain_prepared_projection_terminal(strategy)
202 }
203
204 pub fn trace_query<E>(&self, query: &Query<E>) -> Result<QueryTracePlan, QueryError>
209 where
210 E: EntityKind<Canister = C>,
211 {
212 let (prepared_plan, cache_attribution) =
213 self.cached_prepared_query_plan_for_entity::<E>(query)?;
214 let logical_plan = prepared_plan.logical_plan();
215 let explain = logical_plan.explain();
216 let plan_hash = logical_plan.fingerprint().to_string();
217 let executable_access = prepared_plan.access().executable_contract();
218 let access_strategy = summarize_executable_access_plan(&executable_access);
219 let execution_family = match prepared_plan.mode() {
220 QueryMode::Load(_) => Some(trace_execution_family_from_executor(
221 prepared_plan
222 .execution_family()
223 .map_err(QueryError::execute)?,
224 )),
225 QueryMode::Delete(_) => None,
226 };
227 let reuse = query_plan_cache_reuse_event(cache_attribution);
228
229 Ok(QueryTracePlan::new(
230 plan_hash,
231 access_strategy,
232 execution_family,
233 reuse,
234 explain,
235 ))
236 }
237}