icydb-core 0.141.3

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
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
//! Module: db::executor::projection::materialize::execute
//! Responsibility: row-level projection execution into value rows.
//! Does not own: DISTINCT key storage or structural cursor page dispatch.
//! Boundary: converts retained-slot and data-row inputs into local row views.

#[cfg(test)]
use crate::{
    db::response::ProjectedRow,
    traits::{EntityKind, EntityValue},
    types::Id,
};
use crate::{
    db::{
        data::{CanonicalSlotReader, DataRow},
        executor::{
            projection::{
                eval::{
                    ProjectionEvalError, ScalarProjectionExpr,
                    eval_canonical_scalar_projection_expr_with_required_value_reader_cow,
                },
                materialize::{
                    metrics::ProjectionMaterializationMetricsRecorder,
                    plan::{PreparedProjectionPlan, PreparedProjectionShape},
                    row_view::RowView,
                },
            },
            terminal::{RetainedSlotRow, RowLayout},
        },
    },
    error::InternalError,
    value::Value,
};
use std::borrow::Cow;

#[cfg(test)]
use crate::db::executor::projection::eval::eval_scalar_projection_expr_with_value_reader;
#[cfg(test)]
use crate::db::query::plan::expr::ProjectionSpec;
#[cfg(test)]
use crate::db::query::plan::expr::compile_scalar_projection_expr;

#[cfg(feature = "sql")]
pub(super) fn project_slot_rows(
    prepared_projection: &PreparedProjectionShape,
    rows: Vec<RetainedSlotRow>,
) -> Result<Vec<RowView<'static>>, InternalError> {
    shape_slot_rows(prepared_projection, rows)
}

#[cfg(feature = "sql")]
pub(super) fn project_slot_row(
    prepared_projection: &PreparedProjectionShape,
    row: RetainedSlotRow,
) -> Result<RowView<'static>, InternalError> {
    if let Some(field_slots) = prepared_projection.retained_slot_direct_projection_field_slots() {
        return project_slot_row_from_direct_field_slots(row, field_slots).map(RowView::Owned);
    }

    project_slot_row_dense(prepared_projection, &row).map(RowView::Owned)
}

#[cfg(feature = "sql")]
pub(super) fn project_data_rows(
    row_layout: RowLayout,
    prepared_projection: &PreparedProjectionShape,
    rows: &[DataRow],
    metrics: ProjectionMaterializationMetricsRecorder,
) -> Result<Vec<RowView<'static>>, InternalError> {
    if let Some(field_slots) = prepared_projection.data_row_direct_projection_field_slots() {
        return shape_data_rows_from_direct_field_slots(rows, row_layout, field_slots, metrics);
    }

    let compiled_fields = prepared_projection.scalar_projection_exprs();
    #[cfg(any(test, feature = "diagnostics"))]
    let projected_slot_mask = prepared_projection.projected_slot_mask();
    #[cfg(not(any(test, feature = "diagnostics")))]
    let projected_slot_mask = &[];

    metrics.record_data_rows_scalar_fallback_hit();
    shape_scalar_data_rows(
        compiled_fields,
        rows,
        row_layout,
        projected_slot_mask,
        metrics,
    )
}

#[cfg(feature = "sql")]
pub(super) fn project_data_row(
    row_layout: RowLayout,
    prepared_projection: &PreparedProjectionShape,
    row: &DataRow,
    metrics: ProjectionMaterializationMetricsRecorder,
) -> Result<RowView<'static>, InternalError> {
    if let Some(field_slots) = prepared_projection.data_row_direct_projection_field_slots() {
        return project_data_row_from_direct_field_slots(row_layout, row, field_slots, metrics)
            .map(RowView::Owned);
    }

    let compiled_fields = prepared_projection.scalar_projection_exprs();
    #[cfg(any(test, feature = "diagnostics"))]
    let projected_slot_mask = prepared_projection.projected_slot_mask();
    #[cfg(not(any(test, feature = "diagnostics")))]
    let projected_slot_mask = &[];

    metrics.record_data_rows_scalar_fallback_hit();
    project_scalar_data_row(
        compiled_fields,
        row,
        row_layout,
        projected_slot_mask,
        metrics,
    )
    .map(RowView::Owned)
}

// Decode already-windowed raw data rows into canonical model-field order for
// identity projections. This bypasses scalar projection evaluation and direct
// field-slot projection loops while preserving final `Value` rows.
#[cfg(feature = "sql")]
pub(super) fn project_identity_data_rows(
    row_layout: RowLayout,
    rows: &[DataRow],
    metrics: ProjectionMaterializationMetricsRecorder,
) -> Result<Vec<RowView<'static>>, InternalError> {
    let mut shaped_rows = Vec::with_capacity(rows.len());

    // Phase 1: decode each raw row through the dense full-row contract once.
    for (data_key, raw_row) in rows {
        let values = row_layout.decode_full_value_row(data_key.storage_key(), raw_row)?;
        for _ in 0..values.len() {
            metrics.record_data_rows_slot_access(true);
        }
        shaped_rows.push(RowView::Owned(values));
    }

    Ok(shaped_rows)
}

#[cfg(feature = "sql")]
// Shape one retained slot-row page through either direct field-slot copies or
// the compiled projection evaluator while keeping one row loop.
fn shape_slot_rows(
    prepared_projection: &PreparedProjectionShape,
    rows: Vec<RetainedSlotRow>,
) -> Result<Vec<RowView<'static>>, InternalError> {
    if let Some(field_slots) = prepared_projection.retained_slot_direct_projection_field_slots() {
        return shape_slot_rows_from_direct_field_slots(rows, field_slots);
    }

    shape_slot_rows_dense(prepared_projection, rows)
}

#[cfg(feature = "sql")]
// Shape one dense retained slot-row page through the prepared compiled
// structural projection evaluator without staging another row representation.
fn shape_slot_rows_dense(
    prepared_projection: &PreparedProjectionShape,
    rows: Vec<RetainedSlotRow>,
) -> Result<Vec<RowView<'static>>, InternalError> {
    let projection = prepared_projection.projection();
    let mut shaped_rows = Vec::with_capacity(rows.len());

    // Phase 1: evaluate each retained row once and emit final row elements
    // directly into the selected output representation.
    for row in &rows {
        let projected = project_slot_row_dense(prepared_projection, row)?;
        debug_assert_eq!(projected.len(), projection.len());
        shaped_rows.push(RowView::Owned(projected));
    }

    Ok(shaped_rows)
}

#[cfg(feature = "sql")]
fn project_slot_row_dense(
    prepared_projection: &PreparedProjectionShape,
    row: &RetainedSlotRow,
) -> Result<Vec<Value>, InternalError> {
    let projection = prepared_projection.projection();
    let mut shaped = Vec::with_capacity(projection.len());
    let mut read_slot = |slot: usize| {
        row.slot_ref(slot).map(Cow::Borrowed).ok_or_else(|| {
            ProjectionEvalError::MissingFieldValue {
                field: format!("slot[{slot}]"),
                index: slot,
            }
            .into_invalid_logical_plan_internal_error()
        })
    };
    visit_prepared_projection_values_with_required_value_reader_cow(
        prepared_projection.prepared(),
        &mut read_slot,
        &mut |value| shaped.push(value),
    )?;

    Ok(shaped)
}

#[cfg(feature = "sql")]
// Shape one retained dense slot-row page through direct field-slot copies only.
fn shape_slot_rows_from_direct_field_slots(
    rows: Vec<RetainedSlotRow>,
    field_slots: &[(String, usize)],
) -> Result<Vec<RowView<'static>>, InternalError> {
    let mut shaped_rows = Vec::with_capacity(rows.len());

    // Phase 1: move only requested retained slots into one owned row view per
    // output row. Retained rows do not expose an ordered contiguous projection
    // slice, so the final row still owns the selected values.
    for mut row in rows {
        let mut shaped = Vec::with_capacity(field_slots.len());
        for (field_name, slot) in field_slots {
            let value = row
                .take_slot(*slot)
                .ok_or_else(|| ProjectionEvalError::MissingFieldValue {
                    field: field_name.clone(),
                    index: *slot,
                })
                .map_err(ProjectionEvalError::into_invalid_logical_plan_internal_error)?;
            shaped.push(value);
        }

        shaped_rows.push(RowView::Owned(shaped));
    }

    Ok(shaped_rows)
}

#[cfg(feature = "sql")]
fn project_slot_row_from_direct_field_slots(
    mut row: RetainedSlotRow,
    field_slots: &[(String, usize)],
) -> Result<Vec<Value>, InternalError> {
    if let [field_slot] = field_slots {
        return project_slot_row_from_single_direct_field(row, field_slot).map(|value| vec![value]);
    }

    let mut shaped = Vec::with_capacity(field_slots.len());
    for (field_name, slot) in field_slots {
        let value = row
            .take_slot(*slot)
            .ok_or_else(|| ProjectionEvalError::MissingFieldValue {
                field: field_name.clone(),
                index: *slot,
            })
            .map_err(ProjectionEvalError::into_invalid_logical_plan_internal_error)?;
        shaped.push(value);
    }

    Ok(shaped)
}

