Skip to content

Commit 9a56324

Browse files
authored
fix(query): insert with long sql parse error (#16678)
fix: insert long sql
1 parent d1ad6f4 commit 9a56324

File tree

2 files changed

+25
-8
lines changed

2 files changed

+25
-8
lines changed

src/query/sql/src/planner/planner.rs

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -152,7 +152,7 @@ impl Planner {
152152
};
153153

154154
loop {
155-
let res = {
155+
let res = try {
156156
// Step 2: Parse the SQL.
157157
let (mut stmt, format) = if is_insert_stmt {
158158
(parse_raw_insert_stmt(&tokens, sql_dialect)?, None)
@@ -170,32 +170,32 @@ impl Planner {
170170

171171
self.replace_stmt(&mut stmt)?;
172172

173-
Ok(PlanExtras {
173+
PlanExtras {
174174
format,
175175
statement: stmt,
176-
})
176+
}
177177
};
178178

179-
let mut maybe_partial_insert = false;
179+
let mut insert_values_stmt = false;
180180
if is_insert_or_replace_stmt && matches!(tokenizer.peek(), Some(Ok(_))) {
181181
if let Ok(PlanExtras {
182182
statement:
183183
Statement::Insert(InsertStmt {
184-
source: InsertSource::Select { .. },
184+
source: InsertSource::RawValues { .. },
185185
..
186186
}),
187187
..
188188
}) = &res
189189
{
190-
maybe_partial_insert = true;
190+
insert_values_stmt = true;
191191
}
192192
}
193193

194-
if maybe_partial_insert || (res.is_err() && matches!(tokenizer.peek(), Some(Ok(_)))) {
194+
if insert_values_stmt || (res.is_err() && matches!(tokenizer.peek(), Some(Ok(_)))) {
195195
// Remove the EOI.
196196
tokens.pop();
197197
// Tokenize more and try again.
198-
if tokens.len() < PROBE_INSERT_MAX_TOKENS {
198+
if !insert_values_stmt && tokens.len() < PROBE_INSERT_MAX_TOKENS {
199199
let iter = (&mut tokenizer)
200200
.take(tokens.len() * 2)
201201
.take_while(|token| token.is_ok())
@@ -204,6 +204,7 @@ impl Planner {
204204
.chain(std::iter::once(Token::new_eoi(&final_sql)));
205205
tokens.extend(iter);
206206
} else {
207+
// Take the whole tokenizer
207208
let iter = (&mut tokenizer)
208209
.take_while(|token| token.is_ok())
209210
.map(|token| token.unwrap())

tests/sqllogictests/suites/base/03_common/03_0045_insert_values_with_comment.test

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,5 +31,21 @@ SELECT id, s FROM t1 order by id
3131
3 aaa
3232
4 aaa
3333

34+
statement error 1025
35+
INSERT INTO table_a_temp_data_storage
36+
SELECT col_1,COALESCE(b2.col_2,'other') AS col_3,col_4,col_5,col_6,col_7 FROM
37+
(SELECT a.col_8 AS col_1,a.col_9,a.col_10 AS col_4,'CODE_001' as col_5, a.col_11 AS col_6,'table_b' AS col_7 FROM schema_x.table_b a left join schema_x.table_c b on b.col_12 = 'TABLE_D' and b.col_13 = 'table_b' left join table_e c on 1=1 where a.col_11 >= b.col_14 AND a.col_11 < c.col_15) b1
38+
left join schema_x.table_f b2 ON b1.col_9 = b2.col_16 and b2.col_17 = 'table_b' and b2.col_18 = 'col_9'
39+
40+
statement error 1003
41+
INSERT INTO xxx.table_1
42+
SELECT a, COALESCE(b.c, '其他') AS d, e, f, g, h FROM
43+
(SELECT i.j AS a, i.k, i.l AS e, 'xx' as f, i.m AS g, 'a.table_2' AS h
44+
FROM a.table_2 i
45+
LEFT JOIN table_3 n ON n.o = 'yy' AND n.p = 'a.table_2'
46+
LEFT JOIN table_4 q ON 1=1
47+
WHERE i.m >= n.r AND i.m < q.s) t
48+
LEFT JOIN table_5 b ON t.k = b.u AND b.v = 'a.table_2' AND b.w = 'k'
49+
3450
statement ok
3551
DROP DATABASE db_values_comment

0 commit comments

Comments
 (0)