tupleElement(): Return default value for out-of-bounds-index

Makes the actual and the documented behavior consistent.
This commit is contained in:
Robert Schulze 2023-06-28 11:06:41 +00:00
parent eea3c39959
commit bf54fb4caa
No known key found for this signature in database
GPG Key ID: 26703B55FB13728A
3 changed files with 12 additions and 8 deletions

View File

@ -203,13 +203,15 @@ private:
{
const size_t index = index_column->getUInt(0);
if (index == 0)
throw Exception(ErrorCodes::ILLEGAL_INDEX, "Indices in tuples are 1-based.");
if (index > tuple.getElements().size())
throw Exception(ErrorCodes::ILLEGAL_INDEX, "Index for tuple element is out of range.");
if (index > 0 && index <= tuple.getElements().size())
return {index - 1};
else
{
if (argument_size == 2)
throw Exception(ErrorCodes::NOT_FOUND_COLUMN_IN_BLOCK, "Tuple doesn't have element with index '{}'", index);
return std::nullopt;
}
}
else if (const auto * name_col = checkAndGetColumnConst<ColumnString>(index_column.get()))
{

View File

@ -7,6 +7,8 @@ FROM t_tuple_element_default
z
SELECT tupleElement(t2, \'z\', \'z\')
FROM t_tuple_element_default
z
z
--------------------
[(3,4)]
SELECT tupleElement([(1, 2)], \'a\', [(3, 4)])

View File

@ -10,8 +10,8 @@ EXPLAIN SYNTAX SELECT tupleElement(t1, 'z', 0) FROM t_tuple_element_default;
SELECT tupleElement(t2, 'z', 'z') FROM t_tuple_element_default;
EXPLAIN SYNTAX SELECT tupleElement(t2, 'z', 'z') FROM t_tuple_element_default;
SELECT tupleElement(t1, 3, 'z') FROM t_tuple_element_default; -- { serverError ILLEGAL_INDEX }
SELECT tupleElement(t1, 0, 'z') FROM t_tuple_element_default; -- { serverError ILLEGAL_INDEX }
SELECT tupleElement(t1, 3, 'z') FROM t_tuple_element_default;
SELECT tupleElement(t1, 0, 'z') FROM t_tuple_element_default;
DROP TABLE t_tuple_element_default;