diff --git a/clickhouse-client/src/main/java/com/clickhouse/client/ClickHouseColumn.java b/clickhouse-client/src/main/java/com/clickhouse/client/ClickHouseColumn.java index d089b9bf9..ec0d27899 100644 --- a/clickhouse-client/src/main/java/com/clickhouse/client/ClickHouseColumn.java +++ b/clickhouse-client/src/main/java/com/clickhouse/client/ClickHouseColumn.java @@ -48,14 +48,16 @@ private static ClickHouseColumn update(ClickHouseColumn column) { column.precision = column.dataType.getMaxPrecision(); switch (column.dataType) { case Array: - column.arrayLevel = 1; - column.arrayBaseColumn = column.nested.get(0); - while (column.arrayLevel < 255) { - if (column.arrayBaseColumn.dataType == ClickHouseDataType.Array) { - column.arrayLevel++; - column.arrayBaseColumn = column.arrayBaseColumn.nested.get(0); - } else { - break; + if (!column.nested.isEmpty()) { + column.arrayLevel = 1; + column.arrayBaseColumn = column.nested.get(0); + while (column.arrayLevel < 255) { + if (column.arrayBaseColumn.dataType == ClickHouseDataType.Array) { + column.arrayLevel++; + column.arrayBaseColumn = column.arrayBaseColumn.nested.get(0); + } else { + break; + } } } break; @@ -101,10 +103,14 @@ private static ClickHouseColumn update(ClickHouseColumn column) { case Decimal64: case Decimal128: case Decimal256: - column.scale = Integer.parseInt(column.parameters.get(0)); + if (size > 0) { + column.scale = Integer.parseInt(column.parameters.get(0)); + } break; case FixedString: - column.precision = Integer.parseInt(column.parameters.get(0)); + if (size > 0) { + column.precision = Integer.parseInt(column.parameters.get(0)); + } break; default: break; @@ -307,13 +313,14 @@ public static ClickHouseColumn of(String columnName, ClickHouseDataType dataType public static ClickHouseColumn of(String columnName, ClickHouseDataType dataType, boolean nullable, boolean lowCardinality, String... parameters) { - return new ClickHouseColumn(dataType, columnName, null, nullable, lowCardinality, Arrays.asList(parameters), - null); + return update(new ClickHouseColumn(dataType, columnName, null, nullable, lowCardinality, + Arrays.asList(parameters), null)); } public static ClickHouseColumn of(String columnName, ClickHouseDataType dataType, boolean nullable, ClickHouseColumn... nestedColumns) { - return new ClickHouseColumn(dataType, columnName, null, nullable, false, null, Arrays.asList(nestedColumns)); + return update( + new ClickHouseColumn(dataType, columnName, null, nullable, false, null, Arrays.asList(nestedColumns))); } public static ClickHouseColumn of(String columnName, String columnType) { diff --git a/clickhouse-jdbc/src/main/java/com/clickhouse/jdbc/ClickHouseConnection.java b/clickhouse-jdbc/src/main/java/com/clickhouse/jdbc/ClickHouseConnection.java index 8a626af68..a786f52ff 100644 --- a/clickhouse-jdbc/src/main/java/com/clickhouse/jdbc/ClickHouseConnection.java +++ b/clickhouse-jdbc/src/main/java/com/clickhouse/jdbc/ClickHouseConnection.java @@ -14,6 +14,7 @@ import com.clickhouse.client.ClickHouseColumn; import com.clickhouse.client.ClickHouseConfig; +import com.clickhouse.client.ClickHouseDataType; import com.clickhouse.client.ClickHouseValue; import com.clickhouse.client.ClickHouseValues; import com.clickhouse.client.ClickHouseVersion; @@ -36,7 +37,8 @@ public interface ClickHouseConnection extends Connection { @Override default ClickHouseArray createArrayOf(String typeName, Object[] elements) throws SQLException { ClickHouseConfig config = getConfig(); - ClickHouseColumn column = ClickHouseColumn.of("", typeName); + ClickHouseColumn column = ClickHouseColumn.of("", ClickHouseDataType.Array, false, + ClickHouseColumn.of("", typeName)); ClickHouseValue v = ClickHouseValues.newValue(config, column).update(elements); ClickHouseResultSet rs = new ClickHouseResultSet("", "", createStatement(), ClickHouseSimpleResponse.of(config, Collections.singletonList(column), diff --git a/clickhouse-jdbc/src/test/java/com/clickhouse/jdbc/ClickHousePreparedStatementTest.java b/clickhouse-jdbc/src/test/java/com/clickhouse/jdbc/ClickHousePreparedStatementTest.java index a620e3f3a..273eb7b68 100644 --- a/clickhouse-jdbc/src/test/java/com/clickhouse/jdbc/ClickHousePreparedStatementTest.java +++ b/clickhouse-jdbc/src/test/java/com/clickhouse/jdbc/ClickHousePreparedStatementTest.java @@ -882,6 +882,27 @@ public void testInsertStringAsArray() throws Exception { Assert.assertEquals(rs.getObject(3), new long[] { 3, 0, 1 }); Assert.assertFalse(rs.next()); } + + try (ClickHouseConnection conn = newConnection(new Properties()); + Statement s = conn.createStatement()) { + s.execute("drop table if exists test_string_array_insert; " + + "create table test_string_array_insert(id UInt32, a Array(LowCardinality(String)), b Array(Nullable(String)))engine=Memory"); + + try (PreparedStatement stmt = conn.prepareStatement( + "insert into test_string_array_insert(id, a, b) values (?,?,?)")) { + stmt.setString(1, "1"); + stmt.setObject(2, new String[] { "1", "2", "3" }); + stmt.setArray(3, conn.createArrayOf("String", new String[] { "3", null, "1" })); + Assert.assertEquals(stmt.executeUpdate(), 1); + } + + ResultSet rs = s.executeQuery("select * from test_string_array_insert order by id"); + Assert.assertTrue(rs.next()); + Assert.assertEquals(rs.getInt(1), 1); + Assert.assertEquals(rs.getArray(2).getArray(), new String[] { "1", "2", "3" }); + Assert.assertEquals(rs.getObject(3), new String[] { "3", null, "1" }); + Assert.assertFalse(rs.next()); + } } @Test(groups = "integration")