paleolimbot commented on code in PR #469:
URL: https://github.com/apache/sedona-db/pull/469#discussion_r2654669686
##########
python/sedonadb/tests/functions/test_functions.py:
##########
@@ -119,6 +119,49 @@ def test_st_astext(eng, geom):
eng.assert_query_result(f"SELECT ST_AsText({geom_or_null(geom)})",
expected)
[email protected]("eng", [SedonaDB, PostGIS])
[email protected](
+ ("geom", "expected"),
+ [
+ (None, None),
+ ("LINESTRING EMPTY", '{"type":"LineString","coordinates":[]}'),
+ ("POLYGON EMPTY", '{"type":"Polygon","coordinates":[[]]}'),
+ ("MULTIPOINT EMPTY", '{"type":"MultiPoint","coordinates":[]}'),
+ ("MULTILINESTRING EMPTY",
'{"type":"MultiLineString","coordinates":[]}'),
+ ("MULTIPOLYGON EMPTY", '{"type":"MultiPolygon","coordinates":[]}'),
+ ("GEOMETRYCOLLECTION EMPTY",
'{"type":"GeometryCollection","geometries":[]}'),
+ ("POINT (1 2)", '{"type":"Point","coordinates":[1.0,2.0]}'),
+ (
+ "LINESTRING (0 0, 1 1)",
+ '{"type":"LineString","coordinates":[[0.0,0.0],[1.0,1.0]]}',
+ ),
Review Comment:
For the failing tests, how about something like:
```python
if eng is PostGIS:
expected = expected.replace(".0", "")
```
Slightly less hacky but might not work (not sure if Python dictionary
equality works exactly in the way we need it to for this):
```python
result = eng.execute_and_collect()
df = eng.result_to_pandas(result)
assert df.shape == (1, 1)
assert json.loads(df.iloc[0, 0]) == json.loads(expected)
```
Either works for me! I don't particularly mind the difference (in the
process of making it faster we might be able to fix the incompatibility as
well).
##########
python/sedonadb/tests/functions/test_functions.py:
##########
@@ -119,6 +119,49 @@ def test_st_astext(eng, geom):
eng.assert_query_result(f"SELECT ST_AsText({geom_or_null(geom)})",
expected)
[email protected]("eng", [SedonaDB, PostGIS])
[email protected](
+ ("geom", "expected"),
+ [
+ (None, None),
+ ("LINESTRING EMPTY", '{"type":"LineString","coordinates":[]}'),
Review Comment:
```suggestion
(None, None),
("POINT EMPTY", '{"type":"Point","coordinates":[]}'),
("LINESTRING EMPTY", '{"type":"LineString","coordinates":[]}'),
```
##########
rust/sedona-geo/src/st_asgeojson.rs:
##########
@@ -0,0 +1,148 @@
+// 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::sync::Arc;
+
+use crate::to_geo::GeoTypesExecutor;
+use arrow_array::builder::StringBuilder;
+use arrow_schema::DataType;
+use datafusion_common::error::{DataFusionError, Result};
+use datafusion_expr::ColumnarValue;
+use sedona_expr::scalar_udf::{ScalarKernelRef, SedonaScalarKernel};
+use sedona_schema::{datatypes::SedonaType, matchers::ArgMatcher};
+
+/// ST_AsGeoJSON() kernel implementation using GeoTypesExecutor
+pub fn st_asgeojson_impl() -> ScalarKernelRef {
+ Arc::new(STAsGeoJSON {})
+}
+
+#[derive(Debug)]
+struct STAsGeoJSON {}
+
+impl SedonaScalarKernel for STAsGeoJSON {
+ fn return_type(&self, args: &[SedonaType]) -> Result<Option<SedonaType>> {
+ let matcher = ArgMatcher::new(
+ vec![ArgMatcher::is_geometry()],
+ SedonaType::Arrow(DataType::Utf8),
+ );
+
+ matcher.match_args(args)
+ }
+
+ fn invoke_batch(
+ &self,
+ arg_types: &[SedonaType],
+ args: &[ColumnarValue],
+ ) -> Result<ColumnarValue> {
+ let executor = GeoTypesExecutor::new(arg_types, args);
+
+ // Minimal GeoJSON: {"type":"Point","coordinates":[]}
+ let min_probable_geojson_size = executor.num_iterations() * 33;
+
+ // Initialize an output builder of the appropriate type
+ let mut builder =
+ StringBuilder::with_capacity(executor.num_iterations(),
min_probable_geojson_size);
+
+ executor.execute_wkb_void(|maybe_geom| {
+ match maybe_geom {
+ Some(geom) => {
+ // Convert geo_types::Geometry to geojson::Geometry
+ let geojson_geom: geojson::Geometry = (&geom).into();
Review Comment:
You can solve the lack of empty point support and empty polygon
incompatiblity here by special casing them. Maybe:
```rust
match geom.as_type() {
geo_traits::GeometryType::Point(pt) => {
if pt.coord().is_none() { /* special case the empty point output and
return */ }
}
geo_traits::GeometryType::Polygon(pl) => {
if pl.exterior().is_none() { /* special case the empty polygon output
and return */ }
}
let geo_geom = item_to_geometry(geom)?;
/* current implementation */
}
```
This means you'd have to go back to the `WkbExecutor` because `geo_types`
can't represent an empty point. Most of the functions implemented in this crate
have to do that (the ones that don't aren't used by default at the moment).
--
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]