Skip to content

Commit fc3040b

Browse files
camilesingBohuTANG
andauthored
chore: improve error msg (#18885)
* chore: improve error msg * fix(ndjson): update error tests for new error message * chore: fmt ndjson block builder --------- Co-authored-by: Bohu <[email protected]>
1 parent c18cb33 commit fc3040b

File tree

2 files changed

+13
-12
lines changed

2 files changed

+13
-12
lines changed

src/query/storages/stage/src/read/row_based/formats/ndjson/block_builder.rs

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -48,9 +48,10 @@ impl NdJsonDecoder {
4848
buf: &[u8],
4949
columns: &mut [ColumnBuilder],
5050
null_if: &[&str],
51+
file_full_path: &str,
5152
) -> std::result::Result<(), FileParseError> {
5253
let mut json: serde_json::Value =
53-
serde_json::from_reader(buf).map_err(|e| map_json_error(e, buf))?;
54+
serde_json::from_reader(buf).map_err(|e| map_json_error(e, buf, file_full_path))?;
5455
// todo: this is temporary
5556
if self.field_decoder.is_select {
5657
self.field_decoder
@@ -168,7 +169,7 @@ impl RowDecoder for NdJsonDecoder {
168169
let row = row.trim();
169170
let row_id = batch.start_pos.rows + row_id;
170171
if !row.is_empty() {
171-
if let Err(e) = self.read_row(row, columns, &null_if) {
172+
if let Err(e) = self.read_row(row, columns, &null_if, &state.file_full_path) {
172173
self.load_context.error_handler.on_error(
173174
e.with_row(row_id),
174175
Some((columns, state.num_rows)),
@@ -185,12 +186,11 @@ impl RowDecoder for NdJsonDecoder {
185186
}
186187

187188
// The origin JSON error format "{} at line {} column {}" is misleading for NDJSON.
188-
// - rm `line {}`
189189
// - rename `column {}` to `pos {}`, 1-based to 0 based
190190
// - add info for size and next byte
191191
//
192192
// Use test in case of changes of serde_json.
193-
fn map_json_error(err: serde_json::Error, data: &[u8]) -> FileParseError {
193+
fn map_json_error(err: serde_json::Error, data: &[u8], file_full_path: &str) -> FileParseError {
194194
let pos = if err.column() > 0 {
195195
err.column() - 1
196196
} else {
@@ -199,10 +199,11 @@ fn map_json_error(err: serde_json::Error, data: &[u8]) -> FileParseError {
199199
let len = data.len();
200200

201201
let mut message = err.to_string();
202-
if let Some(p) = message.rfind(" at line") {
202+
if let Some(p) = message.rfind(" column") {
203203
message = message[..p].to_string()
204204
}
205-
message = format!("{message} at pos {pos} of size {len}");
205+
206+
message = format!("{message}, position {pos} of size {len} for File '{file_full_path}'");
206207
if err.column() < len {
207208
message = format!("{message}, next byte is '{}'", data[pos] as char)
208209
}
@@ -220,7 +221,7 @@ mod test {
220221
fn decode_err(data: &str) -> String {
221222
serde_json::from_slice::<serde_json::Value>(data.as_bytes())
222223
.map_err(|e| {
223-
let e = map_json_error(e, data.as_bytes());
224+
let e = map_json_error(e, data.as_bytes(), "mock_file");
224225
if let FileParseError::InvalidRow { message, .. } = e {
225226
message
226227
} else {
@@ -235,15 +236,15 @@ mod test {
235236
fn test_json_decode_error() {
236237
assert_eq!(
237238
decode_err("{").as_str(),
238-
"EOF while parsing an object at pos 0 of size 1"
239+
"EOF while parsing an object at line 1, position 0 of size 1 for File 'mock_file'"
239240
);
240241
assert_eq!(
241242
decode_err("").as_str(),
242-
"EOF while parsing a value at pos 0 of size 0"
243+
"EOF while parsing a value at line 1, position 0 of size 0 for File 'mock_file'"
243244
);
244245
assert_eq!(
245246
decode_err("{\"k\"-}").as_str(),
246-
"expected `:` at pos 4 of size 6, next byte is '-'"
247+
"expected `:` at line 1, position 4 of size 6 for File 'mock_file', next byte is '-'"
247248
);
248249
}
249250
}

tests/sqllogictests/suites/stage/formats/ndjson/ndjson_on_error.test

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,8 @@ CREATE TABLE wrong_ndjson (a Boolean, b Int, c Float, d String, e Date, f Timest
77
query
88
copy /*+ set_var(max_threads=1) */ into wrong_ndjson from @data/ndjson/ pattern = 'wrong_sample.*[.]ndjson' file_format = (type = NDJSON) ON_ERROR=continue
99
----
10-
ndjson/wrong_sample.ndjson 3 1 Invalid NDJSON row: key must be a string at pos 88 of size 114, next byte is 'h' 2
11-
ndjson/wrong_sample2.ndjson 3 1 Invalid NDJSON row: key must be a string at pos 88 of size 114, next byte is 'h' 2
10+
ndjson/wrong_sample.ndjson 3 1 Invalid NDJSON row: key must be a string at line 1, position 88 of size 114 for File 'ndjson/wrong_sample.ndjson', next byte is 'h' 2
11+
ndjson/wrong_sample2.ndjson 3 1 Invalid NDJSON row: key must be a string at line 1, position 88 of size 114 for File 'ndjson/wrong_sample2.ndjson', next byte is 'h' 2
1212

1313
query
1414
select * from wrong_ndjson order by a

0 commit comments

Comments
 (0)