Skip to content

Commit ca3a509

Browse files
jayy-lmaojoeydewaalabonander
authored
feat(postgres): add geometry polygon (#3769)
* feat: add polygon * test: paths for pgpoints in polygon test * fix: import typo * chore(Sqlite): remove ci.db from repo (#3768) * fix: CI * Fix breakage from Rustup 1.28 <https://blog.rust-lang.org/2025/03/02/Rustup-1.28.0.html> * Let `Swatinem/rust-cache` generate cache keys * fix(ci): upgrade Ubuntu image to 24.04 For some reason the `cargo +beta clippy` step is failing because `libsqlite3-sys` starts requiring Glibc >= 2.39 but I don't have time to figure out why and I can't reproduce it in a clean environment. --------- Co-authored-by: joeydewaal <[email protected]> Co-authored-by: Austin Bonander <[email protected]>
1 parent a92626d commit ca3a509

File tree

5 files changed

+377
-0
lines changed

5 files changed

+377
-0
lines changed

sqlx-postgres/src/type_checking.rs

+2
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,8 @@ impl_type_checking!(
4242

4343
sqlx::postgres::types::PgPath,
4444

45+
sqlx::postgres::types::PgPolygon,
46+
4547
#[cfg(feature = "uuid")]
4648
sqlx::types::Uuid,
4749

sqlx-postgres/src/types/geometry/mod.rs

+1
Original file line numberDiff line numberDiff line change
@@ -3,3 +3,4 @@ pub mod line;
33
pub mod line_segment;
44
pub mod path;
55
pub mod point;
6+
pub mod polygon;
+363
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,363 @@
1+
use crate::decode::Decode;
2+
use crate::encode::{Encode, IsNull};
3+
use crate::error::BoxDynError;
4+
use crate::types::{PgPoint, Type};
5+
use crate::{PgArgumentBuffer, PgHasArrayType, PgTypeInfo, PgValueFormat, PgValueRef, Postgres};
6+
use sqlx_core::bytes::Buf;
7+
use sqlx_core::Error;
8+
use std::mem;
9+
use std::str::FromStr;
10+
11+
const BYTE_WIDTH: usize = mem::size_of::<f64>();
12+
13+
/// ## Postgres Geometric Polygon type
14+
///
15+
/// Description: Polygon (similar to closed polygon)
16+
/// Representation: `((x1,y1),...)`
17+
///
18+
/// Polygons are represented by lists of points (the vertexes of the polygon). Polygons are very similar to closed paths; the essential semantic difference is that a polygon is considered to include the area within it, while a path is not.
19+
/// An important implementation difference between polygons and paths is that the stored representation of a polygon includes its smallest bounding box. This speeds up certain search operations, although computing the bounding box adds overhead while constructing new polygons.
20+
/// Values of type polygon are specified using any of the following syntaxes:
21+
///
22+
/// ```text
23+
/// ( ( x1 , y1 ) , ... , ( xn , yn ) )
24+
/// ( x1 , y1 ) , ... , ( xn , yn )
25+
/// ( x1 , y1 , ... , xn , yn )
26+
/// x1 , y1 , ... , xn , yn
27+
/// ```
28+
///
29+
/// where the points are the end points of the line segments comprising the boundary of the polygon.
30+
///
31+
/// Seeh ttps://www.postgresql.org/docs/16/datatype-geometric.html#DATATYPE-POLYGON
32+
#[derive(Debug, Clone, PartialEq)]
33+
pub struct PgPolygon {
34+
pub points: Vec<PgPoint>,
35+
}
36+
37+
#[derive(Copy, Clone, Debug, PartialEq, Eq)]
38+
struct Header {
39+
length: usize,
40+
}
41+
42+
impl Type<Postgres> for PgPolygon {
43+
fn type_info() -> PgTypeInfo {
44+
PgTypeInfo::with_name("polygon")
45+
}
46+
}
47+
48+
impl PgHasArrayType for PgPolygon {
49+
fn array_type_info() -> PgTypeInfo {
50+
PgTypeInfo::with_name("_polygon")
51+
}
52+
}
53+
54+
impl<'r> Decode<'r, Postgres> for PgPolygon {
55+
fn decode(value: PgValueRef<'r>) -> Result<Self, Box<dyn std::error::Error + Send + Sync>> {
56+
match value.format() {
57+
PgValueFormat::Text => Ok(PgPolygon::from_str(value.as_str()?)?),
58+
PgValueFormat::Binary => Ok(PgPolygon::from_bytes(value.as_bytes()?)?),
59+
}
60+
}
61+
}
62+
63+
impl<'q> Encode<'q, Postgres> for PgPolygon {
64+
fn produces(&self) -> Option<PgTypeInfo> {
65+
Some(PgTypeInfo::with_name("polygon"))
66+
}
67+
68+
fn encode_by_ref(&self, buf: &mut PgArgumentBuffer) -> Result<IsNull, BoxDynError> {
69+
self.serialize(buf)?;
70+
Ok(IsNull::No)
71+
}
72+
}
73+
74+
impl FromStr for PgPolygon {
75+
type Err = Error;
76+
77+
fn from_str(s: &str) -> Result<Self, Self::Err> {
78+
let sanitised = s.replace(['(', ')', '[', ']', ' '], "");
79+
let parts = sanitised.split(',').collect::<Vec<_>>();
80+
81+
let mut points = vec![];
82+
83+
if parts.len() % 2 != 0 {
84+
return Err(Error::Decode(
85+
format!("Unmatched pair in POLYGON: {}", s).into(),
86+
));
87+
}
88+
89+
for chunk in parts.chunks_exact(2) {
90+
if let [x_str, y_str] = chunk {
91+
let x = parse_float_from_str(x_str, "could not get x")?;
92+
let y = parse_float_from_str(y_str, "could not get y")?;
93+
94+
let point = PgPoint { x, y };
95+
points.push(point);
96+
}
97+
}
98+
99+
if !points.is_empty() {
100+
return Ok(PgPolygon { points });
101+
}
102+
103+
Err(Error::Decode(
104+
format!("could not get polygon from {}", s).into(),
105+
))
106+
}
107+
}
108+
109+
impl PgPolygon {
110+
fn header(&self) -> Header {
111+
Header {
112+
length: self.points.len(),
113+
}
114+
}
115+
116+
fn from_bytes(mut bytes: &[u8]) -> Result<Self, BoxDynError> {
117+
let header = Header::try_read(&mut bytes)?;
118+
119+
if bytes.len() != header.data_size() {
120+
return Err(format!(
121+
"expected {} bytes after header, got {}",
122+
header.data_size(),
123+
bytes.len()
124+
)
125+
.into());
126+
}
127+
128+
if bytes.len() % BYTE_WIDTH * 2 != 0 {
129+
return Err(format!(
130+
"data length not divisible by pairs of {BYTE_WIDTH}: {}",
131+
bytes.len()
132+
)
133+
.into());
134+
}
135+
136+
let mut out_points = Vec::with_capacity(bytes.len() / (BYTE_WIDTH * 2));
137+
while bytes.has_remaining() {
138+
let point = PgPoint {
139+
x: bytes.get_f64(),
140+
y: bytes.get_f64(),
141+
};
142+
out_points.push(point)
143+
}
144+
Ok(PgPolygon { points: out_points })
145+
}
146+
147+
fn serialize(&self, buff: &mut PgArgumentBuffer) -> Result<(), BoxDynError> {
148+
let header = self.header();
149+
buff.reserve(header.data_size());
150+
header.try_write(buff)?;
151+
152+
for point in &self.points {
153+
buff.extend_from_slice(&point.x.to_be_bytes());
154+
buff.extend_from_slice(&point.y.to_be_bytes());
155+
}
156+
Ok(())
157+
}
158+
159+
#[cfg(test)]
160+
fn serialize_to_vec(&self) -> Vec<u8> {
161+
let mut buff = PgArgumentBuffer::default();
162+
self.serialize(&mut buff).unwrap();
163+
buff.to_vec()
164+
}
165+
}
166+
167+
impl Header {
168+
const HEADER_WIDTH: usize = mem::size_of::<i8>() + mem::size_of::<i32>();
169+
170+
fn data_size(&self) -> usize {
171+
self.length * BYTE_WIDTH * 2
172+
}
173+
174+
fn try_read(buf: &mut &[u8]) -> Result<Self, String> {
175+
if buf.len() < Self::HEADER_WIDTH {
176+
return Err(format!(
177+
"expected polygon data to contain at least {} bytes, got {}",
178+
Self::HEADER_WIDTH,
179+
buf.len()
180+
));
181+
}
182+
183+
let length = buf.get_i32();
184+
185+
let length = usize::try_from(length).ok().ok_or_else(|| {
186+
format!(
187+
"received polygon with length: {length}. Expected length between 0 and {}",
188+
usize::MAX
189+
)
190+
})?;
191+
192+
Ok(Self { length })
193+
}
194+
195+
fn try_write(&self, buff: &mut PgArgumentBuffer) -> Result<(), String> {
196+
let length = i32::try_from(self.length).map_err(|_| {
197+
format!(
198+
"polygon length exceeds allowed maximum ({} > {})",
199+
self.length,
200+
i32::MAX
201+
)
202+
})?;
203+
204+
buff.extend(length.to_be_bytes());
205+
206+
Ok(())
207+
}
208+
}
209+
210+
fn parse_float_from_str(s: &str, error_msg: &str) -> Result<f64, Error> {
211+
s.parse().map_err(|_| Error::Decode(error_msg.into()))
212+
}
213+
214+
#[cfg(test)]
215+
mod polygon_tests {
216+
217+
use std::str::FromStr;
218+
219+
use crate::types::PgPoint;
220+
221+
use super::PgPolygon;
222+
223+
const POLYGON_BYTES: &[u8] = &[
224+
0, 0, 0, 12, 192, 0, 0, 0, 0, 0, 0, 0, 192, 8, 0, 0, 0, 0, 0, 0, 191, 240, 0, 0, 0, 0, 0,
225+
0, 192, 8, 0, 0, 0, 0, 0, 0, 191, 240, 0, 0, 0, 0, 0, 0, 191, 240, 0, 0, 0, 0, 0, 0, 63,
226+
240, 0, 0, 0, 0, 0, 0, 63, 240, 0, 0, 0, 0, 0, 0, 63, 240, 0, 0, 0, 0, 0, 0, 64, 8, 0, 0,
227+
0, 0, 0, 0, 64, 0, 0, 0, 0, 0, 0, 0, 64, 8, 0, 0, 0, 0, 0, 0, 64, 0, 0, 0, 0, 0, 0, 0, 192,
228+
8, 0, 0, 0, 0, 0, 0, 63, 240, 0, 0, 0, 0, 0, 0, 192, 8, 0, 0, 0, 0, 0, 0, 63, 240, 0, 0, 0,
229+
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 191, 240, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 191,
230+
240, 0, 0, 0, 0, 0, 0, 192, 0, 0, 0, 0, 0, 0, 0, 192, 0, 0, 0, 0, 0, 0, 0, 192, 0, 0, 0, 0,
231+
0, 0, 0,
232+
];
233+
234+
#[test]
235+
fn can_deserialise_polygon_type_bytes() {
236+
let polygon = PgPolygon::from_bytes(POLYGON_BYTES).unwrap();
237+
assert_eq!(
238+
polygon,
239+
PgPolygon {
240+
points: vec![
241+
PgPoint { x: -2., y: -3. },
242+
PgPoint { x: -1., y: -3. },
243+
PgPoint { x: -1., y: -1. },
244+
PgPoint { x: 1., y: 1. },
245+
PgPoint { x: 1., y: 3. },
246+
PgPoint { x: 2., y: 3. },
247+
PgPoint { x: 2., y: -3. },
248+
PgPoint { x: 1., y: -3. },
249+
PgPoint { x: 1., y: 0. },
250+
PgPoint { x: -1., y: 0. },
251+
PgPoint { x: -1., y: -2. },
252+
PgPoint { x: -2., y: -2. }
253+
]
254+
}
255+
)
256+
}
257+
258+
#[test]
259+
fn can_deserialise_polygon_type_str_first_syntax() {
260+
let polygon = PgPolygon::from_str("[( 1, 2), (3, 4 )]").unwrap();
261+
assert_eq!(
262+
polygon,
263+
PgPolygon {
264+
points: vec![PgPoint { x: 1., y: 2. }, PgPoint { x: 3., y: 4. }]
265+
}
266+
);
267+
}
268+
269+
#[test]
270+
fn can_deserialise_polygon_type_str_second_syntax() {
271+
let polygon = PgPolygon::from_str("(( 1, 2), (3, 4 ))").unwrap();
272+
assert_eq!(
273+
polygon,
274+
PgPolygon {
275+
points: vec![PgPoint { x: 1., y: 2. }, PgPoint { x: 3., y: 4. }]
276+
}
277+
);
278+
}
279+
280+
#[test]
281+
fn cannot_deserialise_polygon_type_str_uneven_points_first_syntax() {
282+
let input_str = "[( 1, 2), (3)]";
283+
let polygon = PgPolygon::from_str(input_str);
284+
285+
assert!(polygon.is_err());
286+
287+
if let Err(err) = polygon {
288+
assert_eq!(
289+
err.to_string(),
290+
format!("error occurred while decoding: Unmatched pair in POLYGON: {input_str}")
291+
)
292+
}
293+
}
294+
295+
#[test]
296+
fn cannot_deserialise_polygon_type_str_invalid_numbers() {
297+
let input_str = "[( 1, 2), (2, three)]";
298+
let polygon = PgPolygon::from_str(input_str);
299+
300+
assert!(polygon.is_err());
301+
302+
if let Err(err) = polygon {
303+
assert_eq!(
304+
err.to_string(),
305+
format!("error occurred while decoding: could not get y")
306+
)
307+
}
308+
}
309+
310+
#[test]
311+
fn can_deserialise_polygon_type_str_third_syntax() {
312+
let polygon = PgPolygon::from_str("(1, 2), (3, 4 )").unwrap();
313+
assert_eq!(
314+
polygon,
315+
PgPolygon {
316+
points: vec![PgPoint { x: 1., y: 2. }, PgPoint { x: 3., y: 4. }]
317+
}
318+
);
319+
}
320+
321+
#[test]
322+
fn can_deserialise_polygon_type_str_fourth_syntax() {
323+
let polygon = PgPolygon::from_str("1, 2, 3, 4").unwrap();
324+
assert_eq!(
325+
polygon,
326+
PgPolygon {
327+
points: vec![PgPoint { x: 1., y: 2. }, PgPoint { x: 3., y: 4. }]
328+
}
329+
);
330+
}
331+
332+
#[test]
333+
fn can_deserialise_polygon_type_str_float() {
334+
let polygon = PgPolygon::from_str("(1.1, 2.2), (3.3, 4.4)").unwrap();
335+
assert_eq!(
336+
polygon,
337+
PgPolygon {
338+
points: vec![PgPoint { x: 1.1, y: 2.2 }, PgPoint { x: 3.3, y: 4.4 }]
339+
}
340+
);
341+
}
342+
343+
#[test]
344+
fn can_serialise_polygon_type() {
345+
let polygon = PgPolygon {
346+
points: vec![
347+
PgPoint { x: -2., y: -3. },
348+
PgPoint { x: -1., y: -3. },
349+
PgPoint { x: -1., y: -1. },
350+
PgPoint { x: 1., y: 1. },
351+
PgPoint { x: 1., y: 3. },
352+
PgPoint { x: 2., y: 3. },
353+
PgPoint { x: 2., y: -3. },
354+
PgPoint { x: 1., y: -3. },
355+
PgPoint { x: 1., y: 0. },
356+
PgPoint { x: -1., y: 0. },
357+
PgPoint { x: -1., y: -2. },
358+
PgPoint { x: -2., y: -2. },
359+
],
360+
};
361+
assert_eq!(polygon.serialize_to_vec(), POLYGON_BYTES,)
362+
}
363+
}

sqlx-postgres/src/types/mod.rs

+2
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
//! | [`PgLSeg`] | LSEG |
2727
//! | [`PgBox`] | BOX |
2828
//! | [`PgPath`] | PATH |
29+
//! | [`PgPolygon`] | POLYGON |
2930
//! | [`PgHstore`] | HSTORE |
3031
//!
3132
//! <sup>1</sup> SQLx generally considers `CITEXT` to be compatible with `String`, `&str`, etc.,
@@ -265,6 +266,7 @@ pub use geometry::line::PgLine;
265266
pub use geometry::line_segment::PgLSeg;
266267
pub use geometry::path::PgPath;
267268
pub use geometry::point::PgPoint;
269+
pub use geometry::polygon::PgPolygon;
268270
pub use geometry::r#box::PgBox;
269271
pub use hstore::PgHstore;
270272
pub use interval::PgInterval;

0 commit comments

Comments
 (0)