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

tests: support float approximation in roachtest query comparison utils #106552

Merged
merged 2 commits into from
Jul 24, 2023

Conversation

rharding6373
Copy link
Collaborator

@rharding6373 rharding6373 commented Jul 10, 2023

tests, logictest, floatcmp: refactor comparison test util functions

This commit moves some float comparison test util functions from
logictest into the floatcmp package. It also moves a query result
comparison function from the tlp file to query_comparison_util in the
tests package.

This commit also marks roachtests as testonly targets.

Epic: none

Release note: None

tests: support float approximation in roachtest query comparison utils

Before this change unoptimized query oracle tests would compare results
using simple string comparison. However, due to floating point precision
limitations, it's possible for results with floating point to diverge
during the course of normal computation. This results in test failures
that are difficult to reproduce or determine whether they are expected
behavior.

This change utilizes existing floating point comparison functions used
by logic tests to match float values only to a specific precision. Like
the logic tests, we also have special handling for floats and decimals
under the s390x architecture (see #63244). In order to avoid costly
comparisons, we only check floating point precision if the naiive string
comparison approach fails and there are float or decimal types in the
result.

Epic: None
Fixes: #95665

Release note: None

@rharding6373 rharding6373 requested a review from michae2 July 10, 2023 21:23
@cockroach-teamcity
Copy link
Member

This change is Reviewable

@rharding6373 rharding6373 changed the title 20230629 floats 95665 tests: support float approximation in roachtest query comparison utils Jul 10, 2023
@rharding6373 rharding6373 force-pushed the 20230629_floats_95665 branch from 8b8c107 to 0c36d5d Compare July 10, 2023 21:56
@rharding6373 rharding6373 marked this pull request as ready for review July 10, 2023 21:56
@rharding6373 rharding6373 requested a review from a team as a code owner July 10, 2023 21:56
@rharding6373 rharding6373 requested review from herkolategan and renatolabs and removed request for a team July 10, 2023 21:56
Copy link
Collaborator

@michae2 michae2 left a comment

Choose a reason for hiding this comment

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

Thank you for doing this!

Reviewed 9 of 9 files at r1, 2 of 4 files at r2, all commit messages.
Reviewable status: :shipit: complete! 0 of 0 LGTMs obtained (waiting on @herkolategan, @renatolabs, and @rharding6373)


pkg/cmd/roachtest/BUILD.bazel line 73 at r1 (raw file):

    name = "roachtest_test",
    size = "small",
    testonly = 1,

TIL about testonly, nice!


pkg/cmd/roachtest/tests/query_comparison_util.go line 406 at r2 (raw file):

	// comparison, so that we can split the sorted rows if we need to make
	// additional comparisons.
	sep := ",unsortedMatricesDiffWithFloatComp separator,"

This is a nice technique for splitting again, but won't it also show up in the error message? Maybe the reported diff should be calculated again, without this separator, to make it easier to read?


pkg/cmd/roachtest/tests/unoptimized_query_oracle.go line 177 at r2 (raw file):

		return nil
	}
	diff, err := unsortedMatricesDiffWithFloatComp(unoptimizedRows, optimizedRows, h.colTypes)

Nice! I think we also want this in costfuzz and tlp, right?


pkg/testutils/floatcmp/floatcmp.go line 127 at r1 (raw file):

	// normalize converts f to base * 10**power representation where base is in
	// [1.0, 10.0) range.
	normalize := func(f float64) (base float64, power int) {

I know this is how the code was before, but I think it would be more accurate to use math.Frexp to get the power-of-2 normalization. (I think the power-of-10 normalization might itself introduce floating-point error.)

@rharding6373 rharding6373 force-pushed the 20230629_floats_95665 branch from 0c36d5d to c821d5c Compare July 17, 2023 23:41
Copy link
Collaborator Author

@rharding6373 rharding6373 left a comment

Choose a reason for hiding this comment

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

TFTR! PTAL

Reviewable status: :shipit: complete! 0 of 0 LGTMs obtained (waiting on @herkolategan, @michae2, and @renatolabs)


pkg/cmd/roachtest/tests/query_comparison_util.go line 406 at r2 (raw file):

Previously, michae2 (Michael Erickson) wrote…

This is a nice technique for splitting again, but won't it also show up in the error message? Maybe the reported diff should be calculated again, without this separator, to make it easier to read?

You're right. I redid the join/sort if there is an error and there are floats instead.


pkg/cmd/roachtest/tests/unoptimized_query_oracle.go line 177 at r2 (raw file):

Previously, michae2 (Michael Erickson) wrote…

Nice! I think we also want this in costfuzz and tlp, right?

I modified costfuzz to use this. For tlp, we expect a diff because we already did a sql comparison that failed, so I think that using the old diff method is ok.


pkg/testutils/floatcmp/floatcmp.go line 127 at r1 (raw file):

Previously, michae2 (Michael Erickson) wrote…

I know this is how the code was before, but I think it would be more accurate to use math.Frexp to get the power-of-2 normalization. (I think the power-of-10 normalization might itself introduce floating-point error.)

Done.

Copy link
Collaborator

@michae2 michae2 left a comment

Choose a reason for hiding this comment

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

Reviewed 4 of 4 files at r3, all commit messages.
Reviewable status: :shipit: complete! 0 of 0 LGTMs obtained (waiting on @herkolategan, @renatolabs, and @rharding6373)


pkg/cmd/roachtest/tests/query_comparison_util.go line 478 at r3 (raw file):

		}
	}
	return "", nil

