Summary
validate_with_schema() does not validate the query inside INSERT ... SELECT. An unknown column is reported in a plain SELECT but not when the same SELECT feeds an INSERT.
Reproduction
pip install polyglot-sql==0.12.1
import polyglot_sql
schema = {"tables": [
{"name": "orders", "columns": [{"name": "order_id", "type": "INT"}]},
{"name": "order_archive", "columns": [{"name": "order_id", "type": "INT"}]},
]}
for sql in (
"SELECT missing FROM orders",
"INSERT INTO order_archive (order_id) SELECT missing FROM orders",
):
result = polyglot_sql.validate_with_schema(sql, schema, dialect="postgres")
print(result.valid, [(error.code, error.message) for error in result.errors])
Actual
False [('E201', "Unknown column 'missing' in table 'orders'")]
True []
Expected
Both statements are invalid with the same E201 error. The source query of INSERT ... SELECT should be validated like a standalone SELECT.
Summary
validate_with_schema()does not validate the query insideINSERT ... SELECT. An unknown column is reported in a plainSELECTbut not when the sameSELECTfeeds anINSERT.Reproduction
Actual
Expected
Both statements are invalid with the same
E201error. The source query ofINSERT ... SELECTshould be validated like a standaloneSELECT.