Skip to content

Commit

Permalink
fix: insert long sql
Browse files Browse the repository at this point in the history
  • Loading branch information
sundy-li committed Oct 24, 2024
1 parent a6be8e7 commit 79d78bf
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 8 deletions.
17 changes: 9 additions & 8 deletions src/query/sql/src/planner/planner.rs
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,7 @@ impl Planner {
};

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

self.replace_stmt(&mut stmt)?;

Ok(PlanExtras {
PlanExtras {
format,
statement: stmt,
})
}
};

let mut maybe_partial_insert = false;
let mut insert_values_stmt = false;
if is_insert_or_replace_stmt && matches!(tokenizer.peek(), Some(Ok(_))) {
if let Ok(PlanExtras {
statement:
Statement::Insert(InsertStmt {
source: InsertSource::Select { .. },
source: InsertSource::RawValues { .. },
..
}),
..
}) = &res
{
maybe_partial_insert = true;
insert_values_stmt = true;
}
}

if maybe_partial_insert || (res.is_err() && matches!(tokenizer.peek(), Some(Ok(_)))) {
if insert_values_stmt || (res.is_err() && matches!(tokenizer.peek(), Some(Ok(_)))) {
// Remove the EOI.
tokens.pop();
// Tokenize more and try again.
if tokens.len() < PROBE_INSERT_MAX_TOKENS {
if !insert_values_stmt && tokens.len() < PROBE_INSERT_MAX_TOKENS {
let iter = (&mut tokenizer)
.take(tokens.len() * 2)
.take_while(|token| token.is_ok())
Expand All @@ -204,6 +204,7 @@ impl Planner {
.chain(std::iter::once(Token::new_eoi(&final_sql)));
tokens.extend(iter);
} else {
// Take the whole tokenizer
let iter = (&mut tokenizer)
.take_while(|token| token.is_ok())
.map(|token| token.unwrap())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,5 +31,21 @@ SELECT id, s FROM t1 order by id
3 aaa
4 aaa

statement error 1025
INSERT INTO table_a_temp_data_storage
SELECT col_1,COALESCE(b2.col_2,'other') AS col_3,col_4,col_5,col_6,col_7 FROM
(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
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'

statement error 1003
INSERT INTO xxx.table_1
SELECT a, COALESCE(b.c, '其他') AS d, e, f, g, h FROM
(SELECT i.j AS a, i.k, i.l AS e, 'xx' as f, i.m AS g, 'a.table_2' AS h
FROM a.table_2 i
LEFT JOIN table_3 n ON n.o = 'yy' AND n.p = 'a.table_2'
LEFT JOIN table_4 q ON 1=1
WHERE i.m >= n.r AND i.m < q.s) t
LEFT JOIN table_5 b ON t.k = b.u AND b.v = 'a.table_2' AND b.w = 'k'

statement ok
DROP DATABASE db_values_comment

0 comments on commit 79d78bf

Please sign in to comment.