icydb-core 0.182.6

IcyDB — A schema-first typed query engine and persistence runtime for Internet Computer canisters
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
//! Module: db::session::sql::execute::select
//! Responsibility: SQL SELECT projection, grouped execution, and cache-aware
//! prepared-plan execution.
//! Does not own: SQL command routing, write execution, or EXPLAIN rendering.
//! Boundary: keeps SELECT plan-to-result adaptation out of the SQL execution hub.

#[cfg(feature = "diagnostics")]
use crate::db::session::sql::{SqlExecutePhaseAttribution, measure_sql_stage};
use crate::{
    db::{
        DbSession, PersistedRow, QueryError,
        executor::{
            EntityAuthority, SharedPreparedExecutionPlan, StructuralGroupedProjectionResult,
        },
        query::intent::StructuralQuery,
        schema::AcceptedSchemaSnapshot,
        session::{
            finalize_structural_grouped_projection_result,
            sql::SqlProjectionContract,
            sql::projection::{SqlProjectionPayload, execute_sql_projection_rows_for_canister},
            sql::{SqlCacheAttribution, SqlCompiledCommandExecutionContext, SqlStatementResult},
            sql_grouped_cursor_from_bytes,
        },
    },
    traits::{CanisterKind, EntityValue},
};

use super::diagnostics::GroupedSqlDiagnosticsCollector;
#[cfg(feature = "diagnostics")]
use super::diagnostics::measure_execute_phase_with_physical_access;

impl<C: CanisterKind> DbSession<C> {
    // Convert one grouped executor result plus SQL projection labels into the
    // statement result shape shared by normal and diagnostics SQL execution.
    pub(in crate::db::session::sql::execute) fn grouped_sql_statement_result_from_result(
        columns: Vec<String>,
        fixed_scales: Vec<Option<u32>>,
        result: StructuralGroupedProjectionResult,
    ) -> Result<SqlStatementResult, QueryError> {
        let row_count = result.row_count();
        let grouped = finalize_structural_grouped_projection_result(result, None)?;
        let (rows, continuation_cursor, _) = grouped.into_rows_cursor_and_trace();
        let next_cursor = sql_grouped_cursor_from_bytes(continuation_cursor);

        Ok(SqlStatementResult::Grouped {
            columns,
            fixed_scales,
            rows,
            row_count,
            next_cursor,
        })
    }

    // Execute one SQL projection from one shared lower prepared plan plus
    // one thin SQL projection contract so cached and explicit-bypass paths
    // share the same final row-materialization shell.
    fn execute_sql_projection_from_structural_prepared_plan(
        &self,
        prepared_plan: SharedPreparedExecutionPlan,
        projection: SqlProjectionContract,
        cache_attribution: SqlCacheAttribution,
    ) -> Result<(SqlProjectionPayload, SqlCacheAttribution), QueryError> {
        let (columns, fixed_scales) = projection.into_components();
        let (rows, row_count) =
            execute_sql_projection_rows_for_canister(&self.db, self.debug, prepared_plan)
                .map_err(QueryError::execute)?;

        Ok((
            SqlProjectionPayload::new(columns, fixed_scales, rows, row_count),
            cache_attribution,
        ))
    }

    // Execute one SQL projection and immediately shape it into the public
    // statement-result envelope. Diagnostics keeps using the payload-returning
    // sibling so it can measure response finalization separately.
    fn execute_sql_statement_from_structural_prepared_plan(
        &self,
        prepared_plan: SharedPreparedExecutionPlan,
        projection: SqlProjectionContract,
        cache_attribution: SqlCacheAttribution,
    ) -> Result<(SqlStatementResult, SqlCacheAttribution), QueryError> {
        let (payload, cache_attribution) = self
            .execute_sql_projection_from_structural_prepared_plan(
                prepared_plan,
                projection,
                cache_attribution,
            )?;

        Ok((payload.into_statement_result(), cache_attribution))
    }

    // Execute one grouped SQL statement from one shared lowered prepared plan
    // plus one thin SQL projection contract. Normal and diagnostics surfaces
    // share this plan-to-statement shell; diagnostics only swaps response
    // finalization through the optional collector.
    fn execute_grouped_sql_core<T>(
        &self,
        prepared_plan: SharedPreparedExecutionPlan,
        projection: SqlProjectionContract,
        diagnostics: Option<GroupedSqlDiagnosticsCollector<'_>>,
        execute_grouped: impl FnOnce(
            &Self,
            SharedPreparedExecutionPlan,
        )
            -> Result<(StructuralGroupedProjectionResult, T), QueryError>,
    ) -> Result<(SqlStatementResult, T), QueryError> {
        let (columns, fixed_scales) = projection.into_components();
        let (result, extra) = execute_grouped(self, prepared_plan)?;
        let statement_result = if let Some(diagnostics) = diagnostics {
            diagnostics.finalize_grouped_sql_statement::<C>(columns, fixed_scales, result)?
        } else {
            Self::grouped_sql_statement_result_from_result(columns, fixed_scales, result)?
        };

        Ok((statement_result, extra))
    }

