Skip to content

Commit 5f3e7fd

Browse files
authored
Metabase Fixes (#2051)
* Added Tuple implementation * Enhance Tuple toString implementation
1 parent 8ceb97b commit 5f3e7fd

File tree

3 files changed

+73
-3
lines changed

3 files changed

+73
-3
lines changed
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
package com.clickhouse.data;
2+
3+
public class Tuple {
4+
private final Object[] values;
5+
private volatile String output;
6+
public Tuple(Object... values) {
7+
this.values = values;
8+
}
9+
10+
public Object[] getValues() {
11+
return values;
12+
}
13+
14+
public Object getValue(int index) {
15+
return values[index];
16+
}
17+
18+
public int size() {
19+
return values.length;
20+
}
21+
private String buildOutput() {
22+
StringBuilder sb = new StringBuilder();
23+
sb.append("(");
24+
for (int i = 0; i < values.length; i++) {
25+
if (i > 0) {
26+
sb.append(", ");
27+
}
28+
sb.append(values[i]);
29+
}
30+
sb.append(")");
31+
return sb.toString();
32+
}
33+
@Override
34+
public String toString() {
35+
if (output == null) {
36+
output = buildOutput();
37+
}
38+
return output;
39+
}
40+
}

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

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package com.clickhouse.jdbc;
22

3+
import com.clickhouse.data.Tuple;
34
import com.clickhouse.jdbc.internal.ExceptionUtils;
45
import org.slf4j.Logger;
56
import org.slf4j.LoggerFactory;
@@ -546,6 +547,21 @@ private static String encodeObject(Object x) throws SQLException {
546547
arrayString.append("]");
547548

548549
return arrayString.toString();
550+
} else if (x instanceof Tuple) {
551+
StringBuilder tupleString = new StringBuilder();
552+
tupleString.append("(");
553+
Tuple t = (Tuple) x;
554+
Object [] values = t.getValues();
555+
int i = 0;
556+
for (Object item : values) {
557+
if (i > 0) {
558+
tupleString.append(", ");
559+
}
560+
tupleString.append(encodeObject(item));
561+
i++;
562+
}
563+
tupleString.append(")");
564+
return tupleString.toString();
549565
}
550566

551567
return escapeString(x.toString());//Escape single quotes

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

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package com.clickhouse.jdbc;
22

33
import com.clickhouse.client.api.ClientConfigProperties;
4+
import com.clickhouse.data.Tuple;
45
import org.slf4j.Logger;
56
import org.slf4j.LoggerFactory;
67
import org.testng.annotations.BeforeClass;
@@ -455,7 +456,7 @@ public void testBooleanTypes() throws SQLException {
455456
@Test(groups = { "integration" })
456457
public void testArrayTypes() throws SQLException {
457458
runQuery("CREATE TABLE test_arrays (order Int8, "
458-
+ "array Array(Int8), arraystr Array(String)"
459+
+ "array Array(Int8), arraystr Array(String), arraytuple Array(Tuple(Int8, String))"
459460
+ ") ENGINE = MergeTree ORDER BY ()");
460461

461462
// Insert random (valid) values
@@ -473,11 +474,17 @@ public void testArrayTypes() throws SQLException {
473474
arraystr[i] = "string" + rand.nextInt(1000);
474475
}
475476

477+
Tuple[] arraytuple = new Tuple[rand.nextInt(10) + 1];
478+
for (int i = 0; i < arraytuple.length; i++) {
479+
arraytuple[i] = new Tuple(rand.nextInt(256) - 128, "string" + rand.nextInt(1000));
480+
}
481+
476482
// Insert random (valid) values
477483
try (Connection conn = getConnection()) {
478-
try (PreparedStatement stmt = conn.prepareStatement("INSERT INTO test_arrays VALUES ( 1, ?, ? )")) {
484+
try (PreparedStatement stmt = conn.prepareStatement("INSERT INTO test_arrays VALUES ( 1, ?, ?, ?)")) {
479485
stmt.setArray(1, conn.createArrayOf("Int8", array));
480486
stmt.setArray(2, conn.createArrayOf("String", arraystr));
487+
stmt.setArray(3, conn.createArrayOf("Tuple", arraytuple));
481488
stmt.executeUpdate();
482489
}
483490
}
@@ -498,7 +505,14 @@ public void testArrayTypes() throws SQLException {
498505
for (int i = 0; i < arraystr.length; i++) {
499506
assertEquals(arraystrResult[i], arraystr[i]);
500507
}
501-
508+
Object[] arraytupleResult = (Object[]) rs.getArray("arraytuple").getArray();
509+
assertEquals(arraytupleResult.length, arraytuple.length);
510+
for (int i = 0; i < arraytuple.length; i++) {
511+
Tuple tuple = arraytuple[i];
512+
Tuple tupleResult = new Tuple(((Object[]) arraytupleResult[i]));
513+
assertEquals(String.valueOf(tupleResult.getValue(0)), String.valueOf(tuple.getValue(0)));
514+
assertEquals(String.valueOf(tupleResult.getValue(1)), String.valueOf(tuple.getValue(1)));
515+
}
502516
assertFalse(rs.next());
503517
}
504518
}

0 commit comments

Comments
 (0)