Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
19 commits
Select commit Hold shift + click to select a range
92218be
feat: support option values in Rust index filters
Mr-Dust0 May 4, 2026
efb1507
fix(ts): compare option values in btree cache filters
Mr-Dust0 May 4, 2026
b789158
test(csharp): cover nullable btree index generation
Mr-Dust0 May 4, 2026
1e358f9
Merge branch 'master' into issue-4824-filterable-option
Mr-Dust0 Jun 3, 2026
b405f79
Merge remote-tracking branch 'origin/master' into issue-4824-filterab…
Mr-Dust0 Jun 5, 2026
a0d18c6
Merge branch 'master' into issue-4824-filterable-option
Mr-Dust0 Jun 8, 2026
aac5f7b
Merge branch 'master' into issue-4824-filterable-option
Mr-Dust0 Jun 8, 2026
e06a8e4
Merge branch 'master' into issue-4824-filterable-option
Mr-Dust0 Jun 8, 2026
692955f
Merge branch 'master' into issue-4824-filterable-option
Mr-Dust0 Jun 10, 2026
b5a7b27
Merge branch 'master' into issue-4824-filterable-option
Mr-Dust0 Jun 11, 2026
396ab9b
Merge branch 'master' into issue-4824-filterable-option
Mr-Dust0 Jun 12, 2026
399ab80
Merge branch 'master' into issue-4824-filterable-option
Mr-Dust0 Jun 14, 2026
6f3081d
Merge branch 'master' into issue-4824-filterable-option
Mr-Dust0 Jun 15, 2026
859d1fc
Merge branch 'master' into issue-4824-filterable-option
Mr-Dust0 Jun 16, 2026
b9d5771
Merge branch 'master' into issue-4824-filterable-option
Mr-Dust0 Jun 20, 2026
20cbf4d
Merge branch 'master' into issue-4824-filterable-option
Mr-Dust0 Jun 24, 2026
f234818
Make `Option`s filterable on clients, and add SDK tests.
gefjon Jun 26, 2026
e64e7c0
Remove debugging `println`s
gefjon Jun 26, 2026
c795344
Fix C# + C++ SDKs for making `Option`s filterable and expand `sdk-tes…
JasonAtClockwork Jul 7, 2026
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
The table of contents is too big for display.
Diff view
Diff view
  •  
  •  
  •  
37 changes: 23 additions & 14 deletions crates/bindings-cpp/include/spacetimedb/table_with_constraints.h
Original file line number Diff line number Diff line change
Expand Up @@ -210,21 +210,30 @@ struct MultiColumnIndexTag {
// Constraint Concepts
// =============================================================================

namespace detail {
template<typename T>
struct filterable_value_impl
: std::bool_constant<
std::integral<T> ||
std::same_as<T, std::string> ||
std::same_as<T, SpacetimeDB::Identity> ||
std::same_as<T, SpacetimeDB::ConnectionId> ||
std::same_as<T, SpacetimeDB::Timestamp> ||
std::same_as<T, SpacetimeDB::Uuid> ||
std::same_as<T, SpacetimeDB::I128> ||
std::same_as<T, SpacetimeDB::U128> ||
std::same_as<T, SpacetimeDB::I256> ||
std::same_as<T, SpacetimeDB::U256> ||
std::same_as<T, SpacetimeDB::i256> ||
std::same_as<T, SpacetimeDB::u256> ||
std::is_enum_v<T>> {};

template<typename T>
struct filterable_value_impl<std::optional<T>> : filterable_value_impl<std::remove_cvref_t<T>> {};
}

template<typename T>
concept FilterableValue =
std::integral<T> ||
std::same_as<T, std::string> ||
std::same_as<T, SpacetimeDB::Identity> ||
std::same_as<T, SpacetimeDB::ConnectionId> ||
std::same_as<T, SpacetimeDB::Timestamp> ||
std::same_as<T, SpacetimeDB::Uuid> ||
std::same_as<T, SpacetimeDB::I128> ||
std::same_as<T, SpacetimeDB::U128> ||
std::same_as<T, SpacetimeDB::I256> ||
std::same_as<T, SpacetimeDB::U256> ||
std::same_as<T, SpacetimeDB::i256> ||
std::same_as<T, SpacetimeDB::u256> ||
std::is_enum_v<T>;
concept FilterableValue = detail::filterable_value_impl<std::remove_cvref_t<T>>::value;

template<typename T>
concept AutoIncrementable =
Expand Down
111 changes: 111 additions & 0 deletions crates/bindings-csharp/Codegen.Tests/Tests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -354,6 +354,117 @@ public static void @params(ProcedureContext ctx)
Assert.Empty(GetCompilationErrors(compilationAfterGen));
}

