Skip to content

Commit d058d91

Browse files
authored
Merge pull request ClickHouse#79158 from petern48/fix_arraySymmetricDifference
Bug Fix arraySymmetricDifference() to work properly for tables with batch inserts
2 parents 253f4eb + d88e03f commit d058d91

File tree

3 files changed

+51
-17
lines changed

3 files changed

+51
-17
lines changed

src/Functions/array/arrayIntersect.cpp

Lines changed: 11 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -640,6 +640,13 @@ ColumnPtr FunctionArrayIntersect<Mode>::execute(const UnpackedArrays & arrays, M
640640
// We have NULL in output only once if it should be there
641641
bool null_added = false;
642642
bool use_null_map;
643+
const auto & arg = arrays.args[0];
644+
size_t off;
645+
// const array has only one row
646+
if (arg.is_const)
647+
off = (*arg.offsets)[0];
648+
else
649+
off = (*arg.offsets)[row];
643650

644651
if constexpr (std::is_same_v<Mode, ArrayModeUnion>)
645652
{
@@ -657,12 +664,6 @@ ColumnPtr FunctionArrayIntersect<Mode>::execute(const UnpackedArrays & arrays, M
657664
null_map.push_back(1);
658665
null_added = true;
659666
}
660-
const auto & arg = arrays.args[0];
661-
// const array has only one row
662-
if (arg.is_const)
663-
prev_off[0] = 0;
664-
else
665-
prev_off[0] = (*arg.offsets)[row];
666667
}
667668
else if constexpr (std::is_same_v<Mode, ArrayModeSymmetricDifference>)
668669
{
@@ -684,13 +685,6 @@ ColumnPtr FunctionArrayIntersect<Mode>::execute(const UnpackedArrays & arrays, M
684685
else if constexpr (std::is_same_v<Mode, ArrayModeIntersect>)
685686
{
686687
use_null_map = all_nullable;
687-
const auto & arg = arrays.args[0];
688-
size_t off;
689-
// const array has only one row
690-
if (arg.is_const)
691-
off = (*arg.offsets)[0];
692-
else
693-
off = (*arg.offsets)[row];
694688

695689
for (auto i : collections::range(prev_off[0], off))
696690
{
@@ -719,9 +713,6 @@ ColumnPtr FunctionArrayIntersect<Mode>::execute(const UnpackedArrays & arrays, M
719713
const char * data = nullptr;
720714
pair = map.find(columns[0]->serializeValueIntoArena(i, arena, data));
721715
}
722-
prev_off[0] = off;
723-
if (arg.is_const)
724-
prev_off[0] = 0;
725716

726717
if (!current_has_nullable)
727718
all_has_nullable = false;
@@ -732,6 +723,10 @@ ColumnPtr FunctionArrayIntersect<Mode>::execute(const UnpackedArrays & arrays, M
732723
insertElement<Map, ColumnType, is_numeric_column>(pair, result_offset, result_data, null_map, use_null_map);
733724
}
734725
}
726+
// Now we update the offsets for the first array
727+
prev_off[0] = off;
728+
if (arg.is_const)
729+
prev_off[0] = 0;
735730

736731
result_offsets.getElement(row) = result_offset;
737732
}

tests/queries/0_stateless/03357_arraySymmetricDifference.reference

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,3 +14,13 @@ Array(Tuple(Nullable(UInt8), Array(Nullable(String))))
1414
Non-const arguments
1515
[(NULL,['c']),(2,['c',NULL])]
1616
[(NULL,['c']),(2,['c',NULL])]
17+
1 ['1','2']
18+
2 []
19+
3 ['2']
20+
4 ['1','4']
21+
5 ['5']
22+
6 ['5','4']
23+
7 ['0','7']
24+
8 ['10','8','9']
25+
9 ['-1','9']
26+
10 ['5','10']

tests/queries/0_stateless/03357_arraySymmetricDifference.sql

Lines changed: 30 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,4 +27,33 @@ SELECT arraySymmetricDifference(f, s);
2727
WITH
2828
materialize([(1, ['a', 'b']::Array(LowCardinality(String))), (NULL, ['c']::Array(LowCardinality(String)))]) AS f,
2929
materialize([(2, ['c', NULL]::Array(LowCardinality(Nullable(String)))), (1, ['a', 'b']::Array(LowCardinality(String)))]) AS s
30-
SELECT arraySymmetricDifference(f, s)
30+
SELECT arraySymmetricDifference(f, s);
31+
32+
-- Table with batch inserts
33+
DROP TABLE IF EXISTS test_arraySymmetricDifference;
34+
CREATE TABLE test_arraySymmetricDifference
35+
(
36+
`id` Int8,
37+
`arr1` Array(String),
38+
`arr2` Array(String)
39+
)
40+
ENGINE = MergeTree
41+
ORDER BY id;
42+
43+
INSERT INTO test_arraySymmetricDifference
44+
VALUES
45+
(1, ['1'], ['2']),
46+
(2, ['2'], ['2']),
47+
(3, ['3'], ['3', '2']),
48+
(4, ['4'], ['1']),
49+
(5, ['5'], []),
50+
(6, ['6', '4'], ['5', '6']),
51+
(7, ['7', '0'], []),
52+
(8, ['8', '9', '10'], []),
53+
(9, ['9'], ['-1']),
54+
(10, ['10'], ['5']);
55+
56+
SELECT
57+
ta.id AS id,
58+
arraySymmetricDifference(ta.arr1, ta.arr2) AS symmetricDifference
59+
FROM test_arraySymmetricDifference ta;

0 commit comments

Comments
 (0)