|
18 | 18 | package org.apache.flink.cdc.connectors.starrocks.sink;
|
19 | 19 |
|
20 | 20 | import org.apache.flink.cdc.common.types.CharType;
|
| 21 | +import org.apache.flink.cdc.common.types.DecimalType; |
21 | 22 | import org.apache.flink.cdc.common.types.VarCharType;
|
22 | 23 |
|
23 | 24 | import com.starrocks.connector.flink.catalog.StarRocksColumn;
|
@@ -75,6 +76,50 @@ public void testCharTypeForPrimaryKey() {
|
75 | 76 | assertTrue(smallLengthColumn.isNullable());
|
76 | 77 | }
|
77 | 78 |
|
| 79 | + @Test |
| 80 | + public void testDecimalForPrimaryKey() { |
| 81 | + // Map to DECIMAL of StarRocks if column is DECIMAL type and not primary key. |
| 82 | + StarRocksColumn.Builder noPrimaryKeyBuilder = |
| 83 | + new StarRocksColumn.Builder().setColumnName("no_primary_key").setOrdinalPosition(0); |
| 84 | + new DecimalType(20, 1) |
| 85 | + .accept(new StarRocksUtils.CdcDataTypeTransformer(false, noPrimaryKeyBuilder)); |
| 86 | + StarRocksColumn noPrimaryKeyColumn = noPrimaryKeyBuilder.build(); |
| 87 | + assertEquals("no_primary_key", noPrimaryKeyColumn.getColumnName()); |
| 88 | + assertEquals(0, noPrimaryKeyColumn.getOrdinalPosition()); |
| 89 | + assertEquals(StarRocksUtils.DECIMAL, noPrimaryKeyColumn.getDataType()); |
| 90 | + assertEquals(Integer.valueOf(20), noPrimaryKeyColumn.getColumnSize().orElse(null)); |
| 91 | + assertEquals(Integer.valueOf(1), noPrimaryKeyColumn.getDecimalDigits().get()); |
| 92 | + assertTrue(noPrimaryKeyColumn.isNullable()); |
| 93 | + |
| 94 | + // Map to VARCHAR of StarRocks if column is DECIMAL type and primary key. |
| 95 | + StarRocksColumn.Builder primaryKeyBuilder = |
| 96 | + new StarRocksColumn.Builder().setColumnName("primary_key").setOrdinalPosition(1); |
| 97 | + new DecimalType(20, 1) |
| 98 | + .notNull() |
| 99 | + .accept(new StarRocksUtils.CdcDataTypeTransformer(true, primaryKeyBuilder)); |
| 100 | + StarRocksColumn primaryKeyColumn = primaryKeyBuilder.build(); |
| 101 | + assertEquals("primary_key", primaryKeyColumn.getColumnName()); |
| 102 | + assertEquals(1, primaryKeyColumn.getOrdinalPosition()); |
| 103 | + assertEquals(StarRocksUtils.VARCHAR, primaryKeyColumn.getDataType()); |
| 104 | + assertEquals(Integer.valueOf(22), primaryKeyColumn.getColumnSize().orElse(null)); |
| 105 | + assertTrue(!primaryKeyColumn.isNullable()); |
| 106 | + |
| 107 | + // Map to VARCHAR of StarRocks if column is DECIMAL type and primary key |
| 108 | + // DECIMAL(20,0) is common in cdc pipeline, for example, the upstream cdc source is unsigned |
| 109 | + // BIGINT. |
| 110 | + StarRocksColumn.Builder unsignedBigIntKeyBuilder = |
| 111 | + new StarRocksColumn.Builder().setColumnName("primary_key").setOrdinalPosition(1); |
| 112 | + new DecimalType(20, 0) |
| 113 | + .notNull() |
| 114 | + .accept(new StarRocksUtils.CdcDataTypeTransformer(true, unsignedBigIntKeyBuilder)); |
| 115 | + StarRocksColumn unsignedBigIntColumn = unsignedBigIntKeyBuilder.build(); |
| 116 | + assertEquals("primary_key", unsignedBigIntColumn.getColumnName()); |
| 117 | + assertEquals(1, unsignedBigIntColumn.getOrdinalPosition()); |
| 118 | + assertEquals(StarRocksUtils.VARCHAR, unsignedBigIntColumn.getDataType()); |
| 119 | + assertEquals(Integer.valueOf(21), unsignedBigIntColumn.getColumnSize().orElse(null)); |
| 120 | + assertTrue(!unsignedBigIntColumn.isNullable()); |
| 121 | + } |
| 122 | + |
78 | 123 | @Test
|
79 | 124 | public void testVarCharType() {
|
80 | 125 | // the length fo StarRocks should be 3 times as that of CDC if CDC length * 3 <=
|
|
0 commit comments