Skip to content

Commit

Permalink
Support printing postgis data. (#1425)
Browse files Browse the repository at this point in the history
@msullivan 

The tests run on a local instance and pass.

The query `with module ext::postgis select <geometry>'POINT(0 1)';` returns
```
{POINT(0 1)}
```

The box types have some special handling, which doesn't get properly handled in the CLI, which might be confusing.

The query `with module ext::postgis select <box2d>'box(0 0, 1 1)';` returns:
```
{POLYGON((0 0,0 1,1 1,1 0,0 0))}
```

The query `with module ext::postgis select <str><box2d>'BOX(0 0, 1 1)';` (note cast to str) returns:
```
{'BOX(0 0,1 1)'}
```

The javascript binding PR has some special checking for the boxes, but I haven't reproduced it here.
  • Loading branch information
dnwpark authored Dec 11, 2024
1 parent 90cdbb7 commit c829645
Show file tree
Hide file tree
Showing 5 changed files with 240 additions and 4 deletions.
75 changes: 71 additions & 4 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,7 @@ nom = "7.1.3"
bitflags = "2.6"
renamore = "0.3.2"
anes = "0.2.0"
geozero = {version="0.14.0",features=["with-wkb"]}

[dependencies.bzip2]
version = "*"
Expand Down
4 changes: 4 additions & 0 deletions src/outputs/tab_separated.rs
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,10 @@ fn value_to_string(v: &Value) -> Result<String, anyhow::Error> {
| Set(_)
| Tuple(_)
| Range {..}
| PostGisGeometry {..}
| PostGisGeography {..}
| PostGisBox2d {..}
| PostGisBox3d {..}
=> {
Err(anyhow::anyhow!(
"Complex objects like {:?} cannot be printed tab-separated",
Expand Down
17 changes: 17 additions & 0 deletions src/print/native.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ use crate::print::buffer::Result;
use crate::print::formatter::Formatter;
use crate::repl::VectorLimit;
use edgedb_protocol::value::Value;
use geozero::{wkb::Ewkb, ToWkt};

pub trait FormatExt {
fn format<F: Formatter>(&self, prn: &mut F) -> Result<F::Error>;
Expand Down Expand Up @@ -270,6 +271,22 @@ impl FormatExt for Value {
Ok(())
})
}
V::PostGisGeometry(v) => {
let wkb = Ewkb(v);
prn.const_string(wkb.to_ewkt(None).unwrap())
}
V::PostGisGeography(v) => {
let wkb = Ewkb(v);
prn.const_string(wkb.to_ewkt(None).unwrap())
}
V::PostGisBox2d(v) => {
let wkb = Ewkb(v);
prn.const_string(wkb.to_ewkt(None).unwrap())
}
V::PostGisBox3d(v) => {
let wkb = Ewkb(v);
prn.const_string(wkb.to_ewkt(None).unwrap())
}
}
}
}
Expand Down
147 changes: 147 additions & 0 deletions src/print/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ use std::task;

use bigdecimal::BigDecimal;
use bytes::Bytes;
use nom::AsBytes;
use tokio_stream::Stream;

use crate::print::native::FormatExt;
Expand Down Expand Up @@ -579,3 +580,149 @@ fn json() {
]"###
);
}

#[test]
fn postgis_geometry() {
assert_eq!(
test_format(&[Value::PostGisGeometry(
/*
* Point
* 01 - byteOrder, Little Endian
* 01000000 - wkbType, WKBPoint
* 0000000000000040 - x, 2.0
* 000000000000F03F - y, 1.0
*/
b"\
\x01\
\x01\x00\x00\x00\
\x00\x00\x00\x00\x00\x00\x00\x40\
\x00\x00\x00\x00\x00\x00\xF0\x3F\
"[..]
.into()
),],)
.unwrap(),
r###"{POINT(2 1)}"###
);
}

#[test]
fn postgis_geography() {
assert_eq!(
test_format(&[Value::PostGisGeography(
/*
* Point
* 01 - byteOrder, Little Endian
* 01000000 - wkbType, WKBPoint
* 0000000000000040 - x, 2.0
* 000000000000F03F - y, 1.0
*/
b"\
\x01\
\x01\x00\x00\x00\
\x00\x00\x00\x00\x00\x00\x00\x40\
\x00\x00\x00\x00\x00\x00\xF0\x3F\
"[..]
.into(),
),],)
.unwrap(),
r###"{POINT(2 1)}"###
);
}

