PNixx / clickhouse-activerecord

A Ruby database ActiveRecord driver for ClickHouse

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

SUM() columns will get cast to strings

jbwl opened this issue · comments

Gem version: 0.5.15 / branch: rails_7
Rails version: 7.0.8 (also tested on 6.x)

Expected result: This adapter should not cast int values to strings when a sum() with no column alias or an arbitrary alias is being used, like the MySQL adapter can.
Actual result: This adapter fails to typecast correctly when either no, or an arbitrary alias is applied to a summed int column.

This works correctly:

=> MyModel.select("productid,sum(units) as units").group('productid').where(productid: 'abc')

#<MyModel:0x0000ffff8f682448> {
         "productid" => "abc",
         "units" => 999
    }

This does not (no alias name on sum column) leads to int value being cast to string:

=> MyModel.select("productid,sum(units)").group('productid').where(productid: 'abc')

#<MyModel:0x0000ffff8f682448> {
         "productid" => "abc",
         "sum(units)" => "999"
    }

This also does not work (alias name on sum column != column name) leads to int value being cast to string:

=> MyModel.select("productid,sum(units) as my_units").group('productid').where(productid: 'abc')

#<MyModel:0x0000ffff8f682448> {
         "productid" => "abc",
         "my_units" => "999"
    }

It looks like the type casting happens whenever the adapter is unable to detect the column used, i.e. "sum(units)" isn't detected as being the units column, and also "sum(units) as my_units" isn't. This flaw does not exist for example in the MySQL adapter.

Because we are using higher level query builders (i.e. ActiveCube) and have no control over the aliases used in the queries, it's mandatory that it is handled correctly on the adapter level.

duplicate with #92