Repository navigation
Add monotonic toStartOf*Extended, toLastDayOf*Extended, toMondayExtended, and toRelative*NumExtended datetime functions for primary key and partition expressions - #108002
Conversation
…rter,Year}, toLastDayOfMonth
…day toStartOf* family
…atetime_functions setting
|
Workflow [PR], commit [906e935] Summary: ❌
AI ReviewSummaryThis PR adds always-on Missing context / blind spots
Findings
💡 Nits
Tests
Final VerdictStatus: Minimum required actions: fix the duplicate |
| INSERT INTO t_d32_post_2149 VALUES ('2150-01-15'), ('2200-06-15'), ('2299-12-31'); | ||
|
|
||
| SELECT trimLeft(explain) FROM (EXPLAIN indexes = 1 SELECT count() FROM t_d32_post_2149 WHERE toStartOfYearExtended(d) >= toDate32('2150-01-01') SETTINGS optimize_use_implicit_projections = 0) WHERE explain LIKE '%Condition%' OR explain LIKE '%Parts%' OR explain LIKE '%Granules%' OR explain LIKE '%Keys%' OR explain LIKE '%Search Algorithm%' OR explain LIKE '%Min-Max%' OR explain LIKE '%Partition%' OR explain LIKE '%PrimaryKey%'; | ||
| SELECT count() FROM t_d32_post_2149 WHERE toStartOfYearExtended(d) >= toDate32('2150-01-01') SETTINGS optimize_use_implicit_projections = 0 SETTINGS force_primary_key = 1; |
There was a problem hiding this comment.
This query has two SETTINGS clauses in the same SELECT. The parser expects a single settings list, so this stateless test will fail before it reaches the force_primary_key check. Please combine them:
SETTINGS optimize_use_implicit_projections = 0, force_primary_key = 1| FunctionDocumentation::Description description = R"( | ||
| Same as [`toLastDayOfMonth`](#toLastDayOfMonth), but the result is monotonic across the entire range of every supported argument type, regardless of the [`enable_extended_results_for_datetime_functions`](/operations/settings/settings#enable_extended_results_for_datetime_functions) setting. | ||
|
|
||
| The result type is widened only as far as is needed to represent the result without wrapping. The function returns [`Date32`](/sql-reference/data-types/date32) for `Date32` and `DateTime64` arguments, and `Date` for `Date` and `DateTime` arguments. |
There was a problem hiding this comment.
The implementation widens Date input for this Extended variant (widen_date_input = true), so toLastDayOfMonthExtended(toDate('2149-06-06')) returns Date32, not Date. This source documentation publishes the wrong return-type contract; please include Date in the Date32 case. The same correction is needed for toLastDayOfWeekExtended.
|
The problem of fixing incorrect query result is already handled by some bugfix PRs. This PR can be revisited if needed. |
Related: #105244, #101814
The base functions wrongly tell that they are always monotonic but they are not for some input types and can give wrong results when column contains extreme values or even some cases pre-epoch values. This PR creates the
Extendedversions of these base function that guarantee that the functions will always be monotonic across all four input types:Date,DateTime,Date32,DateTime64. For example:Additionally,
enable_extended_results_for_datetime_functionsdoes not cover all the cases; the setting only widens when the input type isDate32orDateTime64but does not when input isDate.Changelog category (leave one):
Changelog entry (a user-readable short description of the changes that goes into CHANGELOG.md):
Add always-monotonic
*Extendedvariants of the datetime functions:toStartOfDayExtended,toStartOfHourExtended,toStartOfMinuteExtended,toStartOfFiveMinutesExtended,toStartOfTenMinutesExtended,toStartOfFifteenMinutesExtended,toMondayExtended,toStartOfWeekExtended,toStartOfMonthExtended,toStartOfQuarterExtended,toStartOfYearExtended,toLastDayOfMonthExtended,toLastDayOfWeekExtended,toRelativeWeekNumExtended,toRelativeDayNumExtended,toRelativeHourNumExtended,toRelativeMinuteNumExtended, andtoRelativeSecondNumExtended. Each returns a wide-enough result type (Date32,DateTime64, orInt64) so that, unlike the corresponding base function, the result does not wrap for arguments outside theDate/DateTimerange and stays monotonic over the whole input range. This makes them the recommended choice in primary key and partition key expressions, where the primary index can then prune data correctly, independent of theenable_extended_results_for_datetime_functionssetting.