diff --git a/src/Functions/FunctionsConversion.h b/src/Functions/FunctionsConversion.h index c9638ab95af..b3f892ffc0d 100644 --- a/src/Functions/FunctionsConversion.h +++ b/src/Functions/FunctionsConversion.h @@ -2147,14 +2147,18 @@ struct ToNumberMonotonicity if constexpr (std::is_floating_point_v) return { .is_monotonic = true, .is_always_monotonic = true }; - /// If converting from Float, for monotonicity, arguments must fit in range of result type. - bool is_type_float = false; - if (const auto * low_cardinality = typeid_cast(&type)) - is_type_float = WhichDataType(low_cardinality->getDictionaryType()).isFloat(); - else - is_type_float = WhichDataType(type).isFloat(); + const auto * low_cardinality = typeid_cast(&type); + const IDataType * low_cardinality_dictionary_type = nullptr; + if (low_cardinality) + low_cardinality_dictionary_type = low_cardinality->getDictionaryType().get(); - if (is_type_float) + WhichDataType which_type(type); + WhichDataType which_inner_type = low_cardinality + ? WhichDataType(low_cardinality_dictionary_type) + : WhichDataType(type); + + /// If converting from Float, for monotonicity, arguments must fit in range of result type. + if (which_inner_type.isFloat()) { if (left.isNull() || right.isNull()) return {}; @@ -2180,7 +2184,7 @@ struct ToNumberMonotonicity const size_t size_of_to = sizeof(T); /// Do not support 128 bit integers and decimals for now. - if (size_of_from > sizeof(Int64)) + if (size_of_from > sizeof(Int64) || which_inner_type.isDecimal()) return {}; const bool left_in_first_half = left.isNull() diff --git a/tests/queries/0_stateless/02519_monotonicity_fuzz.reference b/tests/queries/0_stateless/02519_monotonicity_fuzz.reference new file mode 100644 index 00000000000..9459d4ba2a0 --- /dev/null +++ b/tests/queries/0_stateless/02519_monotonicity_fuzz.reference @@ -0,0 +1 @@ +1.1 diff --git a/tests/queries/0_stateless/02519_monotonicity_fuzz.sql b/tests/queries/0_stateless/02519_monotonicity_fuzz.sql new file mode 100644 index 00000000000..4a0860702bb --- /dev/null +++ b/tests/queries/0_stateless/02519_monotonicity_fuzz.sql @@ -0,0 +1,5 @@ +DROP TABLE IF EXISTS t; +CREATE TABLE t (x Decimal(18, 3)) ENGINE = MergeTree ORDER BY x; +INSERT INTO t VALUES (1.1); +SELECT * FROM t WHERE toUInt64(x) = 1; +DROP TABLE t;