Skip to content

Commit d302749

Browse files
VardhanThigleaasthabharill
authored andcommitted
preventing bit from going negative (#3038)
1 parent 7507776 commit d302749

File tree

5 files changed

+33
-8
lines changed

5 files changed

+33
-8
lines changed

v2/sourcedb-to-spanner/src/main/java/com/google/cloud/teleport/v2/source/reader/io/jdbc/rowmapper/provider/MysqlJdbcValueMappings.java

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,6 @@
2727
import com.google.re2j.Matcher;
2828
import com.google.re2j.Pattern;
2929
import java.math.BigDecimal;
30-
import java.math.BigInteger;
3130
import java.math.RoundingMode;
3231
import java.nio.ByteBuffer;
3332
import java.sql.Blob;
@@ -53,10 +52,6 @@ public class MysqlJdbcValueMappings implements JdbcValueMappingsProvider {
5352
*/
5453
private static final ResultSetValueMapper<?> valuePassThrough = (value, schema) -> value;
5554

56-
/** Map the bytes returned by BIT(N) type to long. MySql Bit(N) has max 64 bits. */
57-
private static final ResultSetValueMapper<byte[]> bytesToLong =
58-
(value, schema) -> new BigInteger(value).longValue();
59-
6055
/** Map BigDecimal type to Byte array. */
6156
private static final ResultSetValueMapper<BigDecimal> bigDecimalToByteArray =
6257
(value, schema) ->
@@ -197,7 +192,7 @@ private static long instantToMicro(Instant instant) {
197192
.put("BIGINT", Pair.of(ResultSet::getLong, valuePassThrough))
198193
.put("BIGINT UNSIGNED", Pair.of(ResultSet::getBigDecimal, bigDecimalToAvroNumber))
199194
.put("BINARY", Pair.of(ResultSet::getBytes, bytesToHexString))
200-
.put("BIT", Pair.of(ResultSet::getBytes, bytesToLong))
195+
.put("BIT", Pair.of(ResultSet::getLong, valuePassThrough))
201196
.put("BLOB", Pair.of(ResultSet::getBlob, blobToHexString))
202197
.put("BOOL", Pair.of(ResultSet::getInt, valuePassThrough))
203198
.put("CHAR", Pair.of(ResultSet::getString, valuePassThrough))

v2/sourcedb-to-spanner/src/test/java/com/google/cloud/teleport/v2/source/reader/io/jdbc/rowmapper/JdbcSourceRowMapperTest.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -291,9 +291,9 @@ private static ImmutableList<Column> mySQLColumns() {
291291
.build())
292292
.add(
293293
Column.builder()
294-
.derbyColumnType("CHAR(8) FOR BIT DATA") // Derby mapping for Bit
294+
.derbyColumnType("BIGINT") // Derby mapping for Bit
295295
.sourceColumnType("BIT")
296-
.inputValue(ByteBuffer.allocate(8).putLong(5L).array())
296+
.inputValue(5L)
297297
.mappedValue(5L)
298298
.build())
299299
.add(

v2/sourcedb-to-spanner/src/test/java/com/google/cloud/teleport/v2/templates/MySQLDataTypesIT.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -158,6 +158,8 @@ private Map<String, List<Map<String, Object>>> getExpectedData() {
158158
"binary",
159159
createRows("binary", "eDU4MD" + repeatString("A", 334), repeatString("/", 340), "NULL"));
160160
expectedData.put("bit", createRows("bit", "f/////////8=", "NULL"));
161+
expectedData.put("bit8", createRows("bit8", "0", "255", "NULL"));
162+
expectedData.put("bit1", createRows("bit1", "false", "true", "NULL"));
161163
expectedData.put("blob", createRows("blob", "eDU4MDA=", repeatString("/", 87380), "NULL"));
162164
expectedData.put("bool", createRows("bool", "false", "true", "NULL"));
163165
expectedData.put("boolean", createRows("boolean", "false", "true", "NULL"));

v2/sourcedb-to-spanner/src/test/resources/DataTypesIT/mysql-data-types.sql

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -152,6 +152,16 @@ CREATE TABLE `bit_table` (
152152
`bit_col` BIT(64) DEFAULT NULL
153153
);
154154

155+
CREATE TABLE `bit8_table` (
156+
`id` INT PRIMARY KEY,
157+
`bit8_col` BIT(8) DEFAULT NULL
158+
);
159+
160+
CREATE TABLE `bit1_table` (
161+
`id` INT PRIMARY KEY,
162+
`bit1_col` BIT(1) DEFAULT NULL
163+
);
164+
155165
CREATE TABLE `boolean_table` (
156166
`id` INT PRIMARY KEY,
157167
`boolean_col` TINYINT(1) DEFAULT NULL
@@ -276,6 +286,8 @@ ALTER TABLE `bigint_table` MODIFY `id` INT AUTO_INCREMENT;
276286
ALTER TABLE `bigint_unsigned_table` MODIFY `id` INT AUTO_INCREMENT;
277287
ALTER TABLE `binary_table` MODIFY `id` INT AUTO_INCREMENT;
278288
ALTER TABLE `bit_table` MODIFY `id` INT AUTO_INCREMENT;
289+
ALTER TABLE `bit8_table` MODIFY `id` INT AUTO_INCREMENT;
290+
ALTER TABLE `bit1_table` MODIFY `id` INT AUTO_INCREMENT;
279291
ALTER TABLE `blob_table` MODIFY `id` INT AUTO_INCREMENT;
280292
ALTER TABLE `bool_table` MODIFY `id` INT AUTO_INCREMENT;
281293
ALTER TABLE `boolean_table` MODIFY `id` INT AUTO_INCREMENT;
@@ -318,6 +330,8 @@ INSERT INTO `bigint_unsigned_table` (`bigint_unsigned_col`) VALUES (184467440737
318330
INSERT INTO `binary_table` (`binary_col`) VALUES (x'7835383030000000000000000000000000000000');
319331
INSERT INTO `binary_table` (`binary_col`) VALUES (REPEAT(X'FF', 255));
320332
INSERT INTO `bit_table` (`bit_col`) VALUES (b'0111111111111111111111111111111111111111111111111111111111111111');
333+
INSERT INTO `bit8_table` (`bit8_col`) VALUES (b'0'), (b'11111111');
334+
INSERT INTO `bit1_table` (`bit1_col`) VALUES (b'0'), (b'1');
321335
INSERT INTO `blob_table` (`blob_col`) VALUES (X'7835383030');
322336
INSERT INTO `blob_table` (`blob_col`) VALUES (REPEAT(X'FF', 65535));
323337
INSERT INTO `bool_table` (`bool_col`) VALUES (0);
@@ -427,6 +441,8 @@ INSERT INTO `bigint_table` (`bigint_col`) VALUES (NULL);
427441
INSERT INTO `bigint_unsigned_table` (`bigint_unsigned_col`) VALUES (NULL);
428442
INSERT INTO `binary_table` (`binary_col`) VALUES (NULL);
429443
INSERT INTO `bit_table` (`bit_col`) VALUES (NULL);
444+
INSERT INTO `bit8_table` (`bit8_col`) VALUES (NULL);
445+
INSERT INTO `bit1_table` (`bit1_col`) VALUES (NULL);
430446
INSERT INTO `blob_table` (`blob_col`) VALUES (NULL);
431447
INSERT INTO `bool_table` (`bool_col`) VALUES (NULL);
432448
INSERT INTO `boolean_table` (`boolean_col`) VALUES (NULL);

v2/sourcedb-to-spanner/src/test/resources/DataTypesIT/mysql-spanner-schema.sql

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,18 @@ CREATE TABLE IF NOT EXISTS bit_table (
1818
bit_col BYTES(MAX),
1919
) PRIMARY KEY(id);
2020

21+
CREATE TABLE IF NOT EXISTS bit8_table (
22+
id INT64 NOT NULL,
23+
-- testing mapping bit to int64
24+
bit8_col INT64,
25+
) PRIMARY KEY(id);
26+
27+
CREATE TABLE IF NOT EXISTS bit1_table (
28+
id INT64 NOT NULL,
29+
-- testing mapping bit to Bool
30+
bit1_col BOOL,
31+
) PRIMARY KEY(id);
32+
2133
CREATE TABLE IF NOT EXISTS blob_table (
2234
id INT64 NOT NULL,
2335
blob_col BYTES(MAX),

0 commit comments

Comments
 (0)