elixir-sqlite/sqlitex

Decimal Support

jazzyb opened this issue · 4 comments

The Postgrex and Mariaex database drivers both support Decimal types, an arbitrary precision decimal module. One Ecto integration test even relies on adapters handling Decimals. I don't currently know anything about how important or widely used this library is, but I wanted to go ahead and start the discussion about whether or not to include support for Decimal in Sqlitex. This is not a high priority.

I've tried to handle Decimals in Sqlite.Ecto, but it is rather complicated since Sqlite.Ecto has so far assumed Sqlitex will handle all the type logic/conversions.

Sqlite.Ecto issue for reference: jazzyb/sqlite_ecto#26

I think supporting decimal types would be a good idea and it looks like there is a pretty solid pattern for us to follow from postgrex and mariaex so we don't have to invent a new datastructure for representing decimals ourselves. Here are my initial concerns:

  • is the decimal package from hex a ubiquitous standard for representing decimals? Will a user for sqlitex ever want to get back decimal results in some other form?
  • can sqlite3 do decimal math? If I create a table that has a DECIMAL(10,5) field and I do a SUM across multiple rows, will it do the math correctly? Or does it just turn them into floats when doing the sum? If the database itself doesn't do decimal math correctly, then it might be silly to support it in our API since you get back invalid results anyway.

"is the decimal package from hex a ubiquitous standard for representing decimals?"

  • It appears to be.

"Will a user for sqlitex ever want to get back decimal results in some other form?"

  • Perhaps, but we can key the Decimal to some type like "decimal", and if a user wants to get back normal floating point values, they can use "float" or "numeric" instead for example.

"can sqlite3 do decimal math?"

  • Nope. :/ If the value is stored as a SQLite floating point type, then SQLite will perform 64-bit precision arithmetic on the value. However, I don't think it would be that hard to check the type and adjust the value based on the declared type, e.g. DECIMAL(10,5). It wouldn't be any different than a person using SQLite directly to store such values.

By the way, Decimal support is desired for all adapters: elixir-ecto/ecto#634

I can get around to implementing this in a couple of weeks and see how much of a pain it turns into. If it's too much, I can go back to José and have the Decimal tests ignored.

👍 sounds good to me. Let me know if I can help out. I'm happy to pitch it to get part of it done if there is a good way to segment the work

I don't know that there is a way to segment the work, but you're welcome to work on it. Like I said, I probably won't find the time to get around to it for a while.

If you start working on it, make sure you push a branch to github, and I'll try to keep up with changes in Sqlite.Ecto and make sure the integration tests are passing.