Skip to content

Commit 5f85d76

Browse files
authored
[Hotfix][Jdbc] Fix mysql tinyint(1) type mapping for TypeMapper (#9012)
1 parent c1b8284 commit 5f85d76

File tree

4 files changed

+132
-2
lines changed

4 files changed

+132
-2
lines changed

Diff for: seatunnel-connectors-v2/connector-jdbc/src/main/java/org/apache/seatunnel/connectors/seatunnel/jdbc/internal/dialect/mysql/MySqlTypeMapper.java

+5-1
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@ public Column mappingColumn(ResultSetMetaData metadata, int colIndex) throws SQL
4848
String columnName = metadata.getColumnLabel(colIndex);
4949
// e.g. tinyint unsigned
5050
String nativeType = metadata.getColumnTypeName(colIndex);
51+
String columnType = nativeType;
5152
int isNullable = metadata.isNullable(colIndex);
5253
int precision = metadata.getPrecision(colIndex);
5354
int scale = metadata.getScale(colIndex);
@@ -56,11 +57,14 @@ public Column mappingColumn(ResultSetMetaData metadata, int colIndex) throws SQL
5657
long octetLength = TypeDefineUtils.charTo4ByteLength((long) precision);
5758
precision = (int) Math.max(precision, octetLength);
5859
}
60+
if ("tinyint".equalsIgnoreCase(nativeType) && precision == 1) {
61+
columnType = "tinyint(1)";
62+
}
5963

6064
BasicTypeDefine typeDefine =
6165
BasicTypeDefine.builder()
6266
.name(columnName)
63-
.columnType(nativeType)
67+
.columnType(columnType)
6468
.dataType(nativeType)
6569
.nullable(isNullable == ResultSetMetaData.columnNullable)
6670
.length((long) precision)

Diff for: seatunnel-connectors-v2/connector-jdbc/src/main/java/org/apache/seatunnel/connectors/seatunnel/jdbc/internal/dialect/oceanbase/OceanBaseMySqlTypeMapper.java

+5-1
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@ public Column mappingColumn(ResultSetMetaData metadata, int colIndex) throws SQL
4848
String columnName = metadata.getColumnLabel(colIndex);
4949
// e.g. tinyint unsigned
5050
String nativeType = metadata.getColumnTypeName(colIndex);
51+
String columnType = nativeType;
5152
int isNullable = metadata.isNullable(colIndex);
5253
int precision = metadata.getPrecision(colIndex);
5354
int scale = metadata.getScale(colIndex);
@@ -56,11 +57,14 @@ public Column mappingColumn(ResultSetMetaData metadata, int colIndex) throws SQL
5657
long octetLength = TypeDefineUtils.charTo4ByteLength((long) precision);
5758
precision = (int) Math.max(precision, octetLength);
5859
}
60+
if ("tinyint".equalsIgnoreCase(nativeType) && precision == 1) {
61+
columnType = "tinyint(1)";
62+
}
5963

