Skip to content

Commit b74def2

Browse files
authored
Merge pull request duckdb#388 from Maxxen/dev
Check for future geometry versions
2 parents 795136a + a190f7c commit b74def2

File tree

5 files changed

+57
-6
lines changed

5 files changed

+57
-6
lines changed

spatial/include/spatial/core/geometry/geometry_processor.hpp

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -220,8 +220,13 @@ class GeometryProcessor {
220220
public:
221221
RESULT Process(const geometry_t &geom, ARGS... args) {
222222

223-
has_z = geom.GetProperties().HasZ();
224-
has_m = geom.GetProperties().HasM();
223+
const auto props = geom.GetProperties();
224+
225+
// Check the version
226+
props.CheckVersion();
227+
228+
has_z = props.HasZ();
229+
has_m = props.HasM();
225230
nesting_level = 0;
226231
current_type = geom.GetType();
227232
parent_type = GeometryType::POINT;

spatial/include/spatial/core/geometry/geometry_properties.hpp

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@ namespace spatial {
55

66
namespace core {
77

8+
static constexpr const uint8_t GEOMETRY_VERSION = 0;
9+
810
struct GeometryProperties {
911
private:
1012
static constexpr const uint8_t Z = 0x01;
@@ -14,6 +16,8 @@ struct GeometryProperties {
1416
// static constexpr const uint8_t EMPTY = 0x08;
1517
// static constexpr const uint8_t GEODETIC = 0x10;
1618
// static constexpr const uint8_t SOLID = 0x20;
19+
static constexpr const uint8_t VERSION_1 = 0x40;
20+
static constexpr const uint8_t VERSION_0 = 0x80;
1721
uint8_t flags = 0;
1822

1923
public:
@@ -24,6 +28,16 @@ struct GeometryProperties {
2428
SetM(has_m);
2529
}
2630

31+
inline void CheckVersion() const {
32+
const auto v0 = (flags & VERSION_0);
33+
const auto v1 = (flags & VERSION_1);
34+
if ((v1 | v0) != GEOMETRY_VERSION) {
35+
throw NotImplementedException(
36+
"This geometry seems to be written with a newer version of the DuckDB spatial library that is not "
37+
"compatible with this version. Please upgrade your DuckDB installation.");
38+
}
39+
}
40+
2741
inline bool HasZ() const {
2842
return (flags & Z) != 0;
2943
}

spatial/include/spatial/core/geometry/geometry_type.hpp

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -83,13 +83,15 @@ class geometry_t {
8383
}
8484

8585
GeometryType GetType() const {
86+
// return the type
8687
return Load<GeometryType>(const_data_ptr_cast(data.GetPrefix()));
8788
}
89+
8890
GeometryProperties GetProperties() const {
89-
return Load<GeometryProperties>(const_data_ptr_cast(data.GetPrefix() + 1));
90-
}
91-
uint16_t GetHash() const {
92-
return Load<uint16_t>(const_data_ptr_cast(data.GetPrefix() + 2));
91+
const auto props = Load<GeometryProperties>(const_data_ptr_cast(data.GetPrefix() + 1));
92+
// Check the version
93+
props.CheckVersion();
94+
return props;
9395
}
9496

9597
bool TryGetCachedBounds(Box2D<double> &bbox) const {
@@ -101,6 +103,9 @@ class geometry_t {
101103
auto hash = cursor.Read<uint16_t>();
102104
(void)hash;
103105

106+
// Check the version
107+
properties.CheckVersion();
108+
104109
if (properties.HasBBox()) {
105110
cursor.Skip(4); // skip padding
106111

test/data/duckdb_v1_0_0.db

524 KB
Binary file not shown.
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
# Query a database with geometry types created in duckdb v1.0.0
2+
require spatial
3+
4+
statement ok
5+
attach 'test/data/duckdb_v1_0_0.db' as db;
6+
7+
statement ok
8+
use db
9+
10+
query IIII rowsort
11+
SELECT st_geometrytype(geom), st_astext(geom), st_isvalid(geom), st_area(geom) FROM types;
12+
----
13+
GEOMETRYCOLLECTION GEOMETRYCOLLECTION (POINT (0 0), LINESTRING (0 0, 1 1)) true 0.0
14+
GEOMETRYCOLLECTION GEOMETRYCOLLECTION EMPTY true 0.0
15+
LINESTRING LINESTRING (0 0, 1 1) true 0.0
16+
LINESTRING LINESTRING EMPTY true 0.0
17+
MULTILINESTRING MULTILINESTRING ((0 0, 1 1), (2 2, 3 3)) true 0.0
18+
MULTILINESTRING MULTILINESTRING EMPTY true 0.0
19+
MULTIPOINT MULTIPOINT (0 0, 1 1) true 0.0
20+
MULTIPOINT MULTIPOINT EMPTY true 0.0
21+
MULTIPOLYGON MULTIPOLYGON (((0 0, 1 0, 1 1, 0 1, 0 0)), ((2 2, 3 2, 3 3, 2 3, 2 2))) true 2.0
22+
MULTIPOLYGON MULTIPOLYGON EMPTY true 0.0
23+
POINT POINT (0 0) true 0.0
24+
POINT POINT EMPTY true 0.0
25+
POLYGON POLYGON ((0 0, 1 0, 1 1, 0 1, 0 0)) true 1.0
26+
POLYGON POLYGON EMPTY true 0.0
27+

0 commit comments

Comments
 (0)