88package org .opensearch .index .compositeindex .datacube .startree .fileformats .node ;
99
1010import org .apache .lucene .store .RandomAccessInput ;
11+ import org .opensearch .index .compositeindex .datacube .DimensionDataType ;
1112import org .opensearch .index .compositeindex .datacube .startree .node .StarTreeNode ;
1213import org .opensearch .index .compositeindex .datacube .startree .node .StarTreeNodeType ;
1314import org .opensearch .search .startree .StarTreeNodeCollector ;
15+ import org .opensearch .search .startree .filter .provider .DimensionFilterMapper ;
1416
1517import java .io .IOException ;
1618import java .io .UncheckedIOException ;
19+ import java .util .Comparator ;
1720import java .util .Iterator ;
1821
1922/**
@@ -193,15 +196,23 @@ public StarTreeNode getChildStarNode() throws IOException {
193196 }
194197
195198 @ Override
196- public StarTreeNode getChildForDimensionValue (Long dimensionValue , StarTreeNode lastMatchedChild ) throws IOException {
199+ public StarTreeNode getChildForDimensionValue (
200+ Long dimensionValue ,
201+ StarTreeNode lastMatchedChild ,
202+ DimensionFilterMapper dimensionFilterMapper
203+ ) throws IOException {
197204 // there will be no children for leaf nodes
198205 if (isLeaf ()) {
199206 return null ;
200207 }
201208
202209 StarTreeNode resultStarTreeNode = null ;
203210 if (null != dimensionValue ) {
204- resultStarTreeNode = binarySearchChild (dimensionValue , lastMatchedChild );
211+ resultStarTreeNode = binarySearchChild (
212+ dimensionValue ,
213+ lastMatchedChild ,
214+ dimensionFilterMapper == null ? DimensionDataType .LONG ::compare : dimensionFilterMapper .comparator ()
215+ );
205216 }
206217 return resultStarTreeNode ;
207218 }
@@ -238,11 +249,13 @@ private static FixedLengthStarTreeNode matchStarTreeNodeTypeOrNull(FixedLengthSt
238249 * Performs a binary search to find a child node with the given dimension value.
239250 *
240251 * @param dimensionValue The dimension value to search for
252+ * @param lastMatchedNode : If not null, we begin the binary search from the node after this.
253+ * @param comparator : Comparator (LONG or UNSIGNED_LONG) to compare the dimension values
241254 * @return The child node if found, null otherwise
242255 * @throws IOException If there's an error reading from the input
243256 */
244- private FixedLengthStarTreeNode binarySearchChild (long dimensionValue , StarTreeNode lastMatchedNode ) throws IOException {
245-
257+ private FixedLengthStarTreeNode binarySearchChild (long dimensionValue , StarTreeNode lastMatchedNode , Comparator < Long > comparator )
258+ throws IOException {
246259 int low = firstChildId ;
247260
248261 int high = getInt (LAST_CHILD_ID_OFFSET );
@@ -268,10 +281,10 @@ private FixedLengthStarTreeNode binarySearchChild(long dimensionValue, StarTreeN
268281 int mid = low + (high - low ) / 2 ;
269282 FixedLengthStarTreeNode midNode = new FixedLengthStarTreeNode (in , mid );
270283 long midDimensionValue = midNode .getDimensionValue ();
271-
272- if (midDimensionValue == dimensionValue ) {
284+ int compare = comparator . compare ( midDimensionValue , dimensionValue );
285+ if (compare == 0 ) {
273286 return midNode ;
274- } else if (midDimensionValue < dimensionValue ) {
287+ } else if (compare < 0 ) {
275288 low = mid + 1 ;
276289 } else {
277290 high = mid - 1 ;
@@ -281,16 +294,19 @@ private FixedLengthStarTreeNode binarySearchChild(long dimensionValue, StarTreeN
281294 }
282295
283296 @ Override
284- public void collectChildrenInRange (long low , long high , StarTreeNodeCollector collector ) throws IOException {
285- if (low <= high ) {
286- FixedLengthStarTreeNode lowStarTreeNode = binarySearchChild (low , true , null );
297+ public void collectChildrenInRange (long low , long high , StarTreeNodeCollector collector , DimensionFilterMapper dimensionFilterMapper )
298+ throws IOException {
299+ Comparator <Long > comparator = dimensionFilterMapper .comparator ();
300+ if (comparator .compare (low , high ) <= 0 ) {
301+ FixedLengthStarTreeNode lowStarTreeNode = binarySearchChild (low , true , null , comparator );
287302 if (lowStarTreeNode != null ) {
288- FixedLengthStarTreeNode highStarTreeNode = binarySearchChild (high , false , lowStarTreeNode );
303+ FixedLengthStarTreeNode highStarTreeNode = binarySearchChild (high , false , lowStarTreeNode , comparator );
289304 if (highStarTreeNode != null ) {
290305 for (int lowNodeId = lowStarTreeNode .nodeId (); lowNodeId <= highStarTreeNode .nodeId (); ++lowNodeId ) {
291306 collector .collectStarTreeNode (new FixedLengthStarTreeNode (in , lowNodeId ));
292307 }
293- } else if (lowStarTreeNode .getDimensionValue () <= high ) { // Low StarTreeNode is the last default node for that dimension.
308+ } else if (comparator .compare (lowStarTreeNode .getDimensionValue (), high ) <= 0 ) { // Low StarTreeNode is the last default//
309+ // node for that dimension.
294310 collector .collectStarTreeNode (lowStarTreeNode );
295311 }
296312 }
@@ -302,11 +318,16 @@ public void collectChildrenInRange(long low, long high, StarTreeNodeCollector co
302318 * @param dimensionValue : The dimension to match.
303319 * @param matchNextHighest : If true then we try to return @dimensionValue or the next Highest. Else, we return @dimensionValue or the next Lowest.
304320 * @param lastMatchedNode : If not null, we begin the binary search from the node after this.
321+ * @param comparator : Comparator (LONG or UNSIGNED_LONG) to compare the dimension values
305322 * @return : Matched node or null.
306323 * @throws IOException :
307324 */
308- private FixedLengthStarTreeNode binarySearchChild (long dimensionValue , boolean matchNextHighest , StarTreeNode lastMatchedNode )
309- throws IOException {
325+ private FixedLengthStarTreeNode binarySearchChild (
326+ long dimensionValue ,
327+ boolean matchNextHighest ,
328+ StarTreeNode lastMatchedNode ,
329+ Comparator <Long > comparator
330+ ) throws IOException {
310331
311332 int low = firstChildId ;
312333 int tempLow = low ;
@@ -342,17 +363,18 @@ private FixedLengthStarTreeNode binarySearchChild(long dimensionValue, boolean m
342363 FixedLengthStarTreeNode midNode = new FixedLengthStarTreeNode (in , mid );
343364 long midDimensionValue = midNode .getDimensionValue ();
344365
345- if (midDimensionValue == dimensionValue ) {
366+ int compare = comparator .compare (midDimensionValue , dimensionValue );
367+ if (compare == 0 ) {
346368 return midNode ;
347369 } else {
348- if (midDimensionValue < dimensionValue ) { // Going to the right from mid to search next
370+ if (compare < 0 ) { // Going to the right from mid to search next
349371 tempLow = mid + 1 ;
350372 // We are going out of bounds for this dimension on the right side.
351373 if (tempLow > high || tempLow == nullNodeId ) {
352374 return matchNextHighest ? null : midNode ;
353375 } else {
354376 FixedLengthStarTreeNode nodeGreaterThanMid = new FixedLengthStarTreeNode (in , tempLow );
355- if (nodeGreaterThanMid .getDimensionValue () > dimensionValue ) {
377+ if (comparator . compare ( nodeGreaterThanMid .getDimensionValue (), dimensionValue ) > 0 ) {
356378 return matchNextHighest ? nodeGreaterThanMid : midNode ;
357379 }
358380 }
@@ -363,7 +385,7 @@ private FixedLengthStarTreeNode binarySearchChild(long dimensionValue, boolean m
363385 return matchNextHighest ? midNode : null ;
364386 } else {
365387 FixedLengthStarTreeNode nodeLessThanMid = new FixedLengthStarTreeNode (in , tempHigh );
366- if (nodeLessThanMid .getDimensionValue () < dimensionValue ) {
388+ if (comparator . compare ( nodeLessThanMid .getDimensionValue (), dimensionValue ) < 0 ) {
367389 return matchNextHighest ? midNode : nodeLessThanMid ;
368390 }
369391 }
0 commit comments