6064
BasicTypeDefine typeDefine =
6165
BasicTypeDefine.builder()
6266
.name(columnName)
63-
.columnType(nativeType)
67+
.columnType(columnType)
6468
.dataType(nativeType)
6569
.nullable(isNullable == ResultSetMetaData.columnNullable)
6670
.length((long) precision)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
/*
2+
* Licensed to the Apache Software Foundation (ASF) under one or more
3+
* contributor license agreements. See the NOTICE file distributed with
4+
* this work for additional information regarding copyright ownership.
5+
* The ASF licenses this file to You under the Apache License, Version 2.0
6+
* (the "License"); you may not use this file except in compliance with
7+
* the License. You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*/
17+
18+
package org.apache.seatunnel.connectors.seatunnel.jdbc.internal.dialect.mysql;
19+
20+
import org.apache.seatunnel.api.table.catalog.Column;
21+
22+
import org.junit.jupiter.api.Test;
23+
24+
import java.sql.ResultSetMetaData;
25+
import java.sql.SQLException;
26+
27+
import static org.junit.jupiter.api.Assertions.assertEquals;
28+
import static org.mockito.Mockito.mock;
29+
import static org.mockito.Mockito.when;
30+
31+
public class MySqlTypeMapperTest {
32+
@Test
33+
void returnsTinyint1WhenNativeTypeIsTinyintAndPrecisionIs1() throws SQLException {
34+
ResultSetMetaData metadata = mock(ResultSetMetaData.class);
35+
when(metadata.getColumnLabel(1)).thenReturn("test_column");
36+
when(metadata.getColumnTypeName(1)).thenReturn("tinyint");
37+
when(metadata.isNullable(1)).thenReturn(ResultSetMetaData.columnNullable);
38+
when(metadata.getPrecision(1)).thenReturn(1);
39+
when(metadata.getScale(1)).thenReturn(0);
40+
41+
MySqlTypeMapper typeMapper = new MySqlTypeMapper();
42+
Column column = typeMapper.mappingColumn(metadata, 1);
43+
44+
assertEquals("tinyint(1)", column.getSourceType());
45+
}
46+
47+
@Test
48+
void returnsOriginalTypeWhenNativeTypeIsTinyintAndPrecisionIsNot1() throws SQLException {
49+
ResultSetMetaData metadata = mock(ResultSetMetaData.class);
50+
when(metadata.getColumnLabel(1)).thenReturn("test_column");
51+
when(metadata.getColumnTypeName(1)).thenReturn("tinyint");
52+
when(metadata.isNullable(1)).thenReturn(ResultSetMetaData.columnNullable);
53+
when(metadata.getPrecision(1)).thenReturn(2);
54+
when(metadata.getScale(1)).thenReturn(0);
55+
56+
MySqlTypeMapper typeMapper = new MySqlTypeMapper();
57+
Column column = typeMapper.mappingColumn(metadata, 1);
58+
59+
assertEquals("tinyint", column.getSourceType());
60+
}
61+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
/*
2+
* Licensed to the Apache Software Foundation (ASF) under one or more
3+
* contributor license agreements. See the NOTICE file distributed with
4+
* this work for additional information regarding copyright ownership.
5+
* The ASF licenses this file to You under the Apache License, Version 2.0
6+
* (the "License"); you may not use this file except in compliance with
7+
* the License. You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*/
17+
18+
package org.apache.seatunnel.connectors.seatunnel.jdbc.internal.dialect.oceanbase;
19+
20+
import org.apache.seatunnel.api.table.catalog.Column;
21+
22+
import org.junit.jupiter.api.Test;
23+
24+
import java.sql.ResultSetMetaData;
25+
import java.sql.SQLException;
26+
27+
import static org.junit.jupiter.api.Assertions.assertEquals;
28+
import static org.mockito.Mockito.mock;
29+
import static org.mockito.Mockito.when;
30+
31+
public class OceanBaseMySqlTypeMapperTest {
32+
@Test
33+
void returnsTinyint1WhenNativeTypeIsTinyintAndPrecisionIs1() throws SQLException {
34+
ResultSetMetaData metadata = mock(ResultSetMetaData.class);
35+
when(metadata.getColumnLabel(1)).thenReturn("test_column");
36+
when(metadata.getColumnTypeName(1)).thenReturn("tinyint");
37+
when(metadata.isNullable(1)).thenReturn(ResultSetMetaData.columnNullable);
38+
when(metadata.getPrecision(1)).thenReturn(1);
39+
when(metadata.getScale(1)).thenReturn(0);
40+
41+
OceanBaseMySqlTypeMapper typeMapper = new OceanBaseMySqlTypeMapper();
42+
Column column = typeMapper.mappingColumn(metadata, 1);
43+
44+
assertEquals("tinyint(1)", column.getSourceType());
45+
}
46+
47+
@Test
48+
void returnsOriginalTypeWhenNativeTypeIsTinyintAndPrecisionIsNot1() throws SQLException {
49+
ResultSetMetaData metadata = mock(ResultSetMetaData.class);
50+
when(metadata.getColumnLabel(1)).thenReturn("test_column");
51+
when(metadata.getColumnTypeName(1)).thenReturn("tinyint");
52+
when(metadata.isNullable(1)).thenReturn(ResultSetMetaData.columnNullable);
53+
when(metadata.getPrecision(1)).thenReturn(2);
54+
when(metadata.getScale(1)).thenReturn(0);
55+
56+
OceanBaseMySqlTypeMapper typeMapper = new OceanBaseMySqlTypeMapper();
57+
Column column = typeMapper.mappingColumn(metadata, 1);
58+
59+
assertEquals("tinyint", column.getSourceType());
60+
}
61+
}

0 commit comments

Comments
 (0)