paleolimbot commented on code in PR #286: URL: https://github.com/apache/sedona-db/pull/286#discussion_r2511752765
########## c/sedona-geos/src/st_polygonize.rs: ########## @@ -0,0 +1,439 @@ +// 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::wkb_to_geos::GEOSWkbFactory; +use arrow_array::{ + builder::OffsetBufferBuilder, cast::as_list_array, Array, ArrayRef, BinaryArray, +}; +use arrow_schema::{DataType, Field, FieldRef}; +use datafusion_common::{cast::as_binary_array, error::Result, DataFusionError, ScalarValue}; +use datafusion_expr::{Accumulator, ColumnarValue}; +use geos::Geom; +use sedona_expr::aggregate_udf::{SedonaAccumulator, SedonaAccumulatorRef}; +use sedona_schema::{ + datatypes::{SedonaType, WKB_GEOMETRY}, + matchers::ArgMatcher, +}; +use wkb::reader::{read_wkb, Wkb}; + +thread_local! { + static GEOS_WKB_FACTORY: GEOSWkbFactory = GEOSWkbFactory::new(); +} + +fn wkb_to_geos_geometry(wkb: &Wkb) -> Result<geos::Geometry> { + GEOS_WKB_FACTORY.with(|factory| { + factory.create(wkb).map_err(|e| { + DataFusionError::Execution(format!("Failed to create geometry from WKB: {e}")) + }) + }) +} + +/// ST_Polygonize() aggregate implementation using GEOS +pub fn st_polygonize_impl() -> SedonaAccumulatorRef { + Arc::new(STPolygonize {}) +} + +#[derive(Debug)] +struct STPolygonize {} + +impl SedonaAccumulator for STPolygonize { + fn return_type(&self, args: &[SedonaType]) -> Result<Option<SedonaType>> { + let matcher = ArgMatcher::new(vec![ArgMatcher::is_geometry()], WKB_GEOMETRY); + matcher.match_args(args) + } + + fn accumulator( + &self, + args: &[SedonaType], + _output_type: &SedonaType, + ) -> Result<Box<dyn Accumulator>> { + Ok(Box::new(PolygonizeAccumulator::new(args[0].clone()))) + } + + fn state_fields(&self, _args: &[SedonaType]) -> Result<Vec<FieldRef>> { + Ok(vec![Arc::new(Field::new( + "geometries", + DataType::List(Arc::new(Field::new("item", DataType::Binary, true))), + false, + ))]) + } +} + +#[derive(Debug)] +struct PolygonizeAccumulator { + input_type: SedonaType, + geometries: Vec<Arc<[u8]>>, +} + +impl PolygonizeAccumulator { + pub fn new(input_type: SedonaType) -> Self { + Self { + input_type, + geometries: Vec::new(), + } + } + + fn make_wkb_result(&self) -> Result<Option<Vec<u8>>> { + if self.geometries.is_empty() { + return Ok(None); + } + + let mut geos_geoms = Vec::with_capacity(self.geometries.len()); + for wkb_bytes in &self.geometries { Review Comment: Right before this loop is where you could declare the function-level `GEOSWkbFactory`. If we run into performance issues with this function we can reevlauate whether a thread local is appropriate. ########## c/sedona-geos/src/st_polygonize.rs: ########## @@ -0,0 +1,439 @@ +// 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::wkb_to_geos::GEOSWkbFactory; +use arrow_array::{ + builder::OffsetBufferBuilder, cast::as_list_array, Array, ArrayRef, BinaryArray, +}; +use arrow_schema::{DataType, Field, FieldRef}; +use datafusion_common::{cast::as_binary_array, error::Result, DataFusionError, ScalarValue}; +use datafusion_expr::{Accumulator, ColumnarValue}; +use geos::Geom; +use sedona_expr::aggregate_udf::{SedonaAccumulator, SedonaAccumulatorRef}; +use sedona_schema::{ + datatypes::{SedonaType, WKB_GEOMETRY}, + matchers::ArgMatcher, +}; +use wkb::reader::{read_wkb, Wkb}; + +thread_local! { + static GEOS_WKB_FACTORY: GEOSWkbFactory = GEOSWkbFactory::new(); +} Review Comment: I don't think you need a thread local factory...we do that in at least one performance-critical place in SedonaDB but here I think it is sufficient to declare it once as a function-level variable before you loop over WKB geometries. -- 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]
