-
Notifications
You must be signed in to change notification settings - Fork 25.2k
Enable sort optimization on float and half_float #126342
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
Merged
mayya-sharipova
merged 8 commits into
elastic:main
from
mayya-sharipova:sort_other_numeric_types
Apr 9, 2025
Merged
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
452695b
Enable sort optimization on float and half_float
mayya-sharipova b9ff203
Merge branch 'main' into sort_other_numeric_types
mayya-sharipova e53fe87
Update docs/changelog/126342.yaml
mayya-sharipova 1cc73d2
Merge branch 'main' into sort_other_numeric_types
mayya-sharipova 9632d5e
Adjustments
mayya-sharipova ee021d7
Add more unit tests
mayya-sharipova 67f92b7
Merge remote-tracking branch 'upstream/main' into sort_other_numeric_…
mayya-sharipova 8fe6adf
Merge branch 'main' into sort_other_numeric_types
mayya-sharipova 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 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,5 @@ | ||
pr: 126342 | ||
summary: Enable sort optimization on float and `half_float` | ||
area: Search | ||
type: enhancement | ||
issues: [] |
321 changes: 230 additions & 91 deletions
321
server/src/internalClusterTest/java/org/elasticsearch/search/sort/FieldSortIT.java
Large diffs are not rendered by default.
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
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
116 changes: 116 additions & 0 deletions
116
.../src/main/java/org/elasticsearch/index/fielddata/fieldcomparator/HalfFloatComparator.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,116 @@ | ||
/* | ||
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one | ||
* or more contributor license agreements. Licensed under the "Elastic License | ||
* 2.0", the "GNU Affero General Public License v3.0 only", and the "Server Side | ||
* Public License v 1"; you may not use this file except in compliance with, at | ||
* your election, the "Elastic License 2.0", the "GNU Affero General Public | ||
* License v3.0 only", or the "Server Side Public License, v 1". | ||
*/ | ||
|
||
package org.elasticsearch.index.fielddata.fieldcomparator; | ||
|
||
import org.apache.lucene.index.LeafReaderContext; | ||
import org.apache.lucene.sandbox.document.HalfFloatPoint; | ||
import org.apache.lucene.search.LeafFieldComparator; | ||
import org.apache.lucene.search.Pruning; | ||
import org.apache.lucene.search.comparators.NumericComparator; | ||
import org.apache.lucene.util.BitUtil; | ||
|
||
import java.io.IOException; | ||
|
||
/** | ||
* Comparator for hal_float values. | ||
* This comparator provides a skipping functionality – an iterator that can skip over non-competitive documents. | ||
*/ | ||
public class HalfFloatComparator extends NumericComparator<Float> { | ||
private final float[] values; | ||
protected float topValue; | ||
protected float bottom; | ||
|
||
public HalfFloatComparator(int numHits, String field, Float missingValue, boolean reverse, Pruning pruning) { | ||
super(field, missingValue != null ? missingValue : 0.0f, reverse, pruning, HalfFloatPoint.BYTES); | ||
values = new float[numHits]; | ||
} | ||
|
||
@Override | ||
public int compare(int slot1, int slot2) { | ||
return Float.compare(values[slot1], values[slot2]); | ||
} | ||
|
||
@Override | ||
public void setTopValue(Float value) { | ||
super.setTopValue(value); | ||
topValue = value; | ||
} | ||
|
||
@Override | ||
public Float value(int slot) { | ||
return Float.valueOf(values[slot]); | ||
} | ||
|
||
@Override | ||
protected long missingValueAsComparableLong() { | ||
return HalfFloatPoint.halfFloatToSortableShort(missingValue); | ||
} | ||
|
||
@Override | ||
protected long sortableBytesToLong(byte[] bytes) { | ||
// Copied form HalfFloatPoint::sortableBytesToShort | ||
short x = (short) BitUtil.VH_BE_SHORT.get(bytes, 0); | ||
// Re-flip the sign bit to restore the original value: | ||
return (short) (x ^ 0x8000); | ||
} | ||
|
||
@Override | ||
public LeafFieldComparator getLeafComparator(LeafReaderContext context) throws IOException { | ||
return new HalfFloatLeafComparator(context); | ||
} | ||
|
||
/** Leaf comparator for {@link HalfFloatComparator} that provides skipping functionality */ | ||
public class HalfFloatLeafComparator extends NumericLeafComparator { | ||
|
||
public HalfFloatLeafComparator(LeafReaderContext context) throws IOException { | ||
super(context); | ||
} | ||
|
||
private float getValueForDoc(int doc) throws IOException { | ||
if (docValues.advanceExact(doc)) { | ||
return Float.intBitsToFloat((int) docValues.longValue()); | ||
} else { | ||
return missingValue; | ||
} | ||
} | ||
|
||
@Override | ||
public void setBottom(int slot) throws IOException { | ||
bottom = values[slot]; | ||
super.setBottom(slot); | ||
} | ||
|
||
@Override | ||
public int compareBottom(int doc) throws IOException { | ||
return Float.compare(bottom, getValueForDoc(doc)); | ||
} | ||
|
||
@Override | ||
public int compareTop(int doc) throws IOException { | ||
return Float.compare(topValue, getValueForDoc(doc)); | ||
} | ||
|
||
@Override | ||
public void copy(int slot, int doc) throws IOException { | ||
values[slot] = getValueForDoc(doc); | ||
super.copy(slot, doc); | ||
} | ||
|
||
@Override | ||
protected long bottomAsComparableLong() { | ||
return HalfFloatPoint.halfFloatToSortableShort(bottom); | ||
} | ||
|
||
@Override | ||
protected long topAsComparableLong() { | ||
return HalfFloatPoint.halfFloatToSortableShort(topValue); | ||
} | ||
} | ||
} |
54 changes: 54 additions & 0 deletions
54
...va/org/elasticsearch/index/fielddata/fieldcomparator/HalfFloatValuesComparatorSource.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,54 @@ | ||
/* | ||
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one | ||
* or more contributor license agreements. Licensed under the "Elastic License | ||
* 2.0", the "GNU Affero General Public License v3.0 only", and the "Server Side | ||
* Public License v 1"; you may not use this file except in compliance with, at | ||
* your election, the "Elastic License 2.0", the "GNU Affero General Public | ||
* License v3.0 only", or the "Server Side Public License, v 1". | ||
*/ | ||
package org.elasticsearch.index.fielddata.fieldcomparator; | ||
|
||
import org.apache.lucene.index.LeafReaderContext; | ||
import org.apache.lucene.index.NumericDocValues; | ||
import org.apache.lucene.search.FieldComparator; | ||
import org.apache.lucene.search.LeafFieldComparator; | ||
import org.apache.lucene.search.Pruning; | ||
import org.elasticsearch.core.Nullable; | ||
import org.elasticsearch.index.fielddata.IndexNumericFieldData; | ||
import org.elasticsearch.search.MultiValueMode; | ||
|
||
import java.io.IOException; | ||
|
||
/** | ||
* Comparator source for half_float values. | ||
*/ | ||
public class HalfFloatValuesComparatorSource extends FloatValuesComparatorSource { | ||
public HalfFloatValuesComparatorSource( | ||
IndexNumericFieldData indexFieldData, | ||
@Nullable Object missingValue, | ||
MultiValueMode sortMode, | ||
Nested nested | ||
) { | ||
super(indexFieldData, missingValue, sortMode, nested); | ||
} | ||
|
||
@Override | ||
public FieldComparator<?> newComparator(String fieldname, int numHits, Pruning enableSkipping, boolean reversed) { | ||
assert indexFieldData == null || fieldname.equals(indexFieldData.getFieldName()); | ||
|
||
final float fMissingValue = (Float) missingObject(missingValue, reversed); | ||
// NOTE: it's important to pass null as a missing value in the constructor so that | ||
// the comparator doesn't check docsWithField since we replace missing values in select() | ||
return new HalfFloatComparator(numHits, fieldname, null, reversed, enableSkipping) { | ||
@Override | ||
public LeafFieldComparator getLeafComparator(LeafReaderContext context) throws IOException { | ||
return new HalfFloatLeafComparator(context) { | ||
@Override | ||
protected NumericDocValues getNumericDocValues(LeafReaderContext context, String field) throws IOException { | ||
return HalfFloatValuesComparatorSource.this.getNumericDocValues(context, fMissingValue).getRawFloatValues(); | ||
} | ||
}; | ||
} | ||
}; | ||
} | ||
} |
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
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
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
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.
more a question than feedback. Since HALF_FLOAT is no longer handled by FloatValuesComparatorSource are there missing unit tests that are needed. I noticed FloatValuesComparatorSource is referenced by FloatNestedSortingTests. Does it make sense to have a HalfFloatNestedSortingTests?
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.
@john-wagster Thanks for the feedback, addressed in ee021d7