|
22 | 22 |
|
23 | 23 | import com.apple.foundationdb.record.EvaluationContext;
|
24 | 24 | import com.apple.foundationdb.record.ExecuteProperties;
|
| 25 | +import com.apple.foundationdb.record.RecordCursor; |
| 26 | +import com.apple.foundationdb.record.RecordCursorResult; |
25 | 27 | import com.apple.foundationdb.record.RecordMetaData;
|
26 | 28 | import com.apple.foundationdb.record.TestRecords1Proto;
|
27 | 29 | import com.apple.foundationdb.record.provider.foundationdb.FDBRecordContext;
|
|
54 | 56 | import java.util.ArrayList;
|
55 | 57 | import java.util.Arrays;
|
56 | 58 | import java.util.Collections;
|
| 59 | +import java.util.LinkedList; |
57 | 60 | import java.util.List;
|
58 | 61 | import java.util.function.BiConsumer;
|
59 | 62 | import java.util.function.Function;
|
@@ -280,6 +283,47 @@ void aggregateNoRecordsNoGroupNoAggregate(final boolean useNestedResult) {
|
280 | 283 | }
|
281 | 284 | }
|
282 | 285 |
|
| 286 | + @ParameterizedTest(name = "[{displayName}-{index}] {0}") |
| 287 | + @BooleanSource |
| 288 | + void test(final boolean useNestedResult) { |
| 289 | + try (final var context = openContext()) { |
| 290 | + openSimpleRecordStore(context, NO_HOOK); |
| 291 | + |
| 292 | + final var plan = |
| 293 | + new AggregationPlanBuilder(recordStore.getRecordMetaData(), "MySimpleRecord") |
| 294 | + .withAggregateValue("num_value_2", value -> new NumericAggregationValue.Sum(NumericAggregationValue.PhysicalOperator.SUM_I, value)) |
| 295 | + .withGroupCriterion("num_value_3_indexed") |
| 296 | + .withGroupCriterion("str_value_indexed") |
| 297 | + .build(useNestedResult); |
| 298 | + |
| 299 | + final var result = executePlanWithRecordScanLimit(plan, 2); |
| 300 | + assertResults(useNestedResult ? this::assertResultNested : this::assertResultFlattened, result, resultOf(0, "0", 1), resultOf(1, "0", 2), resultOf(1, "1", 3), resultOf(2, "1", 9)); |
| 301 | + } |
| 302 | + } |
| 303 | + |
| 304 | + private List<QueryResult> executePlanWithRecordScanLimit(final RecordQueryPlan plan, final int recordScanLimit) { |
| 305 | + byte[] continuation = null; |
| 306 | + List<QueryResult> queryResults = new LinkedList<>(); |
| 307 | + while (true) { |
| 308 | + RecordCursor<QueryResult> currentCursor = executePlan(plan, 0, recordScanLimit, continuation); |
| 309 | + RecordCursorResult<QueryResult> currentCursorResult; |
| 310 | + while (true) { |
| 311 | + currentCursorResult = currentCursor.getNext(); |
| 312 | + continuation = currentCursorResult.getContinuation().toBytes(); |
| 313 | + if (!currentCursorResult.hasNext()) { |
| 314 | + break; |
| 315 | + } |
| 316 | + queryResults.add(currentCursorResult.get()); |
| 317 | + System.out.println("current result:" + currentCursorResult.get().getMessage()); |
| 318 | + } |
| 319 | + System.out.println("getNoNextReson:" + currentCursorResult.getNoNextReason()); |
| 320 | + if (currentCursorResult.getNoNextReason() == RecordCursor.NoNextReason.SOURCE_EXHAUSTED) { |
| 321 | + break; |
| 322 | + } |
| 323 | + } |
| 324 | + return queryResults; |
| 325 | + } |
| 326 | + |
283 | 327 | private void populateDB(final int numRecords) throws Exception {
|
284 | 328 | try (FDBRecordContext context = openContext()) {
|
285 | 329 | openSimpleRecordStore(context);
|
@@ -307,6 +351,17 @@ private List<QueryResult> executePlan(final RecordQueryPlan plan) {
|
307 | 351 | }
|
308 | 352 | }
|
309 | 353 |
|
| 354 | + @Nonnull |
| 355 | + private List<QueryResult> executePlan(final RecordQueryPlan plan) { |
| 356 | + final var types = plan.getDynamicTypes(); |
| 357 | + final var typeRepository = TypeRepository.newBuilder().addAllTypes(types).build(); |
| 358 | + try { |
| 359 | + return plan.executePlan(recordStore, EvaluationContext.forTypeRepository(typeRepository), null, ExecuteProperties.SERIAL_EXECUTE).asList().get(); |
| 360 | + } catch (final Throwable t) { |
| 361 | + throw Assertions.<RuntimeException>fail(t); |
| 362 | + } |
| 363 | + } |
| 364 | + |
310 | 365 | private void assertResults(@Nonnull final BiConsumer<QueryResult, List<?>> checkConsumer, @Nonnull final List<QueryResult> actual, @Nonnull final List<?>... expected) {
|
311 | 366 | Assertions.assertEquals(expected.length, actual.size());
|
312 | 367 | for (var i = 0 ; i < actual.size() ; i++) {
|
|
0 commit comments