#[cfg(feature = "sql")]
fn project_slot_row_from_single_direct_field(
    mut row: RetainedSlotRow,
    (field_name, slot): &(String, usize),
) -> Result<Value, InternalError> {
    row.take_slot(*slot)
        .ok_or_else(|| ProjectionEvalError::MissingFieldValue {
            field: field_name.clone(),
            index: *slot,
        })
        .map_err(ProjectionEvalError::into_invalid_logical_plan_internal_error)
}

#[cfg(feature = "sql")]
// Shape one raw data-row page through direct field-slot copies only.
fn shape_data_rows_from_direct_field_slots(
    rows: &[DataRow],
    row_layout: RowLayout,
    field_slots: &[(String, usize)],
    metrics: ProjectionMaterializationMetricsRecorder,
) -> Result<Vec<RowView<'static>>, InternalError> {
    if let [field_slot] = field_slots {
        return shape_data_rows_from_single_direct_field(rows, row_layout, field_slot, metrics);
    }

    let mut shaped_rows = Vec::with_capacity(rows.len());

    // Phase 1: open each structural row once, then decode only the declared
    // direct field slots into the final output representation.
    for (data_key, raw_row) in rows {
        let row_fields = row_layout.open_raw_row(raw_row)?;
        row_fields.validate_storage_key(data_key)?;

        let mut shaped = Vec::with_capacity(field_slots.len());
        for (_field_name, slot) in field_slots {
            metrics.record_data_rows_slot_access(true);

            let value = row_fields.required_value_by_contract(*slot)?;
            shaped.push(value);
        }
        shaped_rows.push(RowView::Owned(shaped));
    }

    Ok(shaped_rows)
}

// Shape one raw data-row direct slot without the generic multi-field projection
// loop. The single requested value is still materialized at the output boundary.
#[cfg(feature = "sql")]
fn shape_data_rows_from_single_direct_field(
    rows: &[DataRow],
    row_layout: RowLayout,
    field_slot: &(String, usize),
    metrics: ProjectionMaterializationMetricsRecorder,
) -> Result<Vec<RowView<'static>>, InternalError> {
    let mut shaped_rows = Vec::with_capacity(rows.len());

    // Phase 1: validate each row as before, then decode only the one projected
    // slot and emit it directly.
    for row in rows {
        let value =
            project_data_row_from_single_direct_field(row_layout, row, field_slot, metrics)?;
        shaped_rows.push(RowView::Owned(vec![value]));
    }

    Ok(shaped_rows)
}

#[cfg(feature = "sql")]
fn project_data_row_from_direct_field_slots(
    row_layout: RowLayout,
    row: &DataRow,
    field_slots: &[(String, usize)],
    metrics: ProjectionMaterializationMetricsRecorder,
) -> Result<Vec<Value>, InternalError> {
    if let [field_slot] = field_slots {
        return project_data_row_from_single_direct_field(row_layout, row, field_slot, metrics)
            .map(|value| vec![value]);
    }

    let (data_key, raw_row) = row;
    let row_fields = row_layout.open_raw_row(raw_row)?;
    row_fields.validate_storage_key(data_key)?;

    let mut shaped = Vec::with_capacity(field_slots.len());
    for (_field_name, slot) in field_slots {
        metrics.record_data_rows_slot_access(true);

        shaped.push(row_fields.required_value_by_contract(*slot)?);
    }

    Ok(shaped)
}

#[cfg(feature = "sql")]
fn project_data_row_from_single_direct_field(
    row_layout: RowLayout,
    (data_key, raw_row): &DataRow,
    (_field_name, slot): &(String, usize),
    metrics: ProjectionMaterializationMetricsRecorder,
) -> Result<Value, InternalError> {
    let row_fields = row_layout.open_raw_row(raw_row)?;
    row_fields.validate_storage_key(data_key)?;

    metrics.record_data_rows_slot_access(true);

    row_fields.required_value_by_contract(*slot)
}

