paleolimbot commented on code in PR #269: URL: https://github.com/apache/sedona-db/pull/269#discussion_r2491374723
########## rust/sedona-functions/src/st_dump.rs: ########## @@ -0,0 +1,433 @@ +// 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 arrow_array::{ + builder::{BinaryBuilder, ListBuilder, StructBuilder, UInt32Builder}, + ListArray, +}; +use arrow_schema::{DataType, Field, Fields}; +use datafusion_common::error::Result; +use datafusion_expr::{ + scalar_doc_sections::DOC_SECTION_OTHER, ColumnarValue, Documentation, Volatility, +}; +use geo_traits::{ + GeometryCollectionTrait, GeometryTrait, GeometryType, MultiLineStringTrait, MultiPointTrait, + MultiPolygonTrait, +}; +use sedona_common::sedona_internal_err; +use sedona_expr::scalar_udf::{SedonaScalarKernel, SedonaScalarUDF}; +use sedona_geometry::wkb_factory::WKB_MIN_PROBABLE_BYTES; +use sedona_schema::{ + datatypes::{SedonaType, WKB_GEOMETRY}, + matchers::ArgMatcher, +}; +use std::sync::Arc; + +use crate::executor::WkbExecutor; + +/// ST_Dump() scalar UDF +/// +/// Native implementation to get all the points of a geometry as MULTIPOINT +pub fn st_dump_udf() -> SedonaScalarUDF { + SedonaScalarUDF::new( + "st_dump", + vec![Arc::new(STDump)], + Volatility::Immutable, + Some(st_dump_doc()), + ) +} + +fn st_dump_doc() -> Documentation { + Documentation::builder( + DOC_SECTION_OTHER, + "Extracts the components of a geometry.", + "ST_Dump (geom: Geometry)", + ) + .with_argument("geom", "geometry: Input geometry") + .with_sql_example("SELECT ST_Dump(ST_GeomFromWKT('MULTIPOINT (0 1, 2 3, 4 5)'))") + .build() +} + +#[derive(Debug)] +struct STDump; + +// This enum is solely for passing the subset of wkb geometry to STDumpStructBuilder. +// Maybe we can pass the underlying raw WKB bytes directly, but this just works for now. +enum SingleWkb<'a> { + Point(&'a wkb::reader::Point<'a>), + LineString(&'a wkb::reader::LineString<'a>), + Polygon(&'a wkb::reader::Polygon<'a>), +} + +// A builder for a single struct of { path: [u32], geom: POINT | LINESTRING | POLYGON } +struct STDumpStructBuilder<'a> { + struct_builder: &'a mut StructBuilder, +} + +// A builder for a list of the structs +struct STDumpBuilder { + builder: ListBuilder<StructBuilder>, +} + +impl<'a> STDumpStructBuilder<'a> { + // This appends both path and geom at once. + fn append( + &mut self, + parent_path: &[u32], + cur_index: Option<u32>, + wkb: SingleWkb<'_>, + ) -> Result<()> { + let path_builder = self + .struct_builder + .field_builder::<ListBuilder<UInt32Builder>>(0) + .unwrap(); Review Comment: I am not sure it matters here given that you've nicely demonstrated that this is pretty fast, but we have tried in other places to avoid downcasting in a loop. Something like: ```rust struct STDumpBuilder { path_array_builder: UInt32Builder, path_array_offsets_builder: OffsetBufferBuilder, geom_builder: BinaryBuilder, } ``` ...and then implement a `finish() -> StructArray` that assembles at the end. (optional!) -- 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]
