github.com/dolthub/go-mysql-server@v0.18.0/sql/expression/function/spatial/wkb_test.go (about) 1 // Copyright 2020-2021 Dolthub, Inc. 2 // 3 // Licensed under the Apache License, Version 2.0 (the "License"); 4 // you may not use this file except in compliance with the License. 5 // You may obtain a copy of the License at 6 // 7 // http://www.apache.org/licenses/LICENSE-2.0 8 // 9 // Unless required by applicable law or agreed to in writing, software 10 // distributed under the License is distributed on an "AS IS" BASIS, 11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 // See the License for the specific language governing permissions and 13 // limitations under the License. 14 15 package spatial 16 17 import ( 18 "encoding/hex" 19 "testing" 20 21 "github.com/stretchr/testify/require" 22 23 "github.com/dolthub/go-mysql-server/sql" 24 "github.com/dolthub/go-mysql-server/sql/expression" 25 "github.com/dolthub/go-mysql-server/sql/types" 26 ) 27 28 func TestAsWKB(t *testing.T) { 29 t.Run("convert point", func(t *testing.T) { 30 require := require.New(t) 31 f := NewAsWKB(expression.NewLiteral(types.Point{X: 1, Y: 2}, types.PointType{})) 32 v, err := f.Eval(sql.NewEmptyContext(), nil) 33 require.NoError(err) 34 res, err := hex.DecodeString("0101000000000000000000F03F0000000000000040") 35 require.NoError(err) 36 require.Equal(res, v) 37 }) 38 39 t.Run("convert point with negative floats", func(t *testing.T) { 40 require := require.New(t) 41 f := NewAsWKB(expression.NewLiteral(types.Point{X: -123.45, Y: 678.9}, types.PointType{})) 42 v, err := f.Eval(sql.NewEmptyContext(), nil) 43 require.NoError(err) 44 res, err := hex.DecodeString("0101000000CDCCCCCCCCDC5EC03333333333378540") 45 require.NoError(err) 46 require.Equal(res, v) 47 }) 48 49 t.Run("convert linestring", func(t *testing.T) { 50 require := require.New(t) 51 f := NewAsWKB(expression.NewLiteral(types.LineString{Points: []types.Point{{X: 1, Y: 2}, {X: 3, Y: 4}}}, types.LineStringType{})) 52 v, err := f.Eval(sql.NewEmptyContext(), nil) 53 require.NoError(err) 54 res, err := hex.DecodeString("010200000002000000000000000000F03F000000000000004000000000000008400000000000001040") 55 require.NoError(err) 56 require.Equal(res, v) 57 }) 58 59 t.Run("convert polygon", func(t *testing.T) { 60 require := require.New(t) 61 f := NewAsWKB(expression.NewLiteral(types.Polygon{Lines: []types.LineString{{Points: []types.Point{{X: 0, Y: 0}, {X: 1, Y: 1}, {X: 1, Y: 0}, {X: 0, Y: 0}}}}}, types.PolygonType{})) 62 v, err := f.Eval(sql.NewEmptyContext(), nil) 63 require.NoError(err) 64 res, err := hex.DecodeString("0103000000010000000400000000000000000000000000000000000000000000000000F03F000000000000F03F000000000000F03F000000000000000000000000000000000000000000000000") 65 require.NoError(err) 66 require.Equal(res, v) 67 }) 68 69 t.Run("convert multipoint", func(t *testing.T) { 70 require := require.New(t) 71 f := NewAsWKB(expression.NewLiteral(types.MultiPoint{Points: []types.Point{{X: 1, Y: 2}, {X: 3, Y: 4}}}, types.MultiPointType{})) 72 v, err := f.Eval(sql.NewEmptyContext(), nil) 73 require.NoError(err) 74 res, err := hex.DecodeString("0104000000020000000101000000000000000000F03F0000000000000040010100000000000000000008400000000000001040") 75 require.NoError(err) 76 require.Equal(res, v) 77 }) 78 79 t.Run("convert multilinestring", func(t *testing.T) { 80 require := require.New(t) 81 f := NewAsWKB(expression.NewLiteral(types.MultiLineString{Lines: []types.LineString{{Points: []types.Point{{X: 0, Y: 0}, {X: 1, Y: 1}, {X: 2, Y: 2}}}}}, types.MultiLineStringType{})) 82 v, err := f.Eval(sql.NewEmptyContext(), nil) 83 require.NoError(err) 84 res, err := hex.DecodeString("01050000000100000001020000000300000000000000000000000000000000000000000000000000F03F000000000000F03F00000000000000400000000000000040") 85 require.NoError(err) 86 require.Equal(res, v) 87 }) 88 89 t.Run("convert multipolygon", func(t *testing.T) { 90 require := require.New(t) 91 line := types.LineString{Points: []types.Point{{X: 0, Y: 0}, {X: 1, Y: 0}, {X: 1, Y: 1}, {X: 0, Y: 0}}} 92 poly := types.Polygon{Lines: []types.LineString{line}} 93 f := NewAsWKB(expression.NewLiteral(types.MultiPolygon{Polygons: []types.Polygon{poly}}, types.MultiPolygonType{})) 94 v, err := f.Eval(sql.NewEmptyContext(), nil) 95 require.NoError(err) 96 res, err := hex.DecodeString("0106000000010000000103000000010000000400000000000000000000000000000000000000000000000000F03F0000000000000000000000000000F03F000000000000F03F00000000000000000000000000000000") 97 require.NoError(err) 98 require.Equal(res, v) 99 }) 100 101 t.Run("convert empty geometrycollection", func(t *testing.T) { 102 require := require.New(t) 103 f := NewAsWKB(expression.NewLiteral(types.GeomColl{}, types.GeomCollType{})) 104 v, err := f.Eval(sql.NewEmptyContext(), nil) 105 require.NoError(err) 106 res, err := hex.DecodeString("010700000000000000") 107 require.NoError(err) 108 require.Equal(res, v) 109 }) 110 111 t.Run("convert geometrycollection", func(t *testing.T) { 112 require := require.New(t) 113 point := types.Point{X: 1, Y: 2} 114 line := types.LineString{Points: []types.Point{{X: 1, Y: 2}, {X: 3, Y: 4}}} 115 poly := types.Polygon{Lines: []types.LineString{{Points: []types.Point{{X: 0, Y: 0}, {X: 1, Y: 1}, {X: 1, Y: 0}, {X: 0, Y: 0}}}}} 116 mpoint := types.MultiPoint{Points: []types.Point{point, point}} 117 mline := types.MultiLineString{Lines: []types.LineString{line, line}} 118 mpoly := types.MultiPolygon{Polygons: []types.Polygon{poly, poly}} 119 gColl := types.GeomColl{} 120 g := types.GeomColl{Geoms: []types.GeometryValue{ 121 point, 122 line, 123 poly, 124 mpoint, 125 mline, 126 mpoly, 127 gColl, 128 }} 129 f := NewAsWKB(expression.NewLiteral(g, types.GeomCollType{})) 130 v, err := f.Eval(sql.NewEmptyContext(), nil) 131 require.NoError(err) 132 res, err := hex.DecodeString("0107000000070000000101000000000000000000F03F0000000000000040010200000002000000000000000000F03F0000000000000040000000000000084000000000000010400103000000010000000400000000000000000000000000000000000000000000000000F03F000000000000F03F000000000000F03F0000000000000000000000000000000000000000000000000104000000020000000101000000000000000000F03F00000000000000400101000000000000000000F03F0000000000000040010500000002000000010200000002000000000000000000F03F000000000000004000000000000008400000000000001040010200000002000000000000000000F03F0000000000000040000000000000084000000000000010400106000000020000000103000000010000000400000000000000000000000000000000000000000000000000F03F000000000000F03F000000000000F03F0000000000000000000000000000000000000000000000000103000000010000000400000000000000000000000000000000000000000000000000F03F000000000000F03F000000000000F03F000000000000000000000000000000000000000000000000010700000000000000") 133 require.NoError(err) 134 require.Equal(res, v) 135 }) 136 137 t.Run("convert null", func(t *testing.T) { 138 require := require.New(t) 139 f := NewAsWKB(expression.NewLiteral(nil, types.Null)) 140 v, err := f.Eval(sql.NewEmptyContext(), nil) 141 require.NoError(err) 142 require.Equal(nil, v) 143 }) 144 145 t.Run("wrong type", func(t *testing.T) { 146 require := require.New(t) 147 f := NewAsWKB(expression.NewLiteral("notageometry", types.Blob)) 148 _, err := f.Eval(sql.NewEmptyContext(), nil) 149 require.Error(err) 150 }) 151 152 t.Run("check return type", func(t *testing.T) { 153 require := require.New(t) 154 f := NewAsWKB(expression.NewLiteral(types.Point{X: 1, Y: 2}, types.PointType{})) 155 156 v, err := f.Eval(sql.NewEmptyContext(), nil) 157 require.NoError(err) 158 159 typ := f.Type() 160 _, _, err = typ.Convert(v) 161 require.NoError(err) 162 }) 163 } 164 165 func TestGeomFromWKB(t *testing.T) { 166 t.Run("convert point in little endian", func(t *testing.T) { 167 require := require.New(t) 168 res, err := hex.DecodeString("0101000000000000000000F03F0000000000000040") 169 require.NoError(err) 170 f, err := NewGeomFromWKB(expression.NewLiteral(res, types.Blob)) 171 require.NoError(err) 172 173 v, err := f.Eval(sql.NewEmptyContext(), nil) 174 require.NoError(err) 175 require.Equal(types.Point{X: 1, Y: 2}, v) 176 }) 177 178 t.Run("convert point in big endian", func(t *testing.T) { 179 require := require.New(t) 180 res, err := hex.DecodeString("00000000013FF00000000000004000000000000000") 181 require.NoError(err) 182 f, err := NewGeomFromWKB(expression.NewLiteral(res, types.Blob)) 183 require.NoError(err) 184 185 v, err := f.Eval(sql.NewEmptyContext(), nil) 186 require.NoError(err) 187 require.Equal(types.Point{X: 1, Y: 2}, v) 188 }) 189 190 t.Run("convert point bad point", func(t *testing.T) { 191 require := require.New(t) 192 res, err := hex.DecodeString("00000000013FF0000000000000") 193 require.NoError(err) 194 f, err := NewGeomFromWKB(expression.NewLiteral(res, types.Blob)) 195 require.NoError(err) 196 197 _, err = f.Eval(sql.NewEmptyContext(), nil) 198 require.Error(err) 199 }) 200 201 t.Run("convert point with negative floats", func(t *testing.T) { 202 require := require.New(t) 203 res, err := hex.DecodeString("0101000000CDCCCCCCCCDC5EC03333333333378540") 204 require.NoError(err) 205 f, err := NewGeomFromWKB(expression.NewLiteral(res, types.Blob)) 206 require.NoError(err) 207 208 v, err := f.Eval(sql.NewEmptyContext(), nil) 209 require.NoError(err) 210 require.Equal(types.Point{X: -123.45, Y: 678.9}, v) 211 }) 212 213 t.Run("convert linestring in little endian", func(t *testing.T) { 214 require := require.New(t) 215 res, err := hex.DecodeString("010200000002000000000000000000F03F000000000000004000000000000008400000000000001040") 216 require.NoError(err) 217 f, err := NewGeomFromWKB(expression.NewLiteral(res, types.Blob)) 218 require.NoError(err) 219 220 v, err := f.Eval(sql.NewEmptyContext(), nil) 221 require.NoError(err) 222 require.Equal(types.LineString{Points: []types.Point{{X: 1, Y: 2}, {X: 3, Y: 4}}}, v) 223 }) 224 225 t.Run("convert polygon in little endian", func(t *testing.T) { 226 require := require.New(t) 227 res, err := hex.DecodeString("0103000000010000000400000000000000000000000000000000000000000000000000F03F000000000000F03F000000000000F03F000000000000000000000000000000000000000000000000") 228 require.NoError(err) 229 f, err := NewGeomFromWKB(expression.NewLiteral(res, types.Blob)) 230 require.NoError(err) 231 232 v, err := f.Eval(sql.NewEmptyContext(), nil) 233 require.NoError(err) 234 require.Equal(types.Polygon{Lines: []types.LineString{{Points: []types.Point{{X: 0, Y: 0}, {X: 1, Y: 1}, {X: 1, Y: 0}, {X: 0, Y: 0}}}}}, v) 235 }) 236 237 t.Run("convert multipoint in little endian", func(t *testing.T) { 238 require := require.New(t) 239 res, err := hex.DecodeString("0104000000020000000101000000000000000000F03F0000000000000040010100000000000000000008400000000000001040") 240 require.NoError(err) 241 f, err := NewGeomFromWKB(expression.NewLiteral(res, types.Blob)) 242 require.NoError(err) 243 244 v, err := f.Eval(sql.NewEmptyContext(), nil) 245 require.NoError(err) 246 require.Equal(types.MultiPoint{Points: []types.Point{{X: 1, Y: 2}, {X: 3, Y: 4}}}, v) 247 }) 248 249 t.Run("convert multilinestring in little endian", func(t *testing.T) { 250 require := require.New(t) 251 res, err := hex.DecodeString("010500000002000000010200000002000000000000000000F03F000000000000004000000000000008400000000000001040010200000002000000000000000000144000000000000018400000000000001C400000000000002040") 252 require.NoError(err) 253 f, err := NewGeomFromWKB(expression.NewLiteral(res, types.Blob)) 254 require.NoError(err) 255 256 v, err := f.Eval(sql.NewEmptyContext(), nil) 257 require.NoError(err) 258 require.Equal(types.MultiLineString{Lines: []types.LineString{{Points: []types.Point{{X: 1, Y: 2}, {X: 3, Y: 4}}}, {Points: []types.Point{{X: 5, Y: 6}, {X: 7, Y: 8}}}}}, v) 259 }) 260 261 t.Run("convert multipolygon in little endian", func(t *testing.T) { 262 require := require.New(t) 263 res, err := hex.DecodeString("0106000000010000000103000000010000000400000000000000000000000000000000000000000000000000F03F0000000000000000000000000000F03F000000000000F03F00000000000000000000000000000000") 264 require.NoError(err) 265 f, err := NewGeomFromWKB(expression.NewLiteral(res, types.Blob)) 266 require.NoError(err) 267 268 v, err := f.Eval(sql.NewEmptyContext(), nil) 269 require.NoError(err) 270 line := types.LineString{Points: []types.Point{{X: 0, Y: 0}, {X: 1, Y: 0}, {X: 1, Y: 1}, {X: 0, Y: 0}}} 271 poly := types.Polygon{Lines: []types.LineString{line}} 272 require.Equal(types.MultiPolygon{Polygons: []types.Polygon{poly}}, v) 273 }) 274 275 t.Run("convert empty geometry collection in little endian", func(t *testing.T) { 276 require := require.New(t) 277 res, err := hex.DecodeString("010700000000000000") 278 require.NoError(err) 279 f, err := NewGeomFromWKB(expression.NewLiteral(res, types.Blob)) 280 require.NoError(err) 281 282 v, err := f.Eval(sql.NewEmptyContext(), nil) 283 require.NoError(err) 284 require.Equal(types.GeomColl{Geoms: []types.GeometryValue{}}, v) 285 }) 286 287 t.Run("convert empty geometry collection in little endian", func(t *testing.T) { 288 require := require.New(t) 289 res, err := hex.DecodeString("0107000000070000000101000000000000000000F03F0000000000000040010200000002000000000000000000F03F0000000000000040000000000000084000000000000010400103000000010000000400000000000000000000000000000000000000000000000000F03F000000000000F03F000000000000F03F0000000000000000000000000000000000000000000000000104000000020000000101000000000000000000F03F00000000000000400101000000000000000000F03F0000000000000040010500000002000000010200000002000000000000000000F03F000000000000004000000000000008400000000000001040010200000002000000000000000000F03F0000000000000040000000000000084000000000000010400106000000020000000103000000010000000400000000000000000000000000000000000000000000000000F03F000000000000F03F000000000000F03F0000000000000000000000000000000000000000000000000103000000010000000400000000000000000000000000000000000000000000000000F03F000000000000F03F000000000000F03F000000000000000000000000000000000000000000000000010700000000000000") 290 require.NoError(err) 291 f, err := NewGeomFromWKB(expression.NewLiteral(res, types.Blob)) 292 require.NoError(err) 293 294 v, err := f.Eval(sql.NewEmptyContext(), nil) 295 require.NoError(err) 296 point := types.Point{X: 1, Y: 2} 297 line := types.LineString{Points: []types.Point{{X: 1, Y: 2}, {X: 3, Y: 4}}} 298 poly := types.Polygon{Lines: []types.LineString{{Points: []types.Point{{X: 0, Y: 0}, {X: 1, Y: 1}, {X: 1, Y: 0}, {X: 0, Y: 0}}}}} 299 mpoint := types.MultiPoint{Points: []types.Point{point, point}} 300 mline := types.MultiLineString{Lines: []types.LineString{line, line}} 301 mpoly := types.MultiPolygon{Polygons: []types.Polygon{poly, poly}} 302 gColl := types.GeomColl{Geoms: []types.GeometryValue{}} 303 g := types.GeomColl{Geoms: []types.GeometryValue{ 304 point, 305 line, 306 poly, 307 mpoint, 308 mline, 309 mpoly, 310 gColl, 311 }} 312 require.Equal(g, v) 313 }) 314 315 t.Run("convert point with srid 0", func(t *testing.T) { 316 require := require.New(t) 317 res, err := hex.DecodeString("0101000000000000000000F03F0000000000000040") 318 require.NoError(err) 319 f, err := NewGeomFromWKB(expression.NewLiteral(res, types.Blob), 320 expression.NewLiteral(0, types.Uint32)) 321 require.NoError(err) 322 323 v, err := f.Eval(sql.NewEmptyContext(), nil) 324 require.NoError(err) 325 require.Equal(types.Point{SRID: types.CartesianSRID, X: 1, Y: 2}, v) 326 }) 327 328 t.Run("convert point with srid valid 3857", func(t *testing.T) { 329 require := require.New(t) 330 res, err := hex.DecodeString("0101000000000000000000F03F0000000000000040") 331 require.NoError(err) 332 f, err := NewGeomFromWKB(expression.NewLiteral(res, types.Blob), 333 expression.NewLiteral(3857, types.Uint32)) 334 require.NoError(err) 335 336 v, err := f.Eval(sql.NewEmptyContext(), nil) 337 require.NoError(err) 338 require.Equal(types.Point{SRID: 3857, X: 1, Y: 2}, v) 339 }) 340 341 t.Run("convert point with srid valid 4326", func(t *testing.T) { 342 require := require.New(t) 343 res, err := hex.DecodeString("0101000000000000000000F03F0000000000000040") 344 require.NoError(err) 345 f, err := NewGeomFromWKB(expression.NewLiteral(res, types.Blob), 346 expression.NewLiteral(types.GeoSpatialSRID, types.Uint32)) 347 require.NoError(err) 348 349 v, err := f.Eval(sql.NewEmptyContext(), nil) 350 require.NoError(err) 351 require.Equal(types.Point{SRID: types.GeoSpatialSRID, X: 1, Y: 2}, v) 352 }) 353 354 t.Run("convert point with invalid srid 1234", func(t *testing.T) { 355 require := require.New(t) 356 res, err := hex.DecodeString("0101000000000000000000F03F0000000000000040") 357 require.NoError(err) 358 f, err := NewGeomFromWKB(expression.NewLiteral(res, types.Blob), 359 expression.NewLiteral(1234, types.Uint32)) 360 require.NoError(err) 361 362 _, err = f.Eval(sql.NewEmptyContext(), nil) 363 require.Error(err) 364 }) 365 366 t.Run("convert point with srid 4326 axis srid-defined", func(t *testing.T) { 367 require := require.New(t) 368 res, err := hex.DecodeString("0101000000000000000000F03F0000000000000040") 369 require.NoError(err) 370 f, err := NewGeomFromWKB(expression.NewLiteral(res, types.Blob), 371 expression.NewLiteral(types.GeoSpatialSRID, types.Uint32), 372 expression.NewLiteral("axis-order=srid-defined", types.Blob)) 373 require.NoError(err) 374 375 v, err := f.Eval(sql.NewEmptyContext(), nil) 376 require.NoError(err) 377 require.Equal(types.Point{SRID: types.GeoSpatialSRID, X: 1, Y: 2}, v) 378 }) 379 380 t.Run("convert point with srid 4326 axis long-lat", func(t *testing.T) { 381 require := require.New(t) 382 res, err := hex.DecodeString("0101000000000000000000F03F0000000000000040") 383 require.NoError(err) 384 f, err := NewGeomFromWKB(expression.NewLiteral(res, types.Blob), 385 expression.NewLiteral(types.GeoSpatialSRID, types.Uint32), 386 expression.NewLiteral("axis-order=long-lat", types.Blob)) 387 require.NoError(err) 388 389 v, err := f.Eval(sql.NewEmptyContext(), nil) 390 require.NoError(err) 391 require.Equal(types.Point{SRID: types.GeoSpatialSRID, X: 2, Y: 1}, v) 392 }) 393 394 t.Run("convert point with srid 4326 axis long-lat", func(t *testing.T) { 395 require := require.New(t) 396 res, err := hex.DecodeString("0101000000000000000000F03F0000000000000040") 397 require.NoError(err) 398 f, err := NewGeomFromWKB(expression.NewLiteral(res, types.Blob), 399 expression.NewLiteral(types.GeoSpatialSRID, types.Uint32), 400 expression.NewLiteral("axis-order=long-lat", types.Blob)) 401 require.NoError(err) 402 403 v, err := f.Eval(sql.NewEmptyContext(), nil) 404 require.NoError(err) 405 require.Equal(types.Point{SRID: types.GeoSpatialSRID, X: 2, Y: 1}, v) 406 }) 407 408 t.Run("convert linestring with valid srid 3857", func(t *testing.T) { 409 require := require.New(t) 410 res, err := hex.DecodeString("010200000002000000000000000000F03F000000000000004000000000000008400000000000001040") 411 require.NoError(err) 412 f, err := NewGeomFromWKB(expression.NewLiteral(res, types.Blob), 413 expression.NewLiteral(3857, types.Uint32)) 414 require.NoError(err) 415 416 v, err := f.Eval(sql.NewEmptyContext(), nil) 417 require.NoError(err) 418 require.Equal(types.LineString{SRID: 3857, Points: []types.Point{{SRID: 3857, X: 1, Y: 2}, {SRID: 3857, X: 3, Y: 4}}}, v) 419 }) 420 421 t.Run("convert linestring with valid srid 4326", func(t *testing.T) { 422 require := require.New(t) 423 res, err := hex.DecodeString("010200000002000000000000000000F03F000000000000004000000000000008400000000000001040") 424 require.NoError(err) 425 f, err := NewGeomFromWKB(expression.NewLiteral(res, types.Blob), 426 expression.NewLiteral(types.GeoSpatialSRID, types.Uint32)) 427 require.NoError(err) 428 429 v, err := f.Eval(sql.NewEmptyContext(), nil) 430 require.NoError(err) 431 require.Equal(types.LineString{SRID: types.GeoSpatialSRID, Points: []types.Point{{SRID: types.GeoSpatialSRID, X: 1, Y: 2}, {SRID: types.GeoSpatialSRID, X: 3, Y: 4}}}, v) 432 }) 433 434 t.Run("convert linestring with invalid srid 1234", func(t *testing.T) { 435 require := require.New(t) 436 res, err := hex.DecodeString("010200000002000000000000000000F03F000000000000004000000000000008400000000000001040") 437 require.NoError(err) 438 f, err := NewGeomFromWKB(expression.NewLiteral(res, types.Blob), 439 expression.NewLiteral(1234, types.Uint32)) 440 require.NoError(err) 441 442 _, err = f.Eval(sql.NewEmptyContext(), nil) 443 require.Error(err) 444 }) 445 446 t.Run("convert linestring with srid 4326 axis long-lat", func(t *testing.T) { 447 require := require.New(t) 448 res, err := hex.DecodeString("010200000002000000000000000000F03F000000000000004000000000000008400000000000001040") 449 require.NoError(err) 450 f, err := NewGeomFromWKB(expression.NewLiteral(res, types.Blob), 451 expression.NewLiteral(types.GeoSpatialSRID, types.Uint32), 452 expression.NewLiteral("axis-order=long-lat", types.Blob)) 453 require.NoError(err) 454 455 v, err := f.Eval(sql.NewEmptyContext(), nil) 456 require.NoError(err) 457 require.Equal(types.LineString{SRID: types.GeoSpatialSRID, Points: []types.Point{{SRID: types.GeoSpatialSRID, X: 2, Y: 1}, {SRID: types.GeoSpatialSRID, X: 4, Y: 3}}}, v) 458 }) 459 460 t.Run("convert polygon with valid srid 4326", func(t *testing.T) { 461 require := require.New(t) 462 res, err := hex.DecodeString("0103000000010000000400000000000000000000000000000000000000000000000000F03F000000000000F03F000000000000F03F000000000000000000000000000000000000000000000000") 463 require.NoError(err) 464 f, err := NewGeomFromWKB(expression.NewLiteral(res, types.Blob), 465 expression.NewLiteral(types.GeoSpatialSRID, types.Uint32)) 466 require.NoError(err) 467 468 v, err := f.Eval(sql.NewEmptyContext(), nil) 469 require.NoError(err) 470 require.Equal(types.Polygon{SRID: types.GeoSpatialSRID, Lines: []types.LineString{{SRID: types.GeoSpatialSRID, Points: []types.Point{{SRID: types.GeoSpatialSRID, X: 0, Y: 0}, {SRID: types.GeoSpatialSRID, X: 1, Y: 1}, {SRID: types.GeoSpatialSRID, X: 1, Y: 0}, {SRID: types.GeoSpatialSRID, X: 0, Y: 0}}}}}, v) 471 }) 472 473 t.Run("convert polygon with valid srid 3857", func(t *testing.T) { 474 require := require.New(t) 475 res, err := hex.DecodeString("0103000000010000000400000000000000000000000000000000000000000000000000F03F000000000000F03F000000000000F03F000000000000000000000000000000000000000000000000") 476 require.NoError(err) 477 f, err := NewGeomFromWKB(expression.NewLiteral(res, types.Blob), 478 expression.NewLiteral(3857, types.Uint32)) 479 require.NoError(err) 480 481 v, err := f.Eval(sql.NewEmptyContext(), nil) 482 require.NoError(err) 483 require.Equal(types.Polygon{SRID: 3857, Lines: []types.LineString{{SRID: 3857, Points: []types.Point{{SRID: 3857, X: 0, Y: 0}, {SRID: 3857, X: 1, Y: 1}, {SRID: 3857, X: 1, Y: 0}, {SRID: 3857, X: 0, Y: 0}}}}}, v) 484 }) 485 486 t.Run("convert polygon with invalid srid 2", func(t *testing.T) { 487 require := require.New(t) 488 res, err := hex.DecodeString("0103000000010000000400000000000000000000000000000000000000000000000000F03F000000000000F03F000000000000F03F000000000000000000000000000000000000000000000000") 489 require.NoError(err) 490 f, err := NewGeomFromWKB(expression.NewLiteral(res, types.Blob), 491 expression.NewLiteral(2, types.Uint32)) 492 require.NoError(err) 493 494 _, err = f.Eval(sql.NewEmptyContext(), nil) 495 require.Error(err) 496 }) 497 498 t.Run("convert polygon with srid 4326 axis long-lat", func(t *testing.T) { 499 require := require.New(t) 500 res, err := hex.DecodeString("0103000000010000000400000000000000000000000000000000000000000000000000F03F000000000000F03F000000000000F03F000000000000000000000000000000000000000000000000") 501 require.NoError(err) 502 f, err := NewGeomFromWKB(expression.NewLiteral(res, types.Blob), 503 expression.NewLiteral(types.GeoSpatialSRID, types.Uint32), 504 expression.NewLiteral("axis-order=long-lat", types.Blob)) 505 require.NoError(err) 506 507 v, err := f.Eval(sql.NewEmptyContext(), nil) 508 require.NoError(err) 509 require.Equal(types.Polygon{SRID: types.GeoSpatialSRID, Lines: []types.LineString{{SRID: types.GeoSpatialSRID, Points: []types.Point{{SRID: types.GeoSpatialSRID, X: 0, Y: 0}, {SRID: types.GeoSpatialSRID, X: 1, Y: 1}, {SRID: types.GeoSpatialSRID, X: 0, Y: 1}, {SRID: types.GeoSpatialSRID, X: 0, Y: 0}}}}}, v) 510 }) 511 512 t.Run("convert multipoint with valid srid 3857", func(t *testing.T) { 513 require := require.New(t) 514 res, err := hex.DecodeString("0104000000020000000101000000000000000000F03F0000000000000040010100000000000000000008400000000000001040") 515 require.NoError(err) 516 f, err := NewGeomFromWKB(expression.NewLiteral(res, types.Blob), 517 expression.NewLiteral(3857, types.Uint32)) 518 require.NoError(err) 519 520 v, err := f.Eval(sql.NewEmptyContext(), nil) 521 require.NoError(err) 522 require.Equal(types.MultiPoint{SRID: 3857, Points: []types.Point{{SRID: 3857, X: 1, Y: 2}, {SRID: 3857, X: 3, Y: 4}}}, v) 523 }) 524 525 t.Run("convert multipoint with valid srid 4326", func(t *testing.T) { 526 require := require.New(t) 527 res, err := hex.DecodeString("0104000000020000000101000000000000000000F03F0000000000000040010100000000000000000008400000000000001040") 528 require.NoError(err) 529 f, err := NewGeomFromWKB(expression.NewLiteral(res, types.Blob), 530 expression.NewLiteral(types.GeoSpatialSRID, types.Uint32)) 531 require.NoError(err) 532 533 v, err := f.Eval(sql.NewEmptyContext(), nil) 534 require.NoError(err) 535 require.Equal(types.MultiPoint{SRID: types.GeoSpatialSRID, Points: []types.Point{{SRID: types.GeoSpatialSRID, X: 1, Y: 2}, {SRID: types.GeoSpatialSRID, X: 3, Y: 4}}}, v) 536 }) 537 538 t.Run("convert multipoint with invalid srid 1234", func(t *testing.T) { 539 require := require.New(t) 540 res, err := hex.DecodeString("0104000000020000000101000000000000000000F03F0000000000000040010100000000000000000008400000000000001040") 541 require.NoError(err) 542 f, err := NewGeomFromWKB(expression.NewLiteral(res, types.Blob), 543 expression.NewLiteral(1234, types.Uint32)) 544 require.NoError(err) 545 546 _, err = f.Eval(sql.NewEmptyContext(), nil) 547 require.Error(err) 548 }) 549 550 t.Run("convert multipoint with srid 4326 axis long-lat", func(t *testing.T) { 551 require := require.New(t) 552 res, err := hex.DecodeString("0104000000020000000101000000000000000000F03F0000000000000040010100000000000000000008400000000000001040") 553 require.NoError(err) 554 f, err := NewGeomFromWKB(expression.NewLiteral(res, types.Blob), 555 expression.NewLiteral(types.GeoSpatialSRID, types.Uint32), 556 expression.NewLiteral("axis-order=long-lat", types.Blob)) 557 require.NoError(err) 558 559 v, err := f.Eval(sql.NewEmptyContext(), nil) 560 require.NoError(err) 561 require.Equal(types.MultiPoint{SRID: types.GeoSpatialSRID, Points: []types.Point{{SRID: types.GeoSpatialSRID, X: 2, Y: 1}, {SRID: types.GeoSpatialSRID, X: 4, Y: 3}}}, v) 562 }) 563 564 t.Run("convert null", func(t *testing.T) { 565 require := require.New(t) 566 f, err := NewGeomFromWKB(expression.NewLiteral(nil, types.Null)) 567 require.NoError(err) 568 v, err := f.Eval(sql.NewEmptyContext(), nil) 569 require.NoError(err) 570 require.Equal(nil, v) 571 }) 572 573 t.Run("convert null srid", func(t *testing.T) { 574 require := require.New(t) 575 res, err := hex.DecodeString("0101000000000000000000F03F0000000000000040") 576 require.NoError(err) 577 f, err := NewGeomFromWKB(expression.NewLiteral(res, types.Blob), 578 expression.NewLiteral(nil, types.Null)) 579 v, err := f.Eval(sql.NewEmptyContext(), nil) 580 require.NoError(err) 581 require.Equal(nil, v) 582 }) 583 584 t.Run("convert null axis option", func(t *testing.T) { 585 require := require.New(t) 586 res, err := hex.DecodeString("0101000000000000000000F03F0000000000000040") 587 require.NoError(err) 588 f, err := NewGeomFromWKB(expression.NewLiteral(res, types.Blob), 589 expression.NewLiteral(0, types.Uint32), 590 expression.NewLiteral(nil, types.Null)) 591 v, err := f.Eval(sql.NewEmptyContext(), nil) 592 require.NoError(err) 593 require.Equal(nil, v) 594 }) 595 596 t.Run("empty args errors", func(t *testing.T) { 597 require := require.New(t) 598 _, err := NewPointFromWKB() 599 require.Error(err) 600 _, err = NewLineFromWKB() 601 require.Error(err) 602 _, err = NewPolyFromWKB() 603 require.Error(err) 604 _, err = NewMultiPoint() 605 require.Error(err) 606 _, err = NewMultiLineString() 607 require.Error(err) 608 _, err = NewMultiPolygon() 609 require.Error(err) 610 _, err = NewGeomFromWKB() 611 require.Error(err) 612 _, err = NewGeomCollFromWKB() 613 require.Error(err) 614 }) 615 }