Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
64465: util/encoding: break dependency on geo r=ajwerner a=ajwerner

The `geo` package depends on tons of stuff. The usage
only needed geopb.

Part of cockroachdb#64450.

Release note: None

Co-authored-by: Andrew Werner <awerner32@gmail.com>
  • Loading branch information
craig[bot] and ajwerner committed Apr 30, 2021
2 parents 55f06d7 + e3547dc commit bbf037b
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 bbf037b

Please sign in to comment.