plausible / ecto_ch

ClickHouse adapter for Ecto
MIT License
79 stars 11 forks source link

Large Ints are not supported in parameterized queries. #180

Closed aschreifels closed 4 months ago

aschreifels commented 4 months ago

Problem Description

Currently all parameterized queries (schema less or dynamic) will default to either Int64 type or UInt64 Type and does not support going beyond the UInt64 boundary.

It would be nice to support this for usage of ClickHouse that has columns using Int128, UInt128, Int256, & UInt256

This support could be easily added in connection.ex. See below:

Solution

# connection.ex:1097
defp param_type(i) when is_integer(i) and i > 0x7FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF, do: "UInt256"
defp param_type(i) when is_integer(i) and i > 0xFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF, do: "Int256"
defp param_type(i) when is_integer(i) and i > 0x7FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF, do: "UInt128"
defp param_type(i) when is_integer(i) and i > 0xFFFFFFFFFFFFFFFF, do: "Int128"
defp param_type(i) when is_integer(i) and i > 0x7FFFFFFFFFFFFFFF, do: "UInt64"
defp param_type(i) when is_integer(i), do: "Int64"

Test

test "integer boundaries" do
    params =
    [
      9223372036854775807, #Int64 Boundary
      18446744073709551615, #UInt64 Boundary
      170141183460469231731687303715884105727, #Int128 Boundary
      340282366920938463463374607431768211455, #UInt128 Boundary
      57896044618658097711785492504343953926634992332820282019728792003956564819967, #Int256 Boundary
      115792089237316195423570985008687907853269984665640564039457584007913129639935, #UInt256 Boundary
    ]

    assert to_string(Connection.build_params(_ix = 0, _len = 6, params)) ==
             "{$0:Int64},{$1:UInt64},{$2:Int128},{$3:UInt128},{$4:Int256},{$5:UInt256}"
end