Skip to content

Commit d98b602

Browse files
committed
fix scanner uuid
1 parent c286a45 commit d98b602

File tree

5 files changed

+32
-30
lines changed

5 files changed

+32
-30
lines changed

internal/params/builder_test.go

Lines changed: 0 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -345,22 +345,6 @@ func TestBuilder(t *testing.T) {
345345
},
346346
},
347347
},
348-
{
349-
method: "UUID",
350-
args: []any{[...]byte{1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16}},
351-
352-
expected: expected{
353-
Type: &Ydb.Type{
354-
Type: &Ydb.Type_TypeId{TypeId: Ydb.Type_UUID},
355-
},
356-
Value: &Ydb.Value{
357-
Value: &Ydb.Value_Low_128{
358-
Low_128: 651345242494996240,
359-
},
360-
High_128: 72623859790382856,
361-
},
362-
},
363-
},
364348
{
365349
method: "TzDatetime",
366350
args: []any{time.Unix(123456789, 456).UTC()},

internal/table/scanner/scanner.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1083,6 +1083,13 @@ func (s *valueScanner) scanOptional(v interface{}, defaultValueForOptional bool)
10831083
val := value.NewUUIDIssue1501FixedBytesWrapper(src)
10841084
*v = &val
10851085
}
1086+
case **uuid.UUID:
1087+
if s.isNull() {
1088+
*v = nil
1089+
} else {
1090+
src := s.uuid()
1091+
*v = &src
1092+
}
10861093
case **interface{}:
10871094
if s.isNull() {
10881095
*v = nil

internal/table/scanner/scanner_data_test.go

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import (
55
"strconv"
66
"time"
77

8+
"github.com/google/uuid"
89
"github.com/ydb-platform/ydb-go-genproto/protos/Ydb"
910

1011
"github.com/ydb-platform/ydb-go-sdk/v3/table/result/indexed"
@@ -63,7 +64,7 @@ var scannerData = []struct {
6364
name: "date",
6465
typeID: Ydb.Type_DATE,
6566
}},
66-
values: []indexed.RequiredOrOptional{new([16]byte), new(time.Time)},
67+
values: []indexed.RequiredOrOptional{new(uuid.UUID), new(time.Time)},
6768
},
6869
{
6970
name: "Scan JSON, DOUBLE",
@@ -272,7 +273,7 @@ var scannerData = []struct {
272273
typeID: Ydb.Type_DOUBLE,
273274
optional: true,
274275
}},
275-
values: []indexed.RequiredOrOptional{new(*time.Duration), new(*[16]byte), new(*float64)},
276+
values: []indexed.RequiredOrOptional{new(*time.Duration), new(*uuid.UUID), new(*float64)},
276277
},
277278
{
278279
name: "Scan int64, date, string as ydb.Scanner",
@@ -424,7 +425,7 @@ var scannerData = []struct {
424425
optional: true,
425426
nilValue: true,
426427
}},
427-
values: []indexed.RequiredOrOptional{new(*uint8), new(*[]byte), new(*time.Time), new(*[16]byte)},
428+
values: []indexed.RequiredOrOptional{new(*uint8), new(*[]byte), new(*time.Time), new(*uuid.UUID)},
428429
},
429430
{
430431
name: "Scan string as byte array.",

internal/table/scanner/scanner_test.go

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import (
88
"testing"
99
"time"
1010

11+
"github.com/google/uuid"
1112
"github.com/stretchr/testify/require"
1213
"github.com/ydb-platform/ydb-go-genproto/protos/Ydb"
1314

@@ -454,21 +455,23 @@ func valueFromPrimitiveTypeID(c *column, r xrand.Rand) (*Ydb.Value, interface{})
454455
Value: &Ydb.Value_NullFlagValue{},
455456
}
456457
if c.testDefault {
457-
var dv [16]byte
458+
var dv uuid.UUID
458459

459460
return ydbval, &dv
460461
}
461-
var dv *[16]byte
462+
var dv *uuid.UUID
462463

463464
return ydbval, &dv
464465
}
465-
v := [16]byte{}
466-
binary.BigEndian.PutUint64(v[0:8], uint64(rv))
467-
binary.BigEndian.PutUint64(v[8:16], uint64(rv))
466+
v := uuid.UUID{}
467+
468+
binary.LittleEndian.PutUint64(v[0:8], uint64(rv))
469+
binary.LittleEndian.PutUint64(v[8:16], uint64(rv))
470+
low, high := value.UUIDToHiLoPair(v)
468471
ydbval := &Ydb.Value{
469-
High_128: binary.BigEndian.Uint64(v[0:8]),
472+
High_128: high,
470473
Value: &Ydb.Value_Low_128{
471-
Low_128: binary.BigEndian.Uint64(v[8:16]),
474+
Low_128: low,
472475
},
473476
}
474477
if c.optional && !c.testDefault {

internal/value/value.go

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2173,20 +2173,27 @@ func (v *uuidValue) toYDB(a *allocator.Allocator) *Ydb.Value {
21732173
return v.toYDBWithBug(a)
21742174
}
21752175

2176-
var bytes [16]byte
2176+
var low, high uint64
21772177
if v != nil {
2178-
bytes = uuidDirectBytesToLe(v.value)
2178+
low, high = UUIDToHiLoPair(v.value)
21792179
}
21802180
vv := a.Low128()
2181-
vv.Low_128 = binary.LittleEndian.Uint64(bytes[0:8])
2181+
vv.Low_128 = low
21822182

21832183
vvv := a.Value()
2184-
vvv.High_128 = binary.LittleEndian.Uint64(bytes[8:16])
2184+
vvv.High_128 = high
21852185
vvv.Value = vv
21862186

21872187
return vvv
21882188
}
21892189

2190+
func UUIDToHiLoPair(id uuid.UUID) (low, high uint64) {
2191+
bytes := uuidDirectBytesToLe(id)
2192+
low = binary.LittleEndian.Uint64(bytes[0:8])
2193+
high = binary.LittleEndian.Uint64(bytes[8:16])
2194+
return low, high
2195+
}
2196+
21902197
func (v *uuidValue) toYDBWithBug(a *allocator.Allocator) *Ydb.Value {
21912198
var bytes [16]byte
21922199
if v != nil {

0 commit comments

Comments
 (0)