paleolimbot commented on code in PR #990: URL: https://github.com/apache/sedona-db/pull/990#discussion_r3483111618
########## c/sedona-geos/src/st_buildarea.rs: ########## @@ -0,0 +1,156 @@ +// 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 arrow_array::builder::BinaryBuilder; +use datafusion_common::{error::Result, DataFusionError}; +use datafusion_expr::ColumnarValue; +use geos::{Geom, Geometry, GeometryTypes}; +use sedona_expr::{ + item_crs::ItemCrsKernel, + scalar_udf::{ScalarKernelRef, SedonaScalarKernel}, +}; +use sedona_geometry::wkb_factory::WKB_MIN_PROBABLE_BYTES; +use sedona_schema::{ + datatypes::{SedonaType, WKB_GEOGRAPHY, WKB_GEOMETRY}, + matchers::ArgMatcher, +}; + +use crate::executor::GeosExecutor; +use crate::geos_to_wkb::write_geos_geometry; + +/// ST_BuildArea() implementation using the geos crate +pub fn st_build_area_impl() -> Vec<ScalarKernelRef> { + ItemCrsKernel::wrap_impl(vec![ + Arc::new(STBuildArea { + matcher: ArgMatcher::new(vec![ArgMatcher::is_geometry()], WKB_GEOMETRY), + }), + Arc::new(STBuildArea { + matcher: ArgMatcher::new(vec![ArgMatcher::is_geography()], WKB_GEOGRAPHY), + }), + ]) +} + +#[derive(Debug)] +struct STBuildArea { + matcher: ArgMatcher, +} + +impl SedonaScalarKernel for STBuildArea { + fn return_type(&self, args: &[SedonaType]) -> Result<Option<SedonaType>> { + self.matcher.match_args(args) + } + + fn invoke_batch( + &self, + arg_types: &[SedonaType], + args: &[ColumnarValue], + ) -> Result<ColumnarValue> { + let executor = GeosExecutor::new(arg_types, args); + let mut builder = BinaryBuilder::with_capacity( + executor.num_iterations(), + WKB_MIN_PROBABLE_BYTES * executor.num_iterations(), + ); + executor.execute_wkb_void(|maybe_geom| { + match maybe_geom { + Some(geom) => { + if invoke_scalar(&geom, &mut builder)? { + builder.append_value([]); + } else { + builder.append_null(); + } + } + _ => builder.append_null(), + } + Ok(()) + })?; + + executor.finish(Arc::new(builder.finish())) + } +} + +fn invoke_scalar(geom: &Geometry, writer: &mut impl std::io::Write) -> Result<bool> { + let geom_type = geom + .geometry_type() + .map_err(|e| DataFusionError::Execution(format!("Failed to get geometry type: {e}")))?; + + match geom_type { + GeometryTypes::LineString + | GeometryTypes::MultiLineString + | GeometryTypes::GeometryCollection => {} + _ => return Ok(false), + } + + let result = geom + .build_area() + .map_err(|e| DataFusionError::Execution(format!("ST_BuildArea failed: {e}")))?; Review Comment: ```suggestion .map_err(|e| exec_datafusion_err!("ST_BuildArea failed: {e}"))?; ``` ########## c/sedona-geos/src/st_buildarea.rs: ########## @@ -0,0 +1,156 @@ +// 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 arrow_array::builder::BinaryBuilder; +use datafusion_common::{error::Result, DataFusionError}; +use datafusion_expr::ColumnarValue; +use geos::{Geom, Geometry, GeometryTypes}; +use sedona_expr::{ + item_crs::ItemCrsKernel, + scalar_udf::{ScalarKernelRef, SedonaScalarKernel}, +}; +use sedona_geometry::wkb_factory::WKB_MIN_PROBABLE_BYTES; +use sedona_schema::{ + datatypes::{SedonaType, WKB_GEOGRAPHY, WKB_GEOMETRY}, + matchers::ArgMatcher, +}; + +use crate::executor::GeosExecutor; +use crate::geos_to_wkb::write_geos_geometry; + +/// ST_BuildArea() implementation using the geos crate +pub fn st_build_area_impl() -> Vec<ScalarKernelRef> { + ItemCrsKernel::wrap_impl(vec![ + Arc::new(STBuildArea { + matcher: ArgMatcher::new(vec![ArgMatcher::is_geometry()], WKB_GEOMETRY), + }), + Arc::new(STBuildArea { + matcher: ArgMatcher::new(vec![ArgMatcher::is_geography()], WKB_GEOGRAPHY), + }), Review Comment: ```suggestion ``` Sorry for missing this, but we can't support Geography with this one because the definition of "enclosing" isn't the same on the sphere. ########## c/sedona-geos/src/st_exteriorring.rs: ########## @@ -0,0 +1,159 @@ +// 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 arrow_array::builder::BinaryBuilder; +use datafusion_common::{error::Result, DataFusionError}; +use datafusion_expr::ColumnarValue; +use geos::{Geom, Geometry, GeometryTypes}; +use sedona_expr::{ + item_crs::ItemCrsKernel, + scalar_udf::{ScalarKernelRef, SedonaScalarKernel}, +}; +use sedona_geometry::wkb_factory::WKB_MIN_PROBABLE_BYTES; +use sedona_schema::{ + datatypes::{SedonaType, WKB_GEOGRAPHY, WKB_GEOMETRY}, + matchers::ArgMatcher, +}; + +use crate::executor::GeosExecutor; +use crate::geos_to_wkb::write_geos_geometry; + +/// ST_ExteriorRing() implementation using the geos crate +/// +/// Returns the exterior ring of a Polygon, or NULL for non-polygon geometries. +pub fn st_exterior_ring_impl() -> Vec<ScalarKernelRef> { + ItemCrsKernel::wrap_impl(vec![ + Arc::new(STExteriorRing { + matcher: ArgMatcher::new(vec![ArgMatcher::is_geometry()], WKB_GEOMETRY), + }), + Arc::new(STExteriorRing { + matcher: ArgMatcher::new(vec![ArgMatcher::is_geography()], WKB_GEOGRAPHY), + }), + ]) +} + +#[derive(Debug)] +struct STExteriorRing { + matcher: ArgMatcher, +} + +impl SedonaScalarKernel for STExteriorRing { + fn return_type(&self, args: &[SedonaType]) -> Result<Option<SedonaType>> { + self.matcher.match_args(args) + } + + fn invoke_batch( + &self, + arg_types: &[SedonaType], + args: &[ColumnarValue], + ) -> Result<ColumnarValue> { + let executor = GeosExecutor::new(arg_types, args); + let mut builder = BinaryBuilder::with_capacity( + executor.num_iterations(), + WKB_MIN_PROBABLE_BYTES * executor.num_iterations(), + ); + executor.execute_wkb_void(|maybe_geom| { + match maybe_geom { + Some(geom) => { + if invoke_scalar(&geom, &mut builder)? { + builder.append_value([]); + } else { + builder.append_null(); + } + } + _ => builder.append_null(), + } + Ok(()) + })?; + + executor.finish(Arc::new(builder.finish())) + } +} + +fn invoke_scalar(geom: &Geometry, writer: &mut impl std::io::Write) -> Result<bool> { + let geom_type = geom + .geometry_type() + .map_err(|e| DataFusionError::Execution(format!("Failed to get geometry type: {e}")))?; + + if geom_type != GeometryTypes::Polygon { + return Ok(false); + } + + let ring = geom + .get_exterior_ring() + .map_err(|e| DataFusionError::Execution(format!("ST_ExteriorRing failed: {e}")))?; Review Comment: ```suggestion .map_err(|e| exec_datafusion_err!("ST_ExteriorRing failed: {e}"))?; ``` ########## c/sedona-geos/src/st_exteriorring.rs: ########## @@ -0,0 +1,159 @@ +// 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 arrow_array::builder::BinaryBuilder; +use datafusion_common::{error::Result, DataFusionError}; +use datafusion_expr::ColumnarValue; +use geos::{Geom, Geometry, GeometryTypes}; +use sedona_expr::{ + item_crs::ItemCrsKernel, + scalar_udf::{ScalarKernelRef, SedonaScalarKernel}, +}; +use sedona_geometry::wkb_factory::WKB_MIN_PROBABLE_BYTES; +use sedona_schema::{ + datatypes::{SedonaType, WKB_GEOGRAPHY, WKB_GEOMETRY}, + matchers::ArgMatcher, +}; + +use crate::executor::GeosExecutor; +use crate::geos_to_wkb::write_geos_geometry; + +/// ST_ExteriorRing() implementation using the geos crate +/// +/// Returns the exterior ring of a Polygon, or NULL for non-polygon geometries. +pub fn st_exterior_ring_impl() -> Vec<ScalarKernelRef> { + ItemCrsKernel::wrap_impl(vec![ + Arc::new(STExteriorRing { + matcher: ArgMatcher::new(vec![ArgMatcher::is_geometry()], WKB_GEOMETRY), + }), + Arc::new(STExteriorRing { + matcher: ArgMatcher::new(vec![ArgMatcher::is_geography()], WKB_GEOGRAPHY), + }), + ]) +} + +#[derive(Debug)] +struct STExteriorRing { + matcher: ArgMatcher, +} + +impl SedonaScalarKernel for STExteriorRing { + fn return_type(&self, args: &[SedonaType]) -> Result<Option<SedonaType>> { + self.matcher.match_args(args) + } + + fn invoke_batch( + &self, + arg_types: &[SedonaType], + args: &[ColumnarValue], + ) -> Result<ColumnarValue> { + let executor = GeosExecutor::new(arg_types, args); + let mut builder = BinaryBuilder::with_capacity( + executor.num_iterations(), + WKB_MIN_PROBABLE_BYTES * executor.num_iterations(), + ); + executor.execute_wkb_void(|maybe_geom| { + match maybe_geom { + Some(geom) => { + if invoke_scalar(&geom, &mut builder)? { + builder.append_value([]); + } else { + builder.append_null(); + } + } + _ => builder.append_null(), + } + Ok(()) + })?; + + executor.finish(Arc::new(builder.finish())) + } +} + +fn invoke_scalar(geom: &Geometry, writer: &mut impl std::io::Write) -> Result<bool> { + let geom_type = geom + .geometry_type() + .map_err(|e| DataFusionError::Execution(format!("Failed to get geometry type: {e}")))?; + + if geom_type != GeometryTypes::Polygon { + return Ok(false); + } + + let ring = geom + .get_exterior_ring() + .map_err(|e| DataFusionError::Execution(format!("ST_ExteriorRing failed: {e}")))?; + let line = + Geometry::create_line_string(ring.get_coord_seq().map_err(|e| { + DataFusionError::Execution(format!("Failed to get ring coordinates: {e}")) + })?) + .map_err(|e| { + DataFusionError::Execution(format!("Failed to create exterior linestring: {e}")) Review Comment: ```suggestion exec_datafusion_err!("Failed to create exterior linestring: {e}") ``` ########## c/sedona-geos/src/st_exteriorring.rs: ########## @@ -0,0 +1,159 @@ +// 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 arrow_array::builder::BinaryBuilder; +use datafusion_common::{error::Result, DataFusionError}; +use datafusion_expr::ColumnarValue; +use geos::{Geom, Geometry, GeometryTypes}; +use sedona_expr::{ + item_crs::ItemCrsKernel, + scalar_udf::{ScalarKernelRef, SedonaScalarKernel}, +}; +use sedona_geometry::wkb_factory::WKB_MIN_PROBABLE_BYTES; +use sedona_schema::{ + datatypes::{SedonaType, WKB_GEOGRAPHY, WKB_GEOMETRY}, + matchers::ArgMatcher, +}; + +use crate::executor::GeosExecutor; +use crate::geos_to_wkb::write_geos_geometry; + +/// ST_ExteriorRing() implementation using the geos crate +/// +/// Returns the exterior ring of a Polygon, or NULL for non-polygon geometries. +pub fn st_exterior_ring_impl() -> Vec<ScalarKernelRef> { + ItemCrsKernel::wrap_impl(vec![ + Arc::new(STExteriorRing { + matcher: ArgMatcher::new(vec![ArgMatcher::is_geometry()], WKB_GEOMETRY), + }), + Arc::new(STExteriorRing { + matcher: ArgMatcher::new(vec![ArgMatcher::is_geography()], WKB_GEOGRAPHY), + }), + ]) +} + +#[derive(Debug)] +struct STExteriorRing { + matcher: ArgMatcher, +} + +impl SedonaScalarKernel for STExteriorRing { + fn return_type(&self, args: &[SedonaType]) -> Result<Option<SedonaType>> { + self.matcher.match_args(args) + } + + fn invoke_batch( + &self, + arg_types: &[SedonaType], + args: &[ColumnarValue], + ) -> Result<ColumnarValue> { + let executor = GeosExecutor::new(arg_types, args); + let mut builder = BinaryBuilder::with_capacity( + executor.num_iterations(), + WKB_MIN_PROBABLE_BYTES * executor.num_iterations(), + ); + executor.execute_wkb_void(|maybe_geom| { + match maybe_geom { + Some(geom) => { + if invoke_scalar(&geom, &mut builder)? { + builder.append_value([]); + } else { + builder.append_null(); + } + } + _ => builder.append_null(), + } + Ok(()) + })?; + + executor.finish(Arc::new(builder.finish())) + } +} + +fn invoke_scalar(geom: &Geometry, writer: &mut impl std::io::Write) -> Result<bool> { + let geom_type = geom + .geometry_type() + .map_err(|e| DataFusionError::Execution(format!("Failed to get geometry type: {e}")))?; Review Comment: ```suggestion .map_err(|e| exec_datafusion_err!("Failed to get geometry type: {e}"))?; ``` ########## c/sedona-geos/src/st_delaunaytriangles.rs: ########## @@ -0,0 +1,288 @@ +// 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 arrow_array::builder::BinaryBuilder; +use arrow_schema::DataType; +use datafusion_common::{ + cast::{as_float64_array, as_int64_array}, + DataFusionError, Result, +}; +use datafusion_expr::ColumnarValue; +use geos::{Geom, Geometry}; +use sedona_expr::{ + item_crs::ItemCrsKernel, + scalar_udf::{ScalarKernelRef, SedonaScalarKernel}, +}; +use sedona_geometry::wkb_factory::WKB_MIN_PROBABLE_BYTES; +use sedona_schema::{ + datatypes::{SedonaType, WKB_GEOMETRY}, + matchers::ArgMatcher, +}; + +use crate::executor::GeosExecutor; +use crate::geos_to_wkb::write_geos_geometry; + +fn invoke_scalar( + geom: &Geometry, + tolerance: f64, + only_edges: bool, + writer: &mut impl std::io::Write, +) -> Result<()> { + let result = geom + .delaunay_triangulation(tolerance, only_edges) + .map_err(|e| DataFusionError::Execution(format!("ST_DelaunayTriangles failed: {e}")))?; Review Comment: ```suggestion .map_err(|e| exec_datafusion_err!("ST_DelaunayTriangles failed: {e}"))?; ``` ########## c/sedona-geos/src/st_exteriorring.rs: ########## @@ -0,0 +1,159 @@ +// 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 arrow_array::builder::BinaryBuilder; +use datafusion_common::{error::Result, DataFusionError}; +use datafusion_expr::ColumnarValue; +use geos::{Geom, Geometry, GeometryTypes}; +use sedona_expr::{ + item_crs::ItemCrsKernel, + scalar_udf::{ScalarKernelRef, SedonaScalarKernel}, +}; +use sedona_geometry::wkb_factory::WKB_MIN_PROBABLE_BYTES; +use sedona_schema::{ + datatypes::{SedonaType, WKB_GEOGRAPHY, WKB_GEOMETRY}, + matchers::ArgMatcher, +}; + +use crate::executor::GeosExecutor; +use crate::geos_to_wkb::write_geos_geometry; + +/// ST_ExteriorRing() implementation using the geos crate +/// +/// Returns the exterior ring of a Polygon, or NULL for non-polygon geometries. +pub fn st_exterior_ring_impl() -> Vec<ScalarKernelRef> { + ItemCrsKernel::wrap_impl(vec![ + Arc::new(STExteriorRing { + matcher: ArgMatcher::new(vec![ArgMatcher::is_geometry()], WKB_GEOMETRY), + }), + Arc::new(STExteriorRing { + matcher: ArgMatcher::new(vec![ArgMatcher::is_geography()], WKB_GEOGRAPHY), + }), + ]) +} + +#[derive(Debug)] +struct STExteriorRing { + matcher: ArgMatcher, +} + +impl SedonaScalarKernel for STExteriorRing { + fn return_type(&self, args: &[SedonaType]) -> Result<Option<SedonaType>> { + self.matcher.match_args(args) + } + + fn invoke_batch( + &self, + arg_types: &[SedonaType], + args: &[ColumnarValue], + ) -> Result<ColumnarValue> { + let executor = GeosExecutor::new(arg_types, args); + let mut builder = BinaryBuilder::with_capacity( + executor.num_iterations(), + WKB_MIN_PROBABLE_BYTES * executor.num_iterations(), + ); + executor.execute_wkb_void(|maybe_geom| { + match maybe_geom { + Some(geom) => { + if invoke_scalar(&geom, &mut builder)? { + builder.append_value([]); + } else { + builder.append_null(); + } + } + _ => builder.append_null(), + } + Ok(()) + })?; + + executor.finish(Arc::new(builder.finish())) + } +} + +fn invoke_scalar(geom: &Geometry, writer: &mut impl std::io::Write) -> Result<bool> { + let geom_type = geom + .geometry_type() + .map_err(|e| DataFusionError::Execution(format!("Failed to get geometry type: {e}")))?; + + if geom_type != GeometryTypes::Polygon { + return Ok(false); + } + + let ring = geom + .get_exterior_ring() + .map_err(|e| DataFusionError::Execution(format!("ST_ExteriorRing failed: {e}")))?; + let line = + Geometry::create_line_string(ring.get_coord_seq().map_err(|e| { + DataFusionError::Execution(format!("Failed to get ring coordinates: {e}")) Review Comment: ```suggestion exec_datafusion_err!("Failed to get ring coordinates: {e}") ``` ########## docs/reference/sql/st_buildarea.qmd: ########## @@ -0,0 +1,41 @@ +--- +# 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. + +title: ST_BuildArea +description: Returns a geometry that encloses the area formed by the given linework. +kernels: + - returns: geometry + args: [geometry] + - returns: geography + args: [geography] Review Comment: ```suggestion ``` -- 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]
