Skip to content

Commit 3c8394d

Browse files
committed
amend impl
1 parent dc11454 commit 3c8394d

File tree

3 files changed

+26
-21
lines changed

3 files changed

+26
-21
lines changed

enginetest/queries/script_queries.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -124,9 +124,9 @@ var ScriptTests = []ScriptTest{
124124
{
125125
// https://github.com/dolthub/dolt/issues/9857
126126
Name: "UUID_SHORT() function returns 64-bit unsigned integers with proper construction",
127-
Dialect: "mysql",
127+
Dialect: "mysql",
128128
SetUpScript: []string{},
129-
Assertions: []ScriptTestAssertion{
129+
Assertions: []ScriptTestAssertion{
130130
{
131131
Query: "SELECT UUID_SHORT() > 0",
132132
Expected: []sql.Row{

sql/expression/function/uuid.go

Lines changed: 21 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,8 @@ package function
1717
import (
1818
"fmt"
1919
"sync"
20-
"time"
2120

21+
"github.com/dolthub/go-mysql-server/sql/variables"
2222
"github.com/dolthub/vitess/go/sqltypes"
2323
"github.com/dolthub/vitess/go/vt/proto/query"
2424
"github.com/google/uuid"
@@ -31,7 +31,6 @@ import (
3131
var (
3232
uuidShortMu sync.Mutex
3333
uuidShortCounter uint64
34-
uuidShortStartup = uint64(time.Now().Unix())
3534
)
3635

3736
// UUID()
@@ -563,24 +562,27 @@ func NewUUIDShortFunc() sql.Expression {
563562
return &UUIDShortFunc{}
564563
}
565564

566-
// Description implements sql.FunctionExpression
567-
func (u UUIDShortFunc) Description() string {
565+
// Description returns a human-readable description of the UUID_SHORT function.
566+
func (u *UUIDShortFunc) Description() string {
568567
return "returns a short universal identifier as a 64-bit unsigned integer."
569568
}
570569

571-
func (u UUIDShortFunc) String() string {
570+
// String returns a string representation of the UUID_SHORT function call.
571+
func (u *UUIDShortFunc) String() string {
572572
return "UUID_SHORT()"
573573
}
574574

575-
func (u UUIDShortFunc) Type() sql.Type {
575+
// Type returns the data type of the UUID_SHORT function result (Uint64).
576+
func (u *UUIDShortFunc) Type() sql.Type {
576577
return types.Uint64
577578
}
578579

579580
// CollationCoercibility implements the interface sql.CollationCoercible.
580-
func (UUIDShortFunc) CollationCoercibility(ctx *sql.Context) (collation sql.CollationID, coercibility byte) {
581+
func (u *UUIDShortFunc) CollationCoercibility(ctx *sql.Context) (collation sql.CollationID, coercibility byte) {
581582
return sql.Collation_binary, 5
582583
}
583584

585+
// Eval generates a 64-bit UUID_SHORT value using server_id, startup time, and counter.
584586
func (u *UUIDShortFunc) Eval(ctx *sql.Context, row sql.Row) (interface{}, error) {
585587
uuidShortMu.Lock()
586588
defer uuidShortMu.Unlock()
@@ -595,37 +597,40 @@ func (u *UUIDShortFunc) Eval(ctx *sql.Context, row sql.Row) (interface{}, error)
595597
}
596598

597599
// Construct the UUID_SHORT value according to MySQL specification:
598-
result := ((serverID & 255) << 56) + (uuidShortStartup << 24) + uuidShortCounter
599-
600+
result := ((serverID & 255) << 56) + (uint64(variables.ServerStartUpTime.Unix()) << 24) + uuidShortCounter
600601
return result, nil
601602
}
602603

603-
func (u UUIDShortFunc) WithChildren(children ...sql.Expression) (sql.Expression, error) {
604+
// WithChildren returns a new UUID_SHORT function with the given children (must be empty).
605+
func (u *UUIDShortFunc) WithChildren(children ...sql.Expression) (sql.Expression, error) {
604606
if len(children) != 0 {
605607
return nil, sql.ErrInvalidChildrenNumber.New(u, len(children), 0)
606608
}
607609

608610
return &UUIDShortFunc{}, nil
609611
}
610612

611-
func (u UUIDShortFunc) FunctionName() string {
613+
// FunctionName returns the name of the UUID_SHORT function.
614+
func (u *UUIDShortFunc) FunctionName() string {
612615
return "UUID_SHORT"
613616
}
614617

615-
func (u UUIDShortFunc) Resolved() bool {
618+
// Resolved returns true since UUID_SHORT has no dependencies to resolve.
619+
func (u *UUIDShortFunc) Resolved() bool {
616620
return true
617621
}
618622

619623
// Children returns the children expressions of this expression.
620-
func (u UUIDShortFunc) Children() []sql.Expression {
624+
func (u *UUIDShortFunc) Children() []sql.Expression {
621625
return nil
622626
}
623627

624-
// IsNullable returns whether the expression can be null.
625-
func (u UUIDShortFunc) IsNullable() bool {
628+
// IsNullable returns false since UUID_SHORT always returns a value.
629+
func (u *UUIDShortFunc) IsNullable() bool {
626630
return false
627631
}
628632

629-
func (u UUIDShortFunc) IsNonDeterministic() bool {
633+
// IsNonDeterministic returns true since UUID_SHORT generates different values on each call.
634+
func (u *UUIDShortFunc) IsNonDeterministic() bool {
630635
return true
631636
}

sql/variables/system_variables.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -34,8 +34,8 @@ import (
3434
// There's also this page, which shows that a TON of variables are still missing ):
3535
// https://dev.mysql.com/doc/refman/8.0/en/server-system-variable-reference.html
3636

37-
// serverStartUpTime is needed by uptime status variable
38-
var serverStartUpTime = time.Now()
37+
// ServerStartUpTime is needed by uptime status variable
38+
var ServerStartUpTime = time.Now()
3939

4040
// globalSystemVariables is the underlying type of SystemVariables.
4141
type globalSystemVariables struct {
@@ -2934,7 +2934,7 @@ var systemVars = map[string]sql.SystemVariable{
29342934
Type: types.NewSystemBoolType("updatable_views_with_limit"),
29352935
Default: int8(1),
29362936
ValueFunction: func() (interface{}, error) {
2937-
return int(time.Now().Sub(serverStartUpTime).Seconds()), nil
2937+
return int(time.Now().Sub(ServerStartUpTime).Seconds()), nil
29382938
},
29392939
},
29402940
"use_secondary_engine": &sql.MysqlSystemVariable{

0 commit comments

Comments
 (0)