Skip to content
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,pgwire: fixes for float formatting #82022

Merged
merged 4 commits into from
May 30, 2022
Merged

Conversation

rafiss
Copy link
Collaborator

@rafiss rafiss commented May 28, 2022

sql/pgwire: update float formatting to be compatible with PG12

Release note (sql change): The extra_float_digits session variable now
defaults to 1. The meaning of the variable has also changed. Now, any
value greater than zero causes floats to be formatted in their
shortest precise decimal representation. That is, the string representation
produced is closer to the actual binary value than to any other value.
(Previously, this was only the behavior when extra_float_digits was set
to 3.) This change was made in accordance with an equivalent change that
was part of the PostgreSQL 12.0 release.

The behavior of a non-positive extra_float_digits value is unchanged. That
will still reduce the number of float digits shown in the output string.
The formula for number of digits shown is max(1, (DIGITS + extra_float_digits)),
where DIGITS=6 for FLOAT4 values, and DIGITS=15 for FLOAT8 values.

sql/pgwire: fix formatting of float arrays

Release note (bug fix): Fixed the formatting of floats in arrays sent
over the client-server pgwire protocol so that they respect the
extra_float_digits parameter, and correctly format infinity values.

eval: use common function for float->text cast

There's no behavior change, but this reduces code duplication.

This also adds a test that demonstrates a gap in our type/cast system.

@rafiss rafiss requested review from otan and a team May 28, 2022 19:24
@rafiss rafiss requested a review from a team as a code owner May 28, 2022 19:24
@cockroach-teamcity
Copy link
Member

This change is Reviewable

Copy link
Contributor

@knz knz left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice - but I'll let oliver sign off.

Reviewed 6 of 6 files at r1, 3 of 3 files at r2, 2 of 2 files at r3, all commit messages.
Reviewable status: :shipit: complete! 0 of 0 LGTMs obtained (waiting on @otan)



send
Query {"String": "SET extra_float_digits = -1"}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can you add a test for a positive extra_float_digits > 1?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

adding

ReadyForQuery
----
{"Type":"RowDescription","Fields":[{"Name":"float4","TableOID":0,"TableAttributeNumber":0,"DataTypeOID":700,"DataTypeSize":4,"TypeModifier":-1,"Format":0},{"Name":"float8","TableOID":0,"TableAttributeNumber":0,"DataTypeOID":701,"DataTypeSize":8,"TypeModifier":-1,"Format":0}]}
{"Type":"DataRow","Values":[{"text":"0.33333334"},{"text":"0.3333333333333333"}]}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ooh we round up to 4? is this consistent when running this with PG?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yep, the expected result was obtained by running against PG

@@ -162,6 +170,27 @@ func init() {
}
}

// PgwireFormatFloat returns a []byte representing a float according to
// pgwire encoding. The result is appended to the given buffer.
func PgwireFormatFloat(
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

wonder if this should be PGWire, but that's an optional nit

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

i was matching the naming of PgwireFormatBool

@rafiss rafiss force-pushed the fmt-floats branch 4 times, most recently from 34af9ad to 9786005 Compare May 29, 2022 23:53
@rafiss
Copy link
Collaborator Author

rafiss commented May 30, 2022

bors r=otan

@craig
Copy link
Contributor

craig bot commented May 30, 2022

Build failed:

rafiss added 4 commits May 30, 2022 00:00
Release note (sql change): The extra_float_digits session variable now
defaults to 1. The meaning of the variable has also changed. Now, any
value greater than zero causes floats to be formatted in their
shortest precise decimal representation. That is, the string representation
produced is closer to the actual binary value than to any other value.
(Previously, this was only the behavior when extra_float_digits was set
to 3.) This change was made in accordance with an equivalent change that
was part of the PostgreSQL 12.0 release.

The behavior of a non-positive extra_float_digits value is unchanged. That
will still reduce the number of float digits shown in the output string.
The formula for number of digits shown is `max(1, (DIGITS + extra_float_digits))`,
where DIGITS=6 for FLOAT4 values, and DIGITS=15 for FLOAT8 values.
Release note (bug fix): Fixed the formatting of floats in arrays and tuples
sent over the client-server pgwire protocol so that they respect the
extra_float_digits parameter, and correctly  format infinity values.
There's no behavior change, but this reduces code duplication.

This also adds a test that demonstrates a gap in our type/cast system.

Release note: None
The encoding tests were updated so they use the new default value of 1
for extra_float_digits. This makes the test more aligned to Postgres
behavior.

Release note: None
@rafiss
Copy link
Collaborator Author

rafiss commented May 30, 2022

bors r=otan

craig bot pushed a commit that referenced this pull request May 30, 2022
82022: sql,pgwire: fixes for float formatting r=otan a=rafiss

### sql/pgwire: update float formatting to be compatible with PG12

Release note (sql change): The extra_float_digits session variable now
defaults to 1. The meaning of the variable has also changed. Now, any
value greater than zero causes floats to be formatted in their
shortest precise decimal representation. That is, the string representation
produced is closer to the actual binary value than to any other value.
(Previously, this was only the behavior when extra_float_digits was set
to 3.) This change was made in accordance with an equivalent change that
was part of the PostgreSQL 12.0 release.

The behavior of a non-positive extra_float_digits value is unchanged. That
will still reduce the number of float digits shown in the output string.
The formula for number of digits shown is `max(1, (DIGITS + extra_float_digits))`,
where DIGITS=6 for FLOAT4 values, and DIGITS=15 for FLOAT8 values.

### sql/pgwire: fix formatting of float arrays

Release note (bug fix): Fixed the formatting of floats in arrays sent
over the client-server pgwire protocol so that they respect the
extra_float_digits parameter, and correctly  format infinity values.

### eval: use common function for float->text cast

There's no behavior change, but this reduces code duplication.

This also adds a test that demonstrates a gap in our type/cast system.

Co-authored-by: Rafi Shamim <rafi@cockroachlabs.com>
@craig
Copy link
Contributor

craig bot commented May 30, 2022

Build failed:

@rafiss
Copy link
Collaborator Author

rafiss commented May 30, 2022

bors=otan

@rafiss
Copy link
Collaborator Author

rafiss commented May 30, 2022

bors r=otan

1 similar comment
@rafiss
Copy link
Collaborator Author

rafiss commented May 30, 2022

bors r=otan

@craig
Copy link
Contributor

craig bot commented May 30, 2022

Build succeeded:

@craig craig bot merged commit 9ebc6c8 into cockroachdb:master May 30, 2022
@rafiss rafiss deleted the fmt-floats branch May 31, 2022 20:11
mgartner added a commit to mgartner/cockroach that referenced this pull request Jun 28, 2023
This commit removes a TODO that was partially addressed by cockroachdb#82022.

Informs cockroachdb#73747

Release note: None
mgartner added a commit to mgartner/cockroach that referenced this pull request Jun 29, 2023
This commit removes a TODO that was partially addressed by cockroachdb#82022.

Informs cockroachdb#73743

Release note: None
craig bot pushed a commit that referenced this pull request Jul 5, 2023
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>
mgartner added a commit to mgartner/cockroach that referenced this pull request Aug 14, 2024
This commit removes a TODO that was partially addressed by cockroachdb#82022.

Informs cockroachdb#73743

Release note: None
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants