Skip to content

Commit

Permalink
fix(cli): check parse sql token error (#573)
Browse files Browse the repository at this point in the history
  • Loading branch information
b41sh authored Jan 21, 2025
1 parent 67f99af commit 53b410e
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 10 deletions.
29 changes: 19 additions & 10 deletions cli/src/session.rs
Original file line number Diff line number Diff line change
Expand Up @@ -453,17 +453,26 @@ impl Session {
self.query.push_str(line);

'Parser: loop {
let mut tokenizer = Tokenizer::new(&self.query);

while let Some(Ok(token)) = tokenizer.next() {
if let TokenKind::SemiColon = token.kind {
// push to current and continue the tokenizer
let (sql, remain) = self.query.split_at(token.span.end as usize);
if !sql.is_empty() {
queries.push(sql.to_string());
let mut is_valid = true;
let tokenizer = Tokenizer::new(&self.query);
for token in tokenizer {
match token {
Ok(token) => {
if let TokenKind::SemiColon = token.kind {
// push to current and continue the tokenizer
let (sql, remain) = self.query.split_at(token.span.end as usize);
if is_valid && !sql.is_empty() {
queries.push(sql.to_string());
}
self.query = remain.to_string();
continue 'Parser;
}
}
Err(_) => {
// ignore current query if have invalid token.
is_valid = false;
continue;
}
self.query = remain.to_string();
continue 'Parser;
}
}
break;
Expand Down
3 changes: 3 additions & 0 deletions cli/tests/00-base.sql
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,9 @@ insert into t values(null);
select * from t;
drop table t;

-- issue 572
CREATE TABLE 🐳🍞(🐳🐳 INTEGER, 🍞🍞 INTEGER);

-- enable it after we support code string in databend
-- select $$aa$$;
-- select $$
Expand Down

0 comments on commit 53b410e

Please sign in to comment.