#[test]
fn postgis_box_2d() {
assert_eq!(
test_format(&[Value::PostGisBox2d(
/*
* Polygon
* 01 - byteOrder, Little Endian
* 03000000 - wkbType, wkbPolygon
* 01000000 - numRings, 1
* 05000000 - numPoints, 5
* 000000000000F03F - x, 1.0
* 000000000000F03F - y, 1.0
* 0000000000000040 - x, 2.0
* 000000000000F03F - y, 1.0
* 0000000000000040 - x, 2.0
* 0000000000000040 - y, 2.0
* 000000000000F03F - x, 1.0
* 0000000000000040 - y, 2.0
* 000000000000F03F - x, 1.0
* 000000000000F03F - y, 1.0
*/
b"\
\x01\
\x03\x00\x00\x00\
\x01\x00\x00\x00\
\x05\x00\x00\x00\
\x00\x00\x00\x00\x00\x00\xF0\x3F\
\x00\x00\x00\x00\x00\x00\xF0\x3F\
\x00\x00\x00\x00\x00\x00\x00\x40\
\x00\x00\x00\x00\x00\x00\xF0\x3F\
\x00\x00\x00\x00\x00\x00\x00\x40\
\x00\x00\x00\x00\x00\x00\x00\x40\
\x00\x00\x00\x00\x00\x00\xF0\x3F\
\x00\x00\x00\x00\x00\x00\x00\x40\
\x00\x00\x00\x00\x00\x00\xF0\x3F\
\x00\x00\x00\x00\x00\x00\xF0\x3F\
"[..]
.into(),
),],)
.unwrap(),
r###"{POLYGON((1 1,2 1,2 2,1 2,1 1))}"###
);
}

#[test]
fn postgis_box_3d() {
assert_eq!(
test_format(&[Value::PostGisBox2d(
/*
* Polygon
* 01 - byteOrder, Little Endian
* 03000080 - wkbType, wkbPolygonZ
* 01000000 - numRings, 1
* 05000000 - numPoints, 5
* 000000000000F03F - x, 1.0
* 000000000000F03F - y, 1.0
* 0000000000000000 - z, 0.0
* 0000000000000040 - x, 2.0
* 000000000000F03F - y, 1.0
* 0000000000000000 - z, 0.0
* 0000000000000040 - x, 2.0
* 0000000000000040 - y, 2.0
* 0000000000000000 - z, 0.0
* 000000000000F03F - x, 1.0
* 0000000000000040 - y, 2.0
* 0000000000000000 - z, 0.0
* 000000000000F03F - x, 1.0
* 000000000000F03F - y, 1.0
* 0000000000000000 - z, 0.0
*/
b"\
\x01\
\x03\x00\x00\x80\
\x01\x00\x00\x00\
\x05\x00\x00\x00\
\x00\x00\x00\x00\x00\x00\xF0\x3F\
\x00\x00\x00\x00\x00\x00\xF0\x3F\
\x00\x00\x00\x00\x00\x00\x08\x40\
\x00\x00\x00\x00\x00\x00\x00\x40\
\x00\x00\x00\x00\x00\x00\xF0\x3F\
\x00\x00\x00\x00\x00\x00\x08\x40\
\x00\x00\x00\x00\x00\x00\x00\x40\
\x00\x00\x00\x00\x00\x00\x00\x40\
\x00\x00\x00\x00\x00\x00\x08\x40\
\x00\x00\x00\x00\x00\x00\xF0\x3F\
\x00\x00\x00\x00\x00\x00\x00\x40\
\x00\x00\x00\x00\x00\x00\x08\x40\
\x00\x00\x00\x00\x00\x00\xF0\x3F\
\x00\x00\x00\x00\x00\x00\xF0\x3F\
\x00\x00\x00\x00\x00\x00\x08\x40\
"[..]
.into(),
),],)
.unwrap(),
r###"{POLYGON((1 1 3,2 1 3,2 2 3,1 2 3,1 1 3))}"###
);
}

0 comments on commit c829645

Please sign in to comment.