|
26 | 26 | import org.apache.seatunnel.api.table.type.SeaTunnelDataType; |
27 | 27 | import org.apache.seatunnel.api.table.type.SeaTunnelRow; |
28 | 28 | import org.apache.seatunnel.api.table.type.SeaTunnelRowType; |
| 29 | +import org.apache.seatunnel.api.configuration.ReadonlyConfig; |
29 | 30 | import org.apache.seatunnel.connectors.seatunnel.milvus.exception.MilvusConnectorException; |
30 | 31 | import org.junit.jupiter.api.Assertions; |
31 | 32 | import org.junit.jupiter.api.Test; |
32 | 33 |
|
33 | 34 | import java.time.LocalDateTime; |
34 | 35 | import java.time.OffsetDateTime; |
| 36 | +import java.time.ZoneId; |
35 | 37 | import java.time.ZoneOffset; |
36 | 38 | import java.util.HashMap; |
37 | 39 | import java.util.List; |
| 40 | +import java.util.Map; |
38 | 41 |
|
39 | 42 | public class MilvusSinkConverterTest { |
40 | 43 |
|
@@ -134,6 +137,172 @@ public void testConvertByMilvusType_Timestamptz_InvalidStringFallback() { |
134 | 137 | Assertions.assertEquals("not-a-date", result); |
135 | 138 | } |
136 | 139 |
|
| 140 | + // --- Per-field timezone handling for Timestamptz --- |
| 141 | + |
| 142 | + @Test |
| 143 | + public void testConvertByMilvusType_Timestamptz_LocalDateTime_PerFieldShanghai() { |
| 144 | + Map<String, ZoneId> overrides = new HashMap<>(); |
| 145 | + overrides.put("ts", ZoneId.of("Asia/Shanghai")); |
| 146 | + MilvusSinkConverter shanghaiConverter = new MilvusSinkConverter( |
| 147 | + GeometryConverter.PASSTHROUGH, overrides); |
| 148 | + FieldSchema schema = FieldSchema.builder() |
| 149 | + .name("ts").dataType(DataType.Timestamptz).build(); |
| 150 | + LocalDateTime ldt = LocalDateTime.of(2024, 1, 19, 10, 0, 0); |
| 151 | + Object result = shanghaiConverter.convertByMilvusType(schema, ldt); |
| 152 | + // 10:00 Shanghai = 02:00 UTC |
| 153 | + Assertions.assertEquals("2024-01-19T02:00:00Z", result); |
| 154 | + } |
| 155 | + |
| 156 | + @Test |
| 157 | + public void testConvertByMilvusType_Timestamptz_LocalDateTime_PerFieldUtc() { |
| 158 | + Map<String, ZoneId> overrides = new HashMap<>(); |
| 159 | + overrides.put("ts", ZoneId.of("UTC")); |
| 160 | + MilvusSinkConverter utcConverter = new MilvusSinkConverter( |
| 161 | + GeometryConverter.PASSTHROUGH, overrides); |
| 162 | + FieldSchema schema = FieldSchema.builder() |
| 163 | + .name("ts").dataType(DataType.Timestamptz).build(); |
| 164 | + LocalDateTime ldt = LocalDateTime.of(2024, 1, 19, 10, 0, 0); |
| 165 | + Object result = utcConverter.convertByMilvusType(schema, ldt); |
| 166 | + Assertions.assertEquals("2024-01-19T10:00:00Z", result); |
| 167 | + } |
| 168 | + |
| 169 | + @Test |
| 170 | + public void testConvertByMilvusType_Timestamptz_LocalDateTime_PerFieldUtcOffset() { |
| 171 | + // UTC offset format "+08:00" should work the same as IANA "Asia/Shanghai" |
| 172 | + Map<String, ZoneId> overrides = new HashMap<>(); |
| 173 | + overrides.put("ts", ZoneId.of("+08:00")); |
| 174 | + MilvusSinkConverter offsetConverter = new MilvusSinkConverter( |
| 175 | + GeometryConverter.PASSTHROUGH, overrides); |
| 176 | + FieldSchema schema = FieldSchema.builder() |
| 177 | + .name("ts").dataType(DataType.Timestamptz).build(); |
| 178 | + LocalDateTime ldt = LocalDateTime.of(2024, 1, 19, 10, 0, 0); |
| 179 | + Object result = offsetConverter.convertByMilvusType(schema, ldt); |
| 180 | + // 10:00 +08:00 = 02:00 UTC |
| 181 | + Assertions.assertEquals("2024-01-19T02:00:00Z", result); |
| 182 | + } |
| 183 | + |
| 184 | + @Test |
| 185 | + public void testConvertByMilvusType_Timestamptz_LocalDateTime_NoOverrideFallsBackToSystemDefault() { |
| 186 | + // No per-field override → systemDefault |
| 187 | + FieldSchema schema = FieldSchema.builder() |
| 188 | + .name("ts").dataType(DataType.Timestamptz).build(); |
| 189 | + LocalDateTime ldt = LocalDateTime.of(2024, 1, 19, 10, 0, 0); |
| 190 | + Object result = converter.convertByMilvusType(schema, ldt); |
| 191 | + String expected = ldt.atZone(ZoneId.systemDefault()).toInstant().toString(); |
| 192 | + Assertions.assertEquals(expected, result); |
| 193 | + } |
| 194 | + |
| 195 | + @Test |
| 196 | + public void testConvertByMilvusType_Timestamptz_MultipleFieldsDifferentZones() { |
| 197 | + Map<String, ZoneId> overrides = new HashMap<>(); |
| 198 | + overrides.put("created_at", ZoneId.of("Asia/Shanghai")); |
| 199 | + overrides.put("updated_at", ZoneId.of("US/Eastern")); |
| 200 | + MilvusSinkConverter multiConverter = new MilvusSinkConverter( |
| 201 | + GeometryConverter.PASSTHROUGH, overrides); |
| 202 | + |
| 203 | + LocalDateTime ldt = LocalDateTime.of(2024, 6, 15, 12, 0, 0); |
| 204 | + |
| 205 | + FieldSchema createdSchema = FieldSchema.builder() |
| 206 | + .name("created_at").dataType(DataType.Timestamptz).build(); |
| 207 | + Object createdResult = multiConverter.convertByMilvusType(createdSchema, ldt); |
| 208 | + // 12:00 Shanghai (+8) = 04:00 UTC |
| 209 | + Assertions.assertEquals("2024-06-15T04:00:00Z", createdResult); |
| 210 | + |
| 211 | + FieldSchema updatedSchema = FieldSchema.builder() |
| 212 | + .name("updated_at").dataType(DataType.Timestamptz).build(); |
| 213 | + Object updatedResult = multiConverter.convertByMilvusType(updatedSchema, ldt); |
| 214 | + // 12:00 US/Eastern (EDT = -4 in June) = 16:00 UTC |
| 215 | + Assertions.assertEquals("2024-06-15T16:00:00Z", updatedResult); |
| 216 | + } |
| 217 | + |
| 218 | + @Test |
| 219 | + public void testFromConfig_FieldSchemaTimezone_WiredToConverter() { |
| 220 | + // Simulate field_schema config with timezone |
| 221 | + Map<String, Object> fieldEntry = new HashMap<>(); |
| 222 | + fieldEntry.put("field_name", "event_time"); |
| 223 | + fieldEntry.put("data_type", 26); // Timestamptz |
| 224 | + fieldEntry.put("timezone", "Asia/Shanghai"); |
| 225 | + |
| 226 | + List<Object> fieldSchemaList = new java.util.ArrayList<>(); |
| 227 | + fieldSchemaList.add(fieldEntry); |
| 228 | + |
| 229 | + Map<String, Object> configMap = new HashMap<>(); |
| 230 | + configMap.put("field_schema", fieldSchemaList); |
| 231 | + ReadonlyConfig config = ReadonlyConfig.fromMap(configMap); |
| 232 | + MilvusSinkConverter shanghaiConverter = MilvusSinkConverter.fromConfig(config); |
| 233 | + |
| 234 | + FieldSchema schema = FieldSchema.builder() |
| 235 | + .name("event_time").dataType(DataType.Timestamptz).build(); |
| 236 | + LocalDateTime ldt = LocalDateTime.of(2024, 1, 19, 10, 0, 0); |
| 237 | + Object result = shanghaiConverter.convertByMilvusType(schema, ldt); |
| 238 | + // 10:00 Shanghai = 02:00 UTC |
| 239 | + Assertions.assertEquals("2024-01-19T02:00:00Z", result); |
| 240 | + } |
| 241 | + |
| 242 | + @Test |
| 243 | + public void testConvertByMilvusType_Timestamptz_String_WithOverrideUsesConfiguredTz() { |
| 244 | + // String input + per-field timezone → parse as naive wall-clock, apply configured tz |
| 245 | + Map<String, ZoneId> overrides = new HashMap<>(); |
| 246 | + overrides.put("ts", ZoneId.of("Asia/Shanghai")); |
| 247 | + MilvusSinkConverter shanghaiConverter = new MilvusSinkConverter( |
| 248 | + GeometryConverter.PASSTHROUGH, overrides); |
| 249 | + FieldSchema schema = FieldSchema.builder() |
| 250 | + .name("ts").dataType(DataType.Timestamptz).build(); |
| 251 | + Object result = shanghaiConverter.convertByMilvusType(schema, "2024-01-19 10:00:00"); |
| 252 | + // 10:00 Shanghai = 02:00 UTC |
| 253 | + Assertions.assertEquals("2024-01-19T02:00:00Z", result); |
| 254 | + } |
| 255 | + |
| 256 | + @Test |
| 257 | + public void testConvertByMilvusType_Timestamptz_String_WithoutOverrideUsesOriginalBehavior() { |
| 258 | + // String input + no per-field timezone → original Timestamp.valueOf behavior |
| 259 | + FieldSchema schema = FieldSchema.builder() |
| 260 | + .name("ts").dataType(DataType.Timestamptz).build(); |
| 261 | + Object result = converter.convertByMilvusType(schema, "2024-01-19 10:00:00"); |
| 262 | + Assertions.assertInstanceOf(String.class, result); |
| 263 | + Assertions.assertTrue(result.toString().contains("2024-01-19")); |
| 264 | + } |
| 265 | + |
| 266 | + @Test |
| 267 | + public void testConvertByMilvusType_Timestamptz_String_Iso8601PassedThrough() { |
| 268 | + // ISO 8601 with offset — should pass through even with per-field timezone |
| 269 | + Map<String, ZoneId> overrides = new HashMap<>(); |
| 270 | + overrides.put("ts", ZoneId.of("Asia/Shanghai")); |
| 271 | + MilvusSinkConverter shanghaiConverter = new MilvusSinkConverter( |
| 272 | + GeometryConverter.PASSTHROUGH, overrides); |
| 273 | + FieldSchema schema = FieldSchema.builder() |
| 274 | + .name("ts").dataType(DataType.Timestamptz).build(); |
| 275 | + Object result = shanghaiConverter.convertByMilvusType(schema, "2024-01-19T10:00:00Z"); |
| 276 | + // Already has offset Z — LocalDateTime.parse fails, falls through as-is |
| 277 | + Assertions.assertEquals("2024-01-19T10:00:00Z", result); |
| 278 | + } |
| 279 | + |
| 280 | + @Test |
| 281 | + public void testConvertByMilvusType_Timestamptz_OffsetDateTime_IgnoresPerFieldTimezone() { |
| 282 | + // OffsetDateTime already carries offset — per-field timezone must NOT affect it |
| 283 | + Map<String, ZoneId> overrides = new HashMap<>(); |
| 284 | + overrides.put("ts", ZoneId.of("Asia/Shanghai")); |
| 285 | + MilvusSinkConverter shanghaiConverter = new MilvusSinkConverter( |
| 286 | + GeometryConverter.PASSTHROUGH, overrides); |
| 287 | + FieldSchema schema = FieldSchema.builder() |
| 288 | + .name("ts").dataType(DataType.Timestamptz).build(); |
| 289 | + OffsetDateTime odt = OffsetDateTime.of(2024, 1, 19, 10, 0, 0, 0, ZoneOffset.ofHours(5)); |
| 290 | + Object result = shanghaiConverter.convertByMilvusType(schema, odt); |
| 291 | + // 10:00+05 = 05:00 UTC, regardless of per-field timezone setting |
| 292 | + Assertions.assertEquals("2024-01-19T05:00:00Z", result); |
| 293 | + } |
| 294 | + |
| 295 | + @Test |
| 296 | + public void testConvertBySeaTunnelType_TimestampTz_LocalDateTime_UsesSystemDefault() { |
| 297 | + // convertBySeaTunnelType has no field name, always uses systemDefault |
| 298 | + LocalDateTime ldt = LocalDateTime.of(2024, 1, 19, 10, 0, 0); |
| 299 | + Object result = converter.convertBySeaTunnelType( |
| 300 | + LocalTimeType.OFFSET_DATE_TIME_TYPE, false, ldt); |
| 301 | + Assertions.assertInstanceOf(String.class, result); |
| 302 | + String expected = ldt.atZone(ZoneId.systemDefault()).toInstant().toString(); |
| 303 | + Assertions.assertEquals(expected, result); |
| 304 | + } |
| 305 | + |
137 | 306 | // --- Consistency: both paths return same type for same input --- |
138 | 307 |
|
139 | 308 | @Test |
|
0 commit comments