Fix wrong implementation of getDataAt for multidimensional arrays

This commit is contained in:
Alexey Milovidov 2022-09-16 08:12:28 +02:00
parent c3ff66bd9d
commit 701fdd77b4
3 changed files with 27 additions and 5 deletions

View File

@ -151,22 +151,30 @@ void ColumnArray::get(size_t n, Field & res) const
StringRef ColumnArray::getDataAt(size_t n) const StringRef ColumnArray::getDataAt(size_t n) const
{ {
assert(n < size());
/** Returns the range of memory that covers all elements of the array. /** Returns the range of memory that covers all elements of the array.
* Works for arrays of fixed length values. * Works for arrays of fixed length values.
* For arrays of strings and arrays of arrays, the resulting chunk of memory may not be one-to-one correspondence with the elements, * For arrays of strings and arrays of arrays, the resulting chunk of memory may not be one-to-one correspondence with the elements,
* since it contains only the data laid in succession, but not the offsets. * since it contains only the data laid in succession, but not the offsets.
*/ */
size_t offset_of_first_elem = offsetAt(n);
StringRef first = getData().getDataAtWithTerminatingZero(offset_of_first_elem);
size_t array_size = sizeAt(n); size_t array_size = sizeAt(n);
if (array_size == 0)
return StringRef(first.data, 0);
if (array_size == 0)
return StringRef(nullptr, 0);
size_t offset_of_first_elem = offsetAt(n);
size_t offset_of_last_elem = getOffsets()[n] - 1; size_t offset_of_last_elem = getOffsets()[n] - 1;
StringRef first = getData().getDataAtWithTerminatingZero(offset_of_first_elem);
StringRef last = getData().getDataAtWithTerminatingZero(offset_of_last_elem); StringRef last = getData().getDataAtWithTerminatingZero(offset_of_last_elem);
if (first.empty())
return last;
if (last.empty())
return first;
return StringRef(first.data, last.data + last.size - first.data); return StringRef(first.data, last.data + last.size - first.data);
} }

View File

@ -0,0 +1,7 @@
!
Hello
Hello\0
World\0

View File

@ -0,0 +1,7 @@
SELECT formatRow('RawBLOB', [[[33]], []]);
SELECT formatRow('RawBLOB', [[[]], []]);
SELECT formatRow('RawBLOB', [[[[[[[0x48, 0x65, 0x6c, 0x6c, 0x6f]]]]]], []]);
SELECT formatRow('RawBLOB', []::Array(Array(Nothing)));
SELECT formatRow('RawBLOB', [[], [['Hello']]]);
SELECT formatRow('RawBLOB', [[['World']], []]);
SELECT formatRow('RawBLOB', []::Array(String));