Skip to content

Commit c318ba6

Browse files
author
Paultagoras
committed
Code + Test
1 parent 20babb5 commit c318ba6

File tree

2 files changed

+31
-8
lines changed

2 files changed

+31
-8
lines changed

client-v2/src/main/java/com/clickhouse/client/api/data_formats/internal/AbstractBinaryFormatReader.java

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@
1010
import com.clickhouse.client.api.query.POJOSetter;
1111
import com.clickhouse.client.api.query.QuerySettings;
1212
import com.clickhouse.data.ClickHouseColumn;
13+
import com.clickhouse.data.ClickHouseDataType;
14+
import com.clickhouse.data.ClickHouseEnum;
1315
import com.clickhouse.data.value.ClickHouseBitmap;
1416
import com.clickhouse.data.value.ClickHouseGeoMultiPolygonValue;
1517
import com.clickhouse.data.value.ClickHouseGeoPointValue;
@@ -275,20 +277,19 @@ public String getString(String colName) {
275277
return null;
276278
} else if (value instanceof String) {
277279
return (String) value;
280+
} else {
281+
ClickHouseDataType dataType = schema.getColumnByName(colName).getDataType();
282+
if (dataType == ClickHouseDataType.Enum8 || dataType == ClickHouseDataType.Enum16) {
283+
ClickHouseEnum clickHouseEnum = schema.getColumnByName(colName).getEnumConstants();
284+
return clickHouseEnum.name(Integer.parseInt(value.toString()));
285+
}
278286
}
279287
return value.toString();
280288
}
281289

282290
@Override
283291
public String getString(int index) {
284-
// TODO: it may be incorrect to call .toString() on some objects
285-
Object value = readValue(index);
286-
if (value == null) {
287-
return null;
288-
} else if (value instanceof String) {
289-
return (String) value;
290-
}
291-
return value.toString();
292+
return getString(schema.columnIndexToName(index));
292293
}
293294

294295
private <T> T readNumberValue(String colName, NumberConverter.NumberType targetType) {

client-v2/src/test/java/com/clickhouse/client/query/QueryTests.java

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1957,4 +1957,26 @@ public void testReadingSimpleAggregateFunction() throws Exception {
19571957
Assert.assertFalse(reader.hasNext());
19581958
}
19591959
}
1960+
1961+
@Test(groups = {"integration"})
1962+
public void testReadingEnumsAsStrings() throws Exception {
1963+
final String tableName = "enums_as_strings_test_table";
1964+
client.execute("DROP TABLE IF EXISTS " + tableName).get();
1965+
client.execute("CREATE TABLE `" + tableName + "` " +
1966+
"(idx UInt8, enum1 Enum8('a' = 1, 'b' = 2, 'c' = 3), enum2 Enum16('atch' = 1, 'batch' = 2, 'catch' = 3)) " +
1967+
"ENGINE Memory").get();
1968+
1969+
try (InsertResponse response = client.insert(tableName, new ByteArrayInputStream("1\ta\t2".getBytes(StandardCharsets.UTF_8)), ClickHouseFormat.TSV).get(30, TimeUnit.SECONDS)) {
1970+
Assert.assertEquals(response.getWrittenRows(), 1);
1971+
}
1972+
1973+
try (QueryResponse queryResponse = client.query("SELECT * FROM " + tableName + " LIMIT 1").get(30, TimeUnit.SECONDS)) {
1974+
ClickHouseBinaryFormatReader reader = client.newBinaryFormatReader(queryResponse);
1975+
Assert.assertNotNull(reader.next());
1976+
Assert.assertEquals(reader.getByte("idx"), Byte.valueOf("1"));
1977+
Assert.assertEquals(reader.getString("enum1"), "a");
1978+
Assert.assertEquals(reader.getString("enum2"), "batch");
1979+
Assert.assertFalse(reader.hasNext());
1980+
}
1981+
}
19601982
}

0 commit comments

Comments
 (0)