Repository navigation
fix: a conversion that can change the value is refused where the lambda is written - #130
Merged
Merged
Conversation
…da is written
Every Convert node was left out of the template, so (int)x.Amount == 2 on a
decimal was stored as {Amount} = 2 and the constraint covered other rows than
the lambda names (#103).
The conversions left out are now a table, SilentConversions: the widenings
between integer types, and char to int for the comparison of a char. A
Nullable<T> on either side is looked through, and a conversion to a type the
source already is needs no entry. One pass over the lambda refuses every other
conversion of what it reads from the entity, the conversion lambda of a ??
included, ahead of the dispatch. A conversion of a captured value is done by
C# and its result written: (int)limit was written as 2.5.
The parity sweep reads the conversions from the trees the compiler built and
crosses the table with a column and a sample of the target type. EF Core writes
a widening between two store types as a cast, which the translator does not
render yet: 432 rows waived as answering alike, checked live (#125).
Closes #103
Co-Authored-By: Claude Fable 5.2 <[email protected]>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Closes #103
Problem
LambdaTranslatortook everyConvertnode off and wrote what was under it. That is right for the conversions the compiler adds and wrong for one that changes the value:was stored as
{Amount} = 2and reached the database asWHERE ("Amount" = 2). ForAmount = 2.5the lambda is true and the predicate false: the row is outside the constraint, and nothing was refused. The same on the value's side:x.Rank == (int)limitwith a capturedlimitof 2.5 was written as{Rank} = 2.5.Change
LambdaTranslator.SilentConversionsis a table likeStringMethodsandLiteralTypes: the pairs of different types whose conversion is left out of a template.IsSilentConversion(from, to)is the whole rule, decided from the two types alone.ConversionCheck), refuses every other conversion of what the lambda reads from the entity, withNotSupportedExceptionand a message that names the conversion and says what to write. The dispatch takes a conversion off in eight places (a comparison's operand, a null test, a concatenation, …); a check in each would be one forgotten. The conversion lambda of a??(x.Score ?? x.Price) is held to the same table, and so isConvertChecked.(int)x.Statusis refused as a conversion.Left out of the template, as before
Nullable<T>, and back ((int)x.Scoreisx.Score.Value)object, an interface,Enumsbyte→short,int,long;short→int,long;int→longbyte→short,ushort,int,uint,long,ulong;ushort→int,uint,long,ulong;uint→long,ulongchar→intRefused: everything else. To an integer from
decimal,double,float; to a narrower integer or one of the other sign (long→int,int→uint,ulong→long); any type tofloatordouble, includingint→doubleandfloat→double; any type todecimal;charto anything butint; an enum to its number outside a comparison; a downcast; a user-defined conversion.What the compiler emits for comparisons the tests already held (each is a row of
A_conversion_that_keeps_every_value_is_left_out_as_before, which states the conversions in its tree and the template it had): ashort,byte,sbyteorushortagainst anything is promoted toint;intagainstlongtolong;uintagainstintboth tolong;byteagainstuinttouint; a column against a nullable one is lifted;+on a string and a number boxes the number toobject; acharis compared as anint.char→intis the one case that stays accepted and is no plain widening: the tree ofx.Grade == 'A'is the tree of(int)x.Grade == 65, and the comparison writes the constant back as the char.The parity sweep crosses the table. A row's conversions are read from the tree the compiler built (
ParityCatalogue.ConversionsIn),AcceptedConstructslists the table, and the catalogue adds, for every pair, a column of the source type against a column and a sample of the target type, bare and with the cast written out.ParityRowgot ansbyte, aushortand auintcolumn for it.Tests
LambdaTranslatorTests, five new tests and the table's list inThe_accepted_constructs_are_in_the_tables;PgExclusionDeclarationTests.A_cast_that_can_change_the_value_fails_at_the_declaration. Locally: build without a warning; kernel suite 522 passed with PostgreSQL 18 (TranslatorParityLiveTestsincluded); PostgreSQL package 193 passed;SchemaKit.ApiProbes125 passed.Each rule broken in a scratch copy, and the tests that fail (non-integration runs):
A_cast_that_can_change_the_value_is_refused_where_it_is_written,Every_other_conversion_between_numbers_is_refused,A_pair_of_numeric_types_is_silent_exactly_when_the_table_has_it, and the PostgreSQL package'sA_cast_that_can_change_the_value_fails_at_the_declaration.A_conversion_that_keeps_every_value_is_left_out_as_beforepasses, which is what "as before" rests onshort→inttaken out of the tableA_conversion_that_keeps_every_value_is_left_out_as_before,The_accepted_constructs_are_in_the_tables, the sweep's "Every construct the translator accepts is compared with EF Core at least once"int→doubleadded to the tableEvery_other_conversion_between_numbers_is_refused,The_accepted_constructs_are_in_the_tables, the sweep's "Every typed filter and expression renders as EF Core's query…"A_conversion_of_a_captured_value_is_done_before_the_value_is_writtenConvertCheckednot held to the tableEvery_other_conversion_between_numbers_is_refusedWhat_a_predicate_cannot_say_is_refused,Every_other_conversion_between_numbers_is_refusedNullable<T>not looked throughA_conversion_that_keeps_every_value_is_left_out_as_before,A_pair_of_numeric_types_is_silent_exactly_when_the_table_has_it, the sweep's "No waiver outlives the disagreement it waives"??conversion lambda not checkedA_cast_that_can_change_the_value_is_refused_where_it_is_writtenWhat the issue did not expect
p."Int"::bigint = 5forx.Int == 5L) where the translator writes"Int" = 5. They select the same rows, which the live sweep checks, but a partial index on the second does not serve the first (measured on PostgreSQL 18). This was true before this change and unseen, since the sweep compared a column only with its own type: A widening the translator leaves out is a cast in EF Core's query: x.Int == 5L is stored as {Int} = 5, and the index does not serve "Int"::bigint = 5 #125.ConvertCheckedfor the promotions too, sox.Small == 5is refused there, before and after this change: In a checked context every promoted comparison is refused: the compiler writes ConvertChecked, and the translator knows Convert #126.x.Ratio == 0.1fis stored as{Ratio} = 0.1, which PostgreSQL compares asdouble precisionand no row holding0.1::realmatches: A float literal is written with its shortest digits, and PostgreSQL compares it as double precision: x.Ratio == 0.1f matches no row that holds 0.1f #127. Not caused by this change.floatcaptured and compared with adoublecolumn is converted before it is written,0.10000000149011612for0.1f, which is the value C# compares. It was written as0.1.Decisions in this PR that are not confirmed
int→decimal,int→doubleandfloat→doubleare refused, although each keeps every value and the compiler adds them unasked:x.Rank > 2.5m,x.Rank > 2.5,x.Ratio > 0.5and a comparison of anintcolumn with adecimalone were accepted until now and are refused. The reason is the task's rule (integer widenings only) and that how a database types the two sides is its own rule (SQL Server reads0.5as a decimal and converts it toreal). The message says to write the value in the property's type (0.5f). Each is one row of the table if the maintainer wants it back.char→intstays silent everywhere, not only as a comparison's operand, soE(x => (int)x.Grade)is the column.Nullable<T>→Tstays silent, as.Valueis inValueMembers.ParityRow.🤖 Generated with Claude Code