From 220556d434da77e128a0cb3390f383344f5da9ab Mon Sep 17 00:00:00 2001 From: Doug Turnbull Date: Tue, 5 Jul 2016 16:55:03 -0400 Subject: [PATCH 01/20] Add comparison functions for function queries --- .../valuesource/CompareNumericFunction.java | 131 ++++++++++++++++++ .../valuesource/GreaterThanEqualFunction.java | 37 +++++ .../valuesource/GreaterThanFunction.java | 40 ++++++ .../valuesource/LessThanEqualFunction.java | 39 ++++++ .../valuesource/LessThanFunction.java | 37 +++++ .../apache/solr/search/ValueSourceParser.java | 43 ++++++ .../search/function/TestFunctionQuery.java | 2 +- 7 files changed, 328 insertions(+), 1 deletion(-) create mode 100644 lucene/queries/src/java/org/apache/lucene/queries/function/valuesource/CompareNumericFunction.java create mode 100644 lucene/queries/src/java/org/apache/lucene/queries/function/valuesource/GreaterThanEqualFunction.java create mode 100644 lucene/queries/src/java/org/apache/lucene/queries/function/valuesource/GreaterThanFunction.java create mode 100644 lucene/queries/src/java/org/apache/lucene/queries/function/valuesource/LessThanEqualFunction.java create mode 100644 lucene/queries/src/java/org/apache/lucene/queries/function/valuesource/LessThanFunction.java diff --git a/lucene/queries/src/java/org/apache/lucene/queries/function/valuesource/CompareNumericFunction.java b/lucene/queries/src/java/org/apache/lucene/queries/function/valuesource/CompareNumericFunction.java new file mode 100644 index 000000000000..cef55e3aa352 --- /dev/null +++ b/lucene/queries/src/java/org/apache/lucene/queries/function/valuesource/CompareNumericFunction.java @@ -0,0 +1,131 @@ +package org.apache.lucene.queries.function.valuesource; + +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +import java.io.IOException; +import java.util.Map; + +import org.apache.lucene.index.LeafReaderContext; +import org.apache.lucene.queries.function.ValueSource; +import org.apache.lucene.queries.function.FunctionValues; +import org.apache.lucene.search.IndexSearcher; + + +/** + * Base class for comparison operators used within if statements + * To Solr's if function query a 0 is considered "false", all other values are "true" + */ +public abstract class CompareNumericFunction extends ValueSource { + + public final ValueSource lhs; + public final ValueSource rhs; + + public CompareNumericFunction(ValueSource lhs, ValueSource rhs) { + this.lhs = lhs; + this.rhs = rhs; + } + + // Perform the comparison, returning true or false + public abstract boolean compareNumeric(double lhs, double rhs); + + // Uniquely identify the operation (ie "gt", "lt" "gte, etc) + public abstract String getLabel(); + + // string comparison? Probably should be a seperate function + // public abstract boolean compareString(String lhs, String rhs); + + public FunctionValues getValues(Map context, LeafReaderContext readerContext) throws IOException { + final FunctionValues lhsVal = this.lhs.getValues(context, readerContext); + final FunctionValues rhsVal = this.rhs.getValues(context, readerContext); + final String compLabel = this.getLabel(); + + return new FunctionValues() { + @Override + public byte byteVal(int doc) { + + return boolVal(doc) ? (byte)1 : (byte)0; + } + + @Override + public short shortVal(int doc) { + return byteVal(doc); + } + + @Override + public float floatVal(int doc) { + return byteVal(doc); + } + + @Override + public int intVal(int doc) { + return byteVal(doc); + } + + @Override + public long longVal(int doc) { + return byteVal(doc); + } + + @Override + public double doubleVal(int doc) { + return byteVal(doc); + } + + + @Override + public boolean boolVal(int doc) { + return compareNumeric(lhsVal.floatVal(doc), rhsVal.floatVal(doc)); + } + + @Override + public String toString(int doc) { + return compLabel + "(" + lhsVal.toString(doc) + "," + rhsVal.toString(doc) + ")"; + } + }; + + + }; + + @Override + public boolean equals(Object o) { + if (!(o instanceof CompareNumericFunction)) return false; + CompareNumericFunction other = (CompareNumericFunction)o; + return getLabel().equals(other.getLabel()) + && lhs.equals(other.lhs) + && rhs.equals(other.rhs); } + + @Override + public int hashCode() { + int h = getLabel().hashCode(); + h = h * 31 + lhs.hashCode(); + h = h * 31 + rhs.hashCode(); + return h; + } + + @Override + public String description() { + return getLabel() + "(" + lhs.description() + "," + rhs.description() + ")"; + } + + @Override + public void createWeight(Map context, IndexSearcher searcher) throws IOException { + lhs.createWeight(context, searcher); + rhs.createWeight(context, searcher); + } + +} diff --git a/lucene/queries/src/java/org/apache/lucene/queries/function/valuesource/GreaterThanEqualFunction.java b/lucene/queries/src/java/org/apache/lucene/queries/function/valuesource/GreaterThanEqualFunction.java new file mode 100644 index 000000000000..71cf74cd0026 --- /dev/null +++ b/lucene/queries/src/java/org/apache/lucene/queries/function/valuesource/GreaterThanEqualFunction.java @@ -0,0 +1,37 @@ +package org.apache.lucene.queries.function.valuesource; + +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +import org.apache.lucene.queries.function.ValueSource; + +public class GreaterThanEqualFunction extends CompareNumericFunction { + + public GreaterThanEqualFunction(ValueSource lhs, ValueSource rhs) { + super(lhs, rhs); + } + + @Override + public boolean compareNumeric(double lhs, double rhs) { + return lhs >= rhs; + } + + @Override + public String getLabel() { + return "gte"; + } +} diff --git a/lucene/queries/src/java/org/apache/lucene/queries/function/valuesource/GreaterThanFunction.java b/lucene/queries/src/java/org/apache/lucene/queries/function/valuesource/GreaterThanFunction.java new file mode 100644 index 000000000000..260f1e39f405 --- /dev/null +++ b/lucene/queries/src/java/org/apache/lucene/queries/function/valuesource/GreaterThanFunction.java @@ -0,0 +1,40 @@ +package org.apache.lucene.queries.function.valuesource; + +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +import org.apache.lucene.queries.function.ValueSource; + +/** + * Created by doug on 7/5/16. + */ +public class GreaterThanFunction extends CompareNumericFunction { + + public GreaterThanFunction(ValueSource lhs, ValueSource rhs) { + super(lhs, rhs); + } + + @Override + public boolean compareNumeric(double lhs, double rhs) { + return lhs > rhs; + } + + @Override + public String getLabel() { + return "gt"; + } +} diff --git a/lucene/queries/src/java/org/apache/lucene/queries/function/valuesource/LessThanEqualFunction.java b/lucene/queries/src/java/org/apache/lucene/queries/function/valuesource/LessThanEqualFunction.java new file mode 100644 index 000000000000..bff5dc13c7f7 --- /dev/null +++ b/lucene/queries/src/java/org/apache/lucene/queries/function/valuesource/LessThanEqualFunction.java @@ -0,0 +1,39 @@ +package org.apache.lucene.queries.function.valuesource; + +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +import org.apache.lucene.queries.function.ValueSource; + +/** + * Created by doug on 7/5/16. + */ +public class LessThanEqualFunction extends CompareNumericFunction { + public LessThanEqualFunction(ValueSource lhs, ValueSource rhs) { + super(lhs, rhs); + } + + @Override + public boolean compareNumeric(double lhs, double rhs) { + return lhs <= rhs; + } + + @Override + public String getLabel() { + return "lte"; + } +} diff --git a/lucene/queries/src/java/org/apache/lucene/queries/function/valuesource/LessThanFunction.java b/lucene/queries/src/java/org/apache/lucene/queries/function/valuesource/LessThanFunction.java new file mode 100644 index 000000000000..efe61d8a8c87 --- /dev/null +++ b/lucene/queries/src/java/org/apache/lucene/queries/function/valuesource/LessThanFunction.java @@ -0,0 +1,37 @@ +package org.apache.lucene.queries.function.valuesource; + +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +import org.apache.lucene.queries.function.ValueSource; + +public class LessThanFunction extends CompareNumericFunction { + + public LessThanFunction(ValueSource lhs, ValueSource rhs) { + super(lhs, rhs); + } + + @Override + public boolean compareNumeric(double lhs, double rhs) { + return lhs < rhs; + } + + @Override + public String getLabel() { + return "lt"; + } +} 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 65a4d0d1c46f..b4cb3aaf9949 100644 --- a/solr/core/src/java/org/apache/solr/search/ValueSourceParser.java +++ b/solr/core/src/java/org/apache/solr/search/ValueSourceParser.java @@ -822,6 +822,49 @@ public ValueSource parse(FunctionQParser fp) throws SyntaxError { } }); + addParser("gt", new ValueSourceParser() { + @Override + public ValueSource parse(FunctionQParser fp) throws SyntaxError { + ValueSource lhs = fp.parseValueSource(); + ValueSource rhs = fp.parseValueSource(); + + return new GreaterThanFunction(lhs, rhs); + } + }); + + addParser("lt", new ValueSourceParser() { + @Override + public ValueSource parse(FunctionQParser fp) throws SyntaxError { + ValueSource lhs = fp.parseValueSource(); + ValueSource rhs = fp.parseValueSource(); + + return new LessThanFunction(lhs, rhs); + } + }); + + addParser("gte", new ValueSourceParser() { + @Override + public ValueSource parse(FunctionQParser fp) throws SyntaxError { + ValueSource lhs = fp.parseValueSource(); + ValueSource rhs = fp.parseValueSource(); + + return new GreaterThanEqualFunction(lhs, rhs); + } + }); + + + addParser("lte", new ValueSourceParser() { + @Override + public ValueSource parse(FunctionQParser fp) throws SyntaxError { + ValueSource lhs = fp.parseValueSource(); + ValueSource rhs = fp.parseValueSource(); + + return new LessThanEqualFunction(lhs, rhs); + } + }); + + + addParser("def", new ValueSourceParser() { @Override public ValueSource parse(FunctionQParser fp) throws SyntaxError { 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 f94a9ee17394..7fae07e52d4c 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 @@ -98,7 +98,7 @@ public String func(String field, String template) { return sb.toString(); } - void singleTest(String field, String funcTemplate, List args, float... results) { + protected void singleTest(String field, String funcTemplate, List args, float... results) { String parseableQuery = func(field, funcTemplate); List nargs = new ArrayList<>(Arrays.asList("q", parseableQuery From 57b09cce4b1fb8ae022d7a8037c6fe447c71d7f3 Mon Sep 17 00:00:00 2001 From: Doug Turnbull Date: Tue, 5 Jul 2016 20:43:33 -0400 Subject: [PATCH 02/20] Adds eq operator for numerical equivelance --- .../function/valuesource/EqualFunction.java | 36 +++++++++++++++++++ .../apache/solr/search/ValueSourceParser.java | 10 ++++++ 2 files changed, 46 insertions(+) create mode 100644 lucene/queries/src/java/org/apache/lucene/queries/function/valuesource/EqualFunction.java diff --git a/lucene/queries/src/java/org/apache/lucene/queries/function/valuesource/EqualFunction.java b/lucene/queries/src/java/org/apache/lucene/queries/function/valuesource/EqualFunction.java new file mode 100644 index 000000000000..5ba93f05a45c --- /dev/null +++ b/lucene/queries/src/java/org/apache/lucene/queries/function/valuesource/EqualFunction.java @@ -0,0 +1,36 @@ +package org.apache.lucene.queries.function.valuesource; + +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +import org.apache.lucene.queries.function.ValueSource; + +public class EqualFunction extends CompareNumericFunction { + public EqualFunction(ValueSource lhs, ValueSource rhs) { + super(lhs, rhs); + } + + @Override + public boolean compareNumeric(double lhs, double rhs) { + return lhs == rhs; + } + + @Override + public String getLabel() { + return "eq"; + } +} 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 b4cb3aaf9949..017877c26871 100644 --- a/solr/core/src/java/org/apache/solr/search/ValueSourceParser.java +++ b/solr/core/src/java/org/apache/solr/search/ValueSourceParser.java @@ -863,6 +863,16 @@ public ValueSource parse(FunctionQParser fp) throws SyntaxError { } }); + addParser("eq", new ValueSourceParser() { + @Override + public ValueSource parse(FunctionQParser fp) throws SyntaxError { + ValueSource lhs = fp.parseValueSource(); + ValueSource rhs = fp.parseValueSource(); + + return new EqualFunction(lhs, rhs); + } + }); + addParser("def", new ValueSourceParser() { From d5493eec5e830583e14881f6dd2c41a83fd8d1e0 Mon Sep 17 00:00:00 2001 From: Doug Turnbull Date: Tue, 5 Jul 2016 20:44:38 -0400 Subject: [PATCH 03/20] Adds tests for comparison function --- .../search/function/TestFunctionQuery.java | 41 +++++++++++++++++++ 1 file changed, 41 insertions(+) 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 7fae07e52d4c..7889adb18907 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 @@ -793,4 +793,45 @@ public void testMissingFieldFunctionBehavior() throws Exception { } } + @Test + public void testNumericComparisons() { + assertU(adoc("id", "1", "age_i", "35")); + assertU(adoc("id", "2", "age_i", "25")); + assertU(commit()); + + singleTest("age_i", "if(gt(age_i,30),5,2)", + /*id*/1, /*score*/5, + /*id*/2, /*score*/2); + + singleTest("age_i", "if(lt(age_i,30),5,2)", + /*id*/1, /*score*/2, + /*id*/2, /*score*/5); + + singleTest("age_i", "if(lte(age_i,35),5,2)", + /*id*/1, /*score*/5, + /*id*/2, /*score*/5); + + singleTest("age_i", "if(gte(age_i,25),5,2)", + /*id*/1, /*score*/5, + /*id*/2, /*score*/5); + + singleTest("age_i", "if(lte(age_i,25),5,2)", + /*id*/1, /*score*/2, + /*id*/2, /*score*/5); + + singleTest("age_i", "if(gte(age_i,35),5,2)", + /*id*/1, /*score*/5, + /*id*/2, /*score*/2); + + + singleTest("age_i", "if(eq(age_i,30),5,2)", + /*id*/1, /*score*/2, + /*id*/2, /*score*/2); + + singleTest("age_i", "if(eq(age_i,35),5,2)", + /*id*/1, /*score*/5, + /*id*/2, /*score*/2); + + } + } From 0fec7e5767b1fa99891bce41a7148b45f7bbb687 Mon Sep 17 00:00:00 2001 From: Doug Turnbull Date: Wed, 6 Jul 2016 08:59:35 -0400 Subject: [PATCH 04/20] Changes to BoolFunction; makes members private --- .../function/valuesource/CompareNumericFunction.java | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/lucene/queries/src/java/org/apache/lucene/queries/function/valuesource/CompareNumericFunction.java b/lucene/queries/src/java/org/apache/lucene/queries/function/valuesource/CompareNumericFunction.java index cef55e3aa352..822b651e2163 100644 --- a/lucene/queries/src/java/org/apache/lucene/queries/function/valuesource/CompareNumericFunction.java +++ b/lucene/queries/src/java/org/apache/lucene/queries/function/valuesource/CompareNumericFunction.java @@ -30,10 +30,10 @@ * Base class for comparison operators used within if statements * To Solr's if function query a 0 is considered "false", all other values are "true" */ -public abstract class CompareNumericFunction extends ValueSource { +public abstract class CompareNumericFunction extends BoolFunction { - public final ValueSource lhs; - public final ValueSource rhs; + private final ValueSource lhs; + private final ValueSource rhs; public CompareNumericFunction(ValueSource lhs, ValueSource rhs) { this.lhs = lhs; From 3d45cd8b6fa662d9213cb57fbf35bba958662b53 Mon Sep 17 00:00:00 2001 From: Doug Turnbull Date: Wed, 6 Jul 2016 20:07:26 -0400 Subject: [PATCH 05/20] Removes noisy child classes for gt, gte etc Previously we had implemented an abstract class. We decided it would be cleaner to pass args into a constructor and create simpler anonymous classes. This mirrors the functionality of and, or, etc --- .../valuesource/CompareNumericFunction.java | 12 ++-- .../function/valuesource/EqualFunction.java | 36 ------------ .../valuesource/GreaterThanEqualFunction.java | 37 ------------- .../valuesource/GreaterThanFunction.java | 40 -------------- .../valuesource/LessThanEqualFunction.java | 39 ------------- .../valuesource/LessThanFunction.java | 37 ------------- .../apache/solr/search/ValueSourceParser.java | 55 ++++++++++++++----- 7 files changed, 48 insertions(+), 208 deletions(-) delete mode 100644 lucene/queries/src/java/org/apache/lucene/queries/function/valuesource/EqualFunction.java delete mode 100644 lucene/queries/src/java/org/apache/lucene/queries/function/valuesource/GreaterThanEqualFunction.java delete mode 100644 lucene/queries/src/java/org/apache/lucene/queries/function/valuesource/GreaterThanFunction.java delete mode 100644 lucene/queries/src/java/org/apache/lucene/queries/function/valuesource/LessThanEqualFunction.java delete mode 100644 lucene/queries/src/java/org/apache/lucene/queries/function/valuesource/LessThanFunction.java diff --git a/lucene/queries/src/java/org/apache/lucene/queries/function/valuesource/CompareNumericFunction.java b/lucene/queries/src/java/org/apache/lucene/queries/function/valuesource/CompareNumericFunction.java index 822b651e2163..e2f57df0b887 100644 --- a/lucene/queries/src/java/org/apache/lucene/queries/function/valuesource/CompareNumericFunction.java +++ b/lucene/queries/src/java/org/apache/lucene/queries/function/valuesource/CompareNumericFunction.java @@ -34,17 +34,21 @@ public abstract class CompareNumericFunction extends BoolFunction { private final ValueSource lhs; private final ValueSource rhs; + private final String label; - public CompareNumericFunction(ValueSource lhs, ValueSource rhs) { + public CompareNumericFunction(ValueSource lhs, ValueSource rhs, String label) { this.lhs = lhs; this.rhs = rhs; + this.label = label; } // Perform the comparison, returning true or false - public abstract boolean compareNumeric(double lhs, double rhs); + public abstract boolean compare(double lhs, double rhs); // Uniquely identify the operation (ie "gt", "lt" "gte, etc) - public abstract String getLabel(); + public String getLabel() { + return this.label; + } // string comparison? Probably should be a seperate function // public abstract boolean compareString(String lhs, String rhs); @@ -89,7 +93,7 @@ public double doubleVal(int doc) { @Override public boolean boolVal(int doc) { - return compareNumeric(lhsVal.floatVal(doc), rhsVal.floatVal(doc)); + return compare(lhsVal.floatVal(doc), rhsVal.floatVal(doc)); } @Override diff --git a/lucene/queries/src/java/org/apache/lucene/queries/function/valuesource/EqualFunction.java b/lucene/queries/src/java/org/apache/lucene/queries/function/valuesource/EqualFunction.java deleted file mode 100644 index 5ba93f05a45c..000000000000 --- a/lucene/queries/src/java/org/apache/lucene/queries/function/valuesource/EqualFunction.java +++ /dev/null @@ -1,36 +0,0 @@ -package org.apache.lucene.queries.function.valuesource; - -/* - * Licensed to the Apache Software Foundation (ASF) under one or more - * contributor license agreements. See the NOTICE file distributed with - * this work for additional information regarding copyright ownership. - * The ASF licenses this file to You under the Apache License, Version 2.0 - * (the "License"); you may not use this file except in compliance with - * the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -import org.apache.lucene.queries.function.ValueSource; - -public class EqualFunction extends CompareNumericFunction { - public EqualFunction(ValueSource lhs, ValueSource rhs) { - super(lhs, rhs); - } - - @Override - public boolean compareNumeric(double lhs, double rhs) { - return lhs == rhs; - } - - @Override - public String getLabel() { - return "eq"; - } -} diff --git a/lucene/queries/src/java/org/apache/lucene/queries/function/valuesource/GreaterThanEqualFunction.java b/lucene/queries/src/java/org/apache/lucene/queries/function/valuesource/GreaterThanEqualFunction.java deleted file mode 100644 index 71cf74cd0026..000000000000 --- a/lucene/queries/src/java/org/apache/lucene/queries/function/valuesource/GreaterThanEqualFunction.java +++ /dev/null @@ -1,37 +0,0 @@ -package org.apache.lucene.queries.function.valuesource; - -/* - * Licensed to the Apache Software Foundation (ASF) under one or more - * contributor license agreements. See the NOTICE file distributed with - * this work for additional information regarding copyright ownership. - * The ASF licenses this file to You under the Apache License, Version 2.0 - * (the "License"); you may not use this file except in compliance with - * the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -import org.apache.lucene.queries.function.ValueSource; - -public class GreaterThanEqualFunction extends CompareNumericFunction { - - public GreaterThanEqualFunction(ValueSource lhs, ValueSource rhs) { - super(lhs, rhs); - } - - @Override - public boolean compareNumeric(double lhs, double rhs) { - return lhs >= rhs; - } - - @Override - public String getLabel() { - return "gte"; - } -} diff --git a/lucene/queries/src/java/org/apache/lucene/queries/function/valuesource/GreaterThanFunction.java b/lucene/queries/src/java/org/apache/lucene/queries/function/valuesource/GreaterThanFunction.java deleted file mode 100644 index 260f1e39f405..000000000000 --- a/lucene/queries/src/java/org/apache/lucene/queries/function/valuesource/GreaterThanFunction.java +++ /dev/null @@ -1,40 +0,0 @@ -package org.apache.lucene.queries.function.valuesource; - -/* - * Licensed to the Apache Software Foundation (ASF) under one or more - * contributor license agreements. See the NOTICE file distributed with - * this work for additional information regarding copyright ownership. - * The ASF licenses this file to You under the Apache License, Version 2.0 - * (the "License"); you may not use this file except in compliance with - * the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -import org.apache.lucene.queries.function.ValueSource; - -/** - * Created by doug on 7/5/16. - */ -public class GreaterThanFunction extends CompareNumericFunction { - - public GreaterThanFunction(ValueSource lhs, ValueSource rhs) { - super(lhs, rhs); - } - - @Override - public boolean compareNumeric(double lhs, double rhs) { - return lhs > rhs; - } - - @Override - public String getLabel() { - return "gt"; - } -} diff --git a/lucene/queries/src/java/org/apache/lucene/queries/function/valuesource/LessThanEqualFunction.java b/lucene/queries/src/java/org/apache/lucene/queries/function/valuesource/LessThanEqualFunction.java deleted file mode 100644 index bff5dc13c7f7..000000000000 --- a/lucene/queries/src/java/org/apache/lucene/queries/function/valuesource/LessThanEqualFunction.java +++ /dev/null @@ -1,39 +0,0 @@ -package org.apache.lucene.queries.function.valuesource; - -/* - * Licensed to the Apache Software Foundation (ASF) under one or more - * contributor license agreements. See the NOTICE file distributed with - * this work for additional information regarding copyright ownership. - * The ASF licenses this file to You under the Apache License, Version 2.0 - * (the "License"); you may not use this file except in compliance with - * the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -import org.apache.lucene.queries.function.ValueSource; - -/** - * Created by doug on 7/5/16. - */ -public class LessThanEqualFunction extends CompareNumericFunction { - public LessThanEqualFunction(ValueSource lhs, ValueSource rhs) { - super(lhs, rhs); - } - - @Override - public boolean compareNumeric(double lhs, double rhs) { - return lhs <= rhs; - } - - @Override - public String getLabel() { - return "lte"; - } -} diff --git a/lucene/queries/src/java/org/apache/lucene/queries/function/valuesource/LessThanFunction.java b/lucene/queries/src/java/org/apache/lucene/queries/function/valuesource/LessThanFunction.java deleted file mode 100644 index efe61d8a8c87..000000000000 --- a/lucene/queries/src/java/org/apache/lucene/queries/function/valuesource/LessThanFunction.java +++ /dev/null @@ -1,37 +0,0 @@ -package org.apache.lucene.queries.function.valuesource; - -/* - * Licensed to the Apache Software Foundation (ASF) under one or more - * contributor license agreements. See the NOTICE file distributed with - * this work for additional information regarding copyright ownership. - * The ASF licenses this file to You under the Apache License, Version 2.0 - * (the "License"); you may not use this file except in compliance with - * the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -import org.apache.lucene.queries.function.ValueSource; - -public class LessThanFunction extends CompareNumericFunction { - - public LessThanFunction(ValueSource lhs, ValueSource rhs) { - super(lhs, rhs); - } - - @Override - public boolean compareNumeric(double lhs, double rhs) { - return lhs < rhs; - } - - @Override - public String getLabel() { - return "lt"; - } -} 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 017877c26871..386862cc7fb2 100644 --- a/solr/core/src/java/org/apache/solr/search/ValueSourceParser.java +++ b/solr/core/src/java/org/apache/solr/search/ValueSourceParser.java @@ -825,30 +825,45 @@ public ValueSource parse(FunctionQParser fp) throws SyntaxError { addParser("gt", new ValueSourceParser() { @Override public ValueSource parse(FunctionQParser fp) throws SyntaxError { - ValueSource lhs = fp.parseValueSource(); - ValueSource rhs = fp.parseValueSource(); + ValueSource lhsValSource = fp.parseValueSource(); + ValueSource rhsValSource = fp.parseValueSource(); - return new GreaterThanFunction(lhs, rhs); + return new ComparisonPredicate(lhsValSource, rhsValSource, "gt") { + @Override + public boolean compare(double lhs, double rhs) { + return lhs > rhs; + } + }; } }); addParser("lt", new ValueSourceParser() { @Override public ValueSource parse(FunctionQParser fp) throws SyntaxError { - ValueSource lhs = fp.parseValueSource(); - ValueSource rhs = fp.parseValueSource(); + ValueSource lhsValSource = fp.parseValueSource(); + ValueSource rhsValSource = fp.parseValueSource(); - return new LessThanFunction(lhs, rhs); + return new ComparisonPredicate(lhsValSource, rhsValSource, "lt") { + @Override + public boolean compare(double lhs, double rhs) { + return lhs < rhs; + } + }; } }); addParser("gte", new ValueSourceParser() { @Override public ValueSource parse(FunctionQParser fp) throws SyntaxError { - ValueSource lhs = fp.parseValueSource(); - ValueSource rhs = fp.parseValueSource(); + ValueSource lhsValSource = fp.parseValueSource(); + ValueSource rhsValSource = fp.parseValueSource(); - return new GreaterThanEqualFunction(lhs, rhs); + return new ComparisonPredicate(lhsValSource, rhsValSource, "gte") { + @Override + public boolean compare(double lhs, double rhs) { + return lhs >= rhs; + } + }; } }); @@ -856,20 +871,30 @@ public ValueSource parse(FunctionQParser fp) throws SyntaxError { addParser("lte", new ValueSourceParser() { @Override public ValueSource parse(FunctionQParser fp) throws SyntaxError { - ValueSource lhs = fp.parseValueSource(); - ValueSource rhs = fp.parseValueSource(); + ValueSource lhsValSource = fp.parseValueSource(); + ValueSource rhsValSource = fp.parseValueSource(); - return new LessThanEqualFunction(lhs, rhs); + return new ComparisonPredicate(lhsValSource, rhsValSource, "lte") { + @Override + public boolean compare(double lhs, double rhs) { + return lhs <= rhs; + } + }; } }); addParser("eq", new ValueSourceParser() { @Override public ValueSource parse(FunctionQParser fp) throws SyntaxError { - ValueSource lhs = fp.parseValueSource(); - ValueSource rhs = fp.parseValueSource(); + ValueSource lhsValSource = fp.parseValueSource(); + ValueSource rhsValSource = fp.parseValueSource(); - return new EqualFunction(lhs, rhs); + return new ComparisonPredicate(lhsValSource, rhsValSource, "eq") { + @Override + public boolean compare(double lhs, double rhs) { + return lhs == rhs; + } + }; } }); From 218b986ca0fa395b67817c858b6020160c8b5a7b Mon Sep 17 00:00:00 2001 From: Doug Turnbull Date: Wed, 6 Jul 2016 20:07:26 -0400 Subject: [PATCH 06/20] Removes noisy child classes for gt, gte etc Previously we had implemented an abstract class. We decided it would be cleaner to pass args into a constructor and create simpler anonymous classes. This mirrors the functionality of and, or, etc --- .../valuesource/CompareNumericFunction.java | 12 ++-- .../function/valuesource/EqualFunction.java | 36 ------------ .../valuesource/GreaterThanEqualFunction.java | 37 ------------- .../valuesource/GreaterThanFunction.java | 40 -------------- .../valuesource/LessThanEqualFunction.java | 39 ------------- .../valuesource/LessThanFunction.java | 37 ------------- .../apache/solr/search/ValueSourceParser.java | 55 ++++++++++++++----- 7 files changed, 48 insertions(+), 208 deletions(-) delete mode 100644 lucene/queries/src/java/org/apache/lucene/queries/function/valuesource/EqualFunction.java delete mode 100644 lucene/queries/src/java/org/apache/lucene/queries/function/valuesource/GreaterThanEqualFunction.java delete mode 100644 lucene/queries/src/java/org/apache/lucene/queries/function/valuesource/GreaterThanFunction.java delete mode 100644 lucene/queries/src/java/org/apache/lucene/queries/function/valuesource/LessThanEqualFunction.java delete mode 100644 lucene/queries/src/java/org/apache/lucene/queries/function/valuesource/LessThanFunction.java diff --git a/lucene/queries/src/java/org/apache/lucene/queries/function/valuesource/CompareNumericFunction.java b/lucene/queries/src/java/org/apache/lucene/queries/function/valuesource/CompareNumericFunction.java index 822b651e2163..e2f57df0b887 100644 --- a/lucene/queries/src/java/org/apache/lucene/queries/function/valuesource/CompareNumericFunction.java +++ b/lucene/queries/src/java/org/apache/lucene/queries/function/valuesource/CompareNumericFunction.java @@ -34,17 +34,21 @@ public abstract class CompareNumericFunction extends BoolFunction { private final ValueSource lhs; private final ValueSource rhs; + private final String label; - public CompareNumericFunction(ValueSource lhs, ValueSource rhs) { + public CompareNumericFunction(ValueSource lhs, ValueSource rhs, String label) { this.lhs = lhs; this.rhs = rhs; + this.label = label; } // Perform the comparison, returning true or false - public abstract boolean compareNumeric(double lhs, double rhs); + public abstract boolean compare(double lhs, double rhs); // Uniquely identify the operation (ie "gt", "lt" "gte, etc) - public abstract String getLabel(); + public String getLabel() { + return this.label; + } // string comparison? Probably should be a seperate function // public abstract boolean compareString(String lhs, String rhs); @@ -89,7 +93,7 @@ public double doubleVal(int doc) { @Override public boolean boolVal(int doc) { - return compareNumeric(lhsVal.floatVal(doc), rhsVal.floatVal(doc)); + return compare(lhsVal.floatVal(doc), rhsVal.floatVal(doc)); } @Override diff --git a/lucene/queries/src/java/org/apache/lucene/queries/function/valuesource/EqualFunction.java b/lucene/queries/src/java/org/apache/lucene/queries/function/valuesource/EqualFunction.java deleted file mode 100644 index 5ba93f05a45c..000000000000 --- a/lucene/queries/src/java/org/apache/lucene/queries/function/valuesource/EqualFunction.java +++ /dev/null @@ -1,36 +0,0 @@ -package org.apache.lucene.queries.function.valuesource; - -/* - * Licensed to the Apache Software Foundation (ASF) under one or more - * contributor license agreements. See the NOTICE file distributed with - * this work for additional information regarding copyright ownership. - * The ASF licenses this file to You under the Apache License, Version 2.0 - * (the "License"); you may not use this file except in compliance with - * the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -import org.apache.lucene.queries.function.ValueSource; - -public class EqualFunction extends CompareNumericFunction { - public EqualFunction(ValueSource lhs, ValueSource rhs) { - super(lhs, rhs); - } - - @Override - public boolean compareNumeric(double lhs, double rhs) { - return lhs == rhs; - } - - @Override - public String getLabel() { - return "eq"; - } -} diff --git a/lucene/queries/src/java/org/apache/lucene/queries/function/valuesource/GreaterThanEqualFunction.java b/lucene/queries/src/java/org/apache/lucene/queries/function/valuesource/GreaterThanEqualFunction.java deleted file mode 100644 index 71cf74cd0026..000000000000 --- a/lucene/queries/src/java/org/apache/lucene/queries/function/valuesource/GreaterThanEqualFunction.java +++ /dev/null @@ -1,37 +0,0 @@ -package org.apache.lucene.queries.function.valuesource; - -/* - * Licensed to the Apache Software Foundation (ASF) under one or more - * contributor license agreements. See the NOTICE file distributed with - * this work for additional information regarding copyright ownership. - * The ASF licenses this file to You under the Apache License, Version 2.0 - * (the "License"); you may not use this file except in compliance with - * the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -import org.apache.lucene.queries.function.ValueSource; - -public class GreaterThanEqualFunction extends CompareNumericFunction { - - public GreaterThanEqualFunction(ValueSource lhs, ValueSource rhs) { - super(lhs, rhs); - } - - @Override - public boolean compareNumeric(double lhs, double rhs) { - return lhs >= rhs; - } - - @Override - public String getLabel() { - return "gte"; - } -} diff --git a/lucene/queries/src/java/org/apache/lucene/queries/function/valuesource/GreaterThanFunction.java b/lucene/queries/src/java/org/apache/lucene/queries/function/valuesource/GreaterThanFunction.java deleted file mode 100644 index 260f1e39f405..000000000000 --- a/lucene/queries/src/java/org/apache/lucene/queries/function/valuesource/GreaterThanFunction.java +++ /dev/null @@ -1,40 +0,0 @@ -package org.apache.lucene.queries.function.valuesource; - -/* - * Licensed to the Apache Software Foundation (ASF) under one or more - * contributor license agreements. See the NOTICE file distributed with - * this work for additional information regarding copyright ownership. - * The ASF licenses this file to You under the Apache License, Version 2.0 - * (the "License"); you may not use this file except in compliance with - * the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -import org.apache.lucene.queries.function.ValueSource; - -/** - * Created by doug on 7/5/16. - */ -public class GreaterThanFunction extends CompareNumericFunction { - - public GreaterThanFunction(ValueSource lhs, ValueSource rhs) { - super(lhs, rhs); - } - - @Override - public boolean compareNumeric(double lhs, double rhs) { - return lhs > rhs; - } - - @Override - public String getLabel() { - return "gt"; - } -} diff --git a/lucene/queries/src/java/org/apache/lucene/queries/function/valuesource/LessThanEqualFunction.java b/lucene/queries/src/java/org/apache/lucene/queries/function/valuesource/LessThanEqualFunction.java deleted file mode 100644 index bff5dc13c7f7..000000000000 --- a/lucene/queries/src/java/org/apache/lucene/queries/function/valuesource/LessThanEqualFunction.java +++ /dev/null @@ -1,39 +0,0 @@ -package org.apache.lucene.queries.function.valuesource; - -/* - * Licensed to the Apache Software Foundation (ASF) under one or more - * contributor license agreements. See the NOTICE file distributed with - * this work for additional information regarding copyright ownership. - * The ASF licenses this file to You under the Apache License, Version 2.0 - * (the "License"); you may not use this file except in compliance with - * the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -import org.apache.lucene.queries.function.ValueSource; - -/** - * Created by doug on 7/5/16. - */ -public class LessThanEqualFunction extends CompareNumericFunction { - public LessThanEqualFunction(ValueSource lhs, ValueSource rhs) { - super(lhs, rhs); - } - - @Override - public boolean compareNumeric(double lhs, double rhs) { - return lhs <= rhs; - } - - @Override - public String getLabel() { - return "lte"; - } -} diff --git a/lucene/queries/src/java/org/apache/lucene/queries/function/valuesource/LessThanFunction.java b/lucene/queries/src/java/org/apache/lucene/queries/function/valuesource/LessThanFunction.java deleted file mode 100644 index efe61d8a8c87..000000000000 --- a/lucene/queries/src/java/org/apache/lucene/queries/function/valuesource/LessThanFunction.java +++ /dev/null @@ -1,37 +0,0 @@ -package org.apache.lucene.queries.function.valuesource; - -/* - * Licensed to the Apache Software Foundation (ASF) under one or more - * contributor license agreements. See the NOTICE file distributed with - * this work for additional information regarding copyright ownership. - * The ASF licenses this file to You under the Apache License, Version 2.0 - * (the "License"); you may not use this file except in compliance with - * the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -import org.apache.lucene.queries.function.ValueSource; - -public class LessThanFunction extends CompareNumericFunction { - - public LessThanFunction(ValueSource lhs, ValueSource rhs) { - super(lhs, rhs); - } - - @Override - public boolean compareNumeric(double lhs, double rhs) { - return lhs < rhs; - } - - @Override - public String getLabel() { - return "lt"; - } -} 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 017877c26871..b65866c2f8b1 100644 --- a/solr/core/src/java/org/apache/solr/search/ValueSourceParser.java +++ b/solr/core/src/java/org/apache/solr/search/ValueSourceParser.java @@ -825,30 +825,45 @@ public ValueSource parse(FunctionQParser fp) throws SyntaxError { addParser("gt", new ValueSourceParser() { @Override public ValueSource parse(FunctionQParser fp) throws SyntaxError { - ValueSource lhs = fp.parseValueSource(); - ValueSource rhs = fp.parseValueSource(); + ValueSource lhsValSource = fp.parseValueSource(); + ValueSource rhsValSource = fp.parseValueSource(); - return new GreaterThanFunction(lhs, rhs); + return new CompareNumericFunction(lhsValSource, rhsValSource, "gt") { + @Override + public boolean compare(double lhs, double rhs) { + return lhs > rhs; + } + }; } }); addParser("lt", new ValueSourceParser() { @Override public ValueSource parse(FunctionQParser fp) throws SyntaxError { - ValueSource lhs = fp.parseValueSource(); - ValueSource rhs = fp.parseValueSource(); + ValueSource lhsValSource = fp.parseValueSource(); + ValueSource rhsValSource = fp.parseValueSource(); - return new LessThanFunction(lhs, rhs); + return new CompareNumericFunction(lhsValSource, rhsValSource, "lt") { + @Override + public boolean compare(double lhs, double rhs) { + return lhs < rhs; + } + }; } }); addParser("gte", new ValueSourceParser() { @Override public ValueSource parse(FunctionQParser fp) throws SyntaxError { - ValueSource lhs = fp.parseValueSource(); - ValueSource rhs = fp.parseValueSource(); + ValueSource lhsValSource = fp.parseValueSource(); + ValueSource rhsValSource = fp.parseValueSource(); - return new GreaterThanEqualFunction(lhs, rhs); + return new CompareNumericFunction(lhsValSource, rhsValSource, "gte") { + @Override + public boolean compare(double lhs, double rhs) { + return lhs >= rhs; + } + }; } }); @@ -856,20 +871,30 @@ public ValueSource parse(FunctionQParser fp) throws SyntaxError { addParser("lte", new ValueSourceParser() { @Override public ValueSource parse(FunctionQParser fp) throws SyntaxError { - ValueSource lhs = fp.parseValueSource(); - ValueSource rhs = fp.parseValueSource(); + ValueSource lhsValSource = fp.parseValueSource(); + ValueSource rhsValSource = fp.parseValueSource(); - return new LessThanEqualFunction(lhs, rhs); + return new CompareNumericFunction(lhsValSource, rhsValSource, "lte") { + @Override + public boolean compare(double lhs, double rhs) { + return lhs <= rhs; + } + }; } }); addParser("eq", new ValueSourceParser() { @Override public ValueSource parse(FunctionQParser fp) throws SyntaxError { - ValueSource lhs = fp.parseValueSource(); - ValueSource rhs = fp.parseValueSource(); + ValueSource lhsValSource = fp.parseValueSource(); + ValueSource rhsValSource = fp.parseValueSource(); - return new EqualFunction(lhs, rhs); + return new CompareNumericFunction(lhsValSource, rhsValSource, "eq") { + @Override + public boolean compare(double lhs, double rhs) { + return lhs == rhs; + } + }; } }); From 7cddc42d6cbad085e7d708da0ec5c01c421a6d08 Mon Sep 17 00:00:00 2001 From: Doug Turnbull Date: Wed, 6 Jul 2016 20:19:03 -0400 Subject: [PATCH 07/20] use BoolDocValues instead of FunctionValues --- .../valuesource/CompareNumericFunction.java | 35 ++----------------- 1 file changed, 2 insertions(+), 33 deletions(-) diff --git a/lucene/queries/src/java/org/apache/lucene/queries/function/valuesource/CompareNumericFunction.java b/lucene/queries/src/java/org/apache/lucene/queries/function/valuesource/CompareNumericFunction.java index e2f57df0b887..4943d4cbd454 100644 --- a/lucene/queries/src/java/org/apache/lucene/queries/function/valuesource/CompareNumericFunction.java +++ b/lucene/queries/src/java/org/apache/lucene/queries/function/valuesource/CompareNumericFunction.java @@ -23,6 +23,7 @@ import org.apache.lucene.index.LeafReaderContext; import org.apache.lucene.queries.function.ValueSource; import org.apache.lucene.queries.function.FunctionValues; +import org.apache.lucene.queries.function.docvalues.BoolDocValues; import org.apache.lucene.search.IndexSearcher; @@ -58,39 +59,7 @@ public FunctionValues getValues(Map context, LeafReaderContext readerContext) th final FunctionValues rhsVal = this.rhs.getValues(context, readerContext); final String compLabel = this.getLabel(); - return new FunctionValues() { - @Override - public byte byteVal(int doc) { - - return boolVal(doc) ? (byte)1 : (byte)0; - } - - @Override - public short shortVal(int doc) { - return byteVal(doc); - } - - @Override - public float floatVal(int doc) { - return byteVal(doc); - } - - @Override - public int intVal(int doc) { - return byteVal(doc); - } - - @Override - public long longVal(int doc) { - return byteVal(doc); - } - - @Override - public double doubleVal(int doc) { - return byteVal(doc); - } - - + return new BoolDocValues(this) { @Override public boolean boolVal(int doc) { return compare(lhsVal.floatVal(doc), rhsVal.floatVal(doc)); From c59e7066b50e122100b30f972427eec25e4ecb05 Mon Sep 17 00:00:00 2001 From: Doug Turnbull Date: Wed, 6 Jul 2016 20:21:56 -0400 Subject: [PATCH 08/20] Rename to ComparisonValueSource --- ...NumericFunction.java => ComparisonValueSource.java} | 10 ++++------ .../java/org/apache/solr/search/ValueSourceParser.java | 10 +++++----- 2 files changed, 9 insertions(+), 11 deletions(-) rename lucene/queries/src/java/org/apache/lucene/queries/function/valuesource/{CompareNumericFunction.java => ComparisonValueSource.java} (91%) diff --git a/lucene/queries/src/java/org/apache/lucene/queries/function/valuesource/CompareNumericFunction.java b/lucene/queries/src/java/org/apache/lucene/queries/function/valuesource/ComparisonValueSource.java similarity index 91% rename from lucene/queries/src/java/org/apache/lucene/queries/function/valuesource/CompareNumericFunction.java rename to lucene/queries/src/java/org/apache/lucene/queries/function/valuesource/ComparisonValueSource.java index 4943d4cbd454..fe2a85bf2bc1 100644 --- a/lucene/queries/src/java/org/apache/lucene/queries/function/valuesource/CompareNumericFunction.java +++ b/lucene/queries/src/java/org/apache/lucene/queries/function/valuesource/ComparisonValueSource.java @@ -31,13 +31,13 @@ * Base class for comparison operators used within if statements * To Solr's if function query a 0 is considered "false", all other values are "true" */ -public abstract class CompareNumericFunction extends BoolFunction { +public abstract class ComparisonValueSource extends BoolFunction { private final ValueSource lhs; private final ValueSource rhs; private final String label; - public CompareNumericFunction(ValueSource lhs, ValueSource rhs, String label) { + public ComparisonValueSource(ValueSource lhs, ValueSource rhs, String label) { this.lhs = lhs; this.rhs = rhs; this.label = label; @@ -70,14 +70,12 @@ public String toString(int doc) { return compLabel + "(" + lhsVal.toString(doc) + "," + rhsVal.toString(doc) + ")"; } }; - - }; @Override public boolean equals(Object o) { - if (!(o instanceof CompareNumericFunction)) return false; - CompareNumericFunction other = (CompareNumericFunction)o; + if (!(o instanceof ComparisonValueSource)) return false; + ComparisonValueSource other = (ComparisonValueSource)o; return getLabel().equals(other.getLabel()) && lhs.equals(other.lhs) && rhs.equals(other.rhs); } 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 b65866c2f8b1..615d5cbe3727 100644 --- a/solr/core/src/java/org/apache/solr/search/ValueSourceParser.java +++ b/solr/core/src/java/org/apache/solr/search/ValueSourceParser.java @@ -828,7 +828,7 @@ public ValueSource parse(FunctionQParser fp) throws SyntaxError { ValueSource lhsValSource = fp.parseValueSource(); ValueSource rhsValSource = fp.parseValueSource(); - return new CompareNumericFunction(lhsValSource, rhsValSource, "gt") { + return new ComparisonValueSource(lhsValSource, rhsValSource, "gt") { @Override public boolean compare(double lhs, double rhs) { return lhs > rhs; @@ -843,7 +843,7 @@ public ValueSource parse(FunctionQParser fp) throws SyntaxError { ValueSource lhsValSource = fp.parseValueSource(); ValueSource rhsValSource = fp.parseValueSource(); - return new CompareNumericFunction(lhsValSource, rhsValSource, "lt") { + return new ComparisonValueSource(lhsValSource, rhsValSource, "lt") { @Override public boolean compare(double lhs, double rhs) { return lhs < rhs; @@ -858,7 +858,7 @@ public ValueSource parse(FunctionQParser fp) throws SyntaxError { ValueSource lhsValSource = fp.parseValueSource(); ValueSource rhsValSource = fp.parseValueSource(); - return new CompareNumericFunction(lhsValSource, rhsValSource, "gte") { + return new ComparisonValueSource(lhsValSource, rhsValSource, "gte") { @Override public boolean compare(double lhs, double rhs) { return lhs >= rhs; @@ -874,7 +874,7 @@ public ValueSource parse(FunctionQParser fp) throws SyntaxError { ValueSource lhsValSource = fp.parseValueSource(); ValueSource rhsValSource = fp.parseValueSource(); - return new CompareNumericFunction(lhsValSource, rhsValSource, "lte") { + return new ComparisonValueSource(lhsValSource, rhsValSource, "lte") { @Override public boolean compare(double lhs, double rhs) { return lhs <= rhs; @@ -889,7 +889,7 @@ public ValueSource parse(FunctionQParser fp) throws SyntaxError { ValueSource lhsValSource = fp.parseValueSource(); ValueSource rhsValSource = fp.parseValueSource(); - return new CompareNumericFunction(lhsValSource, rhsValSource, "eq") { + return new ComparisonValueSource(lhsValSource, rhsValSource, "eq") { @Override public boolean compare(double lhs, double rhs) { return lhs == rhs; From 1e5103efcdca7e787c3389f57738230622fe2a9f Mon Sep 17 00:00:00 2001 From: Doug Turnbull Date: Thu, 7 Jul 2016 09:22:30 -0400 Subject: [PATCH 09/20] Changes "label" to "name" like MultiBoolFunction --- .../valuesource/ComparisonValueSource.java | 20 +++++++++---------- 1 file changed, 10 insertions(+), 10 deletions(-) diff --git a/lucene/queries/src/java/org/apache/lucene/queries/function/valuesource/ComparisonValueSource.java b/lucene/queries/src/java/org/apache/lucene/queries/function/valuesource/ComparisonValueSource.java index fe2a85bf2bc1..659ab254b652 100644 --- a/lucene/queries/src/java/org/apache/lucene/queries/function/valuesource/ComparisonValueSource.java +++ b/lucene/queries/src/java/org/apache/lucene/queries/function/valuesource/ComparisonValueSource.java @@ -35,20 +35,20 @@ public abstract class ComparisonValueSource extends BoolFunction { private final ValueSource lhs; private final ValueSource rhs; - private final String label; + private final String name; - public ComparisonValueSource(ValueSource lhs, ValueSource rhs, String label) { + public ComparisonValueSource(ValueSource lhs, ValueSource rhs, String name) { this.lhs = lhs; this.rhs = rhs; - this.label = label; + this.name = name; } // Perform the comparison, returning true or false public abstract boolean compare(double lhs, double rhs); - // Uniquely identify the operation (ie "gt", "lt" "gte, etc) - public String getLabel() { - return this.label; + // Uniquely identify the operation (ie "gt", "lt" "gte", etc) + public String name() { + return this.name; } // string comparison? Probably should be a seperate function @@ -57,7 +57,7 @@ public String getLabel() { public FunctionValues getValues(Map context, LeafReaderContext readerContext) throws IOException { final FunctionValues lhsVal = this.lhs.getValues(context, readerContext); final FunctionValues rhsVal = this.rhs.getValues(context, readerContext); - final String compLabel = this.getLabel(); + final String compLabel = this.name(); return new BoolDocValues(this) { @Override @@ -76,13 +76,13 @@ public String toString(int doc) { public boolean equals(Object o) { if (!(o instanceof ComparisonValueSource)) return false; ComparisonValueSource other = (ComparisonValueSource)o; - return getLabel().equals(other.getLabel()) + return name().equals(other.name()) && lhs.equals(other.lhs) && rhs.equals(other.rhs); } @Override public int hashCode() { - int h = getLabel().hashCode(); + int h = name().hashCode(); h = h * 31 + lhs.hashCode(); h = h * 31 + rhs.hashCode(); return h; @@ -90,7 +90,7 @@ public int hashCode() { @Override public String description() { - return getLabel() + "(" + lhs.description() + "," + rhs.description() + ")"; + return name() + "(" + lhs.description() + "," + rhs.description() + ")"; } @Override From 11977a53af1673be6826323b4109175c72289b19 Mon Sep 17 00:00:00 2001 From: Doug Turnbull Date: Thu, 7 Jul 2016 09:42:22 -0400 Subject: [PATCH 10/20] Add test for comparison value source --- .../queries/function/TestValueSources.java | 60 +++++++++---------- 1 file changed, 28 insertions(+), 32 deletions(-) diff --git a/lucene/queries/src/test/org/apache/lucene/queries/function/TestValueSources.java b/lucene/queries/src/test/org/apache/lucene/queries/function/TestValueSources.java index 509e0ab3cb2f..3829cbbb1c51 100644 --- a/lucene/queries/src/test/org/apache/lucene/queries/function/TestValueSources.java +++ b/lucene/queries/src/test/org/apache/lucene/queries/function/TestValueSources.java @@ -35,38 +35,7 @@ import org.apache.lucene.index.RandomIndexWriter; import org.apache.lucene.index.LeafReaderContext; import org.apache.lucene.queries.function.docvalues.FloatDocValues; -import org.apache.lucene.queries.function.valuesource.BytesRefFieldSource; -import org.apache.lucene.queries.function.valuesource.ConstValueSource; -import org.apache.lucene.queries.function.valuesource.DivFloatFunction; -import org.apache.lucene.queries.function.valuesource.DocFreqValueSource; -import org.apache.lucene.queries.function.valuesource.DoubleConstValueSource; -import org.apache.lucene.queries.function.valuesource.DoubleFieldSource; -import org.apache.lucene.queries.function.valuesource.FloatFieldSource; -import org.apache.lucene.queries.function.valuesource.IDFValueSource; -import org.apache.lucene.queries.function.valuesource.IfFunction; -import org.apache.lucene.queries.function.valuesource.IntFieldSource; -import org.apache.lucene.queries.function.valuesource.JoinDocFreqValueSource; -import org.apache.lucene.queries.function.valuesource.LinearFloatFunction; -import org.apache.lucene.queries.function.valuesource.LiteralValueSource; -import org.apache.lucene.queries.function.valuesource.LongFieldSource; -import org.apache.lucene.queries.function.valuesource.MaxDocValueSource; -import org.apache.lucene.queries.function.valuesource.MaxFloatFunction; -import org.apache.lucene.queries.function.valuesource.MinFloatFunction; -import org.apache.lucene.queries.function.valuesource.MultiFloatFunction; -import org.apache.lucene.queries.function.valuesource.MultiFunction; -import org.apache.lucene.queries.function.valuesource.NormValueSource; -import org.apache.lucene.queries.function.valuesource.NumDocsValueSource; -import org.apache.lucene.queries.function.valuesource.PowFloatFunction; -import org.apache.lucene.queries.function.valuesource.ProductFloatFunction; -import org.apache.lucene.queries.function.valuesource.QueryValueSource; -import org.apache.lucene.queries.function.valuesource.RangeMapFloatFunction; -import org.apache.lucene.queries.function.valuesource.ReciprocalFloatFunction; -import org.apache.lucene.queries.function.valuesource.ScaleFloatFunction; -import org.apache.lucene.queries.function.valuesource.SumFloatFunction; -import org.apache.lucene.queries.function.valuesource.SumTotalTermFreqValueSource; -import org.apache.lucene.queries.function.valuesource.TFValueSource; -import org.apache.lucene.queries.function.valuesource.TermFreqValueSource; -import org.apache.lucene.queries.function.valuesource.TotalTermFreqValueSource; +import org.apache.lucene.queries.function.valuesource.*; import org.apache.lucene.search.CheckHits; import org.apache.lucene.search.IndexSearcher; import org.apache.lucene.search.Query; @@ -243,6 +212,33 @@ public void testIf() throws Exception { new ConstValueSource(1.0f)); assertNoneExist(vs); } + + public void testComparisons() throws Exception { + ValueSource vs = new FloatFieldSource("float"); + ValueSource const52Vs = new ConstValueSource(5.2f); + + ValueSource gtVs = new ComparisonValueSource(const52Vs, vs, "test") { + @Override + public boolean compare(double lhs, double rhs) { + return lhs > rhs; + } + }; + + FunctionQuery q = new FunctionQuery(gtVs); + assertHits(q, new float[] {0f, 0f}); + + ValueSource gteVs = new ComparisonValueSource(const52Vs, vs, "test") { + @Override + public boolean compare(double lhs, double rhs) { + return lhs >= rhs; + } + }; + + // both true with >= + q = new FunctionQuery(gteVs); + assertHits(q, new float[] {1f, 0f}); + + } public void testInt() throws Exception { ValueSource vs = new IntFieldSource("int"); From d48610d19b5f5bf80d34b8fc9b38fff8bc41d4c5 Mon Sep 17 00:00:00 2001 From: Doug Turnbull Date: Thu, 7 Jul 2016 09:54:41 -0400 Subject: [PATCH 11/20] Fixes equals and hashCode to use getClass --- .../queries/function/valuesource/ComparisonValueSource.java | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/lucene/queries/src/java/org/apache/lucene/queries/function/valuesource/ComparisonValueSource.java b/lucene/queries/src/java/org/apache/lucene/queries/function/valuesource/ComparisonValueSource.java index 659ab254b652..6674b05769de 100644 --- a/lucene/queries/src/java/org/apache/lucene/queries/function/valuesource/ComparisonValueSource.java +++ b/lucene/queries/src/java/org/apache/lucene/queries/function/valuesource/ComparisonValueSource.java @@ -70,10 +70,11 @@ public String toString(int doc) { return compLabel + "(" + lhsVal.toString(doc) + "," + rhsVal.toString(doc) + ")"; } }; - }; + } @Override public boolean equals(Object o) { + if (this.getClass() != o.getClass()) return false; if (!(o instanceof ComparisonValueSource)) return false; ComparisonValueSource other = (ComparisonValueSource)o; return name().equals(other.name()) @@ -82,7 +83,8 @@ public boolean equals(Object o) { @Override public int hashCode() { - int h = name().hashCode(); + int h = this.getClass().hashCode(); + h = h * 31 + this.name().hashCode(); h = h * 31 + lhs.hashCode(); h = h * 31 + rhs.hashCode(); return h; From ca6f9a4313efe996cdf1e43d745e143e08bbc4ac Mon Sep 17 00:00:00 2001 From: Doug Turnbull Date: Thu, 7 Jul 2016 12:58:46 -0400 Subject: [PATCH 12/20] Rename to ComparisonBoolFunction to reflect extending BoolFunction --- ...sonValueSource.java => ComparisonBoolFunction.java} | 8 ++++---- .../lucene/queries/function/TestValueSources.java | 4 ++-- .../java/org/apache/solr/search/ValueSourceParser.java | 10 +++++----- 3 files changed, 11 insertions(+), 11 deletions(-) rename lucene/queries/src/java/org/apache/lucene/queries/function/valuesource/{ComparisonValueSource.java => ComparisonBoolFunction.java} (92%) diff --git a/lucene/queries/src/java/org/apache/lucene/queries/function/valuesource/ComparisonValueSource.java b/lucene/queries/src/java/org/apache/lucene/queries/function/valuesource/ComparisonBoolFunction.java similarity index 92% rename from lucene/queries/src/java/org/apache/lucene/queries/function/valuesource/ComparisonValueSource.java rename to lucene/queries/src/java/org/apache/lucene/queries/function/valuesource/ComparisonBoolFunction.java index 6674b05769de..7e18c3f0e633 100644 --- a/lucene/queries/src/java/org/apache/lucene/queries/function/valuesource/ComparisonValueSource.java +++ b/lucene/queries/src/java/org/apache/lucene/queries/function/valuesource/ComparisonBoolFunction.java @@ -31,13 +31,13 @@ * Base class for comparison operators used within if statements * To Solr's if function query a 0 is considered "false", all other values are "true" */ -public abstract class ComparisonValueSource extends BoolFunction { +public abstract class ComparisonBoolFunction extends BoolFunction { private final ValueSource lhs; private final ValueSource rhs; private final String name; - public ComparisonValueSource(ValueSource lhs, ValueSource rhs, String name) { + public ComparisonBoolFunction(ValueSource lhs, ValueSource rhs, String name) { this.lhs = lhs; this.rhs = rhs; this.name = name; @@ -75,8 +75,8 @@ public String toString(int doc) { @Override public boolean equals(Object o) { if (this.getClass() != o.getClass()) return false; - if (!(o instanceof ComparisonValueSource)) return false; - ComparisonValueSource other = (ComparisonValueSource)o; + if (!(o instanceof ComparisonBoolFunction)) return false; + ComparisonBoolFunction other = (ComparisonBoolFunction)o; return name().equals(other.name()) && lhs.equals(other.lhs) && rhs.equals(other.rhs); } diff --git a/lucene/queries/src/test/org/apache/lucene/queries/function/TestValueSources.java b/lucene/queries/src/test/org/apache/lucene/queries/function/TestValueSources.java index 3829cbbb1c51..ed115a52b0d9 100644 --- a/lucene/queries/src/test/org/apache/lucene/queries/function/TestValueSources.java +++ b/lucene/queries/src/test/org/apache/lucene/queries/function/TestValueSources.java @@ -217,7 +217,7 @@ public void testComparisons() throws Exception { ValueSource vs = new FloatFieldSource("float"); ValueSource const52Vs = new ConstValueSource(5.2f); - ValueSource gtVs = new ComparisonValueSource(const52Vs, vs, "test") { + ValueSource gtVs = new ComparisonBoolFunction(const52Vs, vs, "test") { @Override public boolean compare(double lhs, double rhs) { return lhs > rhs; @@ -227,7 +227,7 @@ public boolean compare(double lhs, double rhs) { FunctionQuery q = new FunctionQuery(gtVs); assertHits(q, new float[] {0f, 0f}); - ValueSource gteVs = new ComparisonValueSource(const52Vs, vs, "test") { + ValueSource gteVs = new ComparisonBoolFunction(const52Vs, vs, "test") { @Override public boolean compare(double lhs, double rhs) { return lhs >= rhs; 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 615d5cbe3727..f8cb1545c479 100644 --- a/solr/core/src/java/org/apache/solr/search/ValueSourceParser.java +++ b/solr/core/src/java/org/apache/solr/search/ValueSourceParser.java @@ -828,7 +828,7 @@ public ValueSource parse(FunctionQParser fp) throws SyntaxError { ValueSource lhsValSource = fp.parseValueSource(); ValueSource rhsValSource = fp.parseValueSource(); - return new ComparisonValueSource(lhsValSource, rhsValSource, "gt") { + return new ComparisonBoolFunction(lhsValSource, rhsValSource, "gt") { @Override public boolean compare(double lhs, double rhs) { return lhs > rhs; @@ -843,7 +843,7 @@ public ValueSource parse(FunctionQParser fp) throws SyntaxError { ValueSource lhsValSource = fp.parseValueSource(); ValueSource rhsValSource = fp.parseValueSource(); - return new ComparisonValueSource(lhsValSource, rhsValSource, "lt") { + return new ComparisonBoolFunction(lhsValSource, rhsValSource, "lt") { @Override public boolean compare(double lhs, double rhs) { return lhs < rhs; @@ -858,7 +858,7 @@ public ValueSource parse(FunctionQParser fp) throws SyntaxError { ValueSource lhsValSource = fp.parseValueSource(); ValueSource rhsValSource = fp.parseValueSource(); - return new ComparisonValueSource(lhsValSource, rhsValSource, "gte") { + return new ComparisonBoolFunction(lhsValSource, rhsValSource, "gte") { @Override public boolean compare(double lhs, double rhs) { return lhs >= rhs; @@ -874,7 +874,7 @@ public ValueSource parse(FunctionQParser fp) throws SyntaxError { ValueSource lhsValSource = fp.parseValueSource(); ValueSource rhsValSource = fp.parseValueSource(); - return new ComparisonValueSource(lhsValSource, rhsValSource, "lte") { + return new ComparisonBoolFunction(lhsValSource, rhsValSource, "lte") { @Override public boolean compare(double lhs, double rhs) { return lhs <= rhs; @@ -889,7 +889,7 @@ public ValueSource parse(FunctionQParser fp) throws SyntaxError { ValueSource lhsValSource = fp.parseValueSource(); ValueSource rhsValSource = fp.parseValueSource(); - return new ComparisonValueSource(lhsValSource, rhsValSource, "eq") { + return new ComparisonBoolFunction(lhsValSource, rhsValSource, "eq") { @Override public boolean compare(double lhs, double rhs) { return lhs == rhs; From ce8766ea21b6f97888a375d6f0e0ea3db87d6645 Mon Sep 17 00:00:00 2001 From: Doug Turnbull Date: Mon, 25 Jul 2016 14:03:46 -0400 Subject: [PATCH 13/20] Adds test for problem with long comparisons --- .../solr/search/function/TestFunctionQuery.java | 16 +++++++++++++++- 1 file changed, 15 insertions(+), 1 deletion(-) 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 7889adb18907..dae7a435ef9f 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 @@ -834,4 +834,18 @@ public void testNumericComparisons() { } -} + public void testLongComparisons() { + assertU(adoc("id", "1", "number_of_atoms_in_universe_l", Long.toString(Long.MAX_VALUE))); + assertU(adoc("id", "2", "number_of_atoms_in_universe_l", Long.toString(Long.MAX_VALUE - 1))); + assertU(commit()); + + singleTest("number_of_atoms_in_universe_l", "if(gt(number_of_atoms_in_universe_l," + Long.toString(Long.MAX_VALUE - 1) + "),5,2)", + /*id*/1, /*score*/5, + /*id*/2, /*score*/2); + + singleTest("number_of_atoms_in_universe_l", "if(lt(number_of_atoms_in_universe_l," + Long.toString(Long.MAX_VALUE) + "),5,2)", + /*id*/2, /*score*/5, + /*id*/1, /*score*/2); + } + + } From 6e82c273cd5e693c5ccc3ec079f81d846d5a94b9 Mon Sep 17 00:00:00 2001 From: Doug Turnbull Date: Mon, 25 Jul 2016 14:53:42 -0400 Subject: [PATCH 14/20] Forces comparison to use longs when needed --- .../valuesource/ComparisonBoolFunction.java | 16 +++++++++++- .../queries/function/TestValueSources.java | 11 ++++++++ .../apache/solr/search/ValueSourceParser.java | 26 +++++++++++++++++++ 3 files changed, 52 insertions(+), 1 deletion(-) diff --git a/lucene/queries/src/java/org/apache/lucene/queries/function/valuesource/ComparisonBoolFunction.java b/lucene/queries/src/java/org/apache/lucene/queries/function/valuesource/ComparisonBoolFunction.java index 7e18c3f0e633..1bb2d8825c6a 100644 --- a/lucene/queries/src/java/org/apache/lucene/queries/function/valuesource/ComparisonBoolFunction.java +++ b/lucene/queries/src/java/org/apache/lucene/queries/function/valuesource/ComparisonBoolFunction.java @@ -37,6 +37,7 @@ public abstract class ComparisonBoolFunction extends BoolFunction { private final ValueSource rhs; private final String name; + public ComparisonBoolFunction(ValueSource lhs, ValueSource rhs, String name) { this.lhs = lhs; this.rhs = rhs; @@ -46,6 +47,8 @@ public ComparisonBoolFunction(ValueSource lhs, ValueSource rhs, String name) { // Perform the comparison, returning true or false public abstract boolean compare(double lhs, double rhs); + public abstract boolean compare(long lhs, long rhs); + // Uniquely identify the operation (ie "gt", "lt" "gte", etc) public String name() { return this.name; @@ -62,7 +65,18 @@ public FunctionValues getValues(Map context, LeafReaderContext readerContext) th return new BoolDocValues(this) { @Override public boolean boolVal(int doc) { - return compare(lhsVal.floatVal(doc), rhsVal.floatVal(doc)); + + // should we treat this as an integer comparison? only if doubleVal == longVal, + // indicating the two values are effectively integers. + // + // If these are integers, we need to compare them as such + // as to avoid floating point precision problems + if (lhsVal.doubleVal(doc) == lhsVal.longVal(doc) && rhsVal.doubleVal(doc) == lhsVal.longVal(doc)) { + return compare(lhsVal.longVal(doc), rhsVal.longVal(doc)); + + } + + return compare(lhsVal.doubleVal(doc), rhsVal.doubleVal(doc)); } @Override diff --git a/lucene/queries/src/test/org/apache/lucene/queries/function/TestValueSources.java b/lucene/queries/src/test/org/apache/lucene/queries/function/TestValueSources.java index ed115a52b0d9..ab0438784f75 100644 --- a/lucene/queries/src/test/org/apache/lucene/queries/function/TestValueSources.java +++ b/lucene/queries/src/test/org/apache/lucene/queries/function/TestValueSources.java @@ -222,6 +222,11 @@ public void testComparisons() throws Exception { public boolean compare(double lhs, double rhs) { return lhs > rhs; } + + @Override + public boolean compare(long lhs, long rhs) { + return lhs > rhs; + } }; FunctionQuery q = new FunctionQuery(gtVs); @@ -232,6 +237,12 @@ public boolean compare(double lhs, double rhs) { public boolean compare(double lhs, double rhs) { return lhs >= rhs; } + + @Override + public boolean compare(long lhs, long rhs) { + return lhs >= rhs; + } + }; // both true with >= 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 f8cb1545c479..b291a2e47f8c 100644 --- a/solr/core/src/java/org/apache/solr/search/ValueSourceParser.java +++ b/solr/core/src/java/org/apache/solr/search/ValueSourceParser.java @@ -833,6 +833,11 @@ public ValueSource parse(FunctionQParser fp) throws SyntaxError { public boolean compare(double lhs, double rhs) { return lhs > rhs; } + + @Override + public boolean compare(long lhs, long rhs) { + return lhs > rhs; + } }; } }); @@ -848,6 +853,11 @@ public ValueSource parse(FunctionQParser fp) throws SyntaxError { public boolean compare(double lhs, double rhs) { return lhs < rhs; } + + @Override + public boolean compare(long lhs, long rhs) { + return lhs < rhs; + } }; } }); @@ -863,6 +873,12 @@ public ValueSource parse(FunctionQParser fp) throws SyntaxError { public boolean compare(double lhs, double rhs) { return lhs >= rhs; } + + @Override + public boolean compare(long lhs, long rhs) { + return lhs >= rhs; + } + }; } }); @@ -879,6 +895,11 @@ public ValueSource parse(FunctionQParser fp) throws SyntaxError { public boolean compare(double lhs, double rhs) { return lhs <= rhs; } + + @Override + public boolean compare(long lhs, long rhs) { + return lhs <= rhs; + } }; } }); @@ -894,6 +915,11 @@ public ValueSource parse(FunctionQParser fp) throws SyntaxError { public boolean compare(double lhs, double rhs) { return lhs == rhs; } + + @Override + public boolean compare(long lhs, long rhs) { + return lhs == rhs; + } }; } }); From c81e4d1566a639b65d269475b5ae8f63aed740a5 Mon Sep 17 00:00:00 2001 From: Doug Turnbull Date: Mon, 25 Jul 2016 15:06:35 -0400 Subject: [PATCH 15/20] Changes to generic method taking Comparables --- .../valuesource/ComparisonBoolFunction.java | 5 +- .../queries/function/TestValueSources.java | 18 ++----- .../apache/solr/search/ValueSourceParser.java | 52 ++++--------------- 3 files changed, 17 insertions(+), 58 deletions(-) diff --git a/lucene/queries/src/java/org/apache/lucene/queries/function/valuesource/ComparisonBoolFunction.java b/lucene/queries/src/java/org/apache/lucene/queries/function/valuesource/ComparisonBoolFunction.java index 1bb2d8825c6a..c6985c39131b 100644 --- a/lucene/queries/src/java/org/apache/lucene/queries/function/valuesource/ComparisonBoolFunction.java +++ b/lucene/queries/src/java/org/apache/lucene/queries/function/valuesource/ComparisonBoolFunction.java @@ -44,10 +44,9 @@ public ComparisonBoolFunction(ValueSource lhs, ValueSource rhs, String name) { this.name = name; } - // Perform the comparison, returning true or false - public abstract boolean compare(double lhs, double rhs); - public abstract boolean compare(long lhs, long rhs); + // Perform the comparison, returning true or false + public abstract > boolean compare(T lhs, T rhs); // Uniquely identify the operation (ie "gt", "lt" "gte", etc) public String name() { diff --git a/lucene/queries/src/test/org/apache/lucene/queries/function/TestValueSources.java b/lucene/queries/src/test/org/apache/lucene/queries/function/TestValueSources.java index ab0438784f75..9ab251d87732 100644 --- a/lucene/queries/src/test/org/apache/lucene/queries/function/TestValueSources.java +++ b/lucene/queries/src/test/org/apache/lucene/queries/function/TestValueSources.java @@ -219,13 +219,8 @@ public void testComparisons() throws Exception { ValueSource gtVs = new ComparisonBoolFunction(const52Vs, vs, "test") { @Override - public boolean compare(double lhs, double rhs) { - return lhs > rhs; - } - - @Override - public boolean compare(long lhs, long rhs) { - return lhs > rhs; + public > boolean compare(T lhs, T rhs) { + return lhs.compareTo(rhs) > 0; } }; @@ -234,13 +229,8 @@ public boolean compare(long lhs, long rhs) { ValueSource gteVs = new ComparisonBoolFunction(const52Vs, vs, "test") { @Override - public boolean compare(double lhs, double rhs) { - return lhs >= rhs; - } - - @Override - public boolean compare(long lhs, long rhs) { - return lhs >= rhs; + public > boolean compare(T lhs, T rhs) { + return lhs.compareTo(rhs) >= 0; } }; 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 b291a2e47f8c..6bcc3961aac4 100644 --- a/solr/core/src/java/org/apache/solr/search/ValueSourceParser.java +++ b/solr/core/src/java/org/apache/solr/search/ValueSourceParser.java @@ -829,14 +829,8 @@ public ValueSource parse(FunctionQParser fp) throws SyntaxError { ValueSource rhsValSource = fp.parseValueSource(); return new ComparisonBoolFunction(lhsValSource, rhsValSource, "gt") { - @Override - public boolean compare(double lhs, double rhs) { - return lhs > rhs; - } - - @Override - public boolean compare(long lhs, long rhs) { - return lhs > rhs; + public > boolean compare(T lhs, T rhs) { + return lhs.compareTo(rhs) > 0; } }; } @@ -849,14 +843,8 @@ public ValueSource parse(FunctionQParser fp) throws SyntaxError { ValueSource rhsValSource = fp.parseValueSource(); return new ComparisonBoolFunction(lhsValSource, rhsValSource, "lt") { - @Override - public boolean compare(double lhs, double rhs) { - return lhs < rhs; - } - - @Override - public boolean compare(long lhs, long rhs) { - return lhs < rhs; + public > boolean compare(T lhs, T rhs) { + return lhs.compareTo(rhs) < 0; } }; } @@ -869,17 +857,11 @@ public ValueSource parse(FunctionQParser fp) throws SyntaxError { ValueSource rhsValSource = fp.parseValueSource(); return new ComparisonBoolFunction(lhsValSource, rhsValSource, "gte") { - @Override - public boolean compare(double lhs, double rhs) { - return lhs >= rhs; - } - - @Override - public boolean compare(long lhs, long rhs) { - return lhs >= rhs; + public > boolean compare(T lhs, T rhs) { + return lhs.compareTo(rhs) >= 0; } - }; + } }); @@ -891,14 +873,8 @@ public ValueSource parse(FunctionQParser fp) throws SyntaxError { ValueSource rhsValSource = fp.parseValueSource(); return new ComparisonBoolFunction(lhsValSource, rhsValSource, "lte") { - @Override - public boolean compare(double lhs, double rhs) { - return lhs <= rhs; - } - - @Override - public boolean compare(long lhs, long rhs) { - return lhs <= rhs; + public > boolean compare(T lhs, T rhs) { + return lhs.compareTo(rhs) <= 0; } }; } @@ -911,14 +887,8 @@ public ValueSource parse(FunctionQParser fp) throws SyntaxError { ValueSource rhsValSource = fp.parseValueSource(); return new ComparisonBoolFunction(lhsValSource, rhsValSource, "eq") { - @Override - public boolean compare(double lhs, double rhs) { - return lhs == rhs; - } - - @Override - public boolean compare(long lhs, long rhs) { - return lhs == rhs; + public > boolean compare(T lhs, T rhs) { + return lhs.compareTo(rhs) == 0; } }; } From d911d4122fe0959005b411e7776c3d6a04599a7e Mon Sep 17 00:00:00 2001 From: Doug Turnbull Date: Mon, 25 Jul 2016 15:33:19 -0400 Subject: [PATCH 16/20] Fixes copy paste error in if exprission --- .../queries/function/valuesource/ComparisonBoolFunction.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lucene/queries/src/java/org/apache/lucene/queries/function/valuesource/ComparisonBoolFunction.java b/lucene/queries/src/java/org/apache/lucene/queries/function/valuesource/ComparisonBoolFunction.java index c6985c39131b..ade65030ebfa 100644 --- a/lucene/queries/src/java/org/apache/lucene/queries/function/valuesource/ComparisonBoolFunction.java +++ b/lucene/queries/src/java/org/apache/lucene/queries/function/valuesource/ComparisonBoolFunction.java @@ -70,7 +70,7 @@ public boolean boolVal(int doc) { // // If these are integers, we need to compare them as such // as to avoid floating point precision problems - if (lhsVal.doubleVal(doc) == lhsVal.longVal(doc) && rhsVal.doubleVal(doc) == lhsVal.longVal(doc)) { + if (lhsVal.doubleVal(doc) == lhsVal.longVal(doc) && rhsVal.doubleVal(doc) == rhsVal.longVal(doc)) { return compare(lhsVal.longVal(doc), rhsVal.longVal(doc)); } From 61419fc0487389a0d0fd9090bf3ccf82289aea79 Mon Sep 17 00:00:00 2001 From: Doug Turnbull Date: Tue, 26 Jul 2016 11:56:53 -0400 Subject: [PATCH 17/20] Makes compare base class accept FunctionValues, places numeric functionality in child class --- .../valuesource/ComparisonBoolFunction.java | 16 +++------------- .../queries/function/TestValueSources.java | 4 ++-- .../apache/solr/search/ValueSourceParser.java | 10 +++++----- 3 files changed, 10 insertions(+), 20 deletions(-) diff --git a/lucene/queries/src/java/org/apache/lucene/queries/function/valuesource/ComparisonBoolFunction.java b/lucene/queries/src/java/org/apache/lucene/queries/function/valuesource/ComparisonBoolFunction.java index ade65030ebfa..de5f4eb45bf1 100644 --- a/lucene/queries/src/java/org/apache/lucene/queries/function/valuesource/ComparisonBoolFunction.java +++ b/lucene/queries/src/java/org/apache/lucene/queries/function/valuesource/ComparisonBoolFunction.java @@ -19,6 +19,7 @@ import java.io.IOException; import java.util.Map; +import java.util.function.Function; import org.apache.lucene.index.LeafReaderContext; import org.apache.lucene.queries.function.ValueSource; @@ -46,7 +47,7 @@ public ComparisonBoolFunction(ValueSource lhs, ValueSource rhs, String name) { // Perform the comparison, returning true or false - public abstract > boolean compare(T lhs, T rhs); + public abstract boolean compare(int doc, FunctionValues lhs, FunctionValues rhs); // Uniquely identify the operation (ie "gt", "lt" "gte", etc) public String name() { @@ -64,18 +65,7 @@ public FunctionValues getValues(Map context, LeafReaderContext readerContext) th return new BoolDocValues(this) { @Override public boolean boolVal(int doc) { - - // should we treat this as an integer comparison? only if doubleVal == longVal, - // indicating the two values are effectively integers. - // - // If these are integers, we need to compare them as such - // as to avoid floating point precision problems - if (lhsVal.doubleVal(doc) == lhsVal.longVal(doc) && rhsVal.doubleVal(doc) == rhsVal.longVal(doc)) { - return compare(lhsVal.longVal(doc), rhsVal.longVal(doc)); - - } - - return compare(lhsVal.doubleVal(doc), rhsVal.doubleVal(doc)); + return compare(doc, lhsVal, rhsVal); } @Override diff --git a/lucene/queries/src/test/org/apache/lucene/queries/function/TestValueSources.java b/lucene/queries/src/test/org/apache/lucene/queries/function/TestValueSources.java index 9ab251d87732..d638f1bf6600 100644 --- a/lucene/queries/src/test/org/apache/lucene/queries/function/TestValueSources.java +++ b/lucene/queries/src/test/org/apache/lucene/queries/function/TestValueSources.java @@ -217,7 +217,7 @@ public void testComparisons() throws Exception { ValueSource vs = new FloatFieldSource("float"); ValueSource const52Vs = new ConstValueSource(5.2f); - ValueSource gtVs = new ComparisonBoolFunction(const52Vs, vs, "test") { + ValueSource gtVs = new SafeNumericComparisonBoolFunction(const52Vs, vs, "test") { @Override public > boolean compare(T lhs, T rhs) { return lhs.compareTo(rhs) > 0; @@ -227,7 +227,7 @@ public > boolean compare(T lhs, T rhs) { FunctionQuery q = new FunctionQuery(gtVs); assertHits(q, new float[] {0f, 0f}); - ValueSource gteVs = new ComparisonBoolFunction(const52Vs, vs, "test") { + ValueSource gteVs = new SafeNumericComparisonBoolFunction(const52Vs, vs, "test") { @Override public > boolean compare(T lhs, T rhs) { return lhs.compareTo(rhs) >= 0; 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 6bcc3961aac4..04bef0c71b1e 100644 --- a/solr/core/src/java/org/apache/solr/search/ValueSourceParser.java +++ b/solr/core/src/java/org/apache/solr/search/ValueSourceParser.java @@ -828,7 +828,7 @@ public ValueSource parse(FunctionQParser fp) throws SyntaxError { ValueSource lhsValSource = fp.parseValueSource(); ValueSource rhsValSource = fp.parseValueSource(); - return new ComparisonBoolFunction(lhsValSource, rhsValSource, "gt") { + return new SafeNumericComparisonBoolFunction(lhsValSource, rhsValSource, "gt") { public > boolean compare(T lhs, T rhs) { return lhs.compareTo(rhs) > 0; } @@ -842,7 +842,7 @@ public ValueSource parse(FunctionQParser fp) throws SyntaxError { ValueSource lhsValSource = fp.parseValueSource(); ValueSource rhsValSource = fp.parseValueSource(); - return new ComparisonBoolFunction(lhsValSource, rhsValSource, "lt") { + return new SafeNumericComparisonBoolFunction(lhsValSource, rhsValSource, "lt") { public > boolean compare(T lhs, T rhs) { return lhs.compareTo(rhs) < 0; } @@ -856,7 +856,7 @@ public ValueSource parse(FunctionQParser fp) throws SyntaxError { ValueSource lhsValSource = fp.parseValueSource(); ValueSource rhsValSource = fp.parseValueSource(); - return new ComparisonBoolFunction(lhsValSource, rhsValSource, "gte") { + return new SafeNumericComparisonBoolFunction(lhsValSource, rhsValSource, "gte") { public > boolean compare(T lhs, T rhs) { return lhs.compareTo(rhs) >= 0; } @@ -872,7 +872,7 @@ public ValueSource parse(FunctionQParser fp) throws SyntaxError { ValueSource lhsValSource = fp.parseValueSource(); ValueSource rhsValSource = fp.parseValueSource(); - return new ComparisonBoolFunction(lhsValSource, rhsValSource, "lte") { + return new SafeNumericComparisonBoolFunction(lhsValSource, rhsValSource, "lte") { public > boolean compare(T lhs, T rhs) { return lhs.compareTo(rhs) <= 0; } @@ -886,7 +886,7 @@ public ValueSource parse(FunctionQParser fp) throws SyntaxError { ValueSource lhsValSource = fp.parseValueSource(); ValueSource rhsValSource = fp.parseValueSource(); - return new ComparisonBoolFunction(lhsValSource, rhsValSource, "eq") { + return new SafeNumericComparisonBoolFunction(lhsValSource, rhsValSource, "eq") { public > boolean compare(T lhs, T rhs) { return lhs.compareTo(rhs) == 0; } From d8ffbc9f6c2214929ce7b6e036dd2dd0b34d553a Mon Sep 17 00:00:00 2001 From: Doug Turnbull Date: Tue, 26 Jul 2016 11:58:40 -0400 Subject: [PATCH 18/20] Adds missing file --- .../SafeNumericComparisonBoolFunction.java | 47 +++++++++++++++++++ 1 file changed, 47 insertions(+) create mode 100644 lucene/queries/src/java/org/apache/lucene/queries/function/valuesource/SafeNumericComparisonBoolFunction.java diff --git a/lucene/queries/src/java/org/apache/lucene/queries/function/valuesource/SafeNumericComparisonBoolFunction.java b/lucene/queries/src/java/org/apache/lucene/queries/function/valuesource/SafeNumericComparisonBoolFunction.java new file mode 100644 index 000000000000..347e1a04b76c --- /dev/null +++ b/lucene/queries/src/java/org/apache/lucene/queries/function/valuesource/SafeNumericComparisonBoolFunction.java @@ -0,0 +1,47 @@ +package org.apache.lucene.queries.function.valuesource; + +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +import org.apache.lucene.queries.function.FunctionValues; +import org.apache.lucene.queries.function.ValueSource; +import org.apache.lucene.queries.function.docvalues.IntDocValues; +import org.apache.lucene.queries.function.docvalues.LongDocValues; + +public abstract class SafeNumericComparisonBoolFunction extends ComparisonBoolFunction { + + public SafeNumericComparisonBoolFunction(ValueSource lhs, ValueSource rhs, String name) { + super(lhs, rhs, name); + } + + public abstract > boolean compare(T lhs, T rhs); + + @Override + public boolean compare(int doc, FunctionValues lhs, FunctionValues rhs) { + // performs the safest possible numeric comparison, if both lhs and rhs are Longs, then + // we perform a Long comparison to avoid the issues with precision when casting to doubles + boolean lhsAnInt = (lhs instanceof LongDocValues || lhs instanceof IntDocValues); + boolean rhsAnInt = (rhs instanceof LongDocValues || rhs instanceof IntDocValues); + if (lhsAnInt && rhsAnInt) { + return compare(lhs.longVal(doc), rhs.longVal(doc)); + } + return compare(lhs.doubleVal(doc), rhs.doubleVal(doc)); + } + + + +} From 683da372309332d883f72c68fd4b6f1095c580d9 Mon Sep 17 00:00:00 2001 From: Doug Turnbull Date: Tue, 26 Jul 2016 16:31:53 -0400 Subject: [PATCH 19/20] Adds comparison value source comparison testing --- .../org/apache/solr/search/QueryEqualityTest.java | 11 +++++++++++ 1 file changed, 11 insertions(+) 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 2a894732ea33..4d5523a5c6fe 100644 --- a/solr/core/src/test/org/apache/solr/search/QueryEqualityTest.java +++ b/solr/core/src/test/org/apache/solr/search/QueryEqualityTest.java @@ -15,6 +15,7 @@ * limitations under the License. */ package org.apache.solr.search; +import com.facebook.presto.sql.tree.Except; import org.apache.lucene.search.Query; import org.apache.lucene.search.QueryUtils; import org.apache.solr.SolrTestCaseJ4; @@ -1075,4 +1076,14 @@ public void testAggs() throws Exception { // assertFuncEquals("agg_multistat(foo_i)", "agg_multistat(foo_i)"); } + public void testCompares() throws Exception { + assertFuncEquals("gt(foo_i,2)", "gt(foo_i, 2)"); + assertFuncEquals("gt(foo_i,2)", "gt(foo_i,2)"); + assertFuncEquals("lt(foo_i,2)", "lt(foo_i,2)"); + assertFuncEquals("lte(foo_i,2)", "lte(foo_i,2)"); + assertFuncEquals("gte(foo_i,2)", "gte(foo_i,2)"); + assertFuncEquals("eq(foo_i,2)", "eq(foo_i,2)"); + + } + } From 3d28dd32c0cfaca0fcee8bf95c4fbb995ef0e54c Mon Sep 17 00:00:00 2001 From: Doug Turnbull Date: Tue, 26 Jul 2016 16:45:03 -0400 Subject: [PATCH 20/20] Adds tests for exists and computed fields --- .../valuesource/ComparisonBoolFunction.java | 6 ++++++ .../solr/search/function/TestFunctionQuery.java | 15 +++++++++++++-- 2 files changed, 19 insertions(+), 2 deletions(-) diff --git a/lucene/queries/src/java/org/apache/lucene/queries/function/valuesource/ComparisonBoolFunction.java b/lucene/queries/src/java/org/apache/lucene/queries/function/valuesource/ComparisonBoolFunction.java index de5f4eb45bf1..47a583d383d9 100644 --- a/lucene/queries/src/java/org/apache/lucene/queries/function/valuesource/ComparisonBoolFunction.java +++ b/lucene/queries/src/java/org/apache/lucene/queries/function/valuesource/ComparisonBoolFunction.java @@ -72,6 +72,12 @@ public boolean boolVal(int doc) { public String toString(int doc) { return compLabel + "(" + lhsVal.toString(doc) + "," + rhsVal.toString(doc) + ")"; } + + @Override + public boolean exists(int doc) { + return lhsVal.exists(doc) && rhsVal.exists(doc); + } + }; } 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 dae7a435ef9f..a7f4d4fe2ba5 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 @@ -25,6 +25,7 @@ import java.util.List; import java.util.Random; +import com.facebook.presto.sql.tree.Except; import org.apache.lucene.index.FieldInvertState; import org.apache.lucene.search.similarities.Similarity; import org.apache.lucene.search.similarities.TFIDFSimilarity; @@ -794,11 +795,18 @@ public void testMissingFieldFunctionBehavior() throws Exception { } @Test - public void testNumericComparisons() { + public void testNumericComparisons() throws Exception { assertU(adoc("id", "1", "age_i", "35")); assertU(adoc("id", "2", "age_i", "25")); assertU(commit()); + // test weighting of functions + assertJQ(req("q", "id:1", "fl", "a:gt(age_i,30),b:lt(age_i,30)") + , "/response/docs/[0]=={'a':true,'b':false}"); + + assertJQ(req("q", "id:1", "fl", "a:exists(gt(foo_i,30))") + , "/response/docs/[0]=={'a':false}"); + singleTest("age_i", "if(gt(age_i,30),5,2)", /*id*/1, /*score*/5, /*id*/2, /*score*/2); @@ -807,6 +815,10 @@ public void testNumericComparisons() { /*id*/1, /*score*/2, /*id*/2, /*score*/5); + singleTest("age_i", "if(lt(age_i,34.5),5,2)", + /*id*/1, /*score*/2, + /*id*/2, /*score*/5); + singleTest("age_i", "if(lte(age_i,35),5,2)", /*id*/1, /*score*/5, /*id*/2, /*score*/5); @@ -831,7 +843,6 @@ public void testNumericComparisons() { singleTest("age_i", "if(eq(age_i,35),5,2)", /*id*/1, /*score*/5, /*id*/2, /*score*/2); - } public void testLongComparisons() {