|
| 1 | +// Licensed to the Apache Software Foundation (ASF) under one |
| 2 | +// or more contributor license agreements. See the NOTICE file |
| 3 | +// distributed with this work for additional information |
| 4 | +// regarding copyright ownership. The ASF licenses this file |
| 5 | +// to you under the Apache License, Version 2.0 (the |
| 6 | +// "License"); you may not use this file except in compliance |
| 7 | +// with the License. You may obtain a copy of the License at |
| 8 | +// |
| 9 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | +// |
| 11 | +// Unless required by applicable law or agreed to in writing, |
| 12 | +// software distributed under the License is distributed on an |
| 13 | +// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 14 | +// KIND, either express or implied. See the License for the |
| 15 | +// specific language governing permissions and limitations |
| 16 | +// under the License. |
| 17 | +use arrow_array::builder::UInt32Builder; |
| 18 | +use std::{sync::Arc, vec}; |
| 19 | + |
| 20 | +use crate::executor::WkbExecutor; |
| 21 | +use arrow_schema::DataType; |
| 22 | +use datafusion_common::{DataFusionError, Result}; |
| 23 | +use datafusion_expr::{ |
| 24 | + scalar_doc_sections::DOC_SECTION_OTHER, ColumnarValue, Documentation, Volatility, |
| 25 | +}; |
| 26 | +use sedona_expr::scalar_udf::{ArgMatcher, SedonaScalarKernel, SedonaScalarUDF}; |
| 27 | +use sedona_schema::datatypes::SedonaType; |
| 28 | + |
| 29 | +/// ST_Srid() scalar UDF implementation |
| 30 | +/// |
| 31 | +/// Scalar function to return the SRID of a geometry or geography |
| 32 | +pub fn st_srid_udf() -> SedonaScalarUDF { |
| 33 | + SedonaScalarUDF::new( |
| 34 | + "st_srid", |
| 35 | + vec![Arc::new(StSrid {})], |
| 36 | + Volatility::Immutable, |
| 37 | + Some(st_srid_doc()), |
| 38 | + ) |
| 39 | +} |
| 40 | + |
| 41 | +fn st_srid_doc() -> Documentation { |
| 42 | + Documentation::builder( |
| 43 | + DOC_SECTION_OTHER, |
| 44 | + "Return the spatial reference system identifier (SRID) of the geometry.", |
| 45 | + "ST_SRID (geom: Geometry)", |
| 46 | + ) |
| 47 | + .with_argument("geom", "geometry: Input geometry or geography") |
| 48 | + .with_sql_example("SELECT ST_SRID(polygon)".to_string()) |
| 49 | + .build() |
| 50 | +} |
| 51 | + |
| 52 | +#[derive(Debug)] |
| 53 | +struct StSrid {} |
| 54 | + |
| 55 | +impl SedonaScalarKernel for StSrid { |
| 56 | + fn return_type(&self, args: &[SedonaType]) -> Result<Option<SedonaType>> { |
| 57 | + let matcher = ArgMatcher::new( |
| 58 | + vec![ArgMatcher::is_geometry_or_geography()], |
| 59 | + SedonaType::Arrow(DataType::UInt32), |
| 60 | + ); |
| 61 | + |
| 62 | + matcher.match_args(args) |
| 63 | + } |
| 64 | + |
| 65 | + fn invoke_batch( |
| 66 | + &self, |
| 67 | + arg_types: &[SedonaType], |
| 68 | + args: &[ColumnarValue], |
| 69 | + ) -> Result<ColumnarValue> { |
| 70 | + let executor = WkbExecutor::new(arg_types, args); |
| 71 | + let mut builder = UInt32Builder::with_capacity(executor.num_iterations()); |
| 72 | + let srid_opt = match &arg_types[0] { |
| 73 | + SedonaType::Wkb(_, Some(crs)) | SedonaType::WkbView(_, Some(crs)) => { |
| 74 | + match crs.srid()? { |
| 75 | + Some(srid) => Some(srid), |
| 76 | + None => return Err(DataFusionError::Execution("CRS has no SRID".to_string())), |
| 77 | + } |
| 78 | + } |
| 79 | + _ => Some(0), |
| 80 | + }; |
| 81 | + |
| 82 | + executor.execute_wkb_void(|maybe_wkb| { |
| 83 | + match maybe_wkb { |
| 84 | + Some(_wkb) => { |
| 85 | + builder.append_option(srid_opt); |
| 86 | + } |
| 87 | + _ => builder.append_null(), |
| 88 | + } |
| 89 | + |
| 90 | + Ok(()) |
| 91 | + })?; |
| 92 | + |
| 93 | + executor.finish(Arc::new(builder.finish())) |
| 94 | + } |
| 95 | +} |
| 96 | + |
| 97 | +#[cfg(test)] |
| 98 | +mod test { |
| 99 | + use datafusion_common::ScalarValue; |
| 100 | + use datafusion_expr::ScalarUDF; |
| 101 | + use sedona_schema::crs::deserialize_crs; |
| 102 | + use sedona_schema::datatypes::Edges; |
| 103 | + use sedona_testing::testers::ScalarUdfTester; |
| 104 | + use std::str::FromStr; |
| 105 | + |
| 106 | + use super::*; |
| 107 | + |
| 108 | + #[test] |
| 109 | + fn udf_metadata() { |
| 110 | + let udf: ScalarUDF = st_srid_udf().into(); |
| 111 | + assert_eq!(udf.name(), "st_srid"); |
| 112 | + assert!(udf.documentation().is_some()) |
| 113 | + } |
| 114 | + |
| 115 | + #[test] |
| 116 | + fn udf() { |
| 117 | + let udf: ScalarUDF = st_srid_udf().into(); |
| 118 | + |
| 119 | + // Test that when no CRS is set, SRID is 0 |
| 120 | + let sedona_type = SedonaType::Wkb(Edges::Planar, None); |
| 121 | + let tester = ScalarUdfTester::new(udf.clone(), vec![sedona_type]); |
| 122 | + tester.assert_return_type(DataType::UInt32); |
| 123 | + let result = tester |
| 124 | + .invoke_scalar("POLYGON ((0 0, 1 0, 0 1, 0 0))") |
| 125 | + .unwrap(); |
| 126 | + tester.assert_scalar_result_equals(result, 0_u32); |
| 127 | + |
| 128 | + // Test that NULL input returns NULL output |
| 129 | + let result = tester.invoke_scalar(ScalarValue::Null).unwrap(); |
| 130 | + tester.assert_scalar_result_equals(result, ScalarValue::Null); |
| 131 | + |
| 132 | + // Test with a CRS with an EPSG code |
| 133 | + let crs_value = serde_json::Value::String("EPSG:4837".to_string()); |
| 134 | + let crs = deserialize_crs(&crs_value).unwrap(); |
| 135 | + let sedona_type = SedonaType::Wkb(Edges::Planar, crs.clone()); |
| 136 | + let tester = ScalarUdfTester::new(udf.clone(), vec![sedona_type]); |
| 137 | + let result = tester |
| 138 | + .invoke_scalar("POLYGON ((0 0, 1 0, 0 1, 0 0))") |
| 139 | + .unwrap(); |
| 140 | + tester.assert_scalar_result_equals(result, 4837_u32); |
| 141 | + |
| 142 | + // Test with a CRS but null geom |
| 143 | + let result = tester.invoke_scalar(ScalarValue::Null).unwrap(); |
| 144 | + tester.assert_scalar_result_equals(result, ScalarValue::Null); |
| 145 | + |
| 146 | + // Call with a CRS with no SRID (should error) |
| 147 | + let crs_value = serde_json::Value::from_str("{}"); |
| 148 | + let crs = deserialize_crs(&crs_value.unwrap()).unwrap(); |
| 149 | + let sedona_type = SedonaType::Wkb(Edges::Planar, crs.clone()); |
| 150 | + let tester = ScalarUdfTester::new(udf.clone(), vec![sedona_type]); |
| 151 | + let result = tester.invoke_scalar("POINT (0 1)"); |
| 152 | + assert!(result.is_err()); |
| 153 | + assert!(result.unwrap_err().to_string().contains("CRS has no SRID")); |
| 154 | + } |
| 155 | +} |
0 commit comments