    // Execute one grouped SQL statement through the shared grouped SQL core
    // without diagnostics response attribution.
    fn execute_grouped_sql_statement_from_prepared_plan<T>(
        &self,
        prepared_plan: SharedPreparedExecutionPlan,
        projection: SqlProjectionContract,
        execute_grouped: impl FnOnce(
            &Self,
            SharedPreparedExecutionPlan,
        )
            -> Result<(StructuralGroupedProjectionResult, T), QueryError>,
    ) -> Result<(SqlStatementResult, T), QueryError> {
        self.execute_grouped_sql_core(prepared_plan, projection, None, execute_grouped)
    }

    // Diagnostics-only grouped SQL execution split that keeps runtime
    // invocation and session response-envelope finalization in separate
    // counters while sharing the same grouped SQL core as normal execution.
    #[cfg(feature = "diagnostics")]
    fn execute_grouped_sql_statement_with_response_attribution<T>(
        &self,
        prepared_plan: SharedPreparedExecutionPlan,
        projection: SqlProjectionContract,
        execute_grouped: impl FnOnce(
            &Self,
            SharedPreparedExecutionPlan,
        )
            -> Result<(StructuralGroupedProjectionResult, T), QueryError>,
    ) -> Result<(SqlStatementResult, T, u64), QueryError> {
        let mut response_finalization_local_instructions = 0;
        let diagnostics =
            GroupedSqlDiagnosticsCollector::new(&mut response_finalization_local_instructions);
        let (statement_result, extra) = self.execute_grouped_sql_core(
            prepared_plan,
            projection,
            Some(diagnostics),
            execute_grouped,
        )?;

        Ok((
            statement_result,
            extra,
            response_finalization_local_instructions,
        ))
    }

    // Execute one SQL load query from a structural lowered query through the
    // shared lower query-plan cache while bypassing only the compiled SQL
    // command cache for lowered or aggregate-only paths.
    pub(in crate::db::session::sql) fn execute_sql_projection_from_structural_query_without_sql_compiled_cache(
        &self,
        query: StructuralQuery,
        authority: EntityAuthority,
        accepted_schema: &AcceptedSchemaSnapshot,
    ) -> Result<(SqlProjectionPayload, SqlCacheAttribution), QueryError> {
        let (prepared_plan, projection, cache_attribution) = self
            .sql_select_prepared_plan_for_accepted_authority(&query, authority, accepted_schema)?;

        self.execute_sql_projection_from_structural_prepared_plan(
            prepared_plan,
            projection,
            cache_attribution,
        )
    }

    #[cfg(feature = "diagnostics")]
    pub(super) fn execute_select_compiled_sql_with_phase_attribution_from_resolver<E>(
        &self,
        query: &StructuralQuery,
        resolve_plan: impl FnOnce() -> Result<
            (
                SharedPreparedExecutionPlan,
                SqlProjectionContract,
                SqlCacheAttribution,
            ),
            QueryError,
        >,
    ) -> Result<
        (
            SqlStatementResult,
            SqlCacheAttribution,
            SqlExecutePhaseAttribution,
        ),
        QueryError,
    >
    where
        E: PersistedRow<Canister = C> + EntityValue,
    {
        if query.has_grouping() {
            let (planner_local_instructions, resolved_query_plan) = measure_sql_stage(resolve_plan);
            let (prepared_plan, projection, cache_attribution) = resolved_query_plan?;

            let ((execute_local_instructions, store_local_instructions), statement_result) =
                measure_execute_phase_with_physical_access(move || {
                    self.execute_grouped_sql_statement_with_response_attribution(
                        prepared_plan,
                        projection,
                        |session, prepared_plan| {
                            let plan = prepared_plan.typed_clone::<E>();
                            session
                                .execute_grouped_with_cursor(plan, None, |executor, plan, cursor| {
                                    executor.execute_grouped_paged_with_cursor_traced_with_phase_attribution(
                                        plan, cursor,
                                    )
                                })
                                .map(|(result, _trace, phase_attribution)| {
                                    (result, phase_attribution)
                                })
                        },
                    )
                });
            let (
                statement_result,
                grouped_phase_attribution,
                response_finalization_local_instructions,
            ) = statement_result?;

            return Ok((
                statement_result,
                cache_attribution,
                SqlExecutePhaseAttribution {
                    planner_local_instructions,
                    store_local_instructions,
                    executor_invocation_local_instructions: execute_local_instructions
                        .saturating_sub(response_finalization_local_instructions),
                    executor_local_instructions: execute_local_instructions
                        .saturating_sub(store_local_instructions)
                        .saturating_sub(response_finalization_local_instructions),
                    response_finalization_local_instructions,
                    grouped_stream_local_instructions: grouped_phase_attribution
                        .stream_local_instructions,
                    grouped_fold_local_instructions: grouped_phase_attribution
                        .fold_local_instructions,
                    grouped_finalize_local_instructions: grouped_phase_attribution
                        .finalize_local_instructions,
                    grouped_count: grouped_phase_attribution.grouped_count,
                    scalar_aggregate_terminal:
                        crate::db::executor::ScalarAggregateTerminalAttribution::none(),
                },
            ));
        }

        let (planner_local_instructions, resolved_query_plan) = measure_sql_stage(resolve_plan);
        let (prepared_plan, projection, cache_attribution) = resolved_query_plan?;

        let ((execute_local_instructions, store_local_instructions), payload) =
            measure_execute_phase_with_physical_access(move || {
                self.execute_sql_projection_from_structural_prepared_plan(
                    prepared_plan,
                    projection,
                    SqlCacheAttribution::default(),
                )
                .map(|(payload, _)| payload)
            });
        let payload = payload?;
        let (response_finalization_local_instructions, statement_result) =
            measure_sql_stage(|| Ok::<_, QueryError>(payload.into_statement_result()));
        let statement_result = statement_result?;

        Ok((
            statement_result,
            cache_attribution,
            SqlExecutePhaseAttribution {
                planner_local_instructions,
                store_local_instructions,
                executor_invocation_local_instructions: execute_local_instructions,
                executor_local_instructions: execute_local_instructions
                    .saturating_sub(store_local_instructions),
                response_finalization_local_instructions,
                grouped_stream_local_instructions: 0,
                grouped_fold_local_instructions: 0,
                grouped_finalize_local_instructions: 0,
                grouped_count: crate::db::executor::GroupedCountAttribution::none(),
                scalar_aggregate_terminal:
                    crate::db::executor::ScalarAggregateTerminalAttribution::none(),
            },
        ))
    }

