pranav-walimbe commented on code in PR #1043:
URL: https://github.com/apache/sedona-db/pull/1043#discussion_r3566677130


##########
rust/sedona-geo/src/st_convexhull_agg.rs:
##########
@@ -0,0 +1,820 @@
+// 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, Int32Builder};
+use arrow_array::{Array, ArrayRef, BooleanArray};
+use arrow_schema::{DataType, Field, FieldRef};
+use datafusion_common::{
+    cast::{as_binary_array, as_int32_array},
+    error::Result,
+    exec_err, DataFusionError, ScalarValue,
+};
+use datafusion_expr::{Accumulator, ColumnarValue, EmitTo, GroupsAccumulator};
+use geo::{algorithm::convex_hull::quick_hull, Coord};
+use geo_traits::{Dimensions, GeometryTrait};
+use sedona_common::sedona_internal_err;
+use sedona_expr::{
+    aggregate_udf::{SedonaAccumulator, SedonaAccumulatorRef},
+    item_crs::ItemCrsSedonaAccumulator,
+};
+use sedona_functions::executor::WkbExecutor;
+use sedona_geometry::bounds::visit_xy_coords;
+use sedona_geometry::wkb_factory::{
+    write_wkb_geometrycollection_header, write_wkb_linestring, 
write_wkb_point, write_wkb_polygon,
+    WKB_MIN_PROBABLE_BYTES,
+};
+use sedona_schema::{
+    datatypes::{SedonaType, WKB_GEOMETRY},
+    matchers::ArgMatcher,
+};
+use wkb::reader::read_wkb;
+
+/// ST_ConvexHull_Agg() implementation
+pub fn st_convexhull_agg_impl() -> Vec<SedonaAccumulatorRef> {
+    ItemCrsSedonaAccumulator::wrap_impl(STConvexHullAgg {})
+}
+
+#[derive(Debug)]
+struct STConvexHullAgg {}
+
+impl SedonaAccumulator for STConvexHullAgg {
+    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(ConvexHullAccumulator::new(args[0].clone())))
+    }
+
+    fn groups_accumulator_supported(&self, _args: &[SedonaType]) -> bool {
+        true
+    }
+
+    fn groups_accumulator(
+        &self,
+        args: &[SedonaType],
+        _output_type: &SedonaType,
+    ) -> Result<Box<dyn GroupsAccumulator>> {
+        Ok(Box::new(ConvexHullGroupsAccumulator::new(args[0].clone())))
+    }
+
+    fn state_fields(&self, _args: &[SedonaType]) -> Result<Vec<FieldRef>> {
+        Ok(vec![
+            Arc::new(WKB_GEOMETRY.to_storage_field("hull", true)?),
+            Arc::new(Field::new("dimension", DataType::Int32, true)),
+        ])
+    }
+}
+
+fn push_hull_coords(geom: impl GeometryTrait<T = f64>, out: &mut Vec<Coord>) 
-> Result<()> {
+    visit_xy_coords(geom, false, &mut |x, y| out.push((x, y).into()))
+        .map_err(|e| DataFusionError::Execution(format!("ST_ConvexHull_Agg(): 
{e}")))
+}
+
+fn dimension_code(dimensions: Dimensions) -> i32 {
+    match dimensions {
+        Dimensions::Xy => 0,
+        Dimensions::Xyz => 1,
+        Dimensions::Xym => 2,
+        Dimensions::Xyzm => 3,
+        Dimensions::Unknown(_) => 4,
+    }
+}
+
+fn observe_dimension(state: &mut Option<i32>, code: i32) {
+    // Some(-1) is a sentinel meaning mixed dimensions were observed
+    *state = match *state {
+        Some(seen) if seen != code => Some(-1),
+        _ => Some(code),
+    };
+}
+
+fn merge_dimension(state: &mut Option<i32>, other: Option<i32>) {
+    if let Some(code) = other {
+        observe_dimension(state, code);
+    }
+}
+
+fn check_dimension(state: Option<i32>) -> Result<()> {
+    if state == Some(-1) {
+        exec_err!("Can't ST_ConvexHull_Agg() mixed dimension geometries")
+    } else {
+        Ok(())
+    }
+}

Review Comment:
   fixed



-- 
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]

Reply via email to