Do we also need to confirm that the rest of the columns are equal? (In other words, if the result contains float columns, and those are equal, but some other non-float column is not equal, will this catch that?)

It might be good to add some testcases with both float and non-float columns.


pkg/cmd/roachtest/tests/query_comparison_util_test.go line 56 at r3 (raw file):

		{
			name:        "multi float approx match",
			colTypes:    []string{"FLOAT8"},

Should this slice have two values?

@rharding6373 rharding6373 force-pushed the 20230629_floats_95665 branch from c821d5c to 438a95f Compare July 18, 2023 16:58
Copy link
Collaborator Author

@rharding6373 rharding6373 left a comment

Choose a reason for hiding this comment

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

Reviewable status: :shipit: complete! 0 of 0 LGTMs obtained (waiting on @herkolategan, @michae2, and @renatolabs)


pkg/cmd/roachtest/tests/query_comparison_util.go line 478 at r3 (raw file):

Previously, michae2 (Michael Erickson) wrote…

Do we also need to confirm that the rest of the columns are equal? (In other words, if the result contains float columns, and those are equal, but some other non-float column is not equal, will this catch that?)

It might be good to add some testcases with both float and non-float columns.

Done.


pkg/cmd/roachtest/tests/query_comparison_util_test.go line 56 at r3 (raw file):

Previously, michae2 (Michael Erickson) wrote…

Should this slice have two values?

Done.

Copy link
Collaborator

@michae2 michae2 left a comment

Choose a reason for hiding this comment

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

:lgtm: Nice work!

Reviewed 2 of 2 files at r4, all commit messages.
Reviewable status: :shipit: complete! 1 of 0 LGTMs obtained (waiting on @herkolategan and @renatolabs)

@rharding6373 rharding6373 force-pushed the 20230629_floats_95665 branch from 438a95f to 9f5c2bc Compare July 21, 2023 22:40
This commit moves some float comparison test util functions from
logictest into the floatcmp package. It also moves a query result
comparison function from the tlp file to query_comparison_util in the
tests package.

This commit also marks roachtests as testonly targets.

Epic: none

Release note: None
Before this change unoptimized query oracle tests would compare results
using simple string comparison. However, due to floating point precision
limitations, it's possible for results with floating point to diverge
during the course of normal computation. This results in test failures
that are difficult to reproduce or determine whether they are expected
behavior.

This change utilizes existing floating point comparison functions used
by logic tests to match float values only to a specific precision. Like
the logic tests, we also have special handling for floats and decimals
under the s390x architecture (see cockroachdb#63244). In order to avoid costly
comparisons, we only check floating point precision if the naiive string
comparison approach fails and there are float or decimal types in the
result.

Epic: None
Fixes: cockroachdb#95665

Release note: None
@rharding6373
Copy link
Collaborator Author

The test failure is unrelated. I opened #107455 to address it.

TFTRs!

bors r+

@craig
Copy link
Contributor

craig bot commented Jul 24, 2023

Build succeeded:

@craig craig bot merged commit 06fb4c1 into cockroachdb:master Jul 24, 2023
michae2 added a commit to michae2/cockroach that referenced this pull request Jul 24, 2023
In cockroachdb#106552 we tried changing float normalization to use base 2 instead
of base 10 (in other words, to use `math.Frexp` instead of our
hand-rolled `normalize`). This appears to have broken a logic test, so
revert back to the pre-existing base 10 normalization.

Fixes: cockroachdb#107461

Release note: None
michae2 added a commit to michae2/cockroach that referenced this pull request Jul 24, 2023
In cockroachdb#106552 we tried changing float normalization to use base 2 instead
of base 10 (in other words, to use `math.Frexp` instead of our
hand-rolled `normalize`). This appears to have broken a logic test, so
revert back to the pre-existing base 10 normalization.

Fixes: cockroachdb#107461

Release note: None
craig bot pushed a commit that referenced this pull request Jul 24, 2023
107490: testutils/floatcmp: revert to base-10 normalization in FloatsMatch r=rharding6373 a=michae2

In #106552 we tried changing float normalization to use base 2 instead of base 10 (in other words, to use `math.Frexp` instead of our hand-rolled `normalize`). This appears to have broken a logic test, so revert back to the pre-existing base 10 normalization.

Fixes: #107461

Release note: None

Co-authored-by: Michael Erickson <[email protected]>
rharding6373 pushed a commit to rharding6373/cockroach that referenced this pull request Sep 13, 2023
In cockroachdb#106552 we tried changing float normalization to use base 2 instead
of base 10 (in other words, to use `math.Frexp` instead of our
hand-rolled `normalize`). This appears to have broken a logic test, so
revert back to the pre-existing base 10 normalization.

Fixes: cockroachdb#107461

Release note: None
rharding6373 pushed a commit to rharding6373/cockroach that referenced this pull request Sep 13, 2023
In cockroachdb#106552 we tried changing float normalization to use base 2 instead
of base 10 (in other words, to use `math.Frexp` instead of our
hand-rolled `normalize`). This appears to have broken a logic test, so
revert back to the pre-existing base 10 normalization.

Fixes: cockroachdb#107461

Release note: None
rharding6373 pushed a commit to rharding6373/cockroach that referenced this pull request Sep 15, 2023
In cockroachdb#106552 we tried changing float normalization to use base 2 instead
of base 10 (in other words, to use `math.Frexp` instead of our
hand-rolled `normalize`). This appears to have broken a logic test, so
revert back to the pre-existing base 10 normalization.

Fixes: cockroachdb#107461

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.

Order of evaluation of aggregations causes precision issues for float
3 participants