Merge pull request #33861 from RogerYK/fix-substring-negative-offset-length

Fix substring negative offset length
This commit is contained in:
Alexey Milovidov 2022-05-08 01:09:36 +03:00 committed by GitHub
commit 3a2ddac2e6
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
12 changed files with 158 additions and 43 deletions

View File

@ -620,9 +620,9 @@ arraySlice(array, offset[, length])
**Arguments**
- `array` Array of data.
- `offset` Indent from the edge of the array. A positive value indicates an offset on the left, and a negative value is an indent on the right. Numbering of the array items begins with 1.
- `length` The length of the required slice. If you specify a negative value, the function returns an open slice `[offset, array_length - length)`. If you omit the value, the function returns the slice `[offset, the_end_of_array]`.
- `array` Array of data.
- `offset` Indent from the edge of the array. A positive value indicates an offset on the left, and a negative value is an indent on the right. Numbering of the array items begins with 1.
- `length` The length of the required slice. If you specify a negative value, the function returns an open slice `[offset, array_length - length]`. If you omit the value, the function returns the slice `[offset, the_end_of_array]`.
**Example**

View File

@ -130,13 +130,9 @@ bitSlice(s, offset[, length])
**Arguments**
- `s` — s is [String](../../sql-reference/data-types/string.md)
or [FixedString](../../sql-reference/data-types/fixedstring.md).
- `offset` — The start index with bit, A positive value indicates an offset on the left, and a negative value is an
indent on the right. Numbering of the bits begins with 1.
- `length` — The length of substring with bit. If you specify a negative value, the function returns an open substring [
offset, array_length - length). If you omit the value, the function returns the substring [offset, the_end_string].
If length exceeds s, it will be truncate.If length isn't multiple of 8, will fill 0 on the right.
- `s` — s is [String](../../sql-reference/data-types/string.md) or [FixedString](../../sql-reference/data-types/fixedstring.md).
- `offset` — The start index with bit, A positive value indicates an offset on the left, and a negative value is an indent on the right. Numbering of the bits begins with 1.
- `length` — The length of substring with bit. If you specify a negative value, the function returns an open substring \[offset, array_length - length\]. If you omit the value, the function returns the substring \[offset, the_end_string\]. If length exceeds s, it will be truncate.If length isn't multiple of 8, will fill 0 on the right.
**Returned value**

View File

@ -480,7 +480,7 @@ Result:
## substring(s, offset, length), mid(s, offset, length), substr(s, offset, length) {#substring}
Returns a substring starting with the byte from the offset index that is length bytes long. Character indexing starts from one (as in standard SQL). The offset and length arguments must be constants.
Returns a substring starting with the byte from the offset index that is length bytes long. Character indexing starts from one (as in standard SQL).
## substringUTF8(s, offset, length) {#substringutf8}

View File

@ -574,9 +574,9 @@ arraySlice(array, offset[, length])
**Аргументы**
- `array` массив данных.
- `offset` отступ от края массива. Положительное значение - отступ слева, отрицательное значение - отступ справа. Отсчет элементов массива начинается с 1.
- `length` длина необходимого среза. Если указать отрицательное значение, то функция вернёт открытый срез `[offset, array_length - length)`. Если не указать значение, то функция вернёт срез `[offset, the_end_of_array]`.
- `array` массив данных.
- `offset` отступ от края массива. Положительное значение - отступ слева, отрицательное значение - отступ справа. Отсчёт элементов массива начинается с 1.
- `length` длина необходимого среза. Если указать отрицательное значение, то функция вернёт открытый срез `[offset, array_length - length]`. Если не указать значение, то функция вернёт срез `[offset, the_end_of_array]`.
**Пример**

View File

@ -397,9 +397,9 @@ SELECT arrayPushFront(['b'], 'a') AS res
**参数**
- `array` 数组。
- `offset` 数组的偏移。正值表示左侧的偏移量负值表示右侧的缩进值。数组下标从1开始。
- `length` - 子数组的长度。如果指定负值,则该函数返回`[offsetarray_length - length`。如果省略该值,则该函数返回`[offsetthe_end_of_array]`。
- `array` 数组。
- `offset` 数组的偏移。正值表示左侧的偏移量负值表示右侧的缩进值。数组下标从1开始。
- `length` - 子数组的长度。如果指定负值,则该函数返回`[offsetarray_length - length]`。如果省略该值,则该函数返回`[offsetthe_end_of_array]`。
**示例**

View File

