RVN015 - Correlated conditions over a nested collection in a dynamic query
-
RVN015 reports dynamic queries that use
Anyto check whether one item in a nested collection satisfies two or more conditions joined with&&. The query asks for the same item to satisfy all the conditions, but an auto-index can match each condition against a different item.RVN015 also reports
Allover a nested collection, because the client does not supportAllinside a query predicate. -
Default severity: Info
Automated code fix: No -
In this article:
Description
A query that does not specify an index, such as session.Query<Order>(), is a dynamic query.
Filtering with Where on fields other than the document ID, or sorting with OrderBy, uses an auto-index.
RavenDB reuses a suitable existing auto-index or creates one if needed.
Indexing nested collections:
For the nested-collection queries discussed here, the auto-index produces one index-entry per document.
The auto-index indexes a nested collection as one multi-valued index-field per property of the collection element.
For the Lines collection of an order, Lines[].Product holds the products of all order lines, and Lines[].Quantity holds their quantities.
The index-entry does not keep track of which values belong to the same order line.
Matching different elements:
As a result, the auto-index can match the conditions in an Any predicate independently.
For x.Lines.Any(line => line.Product == "products/1-A" && line.Quantity > 10),
the index would match an order when some line has the product and some line, possibly a different one, has more than 10 units.
To match all conditions on the same element, query a static fanout index, which produces an index-entry per element.
The server does not create fanout auto-indexes.
Runtime behavior:
The client handles Any and All differently:
-
Any: conditions on different properties of the element:
for the example above, the client throwsInvalidOperationException:
"Using multiple fields inside method 'Any' can lead to unexpected query results for auto index.
A static fanout index should be used instead."RVN015 reports the query at compile time, before it runs.
The client does not reject every predicate that combines conditions on different properties.
For example, it accepts(line.Product == "products/1-A" || line.Product == "products/2-A") && line.Quantity > 10insideAny.
Such a query runs, and different lines can satisfy the product and quantity conditions.The client also does not throw for conditions on the
KeyandValueof a dictionary entry.
For example,x.Counters.Any(kv => kv.Key == "a" && kv.Value == 1)
can match a dictionary containing{ "a": 2, "b": 1 }:
the key matches one entry and the value matches another. RVN015 warns about this at compile time. -
Any: non-range conditions on the same property,
such asline.ProductName.StartsWith("Ch") && line.ProductName.EndsWith("ai"):
the client does not throw, and the query can return orders in which different lines satisfy the two conditions. -
Any: ranges on one property:
an inclusive range such asline.Quantity >= 5 && line.Quantity <= 10is sent as onebetweencondition.
A single indexed value must satisfy both bounds. RVN015 counts this range as one condition, so the range alone is not reported.Ranges with an exclusive bound, such as
line.Quantity > 5 && line.Quantity < 10orline.Quantity >= 5 && line.Quantity < 10, are still reported.
Depending on the generated RQL grouping, the server may combine these bounds into one range,
making the diagnostic a false positive (see Limitations). -
Allover a nested collection:
the client throwsNotSupportedException(Method not supported: All) when the query is executed,
regardless of the predicate.
This also applies to queries over a static index. RVN015 reports this unsupported operation in dynamic queries.
Trigger condition
When is RVN015 reported?
RVN015 checks dynamic queries that start from session.Query<T>() without specifying an index, in sync and async sessions,
including after standard LINQ operators such as OfType<T>(), and through a local variable that holds the query.
Passing only a collection name, such as session.Query<Order>(collectionName: "Orders"),
still creates a dynamic query and is checked.
RVN015 checks predicates passed to:
Where.- Predicate overloads of
First,FirstOrDefault,Single,SingleOrDefault,Any,Count, andLongCount. - Predicate overloads of
FirstAsync,FirstOrDefaultAsync,SingleAsync,SingleOrDefaultAsync,AnyAsync, andCountAsync.
It also checks where clauses in query syntax, including clauses after an into continuation.
In these predicates, RVN015 reports an Any call when:
-
Anyis called on a collection reached through the properties of the queried document, such asx.Lines,
or through the properties of an element of another collection in the same predicate. -
The
Anypredicate is a lambda that joins two or more conditions with&&,
and at least two of these conditions use the lambda parameter (the collection element).
Conditions that do not use the element, such as a captured variable, are not counted.
An inclusive range on one property, such asline.Quantity >= 5 && line.Quantity <= 10,
counts as one condition.
The range alone is not reported, but combining it with another element-dependent condition can trigger RVN015.
Each Any call is checked separately, including an Any call nested in the predicate of another Any call,
such as m.Permissions.Any(…) inside x.Members.Any(m => …).
RVN015 also reports All on a collection reached in the same way, regardless of the predicate or the number of conditions.
It has a separate diagnostic message explaining that All is unsupported inside a query predicate.
Reported on the Any or All method name.
When is RVN015 not reported?
- An
Anypredicate with a single condition on the element, such asx.Lines.Any(line => line.Product == "products/1-A"). - An
Anypredicate consisting only of an inclusive range on one property, such asx.Lines.Any(line => line.Quantity >= 5 && line.Quantity <= 10). - An
Anypredicate such asAny(line => A || B)whose conditions are joined only by||.
This matches the same documents asAny(line => A) || Any(line => B),
so matching the conditions independently returns the correct results. - Separate
Anycalls, which state that each condition can be matched by a different element, such asx.Lines.Any(line => line.Product == "products/1-A") && x.Lines.Any(line => line.Quantity > 10). - An
Anypredicate with only one element-dependent condition and additional conditions on captured values,
such asx.Lines.Any(line => line.Product == productId && includeAll). AnyorAllover a value that is not a collection of the queried document, such as a string, or a local list (productIds.Any(…)).- Queries over a static index:
session.Query<T, TIndex>()orsession.Query<T>(indexName).
For anAnypredicate, whether the conditions are matched on the same element depends on the index map:
a fanout map matches them on the same element, while a map that indexes the collection values in the index-fields of a single index-entry matches them independently (see Indexing nested data). - Document queries (
session.Advanced.DocumentQuery<T>()) and raw queries (session.Advanced.RawQuery<T>()).
The client’s check for multiple fields inside a LINQAnypredicate does not apply to these APIs. When these queries filter nested collection fields through an auto-index, different elements can satisfy different conditions.
How to resolve
For an Any diagnostic, decide whether the conditions must be matched on the same element.
For an All diagnostic, see When every element must match.
-
To match all conditions on the same element, query a static fanout index, whose map produces an index-entry per element of the collection.
Filter by the index-fields of the element, and use
OfType<T>()to return the documents.
The fanout map triggers RVN014.When the fanout is intentional, suppress RVN014 for that index with
#pragma warning disable RVN014.
See Suppress diagnostics for how to disable and restore a diagnostic around a specific code block. -
If each condition can be matched by a different element, use a separate
Anycall for each condition.
The query then states the intended matching explicitly, and the client does not throw.
Before (triggers RVN015)
// Orders that have a line with more than 10 units of products/1-A
var orders = session.Query<Order>()
// Triggers RVN015: Any joins two conditions on the collection element with &&.
// When the query is executed, the client throws InvalidOperationException.
.Where(x => x.Lines.Any(line => line.Product == "products/1-A" && line.Quantity > 10))
.Take(50)
.ToList();
// Query syntax is reported the same way
var orders = (from x in session.Query<Order>()
// Triggers RVN015
where x.Lines.Any(line => line.Product == "products/1-A" && line.Quantity > 10)
select x)
.Take(50)
.ToList();
After (correct)
public class Orders_ByProductAndQuantity : AbstractIndexCreationTask<Order>
{
public class IndexEntry
{
// The index-fields
public string Product { get; set; }
public int Quantity { get; set; }
}
public Orders_ByProductAndQuantity()
{
// The fanout is intentional: each order line is a separate index-entry,
// so a query can match the product and the quantity on the same line.
// Suppress RVN014 for this intentional fanout map.
#pragma warning disable RVN014
Map = orders => from order in orders
from line in order.Lines
select new IndexEntry
{
Product = line.Product,
Quantity = line.Quantity
};
#pragma warning restore RVN014
}
}
// Orders that have a line with more than 10 units of products/1-A
var orders = session
.Query<Orders_ByProductAndQuantity.IndexEntry, Orders_ByProductAndQuantity>()
.Where(x => x.Product == "products/1-A" && x.Quantity > 10)
.OfType<Order>()
.Take(50)
.ToList();
Conditions that can match different elements
If the product and quantity conditions are allowed to match different order lines, use separate Any calls:
// Orders that have a line with products/1-A,
// and a line (the same line or another one) with more than 10 units
var orders = session.Query<Order>()
.Where(x => x.Lines.Any(line => line.Product == "products/1-A") &&
x.Lines.Any(line => line.Quantity > 10))
.Take(50)
.ToList();
When every element must match
All requires every element of the collection to satisfy the predicate.
Filtering the per-element fields of a fanout index finds documents with a matching element; it does not establish that every element matches.
For a fixed requirement, compute a boolean in a static index with one index-entry per document, then query that field.
For example, to find orders where every line has a quantity above 10:
public class Orders_ByAllLineQuantities : AbstractIndexCreationTask<Order>
{
public class IndexEntry
{
// The index-fields
public bool AllLinesAboveTen { get; set; }
}
public Orders_ByAllLineQuantities()
{
Map = orders => from order in orders
select new IndexEntry
{
AllLinesAboveTen = order.Lines.All(line => line.Quantity > 10)
};
}
}
// Orders where every line has more than 10 units
var orders = session
.Query<Orders_ByAllLineQuantities.IndexEntry, Orders_ByAllLineQuantities>()
.Where(x => x.AllLinesAboveTen)
.OfType<Order>()
.Take(50)
.ToList();
All is used in the index map to compute the field; the query predicate tests the boolean.
As with LINQ All, an order with an empty Lines collection also matches.
Limitations
-
The predicate passed to a checked query operator must be a lambda written in the call. A predicate passed as a variable, such as
Where(filter)wherefilteris anExpression<Func<Order, bool>>, is not checked. -
The query must start from
session.Query<T>()in the same method, either in the method chain or in the initializer of a local variable that holds the query. A query received as a parameter, or read from a field or property, is not checked. -
A range on one property with at least one exclusive bound, such as
line.Quantity > 5 && line.Quantity < 10
orline.Quantity >= 5 && line.Quantity < 10, is reported.
The client sends two comparisons, and the server combines them into one range only when the comparisons form their ownandgroup in the generated RQL.
When the bounds are combined, a single indexed value must satisfy both, so RVN015 is a false positive.
This happens, for example, when the range is the only condition inWhere, or is placed in a separate chainedWherecall.Other arrangements can leave the comparisons separate. For example, in
Where(x => x.Company == "companies/1-A" && x.Lines.Any(line => line.Quantity > 5 && line.Quantity < 10)), different lines can satisfy the two bounds.
Related C# analyzers
- RVN014
Fires on index maps that fan out over a collection, including a fanout index created to resolve RVN015.