LUCENE-10534: MinFloatFunction / MaxFloatFunction calls exists twice (#837)

This commit is contained in:
Kevin Risden 2022-05-02 13:13:45 -04:00 committed by GitHub
parent d9d2cb6f09
commit 7efac761f4
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 45 additions and 13 deletions

View File

@ -130,6 +130,8 @@ Optimizations
* LUCENE-10542: FieldSource exists implementations can avoid value retrieval (Kevin Risden)
* LUCENE-10534: MinFloatFunction / MaxFloatFunction exists check can be slow (Kevin Risden)
Bug Fixes
---------------------
* LUCENE-10477: Highlighter: WeightedSpanTermExtractor.extractWeightedSpanTerms to Query#rewrite

View File

@ -33,14 +33,17 @@ public class MaxFloatFunction extends MultiFloatFunction {
@Override
protected float func(int doc, FunctionValues[] valsArr) throws IOException {
if (!exists(doc, valsArr)) return 0.0f;
boolean noneFound = true;
float val = Float.NEGATIVE_INFINITY;
for (FunctionValues vals : valsArr) {
if (vals.exists(doc)) {
noneFound = false;
val = Math.max(vals.floatVal(doc), val);
}
}
if (noneFound) {
return 0.0f;
}
return val;
}

View File

@ -33,14 +33,17 @@ public class MinFloatFunction extends MultiFloatFunction {
@Override
protected float func(int doc, FunctionValues[] valsArr) throws IOException {
if (!exists(doc, valsArr)) return 0.0f;
boolean noneFound = true;
float val = Float.POSITIVE_INFINITY;
for (FunctionValues vals : valsArr) {
if (vals.exists(doc)) {
noneFound = false;
val = Math.min(vals.floatVal(doc), val);
}
}
if (noneFound) {
return 0.0f;
}
return val;
}

View File

@ -380,42 +380,66 @@ public class TestValueSources extends LuceneTestCase {
ValueSource vs =
new MaxFloatFunction(
new ValueSource[] {new ConstValueSource(1f), new ConstValueSource(2f)});
assertHits(new FunctionQuery(vs), new float[] {2f, 2f});
assertAllExist(vs);
assertHits(new FunctionQuery(vs), new float[] {2f, 2f});
// as long as one value exists, then max exists
vs = new MaxFloatFunction(new ValueSource[] {BOGUS_FLOAT_VS, new ConstValueSource(2F)});
vs = new MaxFloatFunction(new ValueSource[] {BOGUS_FLOAT_VS, new ConstValueSource(2f)});
assertAllExist(vs);
assertHits(new FunctionQuery(vs), new float[] {2f, 2f});
vs =
new MaxFloatFunction(
new ValueSource[] {BOGUS_FLOAT_VS, new ConstValueSource(2F), BOGUS_DOUBLE_VS});
new ValueSource[] {BOGUS_FLOAT_VS, new ConstValueSource(2f), BOGUS_DOUBLE_VS});
assertAllExist(vs);
assertHits(new FunctionQuery(vs), new float[] {2f, 2f});
// if none exist, then max doesn't exist
vs = new MaxFloatFunction(new ValueSource[] {BOGUS_FLOAT_VS, BOGUS_INT_VS, BOGUS_DOUBLE_VS});
assertNoneExist(vs);
assertHits(new FunctionQuery(vs), new float[] {0f, 0f});
// if no values exist should return 0f even if value returned is non zero
vs =
new MaxFloatFunction(
new ValueSource[] {
new SumFloatFunction(new ValueSource[] {BOGUS_FLOAT_VS, new ConstValueSource(42)})
});
assertNoneExist(vs);
assertHits(new FunctionQuery(vs), new float[] {0f, 0f});
}
public void testMinFloat() throws Exception {
ValueSource vs =
new MinFloatFunction(
new ValueSource[] {new ConstValueSource(1f), new ConstValueSource(2f)});
assertHits(new FunctionQuery(vs), new float[] {1f, 1f});
assertAllExist(vs);
assertHits(new FunctionQuery(vs), new float[] {1f, 1f});
// as long as one value exists, then min exists
vs = new MinFloatFunction(new ValueSource[] {BOGUS_FLOAT_VS, new ConstValueSource(2F)});
assertHits(new FunctionQuery(vs), new float[] {2F, 2F});
vs = new MinFloatFunction(new ValueSource[] {BOGUS_FLOAT_VS, new ConstValueSource(2f)});
assertAllExist(vs);
assertHits(new FunctionQuery(vs), new float[] {2f, 2f});
vs =
new MinFloatFunction(
new ValueSource[] {BOGUS_FLOAT_VS, new ConstValueSource(2F), BOGUS_DOUBLE_VS});
new ValueSource[] {BOGUS_FLOAT_VS, new ConstValueSource(2f), BOGUS_DOUBLE_VS});
assertAllExist(vs);
assertHits(new FunctionQuery(vs), new float[] {2f, 2f});
// if none exist, then min doesn't exist
vs = new MinFloatFunction(new ValueSource[] {BOGUS_FLOAT_VS, BOGUS_INT_VS, BOGUS_DOUBLE_VS});
assertNoneExist(vs);
assertHits(new FunctionQuery(vs), new float[] {0f, 0f});
// if no values exist should return 0f even if value returned is non zero
vs =
new MinFloatFunction(
new ValueSource[] {
new SumFloatFunction(new ValueSource[] {BOGUS_FLOAT_VS, new ConstValueSource(42)})
});
assertNoneExist(vs);
assertHits(new FunctionQuery(vs), new float[] {0f, 0f});
}
public void testNorm() throws Exception {