reddb-io-engine 1.27.0

Shared RedDB query execution and storage primitives for native and WebAssembly runtimes.
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
use super::{query_error, Result};
use crate::{
    catalog::{
        enforcement::{CollectionContractEnforcer, ContractError, INTERNAL_TTL_METADATA_KEYS},
        schema::resolved_contract_columns,
        CollectionContract, CollectionModel,
    },
    query::{
        evaluator,
        local_read::{self, host::ReadHost, LocalReadError, LOCAL_READ_ROWS_MAX},
        planning::aggregation::validate_expression_depth,
        records::runtime_table_record_from_entity_ref_with_schema,
    },
    storage::{
        entity::{
            compute_entity_field_bloom, EntityData, EntityId, RowData, UnifiedEntity,
            FIRST_USER_ENTITY_ID,
        },
        resident_store::ResidentStore,
        store_wal::StoreWalAction,
    },
};
use reddb_rql::ast::{
    CreateTableQuery, Expr, FieldRef, InsertEntityType, QueryExpr, SelectItem, TableQuery,
    UpdateTarget,
};
use reddb_types::{DataType, Value};
use std::{
    collections::{BTreeMap, HashMap, HashSet},
    sync::{atomic::Ordering, Arc},
};

fn unsupported() -> LocalReadError {
    LocalReadError::Unsupported("local writes require ordinary tables without TTL, AI/events, encrypted columns, RETURNING or conflict clauses")
}
fn contract_error(error: ContractError) -> LocalReadError {
    query_error(error.to_string())
}
fn public_name(name: &str) -> Result<()> {
    if name.starts_with('_')
        || name.starts_with("red.")
        || crate::query::runtime::is_universal_entity_source(name)
    {
        return Err(unsupported());
    }
    Ok(())
}
fn user_column(name: &str) -> Result<()> {
    if name.starts_with('_')
        || [
            "rid",
            "entity_id",
            "collection",
            "kind",
            "tenant",
            "created_at",
            "updated_at",
        ]
        .iter()
        .any(|reserved| name.eq_ignore_ascii_case(reserved))
    {
        return Err(query_error(format!("reserved system field '{name}'")));
    }
    Ok(())
}

pub(super) fn admit_create(query: &CreateTableQuery) -> Result<()> {
    public_name(&query.name)?;
    if query.collection_model != CollectionModel::Table
        || query.default_ttl_ms.is_some()
        || query.ai_policy.is_some()
        || !query.subscriptions.is_empty()
        || query.partition_by.is_some()
        || query.tenant_by.is_some()
        || query.context_index_enabled
        || !query.context_index_fields.is_empty()
        || !query.analytics_config.is_empty()
        || !query.metrics_rollup_policies.is_empty()
        || query.vault_own_master_key
    {
        return Err(unsupported());
    }
    for column in &query.columns {
        user_column(&column.name)?;
    }
    let contract = crate::catalog::ddl::collection_contract_from_create_table(query, 0)
        .map_err(query_error)?;
    admit_contract(&contract)
}

fn admit_contract(contract: &CollectionContract) -> Result<()> {
    if contract.declared_model != CollectionModel::Table
        || contract.default_ttl_ms.is_some()
        || contract.retention_duration_ms.is_some()
        || contract.ai_policy.is_some()
        || !contract.subscriptions.is_empty()
        || contract.context_index_enabled
        || !contract.context_index_fields.is_empty()
    {
        return Err(unsupported());
    }
    for column in resolved_contract_columns(contract).map_err(|error| query_error(error.message))? {
        if matches!(column.data_type, DataType::Secret | DataType::Password) {
            return Err(unsupported());
        }
    }
    Ok(())
}

