Skip to content

Commit

Permalink
Add comment on ugly hack
Browse files Browse the repository at this point in the history
  • Loading branch information
mamcx committed Jun 26, 2024
1 parent dd7b165 commit 2b58358
Show file tree
Hide file tree
Showing 3 changed files with 124 additions and 2 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,110 @@
---
source: crates/bindings/tests/deps.rs
assertion_line: 16
expression: "cargo tree -p spacetimedb -f {lib} -e no-dev"
---
total crates: 57
spacetimedb
├── derive_more
│ ├── convert_case
│ ├── proc_macro2
│ │ └── unicode_ident
│ ├── quote
│ │ └── proc_macro2 (*)
│ └── syn
│ ├── proc_macro2 (*)
│ ├── quote (*)
│ └── unicode_ident
│ [build-dependencies]
│ └── rustc_version
│ └── semver
├── getrandom
│ ├── cfg_if
│ └── libc
├── log
├── rand
│ ├── libc
│ ├── rand_chacha
│ │ ├── ppv_lite86
│ │ └── rand_core
│ │ └── getrandom (*)
│ └── rand_core (*)
├── scoped_tls
├── spacetimedb_bindings_macro
│ ├── bitflags
│ ├── humantime
│ ├── proc_macro2 (*)
│ ├── quote (*)
│ ├── spacetimedb_primitives
│ │ ├── bitflags
│ │ ├── either
│ │ └── nohash_hasher
│ └── syn
│ ├── proc_macro2 (*)
│ ├── quote (*)
│ └── unicode_ident
├── spacetimedb_bindings_sys
│ └── spacetimedb_primitives (*)
├── spacetimedb_lib
│ ├── anyhow
│ ├── bitflags
│ ├── derive_more (*)
│ ├── enum_as_inner
│ │ ├── heck
│ │ ├── proc_macro2 (*)
│ │ ├── quote (*)
│ │ └── syn (*)
│ ├── hex
│ ├── itertools
│ │ └── either
│ ├── spacetimedb_bindings_macro (*)
│ ├── spacetimedb_primitives (*)
│ ├── spacetimedb_sats
│ │ ├── arrayvec
│ │ ├── bitflags
│ │ ├── decorum
│ │ │ ├── approx
│ │ │ │ └── num_traits
│ │ │ │ [build-dependencies]
│ │ │ │ └── autocfg
│ │ │ └── num_traits (*)
│ │ ├── derive_more (*)
│ │ ├── enum_as_inner (*)
│ │ ├── hex
│ │ ├── itertools (*)
│ │ ├── second_stack
│ │ ├── sha3
│ │ │ ├── digest
│ │ │ │ ├── block_buffer
│ │ │ │ │ └── generic_array
│ │ │ │ │ └── typenum
│ │ │ │ │ [build-dependencies]
│ │ │ │ │ └── version_check
│ │ │ │ └── crypto_common
│ │ │ │ ├── generic_array (*)
│ │ │ │ └── typenum
│ │ │ └── keccak
│ │ │ └── cpufeatures
│ │ │ └── libc
│ │ ├── smallvec
│ │ ├── spacetimedb_bindings_macro (*)
│ │ ├── spacetimedb_data_structures
│ │ │ ├── hashbrown
│ │ │ │ ├── ahash
│ │ │ │ │ ├── cfg_if
│ │ │ │ │ ├── once_cell
│ │ │ │ │ └── zerocopy
│ │ │ │ │ [build-dependencies]
│ │ │ │ │ └── version_check
│ │ │ │ └── allocator_api2
│ │ │ ├── nohash_hasher
│ │ │ └── thiserror
│ │ │ └── thiserror_impl
│ │ │ ├── proc_macro2 (*)
│ │ │ ├── quote (*)
│ │ │ └── syn (*)
│ │ ├── spacetimedb_primitives (*)
│ │ └── thiserror (*)
│ └── thiserror (*)
└── spacetimedb_primitives (*)

11 changes: 9 additions & 2 deletions crates/core/src/sql/compiler.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1088,7 +1088,7 @@ mod tests {
let db = TestDB::durable()?;
db.create_table_for_test(
"PlayerState",
&[("entity_id", AlgebraicType::U64)],
&[("entity_id", AlgebraicType::U64), ("identity", Identity::get_type())],
&[(0.into(), "entity_id")],
)?;
db.create_table_for_test(
Expand All @@ -1098,7 +1098,7 @@ mod tests {
)?;
db.create_table_for_test(
"FriendState",
&[("entity_id", AlgebraicType::U64)],
&[("entity_id", AlgebraicType::U64), ("identity", Identity::get_type())],
&[(0.into(), "entity_id")],
)?;
let sql = "SELECT * FROM PlayerState WHERE entity_id = '161853'";
Expand Down Expand Up @@ -1126,6 +1126,13 @@ mod tests {
Err("SqlError: Type Mismatch: `PlayerState.entity_id: U64` != `String(\"161853\"): String`, executing: `SELECT * FROM PlayerState JOIN FriendState ON PlayerState.entity_id = FriendState.entity_id WHERE PlayerState.entity_id = '161853'`".into())
);

let sql = "SELECT * FROM PlayerState JOIN FriendState ON PlayerState.identity = FriendState.identity WHERE PlayerState.entity_id = '161853'";

assert_eq!(
compile_sql(&db, &db.begin_tx(), sql).map_err(|e| e.to_string()),
Err("SqlError: Type Mismatch: `PlayerState.entity_id: U64` != `String(\"161853\"): String`, executing: `SELECT * FROM PlayerState JOIN FriendState ON PlayerState.entity_id = FriendState.entity_id WHERE PlayerState.entity_id = '161853'`".into())
);

// Check we can still compile the query if we remove the type mismatch and have multiple logical operations.
let sql = "SELECT * FROM PlayerState WHERE entity_id = 1 AND entity_id = 2 AND entity_id = 3 OR entity_id = 4 OR entity_id = 5";

Expand Down
5 changes: 5 additions & 0 deletions crates/core/src/sql/type_check.rs
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,7 @@ fn check_both(op: OpQuery, lhs: &Typed, rhs: &Typed) -> Result<(), PlanError> {
}
}

/// Patch the type of the field if the type is a `Identity`, `Address` or `Enum`
fn patch_type(lhs: &FieldOp, ty_lhs: &mut Typed, ty_rhs: &Typed) -> Result<(), PlanError> {
if let FieldOp::Field(f) = lhs {
if let Some(ty) = ty_rhs.ty() {
Expand Down Expand Up @@ -179,6 +180,10 @@ fn type_check(of: &QueryFragment<FieldOp>) -> Result<Typed, PlanError> {
OpQuery::Logic(op) => (*op).into(),
};

// TODO: For the cases of `Identity, Address, Enum` we need to resolve the type from the value we are comparing,
// because the type is not lifted when we parse the query on `spacetimedb_vm::ops::parse`.
//
// This is a temporary solution until we have a better way to resolve the type of the field.
patch_type(lhs, &mut ty_lhs, &ty_rhs)?;
patch_type(rhs, &mut ty_rhs, &ty_lhs)?;

Expand Down

0 comments on commit 2b58358

Please sign in to comment.