@@ -12,6 +12,7 @@ import (
1212 "testing"
1313 "time"
1414
15+ "github.com/google/uuid"
1516 "github.com/stretchr/testify/require"
1617 "github.com/ydb-platform/ydb-go-genproto/protos/Ydb"
1718
@@ -167,3 +168,108 @@ func TestRegressionKikimr17104(t *testing.T) {
167168 })
168169 })
169170}
171+
172+ func TestUUIDSerializationDatabaseSQLIssue1501 (t * testing.T ) {
173+ // https://github.com/ydb-platform/ydb-go-sdk/issues/1501
174+ // test with special uuid - all bytes are different for check any byte swaps
175+
176+ t .Run ("old-send" , func (t * testing.T ) {
177+ // test old behavior - for test way of safe work with data, written with bagged API version
178+ var (
179+ scope = newScope (t )
180+ db = scope .SQLDriver ()
181+ )
182+
183+ idString := "6E73B41C-4EDE-4D08-9CFB-B7462D9E498B"
184+ expectedResultWithBug := "2d9e498b-b746-9cfb-084d-de4e1cb4736e"
185+ id := [16 ]byte (uuid .MustParse (idString ))
186+ row := db .QueryRow (`
187+ DECLARE $val AS UUID;
188+
189+ SELECT CAST($val AS Utf8)` , sql .Named ("val" , id ),
190+ )
191+
192+ require .NoError (t , row .Err ())
193+
194+ var res string
195+
196+ err := row .Scan (& res )
197+ require .NoError (t , err )
198+ require .Equal (t , expectedResultWithBug , res )
199+ })
200+ t .Run ("old-receive-to-bytes" , func (t * testing.T ) {
201+ // test old behavior - for test way of safe work with data, written with bagged API version
202+ var (
203+ scope = newScope (t )
204+ db = scope .SQLDriver ()
205+ )
206+
207+ idString := "6E73B41C-4EDE-4D08-9CFB-B7462D9E498B"
208+ expectedResultWithBug := "8b499e2d-46b7-fb9c-4d08-4ede6e73b41c"
209+ row := db .QueryRow (`
210+ DECLARE $val AS Text;
211+
212+ SELECT CAST($val AS UUID)` ,
213+ sql .Named ("val" , idString ),
214+ )
215+
216+ require .NoError (t , row .Err ())
217+
218+ var res [16 ]byte
219+
220+ err := row .Scan (& res )
221+ require .NoError (t , err )
222+
223+ resUUID := uuid .UUID (res )
224+ require .Equal (t , expectedResultWithBug , resUUID .String ())
225+ })
226+ t .Run ("old-receive-to-string" , 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+ db = scope .SQLDriver ()
231+ )
232+
233+ idString := "6E73B41C-4EDE-4D08-9CFB-B7462D9E498B"
234+ row := db .QueryRow (`
235+ DECLARE $val AS Text;
236+
237+ SELECT CAST($val AS UUID)` ,
238+ sql .Named ("val" , idString ),
239+ )
240+
241+ require .NoError (t , row .Err ())
242+
243+ var res string
244+
245+ err := row .Scan (& res )
246+ require .Error (t , err )
247+ })
248+ t .Run ("old-send-receive" , func (t * testing.T ) {
249+ // test old behavior - for test way of safe work with data, written with bagged API version
250+ var (
251+ scope = newScope (t )
252+ db = scope .SQLDriver ()
253+ )
254+
255+ idString := "6E73B41C-4EDE-4D08-9CFB-B7462D9E498B"
256+ id := uuid .MustParse (idString )
257+ idParam := [16 ]byte (id )
258+ row := db .QueryRow (`
259+ DECLARE $val AS UUID;
260+
261+ SELECT $val` ,
262+ sql .Named ("val" , idParam ),
263+ )
264+
265+ require .NoError (t , row .Err ())
266+
267+ var resBytes [16 ]byte
268+ err := row .Scan (& resBytes )
269+ require .NoError (t , err )
270+
271+ resUUID := uuid .UUID (resBytes )
272+
273+ require .Equal (t , id , resUUID )
274+ })
275+ }
0 commit comments