You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@datasketches.apache.org by "Fengzdadi (via GitHub)" <gi...@apache.org> on 2026/01/04 05:33:52 UTC
[PR] feat: implement generic ReservoirItemsSketch for sampling package [datasketches-go]
Fengzdadi opened a new pull request, #91:
URL: https://github.com/apache/datasketches-go/pull/91
## Summary
Implements `ReservoirItemsSketch[T any]` - a generic reservoir sampling sketch for the Go library. This addresses the ❌ status for sampling sketches in the README.
## Related Issue
#90
## Implementation
### Files Added
| File | Description |
|------|-------------|
| `sampling/reservoir_items_sketch.go` | Generic reservoir sampling implementation |
| `sampling/reservoir_items_union.go` | Union for merging multiple sketches |
| `sampling/reservoir_items_sketch_test.go` | 13 unit tests |
| `examples/reservoir_example_test.go` | Usage examples with int64, string, struct |
### Algorithm
Classic Reservoir Sampling (Vitter's Algorithm R):
- **Initial phase** (n < k): all items are stored
- **Steady state** (n ≥ k): each new item replaces a random item with probability k/n
### API
```go
// Create sketch with capacity k
sketch, _ := sampling.NewReservoirItemsSketch[int64](10)
// Add items
sketch.Update(42)
// Get uniform random sample
samples := sketch.GetSamples()
```
## Notes
- **Generic implementation**: Works with any type (`int64`, `string`, custom structs)
- **Serialization**: Not included in this PR due to complexity of generic type serialization
- **Thread safety**: Not thread-safe; external synchronization required for concurrent use
- **ResizeFactor**: Kept for API compatibility with Java, but Go's slice append handles resizing
## Testing
All tests pass:
```
go test ./sampling/ ./examples/
ok github.com/apache/datasketches-go/sampling
ok github.com/apache/datasketches-go/examples
```
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: dev-unsubscribe@datasketches.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscribe@datasketches.apache.org
For additional commands, e-mail: dev-help@datasketches.apache.org
Re: [PR] feat: implement generic ReservoirItemsSketch for sampling package [datasketches-go]
Posted by "Fengzdadi (via GitHub)" <gi...@apache.org>.
Fengzdadi commented on code in PR #91:
URL: https://github.com/apache/datasketches-go/pull/91#discussion_r2659878811
##########
sampling/reservoir_items_union.go:
##########
@@ -0,0 +1,116 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package sampling
+
+import (
+ "errors"
+ "math/rand"
+)
+
+// ReservoirItemsUnion enables merging of multiple ReservoirItemsSketch instances.
+// This is useful for distributed sampling where each node maintains a local sketch,
+// and the results are merged to get a global sample.
+type ReservoirItemsUnion[T any] struct {
+ maxK int // maximum k for the union
+ gadget *ReservoirItemsSketch[T] // internal sketch
+}
+
+// NewReservoirItemsUnion creates a new union with the specified maximum k.
+func NewReservoirItemsUnion[T any](maxK int) (*ReservoirItemsUnion[T], error) {
+ if maxK < minK {
+ return nil, errors.New("maxK must be at least 1")
+ }
+
+ gadget, err := NewReservoirItemsSketch[T](maxK)
+ if err != nil {
+ return nil, err
+ }
+
+ return &ReservoirItemsUnion[T]{
+ maxK: maxK,
+ gadget: gadget,
+ }, nil
+}
+
+// Update adds a single item to the union.
+func (u *ReservoirItemsUnion[T]) Update(item T) {
+ u.gadget.Update(item)
+}
+
+// UpdateSketch merges another sketch into the union.
+func (u *ReservoirItemsUnion[T]) UpdateSketch(sketch *ReservoirItemsSketch[T]) {
+ if sketch == nil || sketch.IsEmpty() {
+ return
+ }
+
+ samples := sketch.GetSamples()
+ srcN := sketch.GetN()
+
+ if u.gadget.IsEmpty() {
+ // If gadget is empty, copy the source directly
+ for _, v := range samples {
+ u.gadget.data = append(u.gadget.data, v)
+ }
+ u.gadget.n = srcN
+ return
+ }
+
+ // Merge using weighted sampling
+ gadgetN := u.gadget.GetN()
+ totalN := gadgetN + srcN
+ gadgetK := u.gadget.GetNumSamples()
+ targetK := u.maxK
+
+ for _, item := range samples {
+ if u.gadget.GetNumSamples() < targetK {
+ u.gadget.data = append(u.gadget.data, item)
+ } else {
+ j := rand.Int63n(totalN)
+ if j < int64(targetK) {
+ u.gadget.data[j%int64(gadgetK)] = item
+ }
+ }
+ }
+
+ u.gadget.n = totalN
+}
+
+// GetResult returns a copy of the internal sketch.
+func (u *ReservoirItemsUnion[T]) GetResult() (*ReservoirItemsSketch[T], error) {
Review Comment:
Fixed! Removed all "Get" prefixes per Go conventions.
Thanks for the reference!
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: dev-unsubscribe@datasketches.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscribe@datasketches.apache.org
For additional commands, e-mail: dev-help@datasketches.apache.org
Re: [PR] feat: implement generic ReservoirItemsSketch for sampling package [datasketches-go]
Posted by "freakyzoidberg (via GitHub)" <gi...@apache.org>.
freakyzoidberg commented on PR #91:
URL: https://github.com/apache/datasketches-go/pull/91#issuecomment-3710164793
LGTM - Thanks !
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: dev-unsubscribe@datasketches.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscribe@datasketches.apache.org
For additional commands, e-mail: dev-help@datasketches.apache.org
Re: [PR] feat: implement generic ReservoirItemsSketch for sampling package [datasketches-go]
Posted by "Fengzdadi (via GitHub)" <gi...@apache.org>.
Fengzdadi commented on PR #91:
URL: https://github.com/apache/datasketches-go/pull/91#issuecomment-3708383138
> This is review before i look up serialization for ReservoirItemsSketch.
>
> if "T" is some struct, how to serialization/deserialization? If "T" is int, should we reserve 32bit or 64bit in memory?
Thanks for the question!
I reviewed both Java and C++ implementations. My approach:
| Type | Handling |
|------|----------|
| `int32` | 4 bytes (native size) |
| `int64` | 8 bytes (native size) |
| `string` | length prefix + content |
| `struct` | User-provided SerDe interface |
This follows the Java/C++ pattern. I'll provide built-in serializers for common types (int64, string, etc.), and users can implement the interface for custom structs.
Do you think this proposal is feasible?
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: dev-unsubscribe@datasketches.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscribe@datasketches.apache.org
For additional commands, e-mail: dev-help@datasketches.apache.org
Re: [PR] feat: implement generic ReservoirItemsSketch for sampling package [datasketches-go]
Posted by "proost (via GitHub)" <gi...@apache.org>.
proost commented on code in PR #91:
URL: https://github.com/apache/datasketches-go/pull/91#discussion_r2659451707
##########
sampling/reservoir_items_union.go:
##########
@@ -0,0 +1,116 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package sampling
+
+import (
+ "errors"
+ "math/rand"
+)
+
+// ReservoirItemsUnion enables merging of multiple ReservoirItemsSketch instances.
+// This is useful for distributed sampling where each node maintains a local sketch,
+// and the results are merged to get a global sample.
+type ReservoirItemsUnion[T any] struct {
+ maxK int // maximum k for the union
+ gadget *ReservoirItemsSketch[T] // internal sketch
+}
+
+// NewReservoirItemsUnion creates a new union with the specified maximum k.
+func NewReservoirItemsUnion[T any](maxK int) (*ReservoirItemsUnion[T], error) {
+ if maxK < minK {
+ return nil, errors.New("maxK must be at least 1")
+ }
+
+ gadget, err := NewReservoirItemsSketch[T](maxK)
+ if err != nil {
+ return nil, err
+ }
+
+ return &ReservoirItemsUnion[T]{
+ maxK: maxK,
+ gadget: gadget,
+ }, nil
+}
+
+// Update adds a single item to the union.
+func (u *ReservoirItemsUnion[T]) Update(item T) {
+ u.gadget.Update(item)
+}
+
+// UpdateSketch merges another sketch into the union.
+func (u *ReservoirItemsUnion[T]) UpdateSketch(sketch *ReservoirItemsSketch[T]) {
+ if sketch == nil || sketch.IsEmpty() {
+ return
+ }
+
+ samples := sketch.GetSamples()
+ srcN := sketch.GetN()
+
+ if u.gadget.IsEmpty() {
+ // If gadget is empty, copy the source directly
+ for _, v := range samples {
+ u.gadget.data = append(u.gadget.data, v)
+ }
+ u.gadget.n = srcN
+ return
+ }
+
+ // Merge using weighted sampling
+ gadgetN := u.gadget.GetN()
+ totalN := gadgetN + srcN
+ gadgetK := u.gadget.GetNumSamples()
+ targetK := u.maxK
+
+ for _, item := range samples {
+ if u.gadget.GetNumSamples() < targetK {
+ u.gadget.data = append(u.gadget.data, item)
+ } else {
+ j := rand.Int63n(totalN)
+ if j < int64(targetK) {
+ u.gadget.data[j%int64(gadgetK)] = item
+ }
+ }
+ }
+
+ u.gadget.n = totalN
+}
+
+// GetResult returns a copy of the internal sketch.
+func (u *ReservoirItemsUnion[T]) GetResult() (*ReservoirItemsSketch[T], error) {
Review Comment:
usually "Get" prefix is not recommended in go.
ref: https://go.dev/doc/effective_go#Getters
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: dev-unsubscribe@datasketches.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscribe@datasketches.apache.org
For additional commands, e-mail: dev-help@datasketches.apache.org
Re: [PR] feat: implement generic ReservoirItemsSketch for sampling package [datasketches-go]
Posted by "freakyzoidberg (via GitHub)" <gi...@apache.org>.
freakyzoidberg merged PR #91:
URL: https://github.com/apache/datasketches-go/pull/91
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: dev-unsubscribe@datasketches.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscribe@datasketches.apache.org
For additional commands, e-mail: dev-help@datasketches.apache.org
Re: [PR] feat: implement generic ReservoirItemsSketch for sampling package [datasketches-go]
Posted by "proost (via GitHub)" <gi...@apache.org>.
proost commented on PR #91:
URL: https://github.com/apache/datasketches-go/pull/91#issuecomment-3708835597
@Fengzdadi
Thank you for sharing.
It makes sense to me.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: dev-unsubscribe@datasketches.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscribe@datasketches.apache.org
For additional commands, e-mail: dev-help@datasketches.apache.org