#[cfg(feature = "sql")]
fn shape_scalar_data_rows(
    compiled_fields: &[ScalarProjectionExpr],
    rows: &[DataRow],
    row_layout: RowLayout,
    projected_slot_mask: &[bool],
    metrics: ProjectionMaterializationMetricsRecorder,
) -> Result<Vec<RowView<'static>>, InternalError> {
    let mut shaped_rows = Vec::with_capacity(rows.len());

    #[cfg(not(any(test, feature = "diagnostics")))]
    let _ = projected_slot_mask;

    // Phase 1: evaluate fully scalar projections through the compiled scalar
    // expression path once and emit final row elements immediately.
    for (data_key, raw_row) in rows {
        let row_fields = row_layout.open_raw_row(raw_row)?;
        row_fields.validate_storage_key(data_key)?;

        let mut shaped = Vec::with_capacity(compiled_fields.len());
        for compiled in compiled_fields {
            let value = eval_canonical_scalar_projection_expr_with_required_value_reader_cow(
                compiled,
                &mut |slot| {
                    metrics.record_data_rows_slot_access(
                        projected_slot_mask.get(slot).copied().unwrap_or(false),
                    );

                    row_fields.required_value_by_contract_cow(slot)
                },
            )?;
            shaped.push(value.into_owned());
        }
        shaped_rows.push(RowView::Owned(shaped));
    }

    Ok(shaped_rows)
}

#[cfg(feature = "sql")]
fn project_scalar_data_row(
    compiled_fields: &[ScalarProjectionExpr],
    (data_key, raw_row): &DataRow,
    row_layout: RowLayout,
    projected_slot_mask: &[bool],
    metrics: ProjectionMaterializationMetricsRecorder,
) -> Result<Vec<Value>, InternalError> {
    #[cfg(not(any(test, feature = "diagnostics")))]
    let _ = projected_slot_mask;

    let row_fields = row_layout.open_raw_row(raw_row)?;
    row_fields.validate_storage_key(data_key)?;

    let mut shaped = Vec::with_capacity(compiled_fields.len());
    for compiled in compiled_fields {
        let value = eval_canonical_scalar_projection_expr_with_required_value_reader_cow(
            compiled,
            &mut |slot| {
                metrics.record_data_rows_slot_access(
                    projected_slot_mask.get(slot).copied().unwrap_or(false),
                );

                row_fields.required_value_by_contract_cow(slot)
            },
        )?;
        shaped.push(value.into_owned());
    }

    Ok(shaped)
}

#[cfg(test)]
pub(in crate::db::executor::projection) fn project_rows_from_projection<E>(
    projection: &ProjectionSpec,
    rows: &[(Id<E>, E)],
) -> Result<Vec<ProjectedRow<E>>, ProjectionEvalError>
where
    E: EntityKind + EntityValue,
{
    let mut compiled_fields = Vec::with_capacity(projection.len());
    for field in projection.fields() {
        let compiled = compile_scalar_projection_expr(E::MODEL, field.expr()).expect(
            "test projection materialization helpers require scalar-compilable expressions",
        );
        compiled_fields.push(compiled);
    }
    let prepared = PreparedProjectionPlan::Scalar(compiled_fields);
    let mut projected_rows = Vec::with_capacity(rows.len());
    for (id, entity) in rows {
        let mut values = Vec::with_capacity(projection.len());
        let mut read_slot = |slot| entity.get_value_by_index(slot);
        visit_prepared_projection_values_with_value_reader(
            &prepared,
            &mut read_slot,
            &mut |value| values.push(value),
        )?;
        projected_rows.push(ProjectedRow::from_runtime_values(*id, values));
    }

    Ok(projected_rows)
}

#[cfg(test)]
pub(super) fn visit_prepared_projection_values_with_value_reader(
    prepared: &PreparedProjectionPlan,
    read_slot: &mut dyn FnMut(usize) -> Option<Value>,
    on_value: &mut dyn FnMut(Value),
) -> Result<(), ProjectionEvalError> {
    let PreparedProjectionPlan::Scalar(compiled_fields) = prepared;
    for compiled in compiled_fields {
        on_value(eval_scalar_projection_expr_with_value_reader(
            compiled, read_slot,
        )?);
    }

    Ok(())
}

// Walk one prepared projection plan through one reader that can borrow slot
// values from retained structural rows until an expression needs ownership.
pub(in crate::db) fn visit_prepared_projection_values_with_required_value_reader_cow<'a>(
    prepared: &'a PreparedProjectionPlan,
    read_slot: &mut dyn FnMut(usize) -> Result<Cow<'a, Value>, InternalError>,
    on_value: &mut dyn FnMut(Value),
) -> Result<(), InternalError> {
    let PreparedProjectionPlan::Scalar(compiled_fields) = prepared;
    for compiled in compiled_fields {
        on_value(
            eval_canonical_scalar_projection_expr_with_required_value_reader_cow(
                compiled, read_slot,
            )?
            .into_owned(),
        );
    }

    Ok(())
}