Skip to content

Make Timestamp and TimeDuration implement FilterableValue #2695

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 2 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions crates/lib/src/filterable_value.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
use crate::{ConnectionId, Identity};
use core::ops;
use spacetimedb_sats::bsatn;
use spacetimedb_sats::time_duration::TimeDuration;
use spacetimedb_sats::timestamp::Timestamp;
use spacetimedb_sats::{hash::Hash, i256, u256, Serialize};

/// Types which can appear as an argument to an index filtering operation
Expand Down Expand Up @@ -97,6 +99,9 @@ impl_filterable_value! {
ConnectionId: Copy,
Hash: Copy,

Timestamp: Copy,
TimeDuration: Copy,

// Some day we will likely also want to support `Vec<u8>` and `[u8]`,
// as they have trivial portable equality and ordering,
// but @RReverser's proposed filtering rules do not include them.
Expand Down
19 changes: 19 additions & 0 deletions modules/module-test/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,22 @@ pub enum TestF {
Baz(String),
}

#[spacetimedb::table(
name = test_g,
index(name = created_at_index, btree(columns = [name, created_at])),
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Add a test case for this index as well

)]
#[derive(Debug)]
pub struct TestG {
#[primary_key]
#[auto_inc]
id: u64,

name: String,

#[index(btree)]
created_at: Timestamp,
}

// // All tables are private by default.
const _: () = assert!(matches!(get_table_access(test_e::test_e), TableAccess::Private));

Expand Down Expand Up @@ -363,6 +379,9 @@ fn test_btree_index_args(ctx: &ReducerContext) {
ctx.db.test_e().name().delete(&string);
ctx.db.test_e().name().delete("str");

// Testing if it compiles with a range of timestamps.
let _ = ctx.db.test_g().created_at().filter(..ctx.timestamp);

// ctx.db.test_e().name().delete(string); // SHOULD FAIL

// Multi-column i64 index on `points.x, points.y`:
Expand Down
3 changes: 3 additions & 0 deletions modules/sdk-test-cs/Lib.cs
Original file line number Diff line number Diff line change
Expand Up @@ -320,13 +320,16 @@ public static void insert_one_connection_id(ReducerContext ctx, ConnectionId a)
[SpacetimeDB.Table(Name = "one_timestamp", Public = true)]
public partial struct OneTimestamp
{
[SpacetimeDB.Index.BTree]
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Replicate the test in module-test-cs instead of sdk-test-cs

public Timestamp t;
}

[SpacetimeDB.Reducer]
public static void insert_one_timestamp(ReducerContext ctx, Timestamp t)
{
ctx.Db.one_timestamp.Insert(new OneTimestamp { t = t });

var _count = ctx.Db.one_timestamp.t.Filter((t, ctx.Timestamp)).Count();
}

[SpacetimeDB.Table(Name = "one_simple_enum", Public = true)]
Expand Down