Skip to content

sql/stats: fix histogram type check after metadata-only ALTER COLUMN TYPE#162122

Merged
trunk-io[bot] merged 1 commit intocockroachdb:masterfrom
michae2:fix-histogram-type-check-after-alter
Feb 12, 2026
Merged

sql/stats: fix histogram type check after metadata-only ALTER COLUMN TYPE#162122
trunk-io[bot] merged 1 commit intocockroachdb:masterfrom
michae2:fix-histogram-type-check-after-alter

Conversation

@michae2
Copy link
Copy Markdown
Collaborator

@michae2 michae2 commented Jan 31, 2026

Previously, after a metadata-only ALTER COLUMN TYPE conversion (e.g., TIMESTAMPTZ to TIMESTAMP), histograms would fail a type check because Equivalent() requires types to be in the same family. This caused an internal error in test builds or histograms being silently skipped in production, leading to degraded query planning.

This change adds a new HasIdenticalHistogramEncoding() method to types.T that returns true when two types have identical binary encodings for histogram bucket upper bounds. TIMESTAMP and TIMESTAMPTZ are the only cross-family types that share identical encoding (both store time.Time), so histograms remain valid after conversion between these types.

The histogram TypeCheck() function now uses this method instead of Equivalent(), allowing histograms to be reused after metadata-only type conversions where the encoding doesn't change.

Fixes: #125620
Epic: None
Release note: None

@cockroach-teamcity
Copy link
Copy Markdown
Member

This change is Reviewable

@michae2
Copy link
Copy Markdown
Collaborator Author

michae2 commented Jan 31, 2026

(Hold off on reviewing, I need to do a little more testing.)

@michae2 michae2 requested review from a team, ZhouXing19 and yuzefovich February 10, 2026 18:12
@michae2 michae2 marked this pull request as ready for review February 10, 2026 18:12
@michae2
Copy link
Copy Markdown
Collaborator Author

michae2 commented Feb 10, 2026

Ok, this is RFAL.

@michae2
Copy link
Copy Markdown
Collaborator Author

michae2 commented Feb 10, 2026

Hmm, actually, adding one more thing (sorry).

@michae2 michae2 marked this pull request as draft February 10, 2026 18:37
@michae2
Copy link
Copy Markdown
Collaborator Author

michae2 commented Feb 10, 2026

I explored changing ALTER COLUMN TYPE to also update the column type in the histogram(s), but this ended up pretty messy, so never mind. This should be RFAL.

@michae2 michae2 marked this pull request as ready for review February 10, 2026 21:27
Copy link
Copy Markdown
Collaborator

@ZhouXing19 ZhouXing19 left a comment

Choose a reason for hiding this comment

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

:lgtm_strong: Thanks for working on this!

@ZhouXing19 reviewed 5 files and all commit messages, and made 1 comment.
Reviewable status: :shipit: complete! 1 of 0 LGTMs obtained (waiting on @yuzefovich).

@ZhouXing19
Copy link
Copy Markdown
Collaborator

ZhouXing19 commented Feb 10, 2026

Oh, Yahor also had a reminder to unskip TestExplainGist for this error.

@michae2 michae2 force-pushed the fix-histogram-type-check-after-alter branch from 4bb7276 to ed9c9bb Compare February 11, 2026 00:17
Copy link
Copy Markdown
Member

@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.

Nice! :lgtm:

@yuzefovich reviewed 6 files and all commit messages, and made 3 comments.
Reviewable status: :shipit: complete! 2 of 0 LGTMs obtained (waiting on @michae2).


pkg/sql/logictest/testdata/logic_test/distsql_stats line 3988 at r2 (raw file):


# Verify the histogram is being used before the ALTER.
query T

nit: we probably should either add retry option or clear the table stats cache via the builtin to avoid flakes.


pkg/sql/types/types.go line 2447 at r2 (raw file):

// both types use identical encoding (both store time.Time), the histogram
// remains valid.
func (t *T) HasIdenticalHistogramEncoding(other *T) bool {

nit: it seems a bit off to me to have a method with "histogram" and "[binary] encoding" in its name in types package - personally, I'd move it probably to sql/stats/histogram.go file (it seems unlikely it'll be used anywhere else).

@michae2 michae2 force-pushed the fix-histogram-type-check-after-alter branch from ed9c9bb to ed8dde1 Compare February 12, 2026 00:05
Copy link
Copy Markdown
Collaborator Author

@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.

Thanks for the reviews!

/trunk merge

