From 55c20ce45b09b395719e142c3ca30dc3af4b5a9d Mon Sep 17 00:00:00 2001 From: Duc Canh Le Date: Wed, 31 Aug 2022 21:43:08 +0800 Subject: [PATCH] fix tryGetPreparedSet crash when same set expression built from different columns --- src/Storages/MergeTree/KeyCondition.cpp | 5 +++++ .../0_stateless/02416_in_set_same_ast_diff_columns.reference | 1 + .../0_stateless/02416_in_set_same_ast_diff_columns.sql | 3 +++ 3 files changed, 9 insertions(+) create mode 100644 tests/queries/0_stateless/02416_in_set_same_ast_diff_columns.reference create mode 100644 tests/queries/0_stateless/02416_in_set_same_ast_diff_columns.sql diff --git a/src/Storages/MergeTree/KeyCondition.cpp b/src/Storages/MergeTree/KeyCondition.cpp index b42fe49a1d0..609f3ddac59 100644 --- a/src/Storages/MergeTree/KeyCondition.cpp +++ b/src/Storages/MergeTree/KeyCondition.cpp @@ -297,8 +297,13 @@ public: assert(indexes_mapping.size() == data_types.size()); for (size_t i = 0; i < indexes_mapping.size(); ++i) + { + /// Same set expression can be construct from different column(s) with different types + if (indexes_mapping[i].tuple_index >= candidate_set->getDataTypes().size()) + return false; if (!candidate_set->areTypesEqual(indexes_mapping[i].tuple_index, data_types[i])) return false; + } return true; }; diff --git a/tests/queries/0_stateless/02416_in_set_same_ast_diff_columns.reference b/tests/queries/0_stateless/02416_in_set_same_ast_diff_columns.reference new file mode 100644 index 00000000000..d00491fd7e5 --- /dev/null +++ b/tests/queries/0_stateless/02416_in_set_same_ast_diff_columns.reference @@ -0,0 +1 @@ +1 diff --git a/tests/queries/0_stateless/02416_in_set_same_ast_diff_columns.sql b/tests/queries/0_stateless/02416_in_set_same_ast_diff_columns.sql new file mode 100644 index 00000000000..36ab424f34f --- /dev/null +++ b/tests/queries/0_stateless/02416_in_set_same_ast_diff_columns.sql @@ -0,0 +1,3 @@ +CREATE TABLE set_crash (key1 Int32, id1 Int64, c1 Int64) ENGINE = MergeTree PARTITION BY id1 ORDER BY key1; +INSERT INTO set_crash VALUES (-1, 1, 0); +SELECT 1 in (-1,1) FROM set_crash WHERE (key1, id1) in (-1, 1);