Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

tree: correct sizes for geospatial datums #54868

Merged
merged 1 commit into from
Sep 29, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 13 additions & 1 deletion pkg/geo/geopb/geopb.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,25 @@

package geopb

import "fmt"
import (
"fmt"
"unsafe"
)

// EWKBHex returns the EWKB-hex version of this data type
func (b *SpatialObject) EWKBHex() string {
return fmt.Sprintf("%X", b.EWKB)
}

// MemSize returns the size of the spatial object in memory.
func (b *SpatialObject) MemSize() uintptr {
var bboxSize uintptr
if bbox := b.BoundingBox; bbox != nil {
bboxSize = unsafe.Sizeof(*bbox)
}
return unsafe.Sizeof(*b) + bboxSize + uintptr(len(b.EWKB))
}

// MultiType returns the corresponding multi-type for a shape type, or unset
// if there is no multi-type.
func (s ShapeType) MultiType() ShapeType {
Expand Down
4 changes: 2 additions & 2 deletions pkg/sql/sem/tree/datum.go
Original file line number Diff line number Diff line change
Expand Up @@ -2826,7 +2826,7 @@ func (d *DGeography) Format(ctx *FmtCtx) {

// Size implements the Datum interface.
func (d *DGeography) Size() uintptr {
return unsafe.Sizeof(*d)
return d.Geography.SpatialObjectRef().MemSize()
}

// DGeometry is the Geometry Datum.
Expand Down Expand Up @@ -2934,7 +2934,7 @@ func (d *DGeometry) Format(ctx *FmtCtx) {

// Size implements the Datum interface.
func (d *DGeometry) Size() uintptr {
return unsafe.Sizeof(*d)
return d.Geometry.SpatialObjectRef().MemSize()
}

// DBox2D is the Datum representation of the Box2D type.
Expand Down
26 changes: 26 additions & 0 deletions pkg/sql/sem/tree/datum_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1128,3 +1128,29 @@ var _ tree.ParseTimeContext = testParseTimeContext{}
func (t testParseTimeContext) GetRelativeParseTime() time.Time {
return time.Time(t)
}

func TestGeospatialSize(t *testing.T) {
defer leaktest.AfterTest(t)()
testCases := []struct {
wkt string
expected uintptr
}{
{"SRID=4004;POINT EMPTY", 73},
{"SRID=4326;LINESTRING(0 0, 10 0)", 125},
}

for _, tc := range testCases {
t.Run(tc.wkt, func(t *testing.T) {
t.Run("geometry", func(t *testing.T) {
g, err := tree.ParseDGeometry(tc.wkt)
require.NoError(t, err)
require.Equal(t, tc.expected, g.Size())
})
t.Run("geography", func(t *testing.T) {
g, err := tree.ParseDGeography(tc.wkt)
require.NoError(t, err)
require.Equal(t, tc.expected, g.Size())
})
})
}
}