Description
Description
When using Python models in SQLMesh, column names in audit configurations are incorrectly handled during SQL generation. While SQL model audits work correctly (where columns are specified without quotes), Python model audits fail because the quoted column names in the Python configuration are not properly converted to SQL identifiers.
Current Behavior
In Python models like:
@model(
name='table',
audits=[
("not_null", {"columns": ["naam"]}), # Column name is quoted as string
],
)
The generated SQL treats the column name as a string literal instead of a column identifier.
Expected Behavior
- Python models should convert the quoted string column names into proper SQL identifiers
Example
-- Current (incorrect) SQL generation for Python model:
SELECT * FROM table WHERE 'naam' IS NULL
-- Expected SQL generation:
SELECT * FROM table WHERE naam IS NULL
-- or
SELECT * FROM table WHERE "naam" IS NULL
Technical Details
The issue is specific to Python model definitions where column names are necessarily provided as Python strings. The string values aren't being properly converted to SQL identifiers during the audit query generation process.
Steps to Reproduce
- Create a Python model with an audit that references column names:
import typing as t
from datetime import datetime
import pandas as pd
from sqlmesh import ExecutionContext, model
@model(
"example.basic",
columns={
"id": "int",
"name": "text",
},
audits=[
("not_null", {"columns": ["id"]}),
],
)
def execute(
context: ExecutionContext,
start: datetime,
end: datetime,
execution_time: datetime,
**kwargs: t.Any,
) -> pd.DataFrame:
return pd.DataFrame([
{"id": None, "name": "name"}
])
- Run the audit
- Observe the generated SQL contains single-quoted column names
The test should fail but actually succeeds!
Impact
- Affects only Python model audit configurations
- SQL model audits work as expected
- Audit validations for Python models fail because they operate on string literals instead of column values
Suggested Fix
Add proper column identifier conversion specifically for Python model audit configurations, ensuring that string column names are converted to proper SQL identifiers.