Skip to content

Commit

Permalink
Fix Boolean type parsing
Browse files Browse the repository at this point in the history
  • Loading branch information
vgv committed May 22, 2024
1 parent 5bdd00f commit 39b5dea
Show file tree
Hide file tree
Showing 5 changed files with 77 additions and 31 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 @@ -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)"),

Expand Down
40 changes: 27 additions & 13 deletions src/main/kotlin/com/ecwid/clickhouse/raw/ParseUtils.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand All @@ -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()
Expand All @@ -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 -> {
Expand Down
13 changes: 0 additions & 13 deletions src/main/kotlin/com/ecwid/clickhouse/raw/RawRow.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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<String?> {
val value = values[columnIndex]

Expand Down
37 changes: 34 additions & 3 deletions src/main/kotlin/com/ecwid/clickhouse/typed/TypedRow.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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<Boolean> {
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<Boolean?> {
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<Boolean> {
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<Boolean?> {
val array = rawRow.getArrayValue(columnName)
return Convert.Bool.toNullableArray(array)
}

// ----------------- INT_8 --------------------
Expand Down
16 changes: 14 additions & 2 deletions src/main/kotlin/com/ecwid/clickhouse/typed/TypedValues.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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<Boolean>) {
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<Boolean?>) {
rawValues.addArray(columnName, Convert.Bool.fromNullableArray(array))
}

// ----------------- INT_8 --------------------
fun setInt8(columnName: String, value: Byte) {
rawValues.addScalar(columnName, Convert.Int8.fromValue(value))
Expand Down

0 comments on commit 39b5dea

Please sign in to comment.