Skip to content

Commit 608efaf

Browse files
committed
More comments
1 parent 1b9facb commit 608efaf

File tree

3 files changed

+64
-31
lines changed

3 files changed

+64
-31
lines changed

crates/lib/src/db/raw_def/v9.rs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -366,18 +366,18 @@ pub enum RawMiscModuleExportV9 {
366366
ColumnDefaultValue(RawColumnDefaultValueV9),
367367
}
368368

369-
/// Marks a field as having a particular default.
369+
/// Marks a particular table's column as having a particular default.
370370
#[derive(Debug, Clone, SpacetimeType)]
371371
#[sats(crate = crate)]
372372
#[cfg_attr(feature = "test", derive(PartialEq, Eq, PartialOrd, Ord))]
373373
pub struct RawColumnDefaultValueV9 {
374+
/// Identifies which table that has the default value.
375+
/// This corresponds to `name` in `RawTableDefV9`.
374376
pub table: RawIdentifier,
375-
376-
/// Must be the index of a valid column within `ty`.
377+
/// Identifies which column of `table` that has the default value.
377378
pub col_id: ColId,
378-
379-
/// A BSATN-encoded [`AlgebraicValue`] valid at `ty`.
380-
/// (We can't use `AlgebraicValue` directly because it isn't serializable!)
379+
/// A BSATN-encoded [`AlgebraicValue`] valid at the table column's type.
380+
/// (We cannot use `AlgebraicValue` directly as it isn't `Spacetimetype`.)
381381
pub value: Box<[u8]>,
382382
}
383383

crates/schema/src/def.rs

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1116,7 +1116,7 @@ mod tests {
11161116
true,
11171117
)
11181118
.with_default_column_value(1, AlgebraicValue::Bool(false))
1119-
.with_default_column_value(1, AlgebraicValue::U16(10))
1119+
.with_default_column_value(1, AlgebraicValue::unit())
11201120
.finish();
11211121

11221122
let result: Result<ModuleDef, ValidationErrors> = old_builder.finish().try_into();
@@ -1126,5 +1126,10 @@ mod tests {
11261126
result,
11271127
ValidationError::ColumnDefaultValueMalformed { table, col_id, .. } => *table == apples.clone().into() && *col_id == ColId(1)
11281128
);
1129+
assert!(result.is_err_and(|e| e
1130+
.into_iter()
1131+
.filter(|e| matches!(e, ValidationError::ColumnDefaultValueMalformed { .. }))
1132+
.count()
1133+
== 2))
11291134
}
11301135
}

crates/schema/src/def/validate/v9.rs

Lines changed: 52 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,7 @@ pub fn validate(def: RawModuleDefV9) -> Result<ModuleDef> {
8080
.combine_errors()
8181
.and_then(|(mut tables, types, reducers)| {
8282
let sched_exists = check_scheduled_reducers_exist(&tables, &reducers);
83-
let default_values_work = default_values_work(misc_exports, &mut validator, &mut tables);
83+
let default_values_work = proccess_misc_exports(misc_exports, &validator, &mut tables);
8484
(sched_exists, default_values_work).combine_errors()?;
8585

8686
Ok((tables, types, reducers))
@@ -348,21 +348,19 @@ impl ModuleValidator<'_> {
348348
}
349349

350350
fn validate_column_default_value(
351-
&mut self,
352-
tables: &mut HashMap<Identifier, TableDef>,
353-
cdv: RawColumnDefaultValueV9,
354-
) -> Result<()> {
351+
&self,
352+
tables: &HashMap<Identifier, TableDef>,
353+
cdv: &RawColumnDefaultValueV9,
354+
) -> Result<AlgebraicValue> {
355355
let table_name = identifier(cdv.table.clone())?;
356356

357357
// Extract the table. We cannot make progress otherwise.
358-
let table = tables
359-
.get_mut(&table_name)
360-
.ok_or_else(|| ValidationError::TableNotFound {
361-
table: cdv.table.clone(),
362-
})?;
358+
let table = tables.get(&table_name).ok_or_else(|| ValidationError::TableNotFound {
359+
table: cdv.table.clone(),
360+
})?;
363361

364362
// Get the column that a default is being added to.
365-
let Some(col) = table.columns.get_mut(cdv.col_id.idx()) else {
363+
let Some(col) = table.columns.get(cdv.col_id.idx()) else {
366364
return Err(ValidationError::ColumnNotFound {
367365
table: cdv.table.clone(),
368366
def: cdv.table.clone(),
@@ -383,17 +381,8 @@ impl ModuleValidator<'_> {
383381
}
384382
.into()
385383
});
386-
// Ensure there's only one default value.
387-
if col.default_value.is_some() {
388-
return Err(ValidationError::MultipleColumnDefaultValues {
389-
table: cdv.table.clone(),
390-
col_id: cdv.col_id,
391-
}
392-
.into());
393-
}
394-
col.default_value = field_value.ok();
395384

396-
Ok(())
385+
field_value
397386
}
398387

399388
/// Validate a type definition.
@@ -944,20 +933,59 @@ fn check_scheduled_reducers_exist(
944933
.collect_all_errors()
945934
}
946935

947-
fn default_values_work(
936+
fn proccess_misc_exports(
948937
misc_exports: Vec<RawMiscModuleExportV9>,
949-
validator: &mut ModuleValidator,
938+
validator: &ModuleValidator,
950939
tables: &mut IdentifierMap<TableDef>,
951940
) -> Result<()> {
952941
misc_exports
953942
.into_iter()
954943
.map(|export| match export {
955-
RawMiscModuleExportV9::ColumnDefaultValue(fdv) => validator.validate_column_default_value(tables, fdv),
944+
RawMiscModuleExportV9::ColumnDefaultValue(cdv) => process_column_default_value(&cdv, validator, tables),
956945
_ => unimplemented!("unknown misc export"),
957946
})
958947
.collect_all_errors::<()>()
959948
}
960949

950+
fn process_column_default_value(
951+
cdv: &RawColumnDefaultValueV9,
952+
validator: &ModuleValidator,
953+
tables: &mut IdentifierMap<TableDef>,
954+
) -> Result<()> {
955+
// Validate the default value
956+
let validated_value = validator.validate_column_default_value(tables, cdv)?;
957+
958+
let table_name = identifier(cdv.table.clone())?;
959+
let table = tables
960+
.get_mut(&table_name)
961+
.ok_or_else(|| ValidationError::TableNotFound {
962+
table: cdv.table.clone(),
963+
})?;
964+
965+
let column = table
966+
.columns
967+
.get_mut(cdv.col_id.idx())
968+
.ok_or_else(|| ValidationError::ColumnNotFound {
969+
table: cdv.table.clone(),
970+
def: cdv.table.clone(),
971+
column: cdv.col_id,
972+
})?;
973+
974+
// Ensure there's only one default value.
975+
if column.default_value.is_some() {
976+
return Err(ValidationError::MultipleColumnDefaultValues {
977+
table: cdv.table.clone(),
978+
col_id: cdv.col_id,
979+
}
980+
.into());
981+
}
982+
983+
// Set the default value
984+
column.default_value = Some(validated_value);
985+
986+
Ok(())
987+
}
988+
961989
#[cfg(test)]
962990
mod tests {
963991
use crate::def::validate::tests::{

0 commit comments

Comments
 (0)