diff --git a/server/src/main/java/org/opensearch/common/util/FeatureFlags.java b/server/src/main/java/org/opensearch/common/util/FeatureFlags.java index 6df68013a8119..e663d8429da13 100644 --- a/server/src/main/java/org/opensearch/common/util/FeatureFlags.java +++ b/server/src/main/java/org/opensearch/common/util/FeatureFlags.java @@ -100,7 +100,7 @@ public class FeatureFlags { * aggregations. */ public static final String STAR_TREE_INDEX = "opensearch.experimental.feature.composite_index.star_tree.enabled"; - public static final Setting STAR_TREE_INDEX_SETTING = Setting.boolSetting(STAR_TREE_INDEX, false, Property.NodeScope); + public static final Setting STAR_TREE_INDEX_SETTING = Setting.boolSetting(STAR_TREE_INDEX, true, Property.NodeScope); /** * Gates the functionality of application based configuration templates. diff --git a/server/src/main/java/org/opensearch/index/compositeindex/datacube/startree/utils/StarTreeQueryHelper.java b/server/src/main/java/org/opensearch/index/compositeindex/datacube/startree/utils/StarTreeQueryHelper.java index b362b083a8d20..e538be5d5bece 100644 --- a/server/src/main/java/org/opensearch/index/compositeindex/datacube/startree/utils/StarTreeQueryHelper.java +++ b/server/src/main/java/org/opensearch/index/compositeindex/datacube/startree/utils/StarTreeQueryHelper.java @@ -212,7 +212,7 @@ public static LeafBucketCollector getStarTreeLeafCollector( } // Iterate over the values for the current entryId - for (int i = 0, count = valuesIterator.valuesCount(); i < count; i++) { + for (int i = 0, count = valuesIterator.entryValueCount(); i < count; i++) { long value = valuesIterator.nextValue(); valueConsumer.accept(value); // Apply the consumer operation (e.g., max, sum) } diff --git a/server/src/main/java/org/opensearch/index/compositeindex/datacube/startree/utils/iterator/SortedNumericStarTreeValuesIterator.java b/server/src/main/java/org/opensearch/index/compositeindex/datacube/startree/utils/iterator/SortedNumericStarTreeValuesIterator.java index 7d65e9eb1da8e..4b4bfa6a915eb 100644 --- a/server/src/main/java/org/opensearch/index/compositeindex/datacube/startree/utils/iterator/SortedNumericStarTreeValuesIterator.java +++ b/server/src/main/java/org/opensearch/index/compositeindex/datacube/startree/utils/iterator/SortedNumericStarTreeValuesIterator.java @@ -30,7 +30,7 @@ public long nextValue() throws IOException { return ((SortedNumericDocValues) docIdSetIterator).nextValue(); } - public int valuesCount() throws IOException { + public int entryValueCount() throws IOException { return ((SortedNumericDocValues) docIdSetIterator).docValueCount(); } diff --git a/server/src/main/java/org/opensearch/search/aggregations/metrics/AvgAggregator.java b/server/src/main/java/org/opensearch/search/aggregations/metrics/AvgAggregator.java index 0a3a514701e15..2970c5ca851e7 100644 --- a/server/src/main/java/org/opensearch/search/aggregations/metrics/AvgAggregator.java +++ b/server/src/main/java/org/opensearch/search/aggregations/metrics/AvgAggregator.java @@ -184,7 +184,7 @@ public LeafBucketCollector getStarTreeLeafCollector(LeafReaderContext ctx, LeafB } // Iterate over the values for the current entryId - for (int i = 0; i < sumValuesIterator.valuesCount(); i++) { + for (int i = 0; i < sumValuesIterator.entryValueCount(); i++) { kahanSummation.add(NumericUtils.sortableLongToDouble(sumValuesIterator.nextValue())); counts.increment(0, countValueIterator.nextValue()); // Apply the consumer operation (e.g., max, sum) } diff --git a/server/src/main/java/org/opensearch/search/startree/StarTreeFilter.java b/server/src/main/java/org/opensearch/search/startree/StarTreeFilter.java index f1cc1f11a671a..f7fa210691678 100644 --- a/server/src/main/java/org/opensearch/search/startree/StarTreeFilter.java +++ b/server/src/main/java/org/opensearch/search/startree/StarTreeFilter.java @@ -89,7 +89,7 @@ public static FixedBitSet getStarTreeResult(StarTreeValues starTreeValues, Map remainingPredicateColumns; public final int numOfMatchedDocs; diff --git a/server/src/main/java/org/opensearch/search/startree/StarTreeQueryContext.java b/server/src/main/java/org/opensearch/search/startree/StarTreeQueryContext.java index 5a53dd122cdb0..cda3a25b30e53 100644 --- a/server/src/main/java/org/opensearch/search/startree/StarTreeQueryContext.java +++ b/server/src/main/java/org/opensearch/search/startree/StarTreeQueryContext.java @@ -76,5 +76,4 @@ public void setStarTreeValues(LeafReaderContext ctx, FixedBitSet values) { starTreeValues[ctx.ord] = values; } } - } diff --git a/server/src/test/java/org/opensearch/search/SearchServiceStarTreeTests.java b/server/src/test/java/org/opensearch/search/SearchServiceStarTreeTests.java index 0c88154ca2b38..f646b6b063ed0 100644 --- a/server/src/test/java/org/opensearch/search/SearchServiceStarTreeTests.java +++ b/server/src/test/java/org/opensearch/search/SearchServiceStarTreeTests.java @@ -141,6 +141,8 @@ private void assertStarTreeContext( SearchContext context = searchService.createContext(reader, request, null, true); StarTreeQueryContext actualContext = context.getStarTreeQueryContext(); + + if (expectedContext == null) { assertThat(context.getStarTreeQueryContext(), nullValue()); } else { diff --git a/server/src/test/java/org/opensearch/search/aggregations/startree/StarTreeFilterTests.java b/server/src/test/java/org/opensearch/search/aggregations/startree/StarTreeFilterTests.java index 009184d24817d..f8eb71a40319a 100644 --- a/server/src/test/java/org/opensearch/search/aggregations/startree/StarTreeFilterTests.java +++ b/server/src/test/java/org/opensearch/search/aggregations/startree/StarTreeFilterTests.java @@ -242,7 +242,7 @@ private long getDocCountFromStarTree(CompositeIndexReader starTreeDocValuesReade assert canAdvance : "Cannot advance to document ID " + bit + " in values iterator."; // Iterate over values for the current document ID - for (int i = 0, count = valuesIterator.valuesCount(); i < count; i++) { + for (int i = 0, count = valuesIterator.entryValueCount(); i < count; i++) { long value = valuesIterator.nextValue(); // Assert that the value is as expected using the provided consumer docCount += value; diff --git a/server/src/test/java/org/opensearch/search/aggregations/startree/StarTreeQueryContextTests.java b/server/src/test/java/org/opensearch/search/aggregations/startree/StarTreeQueryContextTests.java new file mode 100644 index 0000000000000..076473d2c6a2a --- /dev/null +++ b/server/src/test/java/org/opensearch/search/aggregations/startree/StarTreeQueryContextTests.java @@ -0,0 +1,127 @@ +/* + * 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.search.aggregations.startree; + +import org.apache.lucene.index.LeafReaderContext; +import org.apache.lucene.util.FixedBitSet; +import org.junit.Before; +import org.junit.Test; +import org.opensearch.index.codec.composite.CompositeIndexFieldInfo; +import org.opensearch.search.startree.StarTreeQueryContext; +import org.opensearch.test.OpenSearchTestCase; + +import java.util.HashMap; +import java.util.Map; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNotNull; +import static org.junit.Assert.assertNull; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.when; + +public class StarTreeQueryContextTests extends OpenSearchTestCase { + + private CompositeIndexFieldInfo mockStarTree; + private LeafReaderContext mockLeafReaderContext; + private StarTreeQueryContext context; + private Map queryMap; + + @Before + public void setUp() { + // Mock dependencies + mockStarTree = mock(CompositeIndexFieldInfo.class); + mockLeafReaderContext = mock(LeafReaderContext.class); + + // Prepare queryMap + queryMap = new HashMap<>(); + queryMap.put("field1", 123L); + queryMap.put("field2", 456L); + + // Simulate leaf reader context ord + when(mockLeafReaderContext.ord).thenReturn(1); + } + + + public void testConstructorWithValidNumSegmentsCache() { + // Initialize context with valid numSegmentsCache + int numSegmentsCache = 3; + context = new StarTreeQueryContext(mockStarTree, queryMap, numSegmentsCache); + + // Verify that the star tree and query map are set correctly + assertEquals(mockStarTree, context.getStarTree()); + assertEquals(queryMap, context.getQueryMap()); + + // Verify that the starTreeValues array is initialized correctly + assertNotNull(context.getStarTreeValues()); + assertEquals(numSegmentsCache, context.getStarTreeValues().length); + } + + + public void testConstructorWithNegativeNumSegmentsCache() { + // Initialize context with invalid numSegmentsCache (-1) + context = new StarTreeQueryContext(mockStarTree, queryMap, -1); + + // Verify that the starTreeValues array is not initialized + assertNull(context.getStarTreeValues()); + } + + + public void testGetStarTreeValues_WithValidContext() { + // Initialize context with a cache of size 3 + context = new StarTreeQueryContext(mockStarTree, queryMap, 3); + + // Create a FixedBitSet and assign it to the context + FixedBitSet fixedBitSet = new FixedBitSet(10); + context.setStarTreeValues(mockLeafReaderContext, fixedBitSet); + + // Retrieve the FixedBitSet for the given context + FixedBitSet result = context.getStarTreeValues(mockLeafReaderContext); + + // Verify that the correct FixedBitSet is returned + assertNotNull(result); + assertEquals(fixedBitSet, result); + } + + + public void testGetStarTreeValues_WithNullCache() { + // Initialize context with no cache (numSegmentsCache is -1) + context = new StarTreeQueryContext(mockStarTree, queryMap, -1); + + // Retrieve the FixedBitSet for the given context + FixedBitSet result = context.getStarTreeValues(mockLeafReaderContext); + + // Verify that the result is null since there's no cache + assertNull(result); + } + + public void testSetStarTreeValues() { + // Initialize context with a cache of size 3 + context = new StarTreeQueryContext(mockStarTree, queryMap, 3); + + // Create a FixedBitSet and assign it to the context + FixedBitSet fixedBitSet = new FixedBitSet(10); + context.setStarTreeValues(mockLeafReaderContext, fixedBitSet); + + // Retrieve the FixedBitSet for the given context and verify the update + FixedBitSet result = context.getStarTreeValues(mockLeafReaderContext); + assertEquals(fixedBitSet, result); + } + + public void testSetStarTreeValues_WithNullCache() { + // Initialize context with no cache (numSegmentsCache is -1) + context = new StarTreeQueryContext(mockStarTree, queryMap, -1); + + // Try setting a FixedBitSet and ensure no exception is thrown + FixedBitSet fixedBitSet = new FixedBitSet(10); + context.setStarTreeValues(mockLeafReaderContext, fixedBitSet); + + // Since the cache is null, getStarTreeValues should return null + assertNull(context.getStarTreeValues(mockLeafReaderContext)); + } +}