|
42 | 42 | import org.junit.jupiter.api.Test; |
43 | 43 | import org.opensearch.search.aggregations.AggregationBuilder; |
44 | 44 | import org.opensearch.sql.data.type.ExprType; |
| 45 | +import org.opensearch.sql.expression.function.PPLBuiltinOperators; |
45 | 46 | import org.opensearch.sql.opensearch.data.type.OpenSearchDataType; |
46 | 47 | import org.opensearch.sql.opensearch.data.type.OpenSearchDataType.MappingType; |
47 | 48 | import org.opensearch.sql.opensearch.request.AggregateAnalyzer.ExpressionNotAnalyzableException; |
|
52 | 53 | import org.opensearch.sql.opensearch.response.agg.OpenSearchAggregationResponseParser; |
53 | 54 | import org.opensearch.sql.opensearch.response.agg.SingleValueParser; |
54 | 55 | import org.opensearch.sql.opensearch.response.agg.StatsParser; |
| 56 | +import org.opensearch.sql.opensearch.response.agg.TopHitsParser; |
55 | 57 |
|
56 | 58 | class AggregateAnalyzerTest { |
57 | 59 |
|
@@ -345,6 +347,137 @@ void analyze_groupBy_TextWithoutKeyword() { |
345 | 347 | assertEquals("[field] must not be null", exception.getCause().getMessage()); |
346 | 348 | } |
347 | 349 |
|
| 350 | + @Test |
| 351 | + void analyze_firstAggregation() throws ExpressionNotAnalyzableException { |
| 352 | + AggregateCall firstCall = |
| 353 | + AggregateCall.create( |
| 354 | + PPLBuiltinOperators.FIRST, |
| 355 | + false, |
| 356 | + false, |
| 357 | + false, |
| 358 | + ImmutableList.of(), |
| 359 | + ImmutableList.of(0), |
| 360 | + -1, |
| 361 | + null, |
| 362 | + RelCollations.EMPTY, |
| 363 | + typeFactory.createSqlType(SqlTypeName.INTEGER), |
| 364 | + "first_a"); |
| 365 | + List<String> outputFields = List.of("first_a"); |
| 366 | + Aggregate aggregate = createMockAggregate(List.of(firstCall), ImmutableBitSet.of()); |
| 367 | + Project project = null; // No project needed for simple aggregation |
| 368 | + Pair<List<AggregationBuilder>, OpenSearchAggregationResponseParser> result = |
| 369 | + AggregateAnalyzer.analyze(aggregate, project, rowType, fieldTypes, outputFields, null); |
| 370 | + |
| 371 | + // Verify the aggregation builder JSON output |
| 372 | + assertEquals( |
| 373 | + "[{\"first_a\":{\"top_hits\":{\"from\":0,\"size\":1,\"version\":false,\"seq_no_primary_term\":false,\"explain\":false}}}]", |
| 374 | + result.getLeft().toString()); |
| 375 | + assertInstanceOf(NoBucketAggregationParser.class, result.getRight()); |
| 376 | + MetricParserHelper metricsParser = |
| 377 | + ((NoBucketAggregationParser) result.getRight()).getMetricsParser(); |
| 378 | + assertEquals(1, metricsParser.getMetricParserMap().size()); |
| 379 | + metricsParser |
| 380 | + .getMetricParserMap() |
| 381 | + .forEach( |
| 382 | + (k, v) -> { |
| 383 | + assertEquals("first_a", k); |
| 384 | + assertInstanceOf(TopHitsParser.class, v); |
| 385 | + }); |
| 386 | + } |
| 387 | + |
| 388 | + @Test |
| 389 | + void analyze_lastAggregation() throws ExpressionNotAnalyzableException { |
| 390 | + AggregateCall lastCall = |
| 391 | + AggregateCall.create( |
| 392 | + PPLBuiltinOperators.LAST, |
| 393 | + false, |
| 394 | + false, |
| 395 | + false, |
| 396 | + ImmutableList.of(), |
| 397 | + ImmutableList.of(1), |
| 398 | + -1, |
| 399 | + null, |
| 400 | + RelCollations.EMPTY, |
| 401 | + typeFactory.createSqlType(SqlTypeName.VARCHAR), |
| 402 | + "last_b"); |
| 403 | + List<String> outputFields = List.of("last_b"); |
| 404 | + Aggregate aggregate = createMockAggregate(List.of(lastCall), ImmutableBitSet.of()); |
| 405 | + Project project = null; // No project needed for simple aggregation |
| 406 | + Pair<List<AggregationBuilder>, OpenSearchAggregationResponseParser> result = |
| 407 | + AggregateAnalyzer.analyze(aggregate, project, rowType, fieldTypes, outputFields, null); |
| 408 | + |
| 409 | + // Verify the aggregation builder JSON output |
| 410 | + assertEquals( |
| 411 | + "[{\"last_b\":{\"top_hits\":{\"from\":0,\"size\":1,\"version\":false,\"seq_no_primary_term\":false,\"explain\":false,\"sort\":[{\"_doc\":{\"order\":\"desc\"}}]}}}]", |
| 412 | + result.getLeft().toString()); |
| 413 | + assertInstanceOf(NoBucketAggregationParser.class, result.getRight()); |
| 414 | + MetricParserHelper metricsParser = |
| 415 | + ((NoBucketAggregationParser) result.getRight()).getMetricsParser(); |
| 416 | + assertEquals(1, metricsParser.getMetricParserMap().size()); |
| 417 | + metricsParser |
| 418 | + .getMetricParserMap() |
| 419 | + .forEach( |
| 420 | + (k, v) -> { |
| 421 | + assertEquals("last_b", k); |
| 422 | + assertInstanceOf(TopHitsParser.class, v); |
| 423 | + }); |
| 424 | + } |
| 425 | + |
| 426 | + @Test |
| 427 | + void analyze_firstAndLastAggregations_withGroupBy() throws ExpressionNotAnalyzableException { |
| 428 | + AggregateCall firstCall = |
| 429 | + AggregateCall.create( |
| 430 | + PPLBuiltinOperators.FIRST, |
| 431 | + false, |
| 432 | + false, |
| 433 | + false, |
| 434 | + ImmutableList.of(), |
| 435 | + ImmutableList.of(0), |
| 436 | + -1, |
| 437 | + null, |
| 438 | + RelCollations.EMPTY, |
| 439 | + typeFactory.createSqlType(SqlTypeName.INTEGER), |
| 440 | + "first_a"); |
| 441 | + AggregateCall lastCall = |
| 442 | + AggregateCall.create( |
| 443 | + PPLBuiltinOperators.LAST, |
| 444 | + false, |
| 445 | + false, |
| 446 | + false, |
| 447 | + ImmutableList.of(), |
| 448 | + ImmutableList.of(0), |
| 449 | + -1, |
| 450 | + null, |
| 451 | + RelCollations.EMPTY, |
| 452 | + typeFactory.createSqlType(SqlTypeName.INTEGER), |
| 453 | + "last_a"); |
| 454 | + List<String> outputFields = List.of("b", "first_a", "last_a"); |
| 455 | + Aggregate aggregate = createMockAggregate(List.of(firstCall, lastCall), ImmutableBitSet.of(1)); |
| 456 | + Project project = |
| 457 | + createMockProject(List.of(0, 1)); // Need both fields for groupBy with aggregations |
| 458 | + Pair<List<AggregationBuilder>, OpenSearchAggregationResponseParser> result = |
| 459 | + AggregateAnalyzer.analyze(aggregate, project, rowType, fieldTypes, outputFields, null); |
| 460 | + |
| 461 | + assertEquals( |
| 462 | + "[{\"composite_buckets\":{\"composite\":{\"size\":1000,\"sources\":[" |
| 463 | + + "{\"b\":{\"terms\":{\"field\":\"b.keyword\",\"missing_bucket\":true,\"missing_order\":\"first\",\"order\":\"asc\"}}}]}," |
| 464 | + + "\"aggregations\":{" |
| 465 | + + "\"first_a\":{\"top_hits\":{\"from\":0,\"size\":1,\"version\":false,\"seq_no_primary_term\":false,\"explain\":false,\"_source\":{\"includes\":[\"a\"],\"excludes\":[]}}}," |
| 466 | + + "\"last_a\":{\"top_hits\":{\"from\":0,\"size\":1,\"version\":false,\"seq_no_primary_term\":false,\"explain\":false,\"_source\":{\"includes\":[\"a\"],\"excludes\":[]},\"sort\":[{\"_doc\":{\"order\":\"desc\"}}]}}}}}]", |
| 467 | + result.getLeft().toString()); |
| 468 | + assertInstanceOf(CompositeAggregationParser.class, result.getRight()); |
| 469 | + MetricParserHelper metricsParser = |
| 470 | + ((CompositeAggregationParser) result.getRight()).getMetricsParser(); |
| 471 | + assertEquals(2, metricsParser.getMetricParserMap().size()); |
| 472 | + metricsParser |
| 473 | + .getMetricParserMap() |
| 474 | + .forEach( |
| 475 | + (k, v) -> { |
| 476 | + assertTrue(k.equals("first_a") || k.equals("last_a")); |
| 477 | + assertInstanceOf(TopHitsParser.class, v); |
| 478 | + }); |
| 479 | + } |
| 480 | + |
348 | 481 | @Test |
349 | 482 | void analyze_aggCall_simpleFilter() throws ExpressionNotAnalyzableException { |
350 | 483 | buildAggregation("filter_cnt") |
|
0 commit comments