-
Notifications
You must be signed in to change notification settings - Fork 1k
Closed as not planned
Description
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
Labels
No labels