-
Notifications
You must be signed in to change notification settings - Fork 687
fix(mysql-cdc): up cast all unsigned int types #23278
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
wcy-fdu
wants to merge
22
commits into
main
Choose a base branch
from
wcy/up_cast_unsigned.pr
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
22 commits
Select commit
Hold shift + click to select a range
e2c1cfb
fix
wcy-fdu 2ee2f47
revert change in debezium json
wcy-fdu cda73e1
fmt
wcy-fdu dc91e99
need to cast
wcy-fdu b25e532
work
wcy-fdu a51d93d
clippy happy
wcy-fdu 4f360c9
minor fix
wcy-fdu 4c39c7c
use contain
wcy-fdu d38c3a2
directly convert value
wcy-fdu d36fb13
minor
wcy-fdu 76231b4
minor
wcy-fdu 1fa38af
minor
wcy-fdu b4caaf0
fmt
wcy-fdu a88c6e7
save worl
wcy-fdu 7be416c
Merge branch 'wcy/new_method' into wcy/up_cast_unsigned.pr
wcy-fdu 1ae19df
save work, can work
wcy-fdu 1b90263
save work
wcy-fdu 0c23b79
save work
wcy-fdu 23e29e1
clippy happy
wcy-fdu 96edc04
clippy happy
wcy-fdu a0c9fcd
merge main and resolve conflict
wcy-fdu 4be0d70
add test for unsigned int
wcy-fdu File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
File renamed without changes.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,168 @@ | ||
# Test MySQL CDC with UNSIGNED BIGINT handling | ||
# This test verifies that UNSIGNED BIGINT values are handled correctly in CDC backfill | ||
# with debezium.bigint.unsigned.handling.mode='precise' | ||
|
||
control substitution on | ||
|
||
statement ok | ||
ALTER SYSTEM SET max_concurrent_creating_streaming_jobs TO 1; | ||
|
||
system ok | ||
mysql -e " | ||
CREATE DATABASE IF NOT EXISTS risedev; | ||
USE risedev; | ||
DROP TABLE IF EXISTS test_unsigned_int; | ||
CREATE TABLE test_unsigned_int ( | ||
id BIGINT UNSIGNED NOT NULL AUTO_INCREMENT, | ||
unsigned_bigint BIGINT UNSIGNED NOT NULL DEFAULT '0', | ||
unsigned_int INT UNSIGNED NOT NULL DEFAULT '0', | ||
unsigned_smallint SMALLINT UNSIGNED NOT NULL DEFAULT '0', | ||
unsigned_tinyint TINYINT UNSIGNED NOT NULL DEFAULT '0', | ||
info VARCHAR(50) NOT NULL DEFAULT '', | ||
PRIMARY KEY (id, unsigned_bigint) | ||
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci; | ||
" | ||
|
||
# Insert 50 backfill rows into MySQL (20 non-overflow + 30 overflow) | ||
system ok | ||
mysql -e " | ||
USE risedev; | ||
INSERT INTO test_unsigned_int (unsigned_bigint, unsigned_int, unsigned_smallint, unsigned_tinyint, info) VALUES | ||
(1, 1, 1, 1, 'backfill'), | ||
(12345, 12345, 12345, 123, 'backfill'), | ||
(123456, 123456, 12345, 123, 'backfill'), | ||
(1234567, 1234567, 12345, 123, 'backfill'), | ||
(12345678, 12345678, 12345, 123, 'backfill'), | ||
(123456789, 123456789, 12345, 123, 'backfill'), | ||
(1234567890, 1234567890, 12345, 123, 'backfill'), | ||
(12345678901, 1234567890, 12345, 123, 'backfill'), | ||
(123456789012, 1234567890, 12345, 123, 'backfill'), | ||
(1234567890123, 1234567890, 12345, 123, 'backfill'), | ||
(12345678901234, 1234567890, 12345, 123, 'backfill'), | ||
(123456789012345, 1234567890, 12345, 123, 'backfill'), | ||
(1234567890123456, 1234567890, 12345, 123, 'backfill'), | ||
(12345678901234567, 1234567890, 12345, 123, 'backfill'), | ||
(123456789012345678, 1234567890, 12345, 123, 'backfill'), | ||
(1234567890123456789, 1234567890, 12345, 123, 'backfill'), | ||
(12345678901234567890, 1234567890, 12345, 123, 'backfill'), | ||
(9223372036854775800, 2147483647, 65535, 255, 'backfill'), | ||
(9223372036854775805, 2147483647, 65535, 255, 'backfill'), | ||
(9223372036854775807, 2147483647, 65535, 255, 'backfill'); | ||
INSERT INTO test_unsigned_int (unsigned_bigint, unsigned_int, unsigned_smallint, unsigned_tinyint, info) VALUES | ||
(9223372036854775808, 4294967295, 65535, 255, 'backfill'), | ||
(9223372036854775809, 4294967295, 65535, 255, 'backfill'), | ||
(9223372036854775810, 4294967295, 65535, 255, 'backfill'), | ||
(9223372036854775811, 4294967295, 65535, 255, 'backfill'), | ||
(9223372036854775812, 4294967295, 65535, 255, 'backfill'), | ||
(9223372036854775813, 4294967295, 65535, 255, 'backfill'), | ||
(9223372036854775814, 4294967295, 65535, 255, 'backfill'), | ||
(9223372036854775815, 4294967295, 65535, 255, 'backfill'), | ||
(9223372036854775816, 4294967295, 65535, 255, 'backfill'), | ||
(9223372036854775817, 4294967295, 65535, 255, 'backfill'), | ||
(9223372036854775818, 4294967295, 65535, 255, 'backfill'), | ||
(9223372036854775819, 4294967295, 65535, 255, 'backfill'), | ||
(9223372036854775820, 4294967295, 65535, 255, 'backfill'), | ||
(9223372036854775821, 4294967295, 65535, 255, 'backfill'), | ||
(9223372036854775822, 4294967295, 65535, 255, 'backfill'), | ||
(9223372036854775823, 4294967295, 65535, 255, 'backfill'), | ||
(9223372036854775824, 4294967295, 65535, 255, 'backfill'), | ||
(9223372036854775825, 4294967295, 65535, 255, 'backfill'), | ||
(9223372036854775826, 4294967295, 65535, 255, 'backfill'), | ||
(9223372036854775827, 4294967295, 65535, 255, 'backfill'), | ||
(9223372036854775828, 4294967295, 65535, 255, 'backfill'), | ||
(9223372036854775829, 4294967295, 65535, 255, 'backfill'), | ||
(9223372036854775830, 4294967295, 65535, 255, 'backfill'), | ||
(9223372036854775831, 4294967295, 65535, 255, 'backfill'), | ||
(9223372036854775832, 4294967295, 65535, 255, 'backfill'), | ||
(9223372036854775833, 4294967295, 65535, 255, 'backfill'), | ||
(9223372036854775834, 4294967295, 65535, 255, 'backfill'), | ||
(9223372036854775835, 4294967295, 65535, 255, 'backfill'), | ||
(9223372036854775836, 4294967295, 65535, 255, 'backfill'), | ||
(18446251075179777772, 4294967295, 65535, 255, 'backfill'); | ||
" | ||
|
||
sleep 2s | ||
|
||
statement ok | ||
create source s_unsigned_int with ( | ||
${RISEDEV_MYSQL_WITH_OPTIONS_COMMON}, | ||
username = '${RISEDEV_MYSQL_USER}', | ||
password = '${MYSQL_PWD}', | ||
database.name = 'risedev', | ||
debezium.bigint.unsigned.handling.mode = 'precise' | ||
); | ||
|
||
sleep 2s | ||
|
||
statement ok | ||
create table rw_unsigned_int ( | ||
id DECIMAL, | ||
unsigned_bigint DECIMAL, | ||
unsigned_int BIGINT, | ||
unsigned_smallint INT, | ||
unsigned_tinyint SMALLINT, | ||
info VARCHAR, | ||
primary key (id, unsigned_bigint) | ||
) with ( | ||
snapshot = 'true', | ||
snapshot.batch_size = 10, | ||
) from s_unsigned_int table 'risedev.test_unsigned_int'; | ||
|
||
sleep 5s | ||
|
||
# Insert 3 incremental data (debezium) | ||
system ok | ||
mysql -e " | ||
USE risedev; | ||
INSERT INTO test_unsigned_int (unsigned_bigint, unsigned_int, unsigned_smallint, unsigned_tinyint, info) VALUES | ||
(18446251075179777770, 4294967295, 65535, 255, 'debezium'), | ||
(18446251075179777771, 4294967295, 65535, 255, 'debezium'), | ||
(18446251075179777772, 4294967295, 65535, 255, 'debezium'); | ||
" | ||
|
||
sleep 3s | ||
|
||
# Verify total count in RisingWave (50 backfill + 3 debezium = 53) | ||
query I | ||
select count(*) as row_cnt from rw_unsigned_int; | ||
---- | ||
53 | ||
|
||
# Verify we have both backfill and debezium data | ||
query T | ||
select count(*) as backfill_count from rw_unsigned_int where info = 'backfill'; | ||
---- | ||
50 | ||
|
||
query T | ||
select count(*) as debezium_count from rw_unsigned_int where info = 'debezium'; | ||
---- | ||
3 | ||
|
||
# Verify specific rows match inserted data | ||
# Check id=50 (last backfill row with overflow data) | ||
query TTTTTT | ||
select id, unsigned_bigint, unsigned_int, unsigned_smallint, unsigned_tinyint, info from rw_unsigned_int where id = 50; | ||
---- | ||
50 18446251075179777772 4294967295 65535 255 backfill | ||
|
||
# Check id=53 (last debezium row with overflow data) | ||
query TTTTTT | ||
select id, unsigned_bigint, unsigned_int, unsigned_smallint, unsigned_tinyint, info from rw_unsigned_int where id = 53; | ||
---- | ||
53 18446251075179777772 4294967295 65535 255 debezium | ||
|
||
|
||
|
||
# Clean up | ||
statement ok | ||
drop table rw_unsigned_int; | ||
|
||
statement ok | ||
drop source s_unsigned_int cascade; | ||
|
||
system ok | ||
mysql -e " | ||
USE risedev; | ||
DROP TABLE IF EXISTS test_unsigned_int; | ||
" |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -15,6 +15,7 @@ | |
use std::sync::LazyLock; | ||
|
||
use mysql_async::Row as MysqlRow; | ||
use mysql_common::constants::ColumnFlags; | ||
use risingwave_common::catalog::Schema; | ||
use risingwave_common::log::LogSuppresser; | ||
use risingwave_common::row::OwnedRow; | ||
|
@@ -62,6 +63,42 @@ macro_rules! handle_data_type { | |
}}; | ||
} | ||
|
||
macro_rules! handle_data_type_with_signed { | ||
( | ||
$mysql_row:expr, | ||
$mysql_datum_index:expr, | ||
$column_name:expr, | ||
$signed_type:ty, | ||
$unsigned_type:ty | ||
) => {{ | ||
let column_flags = $mysql_row.columns()[$mysql_datum_index].flags(); | ||
|
||
if column_flags.contains(ColumnFlags::UNSIGNED_FLAG) { | ||
// UNSIGNED type: use unsigned type conversion, then convert to signed | ||
match $mysql_row.take_opt::<Option<$unsigned_type>, _>($mysql_datum_index) { | ||
Some(Ok(Some(val))) => Ok(Some(ScalarImpl::from(val as $signed_type))), | ||
Some(Ok(None)) => Ok(None), | ||
Some(Err(e)) => Err(anyhow::Error::new(e.clone()) | ||
.context("failed to deserialize MySQL value into rust value") | ||
.context(format!( | ||
"column: {}, index: {}, rust_type: {}", | ||
$column_name, | ||
$mysql_datum_index, | ||
stringify!($unsigned_type), | ||
))), | ||
None => bail!( | ||
"no value found at column: {}, index: {}", | ||
$column_name, | ||
$mysql_datum_index | ||
), | ||
} | ||
} else { | ||
// SIGNED type: use default signed type conversion | ||
handle_data_type!($mysql_row, $mysql_datum_index, $column_name, $signed_type) | ||
} | ||
}}; | ||
} | ||
|
||
/// The decoding result can be interpreted as follows: | ||
/// Ok(value) => The value was found and successfully decoded. | ||
/// Err(error) => The value was found but could not be decoded, | ||
|
@@ -107,12 +144,13 @@ pub fn mysql_datum_to_rw_datum( | |
} | ||
} | ||
DataType::Int16 => { | ||
handle_data_type!(mysql_row, mysql_datum_index, column_name, i16) | ||
handle_data_type_with_signed!(mysql_row, mysql_datum_index, column_name, i16, u16) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. How does upcast work here? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Could you explain the relationship between DataType::Int16, i16, and u16? |
||
} | ||
DataType::Int32 => { | ||
handle_data_type!(mysql_row, mysql_datum_index, column_name, i32) | ||
handle_data_type_with_signed!(mysql_row, mysql_datum_index, column_name, i32, u32) | ||
} | ||
DataType::Int64 => { | ||
// for bigint unsigned, should up cast to decimal. | ||
handle_data_type!(mysql_row, mysql_datum_index, column_name, i64) | ||
} | ||
DataType::Float32 => { | ||
|
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I will try to distinguish signed bigint from unsigned bigint in a later PR to achieve more accurate schema check.