jk-shah commented on code in PR #40166:
URL: https://github.com/apache/beam/pull/40166#discussion_r4646150500


##########
sdks/go/pkg/beam/io/postgresio/binary_types_test.go:
##########
@@ -0,0 +1,310 @@
+// Licensed to the Apache Software Foundation (ASF) under one or more
+// contributor license agreements.  See the NOTICE file distributed with
+// this work for additional information regarding copyright ownership.
+// The ASF licenses this file to You under the Apache License, Version 2.0
+// (the "License"); you may not use this file except in compliance with
+// the License.  You may obtain a copy of the License at
+//
+//    http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
+
+package postgresio
+
+import (
+       "encoding/binary"
+       "strings"
+       "testing"
+       "time"
+)
+
+// encodeNumeric builds a PostgreSQL binary NUMERIC payload.
+//
+// Used to construct wire inputs for the decoder tests. The layout mirrors
+// PostgreSQL's own: int16 ndigits, int16 weight, uint16 sign, int16 dscale,
+// then base-10000 digit words.
+func encodeNumeric(weight int16, sign uint16, dscale int16, digits []uint16) 
[]byte {
+       b := make([]byte, 0, 8+len(digits)*2)
+       b = binary.BigEndian.AppendUint16(b, uint16(len(digits)))
+       b = binary.BigEndian.AppendUint16(b, uint16(weight))
+       b = binary.BigEndian.AppendUint16(b, sign)
+       b = binary.BigEndian.AppendUint16(b, uint16(dscale))
+       for _, d := range digits {
+               b = binary.BigEndian.AppendUint16(b, d)
+       }
+       return b
+}
+
+// TestDecodeBinaryNumeric covers the representative shapes PostgreSQL emits.
+//
+// FINDING P1-3. Before this decoder existed, every one of these inputs was
+// returned to the pipeline as an opaque []byte.
+func TestDecodeBinaryNumeric(t *testing.T) {
+       tests := []struct {
+               name   string
+               weight int16
+               sign   uint16
+               dscale int16
+               digits []uint16
+               want   string
+       }{
+               // 123.45 -> digit words [123][4500], weight 0, dscale 2
+               {"typical decimal", 0, numericPositive, 2, []uint16{123, 4500}, 
"123.45"},
+               // -123.45
+               {"negative", 0, numericNegative, 2, []uint16{123, 4500}, 
"-123.45"},
+               // 0 is encoded with no digit words
+               {"zero", 0, numericPositive, 0, nil, "0"},
+               {"zero with scale", 0, numericPositive, 2, nil, "0.00"},
+               // Integer with no fractional part: 1234 -> weight 0, [1234]
+               {"small integer", 0, numericPositive, 0, []uint16{1234}, 
"1234"},
+               // 100000000 -> weight 2, words [1][0000][0000]
+               {"large integer", 2, numericPositive, 0, []uint16{1, 0, 0}, 
"100000000"},
+               // Monetary value with two decimals: 19.99
+               {"money", 0, numericPositive, 2, []uint16{19, 9900}, "19.99"},
+               // Trailing zeros must be preserved to dscale: 5.10
+               {"preserves trailing zero", 0, numericPositive, 2, []uint16{5, 
1000}, "5.10"},
+               // Special values
+               {"NaN", 0, numericNaN, 0, nil, "NaN"},
+               {"positive infinity", 0, numericPosInf, 0, nil, "Infinity"},
+               {"negative infinity", 0, numericNegInf, 0, nil, "-Infinity"},
+       }
+
+       for _, tc := range tests {
+               t.Run(tc.name, func(t *testing.T) {
+                       got, err := 
decodeBinaryNumeric(encodeNumeric(tc.weight, tc.sign, tc.dscale, tc.digits))
+                       if err != nil {
+                               t.Fatalf("decodeBinaryNumeric() error: %v", err)
+                       }
+                       if got != tc.want {
+                               t.Errorf("decodeBinaryNumeric() = %q, want %q", 
got, tc.want)
+                       }
+               })
+       }
+}
+
+// TestDecodeBinaryNumericPreservesPrecisionBeyondFloat64 is the core reason
+// NUMERIC is returned as a string.
+//
+// A float64 carries about 15-17 significant decimal digits. A NUMERIC with
+// more than that would be silently rounded, which for a financial workload is
+// data corruption that no downstream check would notice.

Review Comment:
   Removed from this PR (`2a3f6fc02f`) since its caller is the downstream CDC 
reader
   
   To answer the design question for when it returns: `math/big.Float` uses 
binary (base-2) mantissas rather than base-10, so common decimal values like 
`0.1` or `19.99` have no exact finite binary representation at any precision, 
and `big.Float` also drops PostgreSQL's `dscale` (so `"5.10"` vs `"5.1"` and 
`NUMERIC` `NaN` cannot round-trip losslessly). Keeping the exact decimal 
representation avoids silent base-2 rounding on financial columns until the 
caller chooses a decimal library or target type.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to