diff --git a/history/latest/3216.md b/history/latest/3216.md new file mode 100644 index 000000000..eff625efb --- /dev/null +++ b/history/latest/3216.md @@ -0,0 +1,14 @@ +- **[jdbc-v2]** Fixed `java.sql.Array#getResultSet()` and `getResultSet(long, int)` for an array from + `Connection#createArrayOf` and for a geo value read with `ResultSet#getArray`. For a scalar element type such as + `Int32`, `Nullable(String)` or `Date`, and for `Point`, `Ring`, `LineString`, `MultiPoint`, `Polygon`, + `MultiLineString` and `MultiPolygon` values, they threw an unchecked `IndexOutOfBoundsException`. For a `Tuple` or + `Map` element type, the `VALUE` column had the type of the first tuple field or of the map key, so `getObject(2)` + failed. Now the `VALUE` column of an array from `createArrayOf` has the element type, the same as the `VALUE` column + of an `Array(T)` value read from the server. The `VALUE` column of a geo value has the geo element type, the one + that ClickHouse 26.10 returns for `x[1]`. `Ring`, `LineString` and `MultiPoint` have `Point` elements, `Polygon` has + `Ring` elements, `MultiLineString` has `LineString` elements, `MultiPolygon` has `Polygon` elements, and a `Point` + has `Float64` coordinates. `getBaseTypeName()` does not change. A `Tuple` or `Geometry` value read with `getArray` + now reports its own type for the `VALUE` column, the same as `getBaseTypeName()`, and not the type of its first + field or variant. An array from `createArrayOf` with a geo element type such as `Ring` holds whole `Ring` values, so + its `VALUE` column is `Ring`, the same as for `Array(Ring)`. + (https://github.com/ClickHouse/clickhouse-java/issues/3216) diff --git a/jdbc-v2/src/main/java/com/clickhouse/jdbc/ConnectionImpl.java b/jdbc-v2/src/main/java/com/clickhouse/jdbc/ConnectionImpl.java index 35542a0ec..2e08a49c8 100644 --- a/jdbc-v2/src/main/java/com/clickhouse/jdbc/ConnectionImpl.java +++ b/jdbc-v2/src/main/java/com/clickhouse/jdbc/ConnectionImpl.java @@ -640,13 +640,34 @@ public Array createArrayOf(String typeName, Object[] elements) throws SQLExcepti ClickHouseColumn column = ClickHouseColumn.of("array", typeName); try { - return new com.clickhouse.jdbc.types.Array(column, elements, + return new com.clickhouse.jdbc.types.Array(arrayOfColumn(column), elements, config.isFlagSet(DriverProperties.MULTI_DIMENSIONAL_ARRAY_ELEMENT_TYPE)); } catch (Exception e) { throw new SQLException("Failed to create array", ExceptionUtils.SQL_STATE_CLIENT_ERROR, e); } } + /** + * A geo value such as {@code Ring} is read as an array of its parts. An array that {@code createArrayOf} + * creates with a geo element type holds whole geo values, so it gets the column of {@code Array(T)}, the + * same as an array of that type read from the server. + */ + private static ClickHouseColumn arrayOfColumn(ClickHouseColumn elementColumn) { + switch (elementColumn.getDataType()) { + case Point: + case Ring: + case LineString: + case MultiPoint: + case Polygon: + case MultiLineString: + case MultiPolygon: + return ClickHouseColumn.of(elementColumn.getColumnName(), + "Array(" + elementColumn.getOriginalTypeName() + ")"); + default: + return elementColumn; + } + } + @Override public Struct createStruct(String typeName, Object[] attributes) throws SQLException { ensureOpen(); diff --git a/jdbc-v2/src/main/java/com/clickhouse/jdbc/types/ArrayResultSet.java b/jdbc-v2/src/main/java/com/clickhouse/jdbc/types/ArrayResultSet.java index 31074d3e3..1b131e06a 100644 --- a/jdbc-v2/src/main/java/com/clickhouse/jdbc/types/ArrayResultSet.java +++ b/jdbc-v2/src/main/java/com/clickhouse/jdbc/types/ArrayResultSet.java @@ -31,6 +31,7 @@ import java.sql.Timestamp; import java.util.Arrays; import java.util.Calendar; +import java.util.EnumMap; import java.util.List; import java.util.Map; import java.util.function.Function; @@ -46,6 +47,7 @@ public class ArrayResultSet implements ResultSet { private static final ClickHouseColumn INDEX_COLUMN = ClickHouseColumn.of("INDEX", ClickHouseDataType.UInt32, false, 0, 0); private static final String VALUE_COLUMN = "VALUE"; private static final ValueConverters defaultValueConverters = new ValueConverters(); + private static final Map GEO_ELEMENT_COLUMNS = geoElementColumns(); private int fetchDirection = ResultSet.FETCH_FORWARD; private int fetchSize = 0; @@ -88,7 +90,11 @@ public ArrayResultSet(Object array, ClickHouseColumn column, boolean multiDimens * Resolves the type of a single element of {@code column} (an array-like column). A * {@code Nested(f1 T1, ..., fN TN)} column is read as an array whose elements are * {@code Tuple(f1 T1, ..., fN TN)}, so its element is that tuple rather than the first nested - * field; every other array-like column keeps its existing base/nested-column resolution. + * field. A column without an array level is the element type itself, the same as in + * {@link com.clickhouse.jdbc.types.Array}, because {@code Connection#createArrayOf} creates the + * column from the element type. Geo columns are the exception. A geo value is read as an array, + * so its element is the type that the server returns for an element, for example {@code Point} + * for {@code Ring} and {@code Float64} for {@code Point}. */ private static ClickHouseColumn elementColumn(ClickHouseColumn column) { List nestedColumns = column.getNestedColumns(); @@ -104,9 +110,27 @@ private static ClickHouseColumn elementColumn(ClickHouseColumn column) { tupleType.append(')'); return ClickHouseColumn.of(VALUE_COLUMN, tupleType.toString()); } + if (column.getArrayNestedLevel() == 0) { + return GEO_ELEMENT_COLUMNS.getOrDefault(column.getDataType(), column); + } return column.getArrayNestedLevel() == 1 ? column.getArrayBaseColumn() : nestedColumns.get(0); } + private static Map geoElementColumns() { + Map columns = new EnumMap<>(ClickHouseDataType.class); + columns.put(ClickHouseDataType.Point, ClickHouseColumn.of(VALUE_COLUMN, ClickHouseDataType.Float64.name())); + ClickHouseColumn point = ClickHouseColumn.of(VALUE_COLUMN, ClickHouseDataType.Point.name()); + columns.put(ClickHouseDataType.Ring, point); + columns.put(ClickHouseDataType.LineString, point); + columns.put(ClickHouseDataType.MultiPoint, point); + columns.put(ClickHouseDataType.Polygon, ClickHouseColumn.of(VALUE_COLUMN, ClickHouseDataType.Ring.name())); + columns.put(ClickHouseDataType.MultiLineString, + ClickHouseColumn.of(VALUE_COLUMN, ClickHouseDataType.LineString.name())); + columns.put(ClickHouseDataType.MultiPolygon, + ClickHouseColumn.of(VALUE_COLUMN, ClickHouseDataType.Polygon.name())); + return columns; + } + @Override public boolean next() throws SQLException { if (pos + 1 >= length) { diff --git a/jdbc-v2/src/test/java/com/clickhouse/jdbc/ConnectionTest.java b/jdbc-v2/src/test/java/com/clickhouse/jdbc/ConnectionTest.java index d36e0691f..b4fb38352 100644 --- a/jdbc-v2/src/test/java/com/clickhouse/jdbc/ConnectionTest.java +++ b/jdbc-v2/src/test/java/com/clickhouse/jdbc/ConnectionTest.java @@ -35,6 +35,7 @@ import java.sql.JDBCType; import java.sql.PreparedStatement; import java.sql.ResultSet; +import java.sql.ResultSetMetaData; import java.sql.SQLClientInfoException; import java.sql.SQLException; import java.sql.SQLFeatureNotSupportedException; @@ -782,6 +783,83 @@ public static Object[][] multiDimensionalArrayElementTypeFlags() { }; } + @Test(groups = {"integration"}, dataProvider = "createArrayOfElementTypes") + public void testCreateArrayOfResultSetMatchesReadArray(String typeName, Object[] elements, String literal) + throws Exception { + try (Connection conn = getJdbcConnection(); Statement stmt = conn.createStatement(); + ResultSet rs = stmt.executeQuery("SELECT " + literal + "::Array(" + typeName + ")")) { + assertTrue(rs.next()); + ResultSet expected = rs.getArray(1).getResultSet(); + Array array = conn.createArrayOf(typeName, elements); + ResultSet actual = array.getResultSet(); + + ResultSetMetaData expectedMetaData = expected.getMetaData(); + ResultSetMetaData actualMetaData = actual.getMetaData(); + assertEquals(actualMetaData.getColumnTypeName(2), typeName); + assertEquals(actualMetaData.getColumnTypeName(2), expectedMetaData.getColumnTypeName(2)); + assertEquals(actualMetaData.getColumnType(2), expectedMetaData.getColumnType(2)); + assertEquals(actualMetaData.getColumnClassName(2), expectedMetaData.getColumnClassName(2)); + assertEquals(array.getResultSet(0, elements.length).getMetaData().getColumnTypeName(2), typeName); + + for (int i = 0; i < elements.length; i++) { + assertTrue(expected.next()); + assertTrue(actual.next()); + assertEquals(actual.getInt(1), expected.getInt(1)); + assertEquals(actual.getObject(2), expected.getObject(2)); + } + assertFalse(expected.next()); + assertFalse(actual.next()); + assertEquals(expectThrows(SQLException.class, () -> actual.getObject(2)).getMessage(), "No current row"); + } + } + + @DataProvider + public static Object[][] createArrayOfElementTypes() { + return new Object[][] { + {"Int32", new Integer[] {1, -2}, "[1, -2]"}, + {"Int32", new Integer[] {}, "[]"}, + {"Nullable(String)", new String[] {"a", null}, "['a', NULL]"}, + {"LowCardinality(String)", new String[] {"a", "b"}, "['a', 'b']"}, + {"Date", new Date[] {Date.valueOf("2024-01-02")}, "['2024-01-02']"}, + {"Tuple(String, Int8)", new Object[] {new Object[] {"a", (byte) 1}, new Object[] {"b", (byte) 2}}, + "[('a', 1), ('b', 2)]"}, + {"Map(String, Int32)", new Object[] {Collections.singletonMap("k", 1)}, "[map('k', 1)]"}, + }; + } + + @Test(groups = {"integration"}, dataProvider = "createArrayOfGeoElementTypes") + public void testCreateArrayOfGeoElementType(String typeName, Object[] elements, String partTypeName) + throws Exception { + try (Connection conn = getJdbcConnection()) { + Array array = conn.createArrayOf(typeName, elements); + assertEquals(array.getBaseTypeName(), typeName); + assertEquals(array.getResultSet(0, elements.length).getMetaData().getColumnTypeName(2), typeName); + + ResultSet rs = array.getResultSet(); + assertEquals(rs.getMetaData().getColumnTypeName(2), typeName); + for (int i = 0; i < elements.length; i++) { + assertTrue(rs.next()); + assertEquals(rs.getInt(1), i + 1); + Array element = rs.getArray(2); + assertEquals(element.getBaseTypeName(), typeName); + assertEquals(element.getResultSet().getMetaData().getColumnTypeName(2), partTypeName); + } + assertFalse(rs.next()); + } + } + + @DataProvider + public static Object[][] createArrayOfGeoElementTypes() { + return new Object[][] { + {"Point", new Object[] {new double[] {1.5, 2.5}, new double[] {3.5, 4.5}}, "Float64"}, + {"Ring", new Object[] {new double[][] {{1.5, 2.5}, {3.5, 4.5}}}, "Point"}, + {"LineString", new Object[] {new double[][] {{1.5, 2.5}, {3.5, 4.5}}}, "Point"}, + {"Polygon", new Object[] {new double[][][] {{{1.5, 2.5}, {3.5, 4.5}}}}, "Ring"}, + {"MultiLineString", new Object[] {new double[][][] {{{1.5, 2.5}, {3.5, 4.5}}}}, "LineString"}, + {"MultiPolygon", new Object[] {new double[][][][] {{{{1.5, 2.5}, {3.5, 4.5}}}}}, "Polygon"}, + }; + } + @Test(groups = { "integration" }) public void testCreateStruct() throws SQLException { try (Connection conn = this.getJdbcConnection()) { 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..a71afca1b 100644 --- a/jdbc-v2/src/test/java/com/clickhouse/jdbc/JdbcDataTypeTests.java +++ b/jdbc-v2/src/test/java/com/clickhouse/jdbc/JdbcDataTypeTests.java @@ -3501,6 +3501,53 @@ public void testGeoGeometryPreparedStatement() throws Exception { } } + @Test(groups = { "integration" }, dataProvider = "geoArrayElementTypes") + public void testGeoArrayResultSet(String sql, String unsupportedVersions, String[] elementTypeNames) + throws Exception { + if (unsupportedVersions != null && isVersionMatch(unsupportedVersions)) { + throw new SkipException("The type is not supported by this ClickHouse version"); + } + + try (Connection conn = getJdbcConnection(); Statement stmt = conn.createStatement(); + ResultSet rs = stmt.executeQuery(sql)) { + assertTrue(rs.next()); + Array array = rs.getArray(1); + for (String elementTypeName : elementTypeNames) { + Object expected = ((Object[]) array.getArray())[0]; + ResultSet elements = array.getResultSet(); + assertEquals(elements.getMetaData().getColumnTypeName(2), elementTypeName); + assertEquals(array.getResultSet(0, 1).getMetaData().getColumnTypeName(2), elementTypeName); + assertTrue(elements.next()); + Object element = elements.getObject(2); + if (element instanceof Array) { + array = (Array) element; + assertEquals(array.getBaseTypeName(), elementTypeName); + assertEquals(array.getArray(), expected); + } else { + assertEquals(element, expected); + } + } + } + } + + @DataProvider + public static Object[][] geoArrayElementTypes() { + return new Object[][] { + {"SELECT (1.5, 2.5)::Point", null, new String[] {"Float64"}}, + {"SELECT [(1.5, 2.5), (3.5, 4.5)]::Ring", null, new String[] {"Point", "Float64"}}, + {"SELECT [(1.5, 2.5), (3.5, 4.5)]::LineString", null, new String[] {"Point", "Float64"}}, + {"SELECT [(1.5, 2.5), (3.5, 4.5)]::MultiPoint", MULTI_POINT_UNSUPPORTED_VERSIONS, + new String[] {"Point", "Float64"}}, + {"SELECT [[(1.5, 2.5)], [(3.5, 4.5)]]::Polygon", null, new String[] {"Ring", "Point", "Float64"}}, + {"SELECT [[(1.5, 2.5)], [(3.5, 4.5)]]::MultiLineString", null, + new String[] {"LineString", "Point", "Float64"}}, + {"SELECT [[[(1.5, 2.5)]], [[(3.5, 4.5)]]]::MultiPolygon", null, + new String[] {"Polygon", "Ring", "Point", "Float64"}}, + {"SELECT [(1.5, 2.5), (3.5, 4.5)]::Ring::Geometry SETTINGS allow_suspicious_variant_types = 1", + "(,25.10]", new String[] {"Geometry"}}, + }; + } + private static Object createGeometryParameter(Connection conn, Object value) throws SQLException { if (value instanceof double[]) { return conn.createStruct("Tuple(Float32, Float32)", Arrays.stream((double[]) value).boxed().toArray(Double[]::new));