Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,8 @@

### Bug Fixes

- **[jdbc-v2]** Made JDBC `Array` values render their nested contents through `toString()`, including arrays of named tuples, so clients such as IDE database viewers no longer display the wrapper class name and identity hash. (https://github.com/ClickHouse/clickhouse-java/issues/3045)

- **[client-v2]** Fixed LZ4 input streams not closing their underlying HTTP response stream. Closing an LZ4 stream
returned by `QueryResponse.getInputStream()` now releases the wrapped transport stream, including after a partial
read. (https://github.com/ClickHouse/clickhouse-java/issues/2985)
Expand Down
11 changes: 11 additions & 0 deletions jdbc-v2/src/main/java/com/clickhouse/jdbc/types/Array.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.SQLFeatureNotSupportedException;
import java.util.Arrays;
import java.util.Map;

public class Array implements java.sql.Array {
Expand Down Expand Up @@ -127,6 +128,16 @@ private void ensureValid() throws SQLException {
}
}

/**
* Returns a readable representation of the array contents. JDBC clients such as IDE database viewers
* commonly use {@code toString()} when rendering values returned by {@link java.sql.ResultSet#getObject}.
* Deep formatting is required for arrays of tuples and nested arrays.
*/
@Override
public String toString() {
return Arrays.deepToString(array);
}

@Override
public boolean equals(Object obj) {
if (this == obj) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
package com.clickhouse.jdbc;

import org.testng.annotations.Test;

import java.sql.Array;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Arrays;

import static org.testng.Assert.assertEquals;
import static org.testng.Assert.assertFalse;
import static org.testng.Assert.assertTrue;

public class ArrayTupleIntegrationTest extends JdbcIntegrationTest {

@Test(groups = {"integration"})
public void testArrayOfNamedAndUnnamedTuplesToString() throws Exception {
String uuid = "550e8400-e29b-41d4-a716-446655440000";
String expected = "[[" + uuid + "]]";
String query = "SELECT [('" + uuid + "')::Tuple(id UUID)] AS named, "
+ "[('" + uuid + "')::Tuple(UUID)] AS unnamed";

try (Connection connection = getJdbcConnection();
Statement statement = connection.createStatement();
ResultSet resultSet = statement.executeQuery(query)) {
assertTrue(resultSet.next());

assertArrayValue(resultSet.getObject("named"), expected);
assertArrayValue(resultSet.getObject("unnamed"), expected);

assertFalse(resultSet.next());
}
}

private static void assertArrayValue(Object value, String expected) throws SQLException {
assertTrue(value instanceof Array);
Array array = (Array) value;
assertEquals(value.toString(), expected);
assertEquals(Arrays.deepToString((Object[]) array.getArray()), expected);
}
}
22 changes: 22 additions & 0 deletions jdbc-v2/src/test/java/com/clickhouse/jdbc/types/ArrayTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package com.clickhouse.jdbc.types;

import com.clickhouse.data.ClickHouseColumn;
import org.testng.annotations.Test;

import java.sql.SQLException;
import java.util.UUID;

import static org.testng.Assert.assertEquals;

@Test(groups = {"unit"})
public class ArrayTest {

@Test
public void testToStringForArrayOfNamedTuples() throws SQLException {
ClickHouseColumn column = ClickHouseColumn.of("value", "Array(Tuple(id UUID))");
UUID id = UUID.fromString("550e8400-e29b-41d4-a716-446655440000");
Array array = new Array(column, new Object[] {new Object[] {id}});

assertEquals(array.toString(), "[[550e8400-e29b-41d4-a716-446655440000]]");
}
}