Skip to content

Commit

Permalink
Fix for yet another dictionary LINQ thing, this time for nested prope…
Browse files Browse the repository at this point in the history
…rties. Closes GH-3067
  • Loading branch information
jeremydmiller committed Mar 20, 2024
1 parent aa9c951 commit 727d1ac
Show file tree
Hide file tree
Showing 5 changed files with 153 additions and 8 deletions.
67 changes: 67 additions & 0 deletions src/LinqTests/Bugs/Bug_3067_nested_array_in_dictionary.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Marten;
using Marten.Exceptions;
using Marten.Testing.Harness;
using Shouldly;
using Xunit.Abstractions;

namespace LinqTests.Bugs;

public class Bug_3067_nested_array_in_dictionary : BugIntegrationContext
{
private readonly ITestOutputHelper _output;

public record RootRecord(Guid Id, Dictionary<Guid, NestedRecord> Dict);
public record NestedRecord(List<Guid> Entities);

public Bug_3067_nested_array_in_dictionary(ITestOutputHelper output)
{
_output = output;
}

[Fact]
public async Task filter_with_dict_list_nested()
{
var value = Guid.NewGuid();
theSession.Store(new RootRecord(Guid.NewGuid(), new Dictionary<Guid, NestedRecord>() { {Guid.NewGuid(), new NestedRecord([value])}, {Guid.NewGuid(), new NestedRecord([Guid.NewGuid()])}, {Guid.NewGuid(), new NestedRecord([Guid.NewGuid()])}}));
theSession.Store(new RootRecord(Guid.NewGuid(), new Dictionary<Guid, NestedRecord>() { {Guid.NewGuid(), new NestedRecord([Guid.NewGuid()])}, {Guid.NewGuid(), new NestedRecord([Guid.NewGuid()])}}));
theSession.Store(new RootRecord(Guid.NewGuid(), new Dictionary<Guid, NestedRecord>() { {Guid.NewGuid(), new NestedRecord([Guid.NewGuid()])}, {Guid.NewGuid(), new NestedRecord([Guid.NewGuid()])}}));
theSession.Store(new RootRecord(Guid.NewGuid(), new Dictionary<Guid, NestedRecord>() { {Guid.NewGuid(), new NestedRecord([Guid.NewGuid()])}, {Guid.NewGuid(), new NestedRecord([Guid.NewGuid()])}}));
theSession.Store(new RootRecord(Guid.NewGuid(), new Dictionary<Guid, NestedRecord>() { {Guid.NewGuid(), new NestedRecord([Guid.NewGuid()])}, {Guid.NewGuid(), new NestedRecord([Guid.NewGuid()])}}));
await theSession.SaveChangesAsync();

theSession.Logger = new TestOutputMartenLogger(_output);

var ex = await Should.ThrowAsync<BadLinqExpressionException>(async () =>
{
var results = await theSession.Query<RootRecord>().
Where(x => x.Dict.Values.Any(r => r.Entities.Contains(value)))
.ToListAsync();
});

ex.Message.ShouldContain("#>", StringComparisonOption.Default);
}

[Fact]
public async Task selectmany_with_dict_list()
{
var value = Guid.Parse("725177c5-f453-46a6-98ae-6b9c6f041b34");
theSession.Store(new RootRecord(Guid.NewGuid(), new Dictionary<Guid, NestedRecord>() { {Guid.NewGuid(), new NestedRecord([value])}, {Guid.NewGuid(), new NestedRecord([Guid.Empty, ])}, {Guid.NewGuid(), new NestedRecord([Guid.Empty, ])}, {Guid.NewGuid(), new NestedRecord([Guid.Empty, ])} }));
theSession.Store(new RootRecord(Guid.NewGuid(), new Dictionary<Guid, NestedRecord>() { {Guid.NewGuid(), new NestedRecord([Guid.Empty, ])}}));
theSession.Store(new RootRecord(Guid.NewGuid(), new Dictionary<Guid, NestedRecord>() { {Guid.NewGuid(), new NestedRecord([Guid.Empty, ])}}));
theSession.Store(new RootRecord(Guid.NewGuid(), new Dictionary<Guid, NestedRecord>() { {Guid.NewGuid(), new NestedRecord([Guid.Empty, ])}}));
theSession.Store(new RootRecord(Guid.NewGuid(), new Dictionary<Guid, NestedRecord>() { {Guid.NewGuid(), new NestedRecord([Guid.Empty, ])}}));
theSession.Store(new RootRecord(Guid.NewGuid(), new Dictionary<Guid, NestedRecord>() { {Guid.NewGuid(), new NestedRecord([Guid.Empty, ])}}));

await theSession.SaveChangesAsync();

var results = await theSession.Query<RootRecord>()
.SelectMany(x => x.Dict.Values).Where(x => x.Entities.Contains(value))
.ToListAsync();

Assert.Single(results);
}
}
6 changes: 6 additions & 0 deletions src/Marten/Linq/Members/Dictionaries/DictionaryMember.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Linq.Expressions;
using System.Reflection;
Expand Down Expand Up @@ -81,6 +82,11 @@ public override void PlaceValueInDictionaryForContainment(Dictionary<string, obj

dict[MemberName] = childDict;
}
else if (constant.Value is Dictionary<string, object> raw)
{
throw new BadLinqExpressionException("Marten can not (yet) support value " + constant.Value +
" in a search involving a nested dictionary member. You may need to resort to MatchesSql(), and using the PostgreSQL '#>' JSONPath operator. See https://www.postgresql.org/docs/12/functions-json.html");
}
else
{
throw new BadLinqExpressionException("Marten can not (yet) support value " + constant.Value +
Expand Down
57 changes: 50 additions & 7 deletions src/Marten/Linq/Members/Dictionaries/DictionaryValuesMember.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
using System.Linq;
using System.Linq.Expressions;
using System.Reflection;
using JasperFx.Core;
using JasperFx.Core.Reflection;
using Marten.Exceptions;
using Marten.Internal;
Expand All @@ -20,11 +21,18 @@ namespace Marten.Linq.Members.Dictionaries;
internal class DictionaryValuesMember : QueryableMember, ICollectionMember, IValueCollectionMember
{
private readonly IDictionaryMember _parent;
private readonly StoreOptions _options;
private ImHashMap<string, IQueryableMember> _members = ImHashMap<string, IQueryableMember>.Empty;
private readonly RootMember _root;


public DictionaryValuesMember(IDictionaryMember parent, StoreOptions options) : base(parent, "Values", typeof(ICollection<>).MakeGenericType(parent.ValueType))
{
_parent = parent;
_options = options;

ElementType = parent.ValueType;
_root = new RootMember(ElementType) { Ancestors = Array.Empty<IQueryableMember>() };

var rawLocator = RawLocator;

Expand All @@ -34,12 +42,24 @@ public DictionaryValuesMember(IDictionaryMember parent, StoreOptions options) :
var pgType = PostgresqlProvider.Instance.HasTypeMapping(ElementType) ? innerPgType + "[]" : "jsonb";
Element = new SimpleElementMember(ElementType, pgType);

ExplodeLocator = $"jsonb_path_query({_parent.TypedLocator}, '$.*') ->> 0";
if (ElementType.IsSimple() || (ElementType.IsNullable() && ElementType.GenericTypeArguments[0].IsSimple()))
{
ExplodeLocator = $"jsonb_path_query({_parent.TypedLocator}, '$.*') ->> 0";
}
else
{
ExplodeLocator = $"jsonb_path_query({_parent.TypedLocator}, '$.*')";
}

ArrayLocator = $"CAST(ARRAY(SELECT jsonb_array_elements_text(CAST({rawLocator} as jsonb))) as {innerPgType}[])";

}

public override void PlaceValueInDictionaryForContainment(Dictionary<string, object> dict, ConstantExpression constant)
{
base.PlaceValueInDictionaryForContainment(dict, constant);
}

public Type ElementType { get; }

public SelectManyValueCollection SelectManyUsage { get;}
Expand All @@ -53,16 +73,38 @@ public override IQueryableMember FindMember(MemberInfo member)
return _parent.Count;
}

return Element;
if (member is ElementMember) return Element;

if (_members.TryFind(member.Name, out var m))
{
return m;
}

m = _options.CreateQueryableMember(member, _root);
_members = _members.AddOrUpdate(member.Name, m);

return m;
}

private SelectorStatement createSelectManySelectorStatement(IMartenSession session,
SelectorStatement parentStatement, CollectionUsage collectionUsage, QueryStatistics statistics)
{
if (ElementType == typeof(string)) return new ScalarSelectManyStringStatement(parentStatement);
if (ElementType.IsPrimitive() || (ElementType.IsNullable() && ElementType.GenericTypeArguments[0].IsPrimitive))
{
return typeof(ScalarSelectManyStatement<>).CloseAndBuildAs<SelectorStatement>(parentStatement,
session.Serializer, ElementType);
}

var selectClause =
typeof(DataSelectClause<>).CloseAndBuildAs<ISelectClause>(parentStatement.ExportName, ElementType);
return (SelectorStatement)collectionUsage.BuildSelectManyStatement(session, this, selectClause, statistics, parentStatement);
}

public Statement AttachSelectManyStatement(CollectionUsage collectionUsage, IMartenSession session,
SelectorStatement parentStatement, QueryStatistics statistics)
{
var statement = ElementType == typeof(string)
? new ScalarSelectManyStringStatement(parentStatement)
: typeof(ScalarSelectManyStatement<>).CloseAndBuildAs<SelectorStatement>(parentStatement,
session.Serializer, ElementType);
var statement = createSelectManySelectorStatement(session, parentStatement, collectionUsage, statistics);

// If the collection has any Where() or OrderBy() usages, you'll need an extra statement
if (collectionUsage.OrderingExpressions.Any() || collectionUsage.WhereExpressions.Any())
Expand Down Expand Up @@ -99,7 +141,8 @@ public ISqlFragment ParseWhereForAny(Expression body, IReadOnlyStoreOptions opti

public IComparableMember ParseComparableForCount(Expression body)
{
throw new NotImplementedException();
throw new BadLinqExpressionException(
"Marten does not (yet) support Comparing the Count against Dictionary pair collections");
}

public ISqlFragment ParseWhereForAll(MethodCallExpression body, IReadOnlyStoreOptions options)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,19 @@ namespace Marten.Linq.Members.ValueCollections;
/// </summary>
internal class SelectManyValueCollection: IValueCollectionMember
{
private readonly StoreOptions _options;
private readonly IQueryableMember _parentMember;
private readonly RootMember _root;
public Type ElementType { get; }

public SelectManyValueCollection(ValueCollectionMember valueCollectionMember, MemberInfo parentMember,
Type elementType, StoreOptions options)
{
_options = options;

ElementType = elementType;
_root = new RootMember(ElementType) { Ancestors = Array.Empty<IQueryableMember>() };

var elementMember = new ElementMember(parentMember, elementType);
var element = (QueryableMember)options.CreateQueryableMember(elementMember, valueCollectionMember, elementType);
element.RawLocator = element.TypedLocator = "data";
Expand All @@ -27,18 +34,28 @@ public SelectManyValueCollection(ValueCollectionMember valueCollectionMember, Me
public SelectManyValueCollection(Type elementType, IQueryableMember parentMember, StoreOptions options)
{
ElementType = elementType;
_options = options;
_root = new RootMember(ElementType) { Ancestors = Array.Empty<IQueryableMember>() };

var elementMember = new ElementMember(typeof(ICollection<>).MakeGenericType(elementType), elementType);
var element = (QueryableMember)options.CreateQueryableMember(elementMember, parentMember, elementType);
element.RawLocator = element.TypedLocator = "data";

_parentMember = parentMember;

Element = element;
}

public IQueryableMember Element { get; }

public IQueryableMember FindMember(MemberInfo member)
{
return Element;
if (member is ElementMember)
{
return Element;
}

return _options.CreateQueryableMember(member, _root);
}

public void ReplaceMember(MemberInfo member, IQueryableMember queryableMember)
Expand Down
12 changes: 12 additions & 0 deletions src/Marten/Linq/SqlGeneration/Statement.cs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
using System;
using JasperFx.Core;
using JasperFx.Core.Reflection;
using Marten.Internal;
Expand Down Expand Up @@ -46,6 +47,12 @@ public void InsertAfter(Statement descendent)
descendent.Next = Next;
}

if (object.ReferenceEquals(this, descendent))
{
throw new InvalidOperationException(
"Whoa pardner, you cannot set Next to yourself, that's a stack overflow!");
}

Next = descendent;
descendent.Previous = this;
}
Expand All @@ -62,6 +69,11 @@ public void AddToEnd(Statement descendent)
}
else
{
if (object.ReferenceEquals(this, descendent))
{
return;
}

Next = descendent;
descendent.Previous = this;
}
Expand Down

0 comments on commit 727d1ac

Please sign in to comment.