pub(super) fn plan(
    statement: &QueryExpr,
    resident: &ResidentStore,
    contracts: &HashMap<String, CollectionContract>,
    now_ms: u64,
) -> Result<Vec<StoreWalAction>> {
    let (table, expressions): (&str, Vec<&Expr>) = match statement {
        QueryExpr::Insert(query) => {
            if query.entity_type != InsertEntityType::Row
                || query.on_conflict.is_some()
                || query.returning.is_some()
                || query.ttl_ms.is_some()
                || query.expires_at_ms.is_some()
                || !query.with_metadata.is_empty()
                || query.auto_embed.is_some()
                || query.suppress_events
            {
                return Err(unsupported());
            }
            (&query.table, query.value_exprs.iter().flatten().collect())
        }
        QueryExpr::Update(query) => {
            if query.target != UpdateTarget::Rows
                || query.returning.is_some()
                || query.ttl_ms.is_some()
                || query.expires_at_ms.is_some()
                || !query.with_metadata.is_empty()
                || query.claim_limit.is_some()
                || query.claim_exact
                || !query.order_by.is_empty()
                || query.limit.is_some()
                || query.suppress_events
                || query.compound_assignment_ops.iter().any(Option::is_some)
                || (query.filter.is_some() && query.where_expr.is_none())
            {
                return Err(unsupported());
            }
            (
                &query.table,
                query
                    .assignment_exprs
                    .iter()
                    .map(|(_, expression)| expression)
                    .chain(query.where_expr.iter())
                    .collect(),
            )
        }
        QueryExpr::Delete(query) => {
            if query.returning.is_some()
                || query.suppress_events
                || (query.filter.is_some() && query.where_expr.is_none())
            {
                return Err(unsupported());
            }
            (&query.table, query.where_expr.iter().collect())
        }
        _ => unreachable!("only admitted mutation statements reach planning"),
    };
    public_name(table)?;
    let contract = contracts.get(table).ok_or_else(|| {
        LocalReadError::Catalog(format!("no persisted collection contract for {table}"))
    })?;
    admit_contract(contract)?;
    let columns =
        resolved_contract_columns(contract).map_err(|error| query_error(error.message))?;
    let mut query = TableQuery::new(table);
    for expression in expressions {
        validate_expression_depth(expression).map_err(query_error)?;
        query.select_items.push(SelectItem::Expr {
            expr: expression.clone(),
            alias: None,
        });
    }
    local_read::admission::expressions(&query, &columns)?;
    let manager = resident
        .get_collection(table)
        .ok_or_else(|| LocalReadError::Catalog(format!("collection {table} is missing")))?;
    let mut entities = Vec::new();
    let mut exceeded = false;
    manager.scan_for_each(None, |entity| {
        if entities.len() == LOCAL_READ_ROWS_MAX {
            exceeded = true;
            return false;
        }
        entities.push(entity.clone());
        true
    });
    if exceeded {
        return Err(LocalReadError::ResourceLimit);
    }
    for entity in &entities {
        if !matches!(entity.data, EntityData::Row(_))
            || manager.get_metadata(entity.id).is_some_and(|metadata| {
                INTERNAL_TTL_METADATA_KEYS
                    .iter()
                    .any(|key| metadata.has(key))
            })
        {
            return Err(unsupported());
        }
    }
    let enforcer = CollectionContractEnforcer::new(contract);
    let schema = Arc::new(
        columns
            .iter()
            .map(|column| column.name.clone())
            .collect::<Vec<_>>(),
    );
    let host = ReadHost::new(&query, &columns, &[], Vec::new());
    let context = Context { schema, host };
    let mut actions = Vec::new();
    let mut final_fields: Vec<_> = entities
        .iter()
        .map(|entity| fields(entity, &context.schema))
        .collect();
    match statement {
        QueryExpr::Insert(insert) => {
            let names = if insert.columns.is_empty() {
                contract
                    .declared_columns
                    .iter()
                    .filter(|column| {
                        column.generated.is_none()
                            && !(contract.timestamps_enabled
                                && matches!(column.name.as_str(), "created_at" | "updated_at"))
                    })
                    .map(|column| column.name.clone())
                    .collect::<Vec<_>>()
            } else {
                insert.columns.clone()
            };
            check_columns(contract, &names)?;
            if insert.value_exprs.is_empty()
                || entities
                    .len()
                    .checked_add(insert.value_exprs.len())
                    .is_none_or(|count| count > LOCAL_READ_ROWS_MAX)
            {
                return Err(LocalReadError::ResourceLimit);
            }
            let first_id = resident
                .next_entity_id
                .load(Ordering::SeqCst)
                .max(FIRST_USER_ENTITY_ID);
            for (index, row) in insert.value_exprs.iter().enumerate() {
                if row.len() != names.len() {
                    return Err(query_error("INSERT column/value counts differ"));
                }
                let values = row
                    .iter()
                    .map(|expression| {
                        evaluator::evaluate(expression, &|_: &FieldRef| None)
                            .map_err(LocalReadError::from)
                    })
                    .collect::<Result<Vec<_>>>()?;
                let fields = enforcer
                    .normalize_insert_at(names.iter().cloned().zip(values).collect(), now_ms)
                    .map_err(contract_error)?;
                let id = first_id
                    .checked_add(u64::try_from(index).expect("bounded row index"))
                    .filter(|id| *id < u64::MAX)
                    .ok_or_else(|| query_error("entity ID space exhausted"))?;
                let mut entity = UnifiedEntity::table_row(EntityId::new(id), table, id, Vec::new());
                entity.created_at = now_ms / 1000;
                entity.updated_at = now_ms / 1000;
                entity.sequence_id = id;
                entity.ensure_table_logical_id();
                set_fields(&mut entity, &fields);
                actions.push(StoreWalAction::upsert_entity(
                    table,
                    &entity,
                    None,
                    reddb_file::STORE_VERSION_CURRENT,
                ));
                final_fields.push(fields);
            }
        }
        QueryExpr::Update(update) => {
            if contract.append_only {
                return Err(query_error("append-only tables cannot be updated"));
            }
            check_columns(
                contract,
                &update
                    .assignment_exprs
                    .iter()
                    .map(|(name, _)| name.clone())
                    .collect::<Vec<_>>(),
            )?;
            if update.assignment_exprs.is_empty() {
                return Err(query_error("UPDATE requires assignments"));
            }
            for (index, original) in entities.iter().enumerate() {
                if !context.matches(update.where_expr.as_ref(), original)? {
                    continue;
                }
                let mut fields: BTreeMap<_, _> = final_fields[index].iter().cloned().collect();
                // All RHS expressions see the same row pre-image.
                for (name, expression) in &update.assignment_exprs {
                    fields.insert(name.clone(), context.evaluate(expression, original)?);
                }
                let fields = enforcer
                    .normalize_update_at(fields.into_iter().collect(), now_ms)
                    .map_err(contract_error)?;
                let mut entity = original.clone();
                entity.updated_at = now_ms / 1000;
                set_fields(&mut entity, &fields);
                actions.push(StoreWalAction::upsert_entity(
                    table,
                    &entity,
                    manager.get_metadata(original.id).as_ref(),
                    reddb_file::STORE_VERSION_CURRENT,
                ));
                final_fields[index] = fields;
            }
        }
        QueryExpr::Delete(delete) => {
            if contract.append_only {
                return Err(query_error("append-only tables cannot be deleted"));
            }
            for entity in &entities {
                if context.matches(delete.where_expr.as_ref(), entity)? {
                    actions.push(StoreWalAction::DeleteEntityRecord {
                        collection: table.into(),
                        entity_id: entity.id.raw(),
                    });
                }
            }
            // Deletion cannot introduce a uniqueness conflict.
            return Ok(actions);
        }
        _ => unreachable!("mutation variant was validated"),
    }
    enforcer
        .enforce_batch_uniqueness(&final_fields)
        .map_err(contract_error)?;
    Ok(actions)
}

