-
Notifications
You must be signed in to change notification settings - Fork 141
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Support construct AggregationResponseParser during Aggregator build s…
…tage (#108) * Support construct AggregationResponseParser during Aggregator build stage * modify the doc Signed-off-by: penghuo <[email protected]>
- Loading branch information
Showing
21 changed files
with
650 additions
and
253 deletions.
There are no files selected for viewing
This file contains 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
114 changes: 0 additions & 114 deletions
114
...main/java/org/opensearch/sql/opensearch/response/OpenSearchAggregationResponseParser.java
This file was deleted.
Oops, something went wrong.
This file contains 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
51 changes: 51 additions & 0 deletions
51
.../src/main/java/org/opensearch/sql/opensearch/response/agg/CompositeAggregationParser.java
This file contains 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,51 @@ | ||
/* | ||
* Licensed under the Apache License, Version 2.0 (the "License"). | ||
* You may not use this file except in compliance with the License. | ||
* A copy of the License is located at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* or in the "license" file accompanying this file. This file is distributed | ||
* on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either | ||
* express or implied. See the License for the specific language governing | ||
* permissions and limitations under the License. | ||
*/ | ||
|
||
package org.opensearch.sql.opensearch.response.agg; | ||
|
||
import java.util.Arrays; | ||
import java.util.HashMap; | ||
import java.util.List; | ||
import java.util.Map; | ||
import java.util.stream.Collectors; | ||
import org.opensearch.search.aggregations.Aggregations; | ||
import org.opensearch.search.aggregations.bucket.composite.CompositeAggregation; | ||
|
||
/** | ||
* Composite Aggregation Parser which include composite aggregation and metric parsers. | ||
*/ | ||
public class CompositeAggregationParser implements OpenSearchAggregationResponseParser { | ||
|
||
private final MetricParserHelper metricsParser; | ||
|
||
public CompositeAggregationParser(MetricParser... metricParserList) { | ||
metricsParser = new MetricParserHelper(Arrays.asList(metricParserList)); | ||
} | ||
|
||
public CompositeAggregationParser(List<MetricParser> metricParserList) { | ||
metricsParser = new MetricParserHelper(metricParserList); | ||
} | ||
|
||
@Override | ||
public List<Map<String, Object>> parse(Aggregations aggregations) { | ||
return ((CompositeAggregation) aggregations.asList().get(0)) | ||
.getBuckets().stream().map(this::parse).collect(Collectors.toList()); | ||
} | ||
|
||
private Map<String, Object> parse(CompositeAggregation.Bucket bucket) { | ||
Map<String, Object> resultMap = new HashMap<>(); | ||
resultMap.putAll(bucket.getKey()); | ||
resultMap.putAll(metricsParser.parse(bucket.getAggregations())); | ||
return resultMap; | ||
} | ||
} |
38 changes: 38 additions & 0 deletions
38
opensearch/src/main/java/org/opensearch/sql/opensearch/response/agg/FilterParser.java
This file contains 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,38 @@ | ||
/* | ||
* Licensed under the Apache License, Version 2.0 (the "License"). | ||
* You may not use this file except in compliance with the License. | ||
* A copy of the License is located at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* or in the "license" file accompanying this file. This file is distributed | ||
* on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either | ||
* express or implied. See the License for the specific language governing | ||
* permissions and limitations under the License. | ||
*/ | ||
|
||
package org.opensearch.sql.opensearch.response.agg; | ||
|
||
import java.util.Map; | ||
import lombok.Builder; | ||
import lombok.Getter; | ||
import org.opensearch.search.aggregations.Aggregation; | ||
import org.opensearch.search.aggregations.bucket.filter.Filter; | ||
|
||
/** | ||
* {@link Filter} Parser. | ||
* The current use case is filter aggregation, e.g. avg(age) filter(balance>0). The filter parser | ||
* do nothing and return the result from metricsParser. | ||
*/ | ||
@Builder | ||
public class FilterParser implements MetricParser { | ||
|
||
private final MetricParser metricsParser; | ||
|
||
@Getter private final String name; | ||
|
||
@Override | ||
public Map<String, Object> parse(Aggregation aggregations) { | ||
return metricsParser.parse(((Filter) aggregations).getAggregations().asList().get(0)); | ||
} | ||
} |
36 changes: 36 additions & 0 deletions
36
opensearch/src/main/java/org/opensearch/sql/opensearch/response/agg/MetricParser.java
This file contains 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,36 @@ | ||
/* | ||
* Licensed under the Apache License, Version 2.0 (the "License"). | ||
* You may not use this file except in compliance with the License. | ||
* A copy of the License is located at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* or in the "license" file accompanying this file. This file is distributed | ||
* on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either | ||
* express or implied. See the License for the specific language governing | ||
* permissions and limitations under the License. | ||
*/ | ||
|
||
package org.opensearch.sql.opensearch.response.agg; | ||
|
||
import java.util.Map; | ||
import org.opensearch.search.aggregations.Aggregation; | ||
|
||
/** | ||
* Metric Aggregation Parser. | ||
*/ | ||
public interface MetricParser { | ||
|
||
/** | ||
* Get the name of metric parser. | ||
*/ | ||
String getName(); | ||
|
||
/** | ||
* Parse the {@link Aggregation}. | ||
* | ||
* @param aggregation {@link Aggregation} | ||
* @return the map between metric name and metric value. | ||
*/ | ||
Map<String, Object> parse(Aggregation aggregation); | ||
} |
56 changes: 56 additions & 0 deletions
56
opensearch/src/main/java/org/opensearch/sql/opensearch/response/agg/MetricParserHelper.java
This file contains 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,56 @@ | ||
/* | ||
* Licensed under the Apache License, Version 2.0 (the "License"). | ||
* You may not use this file except in compliance with the License. | ||
* A copy of the License is located at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* or in the "license" file accompanying this file. This file is distributed | ||
* on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either | ||
* express or implied. See the License for the specific language governing | ||
* permissions and limitations under the License. | ||
*/ | ||
|
||
package org.opensearch.sql.opensearch.response.agg; | ||
|
||
import java.util.HashMap; | ||
import java.util.List; | ||
import java.util.Map; | ||
import java.util.stream.Collectors; | ||
import lombok.RequiredArgsConstructor; | ||
import org.opensearch.search.aggregations.Aggregation; | ||
import org.opensearch.search.aggregations.Aggregations; | ||
import org.opensearch.sql.common.utils.StringUtils; | ||
|
||
/** | ||
* Parse multiple metrics in one bucket. | ||
*/ | ||
@RequiredArgsConstructor | ||
public class MetricParserHelper { | ||
|
||
private final Map<String, MetricParser> metricParserMap; | ||
|
||
public MetricParserHelper(List<MetricParser> metricParserList) { | ||
metricParserMap = | ||
metricParserList.stream().collect(Collectors.toMap(MetricParser::getName, m -> m)); | ||
} | ||
|
||
/** | ||
* Parse {@link Aggregations}. | ||
* | ||
* @param aggregations {@link Aggregations} | ||
* @return the map between metric name and metric value. | ||
*/ | ||
public Map<String, Object> parse(Aggregations aggregations) { | ||
Map<String, Object> resultMap = new HashMap<>(); | ||
for (Aggregation aggregation : aggregations) { | ||
if (metricParserMap.containsKey(aggregation.getName())) { | ||
resultMap.putAll(metricParserMap.get(aggregation.getName()).parse(aggregation)); | ||
} else { | ||
throw new RuntimeException(StringUtils.format("couldn't parse field %s in aggregation " | ||
+ "response", aggregation.getName())); | ||
} | ||
} | ||
return resultMap; | ||
} | ||
} |
41 changes: 41 additions & 0 deletions
41
...h/src/main/java/org/opensearch/sql/opensearch/response/agg/NoBucketAggregationParser.java
This file contains 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,41 @@ | ||
/* | ||
* Licensed under the Apache License, Version 2.0 (the "License"). | ||
* You may not use this file except in compliance with the License. | ||
* A copy of the License is located at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* or in the "license" file accompanying this file. This file is distributed | ||
* on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either | ||
* express or implied. See the License for the specific language governing | ||
* permissions and limitations under the License. | ||
*/ | ||
|
||
package org.opensearch.sql.opensearch.response.agg; | ||
|
||
import java.util.Arrays; | ||
import java.util.Collections; | ||
import java.util.List; | ||
import java.util.Map; | ||
import org.opensearch.search.aggregations.Aggregations; | ||
|
||
/** | ||
* No Bucket Aggregation Parser which include only metric parsers. | ||
*/ | ||
public class NoBucketAggregationParser implements OpenSearchAggregationResponseParser { | ||
|
||
private final MetricParserHelper metricsParser; | ||
|
||
public NoBucketAggregationParser(MetricParser... metricParserList) { | ||
metricsParser = new MetricParserHelper(Arrays.asList(metricParserList)); | ||
} | ||
|
||
public NoBucketAggregationParser(List<MetricParser> metricParserList) { | ||
metricsParser = new MetricParserHelper(metricParserList); | ||
} | ||
|
||
@Override | ||
public List<Map<String, Object>> parse(Aggregations aggregations) { | ||
return Collections.singletonList(metricsParser.parse(aggregations)); | ||
} | ||
} |
Oops, something went wrong.