Skip to content

Restore typed buckets for terms aggregates - #2315

Open
minseoky wants to merge 2 commits into
opensearch-project:mainfrom
minseoky:fix/terms-aggregate-typed-buckets
Open

minseoky wants to merge 2 commits into
opensearch-project:mainfrom
minseoky:fix/terms-aggregate-typed-buckets

Conversation

@minseoky

Copy link
Copy Markdown

Description

Fixes the code generator so that the terms aggregates are generated with their bucket type again (TermsAggregateBase<StringTermsBucket> instead of TermsAggregateBase<JsonData>), and regenerates the client. Details and history are in #2302.

Changes to java-codegen, and why

  • TypeMapper.mapInstantiatedGeneric: the generic schema declares buckets as a union (Buckets<TBucket> = oneOf: [keyed map of TBucket, array of TBucket]), while the terms instantiations declare only one branch of it (buckets: array of StringTermsBucket, as the specification does since Fix Terms aggregation response spec accuracy issues opensearch-api-specification#1063). The zipped walk of generic and instantiation never pairs TBucket with StringTermsBucket, so the type parameter silently fell back to JsonData. The change binds the parameter from the union branch whose container kind matches the instantiation (items ↔ items, additionalProperties ↔ additionalProperties). Instantiations that keep the full union shape are untouched (right.hasOneOf() returns early), which is why no other generated file changes.
  • GenericLabelingVisitor.isInstantiatedGeneric: isGeneric propagates through plain references (MultiBucketBase.additionalProperties: Aggregate → TopHitsAggregate.hits → HitsMetadata<TDocument>), so TermsBucketBase is labeled generic although it declares no type parameter. Once the buckets were typed again, that made UnsignedLongTermsAggregate come out as TermsAggregateBase<TermsBucketBase<>>. The added guard only treats a subclass as an instantiation when the extended schema's own chain declares a type parameter.

I would appreciate a check on whether this is an acceptable way to handle the narrowed-union case in TypeMapper. The broader alternative is to make the generic labeling order-independent, as discussed in #2298; this change is deliberately narrower so that it can land without that.

Dependency

The first commit is #2298's NPE fix (28175b8), cherry-picked unchanged with its author: the new binding instantiates StringTermsBucket inside the aggregation reference cycle and hits exactly that NullPointerException without it. If #2298 lands first I will rebase and drop it; if this lands first, #2298 can drop that commit.

Generated code

Tests and samples

AbstractRequestIT, AbstractSearchTemplateRequestIT, TypedKeysTest and samples/Search.java return to typed bucket access; they had been adjusted to JsonData buckets in #1957.

Verification

  • ./gradlew :java-codegen:run — only the 11 terms-related files change, and up_to_date reproduces the committed output
  • ./gradlew clean build -x test, spotlessJavaCheck, :java-codegen:test — pass
  • ./gradlew unitTest — 663 tests, 0 failures
  • ./gradlew clean integrationTest — 39 suites, 243 tests, 0 failures (OpenSearch via Testcontainers)

Issues Resolved

Closes #2302. Also resolves #613 and #1737 (doc_count_error_upper_bound on TermsBucketBase).

By submitting this pull request, I confirm that my contribution is made under the terms of the Apache 2.0 license.
For more information on following Developer Certificate of Origin and signing off your commits, please check here.

yyfamazon and others added 2 commits November 23, 2026 10:53
…de its own reference cycle

TypeMapper.mapInstantiatedGeneric read the type parameters of the generic
shape, which are collected as its fields are added. Instantiated while it is
still being visited (Hit -> inner_hits -> HitsMetadata -> Hit<TDocument>),
the shape has none yet and getTypeParameters() is null. Whether that happens
depends on the order the cycle is entered in; spec opensearch-project#1113 changes it and
breaks the scheduled code generation. The parameters are now read from the
generic schema in that case. The code generated from the vendored spec is
unchanged.

Signed-off-by: yyfamazon <yyf@amazon.com>
(cherry picked from commit 28175b8)
The terms aggregates were generated as TermsAggregateBase<JsonData> since the
October regeneration (opensearch-project#1957): the specification narrows the generic Buckets
union to a plain array for terms (opensearch-api-specification opensearch-project#1063), and
TypeMapper.mapInstantiatedGeneric only bound a type parameter when the generic
schema and the instantiation had the same shape, so TBucket was never bound
and fell back to JsonData.

- TypeMapper: bind the parameter from the union branch whose container kind
  matches the instantiation (items <-> items, additionalProperties <->
  additionalProperties).
- GenericLabelingVisitor: only treat a subclass as an instantiation when the
  extended schema's own chain declares a type parameter; isGeneric also
  propagates through plain references, which mislabeled TermsBucketBase and
  produced TermsAggregateBase<TermsBucketBase<>> for UnsignedLongTermsAggregate.
- Regenerate: the five terms aggregates get their bucket type back and the
  bucket classes are generated again, now with doc_count_error_upper_bound.
- Return AbstractRequestIT, AbstractSearchTemplateRequestIT and TypedKeysTest
  to typed bucket access.

Closes opensearch-project#2302

Signed-off-by: Minseok Choi <minseok184@gmail.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

2 participants