Skip to content

Commit

Permalink
add test
Browse files Browse the repository at this point in the history
  • Loading branch information
Lordworms committed Feb 7, 2025
1 parent adfee55 commit 2a8aacf
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 29 deletions.
34 changes: 5 additions & 29 deletions datafusion/common/src/column.rs
Original file line number Diff line number Diff line change
Expand Up @@ -275,18 +275,11 @@ impl Column {
// user which columns are candidates, or which table
// they come from. For now, let's list the table names
// only.
for qualified_field in qualified_fields {
let (Some(table), _) = qualified_field else {
continue;
};
diagnostic.add_note(
format!(
"possible reference to '{}' in table '{}'",
&self.name, table
),
None,
);
}
add_possible_columns_to_diag(
&mut diagnostic,
&Column::new_unqualified(&self.name),
&columns,
);
err.with_diagnostic(diagnostic)
});
}
Expand All @@ -301,23 +294,6 @@ impl Column {
.flat_map(|s| s.columns())
.collect(),
})
.map_err(|e| match &e {
DataFusionError::SchemaError(
SchemaError::FieldNotFound {
field,
valid_fields,
},
_,
) => {
let mut diagnostic = Diagnostic::new_error(
format!("column '{}' not found", &field.name()),
field.spans().first(),
);
add_possible_columns_to_diag(&mut diagnostic, field, valid_fields);
e.with_diagnostic(diagnostic)
}
_ => e,
})
}

/// Returns a reference to the set of locations in the SQL query where this
Expand Down
29 changes: 29 additions & 0 deletions datafusion/sql/tests/cases/diagnostic.rs
Original file line number Diff line number Diff line change
Expand Up @@ -251,3 +251,32 @@ fn test_field_not_found_suggestion() -> Result<()> {
assert_eq!(suggested_fields[0], "person.first_name");
Ok(())
}

#[test]
fn test_ambiguous_column_suggestion() -> Result<()> {
let query = "SELECT /*whole*/id/*whole*/ FROM test_decimal, person";
let spans = get_spans(query);
let diag = do_query(query);

assert_eq!(diag.message, "column 'id' is ambiguous");
assert_eq!(diag.span, Some(spans["whole"]));

assert_eq!(diag.notes.len(), 2);

let mut suggested_fields: Vec<String> = diag
.notes
.iter()
.filter_map(|note| {
if note.message.starts_with("possible column") {
Some(note.message.replace("possible column ", ""))
} else {
None
}
})
.collect();

suggested_fields.sort();
assert_eq!(suggested_fields, vec!["person.id", "test_decimal.id"]);

Ok(())
}

0 comments on commit 2a8aacf

Please sign in to comment.