Skip to content

Add support for Bool type for ClickHouse connector #25337

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Apr 18, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 6 additions & 3 deletions docs/src/main/sphinx/connector/clickhouse.md
Original file line number Diff line number Diff line change
Expand Up @@ -183,9 +183,12 @@ to the following table:
* - ClickHouse type
- Trino type
- Notes
* - `Bool`
- `BOOLEAN`
-
* - `Int8`
- `TINYINT`
- `TINYINT`, `BOOL`, `BOOLEAN`, and `INT1` are aliases of `Int8`
- `TINYINT` and `INT1` are aliases of `Int8`
* - `Int16`
- `SMALLINT`
- `SMALLINT` and `INT2` are aliases of `Int16`
Expand Down Expand Up @@ -262,11 +265,11 @@ to the following table:
- ClickHouse type
- Notes
* - `BOOLEAN`
- `UInt8`
- `Bool`
-
* - `TINYINT`
- `Int8`
- `TINYINT`, `BOOL`, `BOOLEAN`, and `INT1` are aliases of `Int8`
- `TINYINT` and `INT1` are aliases of `Int8`
* - `SMALLINT`
- `Int16`
- `SMALLINT` and `INT2` are aliases of `Int16`
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,7 @@
import static io.trino.plugin.jdbc.PredicatePushdownController.FULL_PUSHDOWN;
import static io.trino.plugin.jdbc.StandardColumnMappings.bigintColumnMapping;
import static io.trino.plugin.jdbc.StandardColumnMappings.bigintWriteFunction;
import static io.trino.plugin.jdbc.StandardColumnMappings.booleanColumnMapping;
import static io.trino.plugin.jdbc.StandardColumnMappings.booleanWriteFunction;
import static io.trino.plugin.jdbc.StandardColumnMappings.dateReadFunctionUsingLocalDate;
import static io.trino.plugin.jdbc.StandardColumnMappings.decimalColumnMapping;
Expand Down Expand Up @@ -638,6 +639,8 @@ public Optional<ColumnMapping> toColumnMapping(ConnectorSession session, Connect
ClickHouseColumn column = ClickHouseColumn.of("", jdbcTypeName);
ClickHouseDataType columnDataType = column.getDataType();
switch (columnDataType) {
case Bool:
return Optional.of(booleanColumnMapping());
case UInt8:
return Optional.of(ColumnMapping.longMapping(SMALLINT, ResultSet::getShort, uInt8WriteFunction(getClickHouseServerVersion(session))));
case UInt16:
Expand Down Expand Up @@ -757,8 +760,7 @@ public Optional<ColumnMapping> toColumnMapping(ConnectorSession session, Connect
public WriteMapping toWriteMapping(ConnectorSession session, Type type)
{
if (type == BOOLEAN) {
// ClickHouse is no separate type for boolean values. Use UInt8 type, restricted to the values 0 or 1.
return WriteMapping.booleanMapping("UInt8", booleanWriteFunction());
return WriteMapping.booleanMapping("Bool", booleanWriteFunction());
}
if (type == TINYINT) {
return WriteMapping.longMapping("Int8", tinyintWriteFunction());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@
import static io.trino.plugin.jdbc.TypeHandlingJdbcSessionProperties.UNSUPPORTED_TYPE_HANDLING;
import static io.trino.plugin.jdbc.UnsupportedTypeHandling.CONVERT_TO_VARCHAR;
import static io.trino.spi.type.BigintType.BIGINT;
import static io.trino.spi.type.BooleanType.BOOLEAN;
import static io.trino.spi.type.DateType.DATE;
import static io.trino.spi.type.DecimalType.createDecimalType;
import static io.trino.spi.type.DoubleType.DOUBLE;
Expand Down Expand Up @@ -106,6 +107,28 @@ private static void checkIsDoubled(ZoneId zone, LocalDateTime dateTime)
verify(zone.getRules().getValidOffsets(dateTime).size() == 2, "Expected %s to be doubled in %s", dateTime, zone);
}

@Test
public void testTrinoBoolean()
{
SqlDataTypeTest.create()
.addRoundTrip("boolean", "true", BOOLEAN, "true")
.addRoundTrip("boolean", "false", BOOLEAN, "false")
.addRoundTrip("boolean", "NULL", BOOLEAN, "CAST(NULL AS BOOLEAN)")
.execute(getQueryRunner(), trinoCreateAsSelect("test_boolean"))
.execute(getQueryRunner(), trinoCreateAndInsert("test_boolean"));
}

@Test
public void testBool()
{
SqlDataTypeTest.create()
.addRoundTrip("bool", "true", BOOLEAN, "true")
.addRoundTrip("bool", "false", BOOLEAN, "false")
.addRoundTrip("Nullable(bool)", "NULL", BOOLEAN, "CAST(NULL AS BOOLEAN)")
.execute(getQueryRunner(), clickhouseCreateAndInsert("tpch.test_boolean"))
.execute(getQueryRunner(), clickhouseCreateAndTrinoInsert("tpch.test_boolean"));
}

@Test
public void testTinyint()
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -486,7 +486,7 @@ public void testTableProperty()
.isEqualTo(format("" +
"CREATE TABLE clickhouse.tpch.%s (\n" +
" id integer NOT NULL,\n" +
" x smallint NOT NULL,\n" +
" x boolean NOT NULL,\n" +
" y varchar NOT NULL\n" +
")\n" +
"WITH (\n" +
Expand Down