-
Notifications
You must be signed in to change notification settings - Fork 1.3k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat: point #3583
Merged
abonander
merged 12 commits into
launchbadge:main
from
jayy-lmao:feat/geometry-postgres-point
Nov 27, 2024
Merged
feat: point #3583
Changes from 1 commit
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
d20e384
feat: point
jayy-lmao 42f58a4
test: try if eq operator works for arrays of geometries
jayy-lmao 82a21c3
fix: re-introduce comparison
jayy-lmao ea3975e
fix: test other geometry comparison
jayy-lmao 83dcf2a
test: geometry array equality check
jayy-lmao ece19be
test: array match for geo arrays geo match for geo only
jayy-lmao a88cf4f
fix: prepare geometric array type
jayy-lmao caea709
fix: update array comparison
jayy-lmao 7f6df9f
fix: try another method of geometric array comparison
jayy-lmao fe99e48
fix: one more geometry match tests
jayy-lmao 6a0548b
fix: correct query syntax
jayy-lmao 184a0ef
test: geometry test further
jayy-lmao File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
pub mod point; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,138 @@ | ||
use crate::decode::Decode; | ||
use crate::encode::{Encode, IsNull}; | ||
use crate::error::BoxDynError; | ||
use crate::types::Type; | ||
use crate::{PgArgumentBuffer, PgHasArrayType, PgTypeInfo, PgValueFormat, PgValueRef, Postgres}; | ||
use sqlx_core::bytes::Buf; | ||
use sqlx_core::Error; | ||
use std::str::FromStr; | ||
|
||
/// ## Postgres Geometric Point type | ||
/// | ||
/// Description: Point on a plane | ||
/// Representation: `(x, y)` | ||
/// | ||
/// Points are the fundamental two-dimensional building block for geometric types. Values of type point are specified using either of the following syntaxes: | ||
/// ```text | ||
/// ( x , y ) | ||
/// x , y | ||
/// ```` | ||
/// where x and y are the respective coordinates, as floating-point numbers. | ||
/// | ||
/// See https://www.postgresql.org/docs/16/datatype-geometric.html#DATATYPE-GEOMETRIC-POINTS | ||
#[derive(Debug, Clone, PartialEq)] | ||
pub struct PgPoint { | ||
pub x: f64, | ||
pub y: f64, | ||
} | ||
|
||
impl Type<Postgres> for PgPoint { | ||
fn type_info() -> PgTypeInfo { | ||
PgTypeInfo::with_name("point") | ||
} | ||
} | ||
|
||
impl PgHasArrayType for PgPoint { | ||
fn array_type_info() -> PgTypeInfo { | ||
PgTypeInfo::with_name("_point") | ||
} | ||
} | ||
|
||
impl<'r> Decode<'r, Postgres> for PgPoint { | ||
fn decode(value: PgValueRef<'r>) -> Result<Self, Box<dyn std::error::Error + Send + Sync>> { | ||
match value.format() { | ||
PgValueFormat::Text => Ok(PgPoint::from_str(value.as_str()?)?), | ||
PgValueFormat::Binary => Ok(PgPoint::from_bytes(value.as_bytes()?)?), | ||
} | ||
} | ||
} | ||
|
||
impl<'q> Encode<'q, Postgres> for PgPoint { | ||
fn produces(&self) -> Option<PgTypeInfo> { | ||
Some(PgTypeInfo::with_name("point")) | ||
} | ||
|
||
fn encode_by_ref(&self, buf: &mut PgArgumentBuffer) -> Result<IsNull, BoxDynError> { | ||
self.serialize(buf)?; | ||
Ok(IsNull::No) | ||
} | ||
} | ||
|
||
fn parse_float_from_str(s: &str, error_msg: &str) -> Result<f64, Error> { | ||
s.trim() | ||
.parse() | ||
.map_err(|_| Error::Decode(error_msg.into())) | ||
} | ||
|
||
impl FromStr for PgPoint { | ||
type Err = BoxDynError; | ||
|
||
fn from_str(s: &str) -> Result<Self, Self::Err> { | ||
let (x_str, y_str) = s | ||
.trim_matches(|c| c == '(' || c == ')' || c == ' ') | ||
.split_once(',') | ||
.ok_or_else(|| format!("error decoding POINT: could not get x and y from {}", s))?; | ||
|
||
let x = parse_float_from_str(x_str, "error decoding POINT: could not get x")?; | ||
let y = parse_float_from_str(y_str, "error decoding POINT: could not get x")?; | ||
|
||
Ok(PgPoint { x, y }) | ||
} | ||
} | ||
|
||
impl PgPoint { | ||
fn from_bytes(mut bytes: &[u8]) -> Result<PgPoint, BoxDynError> { | ||
let x = bytes.get_f64(); | ||
let y = bytes.get_f64(); | ||
Ok(PgPoint { x, y }) | ||
} | ||
|
||
fn serialize(&self, buff: &mut PgArgumentBuffer) -> Result<(), BoxDynError> { | ||
buff.extend_from_slice(&self.x.to_be_bytes()); | ||
buff.extend_from_slice(&self.y.to_be_bytes()); | ||
Ok(()) | ||
} | ||
|
||
#[cfg(test)] | ||
fn serialize_to_vec(&self) -> Vec<u8> { | ||
let mut buff = PgArgumentBuffer::default(); | ||
self.serialize(&mut buff).unwrap(); | ||
buff.to_vec() | ||
} | ||
} | ||
|
||
#[cfg(test)] | ||
mod point_tests { | ||
|
||
use std::str::FromStr; | ||
|
||
use super::PgPoint; | ||
|
||
const POINT_BYTES: &[u8] = &[ | ||
64, 0, 204, 204, 204, 204, 204, 205, 64, 20, 204, 204, 204, 204, 204, 205, | ||
]; | ||
|
||
#[test] | ||
fn can_deserialise_point_type_bytes() { | ||
let point = PgPoint::from_bytes(POINT_BYTES).unwrap(); | ||
assert_eq!(point, PgPoint { x: 2.1, y: 5.2 }) | ||
} | ||
|
||
#[test] | ||
fn can_deserialise_point_type_str() { | ||
let point = PgPoint::from_str("(2, 3)").unwrap(); | ||
assert_eq!(point, PgPoint { x: 2., y: 3. }); | ||
} | ||
|
||
#[test] | ||
fn can_deserialise_point_type_str_float() { | ||
let point = PgPoint::from_str("(2.5, 3.4)").unwrap(); | ||
assert_eq!(point, PgPoint { x: 2.5, y: 3.4 }); | ||
} | ||
|
||
#[test] | ||
fn can_serialise_point_type() { | ||
let point = PgPoint { x: 2.1, y: 5.2 }; | ||
assert_eq!(point.serialize_to_vec(), POINT_BYTES,) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why is this necessary? What happens without it?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We get the error
could not identify an equality operator for type point
without itWe can't use the
SELECT ({0} is not distinct from $1)::int4, {0}, $2
from https://github.com/jayy-lmao/sqlx/blob/feat/geometry-postgres-point/sqlx-test/src/lib.rs#L230 as the comparisonhttps://github.com/launchbadge/sqlx/actions/runs/11679795147/job/32521594696
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I see.
=
compares area for the two-dimensional types, and there's a different operator for equality,~=
.https://www.postgresql.org/docs/current/functions-geometry.html
We should use that rather than converting from text because there could be differences in rounding between Postgres and Rust that alter the text output of the types.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Took a bit of a fiddle, and needed to split between array/non-array variants.
This is what worked in the end, text avoided: https://github.com/launchbadge/sqlx/pull/3583/files#diff-31a2fb7770cf7cd19a7baadb82c0ccfa1721d92c1eeb4d3f7f9a675de54f20b0R247-R251