Skip to content

validate_with_schema rejects LATERAL references to earlier FROM sources #472

Description

@kvlonge

Summary

validate_with_schema() does not let a LATERAL subquery reference a source that appears earlier in the same FROM clause.

Reproduction

pip install polyglot-sql==0.12.1
import polyglot_sql

schema = {"tables": [{"name": "orders", "columns": [{"name": "total", "type": "INT"}]}]}
sql = """
SELECT nested.value
FROM orders AS orders_source,
     LATERAL (SELECT orders_source.total AS value) AS nested
"""
result = polyglot_sql.validate_with_schema(sql, schema, dialect="snowflake")
print(result.valid, [(error.code, error.message) for error in result.errors])

Actual

False [('E222', "Unknown table or alias 'orders_source' referenced by column 'total'")]

Expected

The query is valid. A LATERAL derived table is correlated, so orders_source is in scope inside it.

Activity

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

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