Skip to content

Commit

Permalink
Merge pull request #13 from Ecwid/bool_type
Browse files Browse the repository at this point in the history
Fix parsing of bool type
  • Loading branch information
mvgreen authored Nov 30, 2023
2 parents 011c7ad + 038a036 commit a0282b5
Show file tree
Hide file tree
Showing 6 changed files with 38 additions and 4 deletions.
2 changes: 2 additions & 0 deletions src/main/kotlin/com/ecwid/clickhouse/Types.kt
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ package com.ecwid.clickhouse

enum class PlatformType(val platformName: String) {

BOOL("Bool"),

INT_8("Int8"),
INT_8_NULLABLE("Nullable(Int8)"),

Expand Down
5 changes: 5 additions & 0 deletions src/main/kotlin/com/ecwid/clickhouse/raw/ParseUtils.kt
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,11 @@ internal fun readRawRow(reader: JsonReader, meta: Meta): RawRow {
values.add(map)
}

JsonToken.BOOLEAN -> {
val value = reader.nextBoolean()
values.add(value)
}

else -> {
val value = reader.nextString()
values.add(value)
Expand Down
13 changes: 13 additions & 0 deletions src/main/kotlin/com/ecwid/clickhouse/raw/RawRow.kt
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,19 @@ data class RawRow(
return getScalarValue(columnIndex)
}

fun getBoolValue(columnIndex: Int): Boolean {
val value = values[columnIndex]
require(value is Boolean) {
"Can't convert scalar $value to boolean"
}
return value
}

fun getBoolValue(columnName: String): Boolean {
val columnIndex = meta.getColumnIndex(columnName)
return getBoolValue(columnIndex)
}

fun getArrayValue(columnIndex: Int): List<String?> {
val value = values[columnIndex]

Expand Down
7 changes: 5 additions & 2 deletions src/main/kotlin/com/ecwid/clickhouse/typed/TypedRow.kt
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,11 @@ data class TypedRow(
// ----------------- Bool ---------------------

fun getBool(columnIndex: Int): Boolean {
val scalar = rawRow.getScalarValue(columnIndex)
return Convert.Bool.toValue(scalar)
return rawRow.getBoolValue(columnIndex)
}

fun getBool(columnName: String): Boolean {
return rawRow.getBoolValue(columnName)
}

// ----------------- INT_8 --------------------
Expand Down
5 changes: 5 additions & 0 deletions src/main/kotlin/com/ecwid/clickhouse/typed/TypedValues.kt
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,11 @@ class TypedValues {

fun getRawValues() = rawValues

// ----------------- Boolean --------------------
fun setBoolean(columnName: String, value: Boolean) {
rawValues.addScalar(columnName, Convert.Bool.fromValue(value))
}

// ----------------- INT_8 --------------------
fun setInt8(columnName: String, value: Byte) {
rawValues.addScalar(columnName, Convert.Int8.fromValue(value))
Expand Down
10 changes: 8 additions & 2 deletions src/test/kotlin/com/ecwid/clickhouse/raw/ParseUtilsKtTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -31,25 +31,31 @@ internal class ParseUtilsKtTest {
{
"name": "field4",
"type": "Array(String)"
},
{
"name": "field5",
"type": "Bool"
}
]
""".trimIndent()

val meta = readMetaObject(JsonReader(json.reader()))

Assertions.assertEquals(5, meta.columns.size)
Assertions.assertEquals(6, meta.columns.size)
Assertions.assertEquals(Type.Platform(PlatformType.DATETIME, "DateTime"), meta.columns[0])
Assertions.assertEquals(Type.Platform(PlatformType.INT_32, "Int32"), meta.columns[1])
Assertions.assertEquals(Type.Platform(PlatformType.INT_32_NULLABLE, "Nullable(Int32)"), meta.columns[2])
Assertions.assertEquals(Type.Platform(PlatformType.STRING_NULLABLE, "Nullable(String)"), meta.columns[3])
Assertions.assertEquals(Type.Array(PlatformType.STRING, "Array(String)"), meta.columns[4])
Assertions.assertEquals(Type.Platform(PlatformType.BOOL, "Bool"), meta.columns[5])

Assertions.assertEquals(5, meta.names.size)
Assertions.assertEquals(6, meta.names.size)
Assertions.assertEquals(0, meta.names["date"])
Assertions.assertEquals(1, meta.names["field1"])
Assertions.assertEquals(2, meta.names["field2"])
Assertions.assertEquals(3, meta.names["field3"])
Assertions.assertEquals(4, meta.names["field4"])
Assertions.assertEquals(5, meta.names["field5"])
}

@Test
Expand Down

0 comments on commit a0282b5

Please sign in to comment.