Skip to content

Commit

Permalink
fix: FieldNotFound error message without valid fields
Browse files Browse the repository at this point in the history
  • Loading branch information
DDtKey committed Jan 18, 2023
1 parent 8ab3a91 commit 769fbab
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 17 deletions.
10 changes: 4 additions & 6 deletions datafusion/common/src/column.rs
Original file line number Diff line number Diff line change
Expand Up @@ -148,12 +148,10 @@ impl Column {

Err(DataFusionError::SchemaError(SchemaError::FieldNotFound {
field: Column::new(self.relation.clone(), self.name),
valid_fields: Some(
schemas
.iter()
.flat_map(|s| s.fields().iter().map(|f| f.qualified_column()))
.collect(),
),
valid_fields: schemas
.iter()
.flat_map(|s| s.fields().iter().map(|f| f.qualified_column()))
.collect(),
}))
}
}
Expand Down
15 changes: 15 additions & 0 deletions datafusion/common/src/dfschema.rs
Original file line number Diff line number Diff line change
Expand Up @@ -825,6 +825,21 @@ mod tests {
Ok(())
}

#[test]
fn select_without_valid_fields() {
let schema = DFSchema::empty();

let err = schema
.index_of_column_by_name(Some("t1"), "c0")
.err()
.unwrap();
assert_eq!("Schema error: No field named 't1'.'c0'.", &format!("{err}"));

// the same check without qualifier
let err = schema.index_of_column_by_name(None, "c0").err().unwrap();
assert_eq!("Schema error: No field named 'c0'.", &format!("{err}"));
}

#[test]
fn equivalent_names_and_types() {
let field1_i16_t = DFField::from(Field::new("f1", DataType::Int16, true));
Expand Down
18 changes: 8 additions & 10 deletions datafusion/common/src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,7 @@ pub enum SchemaError {
/// No field with this name
FieldNotFound {
field: Column,
valid_fields: Option<Vec<Column>>,
valid_fields: Vec<Column>,
},
}

Expand All @@ -137,13 +137,11 @@ pub fn field_not_found(
) -> DataFusionError {
DataFusionError::SchemaError(SchemaError::FieldNotFound {
field: Column::new(qualifier, name),
valid_fields: Some(
schema
.fields()
.iter()
.map(|f| f.qualified_column())
.collect(),
),
valid_fields: schema
.fields()
.iter()
.map(|f| f.qualified_column())
.collect(),
})
}

Expand All @@ -160,11 +158,11 @@ impl Display for SchemaError {
} else {
write!(f, "'{}'", field.name)?;
}
if let Some(fields) = valid_fields {
if !valid_fields.is_empty() {
write!(
f,
". Valid fields are {}",
fields
valid_fields
.iter()
.map(|field| {
if let Some(q) = &field.relation {
Expand Down
2 changes: 1 addition & 1 deletion datafusion/sql/tests/integration_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3085,7 +3085,7 @@ fn test_prepare_statement_to_plan_panic_prepare_wrong_syntax() {

#[test]
#[should_panic(
expected = "value: SchemaError(FieldNotFound { field: Column { relation: None, name: \"id\" }, valid_fields: Some([]) })"
expected = "value: SchemaError(FieldNotFound { field: Column { relation: None, name: \"id\" }, valid_fields: [] })"
)]
fn test_prepare_statement_to_plan_panic_no_relation_and_constant_param() {
let sql = "PREPARE my_plan(INT) AS SELECT id + $1";
Expand Down

0 comments on commit 769fbab

Please sign in to comment.