paleolimbot commented on code in PR #4423:
URL: https://github.com/apache/datafusion-comet/pull/4423#discussion_r3299533578


##########
native/core/Cargo.toml:
##########
@@ -77,6 +77,11 @@ iceberg = { workspace = true }
 iceberg-storage-opendal = { workspace = true }
 serde_json = "1.0"
 uuid = "1.23.0"
+geo = "0.28"
+geoarrow = "0.8"
+geojson = { version = "0.24", features = ["geo-types"] }
+geos = { version = "8.3", features = ["static"] }

Review Comment:
   Just a note that statically linking GEOS is often cited as not being 
compatible with a non-(L)GPL license (although in practice it is frequently 
done). I believe the general Apache line on this is that you can have (L)GPL 
dependencies as long as they are optional (which in this case I think would 
mean a non-default feature flag).



##########
native/core/Cargo.toml:
##########
@@ -77,6 +77,11 @@ iceberg = { workspace = true }
 iceberg-storage-opendal = { workspace = true }
 serde_json = "1.0"
 uuid = "1.23.0"
+geo = "0.28"

Review Comment:
   I believe the latest version is 0.33?



##########
docs/geo-functions.md:
##########
@@ -0,0 +1,583 @@
+# Comet Geo Functions
+
+Comet provides 40 geospatial SQL functions registered as Spark SQL extensions.
+All functions execute natively in the Rust/DataFusion engine when Comet is 
enabled
+(`spark.comet.exec.enabled=true`). Geometries are represented as WKT strings.
+
+## Constructors
+
+Functions that create geometry values.
+
+### st_geomfromwkt
+
+```sql
+st_geomfromwkt(wkt STRING) -> STRING
+```
+
+Parses a WKT string and returns the geometry. Returns `null` if the input is 
`null`.
+
+```sql
+SELECT st_geomfromwkt('POINT(1.0 2.0)');
+-- POINT (1 2)
+```

Review Comment:
   Both Apache Sedona ( 
https://sedona.apache.org/latest/api/sql/Geometry-Constructors/ST_GeomFromWKT/ 
) and SedonaDB ( 
https://sedona.apache.org/sedonadb/latest/reference/sql/st_geomfromwkt/ ) 
maintain copies of approximately these you can link to to avoid maintaining 
these yourselves. (We will probably converge those two docs at some point)



##########
native/core/src/execution/expressions/geo/st_area.rs:
##########
@@ -0,0 +1,75 @@
+// 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.
+
+use std::any::Any;
+use std::sync::Arc;
+
+use arrow::array::{ArrayRef, Float64Array, StringArray};
+use arrow::datatypes::DataType;
+use datafusion::common::Result as DataFusionResult;
+use datafusion::logical_expr::{
+    ColumnarValue, ScalarFunctionArgs, ScalarUDFImpl, Signature, Volatility,
+};
+use geo::Area;
+use wkt::TryFromWkt;
+
+#[derive(Debug, Hash, Eq, PartialEq)]
+pub struct StArea {
+    signature: Signature,
+}
+
+impl Default for StArea {
+    fn default() -> Self {
+        Self {
+            signature: Signature::exact(vec![DataType::Utf8], 
Volatility::Immutable),
+        }
+    }
+}
+
+impl ScalarUDFImpl for StArea {
+    fn as_any(&self) -> &dyn Any {
+        self
+    }
+
+    fn name(&self) -> &str {
+        "st_area"
+    }
+
+    fn signature(&self) -> &Signature {
+        &self.signature
+    }
+
+    fn return_type(&self, _arg_types: &[DataType]) -> 
DataFusionResult<DataType> {
+        Ok(DataType::Float64)
+    }
+
+    fn invoke_with_args(&self, args: ScalarFunctionArgs) -> 
DataFusionResult<ColumnarValue> {
+        let args = ColumnarValue::values_to_arrays(&args.args)?;
+        let geom_col = args[0].as_any().downcast_ref::<StringArray>().unwrap();
+
+        let result: Float64Array = geom_col
+            .iter()
+            .map(|g| {
+                let wkt = g?;
+                let geom = geo::Geometry::<f64>::try_from_wkt_str(wkt).ok()?;
+                Some(geom.unsigned_area())
+            })
+            .collect();
+
+        Ok(ColumnarValue::Array(Arc::new(result) as ArrayRef))
+    }

Review Comment:
   We have ~150 of these defined in SedonaDB ( 
https://github.com/apache/sedona-db ) with thousands of lines of tests against 
a live PostGIS instance. These are exportable/importable from C so you don't 
necessarily need the DataFusion/arrow-rs versions to align ( 
https://github.com/apache/sedona-db/blob/d286f9af8164a48889fd0d6fc82bc2bc274d687e/c/sedona-extension/src/scalar_kernel.rs#L39-L46
 ). You would need a wrapper on this side to handle the to/from WKT for as long 
as you are still using it (WKB is what Spark and SedonaDB are using and it's 
much faster).



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to