Skip to content

Incorrect column qualification in an incorrect subquery #6639

@staskikotx

Description

@staskikotx

Version 28.5.0

import sqlglot
from sqlglot.optimizer.qualify import qualify

schema = {
    "frpm": {
        "FRPM Count (Ages 5-17)": "INT",
        "cdscode": "INT"
    },
    "satscores": {
        "cds": "INT",
        "avgscrread": "INT"
    }
}

sql_query = '''SELECT "FRPM Count (Ages 5-17)" FROM frpm WHERE cdscode = (SELECT cdscode FROM satscores ORDER BY avgscrread DESC NULLS LAST LIMIT 1)'''

# Parse and qualify
expression = sqlglot.parse_one(sql_query, read="postgres")
qualified = qualify(expression, schema=schema, dialect="postgres")

print("Qualified SQL:")
print(qualified.sql(dialect="postgres"))

The output is

Qualified SQL:
SELECT "frpm"."FRPM Count (Ages 5-17)" AS "FRPM Count (Ages 5-17)" FROM "frpm" AS "frpm" WHERE "frpm"."cdscode" = (SELECT "frpm"."cdscode" AS "cdscode" FROM "satscores" AS "satscores" ORDER BY "satscores"."avgscrread" DESC NULLS LAST LIMIT 1)

with incorrect column qualification "frpm"."cdscode" in the subquery.

I think that the column qualifier should throw an error here because the column cdscode does not exist in satscores.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions