This is an automated email from the ASF dual-hosted git repository.
krisden pushed a commit to branch branch_9x
in repository https://gitbox.apache.org/repos/asf/solr.git
The following commit(s) were added to refs/heads/branch_9x by this push:
new 426c684bab1 SOLR-16646: New function query operator isnan to verify if
value is NaN (#1331)
426c684bab1 is described below
commit 426c684bab1a37c075849eeb1401a5628a00bc2b
Author: Gabriel Magno <[email protected]>
AuthorDate: Mon Feb 6 14:51:03 2023 -0300
SOLR-16646: New function query operator isnan to verify if value is NaN
(#1331)
Co-authored-by: Mikhail Khludnev <[email protected]>
---
solr/CHANGES.txt | 2 ++
.../org/apache/solr/search/ValueSourceParser.java | 20 ++++++++++++++++++++
.../org/apache/solr/search/QueryEqualityTest.java | 10 ++++++++++
.../solr/search/function/TestFunctionQuery.java | 15 +++++++++++++++
.../modules/query-guide/pages/function-queries.adoc | 10 ++++++++++
5 files changed, 57 insertions(+)
diff --git a/solr/CHANGES.txt b/solr/CHANGES.txt
index 56b31def973..e4a9218f0ab 100644
--- a/solr/CHANGES.txt
+++ b/solr/CHANGES.txt
@@ -23,6 +23,8 @@ New Features
* SOLR-16608: Ability to compress state.json in Zookeeper (Justin Sweeney via
noble)
+* SOLR-16646: New function query operator isnan to verify if value is NaN
(Gabriel Magno via Kevin Risden)
+
Improvements
---------------------
diff --git a/solr/core/src/java/org/apache/solr/search/ValueSourceParser.java
b/solr/core/src/java/org/apache/solr/search/ValueSourceParser.java
index 43600f9ac9e..c58b33836d4 100644
--- a/solr/core/src/java/org/apache/solr/search/ValueSourceParser.java
+++ b/solr/core/src/java/org/apache/solr/search/ValueSourceParser.java
@@ -953,6 +953,26 @@ public abstract class ValueSourceParser implements
NamedListInitializedPlugin {
}
});
+ addParser(
+ "isnan",
+ new ValueSourceParser() {
+ @Override
+ public ValueSource parse(FunctionQParser fp) throws SyntaxError {
+ ValueSource vs = fp.parseValueSource();
+ return new SimpleBoolFunction(vs) {
+ @Override
+ protected String name() {
+ return "isnan";
+ }
+
+ @Override
+ protected boolean func(int doc, FunctionValues vals) throws
IOException {
+ return Float.isNaN(vals.floatVal(doc));
+ }
+ };
+ }
+ });
+
addParser(
"not",
new ValueSourceParser() {
diff --git a/solr/core/src/test/org/apache/solr/search/QueryEqualityTest.java
b/solr/core/src/test/org/apache/solr/search/QueryEqualityTest.java
index c8536033550..c159ca94628 100644
--- a/solr/core/src/test/org/apache/solr/search/QueryEqualityTest.java
+++ b/solr/core/src/test/org/apache/solr/search/QueryEqualityTest.java
@@ -1069,6 +1069,16 @@ public class QueryEqualityTest extends SolrTestCaseJ4 {
}
}
+ public void testFuncIsnan() throws Exception {
+ SolrQueryRequest req = req("num", "12.3456", "zero", "0");
+ try {
+ assertFuncEquals(req, "isnan(12.3456)", "isnan(12.3456)", "isnan($num)");
+ assertFuncEquals(req, "isnan(div(0,0))", "isnan(div($zero,$zero))");
+ } finally {
+ req.close();
+ }
+ }
+
public void testFuncNot() throws Exception {
SolrQueryRequest req = req("myField", "field_b", "myTrue", "true");
try {
diff --git
a/solr/core/src/test/org/apache/solr/search/function/TestFunctionQuery.java
b/solr/core/src/test/org/apache/solr/search/function/TestFunctionQuery.java
index 56d94a5f2ab..0078ff9b01e 100644
--- a/solr/core/src/test/org/apache/solr/search/function/TestFunctionQuery.java
+++ b/solr/core/src/test/org/apache/solr/search/function/TestFunctionQuery.java
@@ -1170,6 +1170,21 @@ public class TestFunctionQuery extends SolrTestCaseJ4 {
req("q", "id:1", "fl", "t:not(testfunc(false)),f:not(true)"),
"/response/docs/[0]=={'t':true, 'f':false}");
+ // test isnan operator
+ assertJQ(
+ req(
+ "q",
+ "id:1",
+ "fl",
+ "f1:isnan(12.3456)",
+ "fl",
+ "f2:isnan(0)",
+ "fl",
+ "t1:isnan(div(0,0))",
+ "fl",
+ "t2:isnan(sqrt(-1))"),
+ "/response/docs/[0]=={'f1':false, 'f2':false, 't1':true, 't2':true}");
+
// test fields evaluated as booleans in wrapping functions
assertJQ(
req("q", "id:1", "fl", "a:not(foo_ti), b:if(foo_tf,'TT','FF'),
c:and(true,foo_tf)"),
diff --git
a/solr/solr-ref-guide/modules/query-guide/pages/function-queries.adoc
b/solr/solr-ref-guide/modules/query-guide/pages/function-queries.adoc
index be91507526f..8a81bddf8de 100644
--- a/solr/solr-ref-guide/modules/query-guide/pages/function-queries.adoc
+++ b/solr/solr-ref-guide/modules/query-guide/pages/function-queries.adoc
@@ -564,6 +564,16 @@ Returns `true` if any member of the field exists.
* `if(lt(ms(mydatefield),315569259747),0.8,1)` translates to this pseudocode:
`if mydatefield < 315569259747 then 0.8 else 1`
+=== isnan Function
+Returns `true` if the value is a float NaN (not a number).
+
+*Syntax Example*
+
+* `isnan(myfield)`: returns `true` for any document having a NaN stored in
"myfield".
+* `isnan(12.3456)`: returns `false`.
+* `isnan(0)`: returns `false`.
+* `isnan(div(0,0))`: returns `true` (since 0 divided by 0 is not defined and
returns a NaN).
+
== Example Function Queries
To give you a better understanding of how function queries can be used in
Solr, suppose an index stores the dimensions in meters x,y,z of some
hypothetical boxes with arbitrary names stored in field `boxname`.