Skip to content

Commit

Permalink
RavenDB-22703 Fixed issue with sharding
Browse files Browse the repository at this point in the history
  • Loading branch information
Lwiel committed Aug 23, 2024
1 parent 2a0cfe8 commit 31f724b
Show file tree
Hide file tree
Showing 7 changed files with 93 additions and 12 deletions.
27 changes: 25 additions & 2 deletions src/Corax/Querying/IndexSearcher.cs
Original file line number Diff line number Diff line change
Expand Up @@ -331,9 +331,32 @@ public long GetDictionaryIdFor(Slice field)

public long GetTermAmountInField(in FieldMetadata field)
{
var terms = _fieldsTree?.CompactTreeFor(field.FieldName);
long termAmount = 0;

var fieldTree = _fieldsTree?.CompactTreeFor(field.FieldName);

termAmount += fieldTree?.NumberOfEntries ?? 0;

if (TryGetPostingListForNull(field, out var nullPostingListId))
{
var nullPostingList = GetPostingList(nullPostingListId);

termAmount += nullPostingList?.State.NumberOfEntries ?? 0;
}

return termAmount;
}

public long GetTermAmountInFieldForNonExisting(in FieldMetadata field)
{
if (TryGetPostingListForNonExisting(field, out var nonExistingPostingListId))
{
var nonExistingPostingList = GetPostingList(nonExistingPostingListId);

return nonExistingPostingList?.State.NumberOfEntries ?? 0;
}

return terms?.NumberOfEntries ?? 0;
return 0;
}

public bool TryGetTermsOfField(in FieldMetadata field, out ExistsTermProvider<Lookup<CompactKeyLookup>.ForwardIterator> existsTermProvider)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -304,7 +304,7 @@ private void HandleRangeFacetsPerDocument(ref EntryTermsReader reader,
reader.Reset();
while (reader.FindNext(fieldRootPage))
{
if (reader.IsNull)
if (reader.IsNull || reader.IsNonExisting)
continue;

isMatching = result.RangeType switch
Expand Down Expand Up @@ -351,6 +351,9 @@ private void HandleFacetsPerDocument(ref EntryTermsReader reader,
reader.Reset();
while (reader.FindNext(fieldRootPage))
{
if (reader.IsNonExisting)
continue;

var key = reader.IsNull
? Constants.ProjectionNullValueSlice
: reader.Current.Decoded();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,13 +31,17 @@ public DynamicJsonValue GetDocument(ref EntryTermsReader entryReader)
entryReader.Reset();
while (entryReader.MoveNext())
{
if(_indexSearcher.FieldCache.TryGetField(entryReader.FieldRootPage, out var fieldName)==false)
if (_indexSearcher.FieldCache.TryGetField(entryReader.FieldRootPage, out var fieldName) == false)
continue;

if (entryReader.IsNonExisting)
continue;

string value = entryReader.IsNull
? null
: entryReader.Current.ToString();
SetValue(fieldName, value switch

SetValue(fieldName, value switch
{
Constants.EmptyString => string.Empty,
_ => value
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1330,7 +1330,8 @@ public static OrderMetadata[] GetSortMetadata(Parameters builderParameters)
var fieldMetadata = QueryBuilderHelper.GetFieldIdForOrderBy(allocator, field.Name, index, builderParameters.HasDynamics,
builderParameters.DynamicFields, indexMapping, queryMapping, false);

if (builderParameters.IndexSearcher.GetTermAmountInField(fieldMetadata) == 0)
if (builderParameters.IndexSearcher.GetTermAmountInField(fieldMetadata) == 0 &&
builderParameters.IndexSearcher.GetTermAmountInFieldForNonExisting(fieldMetadata) == 0)
continue;

if (field.OrderingType == OrderByFieldType.Distance)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -131,12 +131,12 @@ private ShardedQueryResultDocument AddOrderByFields(Document queryResult, IndexQ
string m = null;
while (reader.FindNext(fieldRootPage))
{
if (reader.IsNull)
if (reader.IsNull || reader.IsNonExisting)
{
m = null;
continue;
}
// we allocating managed string to make things easier, if this show up in profiling
// we are allocating managed string to make things easier, if this show up in profiling
// we can do the comparisons using CompactKeys
var stringValue = Encoding.UTF8.GetString(reader.Current.Decoded());
m ??= stringValue;
Expand All @@ -145,7 +145,7 @@ private ShardedQueryResultDocument AddOrderByFields(Document queryResult, IndexQ
}
result.AddStringOrderByField(m switch
{
Constants.EmptyString =>string.Empty,
Constants.EmptyString => string.Empty,
_ => m
});
break;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -204,15 +204,17 @@ protected PriorityQueue<object[]> RetrieveTerms(long documentId)

while (indexEntry.MoveNext())
{
if(fields.Contains(indexEntry.FieldRootPage) == false)
if (fields.Contains(indexEntry.FieldRootPage) == false)
continue;

if (indexEntry.IsNonExisting)
continue;

var key = indexEntry.IsNull
? null
: indexEntry.Current.Decoded();

InsertTerm(key, indexEntry.Frequency);

}

return CreateQueue(termFreqMap);
Expand Down
50 changes: 49 additions & 1 deletion test/SlowTests/Issues/RavenDB-22703.cs
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,55 @@ public void TestQueryWithOrderByClauseAndNoWhereClause(Options options)
}
}
}


[RavenTheory(RavenTestCategory.Corax | RavenTestCategory.Indexes)]
[RavenData(SearchEngineMode = RavenSearchEngineMode.Corax, DatabaseMode = RavenDatabaseMode.Sharded)]
public void TestOrderingOnShardedDatabase(Options options)
{
using (var store = GetDocumentStore(options))
{
using (var session = store.OpenSession())
{
var bar1 = new Bar() { Foo = new Foo() { BarBool = false, BarShort = 9 } };
var bar2 = new Bar() { Foo = new Foo() { BarBool = null } };
var bar3 = new Bar() { Foo = null };
var bar4 = new Bar() { Foo = new Foo() { BarBool = true, BarShort = 21 } };
var bar5 = new Bar() { Foo = null };

session.Store(bar1);
session.Store(bar2);
session.Store(bar3);
session.Store(bar4);
session.Store(bar5);

session.SaveChanges();

var requestExecutor = store.GetRequestExecutor();
using (requestExecutor.ContextPool.AllocateOperationContext(out var context))
{
var reader = context.ReadObject(new DynamicJsonValue
{
["@metadata"] = new DynamicJsonValue{
["@collection"] = "Bars",
["Raven-Clr-Type"] = "SlowTests.Issues.RavenDB_22703+Bar, SlowTests"
}
}, "bars/6");
requestExecutor.Execute(new PutDocumentCommand(store.Conventions, "bars/6", null, reader), context);
}

var res = session.Query<Bar>()
.OrderByDescending(b => b.Foo.BarBool)
.ThenByDescending(b => b.Foo.BarShort)
.ToList();

Assert.Equal(6, res.Count);

Assert.Equal(true, res[0].Foo.BarBool);
Assert.Equal(false, res[1].Foo.BarBool);
}
}
}

private class Bar
{
public Foo Foo { get; set; } = null!;
Expand Down

0 comments on commit 31f724b

Please sign in to comment.