Skip to content

Commit 3087656

Browse files
committed
address issue in the review
1 parent 1786814 commit 3087656

2 files changed

Lines changed: 41 additions & 16 deletions

File tree

datafusion/sql/src/select.rs

Lines changed: 15 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -738,26 +738,25 @@ impl<S: ContextProvider> SqlToRel<'_, S> {
738738
for input in from {
739739
let current_name = extract_table_name(&input);
740740

741-
if let Some((ref name, ref span)) = current_name {
742-
alias_spans.entry(name.clone()).or_insert(*span);
741+
if let Some((ref name, current_span)) = current_name {
742+
if let Some(prior_span) = alias_spans.get(name) {
743+
let mut diagnostic = Diagnostic::new_error(
744+
"duplicate table alias in FROM clause",
745+
current_span,
746+
);
747+
if let Some(span) = *prior_span {
748+
diagnostic = diagnostic
749+
.with_note("first defined here", Some(span));
750+
}
751+
return plan_err!("duplicate table alias in FROM clause")
752+
.map_err(|e| e.with_diagnostic(diagnostic));
753+
}
754+
alias_spans.insert(name.clone(), current_span);
743755
}
744756

745757
let right = self.plan_table_with_joins(input, planner_context)?;
746758

747-
left = left.cross_join(right).map_err(|e| {
748-
if let Some((ref name, ref current_span)) = current_name
749-
&& let Some(prior_span) =
750-
alias_spans.get(name).copied().flatten()
751-
{
752-
let diagnostic = Diagnostic::new_error(
753-
"duplicate table alias in FROM clause",
754-
*current_span,
755-
)
756-
.with_note("first defined here", Some(prior_span));
757-
return e.with_diagnostic(diagnostic);
758-
}
759-
e
760-
})?;
759+
left = left.cross_join(right)?;
761760
let left_schema = Some(Arc::clone(left.schema()));
762761
planner_context.set_outer_from_schema(left_schema);
763762
}

datafusion/sql/tests/cases/diagnostic.rs

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -442,3 +442,29 @@ fn test_duplicate_bare_table_in_from() -> Result<()> {
442442
assert_eq!(diag.notes[0].span, Some(spans["a"]));
443443
Ok(())
444444
}
445+
446+
#[test]
447+
fn test_duplicate_alias_non_overlapping_columns() -> Result<()> {
448+
let query = "SELECT * FROM /*a*/j1 AS t/*a*/, /*b*/j2 AS t/*b*/";
449+
let spans = get_spans(query);
450+
let diag = do_query(query);
451+
assert_snapshot!(diag.message, @"duplicate table alias in FROM clause");
452+
assert_eq!(diag.span, Some(spans["b"]));
453+
assert_eq!(diag.notes.len(), 1);
454+
assert_snapshot!(diag.notes[0].message, @"first defined here");
455+
assert_eq!(diag.notes[0].span, Some(spans["a"]));
456+
Ok(())
457+
}
458+
459+
#[test]
460+
fn test_duplicate_alias_non_overlapping_three_tables() -> Result<()> {
461+
let query = "SELECT * FROM j1 AS x, /*a*/j2 AS t/*a*/, j3 AS y, /*b*/j1 AS t/*b*/";
462+
let spans = get_spans(query);
463+
let diag = do_query(query);
464+
assert_snapshot!(diag.message, @"duplicate table alias in FROM clause");
465+
assert_eq!(diag.span, Some(spans["b"]));
466+
assert_eq!(diag.notes.len(), 1);
467+
assert_snapshot!(diag.notes[0].message, @"first defined here");
468+
assert_eq!(diag.notes[0].span, Some(spans["a"]));
469+
Ok(())
470+
}

0 commit comments

Comments
 (0)