-
Notifications
You must be signed in to change notification settings - Fork 3.9k
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
sql: truncate floats to FLOAT4 correctly #73743
Comments
The issue can be observed with explicit casts in the demo. In the example below, the result value should be 18755000.
|
The `EliminateAssignmentCast` rule has been combined with the `EliminateCast` rule. Now an assignment cast is eliminated if the source and target type are identical. This now possible thanks to some changes to type resolution, including: 1. Placeholder types are resolved with unspecified type modifiers. This ensures that assignment casts won't be eliminated if the a placeholder value does not conform to the target type's modifiers. 2. When constant integer `NumVal`s are resolved as a typed-value, they are validated to ensure they fit within their type's width. There may be more types we need to perform similar validation for, such as floats (see cockroachdb#73743). 3. Columns in values expressions with values that have non-identical types but the same type OID will be typed with type modifiers. For example, if a values expression has a CHAR(1) value and a CHAR(3) value in the same column, the column will be typed as a CHAR without an explicit width. Fixes cockroachdb#73450 Release note (bug fix): A bug has been fixed that caused incorrect evaluation of placeholder values in EXECUTE statements. The bug presented when the PREPARE statement cast a placeholder value, for example `PREPARE s AS SELECT $1::INT2`. If the assigned value for `$1` exceeded the maximum width value of the cast target type, the result value of the cast could be incorrect. This bug has been present since version 19.1 or earlier.
The `EliminateAssignmentCast` rule has been combined with the `EliminateCast` rule. Now an assignment cast is eliminated if the source and target type are identical. This now possible thanks to some changes to type resolution, including: 1. Placeholder types are resolved with unspecified type modifiers. This ensures that assignment casts won't be eliminated if the a placeholder value does not conform to the target type's modifiers. 2. When constant integer `NumVal`s are resolved as a typed-value, they are validated to ensure they fit within their type's width. There may be more types we need to perform similar validation for, such as floats (see cockroachdb#73743). 3. Columns in values expressions with values that have non-identical types but the same type OID will be typed with type modifiers. For example, if a values expression has a CHAR(1) value and a CHAR(3) value in the same column, the column will be typed as a CHAR without an explicit width. 4. Type modifiers are now correctly omitted from array content types when arrays contain constants. Fixes cockroachdb#73450 Release note (bug fix): A bug has been fixed that caused incorrect evaluation of placeholder values in EXECUTE statements. The bug presented when the PREPARE statement cast a placeholder value, for example `PREPARE s AS SELECT $1::INT2`. If the assigned value for `$1` exceeded the maximum width value of the cast target type, the result value of the cast could be incorrect. This bug has been present since version 19.1 or earlier.
The `EliminateAssignmentCast` rule has been combined with the `EliminateCast` rule. Now an assignment cast is eliminated if the source and target type are identical. This now possible thanks to some changes to type resolution, including: 1. Placeholder types are resolved with unspecified type modifiers. This ensures that assignment casts won't be eliminated if the a placeholder value does not conform to the target type's modifiers. 2. When constant integer `NumVal`s are resolved as a typed-value, they are validated to ensure they fit within their type's width. There may be more types we need to perform similar validation for, such as floats (see cockroachdb#73743). 3. Columns in values expressions with values that have non-identical types but the same type OID will be typed with type modifiers. For example, if a values expression has a CHAR(1) value and a CHAR(3) value in the same column, the column will be typed as a CHAR without an explicit width. 4. Type modifiers are now correctly omitted from array content types when arrays contain constants. Fixes cockroachdb#73450 Release note (bug fix): A bug has been fixed that caused incorrect evaluation of placeholder values in EXECUTE statements. The bug presented when the PREPARE statement cast a placeholder value, for example `PREPARE s AS SELECT $1::INT2`. If the assigned value for `$1` exceeded the maximum width value of the cast target type, the result value of the cast could be incorrect. This bug has been present since version 19.1 or earlier.
73762: opt: eliminate assignment casts with identical source and target types r=mgartner a=mgartner #### sql: remove type modifiers from placeholder types The width and precision of placeholder values are not known during PREPARE, therefore we remove any type modifiers from placeholder types during type checking so that a value of any width will fit within the palceholder type. Note that this change is similar to the changes made in #70722 to placeholder type checking that were later reverted in #72793. In #70722 the type OIDs of placeholders could be altered, e.g., a placeholder originally with type `INT2` would be converted to an `INT`. In this commit, type OIDs of placeholders are not changing, only type modifiers are. This commit should allow some logic added in #72793 to be simplified or removed entirely. Release note: None #### opt: eliminate assignment casts with identical source and target types The `EliminateAssignmentCast` rule has been combined with the `EliminateCast` rule. Now an assignment cast is eliminated if the source and target type are identical. This now possible thanks to some changes to type resolution, including: 1. Placeholder types are resolved with unspecified type modifiers. This ensures that assignment casts won't be eliminated if the a placeholder value does not conform to the target type's modifiers. 2. When constant integer `NumVal`s are resolved as a typed-value, they are validated to ensure they fit within their type's width. There may be more types we need to perform similar validation for, such as floats (see #73743). 3. Columns in values expressions with values that have non-identical types but the same type OID will be typed with type modifiers. For example, if a values expression has a CHAR(1) value and a CHAR(3) value in the same column, the column will be typed as a CHAR without an explicit width. 4. Type modifiers are now correctly omitted from array content types when arrays contain constants. Fixes #73450 Release note (bug fix): A bug has been fixed that caused incorrect evaluation of placeholder values in EXECUTE statements. The bug presented when the PREPARE statement cast a placeholder value, for example `PREPARE s AS SELECT $1::INT2`. If the assigned value for `$1` exceeded the maximum width value of the cast target type, the result value of the cast could be incorrect. This bug has been present since version 19.1 or earlier. 73819: bazel: upgrade `rules_foreign_cc` to 0.7 r=rail a=rickystewart Also add `-fcommon` to compile flags for `krb5`. Closes #71306. Release note: None 73832: sql/opt/exec: output index/expiry in EXPLAIN SPLIT/RELOCATE statements r=RaduBerinde a=knz Release note (sql change): The output of `EXPLAIN ALTER INDEX/TABLE ... RELOCATE/SPLIT` now includes the target table/index name and, for the SPLIT AT variants, the expiry timestamp. Co-authored-by: Marcus Gartner <marcus@cockroachlabs.com> Co-authored-by: Ricky Stewart <ricky@cockroachlabs.com> Co-authored-by: Raphael 'kena' Poss <knz@thaumogen.net>
The `EliminateAssignmentCast` rule has been combined with the `EliminateCast` rule. Now an assignment cast is eliminated if the source and target type are identical. This now possible thanks to some changes to type resolution, including: 1. Placeholder types are resolved with unspecified type modifiers. This ensures that assignment casts won't be eliminated if the a placeholder value does not conform to the target type's modifiers. 2. When constant integer `NumVal`s are resolved as a typed-value, they are validated to ensure they fit within their type's width. There may be more types we need to perform similar validation for, such as floats (see cockroachdb#73743). 3. Columns in values expressions with values that have non-identical types but the same type OID will be typed with type modifiers. For example, if a values expression has a CHAR(1) value and a CHAR(3) value in the same column, the column will be typed as a CHAR without an explicit width. 4. Type modifiers are now correctly omitted from array content types when arrays contain constants. Fixes cockroachdb#73450 Release note (bug fix): A bug has been fixed that caused incorrect evaluation of placeholder values in EXECUTE statements. The bug presented when the PREPARE statement cast a placeholder value, for example `PREPARE s AS SELECT $1::INT2`. If the assigned value for `$1` exceeded the maximum width value of the cast target type, the result value of the cast could be incorrect. This bug has been present since version 19.1 or earlier.
root cause is float to float casts do nothing: cockroach/pkg/sql/sem/tree/cast.go Lines 1878 to 1879 in c089297
for int family, it is done here... cockroach/pkg/sql/sem/tree/cast.go Line 1516 in c089297
|
float4 is actually handled pretty badly. parsing example:
vs psql:
|
urgh, this is already committed into the DB for some people as well. would need to test whether any fix breaks those cases... |
in fact, since we store Float4 as Float8, all arithmetic is done using Float8 so even more stuff is probably wrong? |
Yikes, I think you're probably correct about that. Looks like this won't be a quick win... |
The root problem here seems to be #48613 |
This commit removes a TODO that was partially addressed by cockroachdb#82022. Informs cockroachdb#73743 Release note: None
105736: opt: wrap virtual computed column projections in a cast r=mgartner a=mgartner #### sql/logictest: remove assignment cast TODO This commit removes a TODO that was partially addressed by #82022. Informs #73743 Release note: None #### sql/types: fix T.Identical This commit fixes a bug in `types.T.Identical` which caused it to return false for collated string types with a different string-representation of locales that represents the same locale logically. For example, collated string types with locales `en_US` and `en_us` would not be identical, even though these are both valid representations of the same locale. There is no release note because this has not caused any known bugs. Release note: None #### opt: wrap virtual computed column projections in a cast optbuilder wraps an assignment cast around a virtual computed column expression when writing to the table if the expression does not have the same type as the virtual column. This matches the behavior of all writes to columns of expressions that don't match the target column type. However, the same cast was not applied to the projection of the virtual column expression when reading from the table. This cast is necessary in order to produce the correct value of the column - the projection must produce the same value that would have been written to the table if the column was a stored computed column. This commit corrects optbuilder so that the cast is correctly added to the projection. Note that this commit adds a standard cast, not an assignment cast, as a temporary workaround until #81698 is addressed. This is because an assignment cast can fail in cases when a regular cast will not. Because we don't validate that all existing rows can successfully produce a new virtual column expression during a backfill, adding an assignment cast to the projection of the virtual column could cause future reads to error. Once #81698 is addressed, we can change these casts to assignment casts so that they directly match the values that would be written to the same column if it were stored. Fixes #91817 Informs #81698 Release note (bug fix): A bug has been fixed that could produce incorrect values for virtual computed columns in rare cases. The bug only presented when the virtual column expression's type did not match the type of the virtual column. 105932: opt/exec: add passthrough cols to DELETE USING result cols in explain r=yuzefovich,DrewKimball a=michae2 Now that we support `DELETE USING`, delete nodes can have passthrough columns. Add these to the result columns used by `EXPLAIN (TYPES)`. Fixes: #105803 Release note (sql change): Fix an internal error when using `EXPLAIN (TYPES)` on a `DELETE FROM ... USING ... RETURNING` statement. Co-authored-by: Marcus Gartner <marcus@cockroachlabs.com> Co-authored-by: Michael Erickson <michae2@cockroachlabs.com>
It looks like this was partially fixed by #82022. See: 4d16dbf#diff-c49e1bdbc0004860cadef9d5e1e4d3e2c18b89a254ad9f4885e6c929ff2a5e43R172-R173 But it's not clear to me how that change fixed these tests and the example test in the description above. Maybe @rafiss has an idea? Regardless, I think this is still an issue as seen here: 4d16dbf#diff-c49e1bdbc0004860cadef9d5e1e4d3e2c18b89a254ad9f4885e6c929ff2a5e43R1405-R1411 |
Another related issue: #84326 |
This commit removes a TODO that was partially addressed by cockroachdb#82022. Informs cockroachdb#73743 Release note: None
When inserting a float into a
FLOAT4
column, or casting a value to aFLOAT4
, CRDB does not correctly truncate the precision of the float to fit within aFLOAT4
. This is present in v21.2.2, so it's not a regression from the assignment cast changes that were merged after v21.2, but it is related to assignment casts.In Postgres:
In CRDB (using logic test because the demo client will truncate floats):
Jira issue: CRDB-11718
The text was updated successfully, but these errors were encountered: