Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 6 additions & 2 deletions python/sedonadb/python/sedonadb/testing.py
Original file line number Diff line number Diff line change
Expand Up @@ -635,11 +635,15 @@ def __init__(self, uri=None):
cur.execute("SET max_parallel_workers_per_gather TO 0")


def geom_or_null(arg):
def geom_or_null(arg, srid=None):
"""Format SQL expression for a geometry object or NULL"""
if arg is None:
return "NULL"
return f"ST_GeomFromText('{arg}')"

if srid is None:
return f"ST_GeomFromText('{arg}')"
else:
return f"ST_GeomFromEWKT('SRID={srid};{arg}')"


def geog_or_null(arg):
Expand Down
93 changes: 93 additions & 0 deletions python/sedonadb/tests/functions/test_wkb.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
# 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
import shapely
from sedonadb.testing import PostGIS, SedonaDB, geom_or_null


@pytest.mark.parametrize("eng", [SedonaDB, PostGIS])
@pytest.mark.parametrize("srid", [None, 4326])
@pytest.mark.parametrize(
"geom",
[
# XY dimensions
"POINT (1 2)",
"LINESTRING (1 2, 3 4, 5 6)",
"POLYGON ((0 1, 2 0, 2 3, 0 3, 0 1))",
"MULTIPOINT ((1 2), (3 4))",
"MULTILINESTRING ((1 2, 3 4), (5 6, 7 8))",
"MULTIPOLYGON (((0 1, 2 0, 2 3, 0 3, 0 1)))",
"GEOMETRYCOLLECTION (POINT (1 2), LINESTRING (3 4, 5 6))",
# XYZ dimensions
"POINT Z (1 2 3)",
"LINESTRING Z (1 2 3, 4 5 6)",
"POLYGON Z ((0 1 2, 3 0 2, 3 4 2, 0 4 2, 0 1 2))",
"MULTIPOINT Z ((1 2 3), (4 5 6))",
"MULTILINESTRING Z ((1 2 3, 4 5 6), (7 8 9, 10 11 12))",
"MULTIPOLYGON Z (((0 1 2, 3 0 2, 3 4 2, 0 4 2, 0 1 2)))",
"GEOMETRYCOLLECTION Z (POINT Z (1 2 3))",
# XYM dimensions
"POINT M (1 2 3)",
"LINESTRING M (1 2 3, 4 5 6)",
"POLYGON M ((0 1 2, 3 0 2, 3 4 2, 0 4 2, 0 1 2))",
"MULTIPOINT M ((1 2 3), (4 5 6))",
"MULTILINESTRING M ((1 2 3, 4 5 6), (7 8 9, 10 11 12))",
"MULTIPOLYGON M (((0 1 2, 3 0 2, 3 4 2, 0 4 2, 0 1 2)))",
"GEOMETRYCOLLECTION M (POINT M (1 2 3))",
# XYZM dimensions
"POINT ZM (1 2 3 4)",
"LINESTRING ZM (1 2 3 4, 5 6 7 8)",
"POLYGON ZM ((0 1 2 3, 4 0 2 3, 4 5 2 3, 0 5 2 3, 0 1 2 3))",
"MULTIPOINT ZM ((1 2 3 4), (5 6 7 8))",
"MULTILINESTRING ZM ((1 2 3 4, 5 6 7 8), (9 10 11 12, 13 14 15 16))",
"MULTIPOLYGON ZM (((0 1 2 3, 4 0 2 3, 4 5 2 3, 0 5 2 3, 0 1 2 3)))",
"GEOMETRYCOLLECTION ZM (POINT ZM (1 2 3 4))",
# Empty geometries
"POINT EMPTY",
"LINESTRING EMPTY",
"POLYGON EMPTY",
"MULTIPOINT EMPTY",
"MULTILINESTRING EMPTY",
"MULTIPOLYGON EMPTY",
"GEOMETRYCOLLECTION EMPTY",
# NULL
None,
],
)
def test_st_asewkb(eng, srid, geom):
eng = eng.create_or_skip()

if geom is not None:
shapely_geom = shapely.from_wkt(geom)
if srid is not None:
shapely_geom = shapely.set_srid(shapely_geom, srid)
write_srid = True
else:
write_srid = False

expected = shapely.to_wkb(
shapely_geom,
output_dimension=4,
byte_order=1,
flavor="extended",
include_srid=write_srid,
)
else:
expected = None

eng.assert_query_result(f"SELECT ST_AsEWKB({geom_or_null(geom, srid)})", expected)
17 changes: 13 additions & 4 deletions rust/sedona-common/src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,10 @@
// specific language governing permissions and limitations
// under the License.

/// Macro to create Sedona Internal Error that avoids the misleading error message from
/// DataFusionError::Internal.
/// Macro to create Sedona Internal Error from places such as `map_err()` that
/// require a DataFusionError instead of an Err
#[macro_export]
macro_rules! sedona_internal_err {
macro_rules! sedona_internal_datafusion_err {
($($args:expr),*) => {{
let msg = std::format!(
"SedonaDB internal error: {}{}.\nThis issue was likely caused by a bug in SedonaDB's code. \
Expand All @@ -28,7 +28,16 @@ macro_rules! sedona_internal_err {
datafusion_common::DataFusionError::get_back_trace(),
);
// We avoid using Internal to avoid the message suggesting it's internal to DataFusion
Err(datafusion_common::DataFusionError::External(msg.into()))
datafusion_common::DataFusionError::External(msg.into())
}};
}

/// Macro to create Sedona Internal Error that avoids the misleading error message from
/// DataFusionError::Internal.
#[macro_export]
macro_rules! sedona_internal_err {
($($args:expr),*) => {{
Err($crate::sedona_internal_datafusion_err!($($args),*))
}};
}

Expand Down
14 changes: 14 additions & 0 deletions rust/sedona-functions/src/executor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -383,6 +383,20 @@ impl ScalarGeo for ScalarValue {
| ScalarValue::BinaryView(maybe_item)
| ScalarValue::LargeBinary(maybe_item) => Ok(maybe_item.as_deref()),
ScalarValue::Null => Ok(None),
ScalarValue::Struct(s)
if s.fields().len() == 2
&& s.fields()[0].name() == "item"
&& s.fields()[1].name() == "crs" =>
Comment on lines +386 to +389
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If I remember it correctly, this pattern for decomposing fields in struct type representing item with crs has appeared many times in the code base. Maybe we can define a helper method for doing this.

{
let item_type = SedonaType::from_storage_field(&s.fields()[0])?;
let mut out = None;
s.column(0).iter_as_wkb_bytes(&item_type, 1, |v| {
out = v;
Ok(())
})?;

Ok(out)
}
_ => sedona_internal_err!("Can't iterate over {:?} ScalarValue as &[u8]", self),
}
}
Expand Down
1 change: 1 addition & 0 deletions rust/sedona-functions/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ mod st_affine_helpers;
pub mod st_analyze_agg;
mod st_area;
mod st_asbinary;
mod st_asewkb;
mod st_asgeojson;
mod st_astext;
mod st_azimuth;
Expand Down
1 change: 1 addition & 0 deletions rust/sedona-functions/src/register.rs
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ pub fn default_function_set() -> FunctionSet {
crate::st_affine::st_affine_udf,
crate::st_area::st_area_udf,
crate::st_asbinary::st_asbinary_udf,
crate::st_asewkb::st_asewkb_udf,
crate::st_asgeojson::st_asgeojson_udf,
crate::st_astext::st_astext_udf,
crate::st_azimuth::st_azimuth_udf,
Expand Down
Loading