Skip to content

Commit 28fdf49

Browse files
Backport #86230 to 25.8: Fix crash in case of const and non-const blocks in one INSERT
1 parent 44dd6bb commit 28fdf49

File tree

4 files changed

+96
-49
lines changed

4 files changed

+96
-49
lines changed

src/Interpreters/InterpreterInsertQuery.cpp

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131
#include <Processors/Transforms/CountingTransform.h>
3232
#include <Processors/Transforms/ExpressionTransform.h>
3333
#include <Processors/Transforms/DeduplicationTokenTransforms.h>
34+
#include <Processors/Transforms/MaterializingTransform.h>
3435
#include <Processors/Transforms/PlanSquashingTransform.h>
3536
#include <Processors/Transforms/ApplySquashingTransform.h>
3637
#include <Processors/Transforms/getSourceFromASTInsertQuery.h>
@@ -435,8 +436,18 @@ QueryPipeline InterpreterInsertQuery::addInsertToSelectPipeline(ASTInsertQuery &
435436

436437
pipeline.resize(1);
437438

438-
if (shouldAddSquashingForStorage(table, getContext()) && !no_squash && !async_insert)
439+
bool should_squash = shouldAddSquashingForStorage(table, getContext()) && !no_squash && !async_insert;
440+
if (should_squash)
439441
{
442+
/// Squashing cannot work with const and non-const blocks
443+
pipeline.addSimpleTransform([&](const SharedHeader & in_header) -> ProcessorPtr
444+
{
445+
/// Sparse columns will be converted to full in the InsertDependenciesBuilder,
446+
/// and for squashing we don't need to convert column to full since it will do it by itself
447+
bool remove_sparse = false;
448+
return std::make_shared<MaterializingTransform>(in_header, remove_sparse);
449+
});
450+
440451
pipeline.addSimpleTransform(
441452
[&](const SharedHeader & in_header) -> ProcessorPtr
442453
{
@@ -485,7 +496,7 @@ QueryPipeline InterpreterInsertQuery::addInsertToSelectPipeline(ASTInsertQuery &
485496

486497
pipeline.resize(sink_streams_size);
487498

488-
if (shouldAddSquashingForStorage(table, getContext()) && !no_squash && !async_insert)
499+
if (should_squash)
489500
{
490501
pipeline.addSimpleTransform(
491502
[&](const SharedHeader & in_header) -> ProcessorPtr

tests/queries/0_stateless/03531_insert_removing_sparse_transform.reference

Lines changed: 55 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -11,14 +11,15 @@ digraph
1111
n1[label="ExpressionTransform_1"];
1212
n2[label="ExpressionTransform_2"];
1313
n3[label="CountingTransform_3"];
14-
n4[label="PlanSquashingTransform_4"];
15-
n5[label="DeduplicationToken::AddTokenInfoTransform_5"];
16-
n6[label="ApplySquashingTransform_10"];
17-
n7[label="ConvertingTransform_6"];
18-
n8[label="NestedElementsValidationTransform_7"];
19-
n9[label="RemovingSparseTransform_8"];
20-
n10[label="LogSink_9"];
21-
n11[label="EmptySink_11"];
14+
n4[label="MaterializingTransform_4"];
15+
n5[label="PlanSquashingTransform_5"];
16+
n6[label="DeduplicationToken::AddTokenInfoTransform_6"];
17+
n7[label="ApplySquashingTransform_11"];
18+
n8[label="ConvertingTransform_7"];
19+
n9[label="NestedElementsValidationTransform_8"];
20+
n10[label="RemovingSparseTransform_9"];
21+
n11[label="LogSink_10"];
22+
n12[label="EmptySink_12"];
2223
}
2324
n0 -> n1;
2425
n1 -> n2;
@@ -31,6 +32,7 @@ digraph
3132
n8 -> n9;
3233
n9 -> n10;
3334
n10 -> n11;
35+
n11 -> n12;
3436
}
3537
-- MergeTree support sparse columns - no RemovingSparseTransform
3638
create table t_mt (key Int) engine=MergeTree order by ();
@@ -43,13 +45,14 @@ digraph
4345
n1[label="ExpressionTransform_1"];
4446
n2[label="ExpressionTransform_2"];
4547
n3[label="CountingTransform_3"];
46-
n4[label="PlanSquashingTransform_4"];
47-
n5[label="DeduplicationToken::AddTokenInfoTransform_5"];
48-
n6[label="ApplySquashingTransform_9"];
49-
n7[label="ConvertingTransform_6"];
50-
n8[label="NestedElementsValidationTransform_7"];
51-
n9[label="MergeTreeSink_8"];
52-
n10[label="EmptySink_10"];
48+
n4[label="MaterializingTransform_4"];
49+
n5[label="PlanSquashingTransform_5"];
50+
n6[label="DeduplicationToken::AddTokenInfoTransform_6"];
51+
n7[label="ApplySquashingTransform_10"];
52+
n8[label="ConvertingTransform_7"];
53+
n9[label="NestedElementsValidationTransform_8"];
54+
n10[label="MergeTreeSink_9"];
55+
n11[label="EmptySink_11"];
5356
}
5457
n0 -> n1;
5558
n1 -> n2;
@@ -61,6 +64,7 @@ digraph
6164
n7 -> n8;
6265
n8 -> n9;
6366
n9 -> n10;
67+
n10 -> n11;
6468
}
6569
-- MergeTree pushes to Log, which does not support sparse columns - RemovingSparseTransform added
6670
create materialized view mv to t_log as select * from t_mt;
@@ -73,22 +77,23 @@ digraph
7377
n1[label="ExpressionTransform_1"];
7478
n2[label="ExpressionTransform_2"];
7579
n3[label="CountingTransform_3"];
76-
n4[label="PlanSquashingTransform_4"];
77-
n5[label="DeduplicationToken::AddTokenInfoTransform_5"];
78-
n6[label="ApplySquashingTransform_18"];
79-
n7[label="ConvertingTransform_6"];
80-
n8[label="NestedElementsValidationTransform_7"];
81-
n9[label="MergeTreeSink_8"];
82-
n10[label="Copy_16"];
83-
n11[label="BeginingViewsTransform_9"];
84-
n12[label="ExecutingInnerQueryFromView_12"];
85-
n13[label="CountingTransform_11"];
86-
n14[label="SquashingTransform_10"];
87-
n15[label="NestedElementsValidationTransform_13"];
88-
n16[label="RemovingSparseTransform_14"];
89-
n17[label="LogSink_15"];
90-
n18[label="FinalizingViewsTransform_17"];
91-
n19[label="EmptySink_19"];
80+
n4[label="MaterializingTransform_4"];
81+
n5[label="PlanSquashingTransform_5"];
82+
n6[label="DeduplicationToken::AddTokenInfoTransform_6"];
83+
n7[label="ApplySquashingTransform_19"];
84+
n8[label="ConvertingTransform_7"];
85+
n9[label="NestedElementsValidationTransform_8"];
86+
n10[label="MergeTreeSink_9"];
87+
n11[label="Copy_17"];
88+
n12[label="BeginingViewsTransform_10"];
89+
n13[label="ExecutingInnerQueryFromView_13"];
90+
n14[label="CountingTransform_12"];
91+
n15[label="SquashingTransform_11"];
92+
n16[label="NestedElementsValidationTransform_14"];
93+
n17[label="RemovingSparseTransform_15"];
94+
n18[label="LogSink_16"];
95+
n19[label="FinalizingViewsTransform_18"];
96+
n20[label="EmptySink_20"];
9297
}
9398
n0 -> n1;
9499
n1 -> n2;
@@ -109,6 +114,7 @@ digraph
109114
n16 -> n17;
110115
n17 -> n18;
111116
n18 -> n19;
117+
n19 -> n20;
112118
}
113119
drop table mv;
114120
-- Log does not support sparse columns - RemovingSparseTransform added
@@ -122,22 +128,23 @@ digraph
122128
n1[label="ExpressionTransform_1"];
123129
n2[label="ExpressionTransform_2"];
124130
n3[label="CountingTransform_3"];
125-
n4[label="PlanSquashingTransform_4"];
126-
n5[label="DeduplicationToken::AddTokenInfoTransform_5"];
127-
n6[label="ApplySquashingTransform_18"];
128-
n7[label="ConvertingTransform_6"];
129-
n8[label="NestedElementsValidationTransform_7"];
130-
n9[label="RemovingSparseTransform_8"];
131-
n10[label="LogSink_9"];
132-
n11[label="Copy_16"];
133-
n12[label="BeginingViewsTransform_10"];
134-
n13[label="ExecutingInnerQueryFromView_13"];
135-
n14[label="CountingTransform_12"];
136-
n15[label="SquashingTransform_11"];
137-
n16[label="NestedElementsValidationTransform_14"];
138-
n17[label="MergeTreeSink_15"];
139-
n18[label="FinalizingViewsTransform_17"];
140-
n19[label="EmptySink_19"];
131+
n4[label="MaterializingTransform_4"];
132+
n5[label="PlanSquashingTransform_5"];
133+
n6[label="DeduplicationToken::AddTokenInfoTransform_6"];
134+
n7[label="ApplySquashingTransform_19"];
135+
n8[label="ConvertingTransform_7"];
136+
n9[label="NestedElementsValidationTransform_8"];
137+
n10[label="RemovingSparseTransform_9"];
138+
n11[label="LogSink_10"];
139+
n12[label="Copy_17"];
140+
n13[label="BeginingViewsTransform_11"];
141+
n14[label="ExecutingInnerQueryFromView_14"];
142+
n15[label="CountingTransform_13"];
143+
n16[label="SquashingTransform_12"];
144+
n17[label="NestedElementsValidationTransform_15"];
145+
n18[label="MergeTreeSink_16"];
146+
n19[label="FinalizingViewsTransform_18"];
147+
n20[label="EmptySink_20"];
141148
}
142149
n0 -> n1;
143150
n1 -> n2;
@@ -158,4 +165,5 @@ digraph
158165
n16 -> n17;
159166
n17 -> n18;
160167
n18 -> n19;
168+
n19 -> n20;
161169
}

tests/queries/0_stateless/03601_insert_squashing_remove_const.reference

Whitespace-only changes.
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
DROP TABLE IF EXISTS tbl_x;
2+
3+
CREATE TABLE tbl_x (col2 String) ENGINE = Memory;
4+
5+
-- Produce Const and non-Const block in various SELECTs that may lead to UB w/o removing constness while squashing
6+
INSERT INTO tbl_x
7+
WITH
8+
c4 AS
9+
(
10+
SELECT 'aaa' AS col2
11+
UNION ALL
12+
SELECT 'bbb'
13+
),
14+
c6 AS
15+
(
16+
SELECT r.col2 AS col2
17+
FROM (SELECT 'ccc' AS col2) AS r
18+
LEFT JOIN (SELECT 'foo' AS col2) AS rt
19+
USING col2
20+
)
21+
SELECT
22+
*
23+
FROM
24+
(
25+
SELECT * FROM c4
26+
UNION ALL
27+
SELECT * FROM c6
28+
);

0 commit comments

Comments
 (0)