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

Code before:
func TestUUIDv4(t *testing.T) {
	ctx := createContext(t)
	f, err := newFunctionForTest(ctx, ast.UUIDv4)
	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(4), u.Version(), "Must generate a UUIDv4")
	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 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:

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

Code after:
				case 3:
					require.Equal(t, 4, len(p))
				case 4:
					require.Equal(t, 12, len(p))
				}
			}
			_, err = funcs[tf.funcName].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))
	}
}

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