Skip to content

Commit 4f68d5b

Browse files
committed
added test for all supported GEO types.
1 parent 37036b3 commit 4f68d5b

File tree

3 files changed

+272
-8
lines changed

3 files changed

+272
-8
lines changed

jdbc-v2/src/main/java/com/clickhouse/jdbc/PreparedStatementImpl.java

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -929,9 +929,9 @@ public String encodeArray(Object[] elements, int levels, ClickHouseDataType elem
929929
} else if (cursor.arrayAsTuple) {
930930
arraySb.append(encodeTuple((Object[]) element)).append(',');
931931
cursor.pos++;
932-
} else if (cursor.level == 1 && elementType == ClickHouseDataType.Tuple && element instanceof Array ) {
932+
} else if (cursor.level == 1 && isTupleType(elementType) && element instanceof Array ) {
933933
cursor.arrayObjAsTuple = true;
934-
} else if (cursor.level == 1 && elementType == ClickHouseDataType.Tuple && element instanceof Object[] ) {
934+
} else if (cursor.level == 1 && isTupleType(elementType) && element instanceof Object[] ) {
935935
cursor.arrayAsTuple = true;
936936
} else if (cursor.level == 1) {
937937
arraySb.append(encodeObject(element)).append(',');
@@ -947,6 +947,10 @@ public String encodeArray(Object[] elements, int levels, ClickHouseDataType elem
947947
return arraySb.toString();
948948
}
949949

950+
private static boolean isTupleType(ClickHouseDataType type ) {
951+
return type == ClickHouseDataType.Tuple || type == ClickHouseDataType.Point;
952+
}
953+
950954
private static final class ArrayProcessingCursor {
951955
Object[] array; // current array
952956
int pos; // processing position

jdbc-v2/src/main/java/com/clickhouse/jdbc/ResultSetImpl.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1464,6 +1464,7 @@ public <T> T getObjectImpl(String columnLabel, Class<?> type, Map<String, Class<
14641464
case Point:
14651465
case Ring:
14661466
case LineString:
1467+
case Polygon:
14671468
case MultiPolygon:
14681469
case MultiLineString:
14691470
break; // read as is

jdbc-v2/src/test/java/com/clickhouse/jdbc/DataTypeTests.java

Lines changed: 265 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,13 @@
22

33
import com.clickhouse.client.api.ClientConfigProperties;
44
import com.clickhouse.client.api.DataTypeUtils;
5-
import com.clickhouse.client.api.data_formats.internal.BinaryStreamReader;
65
import com.clickhouse.client.api.internal.ServerSettings;
76
import com.clickhouse.client.api.sql.SQLUtils;
7+
import com.clickhouse.data.ClickHouseDataType;
88
import com.clickhouse.data.ClickHouseVersion;
99
import com.clickhouse.data.Tuple;
1010
import org.slf4j.Logger;
1111
import org.slf4j.LoggerFactory;
12-
import org.testng.Assert;
1312
import org.testng.annotations.BeforeClass;
1413
import org.testng.annotations.Test;
1514

@@ -51,7 +50,6 @@
5150

5251
import static org.testng.Assert.assertEquals;
5352
import static org.testng.Assert.assertFalse;
54-
import static org.testng.Assert.assertNotEquals;
5553
import static org.testng.Assert.assertNull;
5654
import static org.testng.Assert.assertThrows;
5755
import static org.testng.Assert.assertTrue;
@@ -1654,7 +1652,7 @@ public void testVariantTypesSimpleStatement() throws SQLException {
16541652
}
16551653

16561654
@Test(groups = { "integration" })
1657-
public void testSpatialData() throws Exception {
1655+
public void testGeoPoint1() throws Exception {
16581656
final Double[][] spatialArrayData = new Double[][] {
16591657
{4.837388, 52.38795},
16601658
{4.951513, 52.354582},
@@ -1679,13 +1677,12 @@ public void testSpatialData() throws Exception {
16791677
sql.append("as Point) as Point");
16801678

16811679

1682-
16831680
try (Connection conn = getJdbcConnection(); Statement stmt = conn.createStatement()) {
16841681
try (ResultSet rs = stmt.executeQuery(sql.toString())) {
16851682

16861683
ResultSetMetaData metaData = rs.getMetaData();
16871684
assertEquals(metaData.getColumnCount(), 1);
1688-
assertEquals(metaData.getColumnTypeName(1), "Point");
1685+
assertEquals(metaData.getColumnTypeName(1), ClickHouseDataType.Point.name());
16891686
assertEquals(metaData.getColumnType(1), Types.ARRAY);
16901687

16911688
int rowCount = 0;
@@ -1701,4 +1698,266 @@ public void testSpatialData() throws Exception {
17011698
}
17021699
}
17031700
}
1701+
1702+
@Test(groups = { "integration" })
1703+
public void testGeoPoint() throws Exception {
1704+
final double[] row = new double[] {
1705+
10.123456789,
1706+
11.123456789
1707+
};
1708+
1709+
try (Connection conn = getJdbcConnection(); Statement stmt = conn.createStatement()) {
1710+
String table = "test_geo_point";
1711+
stmt.executeUpdate("DROP TABLE IF EXISTS " + table);
1712+
stmt.executeUpdate("CREATE TABLE " + table + " (geom Point) ENGINE = MergeTree ORDER BY ()");
1713+
1714+
try (PreparedStatement pstmt = conn.prepareStatement("INSERT INTO " + table + " VALUES (?)")) {
1715+
Double[] rowObj = Arrays.stream(row).boxed().toArray(Double[]::new);
1716+
pstmt.setObject(1, conn.createStruct("Tuple(Float64, Float64)", rowObj));
1717+
pstmt.executeUpdate();
1718+
}
1719+
1720+
try (ResultSet rs = stmt.executeQuery("SELECT * FROM " + table)) {
1721+
int geomColumn = 1;
1722+
ResultSetMetaData rsMd = rs.getMetaData();
1723+
assertEquals(rsMd.getColumnTypeName(geomColumn), ClickHouseDataType.Point.name());
1724+
assertEquals(rsMd.getColumnType(geomColumn), Types.ARRAY);
1725+
1726+
while (rs.next()) {
1727+
if (rs.isLast()) {
1728+
assertEquals(rs.getRow(), 1);
1729+
}
1730+
1731+
Object asObject = rs.getObject(geomColumn);
1732+
Array asArray = rs.getArray(geomColumn);
1733+
}
1734+
}
1735+
}
1736+
}
1737+
1738+
@Test(groups = { "integration" })
1739+
public void testGeoRing() throws Exception {
1740+
final Double[][] row = new Double[][] {
1741+
{10.123456789, 11.123456789},
1742+
{12.123456789, 13.123456789},
1743+
{14.123456789, 15.123456789},
1744+
{10.123456789, 11.123456789},
1745+
};
1746+
1747+
try (Connection conn = getJdbcConnection(); Statement stmt = conn.createStatement()) {
1748+
final String table = "test_geo_ring";
1749+
stmt.executeUpdate("DROP TABLE IF EXISTS " + table);
1750+
stmt.executeUpdate("CREATE TABLE " + table + " (geom Ring) ENGINE = MergeTree ORDER BY ()");
1751+
1752+
try (PreparedStatement pstmt = conn.prepareStatement("INSERT INTO " + table + " VALUES (?)")) {
1753+
pstmt.setObject(1, conn.createArrayOf("Ring", row));
1754+
pstmt.executeUpdate();
1755+
}
1756+
1757+
try (ResultSet rs = stmt.executeQuery("SELECT * FROM " + table)) {
1758+
int geomColumn = 1;
1759+
ResultSetMetaData rsMd = rs.getMetaData();
1760+
assertEquals(rsMd.getColumnTypeName(geomColumn), ClickHouseDataType.Ring.name());
1761+
assertEquals(rsMd.getColumnType(geomColumn), Types.ARRAY);
1762+
1763+
while (rs.next()) {
1764+
if (rs.isLast()) {
1765+
assertEquals(rs.getRow(), 1);
1766+
}
1767+
1768+
Object asObject = rs.getObject(geomColumn);
1769+
Array asArray = rs.getArray(geomColumn);
1770+
}
1771+
}
1772+
}
1773+
}
1774+
1775+
@Test(groups = { "integration" })
1776+
public void testGeoLineString() throws Exception {
1777+
final Double[][] row = new Double[][] {
1778+
{10.123456789, 11.123456789},
1779+
{12.123456789, 13.123456789},
1780+
{14.123456789, 15.123456789},
1781+
{10.123456789, 11.123456789},
1782+
};
1783+
1784+
try (Connection conn = getJdbcConnection(); Statement stmt = conn.createStatement()) {
1785+
final String table = "test_geo_line_string";
1786+
stmt.executeUpdate("DROP TABLE IF EXISTS " + table);
1787+
stmt.executeUpdate("CREATE TABLE " + table +" (geom LineString) ENGINE = MergeTree ORDER BY ()");
1788+
1789+
try (PreparedStatement pstmt = conn.prepareStatement("INSERT INTO " + table + " VALUES (?)")) {
1790+
pstmt.setObject(1, conn.createArrayOf("LineString", row));
1791+
pstmt.executeUpdate();
1792+
}
1793+
1794+
try (ResultSet rs = stmt.executeQuery("SELECT * FROM " + table)) {
1795+
int geomColumn = 1;
1796+
ResultSetMetaData rsMd = rs.getMetaData();
1797+
assertEquals(rsMd.getColumnTypeName(geomColumn), ClickHouseDataType.LineString.name());
1798+
assertEquals(rsMd.getColumnType(geomColumn), Types.ARRAY);
1799+
1800+
while (rs.next()) {
1801+
if (rs.isLast()) {
1802+
assertEquals(rs.getRow(), 1);
1803+
}
1804+
1805+
Object asObject = rs.getObject(geomColumn);
1806+
Array asArray = rs.getArray(geomColumn);
1807+
}
1808+
}
1809+
}
1810+
}
1811+
1812+
@Test(groups = { "integration" })
1813+
public void testGeoMultiLineString() throws Exception {
1814+
final Double[][][] row = new Double[][][] {
1815+
{ // LineString 1
1816+
{10.123456789, 11.123456789},
1817+
{12.123456789, 13.123456789},
1818+
{14.123456789, 15.123456789},
1819+
{10.123456789, 11.123456789},
1820+
},
1821+
{
1822+
{16.123456789, 17.123456789},
1823+
{18.123456789, 19.123456789},
1824+
{20.123456789, 21.123456789},
1825+
{16.123456789, 17.123456789},
1826+
}
1827+
};
1828+
1829+
try (Connection conn = getJdbcConnection(); Statement stmt = conn.createStatement()) {
1830+
final String table = "test_geo_multi_line_string";
1831+
stmt.executeUpdate("DROP TABLE IF EXISTS " + table);
1832+
stmt.executeUpdate("CREATE TABLE " + table +" (geom MultiLineString) ENGINE = MergeTree ORDER BY ()");
1833+
1834+
try (PreparedStatement pstmt = conn.prepareStatement("INSERT INTO " + table + " VALUES (?)")) {
1835+
pstmt.setObject(1, conn.createArrayOf("Array(Array(Point))", row));
1836+
pstmt.executeUpdate();
1837+
}
1838+
1839+
try (ResultSet rs = stmt.executeQuery("SELECT * FROM " + table)) {
1840+
int geomColumn = 1;
1841+
ResultSetMetaData rsMd = rs.getMetaData();
1842+
assertEquals(rsMd.getColumnTypeName(geomColumn), ClickHouseDataType.MultiLineString.name());
1843+
assertEquals(rsMd.getColumnType(geomColumn), Types.ARRAY);
1844+
1845+
while (rs.next()) {
1846+
if (rs.isLast()) {
1847+
assertEquals(rs.getRow(), 1);
1848+
}
1849+
1850+
Object asObject = rs.getObject(geomColumn);
1851+
Array asArray = rs.getArray(geomColumn);
1852+
}
1853+
}
1854+
}
1855+
}
1856+
1857+
@Test(groups = { "integration" })
1858+
public void testGeoPolygon() throws Exception {
1859+
final Double[][][] row = new Double[][][] {
1860+
{ // Ring 1
1861+
{10.123456789, 11.123456789},
1862+
{12.123456789, 13.123456789},
1863+
{14.123456789, 15.123456789},
1864+
{10.123456789, 11.123456789},
1865+
},
1866+
{ // Ring 2
1867+
{16.123456789, 17.123456789},
1868+
{18.123456789, 19.123456789},
1869+
{20.123456789, 21.123456789},
1870+
{16.123456789, 17.123456789},
1871+
}
1872+
};
1873+
1874+
try (Connection conn = getJdbcConnection(); Statement stmt = conn.createStatement()) {
1875+
final String table = "test_geo_polygon";
1876+
stmt.executeUpdate("DROP TABLE IF EXISTS " + table);
1877+
stmt.executeUpdate("CREATE TABLE " + table +" (geom Polygon) ENGINE = MergeTree ORDER BY ()");
1878+
1879+
try (PreparedStatement pstmt = conn.prepareStatement("INSERT INTO " + table + " VALUES (?)")) {
1880+
pstmt.setObject(1, conn.createArrayOf("Array(Array(Point))", row));
1881+
pstmt.executeUpdate();
1882+
}
1883+
1884+
try (ResultSet rs = stmt.executeQuery("SELECT * FROM " + table)) {
1885+
int geomColumn = 1;
1886+
ResultSetMetaData rsMd = rs.getMetaData();
1887+
assertEquals(rsMd.getColumnTypeName(geomColumn), ClickHouseDataType.Polygon.name());
1888+
assertEquals(rsMd.getColumnType(geomColumn), Types.ARRAY);
1889+
1890+
while (rs.next()) {
1891+
if (rs.isLast()) {
1892+
assertEquals(rs.getRow(), 1);
1893+
}
1894+
1895+
Object asObject = rs.getObject(geomColumn);
1896+
Array asArray = rs.getArray(geomColumn);
1897+
}
1898+
}
1899+
}
1900+
}
1901+
1902+
@Test(groups = { "integration" })
1903+
public void testGeoMultiPolygon() throws Exception {
1904+
final Double[][][][] row = new Double[][][][] {
1905+
{ // Polygon 1
1906+
{ // Ring 1
1907+
{10.123456789, 11.123456789},
1908+
{12.123456789, 13.123456789},
1909+
{14.123456789, 15.123456789},
1910+
{10.123456789, 11.123456789},
1911+
},
1912+
{ // Ring 2
1913+
{16.123456789, 17.123456789},
1914+
{18.123456789, 19.123456789},
1915+
{20.123456789, 21.123456789},
1916+
{16.123456789, 17.123456789},
1917+
}
1918+
},
1919+
{ // Polygon 2
1920+
{ // Ring 1
1921+
{-10.123456789, -11.123456789},
1922+
{-12.123456789, -13.123456789},
1923+
{-14.123456789, -15.123456789},
1924+
{-10.123456789, -11.123456789},
1925+
},
1926+
{ // Ring 2
1927+
{-16.123456789, -17.123456789},
1928+
{-18.123456789, -19.123456789},
1929+
{-20.123456789, -21.123456789},
1930+
{-16.123456789, -17.123456789},
1931+
}
1932+
}
1933+
};
1934+
1935+
try (Connection conn = getJdbcConnection(); Statement stmt = conn.createStatement()) {
1936+
final String table = "test_geo_muti_polygon";
1937+
stmt.executeUpdate("DROP TABLE IF EXISTS " + table);
1938+
stmt.executeUpdate("CREATE TABLE " + table +" (geom MultiPolygon) ENGINE = MergeTree ORDER BY ()");
1939+
1940+
try (PreparedStatement pstmt = conn.prepareStatement("INSERT INTO " + table + " VALUES (?)")) {
1941+
pstmt.setObject(1, conn.createArrayOf("Array(Array(Array(Point)))", row));
1942+
pstmt.executeUpdate();
1943+
}
1944+
1945+
try (ResultSet rs = stmt.executeQuery("SELECT * FROM " + table)) {
1946+
int geomColumn = 1;
1947+
ResultSetMetaData rsMd = rs.getMetaData();
1948+
assertEquals(rsMd.getColumnTypeName(geomColumn), ClickHouseDataType.MultiPolygon.name());
1949+
assertEquals(rsMd.getColumnType(geomColumn), Types.ARRAY);
1950+
1951+
while (rs.next()) {
1952+
if (rs.isLast()) {
1953+
assertEquals(rs.getRow(), 1);
1954+
}
1955+
1956+
Object asObject = rs.getObject(geomColumn);
1957+
Array asArray = rs.getArray(geomColumn);
1958+
}
1959+
}
1960+
}
1961+
}
1962+
17041963
}

0 commit comments

Comments
 (0)