Skip to content

Commit

Permalink
Merge pull request #856 from zhicwu/prepare-patch
Browse files Browse the repository at this point in the history
Correct behavior of Connection.createArrayOf
  • Loading branch information
zhicwu authored Mar 6, 2022
2 parents e31fee4 + 046c3f2 commit 1d6257c
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down

0 comments on commit 1d6257c

Please sign in to comment.