Copilot commented on code in PR #547: URL: https://github.com/apache/sedona-db/pull/547#discussion_r2728877286
########## rust/sedona-functions/src/st_geomfromewkb.rs: ########## @@ -0,0 +1,206 @@ +// 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, vec}; + +use arrow_array::builder::{BinaryBuilder, StringViewBuilder}; +use arrow_schema::DataType; +use datafusion_common::{error::Result, exec_datafusion_err, ScalarValue}; +use datafusion_expr::{ + scalar_doc_sections::DOC_SECTION_OTHER, ColumnarValue, Documentation, Volatility, +}; +use sedona_common::sedona_internal_err; +use sedona_expr::{ + item_crs::make_item_crs, + scalar_udf::{SedonaScalarKernel, SedonaScalarUDF}, +}; +use sedona_geometry::{wkb_factory::WKB_MIN_PROBABLE_BYTES, wkb_header::WkbHeader}; +use sedona_schema::{ + datatypes::{SedonaType, WKB_GEOMETRY, WKB_GEOMETRY_ITEM_CRS, WKB_VIEW_GEOGRAPHY}, + matchers::ArgMatcher, +}; + +use crate::executor::WkbExecutor; + +/// ST_GeomFromEWKB() scalar UDF implementation +/// +/// An implementation of EWKB reading using GeoRust's wkb crate and our internal +/// WkbHeader utility. +pub fn st_geomfromewkb_udf() -> SedonaScalarUDF { + SedonaScalarUDF::new( + "st_geomfromewkb", + vec![Arc::new(STGeomFromEWKB {})], + Volatility::Immutable, + Some(doc()), + ) +} + +fn doc() -> Documentation { + Documentation::builder( + DOC_SECTION_OTHER, + "Construct a geometry from EWKB".to_string(), + "ST_GeomFromEWKB (Wkb: Binary)".to_string(), + ) + .with_argument( + "EWKB", + "binary: Extended well-known binary (EWKB) representation of the geometry".to_string(), + ) + .with_sql_example("SELECT ST_GeomFromEWKB([01 02 00 00 00 02 00 00 00 00 00 00 00 84 D6 00 C0 00 00 00 00 80 B5 D6 BF 00 00 00 60 E1 EF F7 BF 00 00 00 80 07 5D E5 BF])") + .build() +} + +#[derive(Debug)] +struct STGeomFromEWKB {} + +impl SedonaScalarKernel for STGeomFromEWKB { + fn return_type(&self, args: &[SedonaType]) -> Result<Option<SedonaType>> { + let matcher = ArgMatcher::new(vec![ArgMatcher::is_binary()], WKB_GEOMETRY_ITEM_CRS.clone()); + matcher.match_args(args) + } + + fn invoke_batch( + &self, + arg_types: &[SedonaType], + args: &[ColumnarValue], + ) -> Result<ColumnarValue> { + let iter_type = match &arg_types[0] { + SedonaType::Arrow(data_type) => match data_type { + DataType::Binary => WKB_GEOMETRY, + DataType::BinaryView => WKB_VIEW_GEOGRAPHY, + DataType::Null => SedonaType::Arrow(DataType::Null), + _ => { + return sedona_internal_err!( + "Unexpected arguments to invoke_batch: {arg_types:?}" + ) + } + }, + _ => { + return sedona_internal_err!("Unexpected arguments to invoke_batch: {arg_types:?}") + } + }; + + let temp_args = [iter_type]; + let executor = WkbExecutor::new(&temp_args, args); + let mut geom_builder = BinaryBuilder::with_capacity( + executor.num_iterations(), + WKB_MIN_PROBABLE_BYTES * executor.num_iterations(), + ); + let mut srid_builder = StringViewBuilder::with_capacity(executor.num_iterations()); + + executor.execute_wkb_void(|maybe_item| { + match maybe_item { + Some(item) => { + let header = + WkbHeader::try_new(item.buf()).map_err(|e| exec_datafusion_err!("{e}"))?; + let maybe_crs = match header.srid() { + 0 => None, + valid_srid => Some(format!("EPSG:{valid_srid}")), + }; + + wkb::writer::write_geometry(&mut geom_builder, &item, &Default::default()) + .map_err(|e| exec_datafusion_err!("{e}"))?; + geom_builder.append_value([]); Review Comment: Line 115 appends an empty byte array after writing the geometry, which corrupts the WKB data. The geometry was already written to the builder on line 113, so this line should be removed. ```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]
