Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

MATERIALIZED causes Binder Error: table has duplicate column name #10260

Closed
1 task done
tanner-lai opened this issue Jan 17, 2024 · 4 comments
Closed
1 task done

MATERIALIZED causes Binder Error: table has duplicate column name #10260

tanner-lai opened this issue Jan 17, 2024 · 4 comments

Comments

@tanner-lai
Copy link

tanner-lai commented Jan 17, 2024

What happens?

An unexpected error occurs when a CTE is MATERIALIZED: Error: Binder Error: table "CTE" has duplicate column name "A1"

To Reproduce

CREATE TABLE T0(C1 INT);
CREATE TABLE T1(C1 INT);

INSERT INTO T0(C1) VALUES (1);
INSERT INTO T1(C1) VALUES (1);
-- no problem
WITH CTE AS (
SELECT A1, * FROM T0
  LEFT JOIN (
    SELECT C1 AS A1 FROM T1
  ) ON T0.C1 = A1
) SELECT A1 FROM CTE;
-- binder error
WITH CTE AS MATERIALIZED (
SELECT A1, * FROM T0
  LEFT JOIN (
    SELECT C1 AS A1 FROM T1
  ) ON T0.C1 = A1
) SELECT A1 FROM CTE;

OS:

macOS 14.2.1 (23C71)

DuckDB Version:

v0.9.2

DuckDB Client:

CLI

Full Name:

Tanner Lai

Affiliation:

Appian

Have you tried this on the latest main branch?

I have tested with a release build (and could not test with a main build)

Have you tried the steps to reproduce? Do they include all relevant data and configuration? Does the issue you report still appear there?

  • Yes, I have
@kryonix
Copy link
Contributor

kryonix commented Jan 29, 2024

This is expected behavior. Your query produces column name A1 twice:

SELECT A1, * FROM T0
  LEFT JOIN (
    SELECT C1 AS A1 FROM T1
  ) ON T0.C1 = A1
┌───────┬───────┬───────┐
│  A1   │  C1   │  A1   │
│ int32 │ int32 │ int32 │
├───────┼───────┼───────┤
│     1 │     1 │     1 │
└───────┴───────┴───────┘

Either rename one of the occurences SELECT A1 AS "A1.1", * ..., or remove A1 completely, as you select it with * anyways.

@tanner-lai
Copy link
Author

Hmm, I would expect that materializing a CTE should not affect the correctness of the results or whether or not there would be results at all (e.g. in this case where it fails to bind).

WITH CTE AS /* MATERIALIZED */ (
SELECT A1, * FROM T0
  LEFT JOIN (
    SELECT C1 AS A1 FROM T1
  ) ON T0.C1 = A1
) SELECT * FROM CTE;

Like you said A1 is selected twice, but the column is automatically realiased. I think it's not unreasonable to expect materialize to maintain this behavior.

┌───────┬───────┬───────┐
│  A1   │  C1   │ A1:1  │
│ int32 │ int32 │ int32 │
├───────┼───────┼───────┤
│     1 │     1 │     1 │
└───────┴───────┴───────┘

kryonix added a commit to kryonix/duckdb that referenced this issue Jan 30, 2024
@kryonix
Copy link
Contributor

kryonix commented Jan 30, 2024

Alright, that makes sense. See PR #10386.

Mytherin added a commit that referenced this issue Jan 30, 2024
hkulyc pushed a commit to hkulyc/duckdb that referenced this issue Mar 20, 2024
@Mytherin
Copy link
Collaborator

Mytherin commented Apr 4, 2024

Fixed in #10386

@Mytherin Mytherin closed this as completed Apr 4, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

5 participants