Skip to content

Commit 9f6ed0e

Browse files
committed
Fix document partition query bug with entity properties
We were basically ignoring the predicate expression and replacing it with the partition key filter expression only, meaning we would never apply the actual filter the user provided. This uses expression combining now like the memory implementation does.
1 parent 7a1b3df commit 9f6ed0e

File tree

5 files changed

+31
-2
lines changed

5 files changed

+31
-2
lines changed

src/TableStorage.Source/TableStorage.Source.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
<PackageReference Include="Microsoft.OData.Client" Version="7.17.0" />
1717
<PackageReference Include="PolySharp" Version="1.15.0" PrivateAssets="all" />
1818
<PackageReference Include="System.Text.Json" Version="6.0.10" />
19+
<PackageReference Include="Mono.Linq.Expressions" Version="2.0.0" />
1920
</ItemGroup>
2021

2122
<ItemGroup>

src/TableStorage/DocumentPartition`1.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
using System.Linq.Expressions;
66
using System.Threading;
77
using System.Threading.Tasks;
8+
using Mono.Linq.Expressions;
89

910
namespace Devlooped
1011
{
@@ -66,7 +67,7 @@ public IAsyncEnumerable<T> EnumerateAsync(CancellationToken cancellation = defau
6667

6768
/// <inheritdoc />
6869
public IAsyncEnumerable<T> EnumerateAsync(Expression<Func<IDocumentEntity, bool>> predicate, CancellationToken cancellation = default)
69-
=> repository.EnumerateAsync(e => e.PartitionKey == PartitionKey, cancellation);
70+
=> repository.EnumerateAsync(predicate.AndAlso(x => x.PartitionKey == PartitionKey), cancellation);
7071

7172
/// <inheritdoc />
7273
public Task<T?> GetAsync(string rowKey, CancellationToken cancellation = default)

src/TableStorage/DocumentRepository`1.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ namespace Devlooped
1515
/// <inheritdoc />
1616
partial class DocumentRepository<T> : IDocumentRepository<T> where T : class
1717
{
18+
static readonly string documentType = typeof(T).FullName?.Replace('+', '.') ?? typeof(T).Name;
1819
static readonly string documentVersion;
1920
static readonly int documentMajorVersion;
2021
static readonly int documentMinorVersion;
@@ -325,7 +326,7 @@ TableEntity ToTable(T entity)
325326
{
326327
var te = new TableEntity(this.partitionKey.Invoke(entity), this.rowKey.Invoke(entity))
327328
{
328-
{ "Type", typeof(T).FullName?.Replace('+', '.') },
329+
{ "Type", documentType },
329330
{ "Version", documentVersion },
330331
{ "MajorVersion", documentMajorVersion },
331332
{ "MinorVersion", documentMinorVersion },

src/TableStorage/TableStorage.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
<PackageReference Include="Microsoft.OData.Client" Version="7.21.3" />
1515
<PackageReference Include="PolySharp" Version="1.15.0" PrivateAssets="all" />
1616
<PackageReference Include="System.Text.Json" Version="6.0.10" />
17+
<PackageReference Include="Mono.Linq.Expressions" Version="2.0.0" />
1718
</ItemGroup>
1819

1920
<ItemGroup Condition="'$(TargetFramework)' == 'net8.0'">

src/Tests/DocumentRepositoryTests.cs

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -277,6 +277,31 @@ public async Task CanDeleteNonExistentEntity()
277277
.DeleteAsync("foo"));
278278
}
279279

280+
[Fact]
281+
public async Task CanQueryPartitionByExpression()
282+
{
283+
var partition = CreatePartition(DocumentSerializer.Default);
284+
var partitionKey = TablePartition.GetDefaultPartitionKey<DocumentEntity>();
285+
286+
await partition.PutAsync(new DocumentEntity
287+
{
288+
PartitionKey = partitionKey,
289+
RowKey = "Foo",
290+
Title = "Foo",
291+
});
292+
293+
await partition.PutAsync(new DocumentEntity
294+
{
295+
PartitionKey = partitionKey,
296+
RowKey = "Bar",
297+
Title = "Bar",
298+
});
299+
300+
var entity = await partition.EnumerateAsync(x => x.RowKey == "Bar").ToListAsync();
301+
302+
Assert.Single(entity);
303+
}
304+
280305
[ProtoContract]
281306
[MessagePackObject]
282307
public class DocumentEntity : IDocumentTimestamp

0 commit comments

Comments
 (0)