Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 13 additions & 8 deletions CardinalityEstimation/CardinalityEstimator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ public class CardinalityEstimator : ICardinalityEstimator<string>, ICardinalityE
/// <summary>
/// Lookup dictionary for the sparse representation of HLL buckets
/// </summary>
private IDictionary<ushort, byte> lookupSparse;
private Dictionary<ushort, byte> lookupSparse;

/// <summary>
/// Max number of elements to hold in the sparse representation before switching to dense
Expand Down Expand Up @@ -787,19 +787,24 @@ private bool AddElementHash(ulong hashCode)
if (isSparse)
{
lookupSparse.TryGetValue(substream, out byte prevRank);
lookupSparse[substream] = Math.Max(prevRank, sigma);
changed = changed || (prevRank != sigma && lookupSparse[substream] == sigma);
if (lookupSparse.Count > sparseMaxElements)
if (sigma > prevRank)
{
SwitchToDenseRepresentation();
lookupSparse[substream] = sigma;
if (lookupSparse.Count > sparseMaxElements)
{
SwitchToDenseRepresentation();
}
changed = true;
}
}
else
{
var prevMax = lookupDense[substream];
lookupDense[substream] = Math.Max(prevMax, sigma);
changed = changed || (prevMax != sigma && lookupDense[substream] == sigma);
ref var value = ref lookupDense[substream];
if (sigma > value)
{
value = sigma;
changed = true;
}
}
return changed;
}
Expand Down