Skip to content

Commit

Permalink
util/encoding: break dependency on geo
Browse files Browse the repository at this point in the history
The `geo` package depends on tons of stuff. The usage
only needed geopb.

Release note: None
  • Loading branch information
ajwerner committed Apr 30, 2021
1 parent 56b599a commit e3547dc
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 34 deletions.
19 changes: 12 additions & 7 deletions pkg/sql/rowenc/column_type_encoding.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import (

"github.com/cockroachdb/apd/v2"
"github.com/cockroachdb/cockroach/pkg/geo"
"github.com/cockroachdb/cockroach/pkg/geo/geopb"
"github.com/cockroachdb/cockroach/pkg/roachpb"
"github.com/cockroachdb/cockroach/pkg/sql/catalog"
"github.com/cockroachdb/cockroach/pkg/sql/catalog/descpb"
Expand Down Expand Up @@ -99,9 +100,9 @@ func EncodeTableKey(b []byte, val tree.Datum, dir encoding.Direction) ([]byte, e
return encoding.EncodeStringDescending(b, string(*t)), nil
case *tree.DBox2D:
if dir == encoding.Ascending {
return encoding.EncodeBox2DAscending(b, t.CartesianBoundingBox)
return encoding.EncodeBox2DAscending(b, t.CartesianBoundingBox.BoundingBox)
}
return encoding.EncodeBox2DDescending(b, t.CartesianBoundingBox)
return encoding.EncodeBox2DDescending(b, t.CartesianBoundingBox.BoundingBox)
case *tree.DGeography:
so := t.Geography.SpatialObjectRef()
if dir == encoding.Ascending {
Expand Down Expand Up @@ -306,13 +307,15 @@ func DecodeTableKey(
}
return a.NewDBytes(tree.DBytes(r)), rkey, err
case types.Box2DFamily:
var r geo.CartesianBoundingBox
var r geopb.BoundingBox
if dir == encoding.Ascending {
rkey, r, err = encoding.DecodeBox2DAscending(key)
} else {
rkey, r, err = encoding.DecodeBox2DDescending(key)
}
return a.NewDBox2D(tree.DBox2D{CartesianBoundingBox: r}), rkey, err
return a.NewDBox2D(tree.DBox2D{
CartesianBoundingBox: geo.CartesianBoundingBox{BoundingBox: r},
}), rkey, err
case types.GeographyFamily:
g := a.NewDGeographyEmpty()
so := g.Geography.SpatialObjectRef()
Expand Down Expand Up @@ -469,7 +472,7 @@ func EncodeTableValue(
case *tree.DDate:
return encoding.EncodeIntValue(appendTo, uint32(colID), t.UnixEpochDaysWithOrig()), nil
case *tree.DBox2D:
return encoding.EncodeBox2DValue(appendTo, uint32(colID), t.CartesianBoundingBox)
return encoding.EncodeBox2DValue(appendTo, uint32(colID), t.CartesianBoundingBox.BoundingBox)
case *tree.DGeography:
return encoding.EncodeGeoValue(appendTo, uint32(colID), t.SpatialObjectRef())
case *tree.DGeometry:
Expand Down Expand Up @@ -599,7 +602,9 @@ func DecodeUntaggedDatum(a *DatumAlloc, t *types.T, buf []byte) (tree.Datum, []b
if err != nil {
return nil, b, err
}
return a.NewDBox2D(tree.DBox2D{CartesianBoundingBox: data}), b, nil
return a.NewDBox2D(tree.DBox2D{
CartesianBoundingBox: geo.CartesianBoundingBox{BoundingBox: data},
}), b, nil
case types.GeographyFamily:
g := a.NewDGeographyEmpty()
so := g.Geography.SpatialObjectRef()
Expand Down Expand Up @@ -1395,7 +1400,7 @@ func encodeArrayElement(b []byte, d tree.Datum) ([]byte, error) {
case *tree.DDate:
return encoding.EncodeUntaggedIntValue(b, t.UnixEpochDaysWithOrig()), nil
case *tree.DBox2D:
return encoding.EncodeUntaggedBox2DValue(b, t.CartesianBoundingBox)
return encoding.EncodeUntaggedBox2DValue(b, t.CartesianBoundingBox.BoundingBox)
case *tree.DGeography:
return encoding.EncodeUntaggedGeoValue(b, t.SpatialObjectRef())
case *tree.DGeometry:
Expand Down
1 change: 0 additions & 1 deletion pkg/util/encoding/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ go_library(
importpath = "github.com/cockroachdb/cockroach/pkg/util/encoding",
visibility = ["//visibility:public"],
deps = [
"//pkg/geo",
"//pkg/geo/geopb",
"//pkg/util/bitarray",
"//pkg/util/duration",
Expand Down
27 changes: 12 additions & 15 deletions pkg/util/encoding/encoding.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@ import (
"unsafe"

"github.com/cockroachdb/apd/v2"
"github.com/cockroachdb/cockroach/pkg/geo"
"github.com/cockroachdb/cockroach/pkg/geo/geopb"
"github.com/cockroachdb/cockroach/pkg/util/bitarray"
"github.com/cockroachdb/cockroach/pkg/util/duration"
Expand Down Expand Up @@ -1093,7 +1092,7 @@ func decodeTime(b []byte) (r []byte, sec int64, nsec int64, err error) {
}

// EncodeBox2DAscending encodes a bounding box in ascending order.
func EncodeBox2DAscending(b []byte, box geo.CartesianBoundingBox) ([]byte, error) {
func EncodeBox2DAscending(b []byte, box geopb.BoundingBox) ([]byte, error) {
b = append(b, box2DMarker)
b = EncodeFloatAscending(b, box.LoX)
b = EncodeFloatAscending(b, box.HiX)
Expand All @@ -1103,7 +1102,7 @@ func EncodeBox2DAscending(b []byte, box geo.CartesianBoundingBox) ([]byte, error
}

// EncodeBox2DDescending encodes a bounding box in descending order.
func EncodeBox2DDescending(b []byte, box geo.CartesianBoundingBox) ([]byte, error) {
func EncodeBox2DDescending(b []byte, box geopb.BoundingBox) ([]byte, error) {
b = append(b, box2DMarker)
b = EncodeFloatDescending(b, box.LoX)
b = EncodeFloatDescending(b, box.HiX)
Expand All @@ -1113,8 +1112,8 @@ func EncodeBox2DDescending(b []byte, box geo.CartesianBoundingBox) ([]byte, erro
}

// DecodeBox2DAscending decodes a box2D object in ascending order.
func DecodeBox2DAscending(b []byte) ([]byte, geo.CartesianBoundingBox, error) {
box := geo.CartesianBoundingBox{}
func DecodeBox2DAscending(b []byte) ([]byte, geopb.BoundingBox, error) {
box := geopb.BoundingBox{}
if PeekType(b) != Box2D {
return nil, box, errors.Errorf("did not find Box2D marker")
}
Expand All @@ -1141,8 +1140,8 @@ func DecodeBox2DAscending(b []byte) ([]byte, geo.CartesianBoundingBox, error) {
}

// DecodeBox2DDescending decodes a box2D object in descending order.
func DecodeBox2DDescending(b []byte) ([]byte, geo.CartesianBoundingBox, error) {
box := geo.CartesianBoundingBox{}
func DecodeBox2DDescending(b []byte) ([]byte, geopb.BoundingBox, error) {
box := geopb.BoundingBox{}
if PeekType(b) != Box2D {
return nil, box, errors.Errorf("did not find Box2D marker")
}
Expand Down Expand Up @@ -2368,16 +2367,16 @@ func EncodeUntaggedTimeTZValue(appendTo []byte, t timetz.TimeTZ) []byte {
return EncodeNonsortingStdlibVarint(appendTo, int64(t.OffsetSecs))
}

// EncodeBox2DValue encodes a geo.CartesianBoundingBox with its value tag, appends it to
// EncodeBox2DValue encodes a geopb.BoundingBox with its value tag, appends it to
// the supplied buffer and returns the final buffer.
func EncodeBox2DValue(appendTo []byte, colID uint32, b geo.CartesianBoundingBox) ([]byte, error) {
func EncodeBox2DValue(appendTo []byte, colID uint32, b geopb.BoundingBox) ([]byte, error) {
appendTo = EncodeValueTag(appendTo, colID, Box2D)
return EncodeUntaggedBox2DValue(appendTo, b)
}

// EncodeUntaggedBox2DValue encodes a geo.CartesianBoundingBox value, appends it to the supplied buffer,
// EncodeUntaggedBox2DValue encodes a geopb.BoundingBox value, appends it to the supplied buffer,
// and returns the final buffer.
func EncodeUntaggedBox2DValue(appendTo []byte, b geo.CartesianBoundingBox) ([]byte, error) {
func EncodeUntaggedBox2DValue(appendTo []byte, b geopb.BoundingBox) ([]byte, error) {
appendTo = EncodeFloatAscending(appendTo, b.LoX)
appendTo = EncodeFloatAscending(appendTo, b.HiX)
appendTo = EncodeFloatAscending(appendTo, b.LoY)
Expand Down Expand Up @@ -2670,10 +2669,8 @@ func DecodeDecimalValue(b []byte) (remaining []byte, d apd.Decimal, err error) {
}

// DecodeUntaggedBox2DValue decodes a value encoded by EncodeUntaggedBox2DValue.
func DecodeUntaggedBox2DValue(
b []byte,
) (remaining []byte, box geo.CartesianBoundingBox, err error) {
box = geo.CartesianBoundingBox{}
func DecodeUntaggedBox2DValue(b []byte) (remaining []byte, box geopb.BoundingBox, err error) {
box = geopb.BoundingBox{}
remaining = b

remaining, box.LoX, err = DecodeFloatAscending(remaining)
Expand Down
22 changes: 11 additions & 11 deletions pkg/util/encoding/encoding_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1150,18 +1150,18 @@ func TestEncodeDecodeTimeTZ(t *testing.T) {

func TestEncodeDecodeBox2D(t *testing.T) {
testCases := []struct {
ordered []geo.CartesianBoundingBox
ordered []geopb.BoundingBox
}{
{
ordered: []geo.CartesianBoundingBox{
{BoundingBox: geopb.BoundingBox{LoX: -100, HiX: 99, LoY: -100, HiY: 100}},
{BoundingBox: geopb.BoundingBox{LoX: -100, HiX: 100, LoY: -100, HiY: 100}},
{BoundingBox: geopb.BoundingBox{LoX: -50, HiX: 100, LoY: -100, HiY: 100}},
{BoundingBox: geopb.BoundingBox{LoX: 0, HiX: 100, LoY: 0, HiY: 100}},
{BoundingBox: geopb.BoundingBox{LoX: 0, HiX: 100, LoY: 50, HiY: 100}},
{BoundingBox: geopb.BoundingBox{LoX: 10, HiX: 100, LoY: -100, HiY: 100}},
{BoundingBox: geopb.BoundingBox{LoX: 10, HiX: 100, LoY: -10, HiY: 50}},
{BoundingBox: geopb.BoundingBox{LoX: 10, HiX: 100, LoY: -10, HiY: 100}},
ordered: []geopb.BoundingBox{
{LoX: -100, HiX: 99, LoY: -100, HiY: 100},
{LoX: -100, HiX: 100, LoY: -100, HiY: 100},
{LoX: -50, HiX: 100, LoY: -100, HiY: 100},
{LoX: 0, HiX: 100, LoY: 0, HiY: 100},
{LoX: 0, HiX: 100, LoY: 50, HiY: 100},
{LoX: 10, HiX: 100, LoY: -100, HiY: 100},
{LoX: 10, HiX: 100, LoY: -10, HiY: 50},
{LoX: 10, HiX: 100, LoY: -10, HiY: 100},
},
},
}
Expand All @@ -1173,7 +1173,7 @@ func TestEncodeDecodeBox2D(t *testing.T) {
for j := range tc.ordered {
var b []byte
var err error
var decoded geo.CartesianBoundingBox
var decoded geopb.BoundingBox

if dir == Ascending {
b, err = EncodeBox2DAscending(b, tc.ordered[j])
Expand Down

0 comments on commit e3547dc

Please sign in to comment.