Skip to content

SQL UNION ALL + JOIN column resolution fails #25748

@pomo-mondreganto

Description

@pomo-mondreganto

Checks

  • I have checked that this issue has not already been reported.
  • I have confirmed this bug exists on the latest version of Polars.

Reproducible example

use polars::sql::SQLContext;

fn main() {
    let mut ctx = SQLContext::new();

    let query = r#"
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
"#;

    println!("Executing SQL query:");
    println!("{query}");
    println!();

    match ctx.execute(query) {
        Ok(lf) => {
            println!("Query executed successfully!");
            let result = lf.collect().unwrap();
            println!("Result:\n{result}");
        }
        Err(e) => {
            println!("ERROR: {e}");
        }
    }
}

Log output

ERROR: not found: unable to find column "k:c"; valid columns: ["k"]

Resolved plan until failure:

        ---> FAILED HERE RESOLVING 'join' <---
SELECT [col("k")]
   WITH_COLUMNS:
   [dyn int: 0.alias("k")] 
    DF []; PROJECT */0 COLUMNS

Issue description

Seems like there's a problem in how polars SQL engine handles ambiguous column names in UNION ALL + JOIN contexts

Expected behavior

Should not fail, return 2 zeros

Installed versions

Details

polars = { git = "https://github.com/pola-rs/polars", rev = "40d7a8cf70d1294f98a9b717bb7340957cc80591", features = [
"sql",
] }

Metadata

Metadata

Labels

A-sqlArea: Polars SQL functionalityneeds triageAwaiting prioritization by a maintainerrustRelated to Rust Polars

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions