Skip to content

Commit 172e557

Browse files
authored
Minor: Simplify IdentTaker (#13609)
1 parent 68c92c0 commit 172e557

File tree

2 files changed

+31
-16
lines changed

2 files changed

+31
-16
lines changed

datafusion/sql/src/planner.rs

Lines changed: 30 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -622,21 +622,37 @@ pub fn object_name_to_table_reference(
622622
idents_to_table_reference(idents, enable_normalization)
623623
}
624624

625-
struct IdentTaker(Vec<Ident>);
625+
struct IdentTaker {
626+
normalizer: IdentNormalizer,
627+
idents: Vec<Ident>,
628+
}
629+
626630
/// Take the next identifier from the back of idents, panic'ing if
627631
/// there are none left
628632
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)
633+
fn new(idents: Vec<Ident>, enable_normalization: bool) -> Self {
634+
Self {
635+
normalizer: IdentNormalizer::new(enable_normalization),
636+
idents,
637+
}
638+
}
639+
640+
fn take(&mut self) -> String {
641+
let ident = self.idents.pop().expect("no more identifiers");
642+
self.normalizer.normalize(ident)
643+
}
644+
645+
/// Returns the number of remaining identifiers
646+
fn len(&self) -> usize {
647+
self.idents.len()
632648
}
633649
}
634650

635651
// impl Display for a nicer error message
636652
impl std::fmt::Display for IdentTaker {
637653
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
638654
let mut first = true;
639-
for ident in self.0.iter() {
655+
for ident in self.idents.iter() {
640656
if !first {
641657
write!(f, ".")?;
642658
}
@@ -653,29 +669,28 @@ pub(crate) fn idents_to_table_reference(
653669
idents: Vec<Ident>,
654670
enable_normalization: bool,
655671
) -> Result<TableReference> {
656-
let mut taker = IdentTaker(idents);
657-
let num_idents = taker.0.len();
672+
let mut taker = IdentTaker::new(idents, enable_normalization);
658673

659-
match num_idents {
674+
match taker.len() {
660675
1 => {
661-
let table = taker.take(enable_normalization);
676+
let table = taker.take();
662677
Ok(TableReference::bare(table))
663678
}
664679
2 => {
665-
let table = taker.take(enable_normalization);
666-
let schema = taker.take(enable_normalization);
680+
let table = taker.take();
681+
let schema = taker.take();
667682
Ok(TableReference::partial(schema, table))
668683
}
669684
3 => {
670-
let table = taker.take(enable_normalization);
671-
let schema = taker.take(enable_normalization);
672-
let catalog = taker.take(enable_normalization);
685+
let table = taker.take();
686+
let schema = taker.take();
687+
let catalog = taker.take();
673688
Ok(TableReference::full(catalog, schema, table))
674689
}
675690
_ => plan_err!(
676691
"Unsupported compound identifier '{}'. Expected 1, 2 or 3 parts, got {}",
677692
taker,
678-
num_idents
693+
taker.len()
679694
),
680695
}
681696
}

datafusion/sql/src/utils.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -259,7 +259,7 @@ pub(crate) fn make_decimal_type(
259259
}
260260
}
261261

262-
// Normalize an owned identifier to a lowercase string unless the identifier is quoted.
262+
/// Normalize an owned identifier to a lowercase string, unless the identifier is quoted.
263263
pub(crate) fn normalize_ident(id: Ident) -> String {
264264
match id.quote_style {
265265
Some(_) => id.value,

0 commit comments

Comments
 (0)