Skip to content

Commit 2c786e7

Browse files
committed
fix tests
1 parent f275a6a commit 2c786e7

File tree

4 files changed

+128
-47
lines changed

4 files changed

+128
-47
lines changed

enginetest/memory_engine_test.go

Lines changed: 5 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -203,44 +203,19 @@ func TestSingleScript(t *testing.T) {
203203
t.Skip()
204204
var scripts = []queries.ScriptTest{
205205
{
206-
// https://github.com/dolthub/dolt/issues/9857
207-
Name: "UUID_SHORT() function returns 64-bit unsigned integers with proper construction",
206+
Name: "AS OF propagates to nested CALLs",
208207
SetUpScript: []string{},
209208
Assertions: []queries.ScriptTestAssertion{
210209
{
211-
Query: "SELECT UUID_SHORT() > 0",
210+
Query: "create procedure create_proc() create table t (i int primary key, j int);",
212211
Expected: []sql.Row{
213-
{true}, // Should return positive values
212+
{types.NewOkResult(0)},
214213
},
215214
},
216215
{
217-
Query: "SELECT UUID_SHORT() != UUID_SHORT()",
216+
Query: "call create_proc()",
218217
Expected: []sql.Row{
219-
{true}, // Should return different values on each call
220-
},
221-
},
222-
{
223-
Query: "SELECT UUID_SHORT() + 0 > 0",
224-
Expected: []sql.Row{
225-
{true}, // Should work in arithmetic expressions
226-
},
227-
},
228-
{
229-
Query: "SELECT CAST(UUID_SHORT() AS CHAR) != ''",
230-
Expected: []sql.Row{
231-
{true}, // Should cast to non-empty string
232-
},
233-
},
234-
{
235-
Query: "SELECT UUID_SHORT() BETWEEN 1 AND 18446744073709551615",
236-
Expected: []sql.Row{
237-
{true}, // Should be within uint64 range
238-
},
239-
},
240-
{
241-
Query: "SELECT (UUID_SHORT() & 0xFF00000000000000) >> 56 BETWEEN 0 AND 255",
242-
Expected: []sql.Row{
243-
{true}, // Server ID should be 0-255
218+
{types.NewOkResult(0)},
244219
},
245220
},
246221
},

enginetest/queries/script_queries.go

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -121,6 +121,61 @@ type ScriptTestAssertion struct {
121121
// Unlike other engine tests, ScriptTests must be self-contained. No other tables are created outside the definition of
122122
// the tests.
123123
var ScriptTests = []ScriptTest{
124+
{
125+
// https://github.com/dolthub/dolt/issues/9857
126+
Name: "UUID_SHORT() function returns 64-bit unsigned integers with proper construction",
127+
SetUpScript: []string{},
128+
Assertions: []ScriptTestAssertion{
129+
{
130+
Query: "SELECT UUID_SHORT() > 0",
131+
Expected: []sql.Row{
132+
{true}, // Should return positive values
133+
},
134+
},
135+
{
136+
Query: "SELECT UUID_SHORT() != UUID_SHORT()",
137+
Expected: []sql.Row{
138+
{true}, // Should return different values on each call
139+
},
140+
},
141+
{
142+
Query: "SELECT UUID_SHORT() + 0 > 0",
143+
Expected: []sql.Row{
144+
{true}, // Should work in arithmetic expressions
145+
},
146+
},
147+
{
148+
Query: "SELECT CAST(UUID_SHORT() AS CHAR) != ''",
149+
Expected: []sql.Row{
150+
{true}, // Should cast to non-empty string
151+
},
152+
},
153+
{
154+
Query: "SELECT UUID_SHORT() BETWEEN 1 AND 18446744073709551615",
155+
Expected: []sql.Row{
156+
{true}, // Should be within uint64 range
157+
},
158+
},
159+
{
160+
Query: "SELECT (UUID_SHORT() & 0xFF00000000000000) >> 56 BETWEEN 0 AND 255",
161+
Expected: []sql.Row{
162+
{true}, // Server ID should be 0-255
163+
},
164+
},
165+
{
166+
Query: "SET @@global.server_id = 253",
167+
Expected: []sql.Row{
168+
{types.NewOkResult(0)},
169+
},
170+
},
171+
{
172+
Query: "SELECT (UUID_SHORT() & 0xFF00000000000000) >> 56 BETWEEN 0 AND 255",
173+
Expected: []sql.Row{
174+
{true}, // server time won't let us pin this down further
175+
},
176+
},
177+
},
178+
},
124179
{
125180
// https://github.com/dolthub/go-mysql-server/issues/3216
126181
Name: "UNION ALL with BLOB columns",

sql/expression/function/uuid.go

Lines changed: 14 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -29,10 +29,9 @@ import (
2929

3030
// Global state for UUID_SHORT function
3131
var (
32-
uuidShortMu sync.Mutex
33-
uuidShortCounter uint64
34-
uuidShortStartup = uint64(time.Now().Unix())
35-
uuidShortServerID = uint64(1) // Default server ID
32+
uuidShortMu sync.Mutex
33+
uuidShortCounter uint64
34+
uuidShortStartup = uint64(time.Now().Unix())
3635
)
3736

3837
// UUID()
@@ -546,8 +545,6 @@ func (bu BinToUUID) IsNullable() bool {
546545
// of UUID_SHORT() is guaranteed to be unique if the following conditions hold:
547546
//
548547
// The server_id value of the current server is between 0 and 255 and is unique among your set of source and replica servers
549-
// You do not set back the system time for your server host between mysqld restarts
550-
// You invoke UUID_SHORT() on average fewer than 16 million times per second between mysqld restarts
551548
//
552549
// The UUID_SHORT() return value is constructed this way:
553550
// (server_id & 255) << 56
@@ -587,13 +584,19 @@ func (UUIDShortFunc) CollationCoercibility(ctx *sql.Context) (collation sql.Coll
587584
func (u *UUIDShortFunc) Eval(ctx *sql.Context, row sql.Row) (interface{}, error) {
588585
uuidShortMu.Lock()
589586
defer uuidShortMu.Unlock()
590-
587+
591588
uuidShortCounter++
592-
589+
590+
serverID := uint64(1) // Default fallback
591+
if _, val, ok := sql.SystemVariables.GetGlobal("server_id"); ok {
592+
if serverIDVal, ok := val.(uint32); ok {
593+
serverID = uint64(serverIDVal)
594+
}
595+
}
596+
593597
// Construct the UUID_SHORT value according to MySQL specification:
594-
// (server_id & 255) << 56 + (server_startup_time_in_seconds << 24) + incremented_variable
595-
result := ((uuidShortServerID & 255) << 56) + (uuidShortStartup << 24) + uuidShortCounter
596-
598+
result := ((serverID & 255) << 56) + (uuidShortStartup << 24) + uuidShortCounter
599+
597600
return result, nil
598601
}
599602

sql/expression/function/uuid_test.go

Lines changed: 54 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -245,8 +245,7 @@ func TestUUIDShort(t *testing.T) {
245245

246246
func TestUUIDShortMultipleInstances(t *testing.T) {
247247
ctx := sql.NewEmptyContext()
248-
249-
// Create multiple instances to test that they share the global counter (like MySQL)
248+
250249
uuidShort1 := NewUUIDShortFunc()
251250
uuidShort2 := NewUUIDShortFunc()
252251

@@ -261,20 +260,18 @@ func TestUUIDShortMultipleInstances(t *testing.T) {
261260
require.IsType(t, uint64(0), result2)
262261
require.Greater(t, result1.(uint64), uint64(0))
263262
require.Greater(t, result2.(uint64), uint64(0))
264-
263+
265264
// Values should be sequential (global counter)
266265
require.Equal(t, result1.(uint64)+1, result2.(uint64))
267266
}
268267

269268
func TestUUIDShortWithChildren(t *testing.T) {
270269
uuidShortE := NewUUIDShortFunc()
271270

272-
// Test WithChildren with no arguments (should work)
273271
newExpr, err := uuidShortE.WithChildren()
274272
require.NoError(t, err)
275273
require.NotNil(t, newExpr)
276274

277-
// Test WithChildren with arguments (should fail)
278275
_, err = uuidShortE.WithChildren(expression.NewLiteral(1, types.Int64))
279276
require.Error(t, err)
280277
require.Contains(t, err.Error(), "invalid children number")
@@ -283,7 +280,6 @@ func TestUUIDShortWithChildren(t *testing.T) {
283280
func TestUUIDShortProperties(t *testing.T) {
284281
uuidShortE := NewUUIDShortFunc().(*UUIDShortFunc)
285282

286-
// Test function properties
287283
require.Equal(t, "UUID_SHORT", uuidShortE.FunctionName())
288284
require.Equal(t, "returns a short universal identifier as a 64-bit unsigned integer.", uuidShortE.Description())
289285
require.Equal(t, "UUID_SHORT()", uuidShortE.String())
@@ -293,3 +289,55 @@ func TestUUIDShortProperties(t *testing.T) {
293289
require.True(t, uuidShortE.IsNonDeterministic())
294290
require.Nil(t, uuidShortE.Children())
295291
}
292+
293+
func TestUUIDShortServerIdIntegration(t *testing.T) {
294+
ctx := sql.NewEmptyContext()
295+
uuidShortE := NewUUIDShortFunc()
296+
297+
result1, err := uuidShortE.Eval(ctx, sql.Row{nil})
298+
require.NoError(t, err)
299+
require.IsType(t, uint64(0), result1)
300+
301+
serverIDFromResult := (result1.(uint64) & 0xFF00000000000000) >> 56
302+
require.Equal(t, uint64(1), serverIDFromResult)
303+
304+
err = sql.SystemVariables.SetGlobal(ctx, "server_id", uint32(123))
305+
require.NoError(t, err)
306+
307+
result2, err := uuidShortE.Eval(ctx, sql.Row{nil})
308+
require.NoError(t, err)
309+
require.IsType(t, uint64(0), result2)
310+
311+
serverIDFromResult2 := (result2.(uint64) & 0xFF00000000000000) >> 56
312+
require.Equal(t, uint64(123), serverIDFromResult2)
313+
314+
err = sql.SystemVariables.SetGlobal(ctx, "server_id", uint32(255))
315+
require.NoError(t, err)
316+
317+
result3, err := uuidShortE.Eval(ctx, sql.Row{nil})
318+
require.NoError(t, err)
319+
require.IsType(t, uint64(0), result3)
320+
321+
serverIDFromResult3 := (result3.(uint64) & 0xFF00000000000000) >> 56
322+
require.Equal(t, uint64(255), serverIDFromResult3)
323+
324+
err = sql.SystemVariables.SetGlobal(ctx, "server_id", uint32(256))
325+
require.NoError(t, err)
326+
327+
result4, err := uuidShortE.Eval(ctx, sql.Row{nil})
328+
require.NoError(t, err)
329+
require.IsType(t, uint64(0), result4)
330+
331+
serverIDFromResult4 := (result4.(uint64) & 0xFF00000000000000) >> 56
332+
require.Equal(t, uint64(0), serverIDFromResult4)
333+
334+
err = sql.SystemVariables.SetGlobal(ctx, "server_id", uint32(243))
335+
require.NoError(t, err)
336+
337+
result5, err := uuidShortE.Eval(ctx, sql.Row{nil})
338+
require.NoError(t, err)
339+
require.IsType(t, uint64(0), result5)
340+
341+
serverIDFromResult5 := (result5.(uint64) & 0xFF00000000000000) >> 56
342+
require.Equal(t, uint64(243), serverIDFromResult5)
343+
}

0 commit comments

Comments
 (0)