|
18 | 18 | package org.apache.seatunnel.connectors.seatunnel.clickhouse.util; |
19 | 19 |
|
20 | 20 | import org.apache.seatunnel.api.table.catalog.Column; |
| 21 | +import org.apache.seatunnel.api.table.catalog.PhysicalColumn; |
| 22 | +import org.apache.seatunnel.api.table.catalog.PrimaryKey; |
| 23 | +import org.apache.seatunnel.api.table.catalog.TableSchema; |
21 | 24 | import org.apache.seatunnel.api.table.type.BasicType; |
22 | 25 | import org.apache.seatunnel.api.table.type.SeaTunnelDataType; |
| 26 | +import org.apache.seatunnel.connectors.seatunnel.clickhouse.config.ClickhouseSinkOptions; |
23 | 27 |
|
24 | 28 | import org.junit.jupiter.api.Test; |
25 | 29 |
|
| 30 | +import java.util.ArrayList; |
| 31 | +import java.util.Arrays; |
| 32 | +import java.util.Collections; |
| 33 | +import java.util.List; |
| 34 | + |
26 | 35 | import static org.junit.jupiter.api.Assertions.assertEquals; |
27 | 36 | import static org.junit.jupiter.api.Assertions.assertThrows; |
28 | 37 | import static org.mockito.Mockito.mock; |
@@ -101,4 +110,74 @@ void throwsExceptionWhenColumnIsNull() { |
101 | 110 | NullPointerException.class, |
102 | 111 | () -> ClickhouseCatalogUtil.INSTANCE.columnToConnectorType(null)); |
103 | 112 | } |
| 113 | + |
| 114 | + @Test |
| 115 | + void testPrimaryKeyColumnShouldNotBeNullable() { |
| 116 | + // Test that ThreadLocal is properly cleared after getCreateTableSql call |
| 117 | + Column column = mock(Column.class); |
| 118 | + when(column.getName()).thenReturn("pk_column"); |
| 119 | + when(column.getSinkType()).thenReturn("String"); |
| 120 | + when(column.isNullable()).thenReturn(true); |
| 121 | + when(column.getComment()).thenReturn(""); |
| 122 | + |
| 123 | + List<Column> columns = new ArrayList<>(); |
| 124 | + columns.add(column); |
| 125 | + |
| 126 | + TableSchema tableSchema = |
| 127 | + TableSchema.builder() |
| 128 | + .primaryKey(PrimaryKey.of("", Collections.singletonList("pk_column"))) |
| 129 | + .columns(columns) |
| 130 | + .build(); |
| 131 | + |
| 132 | + ClickhouseCatalogUtil.INSTANCE.getCreateTableSql( |
| 133 | + "CREATE TABLE `${database}`.`${table}` (${rowtype_fields})", |
| 134 | + "test_db", |
| 135 | + "test_table", |
| 136 | + tableSchema, |
| 137 | + null, |
| 138 | + ClickhouseSinkOptions.SAVE_MODE_CREATE_TEMPLATE.key()); |
| 139 | + |
| 140 | + // After getCreateTableSql call, ThreadLocal should be cleared |
| 141 | + // so columnToConnectorType should treat it as NOT a primary key |
| 142 | + String result = ClickhouseCatalogUtil.INSTANCE.columnToConnectorType(column); |
| 143 | + assertEquals("`pk_column` Nullable(String) ", result); |
| 144 | + } |
| 145 | + |
| 146 | + @Test |
| 147 | + void testPrimaryKeyColumnWithNullableShouldNotWrapInNullable() { |
| 148 | + // Test the actual scenario: primary key columns should NOT be wrapped in Nullable |
| 149 | + // because ClickHouse doesn't allow nullable columns in ORDER BY / PRIMARY KEY |
| 150 | + String template = |
| 151 | + "CREATE TABLE `${database}`.`${table}` (\n" |
| 152 | + + " ${rowtype_primary_key},\n" |
| 153 | + + " ${rowtype_fields}\n" |
| 154 | + + ") ENGINE = MergeTree()\n" |
| 155 | + + "ORDER BY (${rowtype_primary_key})"; |
| 156 | + |
| 157 | + List<Column> columns = new ArrayList<>(); |
| 158 | + columns.add(PhysicalColumn.of("id", BasicType.LONG_TYPE, (Long) null, true, null, "")); |
| 159 | + columns.add(PhysicalColumn.of("name", BasicType.STRING_TYPE, (Long) null, true, null, "")); |
| 160 | + columns.add(PhysicalColumn.of("age", BasicType.INT_TYPE, (Long) null, true, null, "")); |
| 161 | + |
| 162 | + TableSchema tableSchema = |
| 163 | + TableSchema.builder() |
| 164 | + .primaryKey(PrimaryKey.of("", Arrays.asList("id", "age"))) |
| 165 | + .columns(columns) |
| 166 | + .build(); |
| 167 | + |
| 168 | + String sql = |
| 169 | + ClickhouseCatalogUtil.INSTANCE.getCreateTableSql( |
| 170 | + template, |
| 171 | + "test_db", |
| 172 | + "test_table", |
| 173 | + tableSchema, |
| 174 | + null, |
| 175 | + ClickhouseSinkOptions.SAVE_MODE_CREATE_TEMPLATE.key()); |
| 176 | + |
| 177 | + // Primary key columns (id, age) should NOT be wrapped in Nullable |
| 178 | + assertEquals(true, sql.contains("`id` Int64 ")); |
| 179 | + assertEquals(true, sql.contains("`age` Int32 ")); |
| 180 | + // Non-primary key column (name) should be wrapped in Nullable |
| 181 | + assertEquals(true, sql.contains("`name` Nullable(String) ")); |
| 182 | + } |
104 | 183 | } |
0 commit comments