Skip to content

Commit

Permalink
flatten BuiltinValue into AlgebraicValue (#389)
Browse files Browse the repository at this point in the history
  • Loading branch information
Centril authored Oct 9, 2023
1 parent 099d0b8 commit ff69ec4
Show file tree
Hide file tree
Showing 19 changed files with 502 additions and 734 deletions.
13 changes: 6 additions & 7 deletions crates/bench/src/spacetime_module.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use spacetimedb::db::datastore::traits::TableSchema;
use spacetimedb::db::{Config, FsyncPolicy, Storage};
use spacetimedb_lib::sats::{product, BuiltinValue};
use spacetimedb_lib::sats::product;
use spacetimedb_lib::{sats::ArrayValue, AlgebraicValue, ProductValue};
use spacetimedb_testing::modules::{start_runtime, CompiledModule, ModuleHandle};
use tokio::runtime::Runtime;
Expand Down Expand Up @@ -150,9 +150,8 @@ impl BenchDatabase for SpacetimeModule {
}

fn insert_bulk<T: BenchTable>(&mut self, table_id: &Self::TableId, rows: Vec<T>) -> ResultBench<()> {
let args = product![AlgebraicValue::ArrayOf(ArrayValue::Product(
rows.into_iter().map(|row| row.into_product_value()).collect(),
))];
let rows = rows.into_iter().map(|row| row.into_product_value()).collect();
let args = product![ArrayValue::Product(rows)];
let SpacetimeModule { runtime, module } = self;
let module = module.as_mut().unwrap();
let reducer_name = format!("insert_bulk_{}", table_id.snake_case);
Expand Down Expand Up @@ -217,9 +216,9 @@ impl BenchDatabase for SpacetimeModule {
let column = &table.columns[column_index as usize].col_name;

let value = match value {
AlgebraicValue::Builtin(BuiltinValue::U32(x)) => x.to_string(),
AlgebraicValue::Builtin(BuiltinValue::U64(x)) => x.to_string(),
AlgebraicValue::Builtin(BuiltinValue::String(x)) => format!("'{}'", x),
AlgebraicValue::U32(x) => x.to_string(),
AlgebraicValue::U64(x) => x.to_string(),
AlgebraicValue::String(x) => format!("'{}'", x),
_ => {
unreachable!()
}
Expand Down
10 changes: 5 additions & 5 deletions crates/bench/src/spacetime_raw.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@ use spacetimedb::db::datastore::traits::{IndexDef, TableDef, TableSchema};
use spacetimedb::db::relational_db::{open_db, RelationalDB};
use spacetimedb::error::DBError;
use spacetimedb::sql::execute::run;
use spacetimedb_lib::AlgebraicValue;
use spacetimedb_lib::{identity::AuthCtx, sats::BuiltinValue};
use spacetimedb_lib::identity::AuthCtx;
use spacetimedb_lib::sats::AlgebraicValue;
use std::hint::black_box;
use tempdir::TempDir;

Expand Down Expand Up @@ -150,9 +150,9 @@ impl BenchDatabase for SpacetimeRaw {
let table_name = &table.table_name;

let value = match value {
AlgebraicValue::Builtin(BuiltinValue::U32(x)) => x.to_string(),
AlgebraicValue::Builtin(BuiltinValue::U64(x)) => x.to_string(),
AlgebraicValue::Builtin(BuiltinValue::String(x)) => format!("'{}'", x),
AlgebraicValue::U32(x) => x.to_string(),
AlgebraicValue::U64(x) => x.to_string(),
AlgebraicValue::String(x) => format!("'{}'", x),
_ => {
unreachable!()
}
Expand Down
8 changes: 4 additions & 4 deletions crates/bench/src/sqlite.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ use ahash::AHashMap;
use lazy_static::lazy_static;
use rusqlite::Connection;
use spacetimedb::db::datastore::traits::TableSchema;
use spacetimedb_lib::{sats::BuiltinValue, AlgebraicType, AlgebraicValue, ProductType};
use spacetimedb_lib::sats::{AlgebraicType, AlgebraicValue, ProductType};
use std::{
fmt::Write,
hint::black_box,
Expand Down Expand Up @@ -188,19 +188,19 @@ impl BenchDatabase for SQLite {

begin.execute(())?;
match value {
AlgebraicValue::Builtin(BuiltinValue::String(value)) => {
AlgebraicValue::String(value) => {
for _ in stmt.query_map((&*value,), |row| {
black_box(row);
Ok(())
})? {}
}
AlgebraicValue::Builtin(BuiltinValue::U32(value)) => {
AlgebraicValue::U32(value) => {
for _ in stmt.query_map((value,), |row| {
black_box(row);
Ok(())
})? {}
}
AlgebraicValue::Builtin(BuiltinValue::U64(value)) => {
AlgebraicValue::U64(value) => {
for _ in stmt.query_map((value,), |row| {
black_box(row);
Ok(())
Expand Down
26 changes: 13 additions & 13 deletions crates/core/src/db/datastore/locking_tx_datastore/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ use spacetimedb_lib::{
relation::RelValue,
DataKey, Hash,
};
use spacetimedb_sats::{AlgebraicType, AlgebraicValue, BuiltinValue, ProductType, ProductValue};
use spacetimedb_sats::{AlgebraicType, AlgebraicValue, ProductType, ProductValue};
use thiserror::Error;

#[derive(Error, Debug, PartialEq, Eq)]
Expand Down Expand Up @@ -1168,18 +1168,18 @@ impl Inner {
/// Check if the value is one of the `numeric` types and is `0`.
fn can_replace_with_sequence(value: &AlgebraicValue) -> bool {
match value {
AlgebraicValue::Builtin(BuiltinValue::I8(x)) => *x == 0,
AlgebraicValue::Builtin(BuiltinValue::U8(x)) => *x == 0,
AlgebraicValue::Builtin(BuiltinValue::I16(x)) => *x == 0,
AlgebraicValue::Builtin(BuiltinValue::U16(x)) => *x == 0,
AlgebraicValue::Builtin(BuiltinValue::I32(x)) => *x == 0,
AlgebraicValue::Builtin(BuiltinValue::U32(x)) => *x == 0,
AlgebraicValue::Builtin(BuiltinValue::I64(x)) => *x == 0,
AlgebraicValue::Builtin(BuiltinValue::U64(x)) => *x == 0,
AlgebraicValue::Builtin(BuiltinValue::I128(x)) => *x == 0,
AlgebraicValue::Builtin(BuiltinValue::U128(x)) => *x == 0,
AlgebraicValue::Builtin(BuiltinValue::F32(x)) => *x == 0.0,
AlgebraicValue::Builtin(BuiltinValue::F64(x)) => *x == 0.0,
AlgebraicValue::I8(x) => *x == 0,
AlgebraicValue::U8(x) => *x == 0,
AlgebraicValue::I16(x) => *x == 0,
AlgebraicValue::U16(x) => *x == 0,
AlgebraicValue::I32(x) => *x == 0,
AlgebraicValue::U32(x) => *x == 0,
AlgebraicValue::I64(x) => *x == 0,
AlgebraicValue::U64(x) => *x == 0,
AlgebraicValue::I128(x) => *x == 0,
AlgebraicValue::U128(x) => *x == 0,
AlgebraicValue::F32(x) => *x == 0.0,
AlgebraicValue::F64(x) => *x == 0.0,
_ => false,
}
}
Expand Down
10 changes: 5 additions & 5 deletions crates/core/src/db/datastore/system_tables.rs
Original file line number Diff line number Diff line change
Expand Up @@ -798,7 +798,7 @@ impl<Name: AsRef<str>> From<&StIndexRow<Name>> for ProductValue {
product![
x.index_id,
x.table_id,
AlgebraicValue::ArrayOf(x.cols.clone()),
ArrayValue::from(x.cols.clone()),
AlgebraicValue::String(x.index_name.as_ref().to_string()),
x.is_unique
]
Expand Down Expand Up @@ -941,11 +941,11 @@ impl<'a> TryFrom<&'a ProductValue> for StConstraintRow<&'a str> {
impl<Name: AsRef<str>> From<&StConstraintRow<Name>> for ProductValue {
fn from(x: &StConstraintRow<Name>) -> Self {
product![
AlgebraicValue::U32(x.constraint_id),
x.constraint_id,
AlgebraicValue::String(x.constraint_name.as_ref().to_string()),
AlgebraicValue::U8(x.kind.bits()),
AlgebraicValue::U32(x.table_id),
AlgebraicValue::ArrayOf(x.columns.clone())
x.kind.bits(),
x.table_id,
ArrayValue::from(x.columns.clone())
]
}
}
Expand Down
22 changes: 11 additions & 11 deletions crates/core/src/sql/compiler.rs
Original file line number Diff line number Diff line change
Expand Up @@ -487,7 +487,7 @@ mod tests {
auth::{StAccess, StTableType},
error::ResultTest,
};
use spacetimedb_sats::{AlgebraicType, BuiltinValue};
use spacetimedb_sats::AlgebraicType;
use spacetimedb_vm::expr::{IndexScan, JoinExpr, Query};

use crate::db::{
Expand Down Expand Up @@ -852,8 +852,8 @@ mod tests {
let Query::IndexScan(IndexScan {
table: DbTable { table_id, .. },
col_id: 0,
lower_bound: Bound::Included(AlgebraicValue::Builtin(BuiltinValue::U64(3))),
upper_bound: Bound::Included(AlgebraicValue::Builtin(BuiltinValue::U64(3))),
lower_bound: Bound::Included(AlgebraicValue::U64(3)),
upper_bound: Bound::Included(AlgebraicValue::U64(3)),
}) = query[0]
else {
panic!("unexpected operator {:#?}", query[0]);
Expand Down Expand Up @@ -937,7 +937,7 @@ mod tests {
assert_eq!(table, "lhs");
assert_eq!(field, "a");

let ColumnOp::Field(FieldExpr::Value(AlgebraicValue::Builtin(BuiltinValue::U64(3)))) = **rhs else {
let ColumnOp::Field(FieldExpr::Value(AlgebraicValue::U64(3))) = **rhs else {
panic!("unexpected right hand side {:#?}", **rhs);
};

Expand Down Expand Up @@ -1046,7 +1046,7 @@ mod tests {
assert_eq!(table, "rhs");
assert_eq!(field, "c");

let ColumnOp::Field(FieldExpr::Value(AlgebraicValue::Builtin(BuiltinValue::U64(3)))) = **rhs else {
let ColumnOp::Field(FieldExpr::Value(AlgebraicValue::U64(3))) = **rhs else {
panic!("unexpected right hand side {:#?}", **rhs);
};
Ok(())
Expand Down Expand Up @@ -1088,8 +1088,8 @@ mod tests {
let Query::IndexScan(IndexScan {
table: DbTable { table_id, .. },
col_id: 0,
lower_bound: Bound::Included(AlgebraicValue::Builtin(BuiltinValue::U64(3))),
upper_bound: Bound::Included(AlgebraicValue::Builtin(BuiltinValue::U64(3))),
lower_bound: Bound::Included(AlgebraicValue::U64(3)),
upper_bound: Bound::Included(AlgebraicValue::U64(3)),
}) = query[0]
else {
panic!("unexpected operator {:#?}", query[0]);
Expand Down Expand Up @@ -1132,7 +1132,7 @@ mod tests {
table: DbTable { table_id, .. },
col_id: 1,
lower_bound: Bound::Unbounded,
upper_bound: Bound::Excluded(AlgebraicValue::Builtin(BuiltinValue::U64(4))),
upper_bound: Bound::Excluded(AlgebraicValue::U64(4)),
}) = rhs[0]
else {
panic!("unexpected operator {:#?}", rhs[0]);
Expand Down Expand Up @@ -1209,8 +1209,8 @@ mod tests {
let Query::IndexScan(IndexScan {
table: DbTable { table_id, .. },
col_id: 1,
lower_bound: Bound::Excluded(AlgebraicValue::Builtin(BuiltinValue::U64(2))),
upper_bound: Bound::Excluded(AlgebraicValue::Builtin(BuiltinValue::U64(4))),
lower_bound: Bound::Excluded(AlgebraicValue::U64(2)),
upper_bound: Bound::Excluded(AlgebraicValue::U64(4)),
}) = rhs[0]
else {
panic!("unexpected operator {:#?}", rhs[0]);
Expand All @@ -1235,7 +1235,7 @@ mod tests {
assert_eq!(table, "rhs");
assert_eq!(field, "d");

let ColumnOp::Field(FieldExpr::Value(AlgebraicValue::Builtin(BuiltinValue::U64(3)))) = **value else {
let ColumnOp::Field(FieldExpr::Value(AlgebraicValue::U64(3))) = **value else {
panic!("unexpected right hand side {:#?}", value);
};
Ok(())
Expand Down
6 changes: 2 additions & 4 deletions crates/core/src/subscription/subscription.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use spacetimedb_lib::auth::{StAccess, StTableType};
use spacetimedb_lib::identity::AuthCtx;
use spacetimedb_lib::relation::RelValue;
use spacetimedb_lib::PrimaryKey;
use spacetimedb_sats::{AlgebraicValue, BuiltinValue};
use spacetimedb_sats::AlgebraicValue;
use spacetimedb_vm::expr::QueryExpr;
use std::collections::HashSet;

Expand Down Expand Up @@ -113,9 +113,7 @@ impl QuerySet {
for mut row in result.data {
//Hack: remove the hidden field OP_TYPE_FIELD_NAME. see `to_mem_table`
// Needs to be done before calculating the PK.
let op_type = if let AlgebraicValue::Builtin(BuiltinValue::U8(op)) =
row.data.elements.remove(pos_op_type)
{
let op_type = if let AlgebraicValue::U8(op) = row.data.elements.remove(pos_op_type) {
op
} else {
panic!("Fail to extract `{OP_TYPE_FIELD_NAME}` on `{}`", result.head.table_name)
Expand Down
Loading

0 comments on commit ff69ec4

Please sign in to comment.