@@ -195,22 +195,98 @@ bool TgeometryFunctions::TgeometryToString(Vector &source, Vector &result, idx_t
195195 return true ;
196196}
197197
198+ namespace {
199+
200+ void TgeometryAsWkbExec (DataChunk &args, ExpressionState &, Vector &result) {
201+ UnaryExecutor::Execute<string_t , string_t >(
202+ args.data [0 ], result, args.size (),
203+ [&](string_t input) -> string_t {
204+ size_t sz = input.GetSize ();
205+ uint8_t *copy = (uint8_t *)malloc (sz);
206+ if (!copy) throw InternalException (" asBinary: malloc failed" );
207+ memcpy (copy, input.GetData (), sz);
208+ Temporal *t = reinterpret_cast <Temporal *>(copy);
209+ size_t wkb_sz = 0 ;
210+ uint8_t *wkb = temporal_as_wkb (t, WKB_EXTENDED , &wkb_sz);
211+ free (copy);
212+ if (!wkb || wkb_sz == 0 ) { if (wkb) free (wkb); throw InternalException (" temporal_as_wkb failed" ); }
213+ string_t stored = StringVector::AddStringOrBlob (
214+ result, string_t (reinterpret_cast <const char *>(wkb), wkb_sz));
215+ free (wkb);
216+ return stored;
217+ });
218+ }
219+
220+ void TgeometryFromWkbExec (DataChunk &args, ExpressionState &, Vector &result) {
221+ UnaryExecutor::Execute<string_t , string_t >(
222+ args.data [0 ], result, args.size (),
223+ [&](string_t input) -> string_t {
224+ if (input.GetSize () == 0 )
225+ throw InvalidInputException (" fromBinary: empty WKB input" );
226+ uint8_t *wkb = (uint8_t *)malloc (input.GetSize ());
227+ if (!wkb) throw InternalException (" fromBinary: malloc failed" );
228+ memcpy (wkb, input.GetData (), input.GetSize ());
229+ Temporal *t = temporal_from_wkb (wkb, input.GetSize ());
230+ free (wkb);
231+ if (!t) throw InvalidInputException (" fromBinary: invalid tgeometry WKB" );
232+ size_t sz = temporal_mem_size (t);
233+ string_t stored = StringVector::AddStringOrBlob (
234+ result, string_t (reinterpret_cast <const char *>(t), sz));
235+ free (t);
236+ return stored;
237+ });
238+ }
239+
240+ void TgeometryAsHexWkbExec (DataChunk &args, ExpressionState &, Vector &result) {
241+ UnaryExecutor::Execute<string_t , string_t >(
242+ args.data [0 ], result, args.size (),
243+ [&](string_t input) -> string_t {
244+ size_t sz = input.GetSize ();
245+ uint8_t *copy = (uint8_t *)malloc (sz);
246+ if (!copy) throw InternalException (" asHexWKB: malloc failed" );
247+ memcpy (copy, input.GetData (), sz);
248+ Temporal *t = reinterpret_cast <Temporal *>(copy);
249+ size_t hex_sz = 0 ;
250+ char *hex = temporal_as_hexwkb (t, WKB_EXTENDED , &hex_sz);
251+ free (copy);
252+ if (!hex || hex_sz == 0 ) { if (hex) free (hex); throw InternalException (" temporal_as_hexwkb failed" ); }
253+ string_t stored = StringVector::AddString (result, hex, hex_sz);
254+ free (hex);
255+ return stored;
256+ });
257+ }
258+
259+ void TgeometryFromHexWkbExec (DataChunk &args, ExpressionState &, Vector &result) {
260+ UnaryExecutor::Execute<string_t , string_t >(
261+ args.data [0 ], result, args.size (),
262+ [&](string_t input) -> string_t {
263+ if (input.GetSize () == 0 )
264+ throw InvalidInputException (" fromHexWKB: empty hex input" );
265+ std::string hex_str (input.GetData (), input.GetSize ());
266+ Temporal *t = temporal_from_hexwkb (hex_str.c_str ());
267+ if (!t) throw InvalidInputException (" fromHexWKB: invalid tgeometry hex WKB" );
268+ size_t sz = temporal_mem_size (t);
269+ string_t stored = StringVector::AddStringOrBlob (
270+ result, string_t (reinterpret_cast <const char *>(t), sz));
271+ free (t);
272+ return stored;
273+ });
274+ }
275+
276+ } // anonymous namespace
277+
198278void TGeometryTypes::RegisterScalarInOutFunctions (ExtensionLoader &loader){
199- auto TgeometryAsText = ScalarFunction (
200- " asText" ,
201- {TGeometryTypes::TGEOMETRY ()},
202- LogicalType::VARCHAR ,
203- Tspatial_as_text
204- );
205- duckdb::RegisterSerializedScalarFunction (loader, TgeometryAsText);
206-
207- auto TgeometryAsEWKT = ScalarFunction (
208- " asEWKT" ,
209- {TGeometryTypes::TGEOMETRY ()},
210- LogicalType::VARCHAR ,
211- Tspatial_as_ewkt
212- );
213- duckdb::RegisterSerializedScalarFunction (loader, TgeometryAsEWKT);
279+ const auto T = TGeometryTypes::TGEOMETRY ();
280+ const auto B = LogicalType::BLOB ;
281+ const auto V = LogicalType::VARCHAR ;
282+
283+ duckdb::RegisterSerializedScalarFunction (loader, ScalarFunction (" asBinary" , {T}, B, TgeometryAsWkbExec));
284+ duckdb::RegisterSerializedScalarFunction (loader, ScalarFunction (" tgeometryFromBinary" , {B}, T, TgeometryFromWkbExec));
285+ duckdb::RegisterSerializedScalarFunction (loader, ScalarFunction (" asHexWKB" , {T}, V, TgeometryAsHexWkbExec));
286+ duckdb::RegisterSerializedScalarFunction (loader, ScalarFunction (" tgeometryFromHexWKB" , {V}, T, TgeometryFromHexWkbExec));
287+
288+ duckdb::RegisterSerializedScalarFunction (loader, ScalarFunction (" asText" , {T}, V, Tspatial_as_text));
289+ duckdb::RegisterSerializedScalarFunction (loader, ScalarFunction (" asEWKT" , {T}, V, Tspatial_as_ewkt));
214290}
215291
216292
0 commit comments