value_test.go 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  1. package migrate
  2. import (
  3. "TsDB2/DB/core/types"
  4. "math"
  5. "testing"
  6. )
  7. // TestRoundFloatToIntBoundary 钉住上界的写法。
  8. //
  9. // float64(math.MaxInt64) 存不下 2^63-1, 会被舍入成 2^63 —— 于是 "f > MaxInt64"
  10. // 这个看着像校验的守卫, 恰好放过了唯一越界的那一个值, 而 int64(2^63) 是越界转换
  11. // (结果未定义, amd64 上回绕成 MinInt64): 一个超范围的值静默变成反数存进去。
  12. // 下界不受影响: MinInt64 = -2^63 在 float64 里可以精确表示。
  13. func TestRoundFloatToIntBoundary(t *testing.T) {
  14. if got, err := roundFloatToInt(0); err != nil || got != 0 {
  15. t.Errorf("0 应当原样通过, 实际 (%d, %v)", got, err)
  16. }
  17. if _, err := roundFloatToInt(float64(math.MaxInt32)); err != nil {
  18. t.Errorf("MaxInt32 远在范围内, 不该报错: %v", err)
  19. }
  20. // 下界是可以精确表示的那个值, 不该被误伤
  21. if got, err := roundFloatToInt(math.MinInt64); err != nil || got != math.MinInt64 {
  22. t.Errorf("MinInt64 应当原样通过, 实际 (%d, %v)", got, err)
  23. }
  24. // 注意别写 math.MinInt64 - 1 当越界样例: 那个数学值在 float64 里根本没有对应的
  25. // 格点, 会被舍入回 -2^63 这个合法值。上界那边正好相反 —— MaxInt64 舍入到 2^63,
  26. // 恰好越过边界, 所以 >= 才是对的。
  27. for _, f := range []float64{
  28. math.MaxInt64, // 2^63: 越界, 但 float64(MaxInt64) 也正好等于它
  29. math.MaxInt64 * 2,
  30. math.Pow(2, 63) * 4,
  31. math.Inf(1),
  32. math.Inf(-1),
  33. math.NaN(),
  34. } {
  35. if got, err := roundFloatToInt(f); err == nil {
  36. t.Errorf("%v 超出 int64, 应当报错, 实际返回 %d", f, got)
  37. }
  38. }
  39. }
  40. // TestCoerceForSQL8ByteByDialect uint64 绑定的 Go 类型必须跟目标列的【声明】一致,
  41. // 而两个方言的声明本就不同 (见 sqlTypeName):
  42. //
  43. // - MySQL 声明 BIGINT UNSIGNED → 绑 uint64。以前一律绑 int64(u), 高位为 1 的值
  44. // (包括 VRTX 从 int64 负值搬过来的位模式) 在严格模式下会被判成
  45. // "Out of range value" 直接报错, 非严格模式则被静默改写。
  46. // - SQLite 只有有符号的 INTEGER → 绑 int64, 位模式原样存。
  47. //
  48. // 共同的保证是【位模式不变】; 两种绑定在各自的列里都必须无损。
  49. func TestCoerceForSQL8ByteByDialect(t *testing.T) {
  50. col := Column{Name: "u", Type: types.FieldType_Uint64}
  51. cases := []struct {
  52. name string
  53. in any
  54. wantMySQL any
  55. wantSQLite any
  56. }{
  57. {"uint64 上界", uint64(math.MaxUint64), uint64(math.MaxUint64), int64(-1)},
  58. {"int64 负数 (位模式)", int64(-1), uint64(math.MaxUint64), int64(-1)},
  59. {"普通值", int64(42), uint64(42), int64(42)},
  60. {"2^63 (无符号档才装得下)", uint64(math.MaxInt64) + 1, uint64(math.MaxInt64) + 1, int64(math.MinInt64)},
  61. }
  62. for _, c := range cases {
  63. gotMySQL, err := coerceForSQL(col, c.in, true)
  64. if err != nil {
  65. t.Errorf("%s: MySQL 侧不该报错: %v", c.name, err)
  66. } else if gotMySQL != c.wantMySQL {
  67. t.Errorf("%s: MySQL 侧 = %v (%T), 期望 %v (%T)",
  68. c.name, gotMySQL, gotMySQL, c.wantMySQL, c.wantMySQL)
  69. }
  70. gotSQLite, err := coerceForSQL(col, c.in, false)
  71. if err != nil {
  72. t.Errorf("%s: SQLite 侧不该报错: %v", c.name, err)
  73. } else if gotSQLite != c.wantSQLite {
  74. t.Errorf("%s: SQLite 侧 = %v (%T), 期望 %v (%T)",
  75. c.name, gotSQLite, gotSQLite, c.wantSQLite, c.wantSQLite)
  76. }
  77. }
  78. // 时间列走的还是"还原成时间文本"那条路, 与方言无关 —— 别被上面的方言分支带走。
  79. timeCol := Column{Name: "t", Type: types.FieldType_Uint64, TimeKind: "datetime"}
  80. for _, mysql := range []bool{true, false} {
  81. v, err := coerceForSQL(timeCol, int64(0), mysql)
  82. if err != nil {
  83. t.Fatalf("时间列 (mysql=%v) 不该报错: %v", mysql, err)
  84. }
  85. if _, ok := v.(string); !ok {
  86. t.Errorf("时间列 (mysql=%v) 应当还原成时间文本, 实际 %T", mysql, v)
  87. }
  88. }
  89. }
  90. // TestCoerceForSQLUnsignedSmall 2 / 4 字节无符号档出库时必须给【无符号数值】。
  91. //
  92. // 与 8 字节档同一个道理 (见 TestCoerceForSQL8ByteByDialect), 但 16/32 位不需要按方言分岔:
  93. // 目标列在 MySQL 侧声明成 SMALLINT UNSIGNED / INT UNSIGNED, SQLite 侧是 INTEGER ——
  94. // 0..65535 与 0..4294967295 两边都整个装得下, 给无符号值两个方言都对。
  95. // 反过来把位模式当 int64 原样送出去 (0x8278 读出来是 -32136), MySQL 会判 Out of range:
  96. // 严格模式直接报错, 非严格模式截成 0 —— 静默改数据。
  97. func TestCoerceForSQLUnsignedSmall(t *testing.T) {
  98. cases := []struct {
  99. name string
  100. ft types.FieldType
  101. in any
  102. want int64
  103. }{
  104. {"uint16 上半区位模式", types.FieldType_Uint16, int16(-32136), 33400}, // 0x8278
  105. {"uint16 普通值", types.FieldType_Uint16, int16(3306), 3306},
  106. {"uint16 上界位模式", types.FieldType_Uint16, int16(-1), 65535},
  107. {"uint32 上半区位模式", types.FieldType_Uint32, int32(-1294967296), 3000000000},
  108. {"uint32 上界位模式", types.FieldType_Uint32, int32(-1), 4294967295},
  109. }
  110. for _, c := range cases {
  111. col := Column{Name: "u", Type: c.ft}
  112. for _, mysql := range []bool{true, false} {
  113. got, err := coerceForSQL(col, c.in, mysql)
  114. if err != nil {
  115. t.Errorf("%s (mysql=%v): 不该报错: %v", c.name, mysql, err)
  116. continue
  117. }
  118. if got != c.want {
  119. t.Errorf("%s (mysql=%v): = %v (%T), 期望 %d", c.name, mysql, got, got, c.want)
  120. }
  121. }
  122. }
  123. }
  124. // TestCoerceForVRTXUnsignedSmall 2 / 4 字节无符号档"装得下"的判据是【位模式】, 不是
  125. // int16 / int32 的正数区。
  126. //
  127. // 真实踩到: MySQL 的 SMALLINT UNSIGNED 存了端口号 33400, 迁进 VRTX 的 uint16 列时被判
  128. // "值 33400 超出 int16" —— 可 33400 只占 16 位 (0x8278), 位模式完全装得下。按有符号区间判
  129. // 等于给无符号列凭空加了一条"上界只到 32767"的约束, 把 32768..65535 这半个无符号区整片
  130. // 判成脏数据; 而 -32136 只是同一个位模式的另一种读法, 两个方向都得收。
  131. func TestCoerceForVRTXUnsignedSmall(t *testing.T) {
  132. const col = "port"
  133. cases := []struct {
  134. name string
  135. ft types.FieldType
  136. in any
  137. want any
  138. }{
  139. // 无符号上半区: 合法, 落成负的位模式载体 (读取侧 DecodeValue 也回 int16/int32)
  140. {"uint16 上半区", types.FieldType_Uint16, int64(33400), int16(-32136)},
  141. {"uint16 上界", types.FieldType_Uint16, int64(math.MaxUint16), int16(-1)},
  142. {"uint16 负位模式 (回读 / SQLite INTEGER)", types.FieldType_Uint16, int16(-1), int16(-1)},
  143. {"uint16 普通值", types.FieldType_Uint16, int64(3306), int16(3306)},
  144. {"uint32 上半区", types.FieldType_Uint32, int64(3000000000), int32(-1294967296)},
  145. {"uint32 上界", types.FieldType_Uint32, int64(math.MaxUint32), int32(-1)},
  146. {"uint32 普通值", types.FieldType_Uint32, int64(42), int32(42)},
  147. }
  148. for _, c := range cases {
  149. got, err := coerceForVRTX(c.ft, 0, c.in, col, true, false)
  150. if err != nil {
  151. t.Errorf("%s: 不该报错: %v", c.name, err)
  152. continue
  153. }
  154. if got != c.want {
  155. t.Errorf("%s: = %v (%T), 期望 %v (%T)", c.name, got, got, c.want, c.want)
  156. }
  157. }
  158. // 放不下位模式才是真放不下 —— 这条闸门不能因为放宽上界就整个拆掉。
  159. for _, c := range []struct {
  160. name string
  161. ft types.FieldType
  162. in any
  163. }{
  164. {"uint16 超出 16 位", types.FieldType_Uint16, int64(math.MaxUint16) + 1},
  165. {"uint16 低于 -32768", types.FieldType_Uint16, int64(math.MinInt16) - 1},
  166. {"uint32 超出 32 位", types.FieldType_Uint32, int64(math.MaxUint32) + 1},
  167. } {
  168. if got, err := coerceForVRTX(c.ft, 0, c.in, col, true, false); err == nil {
  169. t.Errorf("%s: 应当报错, 实际返回 %v", c.name, got)
  170. }
  171. }
  172. }