Skip to content

Commit

Permalink
[Postgis] Support Geography (#333)
Browse files Browse the repository at this point in the history
  • Loading branch information
Tang8330 authored Feb 12, 2024
1 parent 57d6139 commit e72e36d
Show file tree
Hide file tree
Showing 5 changed files with 39 additions and 5 deletions.
5 changes: 2 additions & 3 deletions lib/cdc/util/geometry.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,8 @@ import (
"encoding/json"
"fmt"

"github.com/twpayne/go-geom/encoding/ewkb"
"github.com/twpayne/go-geom/encoding/geojson"

"github.com/twpayne/go-geom/encoding/wkb"
)

type GeoJSON struct {
Expand Down Expand Up @@ -81,7 +80,7 @@ func parseGeometry(value interface{}) (string, error) {
return "", fmt.Errorf("error decoding base64: %w", err)
}

geom, err := wkb.Unmarshal(wkbBytes)
geom, err := ewkb.Unmarshal(wkbBytes)
if err != nil {
return "", fmt.Errorf("error unmarshalling WKB bytes: %w", err)
}
Expand Down
2 changes: 1 addition & 1 deletion lib/cdc/util/parse.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ func parseField(field debezium.Field, value interface{}) interface{} {
if err == nil {
return valString
}
case debezium.GeometryType:
case debezium.GeometryType, debezium.GeographyType:
geometryString, err := parseGeometry(value)
if err == nil {
return geometryString
Expand Down
33 changes: 33 additions & 0 deletions lib/cdc/util/parse_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,39 @@ func TestParseField(t *testing.T) {
expectedValue: "123.45",
expectedDecimal: true,
},
{
name: "geometry (no srid)",
field: debezium.Field{
DebeziumType: string(debezium.GeometryType),
},
value: map[string]interface{}{
"srid": nil,
"wkb": "AQEAAAAAAAAAAADwPwAAAAAAABRA",
},
expectedValue: `{"type":"Feature","geometry":{"type":"Point","coordinates":[1,5]},"properties":null}`,
},
{
name: "geometry (w/ srid)",
field: debezium.Field{
DebeziumType: string(debezium.GeometryType),
},
value: map[string]interface{}{
"srid": 4326,
"wkb": "AQEAACDmEAAAAAAAAAAA8D8AAAAAAAAYQA==",
},
expectedValue: `{"type":"Feature","geometry":{"type":"Point","coordinates":[1,6]},"properties":null}`,
},
{
name: "geography (w/ srid)",
field: debezium.Field{
DebeziumType: string(debezium.GeographyType),
},
value: map[string]interface{}{
"srid": 4326,
"wkb": "AQEAACDmEAAAAAAAAADAXkAAAAAAAIBDwA==",
},
expectedValue: `{"type":"Feature","geometry":{"type":"Point","coordinates":[123,-39]},"properties":null}`,
},
}

for _, testCase := range testCases {
Expand Down
2 changes: 1 addition & 1 deletion lib/debezium/schema.go
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ func (f Field) ToKindDetails() typing.KindDetails {
return typing.NewKindDetailsFromTemplate(typing.ETime, ext.DateKindType)
case string(Time), string(TimeMicro), string(TimeKafkaConnect), string(TimeWithTimezone):
return typing.NewKindDetailsFromTemplate(typing.ETime, ext.TimeKindType)
case string(JSON), string(GeometryPointType), string(GeometryType):
case string(JSON), string(GeometryPointType), string(GeometryType), string(GeographyType):
return typing.Struct
case string(KafkaDecimalType):
scaleAndPrecision, err := f.GetScaleAndPrecision()
Expand Down
2 changes: 2 additions & 0 deletions lib/debezium/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ const (
// PostGIS data types
GeometryPointType SupportedDebeziumType = "io.debezium.data.geometry.Point"
GeometryType SupportedDebeziumType = "io.debezium.data.geometry.Geometry"
GeographyType SupportedDebeziumType = "io.debezium.data.geometry.Geography"

KafkaDecimalPrecisionKey = "connect.decimal.precision"
)
Expand All @@ -54,6 +55,7 @@ var typesThatRequireTypeCasting = []SupportedDebeziumType{
JSON,
GeometryPointType,
GeometryType,
GeographyType,
}

func RequiresSpecialTypeCasting(typeLabel string) (bool, SupportedDebeziumType) {
Expand Down

0 comments on commit e72e36d

Please sign in to comment.