From 39b5dead2462a5a9a2059214a3ffbb071b92fd1f Mon Sep 17 00:00:00 2001 From: Vasily Vasilkov Date: Wed, 22 May 2024 11:34:42 +0400 Subject: [PATCH] Fix Boolean type parsing --- src/main/kotlin/com/ecwid/clickhouse/Types.kt | 2 + .../com/ecwid/clickhouse/raw/ParseUtils.kt | 40 +++++++++++++------ .../kotlin/com/ecwid/clickhouse/raw/RawRow.kt | 13 ------ .../com/ecwid/clickhouse/typed/TypedRow.kt | 37 +++++++++++++++-- .../com/ecwid/clickhouse/typed/TypedValues.kt | 16 +++++++- 5 files changed, 77 insertions(+), 31 deletions(-) diff --git a/src/main/kotlin/com/ecwid/clickhouse/Types.kt b/src/main/kotlin/com/ecwid/clickhouse/Types.kt index 077ed8c..ffc4f28 100644 --- a/src/main/kotlin/com/ecwid/clickhouse/Types.kt +++ b/src/main/kotlin/com/ecwid/clickhouse/Types.kt @@ -3,7 +3,9 @@ package com.ecwid.clickhouse enum class PlatformType(val platformName: String) { BOOL("Bool"), + BOOL_NULLABLE("Nullable(Bool)"), + // -------------------------------------------------------------- INT_8("Int8"), INT_8_NULLABLE("Nullable(Int8)"), diff --git a/src/main/kotlin/com/ecwid/clickhouse/raw/ParseUtils.kt b/src/main/kotlin/com/ecwid/clickhouse/raw/ParseUtils.kt index e81b793..e31438d 100644 --- a/src/main/kotlin/com/ecwid/clickhouse/raw/ParseUtils.kt +++ b/src/main/kotlin/com/ecwid/clickhouse/raw/ParseUtils.kt @@ -72,12 +72,19 @@ internal fun readRawRow(reader: JsonReader, meta: Meta): RawRow { reader.beginArray() while (reader.hasNext()) { val next = reader.peek() - if (next == JsonToken.NULL) { - reader.nextNull() - array.add(null) - } else { - val value = reader.nextString() - array.add(value) + when (next) { + JsonToken.NULL -> { + reader.nextNull() + array.add(null) + } + JsonToken.BOOLEAN -> { + val value = reader.nextBoolean() + array.add(value.toString()) + } + else -> { + val value = reader.nextString() + array.add(value) + } } } reader.endArray() @@ -97,12 +104,19 @@ internal fun readRawRow(reader: JsonReader, meta: Meta): RawRow { val name = reader.nextName() val next = reader.peek() - if (next == JsonToken.NULL) { - reader.nextNull() - map[name] = null - } else { - val value = reader.nextString() - map[name] = value + when (next) { + JsonToken.NULL -> { + reader.nextNull() + map[name] = null + } + JsonToken.BOOLEAN -> { + val value = reader.nextBoolean() + map[name] = value.toString() + } + else -> { + val value = reader.nextString() + map[name] = value + } } } reader.endObject() @@ -112,7 +126,7 @@ internal fun readRawRow(reader: JsonReader, meta: Meta): RawRow { JsonToken.BOOLEAN -> { val value = reader.nextBoolean() - values.add(value) + values.add(value.toString()) } else -> { diff --git a/src/main/kotlin/com/ecwid/clickhouse/raw/RawRow.kt b/src/main/kotlin/com/ecwid/clickhouse/raw/RawRow.kt index e432259..a7d2f7b 100644 --- a/src/main/kotlin/com/ecwid/clickhouse/raw/RawRow.kt +++ b/src/main/kotlin/com/ecwid/clickhouse/raw/RawRow.kt @@ -22,19 +22,6 @@ 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 { val value = values[columnIndex] diff --git a/src/main/kotlin/com/ecwid/clickhouse/typed/TypedRow.kt b/src/main/kotlin/com/ecwid/clickhouse/typed/TypedRow.kt index b39364f..106c8d0 100644 --- a/src/main/kotlin/com/ecwid/clickhouse/typed/TypedRow.kt +++ b/src/main/kotlin/com/ecwid/clickhouse/typed/TypedRow.kt @@ -14,13 +14,44 @@ data class TypedRow( fun getMeta() = rawRow.getMeta() // ----------------- Bool --------------------- - fun getBool(columnIndex: Int): Boolean { - return rawRow.getBoolValue(columnIndex) + val scalar = rawRow.getScalarValue(columnIndex) + return Convert.Bool.toValue(scalar) + } + + fun getBoolArray(columnIndex: Int): List { + val array = rawRow.getArrayValue(columnIndex) + return Convert.Bool.toArray(array) + } + + fun getBoolNullable(columnIndex: Int): Boolean? { + val scalar = rawRow.getScalarValue(columnIndex) + return Convert.Bool.toNullableValue(scalar) + } + + fun getBoolNullableArray(columnIndex: Int): List { + val array = rawRow.getArrayValue(columnIndex) + return Convert.Bool.toNullableArray(array) } fun getBool(columnName: String): Boolean { - return rawRow.getBoolValue(columnName) + val scalar = rawRow.getScalarValue(columnName) + return Convert.Bool.toValue(scalar) + } + + fun getBoolArray(columnName: String): List { + val array = rawRow.getArrayValue(columnName) + return Convert.Bool.toArray(array) + } + + fun getBoolNullable(columnName: String): Boolean? { + val scalar = rawRow.getScalarValue(columnName) + return Convert.Bool.toNullableValue(scalar) + } + + fun getBoolNullableArray(columnName: String): List { + val array = rawRow.getArrayValue(columnName) + return Convert.Bool.toNullableArray(array) } // ----------------- INT_8 -------------------- diff --git a/src/main/kotlin/com/ecwid/clickhouse/typed/TypedValues.kt b/src/main/kotlin/com/ecwid/clickhouse/typed/TypedValues.kt index a37c4c7..8c75cc0 100644 --- a/src/main/kotlin/com/ecwid/clickhouse/typed/TypedValues.kt +++ b/src/main/kotlin/com/ecwid/clickhouse/typed/TypedValues.kt @@ -12,11 +12,23 @@ class TypedValues { fun getRawValues() = rawValues - // ----------------- Boolean -------------------- - fun setBoolean(columnName: String, value: Boolean) { + // ----------------- Bool -------------------- + fun setBool(columnName: String, value: Boolean) { rawValues.addScalar(columnName, Convert.Bool.fromValue(value)) } + fun setBoolArray(columnName: String, array: List) { + rawValues.addArray(columnName, Convert.Bool.fromArray(array)) + } + + fun setBoolNullable(columnName: String, value: Boolean?) { + rawValues.addScalar(columnName, Convert.Bool.fromNullableValue(value)) + } + + fun setBoolNullableArray(columnName: String, array: List) { + rawValues.addArray(columnName, Convert.Bool.fromNullableArray(array)) + } + // ----------------- INT_8 -------------------- fun setInt8(columnName: String, value: Byte) { rawValues.addScalar(columnName, Convert.Int8.fromValue(value))