Skip to content

Commit

Permalink
add unit test
Browse files Browse the repository at this point in the history
  • Loading branch information
dentiny committed Feb 17, 2025
1 parent 42f3235 commit 782da47
Show file tree
Hide file tree
Showing 4 changed files with 63 additions and 16 deletions.
7 changes: 7 additions & 0 deletions datafusion/common/src/spans.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,13 @@ pub struct Location {
pub column: u64,
}

impl Location {
/// Creates a new [`Location`] from its line and column information.
pub fn new(line: u64, column: u64) -> Self {
Self { line, column }
}
}

impl fmt::Debug for Location {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "Location({},{})", self.line, self.column)
Expand Down
17 changes: 9 additions & 8 deletions datafusion/expr/src/test/function_stub.rs
Original file line number Diff line number Diff line change
Expand Up @@ -164,14 +164,15 @@ impl AggregateUDFImpl for Sum {
dt if dt.is_signed_integer() => Ok(DataType::Int64),
dt if dt.is_unsigned_integer() => Ok(DataType::UInt64),
dt if dt.is_floating() => Ok(DataType::Float64),
_ => exec_err!("Sum not supported for {}", data_type)
.map_err(|err| {
let diagnostic = Diagnostic::new_error(
format!("Coercion only supports integer and floating point values"),
sum.spans().first(),
);
err.with_diagnostic(diagnostic)
}),
_ => exec_err!("Sum not supported for {}", data_type).map_err(|err| {
let diagnostic = Diagnostic::new_error(
format!(
"Coercion only supports integer and floating point values"
),
sum.spans().first(),
);
err.with_diagnostic(diagnostic)
}),
}
}

Expand Down
17 changes: 9 additions & 8 deletions datafusion/functions-aggregate/src/sum.rs
Original file line number Diff line number Diff line change
Expand Up @@ -166,14 +166,15 @@ impl AggregateUDFImpl for Sum {
dt if dt.is_signed_integer() => Ok(DataType::Int64),
dt if dt.is_unsigned_integer() => Ok(DataType::UInt64),
dt if dt.is_floating() => Ok(DataType::Float64),
_ => exec_err!("Sum not supported for {}", data_type)
.map_err(|err| {
let diagnostic = Diagnostic::new_error(
format!("Coercion only supports integer and floating point values"),
sum.spans().first(),
);
err.with_diagnostic(diagnostic)
}),
_ => exec_err!("Sum not supported for {}", data_type).map_err(|err| {
let diagnostic = Diagnostic::new_error(
format!(
"Coercion only supports integer and floating point values"
),
sum.spans().first(),
);
err.with_diagnostic(diagnostic)
}),
}
}

Expand Down
38 changes: 38 additions & 0 deletions datafusion/sql/tests/cases/diagnostic.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,12 @@

use std::collections::HashMap;

use arrow::datatypes::DataType;
use datafusion_common::diagnostic::DiagnosticKind;
use datafusion_common::DataFusionError;
use datafusion_common::{Diagnostic, Location, Result, Span};
use datafusion_expr::AggregateUDFImpl;
use datafusion_functions_aggregate::sum::Sum;
use datafusion_sql::planner::{ParserOptions, SqlToRel};
use regex::Regex;
use sqlparser::{dialect::GenericDialect, parser::Parser};
Expand Down Expand Up @@ -274,3 +279,36 @@ fn test_ambiguous_column_suggestion() -> Result<()> {

Ok(())
}

#[test]
fn test_sum_coerce_type() -> Result<()> {
let mut sum = Sum::new();
let span = Span::new(Location::new(1, 1), Location::new(2, 1));
let span_for_test = span;
sum.spans_mut().add_span(span);
let datatype_vec = vec![DataType::Date64];
let res = sum.coerce_types(&datatype_vec);
let err = res.expect_err("Expected an error, but got success");

match err {
DataFusionError::Diagnostic(ref diagnostic, _) => {
assert_eq!(diagnostic.kind, DiagnosticKind::Error);
assert_eq!(
diagnostic.message,
"Coercion only supports integer and floating point values"
);
assert_eq!(diagnostic.span, Some(span_for_test));
assert!(diagnostic.notes.is_empty());
assert!(diagnostic.helps.is_empty());
}
_ => {
assert!(
false,
"Expected a Diagnostic error, but got another type: {:?}",
err
);
}
}

Ok(())
}

0 comments on commit 782da47

Please sign in to comment.