-
Notifications
You must be signed in to change notification settings - Fork 25.6k
Add half float mapping to the scripting fields API #82294
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
Merged
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
2c119f2
Add half float field to scripting fields API
jdconrad 41eb019
fix style
jdconrad 1fa1918
Update docs/changelog/82294.yaml
jdconrad edeeb16
response to pr comments
jdconrad 48355ef
Merge branch 'master' into halffloat
jdconrad 8d87f28
Merge branch 'master' into halffloat
jdconrad 97416fd
add close_to to replace match
jdconrad 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 hidden or 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: 82294 | ||
| summary: Add half float mapping to the scripting fields API | ||
| area: Infra/Scripting | ||
| type: enhancement | ||
| issues: [] |
This file contains hidden or 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 hidden or 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 hidden or 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
153 changes: 153 additions & 0 deletions
153
server/src/main/java/org/elasticsearch/script/field/HalfFloatDocValuesField.java
This file contains hidden or 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,153 @@ | ||
| /* | ||
| * 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 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 or the Server | ||
| * Side Public License, v 1. | ||
| */ | ||
|
|
||
| package org.elasticsearch.script.field; | ||
|
|
||
| import org.apache.lucene.util.ArrayUtil; | ||
| import org.elasticsearch.index.fielddata.ScriptDocValues; | ||
| import org.elasticsearch.index.fielddata.SortedNumericDoubleValues; | ||
|
|
||
| import java.io.IOException; | ||
| import java.util.ArrayList; | ||
| import java.util.Collections; | ||
| import java.util.Iterator; | ||
| import java.util.List; | ||
| import java.util.NoSuchElementException; | ||
|
|
||
| public class HalfFloatDocValuesField implements DocValuesField<Float>, ScriptDocValues.Supplier<Double> { | ||
|
|
||
| protected final SortedNumericDoubleValues input; | ||
| protected final String name; | ||
|
|
||
| protected double[] values = new double[0]; | ||
| protected int count; | ||
|
|
||
| private ScriptDocValues.Doubles doubles = null; | ||
|
|
||
| public HalfFloatDocValuesField(SortedNumericDoubleValues input, String name) { | ||
| this.input = input; | ||
| this.name = name; | ||
| } | ||
|
|
||
| @Override | ||
| public void setNextDocId(int docId) throws IOException { | ||
| if (input.advanceExact(docId)) { | ||
| resize(input.docValueCount()); | ||
| for (int i = 0; i < count; i++) { | ||
| values[i] = input.nextValue(); | ||
| } | ||
| } else { | ||
| resize(0); | ||
| } | ||
| } | ||
|
|
||
| protected void resize(int newSize) { | ||
| count = newSize; | ||
|
|
||
| assert count >= 0 : "size must be positive (got " + count + "): likely integer overflow?"; | ||
| values = ArrayUtil.grow(values, count); | ||
| } | ||
|
|
||
| @Override | ||
| public ScriptDocValues<Double> getScriptDocValues() { | ||
| if (doubles == null) { | ||
| doubles = new ScriptDocValues.Doubles(this); | ||
| } | ||
|
|
||
| return doubles; | ||
| } | ||
|
|
||
| @Override | ||
| public Double getInternal(int index) { | ||
| return values[index]; | ||
| } | ||
|
|
||
| @Override | ||
| public String getName() { | ||
| return name; | ||
| } | ||
|
|
||
| @Override | ||
| public boolean isEmpty() { | ||
| return count == 0; | ||
| } | ||
|
|
||
| @Override | ||
| public int size() { | ||
| return count; | ||
| } | ||
|
|
||
| /** | ||
| * Does a downcast for defaultValue from a double to a float | ||
| * to allow users to avoid explicit casting. | ||
| */ | ||
| public float get(double defaultValue) { | ||
| return get(0, defaultValue); | ||
| } | ||
|
|
||
| /** | ||
| * Does a downcast for defaultValue from a double to a float | ||
| * to allow users to avoid explicit casting. | ||
| */ | ||
| public float get(int index, double defaultValue) { | ||
| if (isEmpty() || index < 0 || index >= count) { | ||
| return (float) defaultValue; | ||
| } | ||
|
|
||
| return (float) values[index]; | ||
| } | ||
|
|
||
| @Override | ||
| public Iterator<Float> iterator() { | ||
| return new Iterator<Float>() { | ||
| private int index = 0; | ||
|
|
||
| @Override | ||
| public boolean hasNext() { | ||
| return index < count; | ||
| } | ||
|
|
||
| @Override | ||
| public Float next() { | ||
| if (hasNext() == false) { | ||
| throw new NoSuchElementException(); | ||
| } | ||
| return (float) values[index++]; | ||
| } | ||
| }; | ||
| } | ||
|
|
||
| /** Converts all the values to {@code Double} and returns them as a {@code List}. */ | ||
| public List<Double> asDoubles() { | ||
| if (isEmpty()) { | ||
| return Collections.emptyList(); | ||
| } | ||
|
|
||
| List<Double> doubleValues = new ArrayList<>(count); | ||
|
|
||
| for (int index = 0; index < count; ++index) { | ||
| doubleValues.add(values[index]); | ||
| } | ||
|
|
||
| return doubleValues; | ||
| } | ||
|
|
||
| /** Returns the 0th index value as a {@code double} if it exists, otherwise {@code defaultValue}. */ | ||
| public double asDouble(double defaultValue) { | ||
| return asDouble(0, defaultValue); | ||
| } | ||
|
|
||
| /** Returns the value at {@code index} as a {@code double} if it exists, otherwise {@code defaultValue}. */ | ||
| public double asDouble(int index, double defaultValue) { | ||
| if (isEmpty() || index < 0 || index >= count) { | ||
| return defaultValue; | ||
| } | ||
|
|
||
| return values[index]; | ||
| } | ||
| } |
This file contains hidden or 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
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.
Can you add a note explaining why this takes a double but returns a float.
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.
Added.