From c37b55c3b176df834144208a556f971762734a21 Mon Sep 17 00:00:00 2001 From: Alexey Milovidov Date: Thu, 17 Sep 2020 00:19:58 +0300 Subject: [PATCH] Fix error in "extractAllGroups" function --- src/Functions/extractAllGroups.h | 6 ++++-- .../01497_extract_all_groups_empty_match.reference | 2 ++ .../0_stateless/01497_extract_all_groups_empty_match.sql | 2 ++ 3 files changed, 8 insertions(+), 2 deletions(-) create mode 100644 tests/queries/0_stateless/01497_extract_all_groups_empty_match.reference create mode 100644 tests/queries/0_stateless/01497_extract_all_groups_empty_match.sql diff --git a/src/Functions/extractAllGroups.h b/src/Functions/extractAllGroups.h index d6ec9fadb73..f1168c2ebc2 100644 --- a/src/Functions/extractAllGroups.h +++ b/src/Functions/extractAllGroups.h @@ -129,7 +129,9 @@ public: for (size_t group = 1; group <= groups_count; ++group) data_col->insertData(matched_groups[group].data(), matched_groups[group].size()); - pos = matched_groups[0].data() + matched_groups[0].size(); + /// If match is empty - it's technically Ok but we have to shift one character nevertheless + /// to avoid infinite loop. + pos = matched_groups[0].data() + std::max(1, matched_groups[0].size()); current_nested_offset += groups_count; nested_offsets_data.push_back(current_nested_offset); @@ -167,7 +169,7 @@ public: for (size_t group = 1; group <= groups_count; ++group) all_matches.push_back(matched_groups[group]); - pos = matched_groups[0].data() + matched_groups[0].size(); + pos = matched_groups[0].data() + std::max(1, matched_groups[0].size()); ++matches_per_row; } diff --git a/tests/queries/0_stateless/01497_extract_all_groups_empty_match.reference b/tests/queries/0_stateless/01497_extract_all_groups_empty_match.reference new file mode 100644 index 00000000000..3479fb7a351 --- /dev/null +++ b/tests/queries/0_stateless/01497_extract_all_groups_empty_match.reference @@ -0,0 +1,2 @@ +[[''],[''],[''],[''],[''],[''],['']] +[['','','','','','','']] diff --git a/tests/queries/0_stateless/01497_extract_all_groups_empty_match.sql b/tests/queries/0_stateless/01497_extract_all_groups_empty_match.sql new file mode 100644 index 00000000000..1c4dafd9e2e --- /dev/null +++ b/tests/queries/0_stateless/01497_extract_all_groups_empty_match.sql @@ -0,0 +1,2 @@ +SELECT extractAllGroupsVertical('@#$%^&*', '(\w*)'); +SELECT extractAllGroupsHorizontal('@#$%^&*', '(\w*)');