-
Notifications
You must be signed in to change notification settings - Fork 3.2k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Map Int128/UInt128 where possible #28498
Comments
Am not aware of any database which has these as a native type, though a mapping to decimal/numeric (e.g. via a value converter) is possible, if we think there's value in that. |
A I also image that some would want to map to In the meantime, an example of how one would manually convert this could be helpful. Would you convert to and from string to get this working? |
I don't think we'd want a value conversion that loses information...
Maybe... I'm not sure to what extent that would actually be useful/used.
I think the idea here is to implement this in the ADO.NET providers (e.g. Npgsql, Microsoft.Data.SQLite), where the Int128 gets encoded directly to the database wire protocol representation of the database decimal (when that makes sense and doesn't lose data). Unless I'm mistaken, you cannot losslessly convert Int128 to .NET decimal; although both are 128-bit, the range of .NET decimal (which also has flags/fraction bits) is smaller than the range of Int128, where all bits are used to simply represent the integer value. It's probably possible to do something like an Unsafe cast, but at that point the resulting decimal representation has nothing more to do with the original Int128 value; you might as well be storing it as binary. Of course, if what you want is to represent an Int128 as textual data in the database, then you can do that as usual by using ToString in the value converter. |
I was going with your earlier suggestion: "though a mapping to decimal/numeric (e.g. via a value converter) is possible". Since the title mentions Such a conversion is useful when you have numbers in .NET that are
This issue is about being able to map .NET's Did you have something else in mind?
That would be ideal. But until we have that, I think we should have an example of to employ a custom converter to get those
Exactly. I believe that's the point of the issue. .NET has a type that can hold a 128-bit integer; the database has a few types that could do the job... now we want to be able to get those large integers in or out, i.e. (the title) map Int128/UInt128 where possible - and .NET's In summary, I'm considering which database types this issue applies to and how we could implement the mapping (until the providers support it out-of-the-box). |
Sure, but such a limited/lossy conversion isn't something that should just happen transparently out-of-the-box.
PostgreSQL decimal can hold it, and on SQLite we already use a string representation (IIRC?) for decimal, so we could do the same for Int128. As above, these would need to be done in the ADO.NET providers, and then also mapped at the EF level.
But there's no way to do that (except for string/byte[]), as we've been discussing; EF value converters simply convert from one (model) .NET value to another (provider) .NET value, and then the 2nd one gets persisted. The only .NET conversions I'm aware of which wouldn't be lossy is byte[] (which I'm not sure anyone actually wants) or string. Neither of these allows any numeric database operation, querying, etc.
So again, if you just want to convert to a string, do ToString() in your custom value converter. A conversion to byte[] is a bit more elaborate, but should be pretty trivial as well. |
My first thought was to agree, but then I thought of strings. If a
Postgres wins again. 😄
You've mentioned the cases of wanting to convert to string and wanting textual data in the database. That's all clear. I'm trying to clarify if you mean that this also serves as a workaround to insert a |
I don't think we map to limited-length strings by default, e.g. on SQL Server a string is mapped to
I don't think so, because SqlClient (I think!) wouldn't allow you to insert a .NET string into a SQL Server DECIMAL column, right? |
Right, I didn't mean a default mapping, but just the ability to map it in the first place. I see the confusion: I interpreted the title as "make it possible to map to sensible column types", but it can just as sensibly be read as "map by default where lossless". So the ideal scenario is a database that supports Since the (U)Int128 will have to be passed to the provider in some way that works, I'm hopeful that this behavior will also make an explicit call to All things considered, I'd understand if
I haven't tried it either! I'm hoping it lets you send in most data types as strings, since the language does too. It seems... unnecessary for it to be less forgiving. |
In the meantime - what are the real world concerns with using like:
Is there a specific number size where it becomes an issue? |
@OpenSpacesAndPlaces shouldn't be a problem. Ths strings could get arbitrary long based on the size of your BigInteger instances, and of course you can't query them as numbers. |
No description provided.
The text was updated successfully, but these errors were encountered: