Skip to content

feat: attach Diagnostic to duplicate table name errors - #26202

Open
haochunchang wants to merge 1 commit into
apache:mainfrom
haochunchang:worktree-diag-duplicate-table-name
Open

haochunchang wants to merge 1 commit into
apache:mainfrom
haochunchang:worktree-diag-duplicate-table-name

Conversation

@haochunchang

@haochunchang haochunchang commented Oct 11, 2026 •

Copy link
Copy Markdown

Which issue does this PR close?

Continues #20720 by @buraksenn, rebased onto current main.

Rationale for this change

If a query uses the same name twice in one FROM clause, DataFusion rejects it, but the error is unhelpful in two ways:

  1. No location. The error doesn't point at where in the SQL the repeated name appears.
  2. For repeated table names or aliases, DataFusion only notices after it merges the columns, so the error blames a duplicate column instead of saying the table or alias was used twice.

What changes are included in this PR?

  1. Duplicate CTE names: attach a Diagnostic at the repeated name, with a note at the first definition.
  2. Duplicate relation names in a FROM scope (comma-separated entries, JOINs, parenthesized joins):
    • Each one adds a non-fatal warning Diagnostic. There is exactly 1 warning per duplicate.
    • If planning fails with DuplicateQualifiedField for that name, the error gets a Diagnostic pointing at both relations. The error message text is unchanged.
    • Whether a query plans or fails is unchanged.
  3. Names are compared as TableReferences that match the planned qualifiers.
    • A quoted alias "s.t" does not match s.t, and an unaliased table function foo(..) (qualifier foo()) does not match a table foo. (datafusion/sqllogictest/test_files/join.slt.part)
    • Names the check can't resolve are skipped.

What is the testing strategy for this PR?

  • datafusion/sql/tests/cases/diagnostic.rs:
    • test_duplicate_cte_name: error Diagnostic spans for duplicate CTEs.
    • test_duplicate_table_name: for each FROM shape, checks that there is 1 warning with the right spans:
      • Queries with overlapping columns fail with the original message plus an error Diagnostic.
      • Queries with disjoint columns and LEFT SEMI joins plan successfully.
    • test_duplicate_table_name_single_warning_in_multi_entry_from: 1 warning, not 2, when a duplicate spans comma-separated entries.
    • test_same_table_name_in_different_scopes: no warning across scopes.
  • datafusion/sqllogictest/test_files/join.slt.part: New cases cover a quoted dotted alias vs a schema-qualified table, and a table function vs a same-named table.

Are there any user-facing changes?

  • New warning Diagnostics for duplicate relation names. Callers read them with SqlToRel::take_warnings.
  • An error Diagnostic is now attached to duplicate CTE and duplicate qualified field errors.
  • No breaking changes: the same queries plan or fail as before, with the same error messages.
    • In particular, duplicate aliases that previously planned (e.g. FROM j1 AS t, j2 AS t with disjoint columns) still plan, now with a warning.

🤖 Generated with Claude Code and manually edited by @haochunchang .

@github-actions github-actions Bot added sql SQL Planner sqllogictest SQL Logic Tests (.slt) labels Oct 11, 2026
@haochunchang
haochunchang force-pushed the worktree-diag-duplicate-table-name branch from dde7d4d to e1b368b Compare October 11, 2026 08:45
@haochunchang
haochunchang marked this pull request as ready for review October 11, 2026 10:19
Closes apache#14436. Continues apache#20720 by @buraksenn, rebased onto current main.

- Duplicate CTE names: attach a Diagnostic pointing at the repeated name,
  with a note at the first definition. CTE spans are stored in
  PlannerContext; the public insert_cte/get_cte API is unchanged.
- Duplicate relation names in a FROM scope (comma-separated entries,
  explicit JOINs, and inside parenthesized joins) add a non-fatal warning
  Diagnostic, `table name "t" specified more than once`, once per
  duplicate. If planning then fails with DuplicateQualifiedField for that
  name, an error Diagnostic pointing at both relations is attached. Whether
  a query plans or fails, and the error message text, are unchanged.
- Names are compared as TableReferences that match the planned
  qualifiers, so schema1.person and schema2.person are distinct, a quoted
  alias "s.t" does not match s.t, and an unaliased table function foo(..)
  (qualifier foo()) does not match a table foo. Names the check cannot
  resolve are skipped and left to normal planning.

Co-authored-by: buraksenn <buraksenb@gmail.com>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@haochunchang
haochunchang force-pushed the worktree-diag-duplicate-table-name branch from e1b368b to e3f6375 Compare October 12, 2026 00:10
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

sql SQL Planner sqllogictest SQL Logic Tests (.slt)

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Attach Diagnostic to "duplicate table name" error

1 participant