Skip to content

Commit

Permalink
Add support for Int128, Int256, UInt128, UInt256
Browse files Browse the repository at this point in the history
  • Loading branch information
vgv committed May 22, 2024
1 parent bbfb2bc commit 5bdd00f
Show file tree
Hide file tree
Showing 4 changed files with 355 additions and 0 deletions.
12 changes: 12 additions & 0 deletions src/main/kotlin/com/ecwid/clickhouse/Types.kt
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,12 @@ enum class PlatformType(val platformName: String) {
INT_64("Int64"),
INT_64_NULLABLE("Nullable(Int64)"),

INT_128("Int128"),
INT_128_NULLABLE("Nullable(Int128)"),

INT_256("Int256"),
INT_256_NULLABLE("Nullable(Int256)"),

UINT_8("UInt8"),
UINT_8_NULLABLE("Nullable(UInt8)"),

Expand All @@ -28,6 +34,12 @@ enum class PlatformType(val platformName: String) {
UINT_64("UInt64"),
UINT_64_NULLABLE("Nullable(UInt64)"),

UINT_128("UInt128"),
UINT_128_NULLABLE("Nullable(UInt128)"),

UINT_256("UInt256"),
UINT_256_NULLABLE("Nullable(UInt256)"),

// --------------------------------------------------------------
FLOAT_32("Float32"),
FLOAT_32_NULLABLE("Nullable(Float32)"),
Expand Down
105 changes: 105 additions & 0 deletions src/main/kotlin/com/ecwid/clickhouse/convert/Convert.kt
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.ecwid.clickhouse.convert

import java.math.BigDecimal
import java.math.BigInteger
import java.text.SimpleDateFormat
import java.util.*

Expand Down Expand Up @@ -139,6 +140,58 @@ object Convert {
fun fromNullableArray(array: List<Long?>) = array.map(::fromNullableValue)
}

object Int128 {
@JvmStatic
fun toValue(str: String?) = requireNotNull(str).toBigInteger()

@JvmStatic
fun toNullableValue(str: String?) = str?.toBigInteger()

@JvmStatic
fun toArray(array: List<String?>) = array.map(::toValue)

@JvmStatic
fun toNullableArray(array: List<String?>) = array.map(::toNullableValue)

@JvmStatic
fun fromValue(value: BigInteger) = value.toString()

@JvmStatic
fun fromNullableValue(value: BigInteger?) = value?.toString()

@JvmStatic
fun fromArray(array: List<BigInteger>) = array.map(::fromValue)

@JvmStatic
fun fromNullableArray(array: List<BigInteger?>) = array.map(::fromNullableValue)
}

object Int256 {
@JvmStatic
fun toValue(str: String?) = requireNotNull(str).toBigInteger()

@JvmStatic
fun toNullableValue(str: String?) = str?.toBigInteger()

@JvmStatic
fun toArray(array: List<String?>) = array.map(::toValue)

@JvmStatic
fun toNullableArray(array: List<String?>) = array.map(::toNullableValue)

@JvmStatic
fun fromValue(value: BigInteger) = value.toString()

@JvmStatic
fun fromNullableValue(value: BigInteger?) = value?.toString()

@JvmStatic
fun fromArray(array: List<BigInteger>) = array.map(::fromValue)

@JvmStatic
fun fromNullableArray(array: List<BigInteger?>) = array.map(::fromNullableValue)
}

object UInt32 {
@JvmStatic
fun toValue(str: String?) = Integer.parseUnsignedInt(requireNotNull(str))
Expand Down Expand Up @@ -191,6 +244,58 @@ object Convert {
fun fromNullableArray(array: List<Long?>) = array.map(::fromNullableValue)
}

object UInt128 {
@JvmStatic
fun toValue(str: String?) = requireNotNull(str).toBigInteger()

@JvmStatic
fun toNullableValue(str: String?) = str?.toBigInteger()

@JvmStatic
fun toArray(array: List<String?>) = array.map(::toValue)

@JvmStatic
fun toNullableArray(array: List<String?>) = array.map(::toNullableValue)

@JvmStatic
fun fromValue(value: BigInteger) = value.toString()

@JvmStatic
fun fromNullableValue(value: BigInteger?) = value?.toString()

@JvmStatic
fun fromArray(array: List<BigInteger>) = array.map(::fromValue)

@JvmStatic
fun fromNullableArray(array: List<BigInteger?>) = array.map(::fromNullableValue)
}

object UInt256 {
@JvmStatic
fun toValue(str: String?) = requireNotNull(str).toBigInteger()

@JvmStatic
fun toNullableValue(str: String?) = str?.toBigInteger()

@JvmStatic
fun toArray(array: List<String?>) = array.map(::toValue)

@JvmStatic
fun toNullableArray(array: List<String?>) = array.map(::toNullableValue)

@JvmStatic
fun fromValue(value: BigInteger) = value.toString()

@JvmStatic
fun fromNullableValue(value: BigInteger?) = value?.toString()

@JvmStatic
fun fromArray(array: List<BigInteger>) = array.map(::fromValue)

@JvmStatic
fun fromNullableArray(array: List<BigInteger?>) = array.map(::fromNullableValue)
}

object Float32 {
@JvmStatic
fun toValue(str: String?) = requireNotNull(str).toFloat()
Expand Down
169 changes: 169 additions & 0 deletions src/main/kotlin/com/ecwid/clickhouse/typed/TypedRow.kt
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package com.ecwid.clickhouse.typed
import com.ecwid.clickhouse.convert.Convert
import com.ecwid.clickhouse.raw.RawRow
import java.math.BigDecimal
import java.math.BigInteger
import java.util.*

data class TypedRow(
Expand Down Expand Up @@ -189,6 +190,90 @@ data class TypedRow(
return Convert.Int64.toNullableArray(array)
}

// ----------------- INT_128 --------------------

fun getInt128(columnIndex: Int): BigInteger {
val scalar = rawRow.getScalarValue(columnIndex)
return Convert.Int128.toValue(scalar)
}

fun getInt128Array(columnIndex: Int): List<BigInteger> {
val array = rawRow.getArrayValue(columnIndex)
return Convert.Int128.toArray(array)
}

fun getInt128Nullable(columnIndex: Int): BigInteger? {
val scalar = rawRow.getScalarValue(columnIndex)
return Convert.Int128.toNullableValue(scalar)
}

fun getInt128NullableArray(columnIndex: Int): List<BigInteger?> {
val array = rawRow.getArrayValue(columnIndex)
return Convert.Int128.toNullableArray(array)
}

fun getInt128(columnName: String): BigInteger {
val scalar = rawRow.getScalarValue(columnName)
return Convert.Int128.toValue(scalar)
}

fun getInt128Array(columnName: String): List<BigInteger> {
val array = rawRow.getArrayValue(columnName)
return Convert.Int128.toArray(array)
}

fun getInt128Nullable(columnName: String): BigInteger? {
val scalar = rawRow.getScalarValue(columnName)
return Convert.Int128.toNullableValue(scalar)
}

fun getInt128NullableArray(columnName: String): List<BigInteger?> {
val array = rawRow.getArrayValue(columnName)
return Convert.Int128.toNullableArray(array)
}

// ----------------- INT_256 --------------------

fun getInt256(columnIndex: Int): BigInteger {
val scalar = rawRow.getScalarValue(columnIndex)
return Convert.Int256.toValue(scalar)
}

fun getInt256Array(columnIndex: Int): List<BigInteger> {
val array = rawRow.getArrayValue(columnIndex)
return Convert.Int256.toArray(array)
}

fun getInt256Nullable(columnIndex: Int): BigInteger? {
val scalar = rawRow.getScalarValue(columnIndex)
return Convert.Int256.toNullableValue(scalar)
}

fun getInt256NullableArray(columnIndex: Int): List<BigInteger?> {
val array = rawRow.getArrayValue(columnIndex)
return Convert.Int256.toNullableArray(array)
}

fun getInt256(columnName: String): BigInteger {
val scalar = rawRow.getScalarValue(columnName)
return Convert.Int256.toValue(scalar)
}

fun getInt256Array(columnName: String): List<BigInteger> {
val array = rawRow.getArrayValue(columnName)
return Convert.Int256.toArray(array)
}

fun getInt256Nullable(columnName: String): BigInteger? {
val scalar = rawRow.getScalarValue(columnName)
return Convert.Int256.toNullableValue(scalar)
}

fun getInt256NullableArray(columnName: String): List<BigInteger?> {
val array = rawRow.getArrayValue(columnName)
return Convert.Int256.toNullableArray(array)
}

// ----------------- UINT_8 --------------------

fun getUInt8(columnIndex: Int): Int {
Expand Down Expand Up @@ -356,6 +441,90 @@ data class TypedRow(
return Convert.UInt64.toNullableArray(array)
}

// ----------------- UINT_128 --------------------

fun getUInt128(columnIndex: Int): BigInteger {
val scalar = rawRow.getScalarValue(columnIndex)
return Convert.UInt128.toValue(scalar)
}

fun getUInt128Array(columnIndex: Int): List<BigInteger> {
val array = rawRow.getArrayValue(columnIndex)
return Convert.UInt128.toArray(array)
}

fun getUInt128Nullable(columnIndex: Int): BigInteger? {
val scalar = rawRow.getScalarValue(columnIndex)
return Convert.UInt128.toNullableValue(scalar)
}

fun getUInt128NullableArray(columnIndex: Int): List<BigInteger?> {
val array = rawRow.getArrayValue(columnIndex)
return Convert.UInt128.toNullableArray(array)
}

fun getUInt128(columnName: String): BigInteger {
val scalar = rawRow.getScalarValue(columnName)
return Convert.UInt128.toValue(scalar)
}

fun getUInt128Array(columnName: String): List<BigInteger> {
val array = rawRow.getArrayValue(columnName)
return Convert.UInt128.toArray(array)
}

fun getUInt128Nullable(columnName: String): BigInteger? {
val scalar = rawRow.getScalarValue(columnName)
return Convert.UInt128.toNullableValue(scalar)
}

fun getUInt128NullableArray(columnName: String): List<BigInteger?> {
val array = rawRow.getArrayValue(columnName)
return Convert.UInt128.toNullableArray(array)
}

// ----------------- UINT_256 --------------------

fun getUInt256(columnIndex: Int): BigInteger {
val scalar = rawRow.getScalarValue(columnIndex)
return Convert.UInt256.toValue(scalar)
}

fun getUInt256Array(columnIndex: Int): List<BigInteger> {
val array = rawRow.getArrayValue(columnIndex)
return Convert.UInt256.toArray(array)
}

fun getUInt256Nullable(columnIndex: Int): BigInteger? {
val scalar = rawRow.getScalarValue(columnIndex)
return Convert.UInt256.toNullableValue(scalar)
}

fun getUInt256NullableArray(columnIndex: Int): List<BigInteger?> {
val array = rawRow.getArrayValue(columnIndex)
return Convert.UInt256.toNullableArray(array)
}

fun getUInt256(columnName: String): BigInteger {
val scalar = rawRow.getScalarValue(columnName)
return Convert.UInt256.toValue(scalar)
}

fun getUInt256rray(columnName: String): List<BigInteger> {
val array = rawRow.getArrayValue(columnName)
return Convert.UInt256.toArray(array)
}

fun getUInt256Nullable(columnName: String): BigInteger? {
val scalar = rawRow.getScalarValue(columnName)
return Convert.UInt256.toNullableValue(scalar)
}

fun getUInt256NullableArray(columnName: String): List<BigInteger?> {
val array = rawRow.getArrayValue(columnName)
return Convert.UInt256.toNullableArray(array)
}

// ----------------- FLOAT_32 --------------------
fun getFloat32(columnIndex: Int): Float {
val scalar = rawRow.getScalarValue(columnIndex)
Expand Down
Loading

0 comments on commit 5bdd00f

Please sign in to comment.