Repository: pingcap/tidb
File: pkg/expression/builtin_miscellaneous_test.go (Go)

Code before:
func TestUUIDv7(t *testing.T) {
	ctx := createContext(t)
	f, err := newFunctionForTest(ctx, ast.UUIDv7)
	require.NoError(t, err)
	d, err := f.Eval(ctx, chunk.Row{})
	require.NoError(t, err)
	u, err := uuid.Parse(d.GetString())
	require.NoError(t, err)
	require.Equal(t, uuid.Version(7), u.Version(), "Must generate a UUIDv7")
	parts := strings.Split(d.GetString(), "-")
	require.Equal(t, 5, len(parts))
	for i, p := range parts {
		switch i {
		case 0:
			require.Equal(t, 8, len(p))
		case 1:
			require.Equal(t, 4, len(p))
		case 2:
			require.Equal(t, 4, len(p))
		case 3:
			require.Equal(t, 4, len(p))
		case 4:
			require.Equal(t, 12, len(p))
		}
	}
	_, err = funcs[ast.UUID].getFunction(ctx, datumsToConstants(nil))
	require.NoError(t, err)
}

func TestUUIDVersion(t *testing.T) {
	ctx := createContext(t)
	tbl := []struct {
		arg string
		ret int
	}{
		{"5f13f854-d74a-11f0-9b7a-0ae0156bd76b", 1},
		{"c6437ef1-5b86-3a4e-a071-c2d4ad414e65", 3},
		{"a3e3b4a1-ea6d-471e-9860-8303a8b261f6", 4},
		{"271a8175-dadd-5df9-b0bd-20a4a0b441e6", 5},
		{"1f0e48c1-7860-69cc-9b3f-35f89c103d4d", 6},
		{"019b1440-87b7-7380-ab00-ce413e795004", 7},
	}
	for _, tt := range tbl {
		fc := funcs[ast.UUIDVersion]
		f, err := fc.getFunction(ctx, datumsToConstants(types.MakeDatums(tt.arg)))
		require.NoError(t, err)
		r, err := evalBuiltinFunc(f, ctx, chunk.Row{})
		require.NoError(t, err)
		testutil.DatumEqual(t, types.NewDatum(tt.ret), r,
			fmt.Sprintf("UUID_VERSION('%s') = %d (got %v)", tt.arg, tt.ret, r))
	}

Review comment (suggestion):
```suggestion
	_, err = funcs[ast.UUIDv7].getFunction(ctx, datumsToConstants(nil))
```

Code after:
		r, err := evalBuiltinFunc(f, ctx, chunk.Row{})
		require.NoError(t, err)
		testutil.DatumEqual(t, types.NewDatum(tt.ret), r,
			fmt.Sprintf("UUID_VERSION('%s') = %d (got %v)", tt.arg, tt.ret, r))
	}
}

func TestUUIDTimestamp(t *testing.T) {
	ctx := createContext(t)
	tbl := []struct {
		arg  string
		ret  float64
		null bool
	}{
		{"5f13f854-d74a-11f0-9b7a-0ae0156bd76b", 1765537487.118139, false}, // v1
		{"c6437ef1-5b86-3a4e-a071-c2d4ad414e65", 0, true},                  // v3
		{"a3e3b4a1-ea6d-471e-9860-8303a8b261f6", 0, true},                  // v4
		{"271a8175-dadd-5df9-b0bd-20a4a0b441e6", 0, true},                  // v5
		{"1f0e48c1-7860-69cc-9b3f-35f89c103d4d", 1766995078.970004, false}, // v6
		{"019b1440-87b7-7380-ab00-ce413e795004", 1765571332.023000, false}, // v7
		{"00000000-0000-0000-0000-000000000000", 0, true},                  // Nil UUID
		{"ffffffff-ffff-ffff-ffff-ffffffffffff", 0, true},                  // Max UUID
	}
	for _, tt := range tbl {
		fc := funcs[ast.UUIDTimestamp]
		f, err := fc.getFunction(ctx, datumsToConstants(types.MakeDatums(tt.arg)))
		require.NoError(t, err)
		r, err := evalBuiltinFunc(f, ctx, chunk.Row{})
		require.NoError(t, err)
		if tt.null {
			require.True(t, r.IsNull())
		} else {
			testutil.DatumEqual(t, types.NewDatum(types.NewDecFromFloatForTest(tt.ret)), r,
				fmt.Sprintf("UUID_TIMESTAMP('%s') = %v (got %v)", tt.arg, tt.ret, r))
		}
	}
}

func TestAnyValue(t *testing.T) {
	ctx := createContext(t)
	tbl := []struct {
		arg any
		ret any
	}{
		{nil, nil},
		{1234, 1234},
		{-0x99, -0x99},
		{3.1415926, 3.1415926},
		{"Hello, World", "Hello, World"},
	}
	for _, tt := range tbl {