Convert double script to return array#61504
Convert double script to return array#61504nik9000 merged 4 commits intoelastic:feature/runtime_fieldsfrom
Conversation
|
Pinging @elastic/es-search (:Search/Search) |
This replaces the value collection method in `double` valued runtime script fields with simply returning a `double[]`. Painless has some "convert" features that allow us to define automatic conversions from things like `double` and `Collection` into `double[]` so we use those.
| return null; | ||
| } | ||
| StringBuilder b = new StringBuilder(); | ||
| if ("double".equals(type)) { |
There was a problem hiding this comment.
The if is temporary and it'll become all the time once it is done.
| super(params, searchLookup, ctx); | ||
| } | ||
|
|
||
| public abstract void execute(); |
There was a problem hiding this comment.
Because I'm only doing one of them I have to move the old execute method decalarations into the subclasses.
| * Does the value match this query? | ||
| */ | ||
| protected abstract boolean matches(double[] values, int count); | ||
| protected abstract boolean matches(double[] values); |
There was a problem hiding this comment.
We don't need the count any more because we use the whole array.
javanna
left a comment
There was a problem hiding this comment.
left a couple of questions. Shall we have unit tests for the static conversion methods? LGTM otherwise.
| if (o instanceof Double) { | ||
| return convertFromDouble(((Double) o).doubleValue()); | ||
| } else { | ||
| return (double[]) o; |
There was a problem hiding this comment.
oh boy I was hoping we would not need this sort of stuff, but I guess we do? I mean the instanceof as well as the cast to double array
There was a problem hiding this comment.
😢 Me too! I imagine the painless folk are thinking about it, but I'm not sure. This seems like a perfect spot for invokedynamic, but I'm not an expert at all.
There was a problem hiding this comment.
what happens if we do without this conversion? Really bad I guess?
There was a problem hiding this comment.
@javanna without it you can't really return def. You get a lot of funny class cast exceptions.
@stu-elastic or @jdconrad do we have plans indy-ify this or something? So it'd call the converter based on the def type. Is that what #61389 is all about?
There was a problem hiding this comment.
We needed to get this in your hands ASAP. We'll be improving it: #61389
There was a problem hiding this comment.
Indy for this is really challenging. We are going to look into it, but allowing you to do def conversions this way ensures we have something for runtime fields now that doesn't use reflection invocation. I would recommend that this cover all numeric cases including byte through long as well. Check out something like DefMath.plus.
There was a problem hiding this comment.
Gotcha. So if I implement convertFromDef returning a def type is all on me at the moment. I have to do what DefMath does, basically.
There was a problem hiding this comment.
@nik9000 Yes, that's correct. Again we are going to rectify this, but wanted to make sure we had something that was usable now.
| # This import is required to make painless happy and it isn't 100% clear why | ||
| # This whitelist is required to allow painless to build the factory | ||
| class org.elasticsearch.xpack.runtimefields.DoubleScriptFieldScript$Factory @no_import { | ||
| } |
There was a problem hiding this comment.
I was hoping that this file would go away completely. can you remind what the remaining lines are for?
There was a problem hiding this comment.
I'll try one more time to drop it. I tried and it blew up. But I'll try without it entirely.
I think these are required so painless can use the classes that we defined. But these might should all be "implied" because they are part of the script context.
There was a problem hiding this comment.
I checked - it is still required.
There was a problem hiding this comment.
@stu-elastic and @jdconrad - I think we still need this file because without it painless can't find the factory class. I think that is because is part of a plugin and not in painless's classloader. Or something like that. Is that how the world is supposed to be? Could these classes be implied by default because the context mentions them directly?
There was a problem hiding this comment.
Painless has no way to load these classes w/o explicitly stating them as part of SPI.
There was a problem hiding this comment.
👍.
Could you dig them out of the ScriptContext, I wonder?
There was a problem hiding this comment.
That's something worth looking into. Agreed.
| */ | ||
| public final double[] values() { | ||
| return values; | ||
| public static double[] convertFromDouble(double v) { |
There was a problem hiding this comment.
@stu-elastic and @jdconrad do these look right? I borrowed them from FactoryTests.
I see right now you force the converters to be static - would it be possible to make them non-static on the script?
There was a problem hiding this comment.
Not easily because the only "this" pointer we have is through class bindings.
jdconrad
left a comment
There was a problem hiding this comment.
LGTM. Please address the one important note about convertFromDef.
| if (o instanceof Double) { | ||
| return convertFromDouble(((Double) o).doubleValue()); | ||
| } else { | ||
| return (double[]) o; |
There was a problem hiding this comment.
Indy for this is really challenging. We are going to look into it, but allowing you to do def conversions this way ensures we have something for runtime fields now that doesn't use reflection invocation. I would recommend that this cover all numeric cases including byte through long as well. Check out something like DefMath.plus.
| # This import is required to make painless happy and it isn't 100% clear why | ||
| # This whitelist is required to allow painless to build the factory | ||
| class org.elasticsearch.xpack.runtimefields.DoubleScriptFieldScript$Factory @no_import { | ||
| } |
There was a problem hiding this comment.
Painless has no way to load these classes w/o explicitly stating them as part of SPI.
|
Thanks all! |
This reverts commit 6d8170c. We've decided to stick with the `value` style functions for runtime fields.
This replaces the value collection method in
doublevalued runtimescript fields with simply returning a
double[]. Painless has some"convert" features that allow us to define automatic conversions from
things like
doubleandCollectionintodouble[]so we use those.Relates to #59332