Skip to content

Commit 33c4554

Browse files
CopilotasmyasnikovCopilot
authored
Add integration tests for Decimal scanning and fix byte ordering bug (#1938)
Co-authored-by: copilot-swe-agent[bot] <198982749+Copilot@users.noreply.github.com> Co-authored-by: asmyasnikov <14202262+asmyasnikov@users.noreply.github.com> Co-authored-by: Aleksey Myasnikov <asmyasnikov@ydb.tech> Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
1 parent b6e2e39 commit 33c4554

File tree

21 files changed

+1573
-356
lines changed

21 files changed

+1573
-356
lines changed

CHANGELOG.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
* Moved `internal/decimal` package to `pkg/decimal` for public usage
2+
13
## v3.122.0
24
* Added `trace.NodeHintInfo` field for OnPoolGet trace callback which stores info for node hint misses
35
* Added `ydb_go_sdk_ydb_table_pool_node_hint_miss` and `ydb_go_sdk_ydb_query_pool_node_hint_miss` metrics for node hint misses
@@ -11,7 +13,7 @@
1113
* Changed internal pprof label to pyroscope supported format
1214
* Added `query.ImplicitTxControl()` transaction control (the same as `query.NoTx()` and `query.EmptyTxControl()`). See more about implicit transactions on [ydb.tech](https://ydb.tech/docs/en/concepts/transactions?version=v25.2#implicit)
1315
* Added `SnapshotReadWrite` isolation mode support to `database/sql` driver using `sql.TxOptions{Isolation: sql.LevelSnapshot, ReadOnly: false}`
14-
* Move `internal/ratelimiter/options` to `ratelimiter/options` for public usage
16+
* Moved `internal/ratelimiter/options` to `ratelimiter/options` for public usage
1517

1618
## v3.120.0
1719
* Added support of `SnapshotReadWrite` isolation mode into query and table clients

internal/decimal/decimal_test.go

Lines changed: 0 additions & 86 deletions
This file was deleted.

internal/decimal/type.go

Lines changed: 0 additions & 19 deletions
This file was deleted.

internal/query/scanner/struct_test.go

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,10 @@ import (
99
"github.com/stretchr/testify/require"
1010
"github.com/ydb-platform/ydb-go-genproto/protos/Ydb"
1111

12-
"github.com/ydb-platform/ydb-go-sdk/v3/internal/decimal"
1312
"github.com/ydb-platform/ydb-go-sdk/v3/internal/value"
13+
"github.com/ydb-platform/ydb-go-sdk/v3/pkg/decimal"
1414
"github.com/ydb-platform/ydb-go-sdk/v3/pkg/xtest"
15-
ttypes "github.com/ydb-platform/ydb-go-sdk/v3/table/types"
15+
"github.com/ydb-platform/ydb-go-sdk/v3/table/types"
1616
)
1717

1818
func TestFieldName(t *testing.T) {
@@ -934,9 +934,9 @@ func TestScannerDecimal(t *testing.T) {
934934
},
935935
))
936936
var row struct {
937-
A ttypes.Decimal
937+
A types.Decimal
938938
}
939-
expected := ttypes.Decimal{Bytes: decimal.BigIntToByte(big.NewInt(10200000000), 22, 9), Precision: 22, Scale: 9}
939+
expected := types.Decimal{Bytes: decimal.BigIntToByte(big.NewInt(10200000000), 22), Precision: 22, Scale: 9}
940940
err := scanner.ScanStruct(&row)
941941
require.NoError(t, err)
942942
require.Equal(t, expected, row.A)
@@ -964,9 +964,9 @@ func TestScannerDecimalNegative(t *testing.T) {
964964
},
965965
))
966966
var row struct {
967-
A ttypes.Decimal
967+
A types.Decimal
968968
}
969-
expected := ttypes.Decimal{Bytes: decimal.BigIntToByte(big.NewInt(-2005000000), 22, 9), Precision: 22, Scale: 9}
969+
expected := types.Decimal{Bytes: decimal.BigIntToByte(big.NewInt(-2005000000), 22), Precision: 22, Scale: 9}
970970
err := scanner.ScanStruct(&row)
971971
require.NoError(t, err)
972972
require.Equal(t, expected, row.A)
@@ -995,7 +995,7 @@ func TestScannerDecimalBigDecimal(t *testing.T) {
995995
},
996996
))
997997
var row struct {
998-
A ttypes.Decimal
998+
A types.Decimal
999999
}
10001000
expectedVal := decimal.Decimal{
10011001
Bytes: [16]byte{0, 19, 66, 97, 114, 199, 77, 130, 43, 135, 143, 232, 0, 0, 0, 0},

internal/scanner/scanner.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,9 @@ import (
66

77
"github.com/google/uuid"
88

9-
"github.com/ydb-platform/ydb-go-sdk/v3/internal/decimal"
109
"github.com/ydb-platform/ydb-go-sdk/v3/internal/types"
1110
"github.com/ydb-platform/ydb-go-sdk/v3/internal/value"
11+
"github.com/ydb-platform/ydb-go-sdk/v3/pkg/decimal"
1212
)
1313

1414
// RawValue scanning non-primitive yql types or for own implementation scanner native API

internal/table/scanner/scan_raw.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,10 @@ import (
1212
"github.com/google/uuid"
1313
"github.com/ydb-platform/ydb-go-genproto/protos/Ydb"
1414

15-
"github.com/ydb-platform/ydb-go-sdk/v3/internal/decimal"
1615
"github.com/ydb-platform/ydb-go-sdk/v3/internal/types"
1716
"github.com/ydb-platform/ydb-go-sdk/v3/internal/value"
1817
"github.com/ydb-platform/ydb-go-sdk/v3/internal/xerrors"
18+
"github.com/ydb-platform/ydb-go-sdk/v3/pkg/decimal"
1919
"github.com/ydb-platform/ydb-go-sdk/v3/pkg/xstring"
2020
)
2121

internal/table/scanner/scanner.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,12 +12,12 @@ import (
1212
"github.com/google/uuid"
1313
"github.com/ydb-platform/ydb-go-genproto/protos/Ydb"
1414

15-
"github.com/ydb-platform/ydb-go-sdk/v3/internal/decimal"
1615
"github.com/ydb-platform/ydb-go-sdk/v3/internal/scanner"
1716
internalTypes "github.com/ydb-platform/ydb-go-sdk/v3/internal/types"
1817
"github.com/ydb-platform/ydb-go-sdk/v3/internal/value"
1918
"github.com/ydb-platform/ydb-go-sdk/v3/internal/xerrors"
2019
"github.com/ydb-platform/ydb-go-sdk/v3/internal/xsync"
20+
"github.com/ydb-platform/ydb-go-sdk/v3/pkg/decimal"
2121
"github.com/ydb-platform/ydb-go-sdk/v3/pkg/xstring"
2222
"github.com/ydb-platform/ydb-go-sdk/v3/table/options"
2323
"github.com/ydb-platform/ydb-go-sdk/v3/table/result"

internal/value/any.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package value
22

33
import (
44
"github.com/ydb-platform/ydb-go-sdk/v3/internal/xerrors"
5+
"github.com/ydb-platform/ydb-go-sdk/v3/pkg/decimal"
56
"github.com/ydb-platform/ydb-go-sdk/v3/pkg/xstring"
67
)
78

@@ -88,6 +89,8 @@ func Any(v Value) (any, error) { //nolint:funlen,gocyclo
8889
return xstring.ToBytes(string(vv)), nil
8990
case jsonDocumentValue:
9091
return xstring.ToBytes(string(vv)), nil
92+
case *decimalValue:
93+
return decimal.ToDecimal(vv), nil
9194
default:
9295
return v, nil
9396
}

internal/value/value.go

Lines changed: 7 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,9 @@ import (
1515
"github.com/google/uuid"
1616
"github.com/ydb-platform/ydb-go-genproto/protos/Ydb"
1717

18-
"github.com/ydb-platform/ydb-go-sdk/v3/internal/decimal"
1918
"github.com/ydb-platform/ydb-go-sdk/v3/internal/types"
2019
"github.com/ydb-platform/ydb-go-sdk/v3/internal/xerrors"
20+
"github.com/ydb-platform/ydb-go-sdk/v3/pkg/decimal"
2121
"github.com/ydb-platform/ydb-go-sdk/v3/pkg/xstring"
2222
)
2323

@@ -581,29 +581,15 @@ func Datetime64ValueFromTime(t time.Time) datetime64Value {
581581
return datetime64Value(t.Unix())
582582
}
583583

584-
var _ DecimalValuer = (*decimalValue)(nil)
584+
var _ decimal.Interface = (*decimalValue)(nil)
585585

586586
type decimalValue struct {
587587
value [16]byte
588588
innerType *types.Decimal
589589
}
590590

591-
func (v *decimalValue) Value() [16]byte {
592-
return v.value
593-
}
594-
595-
func (v *decimalValue) Precision() uint32 {
596-
return v.innerType.Precision()
597-
}
598-
599-
func (v *decimalValue) Scale() uint32 {
600-
return v.innerType.Scale()
601-
}
602-
603-
type DecimalValuer interface {
604-
Value() [16]byte
605-
Precision() uint32
606-
Scale() uint32
591+
func (v *decimalValue) Decimal() (bytes [16]byte, precision uint32, scale uint32) {
592+
return v.value, v.innerType.Precision(), v.innerType.Scale()
607593
}
608594

609595
func (v *decimalValue) castTo(dst any) error {
@@ -613,8 +599,7 @@ func (v *decimalValue) castTo(dst any) error {
613599

614600
return nil
615601
case *decimal.Decimal:
616-
decVal := decimal.Decimal{Bytes: v.value, Precision: v.Precision(), Scale: v.Scale()}
617-
*dstValue = decVal
602+
*dstValue = *decimal.ToDecimal(v)
618603

619604
return nil
620605
default:
@@ -631,7 +616,7 @@ func (v *decimalValue) Yql() string {
631616
buffer.WriteString(v.innerType.Name())
632617
buffer.WriteByte('(')
633618
buffer.WriteByte('"')
634-
s := decimal.FromBytes(v.value[:], v.innerType.Precision(), v.innerType.Scale()).String()
619+
s := decimal.FromBytes(v.value[:], v.innerType.Precision()).String()
635620
if len(s) < int(v.innerType.Scale()) {
636621
s = strings.Repeat("0", int(v.innerType.Scale())-len(s)) + s
637622
}
@@ -665,7 +650,7 @@ func (v *decimalValue) toYDB() *Ydb.Value {
665650
}
666651

667652
func DecimalValueFromBigInt(v *big.Int, precision, scale uint32) *decimalValue {
668-
b := decimal.BigIntToByte(v, precision, scale)
653+
b := decimal.BigIntToByte(v, precision)
669654

670655
return DecimalValue(b, precision, scale)
671656
}

internal/value/value_test.go

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1926,9 +1926,10 @@ func TestDecimalValue(t *testing.T) {
19261926
decBytes := [16]byte{1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16}
19271927
v := DecimalValue(decBytes, 22, 9)
19281928
require.NotNil(t, v)
1929-
require.Equal(t, decBytes, v.Value())
1930-
require.Equal(t, uint32(22), v.Precision())
1931-
require.Equal(t, uint32(9), v.Scale())
1929+
bytes, precision, scale := v.Decimal()
1930+
require.Equal(t, decBytes, bytes)
1931+
require.Equal(t, uint32(22), precision)
1932+
require.Equal(t, uint32(9), scale)
19321933
})
19331934

19341935
t.Run("FromString", func(t *testing.T) {

0 commit comments

Comments
 (0)