Skip to content

Commit b11eaf6

Browse files
Merge pull request ClickHouse#89933 from ClickHouse/backport/25.8/89908
Backport ClickHouse#89908 to 25.8: Fix logical error with empty tuple inside `reverse` and `CAST` function
2 parents a6a25fb + 452db28 commit b11eaf6

File tree

6 files changed

+81
-0
lines changed

6 files changed

+81
-0
lines changed

src/Functions/FunctionsConversion.h

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4744,6 +4744,13 @@ class FunctionCast final : public IFunctionBase
47444744
const auto * col = arguments.front().column.get();
47454745

47464746
size_t tuple_size = to_element_types.size();
4747+
4748+
if (tuple_size == 0)
4749+
{
4750+
/// Preserve the number of rows for empty tuple columns
4751+
return ColumnTuple::create(col->size());
4752+
}
4753+
47474754
const ColumnTuple & column_tuple = typeid_cast<const ColumnTuple &>(*col);
47484755

47494756
Columns converted_columns(tuple_size);

src/Functions/reverse.cpp

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,13 @@ class FunctionReverse : public IFunction
9898
if (const ColumnTuple * col_tuple = checkAndGetColumn<ColumnTuple>(column.get()))
9999
{
100100
size_t tuple_size = col_tuple->tupleSize();
101+
102+
if (tuple_size == 0)
103+
{
104+
/// Preserve the number of rows for empty tuple columns
105+
return ColumnTuple::create(col_tuple->size());
106+
}
107+
101108
Columns tuple_columns(tuple_size);
102109
for (size_t i = 0; i < tuple_size; ++i)
103110
{
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
()
2+
()
3+
()
4+
()
5+
()
6+
()
7+
()
8+
()
9+
()
10+
Tuple()
11+
(3,'a',1)
12+
[()]
13+
()
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
SELECT reverse(());
2+
3+
SELECT reverse(tuple());
4+
5+
SELECT reverse(()) FROM numbers(3);
6+
7+
WITH () AS x SELECT reverse(x);
8+
9+
DROP TABLE IF EXISTS table_rev_empty_tuple;
10+
CREATE TABLE table_rev_empty_tuple
11+
(
12+
x Tuple()
13+
) ENGINE = Memory;
14+
15+
INSERT INTO table_rev_empty_tuple SELECT tuple() FROM numbers(5);
16+
17+
SELECT reverse(x) FROM table_rev_empty_tuple LIMIT 3;
18+
19+
SELECT toTypeName(reverse(x)) FROM table_rev_empty_tuple LIMIT 1;
20+
21+
DROP TABLE table_rev_empty_tuple;
22+
23+
SELECT reverse((1, 'a', 3));
24+
25+
SELECT reverse([()]);
26+
27+
SELECT reverse((()));
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
()
2+
()
3+
()
4+
()
5+
()
6+
()
7+
()
8+
()
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
select CAST((), 'SimpleAggregateFunction(min, Tuple())');
2+
3+
DROP TABLE IF EXISTS tab;
4+
CREATE TABLE tab (c0 Tuple()) ENGINE = Memory;
5+
INSERT INTO tab VALUES (()), (()), (());
6+
7+
SELECT CAST(c0, 'SimpleAggregateFunction(min, Tuple())') FROM tab;
8+
9+
DROP TABLE IF EXISTS t0;
10+
CREATE TABLE t0 (c0 SimpleAggregateFunction(min, Tuple())) ENGINE = MergeTree() ORDER BY tuple();
11+
INSERT INTO t0 (c0) VALUES (tuple());
12+
SELECT * FROM t0;
13+
14+
DROP TABLE IF EXISTS t1;
15+
CREATE TABLE t1 (c0 SimpleAggregateFunction(min, Tuple())) ENGINE = MergeTree() ORDER BY tuple() SETTINGS enable_block_number_column = 1, enable_block_offset_column = 1;
16+
UPDATE t1 SET c0 = () WHERE TRUE;
17+
INSERT INTO t1 (c0) VALUES (tuple()), (tuple()), (tuple());
18+
UPDATE t1 SET c0 = () WHERE TRUE;
19+
SELECT * FROM t1;

0 commit comments

Comments
 (0)