Is there any reason that the value passed to type_cast_using_column during an ActiveRecord::Calculation#sum defaults to the string '0' when there are no rows?
Theoretically, '0' will get type cast to the column type, but if the type cannot be inferred from the table information, then you get '0' back, which seems odd to me.
Surely it should be 0 rather than '0' passed to type_cast_using_column, no?
I tried to go a way back like beginning of 2010 and it was still there. Not sure why a sum() would return a String, true. I currently converting it to Float (my case) with …sum(:column_name).to_f
It would be nice if calculations could always rely on the raw_connection to return data types converted to ruby primitives correctly. Not sure if that ‘0’ should be a 0 or not, maybe be helpful to see what fails if changed?
That ‘0’ likely just predates the raw_connection returning rich data types. I believe sql server and oracle have done so for ages, but it’s relatively new for the other adapters.
In preparation for 4.0 perhaps we could do an audit of the various database drivers and make sure they return the correct types. After that we could remove the custom typecasting we do?
That '0' likely just predates the raw_connection returning rich data types. I believe sql server and oracle have done so for ages, but it's relatively new for the other adapters.
Yea, our Ruby ODBC does not return proper types, but our TinyTDS which has been out for a year or more now does. Just like MySQL2 which it was modeled after.
In preparation for 4.0 perhaps we could do an audit of the various database drivers and make sure they return the correct types. After that we could remove the custom typecasting we do?
It depends on the level of API people want to use. For example, PG
always returns strings. That means we need to know the types for which
we're querying. Say someone does this against pg:
connection.execute 'select 1.2 + 20'
we can't know to cast that to a float. Even doing regexp tests will be
wrong since someone may have tried to select a string.
We can do automatic casting, but it's going to break down at some point
depending on the level of abstraction and the database used.
Actually, you can if you use the mysql, pg, postgres, and postgres-pr
drivers (and most other drivers that return strings for everything).
When you get the results of the query, you can query the metadata of
the results to get the types of the columns. Sequel uses this
information to typecast such results correctly, and there is no reason
ActiveRecord couldn't do the same:
It depends on the level of API people want to use. For example, PG
always returns strings. That means we need to know the types for which
we’re querying. Say someone does this against pg:
connection.execute ‘select 1.2 + 20’
we can’t know to cast that to a float. Even doing regexp tests will be
wrong since someone may have tried to select a string.
Most database protocols include type information along with the values themselves, the drivers should be able to do introspection and ‘magically’ cast them completely removing our casting from the picture.
We can do automatic casting, but it’s going to break down at some point
depending on the level of abstraction and the database used.
For a 4.x release though I think we can expect the database drivers to step up and handle that conversion natively. Unless there are any supported databases whose network protocol doesn’t include enough information for the driver to do it itself.
I'm not sure if the pg "over the wire" protocol contains that
information, but I can tell you that libpg doesn't provide functions for
casting to C types (that I can find).
PQftype returns a type id integer that represents the database type.
The ruby postgres drivers (pg, postgres, postgres-pr) expose this
information via the type or ftype method on the PGresult class. The
common database types have fixed type id integers you can statically
map to ruby classes (16 -> boolean, 700 and 701 -> float, etc.).
You may be correct that libpq lacks a function that casts to C types.
But it's fairly simple to cast the C string directly to the desired
ruby type using a mapping based on the type id integer.