    pub(super) fn execute_select_compiled_sql_with_cache_attribution<E>(
        &self,
        query: &StructuralQuery,
    ) -> Result<(SqlStatementResult, SqlCacheAttribution), QueryError>
    where
        E: PersistedRow<Canister = C> + EntityValue,
    {
        let catalog = self
            .accepted_schema_catalog_context_for_query::<E>()
            .map_err(QueryError::execute)?;
        let authority = catalog
            .accepted_entity_authority_for::<E>()
            .map_err(QueryError::execute)?;

        let (prepared_plan, projection, cache_attribution) = self
            .sql_select_prepared_plan_for_accepted_authority_with_schema_fingerprint(
                query,
                authority,
                catalog.snapshot(),
                catalog.fingerprint(),
            )?;

        self.execute_select_compiled_sql_from_prepared_plan::<E>(
            query,
            prepared_plan,
            projection,
            cache_attribution,
        )
    }

    pub(super) fn execute_select_compiled_sql_with_context<E>(
        &self,
        query: &StructuralQuery,
        context: &SqlCompiledCommandExecutionContext,
    ) -> Result<(SqlStatementResult, SqlCacheAttribution), QueryError>
    where
        E: PersistedRow<Canister = C> + EntityValue,
    {
        if let Some((prepared_plan, projection)) = context.command().cached_select_plan(
            context.schema_fingerprint_method_version(),
            context.schema_fingerprint(),
        ) {
            return self.execute_select_compiled_sql_from_prepared_plan::<E>(
                query,
                prepared_plan,
                projection,
                SqlCacheAttribution::shared_query_plan_cache_hit(),
            );
        }

        let authority = match context.accepted_authority() {
            Some(authority) => authority.clone(),
            None => context
                .accepted_catalog()
                .accepted_entity_authority_for::<E>()
                .map_err(QueryError::execute)?,
        };

        let resolved = self
            .sql_select_prepared_plan_for_accepted_authority_with_schema_fingerprint(
                query,
                authority,
                context.accepted_schema(),
                context.schema_fingerprint(),
            );
        if let Ok((prepared_plan, projection, _)) = &resolved {
            context.command().set_cached_select_plan(
                context.schema_fingerprint_method_version(),
                context.schema_fingerprint(),
                prepared_plan.clone(),
                projection.clone(),
            );
        }
        let (prepared_plan, projection, cache_attribution) = resolved?;

        self.execute_select_compiled_sql_from_prepared_plan::<E>(
            query,
            prepared_plan,
            projection,
            cache_attribution,
        )
    }

    fn execute_select_compiled_sql_from_prepared_plan<E>(
        &self,
        query: &StructuralQuery,
        prepared_plan: SharedPreparedExecutionPlan,
        projection: SqlProjectionContract,
        cache_attribution: SqlCacheAttribution,
    ) -> Result<(SqlStatementResult, SqlCacheAttribution), QueryError>
    where
        E: PersistedRow<Canister = C> + EntityValue,
    {
        if query.has_grouping() {
            let (statement_result, ()) = self.execute_grouped_sql_statement_from_prepared_plan(
                prepared_plan,
                projection,
                |session, prepared_plan| {
                    let plan = prepared_plan.typed_clone::<E>();
                    session
                        .execute_grouped_with_trace(plan, None)
                        .map(|(result, _trace)| (result, ()))
                },
            )?;

            return Ok((statement_result, cache_attribution));
        }

        self.execute_sql_statement_from_structural_prepared_plan(
            prepared_plan,
            projection,
            cache_attribution,
        )
    }
}