Skip to content

Commit de452f1

Browse files
committed
Improve unsupported compound identifier message
1 parent 6512437 commit de452f1

File tree

2 files changed

+34
-13
lines changed

2 files changed

+34
-13
lines changed

datafusion/sql/src/planner.rs

Lines changed: 33 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -622,24 +622,41 @@ pub fn object_name_to_table_reference(
622622
idents_to_table_reference(idents, enable_normalization)
623623
}
624624

625+
struct IdentTaker(Vec<Ident>);
626+
/// Take the next identifier from the back of idents, panic'ing if
627+
/// there are none left
628+
impl IdentTaker {
629+
fn take(&mut self, enable_normalization: bool) -> String {
630+
let ident = self.0.pop().expect("no more identifiers");
631+
IdentNormalizer::new(enable_normalization).normalize(ident)
632+
}
633+
}
634+
635+
// impl Display for a nicer error message
636+
impl std::fmt::Display for IdentTaker {
637+
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
638+
let mut first = true;
639+
for ident in self.0.iter() {
640+
if !first {
641+
write!(f, ".")?;
642+
}
643+
write!(f, "{}", ident)?;
644+
first = false;
645+
}
646+
647+
Ok(())
648+
}
649+
}
650+
625651
/// Create a [`TableReference`] after normalizing the specified identifier
626652
pub(crate) fn idents_to_table_reference(
627653
idents: Vec<Ident>,
628654
enable_normalization: bool,
629655
) -> Result<TableReference> {
630-
struct IdentTaker(Vec<Ident>);
631-
/// Take the next identifier from the back of idents, panic'ing if
632-
/// there are none left
633-
impl IdentTaker {
634-
fn take(&mut self, enable_normalization: bool) -> String {
635-
let ident = self.0.pop().expect("no more identifiers");
636-
IdentNormalizer::new(enable_normalization).normalize(ident)
637-
}
638-
}
639-
640656
let mut taker = IdentTaker(idents);
657+
let num_idents = taker.0.len();
641658

642-
match taker.0.len() {
659+
match num_idents {
643660
1 => {
644661
let table = taker.take(enable_normalization);
645662
Ok(TableReference::bare(table))
@@ -655,7 +672,11 @@ pub(crate) fn idents_to_table_reference(
655672
let catalog = taker.take(enable_normalization);
656673
Ok(TableReference::full(catalog, schema, table))
657674
}
658-
_ => plan_err!("Unsupported compound identifier '{:?}'", taker.0),
675+
_ => plan_err!(
676+
"Unsupported compound identifier '{}'. Expected 1, 2 or 3 parts, got {}",
677+
taker,
678+
num_idents
679+
),
659680
}
660681
}
661682

datafusion/sqllogictest/test_files/errors.slt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ SELECT COUNT(*) FROM nonexistentschema.aggregate_test_100
7070
statement error Error during planning: table 'nonexistentcatalog\.public\.aggregate_test_100' not found
7171
SELECT COUNT(*) FROM nonexistentcatalog.public.aggregate_test_100
7272

73-
statement error Error during planning: Unsupported compound identifier '\[Ident \{ value: "way", quote_style: None \}, Ident \{ value: "too", quote_style: None \}, Ident \{ value: "many", quote_style: None \}, Ident \{ value: "namespaces", quote_style: None \}, Ident \{ value: "as", quote_style: None \}, Ident \{ value: "ident", quote_style: None \}, Ident \{ value: "prefixes", quote_style: None \}, Ident \{ value: "aggregate_test_100", quote_style: None \}\]'
73+
statement error DataFusion error: Error during planning: Unsupported compound identifier 'way\.too\.many\.namespaces\.as\.ident\.prefixes\.aggregate_test_100'\. Expected 1, 2 or 3 parts, got 8
7474
SELECT COUNT(*) FROM way.too.many.namespaces.as.ident.prefixes.aggregate_test_100
7575

7676

0 commit comments

Comments
 (0)