|
24 | 24 | import org.apache.fluss.row.TimestampLtz; |
25 | 25 | import org.apache.fluss.row.TimestampNtz; |
26 | 26 | import org.apache.fluss.types.DataType; |
27 | | -import org.apache.fluss.types.DataTypeRoot; |
28 | 27 | import org.apache.fluss.types.DataTypeChecks; |
| 28 | +import org.apache.fluss.types.DataTypeRoot; |
29 | 29 | import org.apache.fluss.types.DecimalType; |
30 | 30 | import org.apache.fluss.types.RowType; |
31 | 31 |
|
@@ -391,7 +391,26 @@ private RowToFieldConverter createRowToFieldConverter(DataType fieldType, Field |
391 | 391 | return null; |
392 | 392 | } |
393 | 393 | BinaryString binaryString = row.getString(pos); |
394 | | - return binaryString.toString(); |
| 394 | + String value = binaryString.toString(); |
| 395 | + if (fieldClass == String.class) { |
| 396 | + return value; |
| 397 | + } else if (fieldClass == Character.class || fieldClass == char.class) { |
| 398 | + if (value.isEmpty()) { |
| 399 | + throw new IllegalArgumentException( |
| 400 | + String.format( |
| 401 | + "Field %s expects Character/char, but the string value is empty.", |
| 402 | + field.getName())); |
| 403 | + } |
| 404 | + return value.charAt(0); |
| 405 | + } else { |
| 406 | + // This should normally be prevented by constructor-time validation of |
| 407 | + // supported types. |
| 408 | + // Keep a defensive check here for clarity. |
| 409 | + throw new IllegalArgumentException( |
| 410 | + String.format( |
| 411 | + "Field %s is not a String or Character/char. Cannot convert from string.", |
| 412 | + field.getName())); |
| 413 | + } |
395 | 414 | }; |
396 | 415 | case BINARY: |
397 | 416 | case BYTES: |
@@ -422,35 +441,38 @@ private RowToFieldConverter createRowToFieldConverter(DataType fieldType, Field |
422 | 441 | int millis = row.getInt(pos); |
423 | 442 | return LocalTime.ofNanoOfDay(millis * 1_000_000L); |
424 | 443 | }; |
425 | | - case TIMESTAMP_WITHOUT_TIME_ZONE: { |
426 | | - final int precision = DataTypeChecks.getPrecision(fieldType); |
427 | | - return (row, pos) -> { |
428 | | - if (row.isNullAt(pos)) { |
429 | | - return null; |
430 | | - } |
431 | | - TimestampNtz timestampNtz = row.getTimestampNtz(pos, precision); |
432 | | - return timestampNtz.toLocalDateTime(); |
433 | | - }; |
434 | | - } |
435 | | - case TIMESTAMP_WITH_LOCAL_TIME_ZONE: { |
436 | | - final int precision = DataTypeChecks.getPrecision(fieldType); |
437 | | - return (row, pos) -> { |
438 | | - if (row.isNullAt(pos)) { |
439 | | - return null; |
440 | | - } |
441 | | - TimestampLtz timestampLtz = row.getTimestampLtz(pos, precision); |
442 | | - if (fieldClass == Instant.class) { |
443 | | - return timestampLtz.toInstant(); |
444 | | - } else if (fieldClass == OffsetDateTime.class) { |
445 | | - return OffsetDateTime.ofInstant(timestampLtz.toInstant(), ZoneOffset.UTC); |
446 | | - } else { |
447 | | - throw new IllegalArgumentException( |
448 | | - String.format( |
449 | | - "Field %s is not an Instant or OffsetDateTime. Cannot convert from TimestampData.", |
450 | | - field.getName())); |
451 | | - } |
452 | | - }; |
453 | | - } |
| 444 | + case TIMESTAMP_WITHOUT_TIME_ZONE: |
| 445 | + { |
| 446 | + final int precision = DataTypeChecks.getPrecision(fieldType); |
| 447 | + return (row, pos) -> { |
| 448 | + if (row.isNullAt(pos)) { |
| 449 | + return null; |
| 450 | + } |
| 451 | + TimestampNtz timestampNtz = row.getTimestampNtz(pos, precision); |
| 452 | + return timestampNtz.toLocalDateTime(); |
| 453 | + }; |
| 454 | + } |
| 455 | + case TIMESTAMP_WITH_LOCAL_TIME_ZONE: |
| 456 | + { |
| 457 | + final int precision = DataTypeChecks.getPrecision(fieldType); |
| 458 | + return (row, pos) -> { |
| 459 | + if (row.isNullAt(pos)) { |
| 460 | + return null; |
| 461 | + } |
| 462 | + TimestampLtz timestampLtz = row.getTimestampLtz(pos, precision); |
| 463 | + if (fieldClass == Instant.class) { |
| 464 | + return timestampLtz.toInstant(); |
| 465 | + } else if (fieldClass == OffsetDateTime.class) { |
| 466 | + return OffsetDateTime.ofInstant( |
| 467 | + timestampLtz.toInstant(), ZoneOffset.UTC); |
| 468 | + } else { |
| 469 | + throw new IllegalArgumentException( |
| 470 | + String.format( |
| 471 | + "Field %s is not an Instant or OffsetDateTime. Cannot convert from TimestampData.", |
| 472 | + field.getName())); |
| 473 | + } |
| 474 | + }; |
| 475 | + } |
454 | 476 | default: |
455 | 477 | throw new UnsupportedOperationException( |
456 | 478 | String.format( |
@@ -538,7 +560,6 @@ public T fromRow(InternalRow row) { |
538 | 560 | } |
539 | 561 | } |
540 | 562 |
|
541 | | - |
542 | 563 | /** |
543 | 564 | * Utility method to create a Set containing the specified Java type classes. |
544 | 565 | * |
|
0 commit comments