[Fact]
public static async Task NullableBTreeIndexesCompile()
{
var fixture = await Fixture.Compile("server");

const string source = """
using SpacetimeDB;

[SpacetimeDB.Table]
public partial struct NullableBTreeIndex
{
[SpacetimeDB.PrimaryKey]
public uint Id;

[SpacetimeDB.Index.BTree]
public uint? AccountId;

[SpacetimeDB.Reducer]
public static void TestNullableBTreeIndex(ReducerContext ctx)
{
_ = ctx.Db.NullableBTreeIndex.AccountId.Filter((uint?)null);
_ = ctx.Db.NullableBTreeIndex.AccountId.Filter((uint?)55);
_ = ctx.Db.NullableBTreeIndex.AccountId.Filter(new Bound<uint?>(null, 99));
}
}

[SpacetimeDB.Table]
public partial struct NullableUniqueIndex
{
[SpacetimeDB.PrimaryKey]
public uint Id;

[SpacetimeDB.Unique]
public uint? AccountId;

[SpacetimeDB.Unique]
public string? Name;

[SpacetimeDB.Unique]
public SpacetimeDB.Uuid? ExternalId;

[SpacetimeDB.Reducer]
public static void TestNullableUniqueIndex(ReducerContext ctx)
{
_ = ctx.Db.NullableUniqueIndex.AccountId.Find((uint?)null);
_ = ctx.Db.NullableUniqueIndex.AccountId.Find((uint?)55);
_ = ctx.Db.NullableUniqueIndex.Name.Find((string?)null);
_ = ctx.Db.NullableUniqueIndex.Name.Find("name");
_ = ctx.Db.NullableUniqueIndex.ExternalId.Find((SpacetimeDB.Uuid?)null);
_ = ctx.Db.NullableUniqueIndex.ExternalId.Find(SpacetimeDB.Uuid.NIL);
}
}

[SpacetimeDB.Table]
public partial struct ExplicitNullableBTreeIndex
{
[SpacetimeDB.PrimaryKey]
public uint Id;

[SpacetimeDB.Index.BTree]
public System.Nullable<uint> AccountId;

[SpacetimeDB.Reducer]
public static void TestExplicitNullableBTreeIndex(ReducerContext ctx)
{
_ = ctx.Db.ExplicitNullableBTreeIndex.AccountId.Filter((uint?)null);
_ = ctx.Db.ExplicitNullableBTreeIndex.AccountId.Filter((uint?)55);
_ = ctx.Db.ExplicitNullableBTreeIndex.AccountId.Filter(new Bound<uint?>(null, 99));
}
}

[SpacetimeDB.Table]
public partial struct ExplicitNullableUniqueIndex
{
[SpacetimeDB.PrimaryKey]
public uint Id;

[SpacetimeDB.Unique]
public System.Nullable<uint> AccountId;

[SpacetimeDB.Reducer]
public static void TestExplicitNullableUniqueIndex(ReducerContext ctx)
{
_ = ctx.Db.ExplicitNullableUniqueIndex.AccountId.Find((uint?)null);
_ = ctx.Db.ExplicitNullableUniqueIndex.AccountId.Find((uint?)55);
}
}
""";

var parseOptions = new CSharpParseOptions(fixture.SampleCompilation.LanguageVersion);
var tree = CSharpSyntaxTree.ParseText(source, parseOptions, path: "NullableBTreeIndex.cs");
var compilation = fixture.SampleCompilation.AddSyntaxTrees(tree);

var driver = CSharpGeneratorDriver.Create(
[
new SpacetimeDB.Codegen.Type().AsSourceGenerator(),
new SpacetimeDB.Codegen.Module().AsSourceGenerator(),
],
driverOptions: new(
disabledOutputs: IncrementalGeneratorOutputKind.None,
trackIncrementalGeneratorSteps: true
),
parseOptions: parseOptions
);

var runResult = driver.RunGenerators(compilation).GetRunResult();
var compilationAfterGen = compilation.AddSyntaxTrees(runResult.GeneratedTrees);

Assert.Empty(GetCompilationErrors(compilationAfterGen));
}

