test: Add reservoir sample e2e tests#27858
Conversation
Reviewer's GuideAdds end-to-end tests for the reservoir_sample aggregation to validate basic deterministic properties of its output over the orders table, including processed_count behavior, sample size limits, value domain, handling of constant and null inputs, and group-by semantics. File-Level Changes
Tips and commandsInteracting with Sourcery
Customizing Your ExperienceAccess your dashboard to:
Getting Help
|
There was a problem hiding this comment.
Hey - I've found 2 issues, and left some high level feedback:
- The tests hardcode assumptions about the scale and row count of the
orderstable (e.g.,SELECT 15000), which could make them brittle if the test dataset changes; consider expressing these expectations in terms ofcount(*)queries instead of fixed constants. - Several assertions scan the full
orderstable independently, which may slow down the e2e suite; consider combining related checks into fewer queries or usingWITHsubqueries / CTEs to reuse a single aggregation where possible.
Prompt for AI Agents
Please address the comments from this code review:
## Overall Comments
- The tests hardcode assumptions about the scale and row count of the `orders` table (e.g., `SELECT 15000`), which could make them brittle if the test dataset changes; consider expressing these expectations in terms of `count(*)` queries instead of fixed constants.
- Several assertions scan the full `orders` table independently, which may slow down the e2e suite; consider combining related checks into fewer queries or using `WITH` subqueries / CTEs to reuse a single aggregation where possible.
## Individual Comments
### Comment 1
<location path="presto-tests/src/main/java/com/facebook/presto/tests/AbstractTestAggregations.java" line_range="1388" />
<code_context>
+ * specific elements. Comprehensive correctness lives in TestReservoirSampleAggregation.
+ */
+ @Test
+ public void testReservoirSample()
+ {
+ // processed_count equals the number of input rows when no initial sample is supplied.
</code_context>
<issue_to_address>
**suggestion (testing):** Add coverage for non-null initial sample and non-zero initial processed_count arguments
Current tests only call the 4‑arg `reservoir_sample` with a `NULL` initial sample and `0` `processed_count`. Please add at least one end‑to‑end test that seeds the aggregator with a non‑empty initial sample and non‑zero `processed_count`, then verifies that the final `processed_count` and sample size account for both the seed and the new rows (i.e., merge/resume behavior).
Suggested implementation:
```java
// The sample is capped at the desired size when the input is larger.
assertQuery(
"SELECT cardinality(rs.sample) FROM (SELECT reservoir_sample(NULL, 0, orderkey, 10) AS rs FROM orders) t",
"SELECT 10");
// A non-empty initial sample and non-zero initial processed_count are incorporated into the final state.
// Seed the aggregation with an initial sample of two elements and a processed_count of 2, then process three
// more rows. The final processed_count should be 5, and the sample should contain five elements (the seed
// plus the new rows, with no eviction because total < desired_sample_size).
assertQuery(
"SELECT rs.processed_count, cardinality(rs.sample) " +
"FROM (" +
"SELECT reservoir_sample(ARRAY[1, 2], 2, x, 5) AS rs " +
"FROM (VALUES 10, 20, 30) t(x)" +
") u",
"VALUES (5, 5)");
// When the input is smaller than the desired size the entire input is kept.
```
If there are existing style or helper methods in `AbstractTestAggregations` for building VALUES queries or for asserting single-row results, you may want to adapt the new assertion to use those helpers for consistency. Also, if `reservoir_sample` has separate overloads for different types, adjust the `ARRAY[1, 2]` and `VALUES 10, 20, 30` literals to match the preferred test type (e.g., BIGINT versus INTEGER) used elsewhere in this test class.
</issue_to_address>
### Comment 2
<location path="presto-tests/src/main/java/com/facebook/presto/tests/AbstractTestAggregations.java" line_range="1405-1413" />
<code_context>
+ "SELECT cardinality(filter(rs.sample, x -> x > 0)) = cardinality(rs.sample) FROM (SELECT reservoir_sample(NULL, 0, orderkey, 20) AS rs FROM orders) t",
+ "SELECT true");
+
+ // A constant input yields a deterministic sample of that constant.
+ assertQuery(
+ "SELECT rs.sample = ARRAY[1.0E0, 1.0E0, 1.0E0] FROM (SELECT reservoir_sample(NULL, 0, 1.0E0, 3) AS rs FROM orders) t",
+ "SELECT true");
+
</code_context>
<issue_to_address>
**suggestion (testing):** Clarify the determinism assumption and/or relax the assertion on the exact array shape
This assertion hard-codes the exact sampled array `[1.0E0, 1.0E0, 1.0E0]` for a constant input, which makes the test fragile if the implementation later changes (e.g., returns fewer elements under an optimization or encodes the constant differently) while still respecting the intended contract. Consider instead asserting properties like `cardinality(rs.sample) = 3` and that all elements equal `1.0E0`. If you truly require full-array determinism for constant inputs, please add a short comment stating that this is an explicit, stable guarantee of the function.
```suggestion
// Sampled elements are drawn from the input: every sampled orderkey is positive.
assertQuery(
"SELECT cardinality(filter(rs.sample, x -> x > 0)) = cardinality(rs.sample) FROM (SELECT reservoir_sample(NULL, 0, orderkey, 20) AS rs FROM orders) t",
"SELECT true");
// A constant input yields a sample of the constant value; verify size and contents, not exact array shape.
assertQuery(
"SELECT cardinality(rs.sample) = 3 AND cardinality(filter(rs.sample, x -> x = 1.0E0)) = 3 FROM (SELECT reservoir_sample(NULL, 0, 1.0E0, 3) AS rs FROM orders) t",
"SELECT true");
```
</issue_to_address>Help me be more useful! Please click 👍 or 👎 on each comment and I'll use the feedback to improve your reviews.
3481b37 to
da40401
Compare
|
@allenshen13 Looks like sourcery had some good suggestion on expanding the testing? Can we take a look at that? This link does not necessarily exist all the time. |
da40401 to
4c60ed0
Compare
Add testReservoirSample to the shared AbstractTestAggregations so both Presto Java and the native/Velox engine exercise the reservoir_sample aggregate. Sampled elements are chosen at random, so the test asserts deterministic properties rather than specific elements: processed_count, sample cardinality (both capped and smaller-than-desired), value domain, a constant-input sample, null counting, and per-group processed_count. Comprehensive correctness lives in TestReservoirSampleAggregation and the Velox unit tests.
4c60ed0 to
eb3d29c
Compare
Description
Add e2e aggregation tests verifying reservoir_sample output size, processed_count, null handling, grouping behavior, and deterministic behavior on constant inputs.
Motivation and Context
Impact
Test Plan
Contributor checklist
Release Notes
Please follow release notes guidelines and fill in the release notes below.
Summary by Sourcery
Tests: