From 4b7abbce68d65f51e316501d4fdb6fc85421208d Mon Sep 17 00:00:00 2001 From: Duc Canh Le Date: Mon, 29 Apr 2024 01:33:48 +0000 Subject: [PATCH] fix abs monotonicity Signed-off-by: Duc Canh Le --- src/Functions/abs.cpp | 2 +- src/Storages/MergeTree/KeyCondition.cpp | 4 ++-- .../0_stateless/03130_abs_in_key_condition_bug.reference | 2 ++ .../queries/0_stateless/03130_abs_in_key_condition_bug.sql | 6 ++++++ 4 files changed, 11 insertions(+), 3 deletions(-) create mode 100644 tests/queries/0_stateless/03130_abs_in_key_condition_bug.reference create mode 100644 tests/queries/0_stateless/03130_abs_in_key_condition_bug.sql diff --git a/src/Functions/abs.cpp b/src/Functions/abs.cpp index 25ff6cc93d0..0cd313caf1e 100644 --- a/src/Functions/abs.cpp +++ b/src/Functions/abs.cpp @@ -45,7 +45,7 @@ template <> struct FunctionUnaryArithmeticMonotonicity if ((left_float < 0 && right_float > 0) || (left_float > 0 && right_float < 0)) return {}; - return { .is_monotonic = true, .is_positive = left_float > 0, .is_strict = true, }; + return { .is_monotonic = true, .is_positive = std::min(left_float, right_float) >= 0, .is_strict = true, }; } }; diff --git a/src/Storages/MergeTree/KeyCondition.cpp b/src/Storages/MergeTree/KeyCondition.cpp index 2d57ea40c9c..304bfb567c3 100644 --- a/src/Storages/MergeTree/KeyCondition.cpp +++ b/src/Storages/MergeTree/KeyCondition.cpp @@ -2860,9 +2860,9 @@ bool KeyCondition::mayBeTrueInRange( String KeyCondition::RPNElement::toString() const { if (argument_num_of_space_filling_curve) - return toString(fmt::format("argument {} of column {}", *argument_num_of_space_filling_curve, key_column), false); + return toString(fmt::format("argument {} of column {}", *argument_num_of_space_filling_curve, key_column), true); else - return toString(fmt::format("column {}", key_column), false); + return toString(fmt::format("column {}", key_column), true); } String KeyCondition::RPNElement::toString(std::string_view column_name, bool print_constants) const diff --git a/tests/queries/0_stateless/03130_abs_in_key_condition_bug.reference b/tests/queries/0_stateless/03130_abs_in_key_condition_bug.reference new file mode 100644 index 00000000000..9cf7186540c --- /dev/null +++ b/tests/queries/0_stateless/03130_abs_in_key_condition_bug.reference @@ -0,0 +1,2 @@ +1 2023-05-04 22:17:23 0 +2 2023-05-04 22:17:23 0 diff --git a/tests/queries/0_stateless/03130_abs_in_key_condition_bug.sql b/tests/queries/0_stateless/03130_abs_in_key_condition_bug.sql new file mode 100644 index 00000000000..d9e9ea6fa50 --- /dev/null +++ b/tests/queries/0_stateless/03130_abs_in_key_condition_bug.sql @@ -0,0 +1,6 @@ +CREATE TABLE t (id UInt64, ts DateTime) ENGINE = MergeTree() ORDER BY (id, ts) SETTINGS index_granularity = 2; + +INSERT INTO t VALUES (1, '2023-05-04 21:17:23') (1, '2023-05-04 22:17:23') (2, '2023-05-04 22:17:23') (2, '2023-05-04 23:17:23'); + +SELECT *, abs(toUnixTimestamp(ts) - toUnixTimestamp(1683238643)) AS error FROM t WHERE error < 3600; +