martin-g commented on code in PR #20720:
URL: https://github.com/apache/datafusion/pull/20720#discussion_r2895368717
##########
datafusion/sql/src/select.rs:
##########
@@ -690,21 +694,70 @@ impl<S: ContextProvider> SqlToRel<'_, S> {
self.plan_table_with_joins(input, planner_context)
}
_ => {
+ let extract_table_name =
+ |t: &TableWithJoins| -> Option<(String, Option<Span>)> {
+ let span =
Span::try_from_sqlparser_span(t.relation.span());
+ match &t.relation {
+ TableFactor::Table { alias: Some(a), .. } => {
+ let name =
+
self.ident_normalizer.normalize(a.name.clone());
+ Some((name, span))
+ }
+ TableFactor::Table {
+ name, alias: None, ..
+ } => {
+ let table_name = name
+ .0
+ .iter()
+ .filter_map(|p| p.as_ident())
+ .map(|id|
self.ident_normalizer.normalize(id.clone()))
+ .next_back()?;
+ Some((table_name, span))
+ }
+ _ => None,
+ }
+ };
+
+ let mut alias_spans: HashMap<String, Option<Span>> =
HashMap::new();
+
let mut from = from.into_iter();
+ let first = from.next().unwrap();
+
+ if let Some((name, span)) = extract_table_name(&first) {
+ alias_spans.entry(name).or_insert(span);
+ }
+
+ let mut left = LogicalPlanBuilder::from(
+ self.plan_table_with_joins(first, planner_context)?,
+ );
- let mut left = LogicalPlanBuilder::from({
- let input = from.next().unwrap();
- self.plan_table_with_joins(input, planner_context)?
- });
let old_outer_from_schema = {
let left_schema = Some(Arc::clone(left.schema()));
planner_context.set_outer_from_schema(left_schema)
};
for input in from {
- // Join `input` with the current result (`left`).
+ let current_name = extract_table_name(&input);
+
+ if let Some((ref name, ref span)) = current_name {
+ alias_spans.entry(name.clone()).or_insert(*span);
Review Comment:
I think this should be done after the closure for map_err at line 747
because it clashes with `prior_span`.
I.e. first run the body of the closure, then insert this entry and finally
execute the cross join.
Something like:
```rust
if let Some((ref name, current_span)) = current_name {
if let Some(first_span) = alias_spans.get(name).copied() {
let diagnostic = Diagnostic::new_error(
"duplicate table alias in FROM clause",
current_span,
)
.with_note("first defined here", first_span);
return plan_err!("duplicate table alias in FROM clause")
.map_err(|e| e.with_diagnostic(diagnostic));
}
alias_spans.insert(name.clone(), current_span);
}
left = left.cross_join(right)?;
```
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]