Skip to content

Commit

Permalink
Merge pull request #1561 from ClickHouse/fixing-multidimensional-arrays
Browse files Browse the repository at this point in the history
Update ClickHouseArrayValue.java
  • Loading branch information
Paultagoras authored Mar 28, 2024
2 parents 9e1772e + 0b9666b commit 6f4275f
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,7 @@
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.util.Arrays;
import java.util.Collection;
import java.util.Enumeration;
import java.util.LinkedHashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.UUID;
import java.util.*;
import java.util.Map.Entry;
import com.clickhouse.data.ClickHouseArraySequence;
import com.clickhouse.data.ClickHouseChecker;
Expand Down Expand Up @@ -595,7 +588,31 @@ public <V extends ClickHouseValue> V getValue(int index, V value) {
@Override
@SuppressWarnings("unchecked")
public ClickHouseArraySequence setValue(int index, ClickHouseValue value) {
getValue()[index] = (T) value.asRawObject();
return this;
try {
getValue()[index] = (T) value.asRawObject();
return this;
} catch (ArrayStoreException arrayStoreException) {
Class<?> existingArrayType = value.asRawObject().getClass();
Class<?> idealArrayType = getValue().getClass();
// Loop to find the root component type
while (existingArrayType.isArray()) {
existingArrayType = existingArrayType.getComponentType();
}

int idealCount = 0;
while (idealArrayType.isArray()) {
idealArrayType = idealArrayType.getComponentType();
idealCount++;
}

// Create a new array of the correct type with the ideal depth
Object newArray = Array.newInstance(existingArrayType, 1);
for (int i = 1; i < idealCount - 1; i++) {
newArray = Array.newInstance(newArray.getClass(), 1);
}

getValue()[index] = (T) newArray;
return this;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -387,6 +387,17 @@ public boolean isUseBinaryString() {
BinaryStreamUtilsTest.generateInput(1, 2, 0x41, 0x31));
Assert.assertTrue(value instanceof ClickHouseArrayValue);
Assert.assertEquals(value.asObject(), new String[] { "A1" });
value = deserialize(null, config,
ClickHouseColumn.of("a", "Array(Array(Array(Nullable(String))))"),
BinaryStreamUtilsTest.generateInput(1, 1, 1, 1, 1, 1, 2, 0x41, 0x31));
Assert.assertTrue(value instanceof ClickHouseArrayValue);
Assert.assertNotNull(value.asArray());
value = deserialize(null, config,
ClickHouseColumn.of("a", "Array(Array(Array(Nullable(String))))"),
BinaryStreamUtilsTest.generateInput(1, 0));
Assert.assertTrue(value instanceof ClickHouseArrayValue);
//Assert.assertEquals(value.asObject(), new String[][][] { new String[][] { new String[] { "A1" } } });
Assert.assertNotNull(value.asArray());
value = deserialize(null, binConf,
ClickHouseColumn.of("a", "Array(String)"),
BinaryStreamUtilsTest.generateInput(1, 2, 0x41, 0x31));
Expand Down

0 comments on commit 6f4275f

Please sign in to comment.