Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feature(query): fix read quoted string #5870

Merged
merged 8 commits into from
Jun 9, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 36 additions & 2 deletions common/io/src/buffer/buffer_read_ext.rs
Original file line number Diff line number Diff line change
Expand Up @@ -46,8 +46,42 @@ pub trait BufferReadExt: BufferRead {

fn read_quoted_text(&mut self, buf: &mut Vec<u8>, quota: u8) -> Result<()> {
self.must_ignore_byte(quota)?;
self.keep_read(buf, |b| b != quota)?;
self.must_ignore_byte(quota)

loop {
Copy link
Member

@zhang2014 zhang2014 Jun 9, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

CSV format also need this. We can implement it in other pr

self.keep_read(buf, |b| b != quota && b != b'\\')?;
if self.ignore_byte(quota)? {
return Ok(());
} else if self.ignore_byte(b'\\')? {
let b = self.fill_buf()?;
if b.is_empty() {
return Err(std::io::Error::new(
ErrorKind::InvalidData,
"Expected to have terminated string literal.".to_string(),
));
}
let c = b[0];
self.ignore_byte(c)?;
match c {
b'n' => buf.push(b'\n'),
b't' => buf.push(b'\t'),
b'r' => buf.push(b'\r'),
b'0' => buf.push(b'\0'),
b'\'' => buf.push(b'\''),
b'\\' => buf.push(b'\\'),
b'\"' => buf.push(b'\"'),
_ => {
buf.push(b'\\');
buf.push(c);
}
}
} else {
break;
}
}
Err(std::io::Error::new(
ErrorKind::InvalidData,
"Expected to have terminated string literal.".to_string(),
))
}

fn read_escaped_string_text(&mut self, buf: &mut Vec<u8>) -> Result<()> {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,8 @@ SELECT * FROM t1;

---- http
1 1 1
2 2 \"2\"-\"2\"
2 2 "2"-"2"


statement ok
CREATE TABLE IF NOT EXISTS t2(a Boolean, b Timestamp, c Date) Engine = fuse;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
-1 33 2021-08-15 10:00:00.000000 string1234
101 67 2021-11-15 10:00:00.000000 string5678
100 100
a"b"c'd
a"b"c\\'d
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,11 @@ select * from t1;
select sum(a),sum(b) from t1;


CREATE TABLE IF NOT EXISTS t_str(a Varchar);
INSERT INTO t_str(a) values( 'a"b\"c\'d');
INSERT INTO t_str(a) values( 'a"b\"c\\\'d');
select * from t_str order by a;

drop table t_str;

DROP DATABASE db1;