release-21.1: opt,tree: fix bugs with Next(), Prev(), and histogram calculation for DTimeTZ #75173
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Backport 2/2 commits from #74914.
/cc @cockroachdb/release
sql/sem/tree: fix Next() and Prev() for DTimeTZ
Prior to this commit, the
DTimeTZ
functionsNext()
andPrev()
could skip over valid values according to the ordering of
DTimeTZ
values in an index (which matches the ordering defined by the
TimeTZ
functionsAfter()
andBefore()
).This commit fixes these functions so that
Next()
now returns the smallestvalid
DTimeTZ
that is greater than the receiver, andPrev()
returnsthe largest valid
DTimeTZ
that is less than the receiver. This isan important invariant that the optimizer relies on when building index
constraints.
Fixes #74912
Release note (bug fix): Fixed a bug that could occur when a
TIMETZ
column was indexed, and a query predicate constrained that column using
a
<
or>
operator with atimetz
constant. If the column contained valueswith time zones that did not match the time zone of the
timetz
constant,it was possible that not all matching values could be returned by the
query. Specifically, the results may not have included values within one
microsecond of the predicate's absolute time. This bug was introduced
when the timetz datatype was first added in 20.1. It exists on all
versions of 20.1, 20.2, 21.1, and 21.2 prior to this patch.
opt: fix bug in histogram calculation for TimeTZ
This commit fixes a bug in the histogram estimation code for
TimeTZ
that made the faulty assumption that
TimeTZ
values are ordered byTimeOfDay
. This is incorrect since it does not take theOffsetSecs
into account. As a result, it was possible to estimate that the size
of a histogram bucket was negative, which caused problems in the
statistics estimation code. This commit fixes the problem by taking
into account both
TimeOfDay
andOffsetSecs
when estimating the size ofa bucket in a
TimeTZ
histogram.Fixes #74667
Release note (bug fix): Fixed an internal error, "estimated row count must
be non-zero", that could occur during planning for queries over a table
with a
TimeTZ
column. This error was due to a faulty assumption in thestatistics estimation code about ordering of
TimeTZ
values, which has nowbeen fixed. The error could occur when
TimeTZ
values used in the query hada different time zone offset than the
TimeTZ
values stored in the table.