Skip to content

Commit 8733dc0

Browse files
committed
refactor error message and add note and help in diagnostic
1 parent 3310087 commit 8733dc0

File tree

2 files changed

+19
-10
lines changed

2 files changed

+19
-10
lines changed

datafusion/sql/src/expr/unary_op.rs

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -46,10 +46,15 @@ impl<S: ContextProvider> SqlToRel<'_, S> {
4646
Ok(operand)
4747
} else {
4848
plan_err!("Unary operator '+' only supports numeric, interval and timestamp types").map_err(|e| {
49-
let spans = operand.spans();
50-
let span = if let Some(s) = spans.as_ref() { s.first() } else { None };
51-
let diagnostic =
52-
Diagnostic::new_error("Unary operator '+' only supports numeric, interval and timestamp types".to_string(), span);
49+
let span = operand.spans().and_then(|s| s.first());
50+
let mut diagnostic = Diagnostic::new_error(
51+
format!("+ cannot be used with {data_type}"),
52+
span
53+
);
54+
if span.is_none() {
55+
diagnostic.add_note("+ can only be used with numbers, intervals, and timestamps", None);
56+
diagnostic.add_help(format!("perhaps you need to cast {operand}"), None);
57+
}
5358
e.with_diagnostic(diagnostic)
5459
})
5560
}

datafusion/sql/tests/cases/diagnostic.rs

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -292,10 +292,17 @@ fn test_unary_op_plus_with_non_column() -> Result<()> {
292292
// create a table with a column of type varchar
293293
let query = "SELECT /*whole*/+'a'/*whole*/ ";
294294
let diag = do_query(query);
295+
assert_eq!(diag.message, "+ cannot be used with Utf8");
295296
assert_eq!(
296-
diag.message,
297-
"Unary operator '+' only supports numeric, interval and timestamp types"
297+
diag.notes[0].message,
298+
"+ can only be used with numbers, intervals, and timestamps"
299+
);
300+
assert_eq!(diag.notes[0].span, None);
301+
assert_eq!(
302+
diag.helps[0].message,
303+
"perhaps you need to cast Utf8(\"a\")"
298304
);
305+
assert_eq!(diag.helps[0].span, None);
299306
assert_eq!(diag.span, None);
300307
Ok(())
301308
}
@@ -306,10 +313,7 @@ fn test_unary_op_plus_with_column() -> Result<()> {
306313
let query = "SELECT +/*whole*/first_name/*whole*/ FROM person";
307314
let spans = get_spans(query);
308315
let diag = do_query(query);
309-
assert_eq!(
310-
diag.message,
311-
"Unary operator '+' only supports numeric, interval and timestamp types"
312-
);
316+
assert_eq!(diag.message, "+ cannot be used with Utf8");
313317
assert_eq!(diag.span, Some(spans["whole"]));
314318
Ok(())
315319
}

0 commit comments

Comments
 (0)