diff --git a/history/latest/3378.md b/history/latest/3378.md new file mode 100644 index 000000000..5eafa27c7 --- /dev/null +++ b/history/latest/3378.md @@ -0,0 +1,7 @@ +- **[jdbc-v2]** Fixed `ResultSet#getArray` and `ResultSet#getObject` for an array of a geo type, for example + `Array(Ring)`, `Array(Polygon)`, `Array(Geometry)`, `Array(Nullable(Point))` or `Array(Array(Ring))`. They threw + `SQLException` caused by a `NullPointerException` in the constructor of `com.clickhouse.jdbc.types.Array`. The + conversion tried to make a `java.sql.Array` of each geo element, but it had no column for the element. Now the + elements are kept as read. `getArray()` of the result is an `Object[]` of nested Java arrays, for example a + `double[][]` for each `Ring`, and `null` for a `NULL` element. `getBaseTypeName()` is the element type, for example + `Ring`, and `getBaseType()` is `Types.ARRAY`. (https://github.com/ClickHouse/clickhouse-java/issues/3378) diff --git a/jdbc-v2/src/main/java/com/clickhouse/jdbc/internal/JdbcUtils.java b/jdbc-v2/src/main/java/com/clickhouse/jdbc/internal/JdbcUtils.java index 31e23efc5..c848f6548 100644 --- a/jdbc-v2/src/main/java/com/clickhouse/jdbc/internal/JdbcUtils.java +++ b/jdbc-v2/src/main/java/com/clickhouse/jdbc/internal/JdbcUtils.java @@ -328,8 +328,13 @@ public static Object convert(Object value, Class type, ClickHouseColumn colum if (column != null && column.getArrayBaseColumn() != null) { ClickHouseColumn baseColumn = column.getArrayBaseColumn(); ClickHouseDataType baseType = baseColumn.isNested() ? ClickHouseDataType.Tuple : baseColumn.getDataType(); - Object[] convertedValues = convertList(listValue, convertToJavaClass(baseType), - getArrayDimensions(column)); + Class elementClass = convertToJavaClass(baseType); + if (elementClass == java.sql.Array.class) { + // A geo element such as a Ring is read as a Java array. There is no element column here + // to build an Array from, so the element is kept as read. + elementClass = Object.class; + } + Object[] convertedValues = convertList(listValue, elementClass, getArrayDimensions(column)); return new Array(column, convertedValues, multiDimensionalArrayElementType); } diff --git a/jdbc-v2/src/test/java/com/clickhouse/jdbc/JdbcDataTypeTests.java b/jdbc-v2/src/test/java/com/clickhouse/jdbc/JdbcDataTypeTests.java index 805a7a38b..9e86084aa 100644 --- a/jdbc-v2/src/test/java/com/clickhouse/jdbc/JdbcDataTypeTests.java +++ b/jdbc-v2/src/test/java/com/clickhouse/jdbc/JdbcDataTypeTests.java @@ -3400,6 +3400,60 @@ public void testGeoMultiPolygon() throws Exception { } } + @Test(groups = { "integration" }, dataProvider = "arrayOfGeoTypes") + public void testArrayOfGeoTypes(String type, String unsupportedVersions, String value, Object expected, + String baseTypeName) throws Exception { + if (isVersionMatch(unsupportedVersions)) { + throw new SkipException(type + " is not supported by this ClickHouse version"); + } + + final String table = "test_array_of_geo_types"; + try (Connection conn = getJdbcConnection(); Statement stmt = conn.createStatement()) { + stmt.executeUpdate("DROP TABLE IF EXISTS " + table); + stmt.executeUpdate("CREATE TABLE " + table + " (rowId Int32, geom " + type + ", tail Int64) ORDER BY ()"); + stmt.executeUpdate("INSERT INTO " + table + " VALUES (1, " + value + ", 42)"); + + try (ResultSet rs = stmt.executeQuery("SELECT * FROM " + table)) { + assertTrue(rs.next()); + assertEquals(rs.getInt("rowId"), 1); + Array asArray = rs.getArray("geom"); + assertEquals(asArray.getBaseTypeName(), baseTypeName); + assertEquals(asArray.getBaseType(), Types.ARRAY); + assertEquals(asArray.getArray(), expected); + Array asObject = (Array) rs.getObject("geom"); + assertEquals(asObject.getBaseTypeName(), baseTypeName); + assertEquals(asObject.getArray(), asArray.getArray()); + assertEquals(rs.getLong("tail"), 42L); + assertFalse(rs.next()); + } + } + } + + @DataProvider + public static Object[][] arrayOfGeoTypes() { + return new Object[][] { + {"Array(Ring)", null, "[[(1.5, 2.5), (3.5, 4.5)], []]", + new double[][][] {{{1.5, 2.5}, {3.5, 4.5}}, {}}, "Ring"}, + {"Array(LineString)", null, "[[(1.5, 2.5), (3.5, 4.5)]]", + new double[][][] {{{1.5, 2.5}, {3.5, 4.5}}}, "LineString"}, + {"Array(MultiPoint)", MULTI_POINT_UNSUPPORTED_VERSIONS, "[[(1.5, 2.5), (3.5, 4.5)]]", + new double[][][] {{{1.5, 2.5}, {3.5, 4.5}}}, "MultiPoint"}, + {"Array(Polygon)", null, "[[[(1.5, 2.5), (3.5, 4.5)]], [[(5.5, 6.5)]]]", + new double[][][][] {{{{1.5, 2.5}, {3.5, 4.5}}}, {{{5.5, 6.5}}}}, "Polygon"}, + {"Array(MultiLineString)", null, "[[[(1.5, 2.5), (3.5, 4.5)]], [[(5.5, 6.5)]]]", + new double[][][][] {{{{1.5, 2.5}, {3.5, 4.5}}}, {{{5.5, 6.5}}}}, "MultiLineString"}, + {"Array(MultiPolygon)", null, "[[[[(1.5, 2.5), (3.5, 4.5)]]], [[[(5.5, 6.5)]]]]", + new double[][][][][] {{{{{1.5, 2.5}, {3.5, 4.5}}}}, {{{{5.5, 6.5}}}}}, "MultiPolygon"}, + {"Array(Geometry)", "(,25.10]", "[[(1.5, 2.5), (3.5, 4.5)], (1.5, 2.5), NULL]", + new Object[] {new double[][] {{1.5, 2.5}, {3.5, 4.5}}, new double[] {1.5, 2.5}, null}, + "Geometry"}, + {"Array(Nullable(Point))", "(,26.8]", "[(1.5, 2.5), NULL]", + new Object[] {new double[] {1.5, 2.5}, null}, "Nullable(Point)"}, + {"Array(Array(Ring))", null, "[[[(1.5, 2.5), (3.5, 4.5)]], []]", + new double[][][][] {{{{1.5, 2.5}, {3.5, 4.5}}}, {}}, "Array(Ring)"}, + }; + } + @Test(groups = { "integration" }) public void testGeoGeometry() throws Exception { if (earlierThan(25, 11)) {