drill-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From par...@apache.org
Subject [11/12] drill git commit: DRILL-5962: Add function STAsJSON to extend GIS support
Date Wed, 22 Nov 2017 22:52:13 GMT
DRILL-5962: Add function STAsJSON to extend GIS support

This closes #1036


Project: http://git-wip-us.apache.org/repos/asf/drill/repo
Commit: http://git-wip-us.apache.org/repos/asf/drill/commit/46ca206e
Tree: http://git-wip-us.apache.org/repos/asf/drill/tree/46ca206e
Diff: http://git-wip-us.apache.org/repos/asf/drill/diff/46ca206e

Branch: refs/heads/master
Commit: 46ca206e612f57811cae44372824933b852fbf28
Parents: b8598b6
Author: chris <chris@thinkdataworks.com>
Authored: Tue Nov 14 16:26:21 2017 -0500
Committer: Parth Chandra <parthc@apache.org>
Committed: Wed Nov 22 11:03:37 2017 -0800

----------------------------------------------------------------------
 .../drill/exec/expr/fn/impl/gis/STAsJson.java   | 64 ++++++++++++++++++++
 .../expr/fn/impl/gis/TestGeometryFunctions.java | 25 ++++++++
 2 files changed, 89 insertions(+)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/drill/blob/46ca206e/contrib/gis/src/main/java/org/apache/drill/exec/expr/fn/impl/gis/STAsJson.java
----------------------------------------------------------------------
diff --git a/contrib/gis/src/main/java/org/apache/drill/exec/expr/fn/impl/gis/STAsJson.java
b/contrib/gis/src/main/java/org/apache/drill/exec/expr/fn/impl/gis/STAsJson.java
new file mode 100644
index 0000000..ddc1865
--- /dev/null
+++ b/contrib/gis/src/main/java/org/apache/drill/exec/expr/fn/impl/gis/STAsJson.java
@@ -0,0 +1,64 @@
+/*
+ * 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.
+ */
+
+/*
+ * Wrapper for ESRI ST_AsJson to convert geometry into REST Json.
+ * Emulates functionality from spatial-framework-for-hadoop.
+ */
+package org.apache.drill.exec.expr.fn.impl.gis;
+
+import javax.inject.Inject;
+
+import org.apache.drill.exec.expr.DrillSimpleFunc;
+import org.apache.drill.exec.expr.annotations.FunctionTemplate;
+import org.apache.drill.exec.expr.annotations.Output;
+import org.apache.drill.exec.expr.annotations.Param;
+import org.apache.drill.exec.expr.holders.VarBinaryHolder;
+import org.apache.drill.exec.expr.holders.VarCharHolder;
+
+import io.netty.buffer.DrillBuf;
+
+@FunctionTemplate(name = "st_asjson", scope = FunctionTemplate.FunctionScope.SIMPLE,
+  nulls = FunctionTemplate.NullHandling.NULL_IF_NULL)
+public class STAsJson implements DrillSimpleFunc {
+  @Param
+  VarBinaryHolder geomParam;
+
+  @Output
+  VarCharHolder out;
+
+  @Inject
+  DrillBuf buffer;
+
+  public void setup() {
+  }
+
+  public void eval() {
+    com.esri.core.geometry.ogc.OGCGeometry geom = com.esri.core.geometry.ogc.OGCGeometry
+      .fromBinary(geomParam.buffer.nioBuffer(geomParam.start, geomParam.end - geomParam.start));
+
+    String json = geom.asJson();
+    byte[] jsonBytes = json.getBytes();
+    int outputSize = jsonBytes.length;
+
+    buffer = out.buffer = buffer.reallocIfNeeded(outputSize);
+    out.start = 0;
+    out.end = outputSize;
+    buffer.setBytes(0, jsonBytes);
+  }
+}

http://git-wip-us.apache.org/repos/asf/drill/blob/46ca206e/contrib/gis/src/test/java/org/apache/drill/exec/expr/fn/impl/gis/TestGeometryFunctions.java
----------------------------------------------------------------------
diff --git a/contrib/gis/src/test/java/org/apache/drill/exec/expr/fn/impl/gis/TestGeometryFunctions.java
b/contrib/gis/src/test/java/org/apache/drill/exec/expr/fn/impl/gis/TestGeometryFunctions.java
index e52291b..b2d4c6f 100644
--- a/contrib/gis/src/test/java/org/apache/drill/exec/expr/fn/impl/gis/TestGeometryFunctions.java
+++ b/contrib/gis/src/test/java/org/apache/drill/exec/expr/fn/impl/gis/TestGeometryFunctions.java
@@ -23,6 +23,7 @@ import org.junit.Test;
 public class TestGeometryFunctions extends BaseTestQuery {
 
   String wktPoint = "POINT (-121.895 37.339)";
+  String json = "{\"x\":-121.895,\"y\":37.339,\"spatialReference\":{\"wkid\":4326}}";
   String geoJson = "{\"type\":\"Point\",\"coordinates\":[-121.895,37.339],"
     + "\"crs\":{\"type\":\"name\",\"properties\":{\"name\":\"EPSG:4326\"}}}";
 
@@ -51,6 +52,30 @@ public class TestGeometryFunctions extends BaseTestQuery {
   }
 
   @Test
+  public void testJSONFromPointCreation() throws Exception {
+    testBuilder()
+      .sqlQuery("select ST_AsJson(ST_Point(-121.895, 37.339)) "
+        + "from cp.`/sample-data/CA-cities.csv` limit 1")
+      .ordered()
+      .baselineColumns("EXPR$0")
+      .baselineValues(json)
+      .build()
+      .run();
+  }
+
+  @Test
+  public void testJSONFromTextCreation() throws Exception {
+    testBuilder()
+      .sqlQuery("select ST_AsJson(ST_GeomFromText('" + wktPoint + "')) "
+        + "from cp.`/sample-data/CA-cities.csv` limit 1")
+      .ordered()
+      .baselineColumns("EXPR$0")
+      .baselineValues(json)
+      .build()
+      .run();
+  }
+
+  @Test
   public void testGeoJSONCreationFromPoint() throws Exception {
     testBuilder()
       .sqlQuery("select ST_AsGeoJSON(ST_Point(-121.895, 37.339)) "


Mime
View raw message