Skip to content
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

Correct handling of null max aggregation values in SearchResponse #1292

Merged
merged 1 commit into from
Sep 3, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -699,7 +699,13 @@ public static Optional<Long> getLatestDataTime(SearchResponse searchResponse) {
.map(SearchResponse::getAggregations)
.map(aggs -> aggs.asMap())
.map(map -> (Max) map.get(CommonName.AGG_NAME_MAX_TIME))
.map(agg -> (long) agg.getValue());
// Explanation:
// - `agg != null`: Ensures the aggregation object is not null.
// - `agg.getValue() > 0`: Checks that the value is positive, as the aggregation might return a default negative value
// like -9223372036854775808 if the underlying data is null or invalid. This check prevents converting such invalid
// values to a timestamp, which would not make sense in the context of time-based data.
.filter(agg -> agg != null && agg.getValue() > 0) // Check if the value is valid and positive.
.map(agg -> (long) agg.getValue()); // Cast to long if valid
}

/**
Expand Down
157 changes: 157 additions & 0 deletions src/test/java/org/opensearch/timeseries/util/ParseUtilsTests.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,157 @@
/*
* Copyright OpenSearch Contributors
* SPDX-License-Identifier: Apache-2.0
*/

package org.opensearch.timeseries.util;

import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;

import java.util.Collections;
import java.util.Optional;

import org.apache.lucene.search.TotalHits;
import org.opensearch.action.search.SearchResponse;
import org.opensearch.action.search.SearchResponseSections;
import org.opensearch.action.search.ShardSearchFailure;
import org.opensearch.search.SearchHit;
import org.opensearch.search.SearchHits;
import org.opensearch.search.aggregations.Aggregations;
import org.opensearch.search.aggregations.metrics.Max;
import org.opensearch.test.OpenSearchTestCase;
import org.opensearch.timeseries.constant.CommonName;

public class ParseUtilsTests extends OpenSearchTestCase {

public void testGetLatestDataTime_withValidMaxValue() {
Max maxAggregation = mock(Max.class);
when(maxAggregation.getValue()).thenReturn(1623840000000.0); // A valid timestamp value

Aggregations aggregations = new Aggregations(Collections.singletonList(maxAggregation));
when(maxAggregation.getName()).thenReturn(CommonName.AGG_NAME_MAX_TIME);

SearchResponseSections sections = new SearchResponseSections(
new SearchHits(new SearchHit[0], new TotalHits(0, TotalHits.Relation.EQUAL_TO), 0),
aggregations,
null,
false,
null,
null,
1
);
SearchResponse searchResponse = new SearchResponse(
sections,
null,
0,
0,
0,
0L,
ShardSearchFailure.EMPTY_ARRAY,
SearchResponse.Clusters.EMPTY
);

Optional<Long> result = ParseUtils.getLatestDataTime(searchResponse);

assertTrue(result.isPresent());
assertEquals("got " + result.get(), 1623840000000L, result.get().longValue());
}

public void testGetLatestDataTime_withNullValue() {
Max maxAggregation = mock(Max.class);
when(maxAggregation.getValue()).thenReturn(Double.NEGATIVE_INFINITY); // Simulating a missing value scenario

Aggregations aggregations = new Aggregations(Collections.singletonList(maxAggregation));
when(maxAggregation.getName()).thenReturn(CommonName.AGG_NAME_MAX_TIME);

SearchResponseSections sections = new SearchResponseSections(
new SearchHits(new SearchHit[0], new TotalHits(0, TotalHits.Relation.EQUAL_TO), 0),
aggregations,
null,
false,
null,
null,
1
);
SearchResponse searchResponse = new SearchResponse(
sections,
null,
0,
0,
0,
0L,
ShardSearchFailure.EMPTY_ARRAY,
SearchResponse.Clusters.EMPTY
);

// Act
Optional<Long> result = ParseUtils.getLatestDataTime(searchResponse);

// Assert
assertTrue(result.isEmpty());
}

public void testGetLatestDataTime_withNoAggregations() {
// Arrange
SearchResponseSections sections = new SearchResponseSections(
new SearchHits(new SearchHit[0], new TotalHits(0, TotalHits.Relation.EQUAL_TO), 0),
null, // No aggregations provided
null,
false,
null,
null,
1
);
SearchResponse searchResponse = new SearchResponse(
sections,
null,
0,
0,
0,
0L,
ShardSearchFailure.EMPTY_ARRAY,
SearchResponse.Clusters.EMPTY
);

// Act
Optional<Long> result = ParseUtils.getLatestDataTime(searchResponse);

// Assert
assertTrue(result.isEmpty());
}

public void testGetLatestDataTime_withNegativeMaxValue() {
// Arrange
Max maxAggregation = mock(Max.class);
when(maxAggregation.getValue()).thenReturn(-9223372036854775808.0); // Invalid negative value

Aggregations aggregations = new Aggregations(Collections.singletonList(maxAggregation));
when(maxAggregation.getName()).thenReturn(CommonName.AGG_NAME_MAX_TIME);

SearchResponseSections sections = new SearchResponseSections(
new SearchHits(new SearchHit[0], new TotalHits(0, TotalHits.Relation.EQUAL_TO), 0),
aggregations,
null,
false,
null,
null,
1
);
SearchResponse searchResponse = new SearchResponse(
sections,
null,
0,
0,
0,
0L,
ShardSearchFailure.EMPTY_ARRAY,
SearchResponse.Clusters.EMPTY
);

// Act
Optional<Long> result = ParseUtils.getLatestDataTime(searchResponse);

// Assert
assertTrue(result.isEmpty());
}
}
Loading