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

logictest: introduce a separate matcher for floats #55530

Merged
merged 1 commit into from
Oct 15, 2020

Conversation

yuzefovich
Copy link
Member

@yuzefovich yuzefovich commented Oct 13, 2020

This commit adds another type annotation F intended to be used for
floating point numbers which have a separate matcher that allows for
some deviation in the precision. Namely, the matcher checks that the
expected and the actual numbers match in 15 significant decimal
digits. Note that I think the matching algorithm introduces some
rounding errors, so it might not work perfectly in all scenarios, yet it
should be good enough in rare test cases when we need it.

Informs: #55268.

Release note: None

@yuzefovich yuzefovich requested review from asubiotto and a team October 13, 2020 22:51
@cockroach-teamcity
Copy link
Member

This change is Reviewable

Copy link
Contributor

@asubiotto asubiotto left a comment

Choose a reason for hiding this comment

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

Reviewed 1 of 2 files at r1.
Reviewable status: :shipit: complete! 0 of 0 LGTMs obtained (waiting on @asubiotto and @yuzefovich)


pkg/sql/logictest/logic.go, line 167 at r1 (raw file):

//        to string (arrays, timestamps, etc.).
//      - I for integer
//      - F for floating point (matches 15 significant decimal digits)

Add a link to the postgres docs here.


pkg/sql/logictest/logic.go, line 2513 at r1 (raw file):

							if *flexTypes && (valT == reflect.Int64) {
								t.signalIgnoredError(
									fmt.Errorf("result type mismatch: expected R, got %T", val), query.pos, query.sql,

nit: update


pkg/sql/logictest/logic.go, line 2630 at r1 (raw file):

	if query.checkResults {
		resultsMatch := reflect.DeepEqual(query.expectedResults, actualResults)

It feels wrong to do a full equality check and then check for float columns if it fails. If we're just checking strings, why do we need to do a DeepEqual? I would iterate over the results in order and change the equality check based on the type.


pkg/sql/logictest/logic.go, line 2656 at r1 (raw file):

			resultsMatch = true
			for i, colT := range query.colTypes {
				expected, actual := query.expectedResults[i], actualResults[i]

I would extract this logic into a separate function which you might be able to unit test (why not?). This'll also make this main body of code easier to read.


pkg/sql/logictest/logic.go, line 2685 at r1 (raw file):

						}
						expFloat = normalize(expFloat)
						actFloat = normalize(actFloat)

Won't this return that 123.45 and 12.345 are equal since they'll both be normalized to 1.2345?

Copy link
Member Author

@yuzefovich yuzefovich 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 @asubiotto)


pkg/sql/logictest/logic.go, line 167 at r1 (raw file):

Previously, asubiotto (Alfonso Subiotto Marqués) wrote…

Add a link to the postgres docs here.

Done.


pkg/sql/logictest/logic.go, line 2630 at r1 (raw file):

Previously, asubiotto (Alfonso Subiotto Marqués) wrote…

It feels wrong to do a full equality check and then check for float columns if it fails. If we're just checking strings, why do we need to do a DeepEqual? I would iterate over the results in order and change the equality check based on the type.

Good idea, done.


pkg/sql/logictest/logic.go, line 2656 at r1 (raw file):

Previously, asubiotto (Alfonso Subiotto Marqués) wrote…

I would extract this logic into a separate function which you might be able to unit test (why not?). This'll also make this main body of code easier to read.

Good idea, done.


pkg/sql/logictest/logic.go, line 2685 at r1 (raw file):

Previously, asubiotto (Alfonso Subiotto Marqués) wrote…

Won't this return that 123.45 and 12.345 are equal since they'll both be normalized to 1.2345?

Yes, you're right, good catch.

This commit adds another type annotation `F` intended to be used for
floating point numbers which have a separate matcher that allows for
some deviation in the precision. Namely, the matcher checks that the
expected and the actual numbers match in 15 significant decimal
digits. Note that I think the matching algorithm introduces some
rounding errors, so it might not work perfectly in all scenarios, yet it
should be good enough in rare test cases when we need it.

Release note: None
Copy link
Contributor

@asubiotto asubiotto left a comment

Choose a reason for hiding this comment

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

Reviewed 1 of 2 files at r1, 4 of 4 files at r2.
Reviewable status: :shipit: complete! 0 of 0 LGTMs obtained (waiting on @asubiotto and @yuzefovich)


pkg/sql/logictest/logic.go, line 2705 at r2 (raw file):

	}
	if expected == 0 || actual == 0 {
		return expected == actual, nil

Is this correct? What about if expected is 0 and actual is 0.0000000000000000000000001?

Copy link
Member Author

@yuzefovich yuzefovich 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 @asubiotto)


pkg/sql/logictest/logic.go, line 2705 at r2 (raw file):

Previously, asubiotto (Alfonso Subiotto Marqués) wrote…

Is this correct? What about if expected is 0 and actual is 0.0000000000000000000000001?

I believe these should be treated as different. The question is what Postgres refers to as 15 decimal digits precision - I believe it means 15 significant decimal digits are correct, and in your example we have only 1 significant digit in the right number if I'm not mistaken.

Copy link
Contributor

@asubiotto asubiotto left a comment

Choose a reason for hiding this comment

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

:lgtm:

Reviewable status: :shipit: complete! 1 of 0 LGTMs obtained (waiting on @yuzefovich)


pkg/sql/logictest/logic.go, line 2705 at r2 (raw file):

Previously, yuzefovich wrote…

I believe these should be treated as different. The question is what Postgres refers to as 15 decimal digits precision - I believe it means 15 significant decimal digits are correct, and in your example we have only 1 significant digit in the right number if I'm not mistaken.

I think you're right.

@yuzefovich
Copy link
Member Author

TFTR!

bors r+

@craig
Copy link
Contributor

craig bot commented Oct 15, 2020

Build succeeded:

@craig craig bot merged commit 5403e6b into cockroachdb:master Oct 15, 2020
@yuzefovich yuzefovich deleted the logic-float branch October 15, 2020 16:07
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.

3 participants