@@ -10,6 +10,7 @@ import (
1010 "strings"
1111 "testing"
1212
13+ "github.com/google/uuid"
1314 "github.com/stretchr/testify/require"
1415 "github.com/ydb-platform/ydb-go-genproto/protos/Ydb"
1516
@@ -113,3 +114,148 @@ func TestRegressionIssue1227RetryBadSession(t *testing.T) {
113114 require .NoError (t , err )
114115 require .EqualValues (t , 100 , cnt )
115116}
117+
118+ func TestUUIDSerializationTableServiceServiceIssue1501 (t * testing.T ) {
119+ // https://github.com/ydb-platform/ydb-go-sdk/issues/1501
120+ // test with special uuid - all bytes are different for check any byte swaps
121+
122+ t .Run ("old-send" , func (t * testing.T ) {
123+ // test old behavior - for test way of safe work with data, written with bagged API version
124+ var (
125+ scope = newScope (t )
126+ ctx = scope .Ctx
127+ db = scope .Driver ()
128+ )
129+
130+ idString := "6E73B41C-4EDE-4D08-9CFB-B7462D9E498B"
131+ expectedResultWithBug := "2d9e498b-b746-9cfb-084d-de4e1cb4736e"
132+ id := uuid .MustParse (idString )
133+
134+ var idFromDB string
135+ err := db .Table ().DoTx (ctx , func (ctx context.Context , tx table.TransactionActor ) error {
136+ res , err := tx .Execute (ctx , `
137+ DECLARE $val AS UUID;
138+
139+ SELECT CAST($val AS Utf8)
140+ ` , table .NewQueryParameters (table .ValueParam ("$val" , types .UUIDValue (id ))))
141+ if err != nil {
142+ return err
143+ }
144+ res .NextResultSet (ctx )
145+ res .NextRow ()
146+
147+ err = res .Scan (& idFromDB )
148+ return err
149+ })
150+ require .NoError (t , err )
151+ require .Equal (t , expectedResultWithBug , idFromDB )
152+ })
153+ t .Run ("old-receive-to-bytes" , func (t * testing.T ) {
154+ // test old behavior - for test way of safe work with data, written with bagged API version
155+ var (
156+ scope = newScope (t )
157+ ctx = scope .Ctx
158+ db = scope .Driver ()
159+ )
160+
161+ idString := "6E73B41C-4EDE-4D08-9CFB-B7462D9E498B"
162+ expectedResultWithBug := "8b499e2d-46b7-fb9c-4d08-4ede6e73b41c"
163+ var resultFromDb uuid.UUID
164+ err := db .Table ().DoTx (ctx , func (ctx context.Context , tx table.TransactionActor ) error {
165+ res , err := tx .Execute (ctx , `
166+ DECLARE $val AS Text;
167+
168+ SELECT CAST($val AS UUID)
169+ ` , table .NewQueryParameters (table .ValueParam ("$val" , types .TextValue (idString ))))
170+ if err != nil {
171+ return err
172+ }
173+
174+ res .NextResultSet (ctx )
175+ res .NextRow ()
176+
177+ var resBytes [16 ]byte
178+ err = res .Scan (& resBytes )
179+ if err != nil {
180+ return err
181+ }
182+
183+ resultFromDb = resBytes
184+ return nil
185+ })
186+
187+ require .NoError (t , err )
188+ require .Equal (t , expectedResultWithBug , resultFromDb .String ())
189+ })
190+ t .Run ("old-receive-to-string" , func (t * testing.T ) {
191+ // test old behavior - for test way of safe work with data, written with bagged API version
192+ var (
193+ scope = newScope (t )
194+ ctx = scope .Ctx
195+ db = scope .Driver ()
196+ )
197+
198+ idString := "6E73B41C-4EDE-4D08-9CFB-B7462D9E498B"
199+ expectedResultWithBug := []byte {0x8b , 0x49 , 0x9e , 0x2d , 0x46 , 0xb7 , 0xfb , 0x9c , 0x4d , 0x8 , 0x4e , 0xde , 0x6e , 0x73 , 0xb4 , 0x1c }
200+ var resultFromDb string
201+ err := db .Table ().DoTx (ctx , func (ctx context.Context , tx table.TransactionActor ) error {
202+ res , err := tx .Execute (ctx , `
203+ DECLARE $val AS Text;
204+
205+ SELECT CAST($val AS UUID)
206+ ` , table .NewQueryParameters (table .ValueParam ("$val" , types .TextValue (idString ))))
207+ if err != nil {
208+ return err
209+ }
210+
211+ res .NextResultSet (ctx )
212+ res .NextRow ()
213+
214+ err = res .ScanWithDefaults (& resultFromDb )
215+ if err != nil {
216+ return err
217+ }
218+
219+ return nil
220+ })
221+
222+ require .NoError (t , err )
223+ resultBytes := []byte (resultFromDb )
224+ require .Equal (t , expectedResultWithBug , resultBytes )
225+ })
226+ t .Run ("old-send-receive" , func (t * testing.T ) {
227+ // test old behavior - for test way of safe work with data, written with bagged API version
228+ var (
229+ scope = newScope (t )
230+ ctx = scope .Ctx
231+ db = scope .Driver ()
232+ )
233+
234+ idString := "6E73B41C-4EDE-4D08-9CFB-B7462D9E498B"
235+ id := uuid .MustParse (idString )
236+
237+ var idFromDB uuid.UUID
238+ err := db .Table ().DoTx (ctx , func (ctx context.Context , tx table.TransactionActor ) error {
239+ res , err := tx .Execute (ctx , `
240+ DECLARE $val AS UUID;
241+
242+ SELECT $val
243+ ` , table .NewQueryParameters (table .ValueParam ("$val" , types .UUIDValue (id ))))
244+ if err != nil {
245+ return err
246+ }
247+ res .NextResultSet (ctx )
248+ res .NextRow ()
249+
250+ var resBytes [16 ]byte
251+ err = res .Scan (& resBytes )
252+ if err != nil {
253+ return err
254+ }
255+ idFromDB = resBytes
256+ return nil
257+ })
258+ require .NoError (t , err )
259+ require .Equal (t , id , idFromDB )
260+ })
261+ }
0 commit comments