fn check_columns(contract: &CollectionContract, names: &[String]) -> Result<()> {
    let mut seen = HashSet::new();
    for name in names {
        user_column(name)?;
        let Some(column) = contract
            .declared_columns
            .iter()
            .find(|column| &column.name == name)
        else {
            return Err(query_error(format!("unknown column {name}")));
        };
        if column.generated.is_some() {
            return Err(query_error(format!(
                "generated column '{name}' cannot be assigned"
            )));
        }
        if !seen.insert(name) {
            return Err(query_error(format!("duplicate column {name}")));
        }
    }
    Ok(())
}
fn fields(entity: &UnifiedEntity, schema: &[String]) -> Vec<(String, Value)> {
    let EntityData::Row(row) = &entity.data else {
        unreachable!("table rows were admitted")
    };
    if row.named.is_some() || row.schema.is_some() {
        return row
            .iter_fields()
            .map(|(name, value)| (name.into(), value.clone()))
            .collect();
    }
    schema
        .iter()
        .cloned()
        .zip(row.columns.iter().cloned())
        .collect()
}
fn set_fields(entity: &mut UnifiedEntity, fields: &[(String, Value)]) {
    let (names, values) = fields.iter().cloned().unzip();
    entity.data = EntityData::Row(RowData::with_names(values, names));
    entity.field_bloom = compute_entity_field_bloom(&entity.data);
}
struct Context<'a> {
    schema: Arc<Vec<String>>,
    host: ReadHost<'a>,
}
impl Context<'_> {
    fn evaluate(&self, expression: &Expr, entity: &UnifiedEntity) -> Result<Value> {
        let record = runtime_table_record_from_entity_ref_with_schema(entity, Some(&self.schema))
            .ok_or_else(|| query_error("table row could not be materialized"))?;
        self.host
            .evaluate(expression, &record)
            .map_err(LocalReadError::from)
    }
    fn matches(&self, predicate: Option<&Expr>, entity: &UnifiedEntity) -> Result<bool> {
        match predicate
            .map(|predicate| self.evaluate(predicate, entity))
            .transpose()?
        {
            None | Some(Value::Boolean(true)) => Ok(true),
            Some(Value::Boolean(false) | Value::Null) => Ok(false),
            _ => Err(query_error("WHERE must evaluate to boolean or NULL")),
        }
    }
}