You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@sedona.apache.org by ji...@apache.org on 2024/01/05 07:18:51 UTC
(sedona) 01/01: Add overloaded functions in UDFsV2
This is an automated email from the ASF dual-hosted git repository.
jiayu pushed a commit to branch snowflake-geojson-overload
in repository https://gitbox.apache.org/repos/asf/sedona.git
commit 3f4708777f7c3595889b072785be4c777b969668
Author: Jia Yu <ji...@apache.org>
AuthorDate: Thu Jan 4 23:18:34 2024 -0800
Add overloaded functions in UDFsV2
---
.../sedona/snowflake/snowsql/GeometrySerde.java | 33 +
.../org/apache/sedona/snowflake/snowsql/UDFs.java | 17 +-
.../apache/sedona/snowflake/snowsql/UDFsV2.java | 974 +++++++++++++++++++++
.../snowsql/annotations/UDFAnnotations.java | 3 +-
.../snowflake/snowsql/ddl/ArgSpecBuilder.java | 22 +-
.../sedona/snowflake/snowsql/ddl/Constants.java | 2 +
.../sedona/snowflake/snowsql/ddl/DDLGenerator.java | 2 +-
.../snowflake/snowsql/ddl/UDFDDLGenerator.java | 32 +-
.../snowflake/snowsql/ddl/UDTFDDLGenerator.java | 6 +-
9 files changed, 1066 insertions(+), 25 deletions(-)
diff --git a/snowflake/src/main/java/org/apache/sedona/snowflake/snowsql/GeometrySerde.java b/snowflake/src/main/java/org/apache/sedona/snowflake/snowsql/GeometrySerde.java
index af777f26f..426507708 100644
--- a/snowflake/src/main/java/org/apache/sedona/snowflake/snowsql/GeometrySerde.java
+++ b/snowflake/src/main/java/org/apache/sedona/snowflake/snowsql/GeometrySerde.java
@@ -15,6 +15,8 @@ package org.apache.sedona.snowflake.snowsql;
import org.apache.sedona.common.Functions;
+import org.apache.sedona.common.enums.FileDataSplitter;
+import org.apache.sedona.common.utils.FormatUtils;
import org.apache.sedona.common.utils.GeomUtils;
import org.locationtech.jts.geom.Geometry;
import org.locationtech.jts.geom.GeometryCollection;
@@ -32,6 +34,14 @@ public class GeometrySerde {
return Functions.asEWKB(geom);
}
+ public static String serGeoJson(Geometry geom) {
+ return Functions.asGeoJson(geom);
+ }
+
+ public static String serGeoJson(Geometry[] geoms) {
+ return Functions.asGeoJson(Functions.createMultiGeometry(geoms));
+ }
+
public static byte[] serialize(Geometry[] geoms) {
return serialize(Functions.createMultiGeometry(geoms));
}
@@ -45,6 +55,29 @@ public class GeometrySerde {
}
}
+ public static Geometry deserGeoJson(String geoJson) {
+ FormatUtils<Geometry> formatUtils = new FormatUtils<>(FileDataSplitter.GEOJSON, false);
+ try {
+ return formatUtils.readGeometry(geoJson);
+ }
+ catch (ParseException e) {
+ throw new RuntimeException(e);
+ }
+ }
+
+ public static Geometry[] deserGeoJson2List(String geoJson) {
+ FormatUtils<Geometry> formatUtils = new FormatUtils<>(FileDataSplitter.GEOJSON, false);
+ Geometry geom;
+ try {
+ geom = formatUtils.readGeometry(geoJson);
+ }
+ catch (ParseException e) {
+ throw new RuntimeException(e);
+ }
+ assert geom instanceof GeometryCollection;
+ return GeomUtils.getSubGeometries(geom);
+ }
+
public static Geometry[] deserialize2List(byte[] bytes) {
Geometry geom = GeometrySerde.deserialize(bytes);
assert geom instanceof GeometryCollection;
diff --git a/snowflake/src/main/java/org/apache/sedona/snowflake/snowsql/UDFs.java b/snowflake/src/main/java/org/apache/sedona/snowflake/snowsql/UDFs.java
index a925cc6fb..2752a55d5 100644
--- a/snowflake/src/main/java/org/apache/sedona/snowflake/snowsql/UDFs.java
+++ b/snowflake/src/main/java/org/apache/sedona/snowflake/snowsql/UDFs.java
@@ -21,7 +21,6 @@ import org.apache.sedona.common.sphere.Haversine;
import org.apache.sedona.common.sphere.Spheroid;
import org.apache.sedona.snowflake.snowsql.annotations.UDFAnnotations;
import org.locationtech.jts.geom.Geometry;
-import org.locationtech.jts.geom.GeometryCollection;
import org.locationtech.jts.geom.Point;
import org.locationtech.jts.io.ParseException;
import org.locationtech.jts.io.WKBWriter;
@@ -30,6 +29,12 @@ import org.xml.sax.SAXException;
import javax.xml.parsers.ParserConfigurationException;
import java.io.IOException;
+/**
+ * User defined functions for Apache Sedona
+ * All functions in this class takes a WKB binary as input and return a WKB binary as output.
+ * These functions can interact with Snowflake native functions but users must manually convert
+ * Snowflake native geometry type to WKB using ST_AsWKB (geom to WKB) and to_geometry (wkb to geom) function.
+ */
public class UDFs {
@UDFAnnotations.ParamMeta(argNames = {"linestring", "point", "position"})
@@ -734,6 +739,16 @@ public class UDFs {
);
}
+ @UDFAnnotations.ParamMeta(argNames = {"geometry", "precisionScale"})
+ public static byte[] ST_ReducePrecision(byte[] geometry, int precisionScale) {
+ return GeometrySerde.serialize(
+ Functions.reducePrecision(
+ GeometrySerde.deserialize(geometry),
+ precisionScale
+ )
+ );
+ }
+
@UDFAnnotations.ParamMeta(argNames = {"linestring"})
public static byte[] ST_RemovePoint(byte[] linestring) {
return GeometrySerde.serialize(
diff --git a/snowflake/src/main/java/org/apache/sedona/snowflake/snowsql/UDFsV2.java b/snowflake/src/main/java/org/apache/sedona/snowflake/snowsql/UDFsV2.java
new file mode 100644
index 000000000..2d1494429
--- /dev/null
+++ b/snowflake/src/main/java/org/apache/sedona/snowflake/snowsql/UDFsV2.java
@@ -0,0 +1,974 @@
+/**
+ * Licensed 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
+ * <p>
+ * http://www.apache.org/licenses/LICENSE-2.0
+ * <p>
+ * 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.
+ */
+package org.apache.sedona.snowflake.snowsql;
+
+import org.apache.sedona.common.Functions;
+import org.apache.sedona.common.Predicates;
+import org.apache.sedona.common.sphere.Haversine;
+import org.apache.sedona.common.sphere.Spheroid;
+import org.apache.sedona.snowflake.snowsql.annotations.UDFAnnotations;
+import org.locationtech.jts.geom.Geometry;
+import org.locationtech.jts.geom.Point;
+
+import java.io.IOException;
+
+/**
+ * User defined functions for Apache Sedona
+ * This class is used to generate DDL for Snowflake UDFs
+ * Different from UDFs.java, this class only contains functions can directly interact with Snowflake
+ * native functions. This means no Constructors (ST_GeomFromXXX).
+ * This requires all functions must take a GeoJSON string as input and return a GeoJSON string as output.
+ */
+public class UDFsV2
+{
+ @UDFAnnotations.ParamMeta(argNames = {"linestring", "point", "position"},
+ argTypes = {"Geometry", "Geometry", "int"},
+ returnTypes = "Geometry")
+ public static String ST_AddPoint(String linestring, String point, int position) {
+ return GeometrySerde.serGeoJson(
+ Functions.addPoint(
+ GeometrySerde.deserGeoJson(linestring),
+ GeometrySerde.deserGeoJson(point),
+ position
+ )
+ );
+ }
+
+ @UDFAnnotations.ParamMeta(argNames = {"geometry"},
+ argTypes = {"Geometry"})
+ public static double ST_Area(String geometry) {
+ return Functions.area(
+ GeometrySerde.deserGeoJson(geometry)
+ );
+ }
+
+ @UDFAnnotations.ParamMeta(argNames = {"geometry"},
+ argTypes = {"Geometry"})
+ public static byte[] ST_AsBinary(String geometry) {
+ return Functions.asWKB(
+ GeometrySerde.deserGeoJson(geometry)
+ );
+ }
+
+ @UDFAnnotations.ParamMeta(argNames = {"geometry"},
+ argTypes = {"Geometry"})
+ public static byte[] ST_AsEWKB(String geometry) {
+ return Functions.asEWKB(
+ GeometrySerde.deserGeoJson(geometry)
+ );
+ }
+
+ @UDFAnnotations.ParamMeta(argNames = {"geometry"},
+ argTypes = {"Geometry"})
+ public static String ST_AsEWKT(String geometry) {
+ return Functions.asEWKT(
+ GeometrySerde.deserGeoJson(geometry)
+ );
+ }
+
+ @UDFAnnotations.ParamMeta(argNames = {"geometry"},
+ argTypes = {"Geometry"})
+ public static String ST_AsGML(String geometry) {
+ return Functions.asGML(
+ GeometrySerde.deserGeoJson(geometry)
+ );
+ }
+
+ @UDFAnnotations.ParamMeta(argNames = {"geometry"},
+ argTypes = {"Geometry"})
+ public static String ST_AsGeoJSON(String geometry) {
+ return Functions.asGeoJson(
+ GeometrySerde.deserGeoJson(geometry)
+ );
+ }
+
+ @UDFAnnotations.ParamMeta(argNames = {"geometry"},
+ argTypes = {"Geometry"})
+ public static String ST_AsKML(String geometry) {
+ return Functions.asKML(
+ GeometrySerde.deserGeoJson(geometry)
+ );
+ }
+
+ @UDFAnnotations.ParamMeta(argNames = {"left", "right"},
+ argTypes = {"Geometry", "Geometry"})
+ public static double ST_Azimuth(String left, String right) {
+ return Functions.azimuth(
+ GeometrySerde.deserGeoJson(left),
+ GeometrySerde.deserGeoJson(right)
+ );
+ }
+
+ @UDFAnnotations.ParamMeta(argNames = {"geometry"},
+ argTypes = {"Geometry"},
+ returnTypes = "Geometry")
+ public static String ST_Boundary(String geometry) {
+ return GeometrySerde.serGeoJson(
+ Functions.boundary(
+ GeometrySerde.deserGeoJson(geometry)
+ )
+ );
+ }
+
+ @UDFAnnotations.ParamMeta(argNames = {"geometry", "radius"}, argTypes = {"Geometry", "double"}, returnTypes = "Geometry")
+ public static String ST_Buffer(String geometry, double radius) {
+ return GeometrySerde.serGeoJson(
+ Functions.buffer(
+ GeometrySerde.deserGeoJson(geometry),
+ radius
+ )
+ );
+ }
+
+ @UDFAnnotations.ParamMeta(argNames = {"geometry"}, argTypes = {"Geometry"}, returnTypes = "Geometry")
+ public static String ST_BuildArea(String geometry) {
+ return GeometrySerde.serGeoJson(
+ Functions.buildArea(
+ GeometrySerde.deserGeoJson(geometry)
+ )
+ );
+ }
+
+ @UDFAnnotations.ParamMeta(argNames = {"geometry"}, argTypes = {"Geometry"}, returnTypes = "Geometry")
+ public static String ST_Centroid(String geometry) {
+ return GeometrySerde.serGeoJson(
+ Functions.getCentroid(
+ GeometrySerde.deserGeoJson(geometry)
+ )
+ );
+ }
+
+ @UDFAnnotations.ParamMeta(argNames = {"geometry"}, argTypes = {"Geometry"}, returnTypes = "Geometry")
+ public static String ST_CollectionExtract(String geometry) throws IOException {
+ return GeometrySerde.serGeoJson(
+ Functions.collectionExtract(
+ GeometrySerde.deserGeoJson(geometry)
+ )
+ );
+ }
+
+ @UDFAnnotations.ParamMeta(argNames = {"geometry", "geomType"}, argTypes = {"Geometry", "int"}, returnTypes = "Geometry")
+ public static String ST_CollectionExtract(String geometry, int geomType) throws IOException {
+ return GeometrySerde.serGeoJson(
+ Functions.collectionExtract(
+ GeometrySerde.deserGeoJson(geometry),
+ geomType
+ )
+ );
+ }
+
+ @UDFAnnotations.ParamMeta(argNames = {"geometry", "pctConvex"}, argTypes = {"Geometry", "double"}, returnTypes = "Geometry")
+ public static String ST_ConcaveHull(String geometry, double pctConvex) {
+ return GeometrySerde.serGeoJson(
+ Functions.concaveHull(
+ GeometrySerde.deserGeoJson(geometry),
+ pctConvex,
+ false
+ )
+ );
+ }
+
+ @UDFAnnotations.ParamMeta(argNames = {"geometry", "pctConvex", "allowHoles"}, argTypes = {"Geometry", "double", "boolean"}, returnTypes = "Geometry")
+ public static String ST_ConcaveHull(String geometry, double pctConvex, boolean allowHoles) {
+ return GeometrySerde.serGeoJson(
+ Functions.concaveHull(
+ GeometrySerde.deserGeoJson(geometry),
+ pctConvex,
+ allowHoles
+ )
+ );
+ }
+
+ @UDFAnnotations.ParamMeta(argNames = {"leftGeometry", "rightGeometry"}, argTypes = {"Geometry", "Geometry"})
+ public static boolean ST_Contains(String leftGeometry, String rightGeometry) {
+ return Predicates.contains(
+ GeometrySerde.deserGeoJson(leftGeometry),
+ GeometrySerde.deserGeoJson(rightGeometry)
+ );
+ }
+
+ @UDFAnnotations.ParamMeta(argNames = {"geometry"}, argTypes = {"Geometry"}, returnTypes = "Geometry")
+ public static String ST_ConvexHull(String geometry) {
+ return GeometrySerde.serGeoJson(
+ Functions.convexHull(
+ GeometrySerde.deserGeoJson(geometry)
+ )
+ );
+ }
+
+ @UDFAnnotations.ParamMeta(argNames = {"leftGeometry", "rightGeometry"}, argTypes = {"Geometry", "Geometry"})
+ public static boolean ST_CoveredBy(String leftGeometry, String rightGeometry) {
+ return Predicates.coveredBy(
+ GeometrySerde.deserGeoJson(leftGeometry),
+ GeometrySerde.deserGeoJson(rightGeometry)
+ );
+ }
+
+ @UDFAnnotations.ParamMeta(argNames = {"leftGeometry", "rightGeometry"}, argTypes = {"Geometry", "Geometry"})
+ public static boolean ST_Covers(String leftGeometry, String rightGeometry) {
+ return Predicates.covers(
+ GeometrySerde.deserGeoJson(leftGeometry),
+ GeometrySerde.deserGeoJson(rightGeometry)
+ );
+ }
+
+ @UDFAnnotations.ParamMeta(argNames = {"leftGeometry", "rightGeometry"}, argTypes = {"Geometry", "Geometry"})
+ public static boolean ST_Crosses(String leftGeometry, String rightGeometry) {
+ return Predicates.crosses(
+ GeometrySerde.deserGeoJson(leftGeometry),
+ GeometrySerde.deserGeoJson(rightGeometry)
+ );
+ }
+
+ @UDFAnnotations.ParamMeta(argNames = {"leftGeometry", "rightGeometry"}, argTypes = {"Geometry", "Geometry"}, returnTypes = "Geometry")
+ public static String ST_Difference(String leftGeometry, String rightGeometry) {
+ return GeometrySerde.serGeoJson(
+ Functions.difference(
+ GeometrySerde.deserGeoJson(leftGeometry),
+ GeometrySerde.deserGeoJson(rightGeometry)
+ )
+ );
+ }
+
+ @UDFAnnotations.ParamMeta(argNames = {"leftGeometry", "rightGeometry"}, argTypes = {"Geometry", "Geometry"})
+ public static boolean ST_Disjoint(String leftGeometry, String rightGeometry) {
+ return Predicates.disjoint(
+ GeometrySerde.deserGeoJson(leftGeometry),
+ GeometrySerde.deserGeoJson(rightGeometry)
+ );
+ }
+
+ @UDFAnnotations.ParamMeta(argNames = {"left", "right"}, argTypes = {"Geometry", "Geometry"})
+ public static double ST_Distance(String left, String right) {
+ return Functions.distance(
+ GeometrySerde.deserGeoJson(left),
+ GeometrySerde.deserGeoJson(right)
+ );
+ }
+
+ @UDFAnnotations.ParamMeta(argNames = {"left", "right"}, argTypes = {"Geometry", "Geometry"})
+ public static double ST_3DDistance(String left, String right) {
+ return Functions.distance3d(
+ GeometrySerde.deserGeoJson(left),
+ GeometrySerde.deserGeoJson(right)
+ );
+ }
+
+ @UDFAnnotations.ParamMeta(argNames = {"geometry"}, argTypes = {"Geometry"}, returnTypes = "Geometry")
+ public static String ST_DumpPoints(String geometry) {
+ Geometry[] points = Functions.dumpPoints(
+ GeometrySerde.deserGeoJson(geometry)
+ );
+ return GeometrySerde.serGeoJson(
+ GeometrySerde.GEOMETRY_FACTORY.createMultiPoint((Point[]) points)
+ );
+ }
+
+ @UDFAnnotations.ParamMeta(argNames = {"geometry"}, argTypes = {"Geometry"}, returnTypes = "Geometry")
+ public static String ST_EndPoint(String geometry) {
+ return GeometrySerde.serGeoJson(
+ Functions.endPoint(
+ GeometrySerde.deserGeoJson(geometry)
+ )
+ );
+ }
+
+ @UDFAnnotations.ParamMeta(argNames = {"geometry"}, argTypes = {"Geometry"}, returnTypes = "Geometry")
+ public static String ST_Envelope(String geometry) {
+ return GeometrySerde.serGeoJson(
+ Functions.envelope(
+ GeometrySerde.deserGeoJson(geometry)
+ )
+ );
+ }
+
+ @UDFAnnotations.ParamMeta(argNames = {"leftGeometry", "rightGeometry"}, argTypes = {"Geometry", "Geometry"})
+ public static boolean ST_Equals(String leftGeometry, String rightGeometry) {
+ return Predicates.equals(
+ GeometrySerde.deserGeoJson(leftGeometry),
+ GeometrySerde.deserGeoJson(rightGeometry)
+ );
+ }
+
+ @UDFAnnotations.ParamMeta(argNames = {"geometry"}, argTypes = {"Geometry"}, returnTypes = "Geometry")
+ public static String ST_ExteriorRing(String geometry) {
+ return GeometrySerde.serGeoJson(
+ Functions.exteriorRing(
+ GeometrySerde.deserGeoJson(geometry)
+ )
+ );
+ }
+
+ @UDFAnnotations.ParamMeta(argNames = {"geometry"}, argTypes = {"Geometry"}, returnTypes = "Geometry")
+ public static String ST_FlipCoordinates(String geometry) {
+ return GeometrySerde.serGeoJson(
+ Functions.flipCoordinates(
+ GeometrySerde.deserGeoJson(geometry)
+ )
+ );
+ }
+
+ @UDFAnnotations.ParamMeta(argNames = {"geometry"}, argTypes = {"Geometry"}, returnTypes = "Geometry")
+ public static String ST_Force_2D(String geometry) {
+ return GeometrySerde.serGeoJson(
+ Functions.force2D(
+ GeometrySerde.deserGeoJson(geometry)
+ )
+ );
+ }
+
+ @UDFAnnotations.ParamMeta(argNames = {"geometry", "precision"}, argTypes = {"Geometry", "int"})
+ public static String ST_GeoHash(String geometry, int precision) {
+ return Functions.geohash(
+ GeometrySerde.deserGeoJson(geometry),
+ precision
+ );
+ }
+
+ @UDFAnnotations.ParamMeta(argNames = {"geometry", "n"}, argTypes = {"Geometry", "int"}, returnTypes = "Geometry")
+ public static String ST_GeometryN(String geometry, int n) {
+ return GeometrySerde.serGeoJson(
+ Functions.geometryN(
+ GeometrySerde.deserGeoJson(geometry),
+ n
+ )
+ );
+ }
+
+ @UDFAnnotations.ParamMeta(argNames = {"geometry"}, argTypes = {"Geometry"})
+ public static String ST_GeometryType(String geometry) {
+ return Functions.geometryType(
+ GeometrySerde.deserGeoJson(geometry)
+ );
+ }
+
+ @UDFAnnotations.ParamMeta(argNames = {"geometry", "n"}, argTypes = {"Geometry", "int"}, returnTypes = "Geometry")
+ public static String ST_InteriorRingN(String geometry, int n) {
+ return GeometrySerde.serGeoJson(
+ Functions.interiorRingN(
+ GeometrySerde.deserGeoJson(geometry),
+ n
+ )
+ );
+ }
+
+ @UDFAnnotations.ParamMeta(argNames = {"leftGeometry", "rightGeometry"}, argTypes = {"Geometry", "Geometry"}, returnTypes = "Geometry")
+ public static String ST_Intersection(String leftGeometry, String rightGeometry) {
+ return GeometrySerde.serGeoJson(
+ Functions.intersection(
+ GeometrySerde.deserGeoJson(leftGeometry),
+ GeometrySerde.deserGeoJson(rightGeometry)
+ )
+ );
+ }
+
+ @UDFAnnotations.ParamMeta(argNames = {"leftGeometry", "rightGeometry"}, argTypes = {"Geometry", "Geometry"})
+ public static boolean ST_Intersects(String leftGeometry, String rightGeometry) {
+ return Predicates.intersects(
+ GeometrySerde.deserGeoJson(leftGeometry),
+ GeometrySerde.deserGeoJson(rightGeometry)
+ );
+ }
+
+ @UDFAnnotations.ParamMeta(argNames = {"geometry"}, argTypes = {"Geometry"})
+ public static boolean ST_IsClosed(String geometry) {
+ return Functions.isClosed(
+ GeometrySerde.deserGeoJson(geometry)
+ );
+ }
+
+ @UDFAnnotations.ParamMeta(argNames = {"geometry"}, argTypes = {"Geometry"})
+ public static boolean ST_IsEmpty(String geometry) {
+ return Functions.isEmpty(
+ GeometrySerde.deserGeoJson(geometry)
+ );
+ }
+
+ @UDFAnnotations.ParamMeta(argNames = {"geometry"}, argTypes = {"Geometry"})
+ public static boolean ST_IsRing(String geometry) {
+ return Functions.isRing(
+ GeometrySerde.deserGeoJson(geometry)
+ );
+ }
+
+ @UDFAnnotations.ParamMeta(argNames = {"geometry"}, argTypes = {"Geometry"})
+ public static boolean ST_IsSimple(String geometry) {
+ return Functions.isSimple(
+ GeometrySerde.deserGeoJson(geometry)
+ );
+ }
+
+ @UDFAnnotations.ParamMeta(argNames = {"geometry"}, argTypes = {"Geometry"})
+ public static boolean ST_IsValid(String geometry) {
+ return Functions.isValid(
+ GeometrySerde.deserGeoJson(geometry)
+ );
+ }
+
+ @UDFAnnotations.ParamMeta(argNames = {"geometry"}, argTypes = {"Geometry"})
+ public static double ST_Length(String geometry) {
+ return Functions.length(
+ GeometrySerde.deserGeoJson(geometry)
+ );
+ }
+
+ @UDFAnnotations.ParamMeta(argNames = {"geometry"}, argTypes = {"Geometry"}, returnTypes = "Geometry")
+ public static String ST_LineFromMultiPoint(String geometry) {
+ return GeometrySerde.serGeoJson(
+ Functions.lineFromMultiPoint(
+ GeometrySerde.deserGeoJson(geometry)
+ )
+ );
+ }
+
+ @UDFAnnotations.ParamMeta(argNames = {"geom", "fraction"}, argTypes = {"Geometry", "double"}, returnTypes = "Geometry")
+ public static String ST_LineInterpolatePoint(String geom, double fraction) {
+ return GeometrySerde.serGeoJson(
+ Functions.lineInterpolatePoint(
+ GeometrySerde.deserGeoJson(geom),
+ fraction
+ )
+ );
+ }
+
+ @UDFAnnotations.ParamMeta(argNames = {"geometry"}, argTypes = {"Geometry"}, returnTypes = "Geometry")
+ public static String ST_LineMerge(String geometry) {
+ return GeometrySerde.serGeoJson(
+ Functions.lineMerge(
+ GeometrySerde.deserGeoJson(geometry)
+ )
+ );
+ }
+
+ @UDFAnnotations.ParamMeta(argNames = {"geom", "fromFraction", "toFraction"}, argTypes = {"Geometry", "double", "double"}, returnTypes = "Geometry")
+ public static String ST_LineSubstring(String geom, double fromFraction, double toFraction) {
+ return GeometrySerde.serGeoJson(
+ Functions.lineSubString(
+ GeometrySerde.deserGeoJson(geom),
+ fromFraction,
+ toFraction
+ )
+ );
+ }
+
+ @UDFAnnotations.ParamMeta(argNames = {"shell"}, argTypes = {"Geometry"}, returnTypes = "Geometry")
+ public static String ST_MakePolygon(String shell) {
+ return GeometrySerde.serGeoJson(
+ Functions.makePolygon(
+ GeometrySerde.deserGeoJson(shell),
+ null
+ )
+ );
+ }
+
+ @UDFAnnotations.ParamMeta(argNames = {"shell", "holes"}, argTypes = {"Geometry", "Geometry"}, returnTypes = "Geometry")
+ public static String ST_MakePolygon(String shell, String holes) {
+ return GeometrySerde.serGeoJson(
+ Functions.makePolygon(
+ GeometrySerde.deserGeoJson(shell),
+ GeometrySerde.deserGeoJson2List(holes)
+ )
+ );
+ }
+
+ @UDFAnnotations.ParamMeta(argNames = {"geometry"}, argTypes = {"Geometry"}, returnTypes = "Geometry")
+ public static String ST_MakeValid(String geometry) {
+ return GeometrySerde.serGeoJson(
+ Functions.makeValid(
+ GeometrySerde.deserGeoJson(geometry),
+ false
+ )
+ );
+ }
+
+ @UDFAnnotations.ParamMeta(argNames = {"geometry", "keepCollapsed"}, argTypes = {"Geometry", "boolean"}, returnTypes = "Geometry")
+ public static String ST_MakeValid(String geometry, boolean keepCollapsed) {
+ return GeometrySerde.serGeoJson(
+ Functions.makeValid(
+ GeometrySerde.deserGeoJson(geometry),
+ keepCollapsed
+ )
+ );
+ }
+
+ @UDFAnnotations.ParamMeta(argNames = {"geometry", "quadrantSegments"}, argTypes = {"Geometry", "int"}, returnTypes = "Geometry")
+ public static String ST_MinimumBoundingCircle(String geometry, int quadrantSegments) {
+ return GeometrySerde.serGeoJson(
+ Functions.minimumBoundingCircle(
+ GeometrySerde.deserGeoJson(geometry),
+ quadrantSegments
+ )
+ );
+ }
+
+ @UDFAnnotations.ParamMeta(argNames = {"geometry"}, argTypes = {"Geometry"}, returnTypes = "Geometry")
+ public static String ST_Multi(String geometry) throws IOException {
+ return GeometrySerde.serGeoJson(
+ Functions.createMultiGeometryFromOneElement(
+ GeometrySerde.deserGeoJson(geometry)
+ )
+ );
+ }
+
+ @UDFAnnotations.ParamMeta(argNames = {"geometry"}, argTypes = {"Geometry"})
+ public static int ST_NDims(String geometry) {
+ return Functions.nDims(
+ GeometrySerde.deserGeoJson(geometry)
+ );
+ }
+
+ @UDFAnnotations.ParamMeta(argNames = {"geometry"}, argTypes = {"Geometry"})
+ public static int ST_NPoints(String geometry) {
+ return Functions.nPoints(
+ GeometrySerde.deserGeoJson(geometry)
+ );
+ }
+
+ @UDFAnnotations.ParamMeta(argNames = {"geometry"}, argTypes = {"Geometry"}, returnTypes = "Geometry")
+ public static String ST_Normalize(String geometry) {
+ return GeometrySerde.serGeoJson(
+ Functions.normalize(
+ GeometrySerde.deserGeoJson(geometry)
+ )
+ );
+ }
+
+ @UDFAnnotations.ParamMeta(argNames = {"geometry"}, argTypes = {"Geometry"})
+ public static int ST_NumGeometries(String geometry) {
+ return Functions.numGeometries(
+ GeometrySerde.deserGeoJson(geometry)
+ );
+ }
+
+ @UDFAnnotations.ParamMeta(argNames = {"geometry"}, argTypes = {"Geometry"})
+ public static Integer ST_NumInteriorRings(String geometry) {
+ return Functions.numInteriorRings(
+ GeometrySerde.deserGeoJson(geometry)
+ );
+ }
+
+ @UDFAnnotations.ParamMeta(argNames = {"leftGeometry", "rightGeometry"}, argTypes = {"Geometry", "Geometry"})
+ public static boolean ST_OrderingEquals(String leftGeometry, String rightGeometry) {
+ return Predicates.orderingEquals(
+ GeometrySerde.deserGeoJson(leftGeometry),
+ GeometrySerde.deserGeoJson(rightGeometry)
+ );
+ }
+
+ @UDFAnnotations.ParamMeta(argNames = {"leftGeometry", "rightGeometry"}, argTypes = {"Geometry", "Geometry"})
+ public static boolean ST_Overlaps(String leftGeometry, String rightGeometry) {
+ return Predicates.overlaps(
+ GeometrySerde.deserGeoJson(leftGeometry),
+ GeometrySerde.deserGeoJson(rightGeometry)
+ );
+ }
+
+ @UDFAnnotations.ParamMeta(argNames = {"geometry", "n"}, argTypes = {"Geometry", "int"}, returnTypes = "Geometry")
+ public static String ST_PointN(String geometry, int n) {
+ return GeometrySerde.serGeoJson(
+ Functions.pointN(
+ GeometrySerde.deserGeoJson(geometry),
+ n
+ )
+ );
+ }
+
+ @UDFAnnotations.ParamMeta(argNames = {"geometry"}, argTypes = {"Geometry"}, returnTypes = "Geometry")
+ public static String ST_PointOnSurface(String geometry) {
+ return GeometrySerde.serGeoJson(
+ Functions.pointOnSurface(
+ GeometrySerde.deserGeoJson(geometry)
+ )
+ );
+ }
+
+ @UDFAnnotations.ParamMeta(argNames = {"geometry", "precisionScale"}, argTypes = {"Geometry", "int"}, returnTypes = "Geometry")
+ public static String ST_PrecisionReduce(String geometry, int precisionScale) {
+ return GeometrySerde.serGeoJson(
+ Functions.reducePrecision(
+ GeometrySerde.deserGeoJson(geometry),
+ precisionScale
+ )
+ );
+ }
+
+ @UDFAnnotations.ParamMeta(argNames = {"geometry", "precisionScale"}, argTypes = {"Geometry", "int"}, returnTypes = "Geometry")
+ public static String ST_ReducePrecision(String geometry, int precisionScale) {
+ return GeometrySerde.serGeoJson(
+ Functions.reducePrecision(
+ GeometrySerde.deserGeoJson(geometry),
+ precisionScale
+ )
+ );
+ }
+
+ @UDFAnnotations.ParamMeta(argNames = {"linestring"}, argTypes = {"Geometry"}, returnTypes = "Geometry")
+ public static String ST_RemovePoint(String linestring) {
+ return GeometrySerde.serGeoJson(
+ Functions.removePoint(
+ GeometrySerde.deserGeoJson(linestring)
+ )
+ );
+ }
+
+ @UDFAnnotations.ParamMeta(argNames = {"linestring", "position"}, argTypes = {"Geometry", "int"}, returnTypes = "Geometry")
+ public static String ST_RemovePoint(String linestring, int position) {
+ return GeometrySerde.serGeoJson(
+ Functions.removePoint(
+ GeometrySerde.deserGeoJson(linestring),
+ position
+ )
+ );
+ }
+
+ @UDFAnnotations.ParamMeta(argNames = {"geometry"}, argTypes = {"Geometry"}, returnTypes = "Geometry")
+ public static String ST_Reverse(String geometry) {
+ return GeometrySerde.serGeoJson(
+ Functions.reverse(
+ GeometrySerde.deserGeoJson(geometry)
+ )
+ );
+ }
+
+ @UDFAnnotations.ParamMeta(argNames = {"input", "level"}, argTypes = {"Geometry", "int"})
+ public static long[] ST_S2CellIDs(byte[] input, int level) {
+ return TypeUtils.castLong(
+ Functions.s2CellIDs(
+ GeometrySerde.deserialize(input),
+ level
+ )
+ );
+ }
+
+ @UDFAnnotations.ParamMeta(argNames = {"geometry"}, argTypes = {"Geometry"})
+ public static int ST_SRID(String geometry) {
+ return Functions.getSRID(
+ GeometrySerde.deserGeoJson(geometry)
+ );
+ }
+
+ @UDFAnnotations.ParamMeta(argNames = {"geometry"}, argTypes = {"Geometry"})
+ public static String ST_AsText(String geometry) {
+ return Functions.asWKT(
+ GeometrySerde.deserGeoJson(geometry)
+ );
+ }
+
+ @UDFAnnotations.ParamMeta(argNames = {"linestring", "position", "point"}, argTypes = {"Geometry", "int", "Geometry"}, returnTypes = "Geometry")
+ public static String ST_SetPoint(String linestring, int position, String point) {
+ return GeometrySerde.serGeoJson(
+ Functions.setPoint(
+ GeometrySerde.deserGeoJson(linestring),
+ position,
+ GeometrySerde.deserGeoJson(point)
+ )
+ );
+ }
+
+ @UDFAnnotations.ParamMeta(argNames = {"geometry", "srid"}, argTypes = {"Geometry", "int"}, returnTypes = "Geometry")
+ public static String ST_SetSRID(String geometry, int srid) {
+ return GeometrySerde.serGeoJson(
+ Functions.setSRID(
+ GeometrySerde.deserGeoJson(geometry),
+ srid
+ )
+ );
+ }
+
+ @UDFAnnotations.ParamMeta(argNames = {"geometry", "distanceTolerance"}, argTypes = {"Geometry", "double"}, returnTypes = "Geometry")
+ public static String ST_SimplifyPreserveTopology(String geometry, double distanceTolerance) {
+ return GeometrySerde.serGeoJson(
+ Functions.simplifyPreserveTopology(
+ GeometrySerde.deserGeoJson(geometry),
+ distanceTolerance
+ )
+ );
+ }
+
+ @UDFAnnotations.ParamMeta(argNames = {"input", "blade"}, argTypes = {"Geometry", "Geometry"}, returnTypes = "Geometry")
+ public static String ST_Split(String input, String blade) {
+ return GeometrySerde.serGeoJson(
+ Functions.split(
+ GeometrySerde.deserGeoJson(input),
+ GeometrySerde.deserGeoJson(blade)
+ )
+ );
+ }
+
+ @UDFAnnotations.ParamMeta(argNames = {"geometry"}, argTypes = {"Geometry"}, returnTypes = "Geometry")
+ public static String ST_StartPoint(String geometry) {
+ return GeometrySerde.serGeoJson(
+ Functions.startPoint(
+ GeometrySerde.deserGeoJson(geometry)
+ )
+ );
+ }
+
+ @UDFAnnotations.ParamMeta(argNames = {"geometry", "maxVertices"}, argTypes = {"Geometry", "int"}, returnTypes = "Geometry")
+ public static String ST_SubDivide(String geometry, int maxVertices) {
+ return GeometrySerde.serGeoJson(
+ Functions.subDivide(
+ GeometrySerde.deserGeoJson(geometry),
+ maxVertices
+ )
+ );
+ }
+
+ @UDFAnnotations.ParamMeta(argNames = {"leftGeom", "rightGeom"}, argTypes = {"Geometry", "Geometry"}, returnTypes = "Geometry")
+ public static String ST_SymDifference(String leftGeom, String rightGeom) {
+ return GeometrySerde.serGeoJson(
+ Functions.symDifference(
+ GeometrySerde.deserGeoJson(leftGeom),
+ GeometrySerde.deserGeoJson(rightGeom)
+ )
+ );
+ }
+
+ @UDFAnnotations.ParamMeta(argNames = {"leftGeometry", "rightGeometry"}, argTypes = {"Geometry", "Geometry"})
+ public static boolean ST_Touches(String leftGeometry, String rightGeometry) {
+ return Predicates.touches(
+ GeometrySerde.deserGeoJson(leftGeometry),
+ GeometrySerde.deserGeoJson(rightGeometry)
+ );
+ }
+
+ @UDFAnnotations.ParamMeta(argNames = {"geometry", "sourceCRS", "targetCRS"}, argTypes = {"Geometry", "String", "String"}, returnTypes = "Geometry")
+ public static String ST_Transform(String geometry, String sourceCRS, String targetCRS) {
+ return GeometrySerde.serGeoJson(
+ GeoToolsWrapper.transform(
+ GeometrySerde.deserGeoJson(geometry),
+ sourceCRS,
+ targetCRS
+ )
+ );
+ }
+
+ @UDFAnnotations.ParamMeta(argNames = {"geometry", "sourceCRS", "targetCRS", "lenient"}, argTypes = {"Geometry", "String", "String", "boolean"}, returnTypes = "Geometry")
+ public static String ST_Transform(String geometry, String sourceCRS, String targetCRS, boolean lenient) {
+ return GeometrySerde.serGeoJson(
+ GeoToolsWrapper.transform(
+ GeometrySerde.deserGeoJson(geometry),
+ sourceCRS,
+ targetCRS,
+ lenient
+ )
+ );
+ }
+
+ @UDFAnnotations.ParamMeta(argNames = {"leftGeom", "rightGeom"}, argTypes = {"Geometry", "Geometry"}, returnTypes = "Geometry")
+ public static String ST_Union(String leftGeom, String rightGeom) {
+ return GeometrySerde.serGeoJson(
+ Functions.union(
+ GeometrySerde.deserGeoJson(leftGeom),
+ GeometrySerde.deserGeoJson(rightGeom)
+ )
+ );
+ }
+
+ @UDFAnnotations.ParamMeta(argNames = {"leftGeometry", "rightGeometry"}, argTypes = {"Geometry", "Geometry"})
+ public static boolean ST_Within(String leftGeometry, String rightGeometry) {
+ return Predicates.within(
+ GeometrySerde.deserGeoJson(leftGeometry),
+ GeometrySerde.deserGeoJson(rightGeometry)
+ );
+ }
+
+ @UDFAnnotations.ParamMeta(argNames = {"geometry"}, argTypes = {"Geometry"})
+ public static Double ST_X(String geometry) {
+ return Functions.x(
+ GeometrySerde.deserGeoJson(geometry)
+ );
+ }
+
+ @UDFAnnotations.ParamMeta(argNames = {"geometry"}, argTypes = {"Geometry"})
+ public static double ST_XMax(String geometry) {
+ return Functions.xMax(
+ GeometrySerde.deserGeoJson(geometry)
+ );
+ }
+
+ @UDFAnnotations.ParamMeta(argNames = {"geometry"}, argTypes = {"Geometry"})
+ public static double ST_XMin(String geometry) {
+ return Functions.xMin(
+ GeometrySerde.deserGeoJson(geometry)
+ );
+ }
+
+ @UDFAnnotations.ParamMeta(argNames = {"geometry"}, argTypes = {"Geometry"})
+ public static Double ST_Y(String geometry) {
+ return Functions.y(
+ GeometrySerde.deserGeoJson(geometry)
+ );
+ }
+
+ @UDFAnnotations.ParamMeta(argNames = {"geometry"}, argTypes = {"Geometry"})
+ public static double ST_YMax(String geometry) {
+ return Functions.yMax(
+ GeometrySerde.deserGeoJson(geometry)
+ );
+ }
+
+ @UDFAnnotations.ParamMeta(argNames = {"geometry"}, argTypes = {"Geometry"})
+ public static double ST_YMin(String geometry) {
+ return Functions.yMin(
+ GeometrySerde.deserGeoJson(geometry)
+ );
+ }
+
+ @UDFAnnotations.ParamMeta(argNames = {"geometry"}, argTypes = {"Geometry"})
+ public static Double ST_Z(String geometry) {
+ return Functions.z(
+ GeometrySerde.deserGeoJson(geometry)
+ );
+ }
+
+ @UDFAnnotations.ParamMeta(argNames = {"geometry"}, argTypes = {"Geometry"})
+ public static Double ST_ZMax(String geometry) {
+ return Functions.zMax(
+ GeometrySerde.deserGeoJson(geometry)
+ );
+ }
+
+ @UDFAnnotations.ParamMeta(argNames = {"geometry"}, argTypes = {"Geometry"})
+ public static Double ST_ZMin(String geometry) {
+ return Functions.zMin(
+ GeometrySerde.deserGeoJson(geometry)
+ );
+ }
+
+ @UDFAnnotations.ParamMeta(argNames = {"geometry"}, argTypes = {"Geometry"})
+ public static Double ST_AreaSpheroid(String geometry) {
+ return Spheroid.area(
+ GeometrySerde.deserGeoJson(geometry)
+ );
+ }
+
+ @UDFAnnotations.ParamMeta(argNames = {"geomA", "geomB"}, argTypes = {"Geometry", "Geometry"})
+ public static Double ST_DistanceSphere(String geomA, String geomB) {
+ return Haversine.distance(
+ GeometrySerde.deserGeoJson(geomA),
+ GeometrySerde.deserGeoJson(geomB)
+ );
+ }
+
+ @UDFAnnotations.ParamMeta(argNames = {"geomA", "geomB", "radius"}, argTypes = {"Geometry", "Geometry", "double"})
+ public static Double ST_DistanceSphere(String geomA, String geomB, double radius) {
+ return Haversine.distance(
+ GeometrySerde.deserGeoJson(geomA),
+ GeometrySerde.deserGeoJson(geomB),
+ radius
+ );
+ }
+
+ @UDFAnnotations.ParamMeta(argNames = {"geomA", "geomB"}, argTypes = {"Geometry", "Geometry"})
+ public static Double ST_DistanceSpheroid(String geomA, String geomB) {
+ return Spheroid.distance(
+ GeometrySerde.deserGeoJson(geomA),
+ GeometrySerde.deserGeoJson(geomB)
+ );
+ }
+
+ @UDFAnnotations.ParamMeta(argNames = {"geom", "zValue"}, argTypes = {"Geometry", "double"}, returnTypes = "Geometry")
+ public static String ST_Force3D(String geom, double zValue) {
+ return GeometrySerde.serGeoJson(
+ Functions.force3D(
+ GeometrySerde.deserGeoJson(geom),
+ zValue
+ )
+ );
+ }
+
+ @UDFAnnotations.ParamMeta(argNames = {"geom"}, argTypes = {"Geometry"}, returnTypes = "Geometry")
+ public static String ST_Force3D(String geom) {
+ return GeometrySerde.serGeoJson(
+ Functions.force3D(
+ GeometrySerde.deserGeoJson(geom)
+ )
+ );
+ }
+
+ @UDFAnnotations.ParamMeta(argNames = {"geom"}, argTypes = {"Geometry"})
+ public static double ST_LengthSpheroid(String geom) {
+ return Spheroid.length(
+ GeometrySerde.deserGeoJson(geom)
+ );
+ }
+
+ @UDFAnnotations.ParamMeta(argNames = {"geom"}, argTypes = {"Geometry"}, returnTypes = "Geometry")
+ public static String ST_GeometricMedian(String geom) throws Exception {
+ return GeometrySerde.serGeoJson(
+ Functions.geometricMedian(
+ GeometrySerde.deserGeoJson(geom)
+ )
+ );
+ }
+
+ @UDFAnnotations.ParamMeta(argNames = {"geom", "tolerance"}, argTypes = {"Geometry", "float"}, returnTypes = "Geometry")
+ public static String ST_GeometricMedian(String geom, float tolerance) throws Exception {
+ return GeometrySerde.serGeoJson(
+ Functions.geometricMedian(
+ GeometrySerde.deserGeoJson(geom),
+ tolerance
+ )
+ );
+ }
+
+ @UDFAnnotations.ParamMeta(argNames = {"geom", "tolerance", "maxIter"}, argTypes = {"Geometry", "float", "int"}, returnTypes = "Geometry")
+ public static String ST_GeometricMedian(String geom, float tolerance, int maxIter) throws Exception {
+ return GeometrySerde.serGeoJson(
+ Functions.geometricMedian(
+ GeometrySerde.deserGeoJson(geom),
+ tolerance,
+ maxIter
+ )
+ );
+ }
+
+ @UDFAnnotations.ParamMeta(argNames = {"geom"}, argTypes = {"Geometry"})
+ public static int ST_NRings(String geom) throws Exception {
+ return Functions.nRings(
+ GeometrySerde.deserGeoJson(geom)
+ );
+ }
+
+ @UDFAnnotations.ParamMeta(argNames = {"geom"}, argTypes = {"Geometry"})
+ public static int ST_NumPoints(String geom) throws Exception {
+ return Functions.numPoints(
+ GeometrySerde.deserGeoJson(geom)
+ );
+ }
+
+ @UDFAnnotations.ParamMeta(argNames = {"geom", "deltaX", "deltaY"}, argTypes = {"Geometry", "double", "double"}, returnTypes = "Geometry")
+ public static String ST_Translate(String geom, double deltaX, double deltaY) {
+ return GeometrySerde.serGeoJson(
+ Functions.translate(
+ GeometrySerde.deserGeoJson(geom),
+ deltaX,
+ deltaY
+ )
+ );
+ }
+
+ @UDFAnnotations.ParamMeta(argNames = {"geom", "deltaX", "deltaY", "deltaZ"}, argTypes = {"Geometry", "double", "double", "double"}, returnTypes = "Geometry")
+ public static String ST_Translate(String geom, double deltaX, double deltaY, double deltaZ) {
+ return GeometrySerde.serGeoJson(
+ Functions.translate(
+ GeometrySerde.deserGeoJson(geom),
+ deltaX,
+ deltaY,
+ deltaZ
+ )
+ );
+ }
+}
diff --git a/snowflake/src/main/java/org/apache/sedona/snowflake/snowsql/annotations/UDFAnnotations.java b/snowflake/src/main/java/org/apache/sedona/snowflake/snowsql/annotations/UDFAnnotations.java
index bcae56b86..4daf75108 100644
--- a/snowflake/src/main/java/org/apache/sedona/snowflake/snowsql/annotations/UDFAnnotations.java
+++ b/snowflake/src/main/java/org/apache/sedona/snowflake/snowsql/annotations/UDFAnnotations.java
@@ -38,6 +38,7 @@ public class UDFAnnotations {
@Target(ElementType.METHOD)
public static @interface ParamMeta {
String[] argNames();
-
+ String[] argTypes() default {};
+ String returnTypes() default "";
}
}
diff --git a/snowflake/src/main/java/org/apache/sedona/snowflake/snowsql/ddl/ArgSpecBuilder.java b/snowflake/src/main/java/org/apache/sedona/snowflake/snowsql/ddl/ArgSpecBuilder.java
index 19600665e..59b4894c8 100644
--- a/snowflake/src/main/java/org/apache/sedona/snowflake/snowsql/ddl/ArgSpecBuilder.java
+++ b/snowflake/src/main/java/org/apache/sedona/snowflake/snowsql/ddl/ArgSpecBuilder.java
@@ -19,19 +19,20 @@ public class ArgSpecBuilder
{
/**
* Build argument spec for a function. Format: argName1 argType1, argName2 argType2, ...
- * @param paramTypes
+ * @param argTypesRaw
* @param argNames
* @return
*/
- public static String args(Parameter[] paramTypes, String[] argNames){
+ public static String args(Parameter[] argTypesRaw, String[] argNames, String[] argTypesCustom){
StringBuilder argTypesBuilder = new StringBuilder();
- for (int it = 0; it < paramTypes.length; it++) {
+ for (int it = 0; it < argTypesRaw.length; it++) {
argTypesBuilder.append(String.format(
"%s %s",
argNames[it],
- Constants.snowflakeTypeMap.get(paramTypes[it].getType().getTypeName())
+ // Use the argTypes array if types are manually specified, otherwise use Reflection to get the type name
+ Constants.snowflakeTypeMap.get(argTypesCustom.length == 0?argTypesRaw[it].getType().getTypeName():argTypesCustom[it])
));
- if (it + 1 != paramTypes.length) {
+ if (it + 1 != argTypesRaw.length) {
argTypesBuilder.append(", ");
}
}
@@ -41,17 +42,18 @@ public class ArgSpecBuilder
/**
* Build argument spec for a function. Format: argType1, argType2, ...
- * @param paramTypes
+ * @param argTypesRaw
* @return
*/
- public static String argTypes(Parameter[] paramTypes){
+ public static String argTypes(Parameter[] argTypesRaw, String[] argTypesCustom){
StringBuilder argTypesBuilder = new StringBuilder();
- for (int it = 0; it < paramTypes.length; it++) {
+ for (int it = 0; it < argTypesRaw.length; it++) {
argTypesBuilder.append(String.format(
"%s",
- Constants.snowflakeTypeMap.get(paramTypes[it].getType().getTypeName())
+ // Use the argTypes array if types are manually specified, otherwise use Reflection to get the type name
+ Constants.snowflakeTypeMap.get(argTypesCustom.length == 0?argTypesRaw[it].getType().getTypeName():argTypesCustom[it])
));
- if (it + 1 != paramTypes.length) {
+ if (it + 1 != argTypesRaw.length) {
argTypesBuilder.append(", ");
}
}
diff --git a/snowflake/src/main/java/org/apache/sedona/snowflake/snowsql/ddl/Constants.java b/snowflake/src/main/java/org/apache/sedona/snowflake/snowsql/ddl/Constants.java
index d4a9326bb..2f6a6b9f2 100644
--- a/snowflake/src/main/java/org/apache/sedona/snowflake/snowsql/ddl/Constants.java
+++ b/snowflake/src/main/java/org/apache/sedona/snowflake/snowsql/ddl/Constants.java
@@ -21,6 +21,7 @@ public class Constants {
static {
snowflakeTypeMap.put(String.class.getTypeName(), "VARCHAR");
+ snowflakeTypeMap.put("String", "VARCHAR");
snowflakeTypeMap.put(byte[].class.getTypeName(), "BINARY");
snowflakeTypeMap.put(long.class.getTypeName(), "NUMBER");
snowflakeTypeMap.put(Long.class.getTypeName(), "NUMBER");
@@ -34,6 +35,7 @@ public class Constants {
snowflakeTypeMap.put(Double.class.getTypeName(), "DOUBLE");
snowflakeTypeMap.put(float.class.getTypeName(), "FLOAT");
snowflakeTypeMap.put(Float.class.getTypeName(), "FLOAT");
+ snowflakeTypeMap.put("Geometry", "GEOMETRY");
}
public static String SEDONA_VERSION = "sedona_version";
diff --git a/snowflake/src/main/java/org/apache/sedona/snowflake/snowsql/ddl/DDLGenerator.java b/snowflake/src/main/java/org/apache/sedona/snowflake/snowsql/ddl/DDLGenerator.java
index 1900ab5c8..ccbd0084c 100644
--- a/snowflake/src/main/java/org/apache/sedona/snowflake/snowsql/ddl/DDLGenerator.java
+++ b/snowflake/src/main/java/org/apache/sedona/snowflake/snowsql/ddl/DDLGenerator.java
@@ -40,7 +40,7 @@ public class DDLGenerator {
}
}
// add sedona version to argMap. This fetches value from pom.xml and only works when running from the terminal
- argMap.put(Constants.SEDONA_VERSION, DDLGenerator.class.getPackage().getImplementationVersion());
+ argMap.put(Constants.SEDONA_VERSION, DDLGenerator.class.getPackage().getImplementationVersion() == null ? "unknown" : DDLGenerator.class.getPackage().getImplementationVersion());
try {
assert argMap.containsKey(Constants.GEOTOOLS_VERSION);
} catch (AssertionError e) {
diff --git a/snowflake/src/main/java/org/apache/sedona/snowflake/snowsql/ddl/UDFDDLGenerator.java b/snowflake/src/main/java/org/apache/sedona/snowflake/snowsql/ddl/UDFDDLGenerator.java
index 2aa6c3ba2..ec6df3f8d 100644
--- a/snowflake/src/main/java/org/apache/sedona/snowflake/snowsql/ddl/UDFDDLGenerator.java
+++ b/snowflake/src/main/java/org/apache/sedona/snowflake/snowsql/ddl/UDFDDLGenerator.java
@@ -14,6 +14,7 @@
package org.apache.sedona.snowflake.snowsql.ddl;
import org.apache.sedona.snowflake.snowsql.UDFs;
+import org.apache.sedona.snowflake.snowsql.UDFsV2;
import org.apache.sedona.snowflake.snowsql.annotations.UDFAnnotations;
import java.io.BufferedReader;
@@ -33,26 +34,33 @@ public class UDFDDLGenerator {
return UDFs.class.getDeclaredMethods();
}
+ public static Method[] udfV2Methods() {
+ return UDFsV2.class.getDeclaredMethods();
+ }
+
public static String buildUDFDDL(Method method, Map<String, String> configs, String stageName, boolean isNativeApp, String appRoleName) {
if (!method.isAnnotationPresent(UDFAnnotations.ParamMeta.class)) {
throw new RuntimeException("Missing ParamMeta annotation for method: " + method.getName());
}
- String[] args = method.getAnnotation(UDFAnnotations.ParamMeta.class).argNames();
- Parameter[] argTypes = method.getParameters();
+ String[] argNames = method.getAnnotation(UDFAnnotations.ParamMeta.class).argNames();
+ Parameter[] argTypesRaw = method.getParameters();
+ String argTypesCustom[] = method.getAnnotation(UDFAnnotations.ParamMeta.class).argTypes();
// generate return type
- String returnType = Constants.snowflakeTypeMap.get(method.getReturnType().getTypeName());
+ String returnType = Constants.snowflakeTypeMap.get(method.getAnnotation(UDFAnnotations.ParamMeta.class).returnTypes().isEmpty() ? method.getReturnType().getTypeName()
+ : method.getAnnotation(UDFAnnotations.ParamMeta.class).returnTypes());
if (returnType == null) {
throw new RuntimeException("Unsupported type: " + method.getReturnType().getTypeName());
}
- String handlerName = UDFs.class.getPackage().getName() + "." + UDFs.class.getSimpleName() + "." + method.getName();
+ String handlerName = method.getDeclaringClass().getName() + "." + method.getName();
// check some function attributes
String null_input_conf = method.isAnnotationPresent(UDFAnnotations.CallOnNull.class) ? "CALLED ON NULL INPUT" : "RETURNS NULL ON NULL INPUT";
String immutable_conf = method.isAnnotationPresent(UDFAnnotations.Volatile.class) ? "VOLATILE" : "IMMUTABLE";
return formatUDFDDL(
method.getName(),
configs.getOrDefault("schema", "sedona"),
- argTypes,
- args,
+ argTypesRaw,
+ argNames,
+ argTypesCustom,
returnType,
stageName,
handlerName,
@@ -72,14 +80,20 @@ public class UDFDDLGenerator {
ddlList.add(buildUDFDDL(method, configs, stageName, isNativeApp, appRoleName));
}
}
+ for (Method method : udfV2Methods()) {
+ if (method.getModifiers() == (Modifier.PUBLIC | Modifier.STATIC)) {
+ ddlList.add(buildUDFDDL(method, configs, stageName, isNativeApp, appRoleName));
+ }
+ }
return ddlList;
}
public static String formatUDFDDL(
String functionName,
String schemaName,
- Parameter[] argTypes,
+ Parameter[] argTypesRaw,
String[] argNames,
+ String[] argTypesCustom,
String returnType,
String stageName,
String handlerName,
@@ -100,7 +114,7 @@ public class UDFDDLGenerator {
).replace(
"{KW_SCHEMA_NAME}", schemaName
).replace(
- "{KW_ARG_SPEC}", ArgSpecBuilder.args(argTypes, argNames)
+ "{KW_ARG_SPEC}", ArgSpecBuilder.args(argTypesRaw, argNames, argTypesCustom)
).replace(
"{KW_RETURN_TYPE}", returnType
).replace(
@@ -118,7 +132,7 @@ public class UDFDDLGenerator {
);
if (isNativeApp) {
ddl += "\n";
- ddl += "GRANT USAGE ON FUNCTION " + schemaName + "." + functionName + "(" + ArgSpecBuilder.argTypes(argTypes) + ") TO APPLICATION ROLE " + appRoleName + ";";
+ ddl += "GRANT USAGE ON FUNCTION " + schemaName + "." + functionName + "(" + ArgSpecBuilder.argTypes(argTypesRaw, argTypesCustom) + ") TO APPLICATION ROLE " + appRoleName + ";";
}
return ddl;
}
diff --git a/snowflake/src/main/java/org/apache/sedona/snowflake/snowsql/ddl/UDTFDDLGenerator.java b/snowflake/src/main/java/org/apache/sedona/snowflake/snowsql/ddl/UDTFDDLGenerator.java
index ceb0356bc..c4710ce53 100644
--- a/snowflake/src/main/java/org/apache/sedona/snowflake/snowsql/ddl/UDTFDDLGenerator.java
+++ b/snowflake/src/main/java/org/apache/sedona/snowflake/snowsql/ddl/UDTFDDLGenerator.java
@@ -37,7 +37,7 @@ public class UDTFDDLGenerator {
public static String formatUDTFDDL(
String functionName,
String schemaName,
- Parameter[] argTypes,
+ Parameter[] argTypesRaw,
String[] argNames,
String returnType,
String stageName,
@@ -59,7 +59,7 @@ public class UDTFDDLGenerator {
).replace(
"{KW_SCHEMA_NAME}", schemaName
).replace(
- "{KW_ARG_SPEC}", ArgSpecBuilder.args(argTypes, argNames)
+ "{KW_ARG_SPEC}", ArgSpecBuilder.args(argTypesRaw, argNames, new String[]{})
).replace(
"{KW_RETURN_TYPE}", returnType
).replace(
@@ -77,7 +77,7 @@ public class UDTFDDLGenerator {
);
if (isNativeApp) {
ddl += "\n";
- ddl += "GRANT USAGE ON FUNCTION " + schemaName + "." + functionName + "(" + ArgSpecBuilder.argTypes(argTypes) + ") TO APPLICATION ROLE " + appRoleName + ";";
+ ddl += "GRANT USAGE ON FUNCTION " + schemaName + "." + functionName + "(" + ArgSpecBuilder.argTypes(argTypesRaw, new String[]{}) + ") TO APPLICATION ROLE " + appRoleName + ";";
}
return ddl;
}