Fix DM NVARCHAR2 type mapping in JDBC connector#10638
Fix DM NVARCHAR2 type mapping in JDBC connector#10638qianchongyang wants to merge 1 commit intoapache:devfrom
Conversation
Add support for NVARCHAR2 data type in Dameng database JDBC connector. Previously, NVARCHAR2 columns were not recognized and caused type conversion errors. Now NVARCHAR2 is properly mapped to STRING type. Fixes apache#10635
Issue 1: sourceType hardcoded causing incorrect NVARCHAR type displayLocation: case DM_NVARCHAR:
case DM_NVARCHAR2:
builder.sourceType(String.format("%s(%s)", DM_NVARCHAR2, typeDefine.getLength()));
builder.dataType(BasicType.STRING_TYPE);
builder.columnLength(TypeDefineUtils.charTo4ByteLength(typeDefine.getLength()));
break;Related Context:
Problem Description:
This results in loss of type information, and users cannot distinguish whether the original column type is NVARCHAR or NVARCHAR2. Potential Risks:
Scope of Impact:
Severity: MAJOR Improvement Suggestion: case DM_NVARCHAR:
case DM_NVARCHAR2:
// Use dmType variable instead of hardcoding DM_NVARCHAR2
builder.sourceType(String.format("%s(%s)", dmType, typeDefine.getLength()));
builder.dataType(BasicType.STRING_TYPE);
builder.columnLength(TypeDefineUtils.charTo4ByteLength(typeDefine.getLength()));
break;Reason:
Refer to the handling of Issue 2: Missing test cases for NVARCHAR2 typeLocation: Related Context:
Problem Description: Potential Risks:
Scope of Impact:
Severity: MAJOR Improvement Suggestion: @Test
public void testConvertNvarchar2() {
BasicTypeDefine<Object> typeDefine =
BasicTypeDefine.builder()
.name("test")
.columnType("nvarchar2(10)")
.dataType("nvarchar2")
.length(10L)
.build();
Column column = DmdbTypeConverter.INSTANCE.convert(typeDefine);
Assertions.assertEquals(typeDefine.getName(), column.getName());
Assertions.assertEquals(BasicType.STRING_TYPE, column.getDataType());
Assertions.assertEquals(40, column.getColumnLength()); // 10 * 4
Assertions.assertEquals(typeDefine.getColumnType(), column.getSourceType().toLowerCase());
}
@Test
public void testConvertNvarchar2WithoutLength() {
BasicTypeDefine<Object> typeDefine =
BasicTypeDefine.builder()
.name("test")
.columnType("nvarchar2")
.dataType("nvarchar2")
.build();
Column column = DmdbTypeConverter.INSTANCE.convert(typeDefine);
Assertions.assertEquals(typeDefine.getName(), column.getName());
Assertions.assertEquals(BasicType.STRING_TYPE, column.getDataType());
// charTo4ByteLength returns null when length is null
Assertions.assertNull(column.getColumnLength());
Assertions.assertEquals("nvarchar2", column.getSourceType().toLowerCase());
}Reason:
Issue 3: Missing comment explaining NVARCHAR vs NVARCHAR2 differencesLocation: Related Context:
Problem Description: Potential Risks:
Scope of Impact:
Severity: MINOR Improvement Suggestion: // NVARCHAR and NVARCHAR2 are both Unicode character types in DM database
// They support storing multi-byte characters (Unicode) and can be treated the same way
// Reference: https://eco.dameng.com/document/dm/zh-cn/sql-dev/dmpl-sql-datatype.html
case DM_NVARCHAR:
case DM_NVARCHAR2:
builder.sourceType(String.format("%s(%s)", dmType, typeDefine.getLength()));
builder.dataType(BasicType.STRING_TYPE);
builder.columnLength(TypeDefineUtils.charTo4ByteLength(typeDefine.getLength()));
break;Reason:
Issue 4: reconvert method does not consider NVARCHAR/NVARCHAR2 distinctionLocation: case STRING:
builder.length(column.getColumnLength());
if (column.getColumnLength() == null || column.getColumnLength() <= 0) {
builder.columnType(DM_TEXT);
builder.dataType(DM_TEXT);
} else if (column.getColumnLength() <= MAX_CHAR_LENGTH_FOR_PAGE_4K) {
builder.columnType(
String.format("%s(%s)", DM_VARCHAR2, column.getColumnLength()));
builder.dataType(DM_VARCHAR2);
} else {
builder.columnType(DM_TEXT);
builder.dataType(DM_TEXT);
}
break;Related Context:
Problem Description: Potential Risks:
Scope of Impact:
Severity: MINOR (because functionally acceptable) Improvement Suggestion:
However, considering:
It is recommended to not make changes for now, but document this behavior. Reason:
|
|
Please enable CI following the instructions. |
|
Could you also add some tests for this change? |
Problem
When synchronizing data from Dameng Database to MySQL via the JDBC connector, NVARCHAR2 data type is not supported. The JDBC connector fails to map NVARCHAR2 columns during table synchronization.
Solution
Added NVARCHAR2 type mapping to the Dameng JDBC dialect:
Validation
Fixes #10635