Skip to content

Commit

Permalink
Adding support for BigDecimal
Browse files Browse the repository at this point in the history
  • Loading branch information
Leonard Wolters committed Sep 11, 2023
1 parent 3d14b83 commit 72908fc
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -176,6 +176,31 @@ class TypeCastFunctionsIT extends DslITSpec {
r(toInt32OrDefault("12\n58 gram", 123)) should be("123")
}

it should "handle BigDecimalOrDefault" in {
// orNull only accept STRING (and only available in version 22.3 and upwards)
assumeMinimalClickhouseVersion(22, 3)

r(toBigDecimalOrDefault("1", 123)) should be("1")
r(toBigDecimalOrDefault(Byte.MaxValue.toString, 123)) should be("127")
r(toBigDecimalOrDefault(Byte.MinValue.toString, 123)) should be("-128")
r(toBigDecimalOrDefault(Short.MaxValue.toString, 123)) should be("32767")
r(toBigDecimalOrDefault(Short.MinValue.toString, 123)) should be("-32768")
r(toBigDecimalOrDefault(Int.MaxValue.toString, 123)) should be("2147483647")
r(toBigDecimalOrDefault(Int.MinValue.toString, 123)) should be("-2147483648")
r(toBigDecimalOrDefault(Long.MaxValue.toString, 123)) should be("9223372036854776000")
r(toBigDecimalOrDefault(Long.MinValue.toString, 123)) should be("-9223372036854776000")
r(toBigDecimalOrDefault(Float.MaxValue.toString, 123)) should be("3.4028235000000003e38")
r(toBigDecimalOrDefault(Float.MinValue.toString, 123)) should be("-3.4028235000000003e38")
r(toBigDecimalOrDefault(Double.MaxValue.toString, 123)) should be("1.7976931348623157e308")
r(toBigDecimalOrDefault(Double.MinValue.toString, 123)) should be("-1.7976931348623157e308")

r(toBigDecimalOrDefault("", 123)) should be("123")
r(toBigDecimalOrDefault("error", 123)) should be("123")
r(toBigDecimalOrDefault("er\nror", 123)) should be("123")
r(toBigDecimalOrDefault("12\n58", 123)) should be("123")
r(toBigDecimalOrDefault("12\n58 gram", 123)) should be("123")
}

def r(col: TypeCastColumn[_]): String =
execute(select(col)).futureValue.trim
}
Original file line number Diff line number Diff line change
Expand Up @@ -80,10 +80,10 @@ trait TypeCastFunctions { self: Magnets =>
extends TypeCastColumn[Double](tableColumn)
with Reinterpretable

case class BigDecimal(tableColumn: ConstOrColMagnet[_],
orZero: Boolean = false,
orDefault: Option[BigDecimal] = None,
orNull: Boolean = false)
case class BigDecimal64(tableColumn: ConstOrColMagnet[_],
orZero: Boolean = false,
orDefault: Option[BigDecimal] = None,
orNull: Boolean = false)
extends TypeCastColumn[BigDecimal](tableColumn)
with Reinterpretable

Expand Down Expand Up @@ -199,12 +199,12 @@ trait TypeCastFunctions { self: Magnets =>
def toFloat64OrNull(tableColumn: ConstOrColMagnet[_]): Float64 = Float64(tableColumn, orNull = true)
def toFloat64OrZero(tableColumn: ConstOrColMagnet[_]): Float64 = Float64(tableColumn, orZero = true)

def toBigDecimal(tableColumn: ConstOrColMagnet[_]): BigDecimal = BigDecimal(tableColumn)
def toBigDecimal(tableColumn: ConstOrColMagnet[_]): BigDecimal64 = BigDecimal64(tableColumn)

def toBigDecimalOrDefault(tableColumn: ConstOrColMagnet[_], value: BigDecimal): BigDecimal =
BigDecimal(tableColumn, orDefault = Option(value))
def toBigDecimalOrNull(tableColumn: ConstOrColMagnet[_]): BigDecimal = BigDecimal(tableColumn, orNull = true)
def toBigDecimalOrZero(tableColumn: ConstOrColMagnet[_]): BigDecimal = BigDecimal(tableColumn, orZero = true)
def toBigDecimalOrDefault(tableColumn: ConstOrColMagnet[_], value: BigDecimal): BigDecimal64 =
BigDecimal64(tableColumn, orDefault = Option(value))
def toBigDecimalOrNull(tableColumn: ConstOrColMagnet[_]): BigDecimal64 = BigDecimal64(tableColumn, orNull = true)
def toBigDecimalOrZero(tableColumn: ConstOrColMagnet[_]): BigDecimal64 = BigDecimal64(tableColumn, orZero = true)

def toDate(tableColumn: ConstOrColMagnet[_]): DateRep = DateRep(tableColumn)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ trait TypeCastFunctionTokenizer {
case c: Int64 => tknz(c.tableColumn.column, ColumnType.Int64, c.orZero, c.orNull, c.orDefault)
case c: Float32 => tknz(c.tableColumn.column, ColumnType.Float32, c.orZero, c.orNull, c.orDefault)
case c: Float64 => tknz(c.tableColumn.column, ColumnType.Float64, c.orZero, c.orNull, c.orDefault)
case c: BigDecimal => tknz(c.tableColumn.column, ColumnType.Float64, c.orZero, c.orNull, c.orDefault)
case c: BigDecimal64 => tknz(c.tableColumn.column, ColumnType.Float64, c.orZero, c.orNull, c.orDefault)
case c: DateRep => tknz(c.tableColumn.column, ColumnType.Date, c.orZero, c.orNull, c.orDefault)
case c: DateTimeRep => tknz(c.tableColumn.column, ColumnType.DateTime, c.orZero, c.orNull, c.orDefault)
case c: Uuid => tknz(c.tableColumn.column, ColumnType.UUID, c.orZero, c.orNull, c.orDefault)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,5 +9,7 @@ class TypeCaseFunctionTokenizerTest extends DslTestSpec {
toSQL(select(toUUID(const("00000000-0000-0000-0000-000000000000")))) shouldBe "SELECT toUUID('00000000-0000-0000-0000-000000000000')"
toSQL(select(toUUIDOrZero(const("123")))) shouldBe "SELECT toUUIDOrZero('123')"
toSQL(select(toUUIDOrNull(const("123")))) shouldBe "SELECT toUUIDOrNull('123')"

toSQL(select(toBigDecimalOrZero(const("123")))) shouldBe "SELECT toFloat64OrZero('123')"
}
}

0 comments on commit 72908fc

Please sign in to comment.