Skip to content

Commit 861764d

Browse files
committed
chore: attach diagnostic to unary_op PLUS
1 parent d3967ae commit 861764d

File tree

2 files changed

+36
-2
lines changed

2 files changed

+36
-2
lines changed

datafusion/sql/src/expr/unary_op.rs

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
// under the License.
1717

1818
use crate::planner::{ContextProvider, PlannerContext, SqlToRel};
19-
use datafusion_common::{not_impl_err, plan_err, DFSchema, Result};
19+
use datafusion_common::{not_impl_err, plan_err, DFSchema, Diagnostic, Result};
2020
use datafusion_expr::{
2121
type_coercion::{is_interval, is_timestamp},
2222
Expr, ExprSchemable,
@@ -45,7 +45,13 @@ impl<S: ContextProvider> SqlToRel<'_, S> {
4545
{
4646
Ok(operand)
4747
} else {
48-
plan_err!("Unary operator '+' only supports numeric, interval and timestamp types")
48+
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(format!("Unary operator '+' only supports numeric, interval and timestamp types"), span);
53+
e.with_diagnostic(diagnostic)
54+
})
4955
}
5056
}
5157
UnaryOperator::Minus => {

datafusion/sql/tests/cases/diagnostic.rs

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -286,3 +286,31 @@ fn test_invalid_function() -> Result<()> {
286286
assert_eq!(diag.span, Some(spans["whole"]));
287287
Ok(())
288288
}
289+
290+
#[test]
291+
fn test_unary_op_plus_with_non_column() -> Result<()> {
292+
// create a table with a column of type varchar
293+
let query = "SELECT /*whole*/+'a'/*whole*/ ";
294+
let spans = get_spans(query);
295+
let diag = do_query(query);
296+
assert_eq!(
297+
diag.message,
298+
"Unary operator '+' only supports numeric, interval and timestamp types"
299+
);
300+
assert_eq!(diag.span, None);
301+
Ok(())
302+
}
303+
304+
#[test]
305+
fn test_unary_op_plus_with_column() -> Result<()> {
306+
// Test with a direct query that references a column with an incompatible type
307+
let query = "SELECT +/*whole*/first_name/*whole*/ FROM person";
308+
let spans = get_spans(query);
309+
let diag = do_query(query);
310+
assert_eq!(
311+
diag.message,
312+
"Unary operator '+' only supports numeric, interval and timestamp types"
313+
);
314+
assert_eq!(diag.span, Some(spans["whole"]));
315+
Ok(())
316+
}

0 commit comments

Comments
 (0)