@ -308,7 +308,7 @@ void NO_INLINE sliceFromRightConstantOffsetBounded(Source && src, Sink && sink,
{
ssize_t size = length;
if (size < 0)
size += static_cast<ssize_t>(src.getElementSize()) - offset;
size += offset;
if (size > 0)
writeSlice(src.getSliceFromRight(offset, size), sink);

View File

@ -217,12 +217,12 @@ arraySlice(arr1, -4, 2)
arraySlice(arr1, -4, -1)
[] 2 4 []
[] 2 4 []
[1,2,3,4,5] 2 4 []
[1,2,3,4,5,6,7] 2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7] 2 4 [4,NULL,6]
[1,2,3,NULL,5,6,7] 2 4 [NULL,5,6]
[1,2,3,4,5,NULL,7] 2 4 [4,5,NULL]
[1,2,3,4,5,6,7] 2 4 [4,5,6]
[1,NULL,3,NULL,5,6,7] 2 4 [NULL,5,6]
arrayPushFront(arr1, 1)
[] [1]
[] [1]
@ -1104,12 +1104,12 @@ arraySlice(arr1, -4, 2)
arraySlice(arr1, -4, -1)
[] 2 4 []
[] 2 4 []
['1','2','3','4','5'] 2 4 []
['1',NULL,'3','4',NULL,'6','7'] 2 4 ['4',NULL]
['1','2','3',NULL,'5','6','7'] 2 4 [NULL,'5']
['1','2','3','4','5',NULL,'7'] 2 4 ['4','5']
['1','2','3','4','5','6','7'] 2 4 ['4','5']
['1',NULL,'3',NULL,'5','6','7'] 2 4 [NULL,'5']
['1','2','3','4','5'] 2 4 ['2','3','4']
['1',NULL,'3','4',NULL,'6','7'] 2 4 ['4',NULL,'6']
['1','2','3',NULL,'5','6','7'] 2 4 [NULL,'5','6']
['1','2','3','4','5',NULL,'7'] 2 4 ['4','5',NULL]
['1','2','3','4','5','6','7'] 2 4 ['4','5','6']
['1',NULL,'3',NULL,'5','6','7'] 2 4 [NULL,'5','6']
arrayPushFront(arr1, 1)
[] ['1']
[] ['1']

View File

@ -0,0 +1,15 @@
[]
[]
[]
[7]
[7]
[7]
-
[]
[]
[]
[6]
[]
[]
[]
[]

View File

@ -0,0 +1,25 @@
select arraySlice([1, 2, 3, 4, 5, 6, 7, 8], -2, -2);
select arraySlice(materialize([1, 2, 3, 4, 5, 6, 7, 8]), -2, -2);
select arraySlice(materialize([1, 2, 3, 4, 5, 6, 7, 8]), materialize(-2), materialize(-2));
select arraySlice([1, 2, 3, 4, 5, 6, 7, 8], -2, -1);
select arraySlice(materialize([1, 2, 3, 4, 5, 6, 7, 8]), -2, -1);
select arraySlice(materialize([1, 2, 3, 4, 5, 6, 7, 8]), materialize(-2), materialize(-1));
select '-';
drop table if exists t;
create table t
(
s Array(Int),
l Int8,
r Int8
) engine = Memory;
insert into t values ([1, 2, 3, 4, 5, 6, 7, 8], -2, -2), ([1, 2, 3, 4, 5, 6, 7, 8], -3, -3);
select arraySlice(s, -2, -2) from t;
select arraySlice(s, l, -2) from t;
select arraySlice(s, -2, r) from t;
select arraySlice(s, l, r) from t;
drop table t;

View File

@ -0,0 +1,31 @@
g
g
g
-
g
g
g
-
6
-
6

View File

@ -0,0 +1,48 @@
select substring('abcdefgh', -2, -2);
select substring(materialize('abcdefgh'), -2, -2);
select substring(materialize('abcdefgh'), materialize(-2), materialize(-2));
select substring('abcdefgh', -2, -1);
select substring(materialize('abcdefgh'), -2, -1);
select substring(materialize('abcdefgh'), materialize(-2), materialize(-1));
select '-';
select substring(cast('abcdefgh' as FixedString(8)), -2, -2);
select substring(materialize(cast('abcdefgh' as FixedString(8))), -2, -2);
select substring(materialize(cast('abcdefgh' as FixedString(8))), materialize(-2), materialize(-2));
select substring(cast('abcdefgh' as FixedString(8)), -2, -1);
select substring(materialize(cast('abcdefgh' as FixedString(8))), -2, -1);
select substring(materialize(cast('abcdefgh' as FixedString(8))), materialize(-2), materialize(-1));
select '-';
drop table if exists t;
create table t
(
s String,
l Int8,
r Int8
) engine = Memory;
insert into t values ('abcdefgh', -2, -2),('12345678', -3, -3);
select substring(s, -2, -2) from t;
select substring(s, l, -2) from t;
select substring(s, -2, r) from t;
select substring(s, l, r) from t;
select '-';
drop table if exists t;
create table t(
s FixedString(8),
l Int8,
r Int8
) engine = Memory;
insert into t values ('abcdefgh', -2, -2),('12345678', -3, -3);
select substring(s, -2, -2) from t;
select substring(s, l, -2) from t;
select substring(s, -2, r) from t;
select substring(s, l, r) from t;
drop table if exists t;