|
8 | 8 | import com.clickhouse.client.api.metadata.NoSuchColumnException; |
9 | 9 | import com.clickhouse.client.api.metadata.TableSchema; |
10 | 10 | import com.clickhouse.client.api.query.NullValueException; |
11 | | -import com.clickhouse.client.api.serde.POJOFieldDeserializer; |
12 | 11 | import com.clickhouse.client.api.query.QuerySettings; |
| 12 | +import com.clickhouse.client.api.serde.POJOFieldDeserializer; |
13 | 13 | import com.clickhouse.data.ClickHouseColumn; |
14 | 14 | import com.clickhouse.data.ClickHouseDataType; |
15 | 15 | import com.clickhouse.data.value.ClickHouseBitmap; |
|
24 | 24 | import java.io.IOException; |
25 | 25 | import java.io.InputStream; |
26 | 26 | import java.lang.ref.WeakReference; |
| 27 | +import java.lang.reflect.Array; |
27 | 28 | import java.math.BigDecimal; |
28 | 29 | import java.math.BigInteger; |
29 | 30 | import java.net.Inet4Address; |
@@ -524,56 +525,93 @@ public ClickHouseGeoMultiPolygonValue getGeoMultiPolygon(String colName) { |
524 | 525 |
|
525 | 526 | @Override |
526 | 527 | public <T> List<T> getList(String colName) { |
527 | | - try { |
528 | | - BinaryStreamReader.ArrayValue array = readValue(colName); |
529 | | - return array.asList(); |
530 | | - } catch (ClassCastException e) { |
531 | | - throw new ClientException("Column is not of array type", e); |
| 528 | + Object value = readValue(colName); |
| 529 | + if (value instanceof BinaryStreamReader.ArrayValue) { |
| 530 | + return ((BinaryStreamReader.ArrayValue) value).asList(); |
| 531 | + } else if (value instanceof List<?>) { |
| 532 | + return (List<T>) value; |
| 533 | + } else { |
| 534 | + throw new ClientException("Column is not of array type"); |
532 | 535 | } |
533 | 536 | } |
534 | 537 |
|
535 | 538 |
|
536 | | - private <T> T getPrimitiveArray(String colName) { |
| 539 | + private <T> T getPrimitiveArray(String colName, Class<?> componentType) { |
537 | 540 | try { |
538 | | - BinaryStreamReader.ArrayValue array = readValue(colName); |
539 | | - if (array.itemType.isPrimitive()) { |
540 | | - return (T) array.array; |
541 | | - } else { |
542 | | - throw new ClientException("Array is not of primitive type"); |
| 541 | + Object value = readValue(colName); |
| 542 | + if (value instanceof BinaryStreamReader.ArrayValue) { |
| 543 | + BinaryStreamReader.ArrayValue array = (BinaryStreamReader.ArrayValue) value; |
| 544 | + if (array.itemType.isPrimitive()) { |
| 545 | + return (T) array.array; |
| 546 | + } else { |
| 547 | + throw new ClientException("Array is not of primitive type"); |
| 548 | + } |
| 549 | + } else if (value instanceof List<?>) { |
| 550 | + List<?> list = (List<?>) value; |
| 551 | + Object array = Array.newInstance(componentType, list.size()); |
| 552 | + for (int i = 0; i < list.size(); i++) { |
| 553 | + Array.set(array, i, list.get(i)); |
| 554 | + } |
| 555 | + return (T)array; |
543 | 556 | } |
| 557 | + throw new ClientException("Column is not of array type"); |
544 | 558 | } catch (ClassCastException e) { |
545 | 559 | throw new ClientException("Column is not of array type", e); |
546 | 560 | } |
547 | 561 | } |
548 | 562 |
|
549 | 563 | @Override |
550 | 564 | public byte[] getByteArray(String colName) { |
551 | | - return getPrimitiveArray(colName); |
| 565 | + try { |
| 566 | + return getPrimitiveArray(colName, byte.class); |
| 567 | + } catch (ClassCastException | IllegalArgumentException e) { |
| 568 | + throw new ClientException("Value cannot be converted to an array of primitives", e); |
| 569 | + } |
552 | 570 | } |
553 | 571 |
|
554 | 572 | @Override |
555 | 573 | public int[] getIntArray(String colName) { |
556 | | - return getPrimitiveArray(colName); |
| 574 | + try { |
| 575 | + return getPrimitiveArray(colName, int.class); |
| 576 | + } catch (ClassCastException | IllegalArgumentException e) { |
| 577 | + throw new ClientException("Value cannot be converted to an array of primitives", e); |
| 578 | + } |
557 | 579 | } |
558 | 580 |
|
559 | 581 | @Override |
560 | 582 | public long[] getLongArray(String colName) { |
561 | | - return getPrimitiveArray(colName); |
| 583 | + try { |
| 584 | + return getPrimitiveArray(colName, long.class); |
| 585 | + } catch (ClassCastException | IllegalArgumentException e) { |
| 586 | + throw new ClientException("Value cannot be converted to an array of primitives", e); |
| 587 | + } |
562 | 588 | } |
563 | 589 |
|
564 | 590 | @Override |
565 | 591 | public float[] getFloatArray(String colName) { |
566 | | - return getPrimitiveArray(colName); |
| 592 | + try { |
| 593 | + return getPrimitiveArray(colName, float.class); |
| 594 | + } catch (ClassCastException | IllegalArgumentException e) { |
| 595 | + throw new ClientException("Value cannot be converted to an array of primitives", e); |
| 596 | + } |
567 | 597 | } |
568 | 598 |
|
569 | 599 | @Override |
570 | 600 | public double[] getDoubleArray(String colName) { |
571 | | - return getPrimitiveArray(colName); |
| 601 | + try { |
| 602 | + return getPrimitiveArray(colName, double.class); |
| 603 | + } catch (ClassCastException | IllegalArgumentException e) { |
| 604 | + throw new ClientException("Value cannot be converted to an array of primitives", e); |
| 605 | + } |
572 | 606 | } |
573 | 607 |
|
574 | 608 | @Override |
575 | 609 | public boolean[] getBooleanArray(String colName) { |
576 | | - return getPrimitiveArray(colName); |
| 610 | + try { |
| 611 | + return getPrimitiveArray(colName, boolean.class); |
| 612 | + } catch (ClassCastException | IllegalArgumentException e) { |
| 613 | + throw new ClientException("Value cannot be converted to an array of primitives", e); |
| 614 | + } |
577 | 615 | } |
578 | 616 |
|
579 | 617 | @Override |
@@ -693,32 +731,32 @@ public <T> List<T> getList(int index) { |
693 | 731 |
|
694 | 732 | @Override |
695 | 733 | public byte[] getByteArray(int index) { |
696 | | - return getPrimitiveArray(schema.columnIndexToName(index)); |
| 734 | + return getByteArray(schema.columnIndexToName(index)); |
697 | 735 | } |
698 | 736 |
|
699 | 737 | @Override |
700 | 738 | public int[] getIntArray(int index) { |
701 | | - return getPrimitiveArray(schema.columnIndexToName(index)); |
| 739 | + return getIntArray(schema.columnIndexToName(index)); |
702 | 740 | } |
703 | 741 |
|
704 | 742 | @Override |
705 | 743 | public long[] getLongArray(int index) { |
706 | | - return getPrimitiveArray(schema.columnIndexToName(index)); |
| 744 | + return getLongArray(schema.columnIndexToName(index)); |
707 | 745 | } |
708 | 746 |
|
709 | 747 | @Override |
710 | 748 | public float[] getFloatArray(int index) { |
711 | | - return getPrimitiveArray(schema.columnIndexToName(index)); |
| 749 | + return getFloatArray(schema.columnIndexToName(index)); |
712 | 750 | } |
713 | 751 |
|
714 | 752 | @Override |
715 | 753 | public double[] getDoubleArray(int index) { |
716 | | - return getPrimitiveArray(schema.columnIndexToName(index)); |
| 754 | + return getDoubleArray(schema.columnIndexToName(index)); |
717 | 755 | } |
718 | 756 |
|
719 | 757 | @Override |
720 | 758 | public boolean[] getBooleanArray(int index) { |
721 | | - return getPrimitiveArray(schema.columnIndexToName(index)); |
| 759 | + return getBooleanArray(schema.columnIndexToName(index)); |
722 | 760 | } |
723 | 761 |
|
724 | 762 | @Override |
|
0 commit comments