|
1 | 1 | package e2e |
2 | 2 |
|
3 | 3 | import ( |
| 4 | + "encoding/hex" |
4 | 5 | "fmt" |
5 | 6 | "math" |
6 | 7 | "strconv" |
@@ -229,6 +230,203 @@ func (s ClickHouseSuite) Test_MySQL_Blobs() { |
229 | 230 | RequireEnvCanceled(s.t, env) |
230 | 231 | } |
231 | 232 |
|
| 233 | +// Test_MySQL_Binary_Trailing_Zeros reproduces trailing-0x00 truncation of fixed-length |
| 234 | +// BINARY(N) columns over CDC. |
| 235 | +// |
| 236 | +// MySQL right-pads BINARY(N) to N bytes with 0x00. Internally this is represented |
| 237 | +// as a binary array of length M=N-number_of_trailing(0x00). When reading from binlog these |
| 238 | +// values need to be adapted back to N bytes based on the column type. |
| 239 | +// |
| 240 | +// See https://github.com/shyiko/mysql-binlog-connector-java/issues/169#issuecomment-301864569 |
| 241 | +// for a similar problem in a different project. |
| 242 | +func (s ClickHouseSuite) Test_MySQL_Binary_Trailing_Zeros() { |
| 243 | + if _, ok := s.source.(*MySqlSource); !ok { |
| 244 | + s.t.Skip("only applies to mysql") |
| 245 | + } |
| 246 | + |
| 247 | + srcTableName := "test_binary_padding" |
| 248 | + srcFullName := s.attachSchemaSuffix(srcTableName) |
| 249 | + quotedSrcFullName := "\"" + strings.ReplaceAll(srcFullName, ".", "\".\"") + "\"" |
| 250 | + dstTableName := "test_binary_padding_dst" |
| 251 | + |
| 252 | + type binaryCase struct { |
| 253 | + id int |
| 254 | + label string |
| 255 | + beforeSnapshot bool |
| 256 | + // BINARY(16) covers the common short metadata path with a 1-byte row length. |
| 257 | + b16InsertHex string |
| 258 | + b16WantHex string |
| 259 | + // BINARY(255) covers the upper edge of the same metadata path without switching |
| 260 | + // to MySQL's 2-byte row length encoding for fixed strings longer than 255 bytes. |
| 261 | + // MySQL does not allow BINARY(N) with N > 255. |
| 262 | + b255InsertHex string |
| 263 | + b255WantHex string |
| 264 | + } |
| 265 | + |
| 266 | + cases := []binaryCase{ |
| 267 | + // SELECT path: correct today, included to prove snapshot and CDC agree. |
| 268 | + { |
| 269 | + id: 1, |
| 270 | + label: "snapshot_trailing_zero", |
| 271 | + beforeSnapshot: true, |
| 272 | + b16InsertHex: strings.Repeat("11", 15) + "00", |
| 273 | + b16WantHex: strings.Repeat("11", 15) + "00", |
| 274 | + b255InsertHex: strings.Repeat("11", 254) + "00", |
| 275 | + b255WantHex: strings.Repeat("11", 254) + "00", |
| 276 | + }, |
| 277 | + // CDC path: MySQL packs these shorter than their declared BINARY(N) length. |
| 278 | + { |
| 279 | + id: 2, |
| 280 | + label: "cdc_trailing_zero", |
| 281 | + beforeSnapshot: false, |
| 282 | + b16InsertHex: "21" + strings.Repeat("11", 14) + "00", |
| 283 | + b16WantHex: "21" + strings.Repeat("11", 14) + "00", |
| 284 | + b255InsertHex: "21" + strings.Repeat("11", 253) + "00", |
| 285 | + b255WantHex: "21" + strings.Repeat("11", 253) + "00", |
| 286 | + }, |
| 287 | + { |
| 288 | + id: 3, |
| 289 | + label: "cdc_multiple_trailing_zeros", |
| 290 | + beforeSnapshot: false, |
| 291 | + b16InsertHex: "22" + strings.Repeat("11", 11) + strings.Repeat("00", 4), |
| 292 | + b16WantHex: "22" + strings.Repeat("11", 11) + strings.Repeat("00", 4), |
| 293 | + b255InsertHex: "22" + strings.Repeat("11", 250) + strings.Repeat("00", 4), |
| 294 | + b255WantHex: "22" + strings.Repeat("11", 250) + strings.Repeat("00", 4), |
| 295 | + }, |
| 296 | + { |
| 297 | + id: 4, |
| 298 | + label: "cdc_all_zero", |
| 299 | + beforeSnapshot: false, |
| 300 | + b16InsertHex: strings.Repeat("00", 16), |
| 301 | + b16WantHex: strings.Repeat("00", 16), |
| 302 | + b255InsertHex: strings.Repeat("00", 255), |
| 303 | + b255WantHex: strings.Repeat("00", 255), |
| 304 | + }, |
| 305 | + { |
| 306 | + id: 5, |
| 307 | + label: "cdc_short_insert", |
| 308 | + beforeSnapshot: false, |
| 309 | + b16InsertHex: "41", |
| 310 | + b16WantHex: "41" + strings.Repeat("00", 15), |
| 311 | + b255InsertHex: "42", |
| 312 | + b255WantHex: "42" + strings.Repeat("00", 254), |
| 313 | + }, |
| 314 | + // Controls: interior zero bytes and non-zero tails should be preserved without extra padding. |
| 315 | + { |
| 316 | + id: 6, |
| 317 | + label: "cdc_interior_zero_nonzero_tail", |
| 318 | + beforeSnapshot: false, |
| 319 | + b16InsertHex: "FF00112233445566778899AABBCCDDEE", |
| 320 | + b16WantHex: "FF00112233445566778899AABBCCDDEE", |
| 321 | + b255InsertHex: "FF00" + strings.Repeat("7F", 252) + "EE", |
| 322 | + b255WantHex: "FF00" + strings.Repeat("7F", 252) + "EE", |
| 323 | + }, |
| 324 | + } |
| 325 | + |
| 326 | + require.NoError(s.t, s.source.Exec(s.t.Context(), fmt.Sprintf(` |
| 327 | + CREATE TABLE IF NOT EXISTS %s ( |
| 328 | + id INT PRIMARY KEY, |
| 329 | + label TEXT NOT NULL, |
| 330 | + b16 BINARY(16) NOT NULL, |
| 331 | + b255 BINARY(255) NOT NULL |
| 332 | + ) |
| 333 | + `, quotedSrcFullName))) |
| 334 | + |
| 335 | + var snapshotCases []binaryCase |
| 336 | + var cdcCases []binaryCase |
| 337 | + for _, tc := range cases { |
| 338 | + if tc.beforeSnapshot { |
| 339 | + snapshotCases = append(snapshotCases, tc) |
| 340 | + } else { |
| 341 | + cdcCases = append(cdcCases, tc) |
| 342 | + } |
| 343 | + } |
| 344 | + snapshotValues := make([]string, 0, len(snapshotCases)) |
| 345 | + for _, tc := range snapshotCases { |
| 346 | + snapshotValues = append(snapshotValues, fmt.Sprintf( |
| 347 | + "(%d,'%s',UNHEX('%s'),UNHEX('%s'))", |
| 348 | + tc.id, tc.label, tc.b16InsertHex, tc.b255InsertHex)) |
| 349 | + } |
| 350 | + require.NoError(s.t, s.source.Exec(s.t.Context(), fmt.Sprintf( |
| 351 | + `INSERT INTO %s (id,label,b16,b255) VALUES %s`, quotedSrcFullName, strings.Join(snapshotValues, ",")))) |
| 352 | + |
| 353 | + connectionGen := FlowConnectionGenerationConfig{ |
| 354 | + FlowJobName: s.attachSuffix(srcTableName), |
| 355 | + TableNameMapping: map[string]string{srcFullName: dstTableName}, |
| 356 | + Destination: s.Peer().Name, |
| 357 | + } |
| 358 | + flowConnConfig := connectionGen.GenerateFlowConnectionConfigs(s) |
| 359 | + flowConnConfig.DoInitialSnapshot = true |
| 360 | + |
| 361 | + tc := NewTemporalClient(s.t) |
| 362 | + env := ExecutePeerflow(s.t, tc, flowConnConfig) |
| 363 | + SetupCDCFlowStatusQuery(s.t, env, flowConnConfig) |
| 364 | + |
| 365 | + EnvWaitForCount(env, s, "waiting for snapshot row", dstTableName, "id,label,b16,b255", len(snapshotCases)) |
| 366 | + |
| 367 | + cdcValues := make([]string, 0, len(cdcCases)) |
| 368 | + for _, tc := range cdcCases { |
| 369 | + cdcValues = append(cdcValues, fmt.Sprintf( |
| 370 | + "(%d,'%s',UNHEX('%s'),UNHEX('%s'))", |
| 371 | + tc.id, tc.label, tc.b16InsertHex, tc.b255InsertHex)) |
| 372 | + } |
| 373 | + require.NoError(s.t, s.source.Exec(s.t.Context(), fmt.Sprintf( |
| 374 | + `INSERT INTO %s (id,label,b16,b255) VALUES %s`, quotedSrcFullName, strings.Join(cdcValues, ",")))) |
| 375 | + |
| 376 | + EnvWaitForCount(env, s, "waiting for cdc rows", dstTableName, "id,label,b16,b255", len(cases)) |
| 377 | + |
| 378 | + rows, err := s.GetRows(dstTableName, "id,label,b16,b255") |
| 379 | + require.NoError(s.t, err) |
| 380 | + require.Len(s.t, rows.Records, len(cases), "expected all binary padding test rows") |
| 381 | + |
| 382 | + byLabel := make(map[string]map[string][]byte, len(rows.Records)) |
| 383 | + for _, row := range rows.Records { |
| 384 | + label, ok := row[1].Value().(string) |
| 385 | + require.True(s.t, ok, "label should be a string") |
| 386 | + byLabel[label] = make(map[string][]byte, 2) |
| 387 | + for _, col := range []struct { |
| 388 | + name string |
| 389 | + idx int |
| 390 | + }{ |
| 391 | + {name: "b16", idx: 2}, |
| 392 | + {name: "b255", idx: 3}, |
| 393 | + } { |
| 394 | + switch v := row[col.idx].Value().(type) { |
| 395 | + case string: |
| 396 | + byLabel[label][col.name] = []byte(v) |
| 397 | + case []byte: |
| 398 | + byLabel[label][col.name] = v |
| 399 | + default: |
| 400 | + require.Failf(s.t, "unexpected type for binary column", |
| 401 | + "label=%s column=%s type=%T", label, col.name, row[col.idx].Value()) |
| 402 | + } |
| 403 | + } |
| 404 | + } |
| 405 | + |
| 406 | + for _, tc := range cases { |
| 407 | + for _, col := range []struct { |
| 408 | + name string |
| 409 | + wantHex string |
| 410 | + }{ |
| 411 | + {name: "b16", wantHex: tc.b16WantHex}, |
| 412 | + {name: "b255", wantHex: tc.b255WantHex}, |
| 413 | + } { |
| 414 | + want, decErr := hex.DecodeString(col.wantHex) |
| 415 | + require.NoError(s.t, decErr) |
| 416 | + gotByCol, ok := byLabel[tc.label] |
| 417 | + require.Truef(s.t, ok, "missing row %q in destination", tc.label) |
| 418 | + got, ok := gotByCol[col.name] |
| 419 | + require.Truef(s.t, ok, "missing column %q for row %q in destination", col.name, tc.label) |
| 420 | + require.Equalf(s.t, want, got, |
| 421 | + "%s %q: MySQL stores %d bytes (%s) but ClickHouse has %d bytes (%s)", |
| 422 | + col.name, tc.label, len(want), col.wantHex, len(got), strings.ToUpper(hex.EncodeToString(got))) |
| 423 | + } |
| 424 | + } |
| 425 | + |
| 426 | + env.Cancel(s.t.Context()) |
| 427 | + RequireEnvCanceled(s.t, env) |
| 428 | +} |
| 429 | + |
232 | 430 | func (s ClickHouseSuite) Test_MySQL_Enum() { |
233 | 431 | if mySource, ok := s.source.(*MySqlSource); !ok { |
234 | 432 | s.t.Skip("only applies to mysql") |
|
0 commit comments