Skip to content

Commit d6b27f2

Browse files
authored
Merge pull request ClickHouse#80207 from ClickHouse/yarik/fix-comparison-conversion
Fix numeric comparison in keyCondition
2 parents 824a937 + 85757ed commit d6b27f2

File tree

3 files changed

+15
-15
lines changed

3 files changed

+15
-15
lines changed

src/Storages/MergeTree/KeyCondition.cpp

Lines changed: 6 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,6 @@ namespace Setting
5858
namespace ErrorCodes
5959
{
6060
extern const int LOGICAL_ERROR;
61-
extern const int BAD_TYPE_OF_FIELD;
6261
}
6362

6463
/// Returns the prefix of like_pattern before the first wildcard, e.g. 'Hello\_World% ...' --> 'Hello\_World'
@@ -1982,19 +1981,6 @@ KeyCondition::RPNElement::RPNElement(Function function_, size_t key_column_, con
19821981
{
19831982
}
19841983

1985-
static void castValueToType(const DataTypePtr & desired_type, Field & src_value, const DataTypePtr & src_type, const String & node_column_name)
1986-
{
1987-
try
1988-
{
1989-
src_value = convertFieldToType(src_value, *desired_type, src_type.get());
1990-
}
1991-
catch (...)
1992-
{
1993-
throw Exception(ErrorCodes::BAD_TYPE_OF_FIELD, "Key expression contains comparison between inconvertible types: "
1994-
"{} and {} inside {}", desired_type->getName(), src_type->getName(), node_column_name);
1995-
}
1996-
}
1997-
19981984

19991985
bool KeyCondition::extractAtomFromTree(const RPNBuilderTreeNode & node, RPNElement & out)
20001986
{
@@ -2254,7 +2240,12 @@ bool KeyCondition::extractAtomFromTree(const RPNBuilderTreeNode & node, RPNEleme
22542240

22552241
if (!const_type->equals(*common_type))
22562242
{
2257-
castValueToType(common_type, const_value, const_type, node.getColumnName());
2243+
// Replace direct call that throws exception with try version
2244+
Field converted = tryConvertFieldToType(const_value, *common_type, const_type.get(), {});
2245+
if (converted.isNull())
2246+
return false;
2247+
2248+
const_value = converted;
22582249

22592250
// Need to set is_constant_transformed unless we're doing exact conversion
22602251
if (!key_expr_type_not_null->equals(*common_type))
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
1
2+
0
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
SELECT 1 FROM numbers(1) WHERE toIntervalHour(number) = 0;
2+
3+
CREATE TABLE t1 (c0 Decimal(18,0)) ENGINE = MergeTree() ORDER BY (c0);
4+
INSERT INTO TABLE t1(c0) VALUES (1);
5+
6+
SELECT c0 = 6812671276462221925::Int64 FROM t1;
7+
SELECT 1 FROM t1 WHERE c0 = 6812671276462221925::Int64;

0 commit comments

Comments
 (0)