Skip to content

Commit fc815ca

Browse files
authored
feat(query): implement ST_SRID (#15377)
ST_SRID Signed-off-by: Fan Yang <yangfanlinux@gmail.com>
1 parent cd371e8 commit fc815ca

File tree

5 files changed

+97
-0
lines changed

5 files changed

+97
-0
lines changed

src/query/functions/src/scalars/geometry.rs

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -865,6 +865,33 @@ pub fn register(registry: &mut FunctionRegistry) {
865865
),
866866
);
867867

868+
registry.register_combine_nullable_1_arg::<GeometryType, Int32Type, _, _>(
869+
"st_srid",
870+
|_, _| FunctionDomain::MayThrow,
871+
vectorize_with_builder_1_arg::<GeometryType, NullableType<Int32Type>>(
872+
|geometry, output, ctx| {
873+
if let Some(validity) = &ctx.validity {
874+
if !validity.get_bit(output.len()) {
875+
output.push_null();
876+
return;
877+
}
878+
}
879+
match read_ewkb_srid(&mut io::Cursor::new(&geometry)) {
880+
Ok(srid) => {
881+
output.push(srid.unwrap_or(4326));
882+
}
883+
Err(e) => {
884+
ctx.set_error(
885+
output.len(),
886+
ErrorCode::GeometryError(e.to_string()).to_string(),
887+
);
888+
output.push(0);
889+
}
890+
};
891+
},
892+
),
893+
);
894+
868895
registry.register_passthrough_nullable_1_arg::<GeometryType, StringType, _, _>(
869896
"to_string",
870897
|_, _| FunctionDomain::MayThrow,

src/query/functions/tests/it/scalars/geometry.rs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ fn test_geometry() {
4040
test_st_makepoint(file);
4141
test_st_makepolygon(file);
4242
test_st_pointn(file);
43+
test_st_srid(file);
4344
test_to_geometry(file);
4445
test_to_string(file);
4546
test_try_to_geometry(file);
@@ -241,6 +242,17 @@ fn test_st_pointn(file: &mut impl Write) {
241242
);
242243
}
243244

245+
fn test_st_srid(file: &mut impl Write) {
246+
run_ast(
247+
file,
248+
"st_srid(to_geometry('POINT(-122.306100 37.554162)', 1234))",
249+
&[],
250+
);
251+
run_ast(file, "st_srid(st_makegeompoint(37.5, 45.5))", &[]);
252+
run_ast(file, "st_srid(st_makegeompoint(NULL, NULL))", &[]);
253+
run_ast(file, "st_srid(NULL)", &[]);
254+
}
255+
244256
fn test_to_geometry(file: &mut impl Write) {
245257
run_ast(file, "to_geometry('POINT(1820.12 890.56)')", &[]);
246258
run_ast(file, "to_geometry('SRID=4326;POINT(1820.12 890.56)')", &[]);

src/query/functions/tests/it/scalars/testdata/function_list.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3548,6 +3548,8 @@ Functions overloads:
35483548
1 st_makepolygon(Geometry NULL) :: Geometry NULL
35493549
0 st_pointn(Geometry, Int32) :: Geometry
35503550
1 st_pointn(Geometry NULL, Int32 NULL) :: Geometry NULL
3551+
0 st_srid(Geometry) :: Int32 NULL
3552+
1 st_srid(Geometry NULL) :: Int32 NULL
35513553
0 strcmp(String, String) :: Int8
35523554
1 strcmp(String NULL, String NULL) :: Int8 NULL
35533555
0 string_to_h3(String) :: UInt64

src/query/functions/tests/it/scalars/testdata/geometry.txt

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -348,6 +348,42 @@ output domain : Undefined
348348
output : 'POINT(3 3)'
349349

350350

351+
ast : st_srid(to_geometry('POINT(-122.306100 37.554162)', 1234))
352+
raw expr : st_srid(to_geometry('POINT(-122.306100 37.554162)', 1234))
353+
checked expr : st_srid<Geometry>(to_geometry<String, Int32>("POINT(-122.306100 37.554162)", to_int32<UInt16>(1234_u16)))
354+
optimized expr : 1234_i32
355+
output type : Int32 NULL
356+
output domain : {1234..=1234}
357+
output : 1234
358+
359+
360+
ast : st_srid(st_makegeompoint(37.5, 45.5))
361+
raw expr : st_srid(st_makegeompoint(37.5, 45.5))
362+
checked expr : st_srid<Geometry>(st_makegeompoint<Float64, Float64>(to_float64<Decimal(3, 1)>(37.5_d128(3,1)), to_float64<Decimal(3, 1)>(45.5_d128(3,1))))
363+
optimized expr : 4326_i32
364+
output type : Int32 NULL
365+
output domain : {4326..=4326}
366+
output : 4326
367+
368+
369+
ast : st_srid(st_makegeompoint(NULL, NULL))
370+
raw expr : st_srid(st_makegeompoint(NULL, NULL))
371+
checked expr : st_srid<Geometry NULL>(st_makegeompoint<Float64 NULL, Float64 NULL>(CAST(NULL AS Float64 NULL), CAST(NULL AS Float64 NULL)))
372+
optimized expr : NULL
373+
output type : Int32 NULL
374+
output domain : {NULL}
375+
output : NULL
376+
377+
378+
ast : st_srid(NULL)
379+
raw expr : st_srid(NULL)
380+
checked expr : st_srid<Geometry NULL>(CAST(NULL AS Geometry NULL))
381+
optimized expr : NULL
382+
output type : Int32 NULL
383+
output domain : {NULL}
384+
output : NULL
385+
386+
351387
ast : to_geometry('POINT(1820.12 890.56)')
352388
raw expr : to_geometry('POINT(1820.12 890.56)')
353389
checked expr : to_geometry<String>("POINT(1820.12 890.56)")

tests/sqllogictests/suites/query/functions/02_0060_function_geometry.test

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -423,6 +423,26 @@ SELECT TO_STRING(ST_POINTN(TO_GEOMETRY('LINESTRING(1 1, 2 2, 3 3, 4 4)'), -2))
423423
POINT(3 3)
424424

425425

426+
query T
427+
SELECT ST_SRID(TO_GEOMETRY('POINT(-122.306100 37.554162)', 1234))
428+
----
429+
1234
430+
431+
query T
432+
SELECT ST_SRID(ST_MAKEGEOMPOINT(37.5, 45.5))
433+
----
434+
4326
435+
436+
query T
437+
SELECT ST_SRID(ST_MAKEGEOMPOINT(NULL, NULL))
438+
----
439+
NULL
440+
441+
query T
442+
SELECT ST_SRID(NULL)
443+
----
444+
NULL
445+
426446
statement ok
427447
SET enable_geo_create_table=0
428448

0 commit comments

Comments
 (0)