Skip to content

sql: make datum compare safe to use with nil eval contexts#127521

Merged
craig[bot] merged 1 commit intocockroachdb:masterfrom
Uzair5162:nil-eval-ctx-merging-pstats-gh-126856
Jul 22, 2024
Merged

sql: make datum compare safe to use with nil eval contexts#127521
craig[bot] merged 1 commit intocockroachdb:masterfrom
Uzair5162:nil-eval-ctx-merging-pstats-gh-126856

Conversation

@Uzair5162
Copy link
Copy Markdown
Contributor

This commit adds a nil check to the eval.Context implementation of GetRelativeParseTime(). This change makes the datum .Compare() function safe to use with nil eval contexts, which is useful in places such as the histogram merging code called by the stat cache where we don't have an eval context.

Fixes: #126856

See also: #125950

Release note: None

@Uzair5162 Uzair5162 requested review from a team and DrewKimball July 19, 2024 14:43
@cockroach-teamcity
Copy link
Copy Markdown
Member

This change is Reviewable

@Uzair5162 Uzair5162 force-pushed the nil-eval-ctx-merging-pstats-gh-126856 branch from 541ed63 to 376d6b3 Compare July 19, 2024 15:38
Copy link
Copy Markdown
Collaborator

@DrewKimball DrewKimball 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 r1, all commit messages.
Reviewable status: :shipit: complete! 0 of 0 LGTMs obtained (waiting on @Uzair5162)


pkg/sql/sem/tree/datum.go line 119 at r1 (raw file):

	// Compare returns -1 if the receiver is less than other, 0 if receiver is
	// equal to other and +1 if receiver is greater than 'other'. Compare is safe
	// to use with a nil eval.Context.

[nit] mention that a nil eval.Context results in default behavior.

Also, there is another case where nil eval.Context isn't valid (and shouldn't be) - tree.Placeholder. Let's call that out in the comment.


pkg/sql/sem/eval/compare_test.go line 28 at r1 (raw file):

// Test that we can compare random datums with a nil eval context.
func TestNilEvalContextCompareRandom(t *testing.T) {

[nit] Let's move this test to datum_test.go, since it's already being used to test datum comparison.


pkg/sql/sem/eval/compare_test.go line 39 at r1 (raw file):

		typ := randgen.RandType(rng)
		left := randgen.RandDatum(rng, typ, false)
		right := randgen.RandDatum(rng, typ, false)

Let's pass true here to allow NULL values. You can often catch edge cases that way.

[nit] Usually, when passing an unnamed value as a parameter like that, we also add an inline comment with the parameter name, like this:

right := randgen.RandDatum(rng, typ, false /* nullOk */)

pkg/sql/sem/eval/compare_test.go line 53 at r1 (raw file):

// Test that we can compare two equal TimeTZ datums with a nil eval context.
func TestNilEvalContextCompareTimeTZ(t *testing.T) {

There's already a TestCompareTimestamps test in datum_test.go - instead of making this new test, let's modify that one to run also run Compare with a nil eval.Context.

@Uzair5162 Uzair5162 force-pushed the nil-eval-ctx-merging-pstats-gh-126856 branch from 376d6b3 to e733b33 Compare July 19, 2024 21:33
Copy link
Copy Markdown
Contributor Author

@Uzair5162 Uzair5162 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 @DrewKimball)


pkg/sql/sem/tree/datum.go line 119 at r1 (raw file):

Previously, DrewKimball (Drew Kimball) wrote…

[nit] mention that a nil eval.Context results in default behavior.

Also, there is another case where nil eval.Context isn't valid (and shouldn't be) - tree.Placeholder. Let's call that out in the comment.

Done. Good find!


pkg/sql/sem/eval/compare_test.go line 28 at r1 (raw file):

Previously, DrewKimball (Drew Kimball) wrote…

[nit] Let's move this test to datum_test.go, since it's already being used to test datum comparison.

datum_test.go is in the tree pkg which creates cyclic dependencies with the eval pkg needed for eval.Context :(


pkg/sql/sem/eval/compare_test.go line 39 at r1 (raw file):

Previously, DrewKimball (Drew Kimball) wrote…

Let's pass true here to allow NULL values. You can often catch edge cases that way.

[nit] Usually, when passing an unnamed value as a parameter like that, we also add an inline comment with the parameter name, like this:

right := randgen.RandDatum(rng, typ, false /* nullOk */)

Done.

@Uzair5162 Uzair5162 requested a review from DrewKimball July 19, 2024 21:37
Copy link
Copy Markdown
Collaborator

@DrewKimball DrewKimball 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 r2, all commit messages.
Reviewable status: :shipit: complete! 1 of 0 LGTMs obtained (waiting on @Uzair5162)


pkg/sql/sem/eval/compare_test.go line 44 at r2 (raw file):

		// Also test comparing a random datum to itself with a nil eval context.
		right = left

[nit] I think it's more clear if you just do left.Compare(..., left). Is there a reason we can't do that?

This commit adds a nil check to the eval.Context implementation of
`GetRelativeParseTime()`. This change makes the datum `.Compare()`
function safe to use with nil eval contexts, which is useful in
places such as the histogram merging code called by the stat cache where
we don't have an eval context.

Fixes: cockroachdb#126856

See also: cockroachdb#125950

Release note: None
@Uzair5162 Uzair5162 force-pushed the nil-eval-ctx-merging-pstats-gh-126856 branch from e733b33 to 75a7960 Compare July 22, 2024 15:18
Copy link
Copy Markdown
Contributor Author

@Uzair5162 Uzair5162 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 (and 1 stale) (waiting on @DrewKimball)


pkg/sql/sem/eval/compare_test.go line 44 at r2 (raw file):

Previously, DrewKimball (Drew Kimball) wrote…

[nit] I think it's more clear if you just do left.Compare(..., left). Is there a reason we can't do that?

Nope, done.

@Uzair5162
Copy link
Copy Markdown
Contributor Author

bors r+

@craig
Copy link
Copy Markdown
Contributor

craig bot commented Jul 22, 2024

@craig craig bot merged commit a27662a into cockroachdb:master Jul 22, 2024
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.

sql/stats: don't use a nil eval context when merging statistics

3 participants