-
Notifications
You must be signed in to change notification settings - Fork 2.3k
[GRPC] Add Terms, Cardinality, Missing aggregations #19894
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Draft
karenyrx
wants to merge
1
commit into
opensearch-project:main
Choose a base branch
from
karenyrx:aggsgrpc
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
1 change: 1 addition & 0 deletions
1
modules/transport-grpc/licenses/protobufs-0.24.0-SNAPSHOT.jar.sha1
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| 20aae22bf8609cb6a963f6897c09384f8d5ecb78 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
1 change: 1 addition & 0 deletions
1
modules/transport-grpc/spi/licenses/protobufs-0.24.0-SNAPSHOT.jar.sha1
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| 20aae22bf8609cb6a963f6897c09384f8d5ecb78 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
119 changes: 119 additions & 0 deletions
119
...earch/transport/grpc/proto/request/search/aggregation/AggregationContainerProtoUtils.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,119 @@ | ||
| /* | ||
| * SPDX-License-Identifier: Apache-2.0 | ||
| * | ||
| * The OpenSearch Contributors require contributions made to | ||
| * this file be licensed under the Apache-2.0 license or a | ||
| * compatible open source license. | ||
| */ | ||
| package org.opensearch.transport.grpc.proto.request.search.aggregation; | ||
|
|
||
| import org.opensearch.core.xcontent.XContentParser; | ||
| import org.opensearch.protobufs.AggregationContainer; | ||
| import org.opensearch.protobufs.ObjectMap; | ||
| import org.opensearch.search.aggregations.AggregationBuilder; | ||
| import org.opensearch.transport.grpc.proto.request.common.ObjectMapProtoUtils; | ||
| import org.opensearch.transport.grpc.proto.request.search.query.AbstractQueryBuilderProtoUtils; | ||
|
|
||
| import java.util.Map; | ||
|
|
||
| /** | ||
| * Utility class for converting AggregationContainer Protocol Buffers to OpenSearch AggregationBuilder objects. | ||
| * This class handles the transformation of Protocol Buffer aggregation containers into their corresponding | ||
| * OpenSearch AggregationBuilder implementations. It serves as the main entry point for converting all | ||
| * aggregation types from protobuf format to OpenSearch format. | ||
| * | ||
| * <p>This class is analogous to the REST-side aggregation parsing framework where {@link XContentParser} | ||
| * is used to parse aggregations from JSON/REST requests. Each aggregation type's {@code PARSER} field | ||
| * (which uses {@code fromXContent}) has a corresponding protobuf converter method that performs the same | ||
| * logical transformation but from Protocol Buffer representation. | ||
| * | ||
| * <p>The dispatch logic mirrors how REST aggregations are parsed: by examining the aggregation type field | ||
| * and delegating to the appropriate type-specific parser. | ||
| */ | ||
| public class AggregationContainerProtoUtils { | ||
|
|
||
| private AggregationContainerProtoUtils() { | ||
| // Utility class, no instances | ||
| } | ||
|
|
||
| /** | ||
| * Converts a Protocol Buffer AggregationContainer to an OpenSearch AggregationBuilder. | ||
| * This method dispatches to the appropriate aggregation-specific converter based on the | ||
| * aggregation type specified in the container. | ||
| * | ||
| * <p>This is the gRPC equivalent of the REST-side aggregation parsing in | ||
| * {@code AggregationBuilder.parseAggregators}, where each aggregation type has a registered | ||
| * parser that reads from {@link XContentParser} via {@code fromXContent}. This method performs | ||
| * the same role but reads from Protocol Buffer structures instead of JSON. | ||
| * | ||
| * @param name The name of the aggregation | ||
| * @param aggContainer The Protocol Buffer AggregationContainer object | ||
| * @return A configured AggregationBuilder instance | ||
| * @throws IllegalArgumentException if the aggregation type is not supported or unrecognized | ||
| * @see org.opensearch.search.aggregations.AggregationBuilder | ||
| */ | ||
| public static AggregationBuilder fromProto(String name, AggregationContainer aggContainer) { | ||
| return fromProto(name, aggContainer, null); | ||
| } | ||
|
|
||
| /** | ||
| * Converts a Protocol Buffer AggregationContainer to an OpenSearch AggregationBuilder. | ||
| * This method dispatches to the appropriate aggregation-specific converter based on the | ||
| * aggregation type specified in the container. It also supports nested queries in aggregations | ||
| * (like filter aggregations) by accepting a queryUtils parameter. | ||
| * | ||
| * <p>This is the gRPC equivalent of the REST-side aggregation parsing in | ||
| * {@code AggregationBuilder.parseAggregators}, where each aggregation type has a registered | ||
| * parser that reads from {@link XContentParser} via {@code fromXContent}. This method performs | ||
| * the same role but reads from Protocol Buffer structures instead of JSON. | ||
| * | ||
| * @param name The name of the aggregation | ||
| * @param aggContainer The Protocol Buffer AggregationContainer object | ||
| * @param queryUtils The query utils instance for parsing nested queries (can be null if not needed) | ||
| * @return A configured AggregationBuilder instance | ||
| * @throws IllegalArgumentException if the aggregation type is not supported or unrecognized | ||
| * @see org.opensearch.search.aggregations.AggregationBuilder | ||
| */ | ||
| public static AggregationBuilder fromProto(String name, AggregationContainer aggContainer, AbstractQueryBuilderProtoUtils queryUtils) { | ||
| AggregationBuilder builder; | ||
|
|
||
| // Dispatch based on the aggregation type | ||
| switch (aggContainer.getAggregationCase()) { | ||
| case CARDINALITY: | ||
| builder = CardinalityAggregationProtoUtils.fromProto(name, aggContainer.getCardinality()); | ||
| break; | ||
|
|
||
| case MISSING: | ||
| builder = MissingAggregationProtoUtils.fromProto(name, aggContainer.getMissing()); | ||
| break; | ||
|
|
||
| case TERMS: | ||
| builder = TermsAggregationProtoUtils.fromProto(name, aggContainer.getTerms()); | ||
| break; | ||
|
|
||
| case FILTER: | ||
| if (queryUtils == null) { | ||
| throw new IllegalArgumentException("Filter aggregation requires queryUtils to be provided for parsing nested queries"); | ||
| } | ||
| builder = FilterAggregationProtoUtils.fromProto(name, aggContainer.getFilter(), queryUtils); | ||
| break; | ||
|
|
||
| case AGGREGATION_NOT_SET: | ||
| throw new IllegalArgumentException("Aggregation type not set for aggregation: " + name); | ||
|
|
||
| default: | ||
| throw new IllegalArgumentException( | ||
| "Unsupported aggregation type: " + aggContainer.getAggregationCase() + " for aggregation: " + name | ||
| ); | ||
| } | ||
|
|
||
| // Handle metadata if present | ||
| if (aggContainer.hasMeta()) { | ||
| ObjectMap metaProto = aggContainer.getMeta(); | ||
| Map<String, Object> metadata = ObjectMapProtoUtils.fromProto(metaProto); | ||
| builder.setMetadata(metadata); | ||
| } | ||
|
|
||
| return builder; | ||
| } | ||
| } |
111 changes: 111 additions & 0 deletions
111
...rch/transport/grpc/proto/request/search/aggregation/CardinalityAggregationProtoUtils.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,111 @@ | ||
| /* | ||
| * SPDX-License-Identifier: Apache-2.0 | ||
| * | ||
| * The OpenSearch Contributors require contributions made to | ||
| * this file be licensed under the Apache-2.0 license or a | ||
| * compatible open source license. | ||
| */ | ||
| package org.opensearch.transport.grpc.proto.request.search.aggregation; | ||
|
|
||
| import org.opensearch.protobufs.CardinalityAggregation; | ||
| import org.opensearch.protobufs.CardinalityExecutionMode; | ||
| import org.opensearch.protobufs.FieldValue; | ||
| import org.opensearch.search.aggregations.metrics.CardinalityAggregationBuilder; | ||
| import org.opensearch.transport.grpc.proto.request.common.ScriptProtoUtils; | ||
| import org.opensearch.transport.grpc.proto.response.common.FieldValueProtoUtils; | ||
| import org.opensearch.transport.grpc.util.ProtobufEnumUtils; | ||
|
|
||
| import java.lang.reflect.Method; | ||
|
|
||
| /** | ||
| * Utility class for converting CardinalityAggregation Protocol Buffers to OpenSearch objects. | ||
| * This class provides methods to transform Protocol Buffer representations of cardinality aggregations | ||
| * into their corresponding OpenSearch CardinalityAggregationBuilder implementations. | ||
| */ | ||
| public class CardinalityAggregationProtoUtils { | ||
|
|
||
| private CardinalityAggregationProtoUtils() { | ||
| // Utility class, no instances | ||
| } | ||
|
|
||
| /** | ||
| * Converts a Protocol Buffer CardinalityAggregation to an OpenSearch CardinalityAggregationBuilder. | ||
| * | ||
| * <p>This method is the gRPC equivalent of {@link CardinalityAggregationBuilder#PARSER}, which parses | ||
| * cardinality aggregations from REST/JSON requests via {@code fromXContent}. Similar to how the parser | ||
| * reads JSON fields, this method extracts values from the Protocol Buffer representation and creates | ||
| * a properly configured CardinalityAggregationBuilder. | ||
| * | ||
| * <p>The REST-side serialization via {@link CardinalityAggregationBuilder#doXContentBody} produces | ||
| * JSON that conceptually mirrors the protobuf structure used here. | ||
| * | ||
| * @param name The name of the aggregation | ||
| * @param cardinalityAggProto The Protocol Buffer CardinalityAggregation object | ||
| * @return A configured CardinalityAggregationBuilder instance | ||
| * @throws IllegalArgumentException if the field value type is not supported | ||
| * @see CardinalityAggregationBuilder#PARSER | ||
| * @see CardinalityAggregationBuilder#doXContentBody | ||
| */ | ||
| public static CardinalityAggregationBuilder fromProto(String name, CardinalityAggregation cardinalityAggProto) { | ||
| CardinalityAggregationBuilder builder = new CardinalityAggregationBuilder(name); | ||
|
|
||
| // Set field if present | ||
| if (cardinalityAggProto.hasField()) { | ||
| builder.field(cardinalityAggProto.getField()); | ||
| } | ||
|
|
||
| // Set missing value if present | ||
| if (cardinalityAggProto.hasMissing()) { | ||
| FieldValue missingValue = cardinalityAggProto.getMissing(); | ||
| Object missing = FieldValueProtoUtils.fromProto(missingValue, false); | ||
| builder.missing(missing); | ||
| } | ||
|
|
||
| // Set script if present | ||
| if (cardinalityAggProto.hasScript()) { | ||
| try { | ||
| builder.script(ScriptProtoUtils.parseFromProtoRequest(cardinalityAggProto.getScript())); | ||
| } catch (Exception e) { | ||
| throw new IllegalArgumentException("Failed to parse script for cardinality aggregation", e); | ||
| } | ||
| } | ||
|
|
||
| // Set precision threshold if present | ||
| if (cardinalityAggProto.hasPrecisionThreshold()) { | ||
| builder.precisionThreshold(cardinalityAggProto.getPrecisionThreshold()); | ||
| } | ||
|
|
||
| // Set execution hint if present (added in OpenSearch 2.19.1) | ||
| // Use reflection for forward-compatibility: if the OpenSearch version doesn't support | ||
| // this field yet, it will be silently ignored rather than causing a compilation error. | ||
| if (cardinalityAggProto.hasExecutionHint()) { | ||
| String executionHint = convertExecutionMode(cardinalityAggProto.getExecutionHint()); | ||
| if (executionHint != null) { | ||
| try { | ||
| Method method = CardinalityAggregationBuilder.class.getMethod("executionHint", String.class); | ||
| method.invoke(builder, executionHint); | ||
| } catch (NoSuchMethodException e) { | ||
| // Method doesn't exist in OpenSearch < 2.19.1 - silently ignore | ||
| } catch (Exception e) { | ||
| throw new IllegalArgumentException("Failed to set execution hint for cardinality aggregation", e); | ||
| } | ||
| } | ||
| } | ||
|
|
||
| return builder; | ||
| } | ||
|
|
||
| /** | ||
| * Converts a Protocol Buffer CardinalityExecutionMode to its string representation. | ||
| * Supports execution hints added in OpenSearch 2.19.1. | ||
| * | ||
| * @param mode The Protocol Buffer CardinalityExecutionMode | ||
| * @return The string representation of the execution mode, or null if unspecified | ||
| */ | ||
| private static String convertExecutionMode(CardinalityExecutionMode mode) { | ||
| if (mode == CardinalityExecutionMode.CARDINALITY_EXECUTION_MODE_UNSPECIFIED) { | ||
| return null; | ||
| } | ||
| return ProtobufEnumUtils.convertToString(mode); | ||
| } | ||
| } | ||
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Remove this