summaryrefslogtreecommitdiffstats
path: root/container-search
diff options
context:
space:
mode:
authorArne Juul <arnej@verizonmedia.com>2020-07-14 11:03:42 +0000
committerArne Juul <arnej@verizonmedia.com>2020-07-15 15:39:24 +0000
commit9debffd31d309a7fe4062241a731d5722f4199e0 (patch)
tree53a6e0ec72e6f74f57b05736ca16290e17da0aa6 /container-search
parentc5fc3ffbbd620f50992ac978fe0dab46a0d19f4f (diff)
refactor and require unit for distance
Diffstat (limited to 'container-search')
-rw-r--r--container-search/src/main/java/com/yahoo/search/query/SelectParser.java30
-rw-r--r--container-search/src/main/java/com/yahoo/search/yql/YqlParser.java18
-rw-r--r--container-search/src/test/java/com/yahoo/search/yql/YqlParserTestCase.java8
-rw-r--r--container-search/src/test/java/com/yahoo/select/SelectTestCase.java4
4 files changed, 29 insertions, 31 deletions
diff --git a/container-search/src/main/java/com/yahoo/search/query/SelectParser.java b/container-search/src/main/java/com/yahoo/search/query/SelectParser.java
index 2476f00f194..719f16edc0a 100644
--- a/container-search/src/main/java/com/yahoo/search/query/SelectParser.java
+++ b/container-search/src/main/java/com/yahoo/search/query/SelectParser.java
@@ -3,8 +3,8 @@ package com.yahoo.search.query;
import com.google.common.base.Preconditions;
import com.yahoo.collections.LazyMap;
-import com.yahoo.geo.ParseDegree;
-import com.yahoo.geo.ParseDistance;
+import com.yahoo.geo.DistanceParser;
+import com.yahoo.geo.ParsedDegree;
import com.yahoo.language.Language;
import com.yahoo.language.process.Normalizer;
import com.yahoo.prelude.IndexFacts;
@@ -434,26 +434,24 @@ public class SelectParser implements Parser {
var arg2 = children.get(2);
var arg3 = children.get(3);
var loc = new Location();
- double radius = -1;
- if (arg3.type() == Type.STRING) {
- radius = new ParseDistance(arg3.asString()).degrees;
- } else if (arg3.type() == Type.LONG) {
- radius = new ParseDistance(String.valueOf(arg3.asLong())).degrees;
- } else {
+ if (arg3.type() != Type.STRING) {
throw new IllegalArgumentException("Invalid geoLocation radius type "+arg3.type()+" for "+arg3);
}
+ var radius = new DistanceParser(arg3.asString(), false);
if (arg1.type() == Type.STRING && arg2.type() == Type.STRING) {
- var coord_1 = new ParseDegree(true, children.get(1).asString());
- var coord_2 = new ParseDegree(false, children.get(2).asString());
- if (coord_1.foundLatitude && coord_2.foundLongitude) {
- loc.setGeoCircle(coord_1.latitude, coord_2.longitude, radius);
- } else if (coord_2.foundLatitude && coord_1.foundLongitude) {
- loc.setGeoCircle(coord_2.latitude, coord_1.longitude, radius);
+ var c1input = children.get(1).asString();
+ var c2input = children.get(2).asString();
+ var coord_1 = ParsedDegree.fromString(c1input, true, false);
+ var coord_2 = ParsedDegree.fromString(c2input, false, true);
+ if (coord_1.isLatitude && coord_2.isLongitude) {
+ loc.setGeoCircle(coord_1.degrees, coord_2.degrees, radius.degrees);
+ } else if (coord_2.isLatitude && coord_1.isLongitude) {
+ loc.setGeoCircle(coord_2.degrees, coord_1.degrees, radius.degrees);
} else {
- throw new IllegalArgumentException("Invalid geoLocation coordinates '"+coord_1+"' and '"+coord_2+"'");
+ throw new IllegalArgumentException("Invalid geoLocation coordinates '"+c1input+"' and '"+c2input+"'");
}
} else if (arg1.type() == Type.DOUBLE && arg2.type() == Type.DOUBLE) {
- loc.setGeoCircle(arg1.asDouble(), arg2.asDouble(), radius);
+ loc.setGeoCircle(arg1.asDouble(), arg2.asDouble(), radius.degrees);
} else {
throw new IllegalArgumentException("Invalid geoLocation coordinate types "+arg1.type()+" and "+arg2.type());
}
diff --git a/container-search/src/main/java/com/yahoo/search/yql/YqlParser.java b/container-search/src/main/java/com/yahoo/search/yql/YqlParser.java
index 5b77da8c3b3..b2f30944ef2 100644
--- a/container-search/src/main/java/com/yahoo/search/yql/YqlParser.java
+++ b/container-search/src/main/java/com/yahoo/search/yql/YqlParser.java
@@ -19,8 +19,8 @@ import com.google.common.annotations.Beta;
import com.google.common.base.Preconditions;
import com.yahoo.collections.LazyMap;
import com.yahoo.collections.LazySet;
-import com.yahoo.geo.ParseDegree;
-import com.yahoo.geo.ParseDistance;
+import com.yahoo.geo.DistanceParser;
+import com.yahoo.geo.ParsedDegree;
import com.yahoo.language.Language;
import com.yahoo.language.detect.Detector;
import com.yahoo.language.process.Normalizer;
@@ -424,14 +424,14 @@ public class YqlParser implements Parser {
List<OperatorNode<ExpressionOperator>> args = ast.getArgument(1);
Preconditions.checkArgument(args.size() == 4, "Expected 4 arguments, got %s.", args.size());
String field = fetchFieldRead(args.get(0));
- var coord_1 = new ParseDegree(true, fetchFieldRead(args.get(1)));
- var coord_2 = new ParseDegree(false, fetchFieldRead(args.get(2)));
- var radius = new ParseDistance(fetchFieldRead(args.get(3)));
+ var coord_1 = ParsedDegree.fromString(fetchFieldRead(args.get(1)), true, false);
+ var coord_2 = ParsedDegree.fromString(fetchFieldRead(args.get(2)), false, true);
+ var radius = new DistanceParser(fetchFieldRead(args.get(3)), false);
var loc = new Location();
- if (coord_1.foundLatitude && coord_2.foundLongitude) {
- loc.setGeoCircle(coord_1.latitude, coord_2.longitude, radius.degrees);
- } else if (coord_2.foundLatitude && coord_1.foundLongitude) {
- loc.setGeoCircle(coord_2.latitude, coord_1.longitude, radius.degrees);
+ if (coord_1.isLatitude && coord_2.isLongitude) {
+ loc.setGeoCircle(coord_1.degrees, coord_2.degrees, radius.degrees);
+ } else if (coord_2.isLatitude && coord_1.isLongitude) {
+ loc.setGeoCircle(coord_2.degrees, coord_1.degrees, radius.degrees);
} else {
throw new IllegalArgumentException("Invalid geoLocation coordinates '"+coord_1+"' and '"+coord_2+"'");
}
diff --git a/container-search/src/test/java/com/yahoo/search/yql/YqlParserTestCase.java b/container-search/src/test/java/com/yahoo/search/yql/YqlParserTestCase.java
index 75199eeec55..62a9e27cd96 100644
--- a/container-search/src/test/java/com/yahoo/search/yql/YqlParserTestCase.java
+++ b/container-search/src/test/java/com/yahoo/search/yql/YqlParserTestCase.java
@@ -549,7 +549,7 @@ public class YqlParserTestCase {
@Test
public void testGeoLocation() {
- assertParse("select foo from bar where geoLocation(workplace, 63.418417, 10.433033, 500000);",
+ assertParse("select foo from bar where geoLocation(workplace, 63.418417, 10.433033, \"0.5 deg\");",
"GEO_LOCATION workplace:(2,10433033,63418417,500000,0,1,0,1921876103)");
assertParse("select foo from bar where geoLocation(headquarters, \"37.416383\", \"-122.024683\", \"100 miles\");",
"GEO_LOCATION headquarters:(2,-122024683,37416383,1450561,0,1,0,3411238761)");
@@ -558,10 +558,10 @@ public class YqlParserTestCase {
assertParseFail("select foo from bar where geoLocation(qux, 1, 2);",
new IllegalArgumentException("Expected 4 arguments, got 3."));
- assertParseFail("select foo from bar where geoLocation(qux, 1.0, \"N1.0\", 100);",
+ assertParseFail("select foo from bar where geoLocation(qux, 2.0, \"N5.0\", \"0.5 deg\");",
new IllegalArgumentException(
- "Invalid geoLocation coordinates '1.0 -> latitude(1.0)' and 'N1.0 -> latitude(1.0)'"));
- assertParse("select foo from bar where geoLocation(workplace, -12, -34, -77);",
+ "Invalid geoLocation coordinates 'Latitude: 2.0 degrees' and 'Latitude: 5.0 degrees'"));
+ assertParse("select foo from bar where geoLocation(workplace, -12, -34, \"-77 d\");",
"GEO_LOCATION workplace:(2,-34000000,-12000000,-1,0,1,0,4201111954)");
}
diff --git a/container-search/src/test/java/com/yahoo/select/SelectTestCase.java b/container-search/src/test/java/com/yahoo/select/SelectTestCase.java
index af1c1e48011..f297fd69f24 100644
--- a/container-search/src/test/java/com/yahoo/select/SelectTestCase.java
+++ b/container-search/src/test/java/com/yahoo/select/SelectTestCase.java
@@ -523,13 +523,13 @@ public class SelectTestCase {
@Test
public void testGeoLocation() {
- assertParse("{ \"geoLocation\": [ \"workplace\", 63.418417, 10.433033, 500000 ] }",
+ assertParse("{ \"geoLocation\": [ \"workplace\", 63.418417, 10.433033, \"0.5 deg\" ] }",
"GEO_LOCATION workplace:(2,10433033,63418417,500000,0,1,0,1921876103)");
assertParse("{ \"geoLocation\": [ \"headquarters\", \"37.416383\", \"-122.024683\", \"100 miles\" ] }",
"GEO_LOCATION headquarters:(2,-122024683,37416383,1450561,0,1,0,3411238761)");
assertParse("{ \"geoLocation\": [ \"home\", \"E10.433033\", \"N63.418417\", \"5km\" ] }",
"GEO_LOCATION home:(2,10433033,63418417,45066,0,1,0,1921876103)");
- assertParse("{ \"geoLocation\": [ \"workplace\", -12.0, -34.0, -77 ] }",
+ assertParse("{ \"geoLocation\": [ \"workplace\", -12.0, -34.0, \"-77 deg\" ] }",
"GEO_LOCATION workplace:(2,-34000000,-12000000,-1,0,1,0,4201111954)");
}