Skip to content

Conversation

@alexander-beedie
Copy link
Collaborator

@alexander-beedie alexander-beedie commented Dec 12, 2025

Closes #25748.

  • The query in the linked Issue failing is expected, but the error message raised was not very helpful - this PR hugely improves the usefulness of the error message, and adds improves detection of ambiguous columns in JOIN clauses.

  • In addition, reformulating the query in the linked Issue showed a different possible error; this was fixed by ensuring that the query each side of the UNION, or any other multi-frame set operation, uses execute_isolated (ref: fix: Solve multiple issues relating to arena mutation in SQL subqueries #25110) to prevent state leakage between the two queries (in this case, join aliases).

  • Also, moved all the SQLVisitor helper implementations into their own location; am planning a big refactor of the polars-sql crate (which isn't that well structured at the moment), but will save that for its own PR later.

Example

import polars as pl
pl.sql("""
    WITH a AS (
        SELECT 0 AS k
    ), b AS (
        SELECT 0 AS k
    ), c AS (
        SELECT 0 AS k
    )
    SELECT k FROM a JOIN c on a.k = c.k
    UNION ALL
    SELECT k FROM b JOIN c on b.k = c.k
""").collect()

Before:

# ColumnNotFoundError: unable to find column "k:c"; valid columns: ["k"]

After:

# SQLInterfaceError: ambiguous reference to column "k" (use one of: a.k, c.k)

@github-actions github-actions bot added A-sql Area: Polars SQL functionality enhancement New feature or an improvement of an existing feature python Related to Python Polars rust Related to Rust Polars labels Dec 12, 2025
@alexander-beedie
Copy link
Collaborator Author

alexander-beedie commented Dec 12, 2025

I see the new error-check actually found an ambiguous join column in the user guide; will fix that example when I get back home tonight 👍

Update: done.

@codecov
Copy link

codecov bot commented Dec 12, 2025

Codecov Report

❌ Patch coverage is 93.60465% with 11 lines in your changes missing coverage. Please review.
✅ Project coverage is 80.65%. Comparing base (20a1598) to head (6af6840).

Files with missing lines Patch % Lines
crates/polars-sql/src/sql_visitors.rs 93.26% 7 Missing ⚠️
crates/polars-sql/src/context.rs 94.11% 4 Missing ⚠️
Additional details and impacted files
@@           Coverage Diff            @@
##             main   #25761    +/-   ##
========================================
  Coverage   80.64%   80.65%            
========================================
  Files        1765     1766     +1     
  Lines      243051   243163   +112     
  Branches     3044     3044            
========================================
+ Hits       196014   196113    +99     
- Misses      46255    46268    +13     
  Partials      782      782            

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

nameexhaustion
nameexhaustion previously approved these changes Jan 7, 2026
@alexander-beedie
Copy link
Collaborator Author

alexander-beedie commented Jan 7, 2026

@nameexhaustion - rebasing now 👌
Looks like some clippy issues need addressing.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

A-sql Area: Polars SQL functionality enhancement New feature or an improvement of an existing feature python Related to Python Polars rust Related to Rust Polars

Projects

None yet

Development

Successfully merging this pull request may close these issues.

SQL UNION ALL + JOIN column resolution fails

2 participants