paleolimbot commented on code in PR #777: URL: https://github.com/apache/sedona-db/pull/777#discussion_r3222365532
########## rust/sedona-functions/src/st_linesubstring.rs: ########## @@ -0,0 +1,205 @@ +// 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 crate::executor::WkbExecutor; +use arrow_array::builder::BinaryBuilder; +use arrow_schema::DataType; +use datafusion_common::{error::Result, DataFusionError, ScalarValue}; +use datafusion_expr::{ColumnarValue, Volatility}; +use geo_traits::{CoordTrait, Dimensions, GeometryTrait, GeometryType, LineStringTrait}; +use sedona_expr::{ + item_crs::ItemCrsKernel, + scalar_udf::{SedonaScalarKernel, SedonaScalarUDF}, +}; +use sedona_geometry::error::SedonaGeometryError; +use sedona_geometry::wkb_factory::write_wkb_coord_trait; +use sedona_schema::{ + datatypes::{SedonaType, WKB_GEOMETRY}, + matchers::ArgMatcher, +}; +use std::{io::Write, sync::Arc}; + +#[derive(Debug)] +struct STLineSubstring; +pub fn st_line_substring_udf() -> SedonaScalarUDF { + SedonaScalarUDF::new( + "st_linesubstring", + ItemCrsKernel::wrap_impl(vec![Arc::new(STLineSubstring)]), + Volatility::Immutable, + ) +} + +impl SedonaScalarKernel for STLineSubstring { + fn return_type(&self, args: &[SedonaType]) -> Result<Option<SedonaType>> { + let matcher = ArgMatcher::new( + vec![ + ArgMatcher::is_geometry(), + ArgMatcher::is_numeric(), + ArgMatcher::is_numeric(), + ], + WKB_GEOMETRY, + ); + matcher.match_args(args) + } + + fn invoke_batch( + &self, + arg_types: &[SedonaType], + args: &[ColumnarValue], + ) -> Result<ColumnarValue> { + let executor = WkbExecutor::new(arg_types, args); + let mut builder = BinaryBuilder::new(); + + let start_frac: Option<f64> = match &args[1].cast_to(&DataType::Float64, None)? { + ColumnarValue::Scalar(ScalarValue::Float64(s)) => *s, + _ => None, + }; + + let end_frac: Option<f64> = match &args[2].cast_to(&DataType::Float64, None)? { + ColumnarValue::Scalar(ScalarValue::Float64(e)) => *e, + _ => None, + }; + + unsafe fn interpolate<C: CoordTrait<T = f64>>( + p1: C, + p2: C, + fraction: f64, + dim: Dimensions, + buf: &mut impl Write, + ) -> Result<(), SedonaGeometryError> { + for i in 0..dim.size() { + let v = + p1.nth_unchecked(i) + (p2.nth_unchecked(i) - p1.nth_unchecked(i)) * fraction; + buf.write_all(&v.to_le_bytes())?; + } + Ok(()) + } + + executor.execute_wkb_void(|maybe_wkb| unsafe { + let mut wkb_body = Vec::new(); + let mut point_count = 0u32; + + let (s_f, e_f) = match (start_frac, end_frac) { + (Some(s), Some(e)) => (s, e), + _ => { + builder.append_null(); + return Ok(()); + } + }; + + if let Some(wkb) = maybe_wkb { + if let GeometryType::LineString(line) = wkb.as_type() { + let num_coords = line.num_coords(); + let dim = line.dim(); + + let mut cumulative_distances = Vec::with_capacity(num_coords); + let mut total_length = 0.0; + cumulative_distances.push(0.0); + + for i in 0..(num_coords - 1) { + let p1 = line.coord(i).unwrap(); + let p2 = line.coord(i + 1).unwrap(); + let dist = ((p2.x() - p1.x()).powi(2) + (p2.y() - p1.y()).powi(2)).sqrt(); + total_length += dist; + cumulative_distances.push(total_length); + } + + let start_dist = s_f * total_length; + let end_dist = e_f * total_length; + + for i in 0..(num_coords - 1) { + let d1 = cumulative_distances[i]; + let d2 = cumulative_distances[i + 1]; + let p1 = line.coord(i).unwrap(); + let p2 = line.coord(i + 1).unwrap(); + + if start_dist >= d1 && start_dist <= d2 { + let segment_len = d2 - d1; + let fraction = if segment_len > 0.0 { + (start_dist - d1) / segment_len + } else { + 0.0 + }; + interpolate(p1, p2, fraction, dim, &mut wkb_body).map_err(|e| { + DataFusionError::Internal(format!( + "Sedona interpolation failed: {}", + e + )) + })?; + point_count += 1; + } + + if d1 > start_dist && d1 < end_dist { + write_wkb_coord_trait(&mut wkb_body, &p1).map_err(|e| { + DataFusionError::Internal(format!("WKB write failed: {}", e)) + })?; + point_count += 1; + } + + if end_dist >= d1 && end_dist <= d2 { + let segment_len = d2 - d1; + let fraction = if segment_len > 0.0 { + (end_dist - d1) / segment_len + } else { + 0.0 + }; + interpolate(p1, p2, fraction, dim, &mut wkb_body).map_err(|e| { + DataFusionError::Internal(format!( + "Sedona interpolation failed: {}", + e + )) + })?; + point_count += 1; + } + } + + // 2. Build Header inside the 'line' scope (Fixes "cannot find dim/line") + if point_count > 0 { + let mut final_wkb = Vec::new(); + final_wkb.push(1u8); // Little Endian + + if s_f == e_f { + // POINT Result (Fixes point vs line test) + let p_type: u32 = if dim == Dimensions::Xyz { 1001 } else { 1 }; + final_wkb.extend_from_slice(&p_type.to_le_bytes()); + let coord_bytes = dim.size() * 8; + if wkb_body.len() >= coord_bytes { + final_wkb.extend_from_slice(&wkb_body[..coord_bytes]); + } + } else { + // LINESTRING Result (Fixes Z-coordinate drop) + let l_type: u32 = if dim == Dimensions::Xyz { 1002 } else { 2 }; + final_wkb.extend_from_slice(&l_type.to_le_bytes()); + final_wkb.extend_from_slice(&point_count.to_le_bytes()); + final_wkb.extend_from_slice(&wkb_body); + } + builder.append_value(final_wkb); + } else { + builder.append_null(); + } + } else { + builder.append_null(); + } Review Comment: Should non-linestrings really return null here or should they error? (You can check by adding a non-linestring case to the Python integration tests where they also run against PostGIS) ########## Cargo.toml: ########## @@ -92,6 +92,7 @@ datafusion-datasource-parquet = { version = "52.5.0" } datafusion-execution = { version = "52.5.0", default-features = false } datafusion-expr = { version = "52.5.0" } datafusion-ffi = { version = "52.5.0" } +datafusion-functions-nested = { version = "52.5.0" } Review Comment: Was it necessary to add this? ########## rust/sedona-functions/src/st_linesubstring.rs: ########## @@ -0,0 +1,205 @@ +// 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 crate::executor::WkbExecutor; +use arrow_array::builder::BinaryBuilder; +use arrow_schema::DataType; +use datafusion_common::{error::Result, DataFusionError, ScalarValue}; +use datafusion_expr::{ColumnarValue, Volatility}; +use geo_traits::{CoordTrait, Dimensions, GeometryTrait, GeometryType, LineStringTrait}; +use sedona_expr::{ + item_crs::ItemCrsKernel, + scalar_udf::{SedonaScalarKernel, SedonaScalarUDF}, +}; +use sedona_geometry::error::SedonaGeometryError; +use sedona_geometry::wkb_factory::write_wkb_coord_trait; +use sedona_schema::{ + datatypes::{SedonaType, WKB_GEOMETRY}, + matchers::ArgMatcher, +}; +use std::{io::Write, sync::Arc}; + +#[derive(Debug)] +struct STLineSubstring; +pub fn st_line_substring_udf() -> SedonaScalarUDF { + SedonaScalarUDF::new( + "st_linesubstring", + ItemCrsKernel::wrap_impl(vec![Arc::new(STLineSubstring)]), + Volatility::Immutable, + ) +} + +impl SedonaScalarKernel for STLineSubstring { + fn return_type(&self, args: &[SedonaType]) -> Result<Option<SedonaType>> { + let matcher = ArgMatcher::new( + vec![ + ArgMatcher::is_geometry(), + ArgMatcher::is_numeric(), + ArgMatcher::is_numeric(), + ], + WKB_GEOMETRY, + ); + matcher.match_args(args) + } + + fn invoke_batch( + &self, + arg_types: &[SedonaType], + args: &[ColumnarValue], + ) -> Result<ColumnarValue> { + let executor = WkbExecutor::new(arg_types, args); + let mut builder = BinaryBuilder::new(); + + let start_frac: Option<f64> = match &args[1].cast_to(&DataType::Float64, None)? { + ColumnarValue::Scalar(ScalarValue::Float64(s)) => *s, + _ => None, + }; + + let end_frac: Option<f64> = match &args[2].cast_to(&DataType::Float64, None)? { + ColumnarValue::Scalar(ScalarValue::Float64(e)) => *e, + _ => None, + }; + + unsafe fn interpolate<C: CoordTrait<T = f64>>( + p1: C, + p2: C, + fraction: f64, + dim: Dimensions, + buf: &mut impl Write, + ) -> Result<(), SedonaGeometryError> { + for i in 0..dim.size() { + let v = + p1.nth_unchecked(i) + (p2.nth_unchecked(i) - p1.nth_unchecked(i)) * fraction; + buf.write_all(&v.to_le_bytes())?; + } + Ok(()) + } + + executor.execute_wkb_void(|maybe_wkb| unsafe { + let mut wkb_body = Vec::new(); + let mut point_count = 0u32; + + let (s_f, e_f) = match (start_frac, end_frac) { + (Some(s), Some(e)) => (s, e), + _ => { + builder.append_null(); + return Ok(()); + } + }; + + if let Some(wkb) = maybe_wkb { + if let GeometryType::LineString(line) = wkb.as_type() { + let num_coords = line.num_coords(); + let dim = line.dim(); + + let mut cumulative_distances = Vec::with_capacity(num_coords); + let mut total_length = 0.0; + cumulative_distances.push(0.0); + + for i in 0..(num_coords - 1) { + let p1 = line.coord(i).unwrap(); + let p2 = line.coord(i + 1).unwrap(); + let dist = ((p2.x() - p1.x()).powi(2) + (p2.y() - p1.y()).powi(2)).sqrt(); + total_length += dist; + cumulative_distances.push(total_length); + } + + let start_dist = s_f * total_length; + let end_dist = e_f * total_length; + + for i in 0..(num_coords - 1) { + let d1 = cumulative_distances[i]; + let d2 = cumulative_distances[i + 1]; + let p1 = line.coord(i).unwrap(); + let p2 = line.coord(i + 1).unwrap(); + + if start_dist >= d1 && start_dist <= d2 { + let segment_len = d2 - d1; + let fraction = if segment_len > 0.0 { + (start_dist - d1) / segment_len + } else { + 0.0 + }; + interpolate(p1, p2, fraction, dim, &mut wkb_body).map_err(|e| { + DataFusionError::Internal(format!( + "Sedona interpolation failed: {}", + e + )) + })?; + point_count += 1; + } + + if d1 > start_dist && d1 < end_dist { + write_wkb_coord_trait(&mut wkb_body, &p1).map_err(|e| { + DataFusionError::Internal(format!("WKB write failed: {}", e)) + })?; + point_count += 1; + } + + if end_dist >= d1 && end_dist <= d2 { + let segment_len = d2 - d1; + let fraction = if segment_len > 0.0 { + (end_dist - d1) / segment_len + } else { + 0.0 + }; + interpolate(p1, p2, fraction, dim, &mut wkb_body).map_err(|e| { + DataFusionError::Internal(format!( + "Sedona interpolation failed: {}", + e + )) + })?; + point_count += 1; + } + } + + // 2. Build Header inside the 'line' scope (Fixes "cannot find dim/line") + if point_count > 0 { + let mut final_wkb = Vec::new(); + final_wkb.push(1u8); // Little Endian + + if s_f == e_f { + // POINT Result (Fixes point vs line test) + let p_type: u32 = if dim == Dimensions::Xyz { 1001 } else { 1 }; + final_wkb.extend_from_slice(&p_type.to_le_bytes()); + let coord_bytes = dim.size() * 8; + if wkb_body.len() >= coord_bytes { + final_wkb.extend_from_slice(&wkb_body[..coord_bytes]); + } + } else { + // LINESTRING Result (Fixes Z-coordinate drop) + let l_type: u32 = if dim == Dimensions::Xyz { 1002 } else { 2 }; + final_wkb.extend_from_slice(&l_type.to_le_bytes()); + final_wkb.extend_from_slice(&point_count.to_le_bytes()); + final_wkb.extend_from_slice(&wkb_body); + } + builder.append_value(final_wkb); + } else { + builder.append_null(); + } + } else { + builder.append_null(); + } + } else { + builder.append_null(); + } + + Ok(()) + })?; + + executor.finish(Arc::new(builder.finish())) + } +} Review Comment: Before this merges it will also need a `mod test {}` like those in the other sedona-functions implementations. These include enough pure Rust test coverage to cover the main functionality including Scalar and Array inputs in execution and any errors that should occur. Basically, enough to cover the branches of the implementation. ########## python/sedonadb/tests/functions/test_linesubstring.py: ########## @@ -0,0 +1,45 @@ +# 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. + +import pytest +from sedonadb.testing import geom_or_null, PostGIS, SedonaDB + + [email protected]("eng", [SedonaDB, PostGIS]) [email protected]( + ("geom", "start", "end", "expected"), + [ + (None, 0.0, 1.0, None), + ("LINESTRING (0 0, 10 0)", 0.2, 0.8, "LINESTRING (2 0, 8 0)"), + ("LINESTRING (0 0, 10 10)", 0.3, 0.6, "LINESTRING (3 3, 6 6)"), + ("LINESTRING (0 0, 10 10)", 0.5, 0.5, "POINT (5 5)"), + ("LINESTRING (0 0, 10 0, 10 10)", 0.25, 0.75, "LINESTRING (5 0, 10 0, 10 5)"), + ( + "LINESTRING Z (0 0 0, 10 10 10)", + 0.5, + 0.8, + "LINESTRING Z (5 5 5, 8 8 8)", + ), Review Comment: Can you add: - Z, M, and ZM cases to ensure dimensions are propagated correctly? You will want to use different numbers for the coordinates to ensure the dimensions are not mixed up (e.g., `LINESTRING Z (0 10 20, 10 20 30)`) - Cases that cover different values of start/end (for the same 2D linestring) ########## rust/sedona-functions/src/st_linesubstring.rs: ########## @@ -0,0 +1,205 @@ +// 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 crate::executor::WkbExecutor; +use arrow_array::builder::BinaryBuilder; +use arrow_schema::DataType; +use datafusion_common::{error::Result, DataFusionError, ScalarValue}; +use datafusion_expr::{ColumnarValue, Volatility}; +use geo_traits::{CoordTrait, Dimensions, GeometryTrait, GeometryType, LineStringTrait}; +use sedona_expr::{ + item_crs::ItemCrsKernel, + scalar_udf::{SedonaScalarKernel, SedonaScalarUDF}, +}; +use sedona_geometry::error::SedonaGeometryError; +use sedona_geometry::wkb_factory::write_wkb_coord_trait; +use sedona_schema::{ + datatypes::{SedonaType, WKB_GEOMETRY}, + matchers::ArgMatcher, +}; +use std::{io::Write, sync::Arc}; + +#[derive(Debug)] +struct STLineSubstring; +pub fn st_line_substring_udf() -> SedonaScalarUDF { + SedonaScalarUDF::new( + "st_linesubstring", + ItemCrsKernel::wrap_impl(vec![Arc::new(STLineSubstring)]), + Volatility::Immutable, + ) +} + +impl SedonaScalarKernel for STLineSubstring { + fn return_type(&self, args: &[SedonaType]) -> Result<Option<SedonaType>> { + let matcher = ArgMatcher::new( + vec![ + ArgMatcher::is_geometry(), + ArgMatcher::is_numeric(), + ArgMatcher::is_numeric(), + ], + WKB_GEOMETRY, + ); + matcher.match_args(args) + } + + fn invoke_batch( + &self, + arg_types: &[SedonaType], + args: &[ColumnarValue], + ) -> Result<ColumnarValue> { + let executor = WkbExecutor::new(arg_types, args); + let mut builder = BinaryBuilder::new(); + + let start_frac: Option<f64> = match &args[1].cast_to(&DataType::Float64, None)? { + ColumnarValue::Scalar(ScalarValue::Float64(s)) => *s, + _ => None, + }; + + let end_frac: Option<f64> = match &args[2].cast_to(&DataType::Float64, None)? { + ColumnarValue::Scalar(ScalarValue::Float64(e)) => *e, + _ => None, + }; Review Comment: This covers the case where these are constants but doesn't cover the case where you have Array input here. Typically we use something like `let start_frac_arrayref = args[1].cast_to(&DataType::Float64, None)?.to_array(executor.num_iterations());` and get an iterator over the float64s. I think the implementation for `st_pointn()` has an example you can draw from. ########## Cargo.lock: ########## Review Comment: I am not sure these updates are needed but I'm happy to push that commit to fix this when we get that close to merging 🙂 ########## python/sedonadb/tests/functions/test_linesubstring.py: ########## @@ -0,0 +1,46 @@ +# 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. + +import pytest +from sedonadb.testing import geom_or_null, PostGIS, SedonaDB + + [email protected]("eng", [SedonaDB, PostGIS]) [email protected]( + ("geom", "start", "end", "expected"), + [ + (None, 0.0, 1.0, None), Review Comment: Can you add these cases to ensure proper null semantics? ########## rust/sedona-functions/src/st_linesubstring.rs: ########## @@ -0,0 +1,205 @@ +// 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 crate::executor::WkbExecutor; +use arrow_array::builder::BinaryBuilder; +use arrow_schema::DataType; +use datafusion_common::{error::Result, DataFusionError, ScalarValue}; +use datafusion_expr::{ColumnarValue, Volatility}; +use geo_traits::{CoordTrait, Dimensions, GeometryTrait, GeometryType, LineStringTrait}; +use sedona_expr::{ + item_crs::ItemCrsKernel, + scalar_udf::{SedonaScalarKernel, SedonaScalarUDF}, +}; +use sedona_geometry::error::SedonaGeometryError; +use sedona_geometry::wkb_factory::write_wkb_coord_trait; +use sedona_schema::{ + datatypes::{SedonaType, WKB_GEOMETRY}, + matchers::ArgMatcher, +}; +use std::{io::Write, sync::Arc}; + +#[derive(Debug)] +struct STLineSubstring; +pub fn st_line_substring_udf() -> SedonaScalarUDF { + SedonaScalarUDF::new( + "st_linesubstring", + ItemCrsKernel::wrap_impl(vec![Arc::new(STLineSubstring)]), + Volatility::Immutable, + ) +} + +impl SedonaScalarKernel for STLineSubstring { + fn return_type(&self, args: &[SedonaType]) -> Result<Option<SedonaType>> { + let matcher = ArgMatcher::new( + vec![ + ArgMatcher::is_geometry(), + ArgMatcher::is_numeric(), + ArgMatcher::is_numeric(), + ], + WKB_GEOMETRY, + ); + matcher.match_args(args) + } + + fn invoke_batch( + &self, + arg_types: &[SedonaType], + args: &[ColumnarValue], + ) -> Result<ColumnarValue> { + let executor = WkbExecutor::new(arg_types, args); + let mut builder = BinaryBuilder::new(); + + let start_frac: Option<f64> = match &args[1].cast_to(&DataType::Float64, None)? { + ColumnarValue::Scalar(ScalarValue::Float64(s)) => *s, + _ => None, + }; + + let end_frac: Option<f64> = match &args[2].cast_to(&DataType::Float64, None)? { + ColumnarValue::Scalar(ScalarValue::Float64(e)) => *e, + _ => None, + }; + + unsafe fn interpolate<C: CoordTrait<T = f64>>( + p1: C, + p2: C, + fraction: f64, + dim: Dimensions, + buf: &mut impl Write, + ) -> Result<(), SedonaGeometryError> { + for i in 0..dim.size() { + let v = + p1.nth_unchecked(i) + (p2.nth_unchecked(i) - p1.nth_unchecked(i)) * fraction; + buf.write_all(&v.to_le_bytes())?; + } + Ok(()) + } + + executor.execute_wkb_void(|maybe_wkb| unsafe { + let mut wkb_body = Vec::new(); + let mut point_count = 0u32; + + let (s_f, e_f) = match (start_frac, end_frac) { + (Some(s), Some(e)) => (s, e), + _ => { + builder.append_null(); + return Ok(()); + } + }; + + if let Some(wkb) = maybe_wkb { + if let GeometryType::LineString(line) = wkb.as_type() { + let num_coords = line.num_coords(); + let dim = line.dim(); + + let mut cumulative_distances = Vec::with_capacity(num_coords); + let mut total_length = 0.0; + cumulative_distances.push(0.0); + + for i in 0..(num_coords - 1) { + let p1 = line.coord(i).unwrap(); + let p2 = line.coord(i + 1).unwrap(); + let dist = ((p2.x() - p1.x()).powi(2) + (p2.y() - p1.y()).powi(2)).sqrt(); + total_length += dist; + cumulative_distances.push(total_length); + } + + let start_dist = s_f * total_length; + let end_dist = e_f * total_length; + + for i in 0..(num_coords - 1) { + let d1 = cumulative_distances[i]; + let d2 = cumulative_distances[i + 1]; + let p1 = line.coord(i).unwrap(); + let p2 = line.coord(i + 1).unwrap(); + + if start_dist >= d1 && start_dist <= d2 { + let segment_len = d2 - d1; + let fraction = if segment_len > 0.0 { + (start_dist - d1) / segment_len + } else { + 0.0 + }; + interpolate(p1, p2, fraction, dim, &mut wkb_body).map_err(|e| { + DataFusionError::Internal(format!( + "Sedona interpolation failed: {}", + e + )) + })?; + point_count += 1; + } + + if d1 > start_dist && d1 < end_dist { + write_wkb_coord_trait(&mut wkb_body, &p1).map_err(|e| { + DataFusionError::Internal(format!("WKB write failed: {}", e)) + })?; + point_count += 1; + } + + if end_dist >= d1 && end_dist <= d2 { + let segment_len = d2 - d1; + let fraction = if segment_len > 0.0 { + (end_dist - d1) / segment_len + } else { + 0.0 + }; + interpolate(p1, p2, fraction, dim, &mut wkb_body).map_err(|e| { + DataFusionError::Internal(format!( + "Sedona interpolation failed: {}", + e + )) + })?; + point_count += 1; + } + } + + // 2. Build Header inside the 'line' scope (Fixes "cannot find dim/line") + if point_count > 0 { + let mut final_wkb = Vec::new(); + final_wkb.push(1u8); // Little Endian + + if s_f == e_f { + // POINT Result (Fixes point vs line test) + let p_type: u32 = if dim == Dimensions::Xyz { 1001 } else { 1 }; + final_wkb.extend_from_slice(&p_type.to_le_bytes()); + let coord_bytes = dim.size() * 8; + if wkb_body.len() >= coord_bytes { + final_wkb.extend_from_slice(&wkb_body[..coord_bytes]); + } + } else { + // LINESTRING Result (Fixes Z-coordinate drop) + let l_type: u32 = if dim == Dimensions::Xyz { 1002 } else { 2 }; + final_wkb.extend_from_slice(&l_type.to_le_bytes()); + final_wkb.extend_from_slice(&point_count.to_le_bytes()); + final_wkb.extend_from_slice(&wkb_body); + } Review Comment: We have utilities in `sedona_geometry::wkb_factory` for this (e.g., write the linestring header or write the point header). This should propagate the dimensions of the input (this section special-cases Z but this could be M or ZM). -- 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]
