Skip to content

fix(query): Copy into CSV file support both CRLF and LF delimiter #18250

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

Merged
merged 2 commits into from
Jun 26, 2025
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
8 changes: 7 additions & 1 deletion src/query/formats/src/delimiter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,13 @@ impl TryFrom<&[u8]> for RecordDelimiter {
type Error = ErrorCode;
fn try_from(s: &[u8]) -> Result<Self> {
match s.len() {
1 => Ok(RecordDelimiter::Any(s[0])),
1 => {
if s.eq(b"\n") {
Ok(RecordDelimiter::Crlf)
} else {
Ok(RecordDelimiter::Any(s[0]))
}
}
2 if s.eq(b"\r\n") => Ok(RecordDelimiter::Crlf),
_ => Err(ErrorCode::InvalidArgument(format!(
"bad RecordDelimiter: '{:?}'",
Expand Down
2 changes: 2 additions & 0 deletions tests/data/csv/crlf_delimiter.csv
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
apple.com,2265cbcc3ef24106c9e0eddbd04f3cca0d03225da3fcca8e2d86f7a394af9283
google.com,d4c9d9027326271a89ce51fcaf328ed673f17be33469ff979e8ab8dd501e664f
2 changes: 2 additions & 0 deletions tests/data/csv/lf_delimiter.csv
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
apple.com,2265cbcc3ef24106c9e0eddbd04f3cca0d03225da3fcca8e2d86f7a394af9283
google.com,d4c9d9027326271a89ce51fcaf328ed673f17be33469ff979e8ab8dd501e664f
25 changes: 25 additions & 0 deletions tests/sqllogictests/suites/stage/formats/csv/csv_delimiter.test
Original file line number Diff line number Diff line change
Expand Up @@ -79,3 +79,28 @@ select * from tt;
----
abc xyz
"abc" xyz

statement ok
drop table if exists tt2

statement ok
create table tt2(site string not null, sha string not null)

query TIITI
copy into tt2 from @data/csv/crlf_delimiter.csv file_format = (type = CSV)
----
csv/crlf_delimiter.csv 2 0 NULL NULL

query TIITI
copy into tt2 from @data/csv/lf_delimiter.csv file_format = (type = CSV)
----
csv/lf_delimiter.csv 2 0 NULL NULL

query TTI
select *, length(sha) from tt2 order by site
----
apple.com 2265cbcc3ef24106c9e0eddbd04f3cca0d03225da3fcca8e2d86f7a394af9283 64
apple.com 2265cbcc3ef24106c9e0eddbd04f3cca0d03225da3fcca8e2d86f7a394af9283 64
google.com d4c9d9027326271a89ce51fcaf328ed673f17be33469ff979e8ab8dd501e664f 64
google.com d4c9d9027326271a89ce51fcaf328ed673f17be33469ff979e8ab8dd501e664f 64

Loading