Backport #71849 to 24.9: Fix: add monotonic estimation for DateTime64 in toDayOfWeek

This commit is contained in:
robot-clickhouse 2024-11-18 16:07:23 +00:00
parent 4d1b4c3c41
commit 88a72105d7
4 changed files with 25 additions and 3 deletions

View File

@ -63,6 +63,7 @@ constexpr time_t MAX_DATETIME_DAY_NUM = 49710; // 2106-02-07
/// This factor transformation will say that the function is monotone everywhere.
struct ZeroTransform
{
static constexpr auto name = "Zero";
static UInt16 execute(Int64, const DateLUTImpl &) { return 0; }
static UInt16 execute(UInt32, const DateLUTImpl &) { return 0; }
static UInt16 execute(Int32, const DateLUTImpl &) { return 0; }

View File

@ -55,13 +55,26 @@ public:
? is_monotonic
: is_not_monotonic;
}
else
if (checkAndGetDataType<DataTypeDateTime64>(&type))
{
return Transform::FactorTransform::execute(UInt32(left.safeGet<UInt64>()), date_lut)
== Transform::FactorTransform::execute(UInt32(right.safeGet<UInt64>()), date_lut)
const auto & left_date_time = left.safeGet<DateTime64>();
TransformDateTime64<typename Transform::FactorTransform> transformer_left(left_date_time.getScale());
const auto & right_date_time = right.safeGet<DateTime64>();
TransformDateTime64<typename Transform::FactorTransform> transformer_right(right_date_time.getScale());
return transformer_left.execute(left_date_time.getValue(), date_lut)
== transformer_right.execute(right_date_time.getValue(), date_lut)
? is_monotonic
: is_not_monotonic;
}
return Transform::FactorTransform::execute(UInt32(left.safeGet<UInt64>()), date_lut)
== Transform::FactorTransform::execute(UInt32(right.safeGet<UInt64>()), date_lut)
? is_monotonic
: is_not_monotonic;
}
protected:

View File

@ -0,0 +1,7 @@
DROP TABLE IF EXISTS decimal_dt;
CREATE TABLE decimal_dt (timestamp DateTime64(9)) ENGINE=MergeTree() ORDER BY timestamp;
INSERT INTO decimal_dt VALUES (toDate('2024-11-11')),(toDate('2024-11-12')),(toDate('2024-11-13')),(toDate('2024-11-14')),(toDate('2024-11-15')),(toDate('2024-11-16')),(toDate('2024-11-17'));
SELECT count() FROM decimal_dt WHERE toDayOfWeek(timestamp) > 3;
DROP TABLE IF EXISTS decimal_dt;