[Fact]
public static async Task TestDiagnostics()
{
Expand Down

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
Expand Up @@ -35,23 +35,6 @@
}
},
{/*
[Unique]
public int? UniqueField;
^^^^^^^^^^^

*/
Message: Field UniqueField is marked as Unique but it has a type int? which is not an equatable primitive.,
Severity: Error,
Descriptor: {
Id: STDB0003,
Title: Unique fields must be equatable,
MessageFormat: Field {0} is marked as Unique but it has a type {1} which is not an equatable primitive.,
Category: SpacetimeDB,
DefaultSeverity: Error,
IsEnabledByDefault: true
}
},
{/*
[SpacetimeDB.Table]
public partial record TestTableTaggedEnum : SpacetimeDB.TaggedEnum<(int X, int Y)> { }
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
Expand All @@ -69,23 +52,6 @@ public partial record TestTableTaggedEnum : SpacetimeDB.TaggedEnum<(int X, int Y
}
},
{/*
[Unique]
public int? UniqueField;
^^^^^^^^^^^

*/
Message: Field UniqueField is marked as Unique but it has a type int? which is not an equatable primitive.,
Severity: Error,
Descriptor: {
Id: STDB0003,
Title: Unique fields must be equatable,
MessageFormat: Field {0} is marked as Unique but it has a type {1} which is not an equatable primitive.,
Category: SpacetimeDB,
DefaultSeverity: Error,
IsEnabledByDefault: true
}
},
{/*
{
[SpacetimeDB.Index.BTree(Accessor = "TestUnexpectedColumns", Columns = ["UnexpectedColumn"])]
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
Expand Down Expand Up @@ -626,4 +592,4 @@ public partial struct TestScheduleIssues
}
}
]
}
}
24 changes: 18 additions & 6 deletions crates/bindings-csharp/Codegen/Module.cs
Original file line number Diff line number Diff line change
Expand Up @@ -160,9 +160,22 @@ public static bool IsNoPayloadEnum(ITypeSymbol type)
&& variants.All(field => field.Type.ToString() == "SpacetimeDB.Unit");
}

public static bool IsEquatable(ITypeSymbol type) =>
(
IsInteger(type)
private static ITypeSymbol UnwrapNullable(ITypeSymbol type) =>
type switch
{
INamedTypeSymbol
{
OriginalDefinition.SpecialType: SpecialType.System_Nullable_T
} nullable => nullable.TypeArguments[0],
_ when type.NullableAnnotation == NullableAnnotation.Annotated =>
type.WithNullableAnnotation(NullableAnnotation.None),
_ => type,
};

public static bool IsEquatable(ITypeSymbol type)
{
type = UnwrapNullable(type);
return IsInteger(type)
|| IsNoPayloadEnum(type)
|| type.SpecialType switch
{
Expand All @@ -173,9 +186,8 @@ public static bool IsEquatable(ITypeSymbol type) =>
or "SpacetimeDB.Timestamp"
or "SpacetimeDB.Uuid",
_ => false,
}
)
&& type.NullableAnnotation != NullableAnnotation.Annotated;
};
}
}

/// <summary>
Expand Down
21 changes: 21 additions & 0 deletions crates/bindings-typescript/src/lib/type_builders.test-d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,27 @@ const _rowOptionOptionalSome: RowOptionOptional = {
foo: 'hello',
};

// Optional columns whose inner type is filterable may be indexed and unique.
// eslint-disable-next-line @typescript-eslint/no-unused-vars
const rowOptionIndex = {
id: t.u64(),
optionalId: t.option(t.u64()).index('btree').unique(),
};
type RowOptionIndex = InferTypeOfRow<typeof rowOptionIndex>;
// eslint-disable-next-line @typescript-eslint/no-unused-vars
const _rowOptionIndexNone: RowOptionIndex = {
id: 1n,
optionalId: undefined,
};
// eslint-disable-next-line @typescript-eslint/no-unused-vars
const _rowOptionIndexSome: RowOptionIndex = {
id: 2n,
optionalId: 1n,
};

// @ts-expect-error optional arrays are not filterable and cannot be indexed.
t.option(t.array(t.u64())).index('btree');

// Test that a row must not allow non-TypeBuilder or ColumnBuilder values
// eslint-disable-next-line @typescript-eslint/no-unused-vars
const row2 = {
Expand Down
Loading
Loading