Skip to content

Commit a248a47

Browse files
committed
fix VertexArray XYM to XYZM upcast, ToString()
1 parent df58e0a commit a248a47

File tree

5 files changed

+35
-18
lines changed

5 files changed

+35
-18
lines changed

spatial/include/spatial/core/geometry/vertex_vector.hpp

+1
Original file line numberDiff line numberDiff line change
@@ -188,6 +188,7 @@ class VertexArray {
188188
bool IsClosed() const;
189189
double Length() const;
190190
string ToString() const;
191+
string ToString(uint32_t count) const;
191192

192193
// Potentially allocating methods
193194
void Append(ArenaAllocator &alloc, const VertexArray &other);

spatial/src/spatial/core/functions/scalar/st_collect.cpp

+2
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,8 @@ static void CollectFunction(DataChunk &args, ExpressionState &state, Vector &res
7272
}
7373
}
7474

75+
// TODO: Dont upcast the children, just append them.
76+
7577
if (all_points) {
7678
MultiPoint collection(arena, geometries.size(), has_z, has_m);
7779
for (idx_t i = 0; i < geometries.size(); i++) {

spatial/src/spatial/core/functions/scalar/st_makeline.cpp

+1
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,7 @@ static void MakeLineBinaryFunction(DataChunk &args, ExpressionState &state, Vect
9191
auto &point_left = geometry_left.As<Point>();
9292
auto &point_right = geometry_right.As<Point>();
9393

94+
// TODO: Dont upcast the child geometries, just append and let the append function handle upcasting of the target instead.
9495
point_left.Vertices().SetVertexType(lstate.factory.allocator, has_z, has_m);
9596
point_right.Vertices().SetVertexType(lstate.factory.allocator, has_z, has_m);
9697

spatial/src/spatial/core/geometry/vertex_vector.cpp

+30-17
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,12 @@ void VertexArray::Append(ArenaAllocator &alloc, const VertexArray &other) {
1717

1818
D_ASSERT(properties.HasZ() == other.properties.HasZ());
1919
D_ASSERT(properties.HasM() == other.properties.HasM());
20+
#ifdef DEBUG
21+
if(properties.HasZ() != other.properties.HasZ()) {
22+
// This is mostly here to prevent the ToString() method to be optimized out.
23+
throw InternalException("Cannot append vertex arrays with different Z properties. self: %s, other: %s", ToString(vertex_count), other.ToString(other.vertex_count));
24+
}
25+
#endif
2026
auto old_count = vertex_count;
2127
auto new_count = vertex_count + other.vertex_count;
2228
Resize(alloc, new_count);
@@ -79,11 +85,15 @@ void VertexArray::SetVertexType(ArenaAllocator &alloc, bool has_z, bool has_m, d
7985
for (int64_t i = vertex_count - 1; i >= 0; i--) {
8086
auto old_offset = i * old_vertex_size;
8187
auto new_offset = i * new_vertex_size;
82-
memcpy(vertex_data + new_offset, vertex_data + old_offset, sizeof(double) * 2);
83-
auto z_offset = old_offset + sizeof(double) * 2;
84-
auto m_offset = old_offset + sizeof(double) * 3;
85-
memcpy(vertex_data + new_offset + m_offset, vertex_data + z_offset, sizeof(double));
86-
memcpy(vertex_data + new_offset + z_offset, &default_z, sizeof(double));
88+
auto old_m_offset = old_offset + sizeof(double) * 2;
89+
auto new_z_offset = new_offset + sizeof(double) * 2;
90+
auto new_m_offset = new_offset + sizeof(double) * 3;
91+
// Move the M value
92+
memcpy(vertex_data + new_m_offset, vertex_data + old_m_offset, sizeof(double));
93+
// Set the new Z value
94+
memcpy(vertex_data + new_z_offset, &default_z, sizeof(double));
95+
// Move the X and Y values
96+
memcpy(vertex_data + new_offset, vertex_data + old_offset, sizeof(double) * 2);
8797
}
8898
} else if (!used_to_have_z && has_z && !used_to_have_m && has_m) {
8999
// 2. We go from XY to XYZM
@@ -190,48 +200,51 @@ double VertexArray::Length() const {
190200
}
191201

192202
string VertexArray::ToString() const {
203+
return ToString(vertex_count);
204+
}
205+
string VertexArray::ToString(uint32_t count) const {
193206
auto has_z = properties.HasZ();
194207
auto has_m = properties.HasM();
195208

196209
if (has_z && has_m) {
197-
string result = StringUtil::Format("VertexArray XYZM (%d) [", vertex_count);
198-
for (uint32_t i = 0; i < vertex_count; i++) {
210+
string result = StringUtil::Format("VertexArray XYZM (%d/%d) [", count, vertex_count);
211+
for (uint32_t i = 0; i < count; i++) {
199212
auto vertex = GetExact<VertexXYZM>(i);
200213
result += StringUtil::Format("(%f, %f, %f, %f)", vertex.x, vertex.y, vertex.z, vertex.m);
201-
if (i < vertex_count - 1) {
214+
if (i < count - 1) {
202215
result += ", ";
203216
}
204217
}
205218
result += "]";
206219
return result;
207220
} else if (has_z) {
208-
string result = StringUtil::Format("VertexArray XYZ (%d) [", vertex_count);
209-
for (uint32_t i = 0; i < vertex_count; i++) {
221+
string result = StringUtil::Format("VertexArray XYZ (%d/%d) [", count, vertex_count);
222+
for (uint32_t i = 0; i < count; i++) {
210223
auto vertex = GetExact<VertexXYZ>(i);
211224
result += StringUtil::Format("(%f, %f, %f)", vertex.x, vertex.y, vertex.z);
212-
if (i < vertex_count - 1) {
225+
if (i < count - 1) {
213226
result += ", ";
214227
}
215228
}
216229
result += "]";
217230
return result;
218231
} else if (has_m) {
219-
string result = StringUtil::Format("VertexArray XYM (%d/%d) [", vertex_count);
220-
for (uint32_t i = 0; i < vertex_count; i++) {
232+
string result = StringUtil::Format("VertexArray XYM (%d/%d) [", count, vertex_count);
233+
for (uint32_t i = 0; i < count; i++) {
221234
auto vertex = GetExact<VertexXYM>(i);
222235
result += StringUtil::Format("(%f, %f, %f)", vertex.x, vertex.y, vertex.m);
223-
if (i < vertex_count - 1) {
236+
if (i < count - 1) {
224237
result += ", ";
225238
}
226239
}
227240
result += "]";
228241
return result;
229242
} else {
230-
string result = StringUtil::Format("VertexArray XY (%d/%d) [", vertex_count);
231-
for (uint32_t i = 0; i < vertex_count; i++) {
243+
string result = StringUtil::Format("VertexArray XY (%d/%d) [", count, vertex_count);
244+
for (uint32_t i = 0; i < count; i++) {
232245
auto vertex = GetExact<VertexXY>(i);
233246
result += StringUtil::Format("(%f, %f)", vertex.x, vertex.y);
234-
if (i < vertex_count - 1) {
247+
if (i < count - 1) {
235248
result += ", ";
236249
}
237250
}

test/sql/geometry/st_collect.test

+1-1
Original file line numberDiff line numberDiff line change
@@ -58,4 +58,4 @@ MULTIPOINT ZM (3 4 0 2, 0 1 2 0)
5858
query I
5959
SELECT ST_Collect(['LINESTRING M (1 2 3, 4 5 6)'::GEOMETRY, 'POINT Z EMPTY'::GEOMETRY]) as merged;
6060
----
61-
MULTILINESTRING ZM ((1 2 0 3, 4 5 0 0))
61+
MULTILINESTRING ZM ((1 2 0 3, 4 5 0 6))

0 commit comments

Comments
 (0)