Skip to content

Commit

Permalink
Add capacity() getter to ApproxMostFrequentStreamSummary (facebookinc…
Browse files Browse the repository at this point in the history
…ubator#12212)

Summary:
Pull Request resolved: facebookincubator#12212

# Context

This diff adds a simple getter to retrieve the capacity parameter (after we hit setCapacity()).

We use this internally to perform some sanity checking when we have potentially two different SteamSummaries and we want to ensure capacities are in a sane spot.

This seems reasonably safe to add, the capacity is set by the user and having something to read back out the set value doesn't seem too dangerous. It's also good for sanity checks and unit tests - e.g. as deserializing a stream summary gives back 0 capacity.

Reviewed By: Yuhta

Differential Revision: D68870501

fbshipit-source-id: d7b67eb9f7f375b3a046a94dd3cde00ed82006e8
  • Loading branch information
Yiyang Chen authored and facebook-github-bot committed Feb 1, 2025
1 parent 3e3f90e commit 8a3aa63
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 0 deletions.
8 changes: 8 additions & 0 deletions velox/functions/lib/ApproxMostFrequentStreamSummary.h
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,9 @@ struct ApproxMostFrequentStreamSummary {
/// Return the number of distinct values currently being tracked.
int size() const;

// Return the maximum number of distinct values that can be tracked.
int capacity() const;

/// Return the pointer to values data. The number of values equals to size().
const T* values() const {
return queue_.values();
Expand Down Expand Up @@ -107,6 +110,11 @@ int ApproxMostFrequentStreamSummary<T, A>::size() const {
return queue_.size();
}

template <typename T, typename A>
int ApproxMostFrequentStreamSummary<T, A>::capacity() const {
return capacity_;
}

template <typename T, typename A>
void ApproxMostFrequentStreamSummary<T, A>::insert(T value, int64_t count) {
auto index = queue_.getValueIndex(value);
Expand Down
12 changes: 12 additions & 0 deletions velox/functions/lib/tests/ApproxMostFrequentStreamSummaryTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -184,5 +184,17 @@ TEST(ApproxMostFrequentStreamSummaryTest, mergeSerialized) {
}
}

TEST(ApproxMostFrequentStreamSummaryTest, capacity) {
constexpr int kCapacity = 30;
ApproxMostFrequentStreamSummary<int> summary;
summary.setCapacity(kCapacity);
ASSERT_EQ(summary.capacity(), 30);
}

TEST(ApproxMostFrequentStreamSummaryTest, unsetCapacity) {
ApproxMostFrequentStreamSummary<int> summary;
ASSERT_EQ(summary.capacity(), 0);
}

} // namespace
} // namespace facebook::velox::functions

0 comments on commit 8a3aa63

Please sign in to comment.