@michae2 made 3 comments and resolved 2 discussions.
Reviewable status: :shipit: complete! 1 of 0 LGTMs obtained (and 1 stale) (waiting on @yuzefovich and @ZhouXing19).


pkg/sql/logictest/testdata/logic_test/distsql_stats line 3988 at r2 (raw file):

Previously, yuzefovich (Yahor Yuzefovich) wrote…

nit: we probably should either add retry option or clear the table stats cache via the builtin to avoid flakes.

Done.


pkg/sql/types/types.go line 2447 at r2 (raw file):

Previously, yuzefovich (Yahor Yuzefovich) wrote…

nit: it seems a bit off to me to have a method with "histogram" and "[binary] encoding" in its name in types package - personally, I'd move it probably to sql/stats/histogram.go file (it seems unlikely it'll be used anywhere else).

Done.

@github-actions

This comment was marked as resolved.

@github-actions github-actions bot added the o-AI-Review-Potential-Issue-Detected AI reviewer found potential issue. Never assign manually—auto-applied by GH action only. label Feb 12, 2026
@michae2 michae2 force-pushed the fix-histogram-type-check-after-alter branch from ed8dde1 to 4284c5a Compare February 12, 2026 00:11
@michae2 michae2 added the O-AI-Review-Real-Issue-Found AI reviewer found real issue label Feb 12, 2026
@michae2
Copy link
Copy Markdown
Collaborator Author

michae2 commented Feb 12, 2026

/trunk merge

@trunk-io
Copy link
Copy Markdown
Contributor

trunk-io bot commented Feb 12, 2026

😎 Merged directly without going through the merge queue, as the queue was empty and the PR was up to date with the target branch - details.

@michae2 michae2 force-pushed the fix-histogram-type-check-after-alter branch from 4284c5a to 838dbbe Compare February 12, 2026 01:42
@michae2
Copy link
Copy Markdown
Collaborator Author

michae2 commented Feb 12, 2026

/trunk merge

…TYPE

Previously, after a metadata-only ALTER COLUMN TYPE conversion (e.g.,
TIMESTAMPTZ to TIMESTAMP), histograms would fail a type check because
`Equivalent()` requires types to be in the same family. This caused an
internal error in test builds or histograms being silently skipped in
production, leading to degraded query planning.

This change adds a new `HasIdenticalHistogramEncoding()` method to
`types.T` that returns true when two types have identical binary
encodings for histogram bucket upper bounds. TIMESTAMP and TIMESTAMPTZ
are the only cross-family types that share identical encoding (both
store time.Time), so histograms remain valid after conversion between
these types.

The histogram `TypeCheck()` function now uses this method instead of
`Equivalent()`, allowing histograms to be reused after metadata-only
type conversions where the encoding doesn't change.

Fixes: cockroachdb#125620
Epic: None
Release note: None

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
@michae2 michae2 force-pushed the fix-histogram-type-check-after-alter branch from 838dbbe to 2bf1688 Compare February 12, 2026 17:14
@trunk-io trunk-io bot merged commit 46ada8d into cockroachdb:master Feb 12, 2026
23 checks passed
@michae2 michae2 deleted the fix-histogram-type-check-after-alter branch February 12, 2026 17:55
@cockroach-teamcity cockroach-teamcity added the X-perf-gain Microbenchmarks CI: Added if a performance gain is detected label Feb 12, 2026
@yuzefovich
Copy link
Copy Markdown
Member

@michae2 thoughts about backporting this? This would be one way to silence the failure on the older branches (#163536).

@michae2
Copy link
Copy Markdown
Collaborator Author

michae2 commented Mar 3, 2026

blathers backport 26.1 25.4

@blathers-crl
Copy link
Copy Markdown

blathers-crl bot commented Mar 3, 2026

Based on the specified backports for this PR, I applied new labels to the following linked issue(s). Please adjust the labels as needed to match the branches actually affected by the issue(s), including adding any known older branches.


Issue #125620: branch-release-25.4, branch-release-26.1.


🦉 Hoot! I am a Blathers, a bot for CockroachDB. My owner is dev-inf.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

o-AI-Review-Potential-Issue-Detected AI reviewer found potential issue. Never assign manually—auto-applied by GH action only. O-AI-Review-Real-Issue-Found AI reviewer found real issue v26.2.0-prerelease X-perf-gain Microbenchmarks CI: Added if a performance gain is detected

Projects

None yet

Development

Successfully merging this pull request may close these issues.

sql: histogram does not match column type after ALTER TABLE ALTER COLUMN SET DATA TYPE

4 participants