From 760362bec006ea4e26ad8b6ca9cd34a3a8bd0987 Mon Sep 17 00:00:00 2001 From: Nathan <148575555+nathan-artie@users.noreply.github.com> Date: Sun, 17 Mar 2024 19:20:59 -0700 Subject: [PATCH] [postgres] Emit `float32` values for `real` columns (#298) --- integration_tests/postgres/main.go | 2 +- lib/debezium/converters/passthrough.go | 4 ++-- lib/postgres/parse/parse.go | 8 ++++++++ 3 files changed, 11 insertions(+), 3 deletions(-) diff --git a/integration_tests/postgres/main.go b/integration_tests/postgres/main.go index 91cd3d2c..5d6126ec 100644 --- a/integration_tests/postgres/main.go +++ b/integration_tests/postgres/main.go @@ -687,7 +687,7 @@ const expectedPayloadTemplate = `{ "x": 12.34, "y": 56.78 }, - "c_real": 45.678001403808594, + "c_real": 45.678, "c_serial": 1000000123, "c_smallint": 32767, "c_text": "QWERTYUIOP", diff --git a/lib/debezium/converters/passthrough.go b/lib/debezium/converters/passthrough.go index c1c3996c..3c4a4de8 100644 --- a/lib/debezium/converters/passthrough.go +++ b/lib/debezium/converters/passthrough.go @@ -78,10 +78,10 @@ func (FloatPassthrough) ToField(name string) debezium.Field { func (FloatPassthrough) Convert(value any) (any, error) { switch castValue := value.(type) { - case float32, float64: + case float32: return castValue, nil } - return nil, fmt.Errorf("expected float32/float64 got %T with value: %v", value, value) + return nil, fmt.Errorf("expected float32 got %T with value: %v", value, value) } // float32, float64 -> float64 diff --git a/lib/postgres/parse/parse.go b/lib/postgres/parse/parse.go index 3cabf9f9..2e982b0b 100644 --- a/lib/postgres/parse/parse.go +++ b/lib/postgres/parse/parse.go @@ -23,6 +23,14 @@ func ParseValue(colKind schema.DataType, value any) (any, error) { return valString, nil } return nil, fmt.Errorf("value: %v not of string type for bit", value) + case schema.Real: + float64Value, ok := value.(float64) + if !ok { + return nil, fmt.Errorf("expected float64 got %T with value: %v", value, value) + } + // pgx returns `real`s as float64 even though they are always 32 bits + // https://www.postgresql.org/docs/current/datatype-numeric.html#DATATYPE-NUMERIC + return float32(float64Value), nil case schema.UserDefinedText: stringSlice, isOk := value.(string) if !isOk {