Model and column descriptions that don't parse to a single string literal are silently stored as empty strings
维护者通常 4 天内回复
还没有人认领这个 Issue。
评估
调研方向
首先阅读 sqlmesh/core/node.py 和 sqlmesh/core/model/meta.py 中的 description validators,然后使用提供的 BigQuery 模型加载示例复现其行为。为模型元数据和列元数据中的复合 description 表达式添加测试覆盖,并验证有效字面量仍会原样加载。Done 意味着不支持的表达式应抛出错误并显示有问题的表达式,而不是静默变为空字符串。
由索引模型根据 Issue 内容生成。
描述
In a SQL model's MODEL (...) block, a description or column_descriptions value that parses into a compound expression rather than a single string literal is stored as ''. There is no error or warning. The text is just gone from the model's metadata.
The easiest way to hit this is a habit carried over from other dialects. '' is not an escape sequence in BigQuery (its only quote escape is \'), and adjacent string literals are concatenated, so a user who writes 'it''s' expecting an apostrophe is writing something BigQuery itself rejects: concatenated literals must be separated by whitespace or comments. SQLGlot 30.8.0, the version SQLMesh pins, accepts it anyway as CONCAT('it', 's'); SQLGlot 30.35.0 now raises a parse error for it. The valid whitespace-separated form, 'first half' 'second half', still parses to CONCAT on current SQLGlot, so the problem below remains reachable after a SQLGlot upgrade.
In our project this silently emptied descriptions in 15 models before we noticed.
Root cause
Both validators convert the parsed expression to text with .name, which is "" for a compound expression such as exp.Concat:
description:_string_expr_validator→str_or_exp_to_str(sqlmesh/core/node.py, lines 367-370 and 563-566 in 0.236.4)column_descriptions:_column_descriptions_validatorusesv.expression.name(sqlmesh/core/model/meta.py, line 332)
Reproduction
Tested on 0.236.4 (sqlglot 30.8.0).
from sqlmesh.core.dialect import parse
from sqlmesh.core.model import load_sql_based_model
SQL = "MODEL (name db.m, dialect bigquery, description {d}, column_descriptions (c = {d})); SELECT 1 AS c"
for d in ["'plain'", "'it''s'", "'first half' 'second half'"]:
m = load_sql_based_model(parse(SQL.format(d=d), default_dialect="bigquery"))
print(f"{d:30} -> {m.description!r} {m.column_descriptions!r}")
'plain' -> 'plain' {'c': 'plain'}
'it''s' -> '' {'c': ''}
'first half' 'second half' -> '' {'c': ''}
Expected behavior
Model loading fails with an error that shows the offending expression, rather than silently storing an empty description.
Proposed fix
Add a description-specific converter that raises when a non-literal expression would convert to an empty string, and use it for both fields. It only changes inputs that are silently blanked today. Everything that currently loads with non-empty text, and an explicit '', behaves exactly as before. owner and stamp are untouched.
--- a/sqlmesh/core/node.py
+++ b/sqlmesh/core/node.py
@@ -364,11 +364,16 @@
raise ConfigError(f"'{v}' needs to be time-like: https://pypi.org/project/dateparser")
return v
- @field_validator("owner", "description", "stamp", mode="before")
+ @field_validator("owner", "stamp", mode="before")
@classmethod
def _string_expr_validator(cls, v: t.Any) -> t.Optional[str]:
return str_or_exp_to_str(v)
+ @field_validator("description", mode="before")
+ @classmethod
+ def _description_validator(cls, v: t.Any) -> t.Optional[str]:
+ return description_to_str(v)
+
@field_validator("interval_unit_", mode="before")
@classmethod
def _interval_unit_validator(cls, v: t.Any) -> t.Optional[t.Union[IntervalUnit, str]]:
@@ -564,3 +569,14 @@
if isinstance(v, exp.Expr):
return v.name
return str(v) if v is not None else None
+
+
+def description_to_str(v: t.Any) -> t.Optional[str]:
+ """Like str_or_exp_to_str, but refuses an expression whose text would be lost.
+
+ `.name` is "" for a compound expression such as exp.Concat, so a description that parsed
+ into one would otherwise be stored as an empty string without any error.
+ """
+ if isinstance(v, exp.Expr) and not isinstance(v, exp.Literal) and not v.name:
+ raise ConfigError(f"A description must be a single string literal, got: {v.sql()}")
+ return str_or_exp_to_str(v)
--- a/sqlmesh/core/model/meta.py
+++ b/sqlmesh/core/model/meta.py
@@ -35,7 +35,7 @@
model_kind_validator,
OnAdditiveChange,
)
-from sqlmesh.core.node import _Node, str_or_exp_to_str
+from sqlmesh.core.node import description_to_str, _Node, str_or_exp_to_str
from sqlmesh.core.reference import Reference
from sqlmesh.utils.date import TimeLike
from sqlmesh.utils.errors import ConfigError
@@ -329,7 +329,7 @@
else {
".".join(
normalize_identifiers(part, dialect=dialect).name for part in v.this.parts
- ): v.expression.name
+ ): description_to_str(v.expression)
for v in vs
}
)
With the patch applied to 0.236.4:
| dialect | input | before | after |
|---|---|---|---|
| bigquery | 'plain' |
'plain' |
'plain' |
| bigquery | 'it\'s' |
"it's" |
"it's" |
| bigquery | 'it''s' |
'' |
ConfigError: A description must be a single string literal, got: CONCAT('it', 's') |
| bigquery | 'first half' 'second half' |
'' |
ConfigError: ... got: CONCAT('first half', 'second half') |
| duckdb | 'it''s' |
"it's" |
"it's" |
| bigquery | some_ident |
'some_ident' |
'some_ident' |
| bigquery | '' |
'' |
'' |
A stricter version would reject every non-literal. .name is lossy beyond this case too: upper('x') is stored as 'x'. But it could break descriptions that load today, so I kept this one minimal.
An alternative would be to fold a Concat of string literals into one string, matching BigQuery's rule for whitespace-separated literals. Speaking for myself, I'd prefer the error. For 'it''s' the user almost certainly meant an apostrophe, so folding would produce "its", which is still not what they intended.
Happy to open a PR with tests if this approach looks right!
- 主要语言
- Python
- 星标
- 3.3k
- 派生
- 471
- 平均合并
- 6 天 7 小时
- 30 天内合并 PR
- 35
环境准备
在浏览器里用你自己的 GitHub 账号启动这个项目的开发容器。
- 没有 Dockerfile 或 Docker Compose 文件
- 有 Pull Request 模板
- 阅读贡献指南
从这里开始
- 先读完整个 Issue,再读项目的贡献指南。
- 在 Issue 下留言说明你要接手 —— 这能避免两个人做同样的事。
- Fork 仓库,在一个分支上完成修改。
- 提交 Pull Request,并在描述里引用这个 Issue 编号。
SQLMesh/sqlmesh 的其他 Issue
-
难度 2/5 1-3 小时 新手友好度 68/100
维护者通常 4 天内回复
-
难度 2/5 1-3 小时 新手友好度 67/100
SQLMesh/sqlmesh#6008 · 1 条评论 ·
维护者通常 4 天内回复
-
VSCode Extension: SQLMesh LSP crashes on startup: ImportError: cannot import name 'WorkspaceDiagnosticRefreshRequest' with lsprotocol 2025.x可能已有人在做 @boomesh11 于 15 天前认领。 未关闭Bug
难度 2/5 1-3 小时 新手友好度 66/100
维护者通常 4 天内回复
-
Improvement
难度 1/5 1 小时以内 新手友好度 85/100
SQLMesh/sqlmesh#5696 · 6 条评论 ·
维护者通常 4 天内回复
-
Support pg_lake未关闭Feature Improvement
难度 2/5 1-3 小时 新手友好度 70/100
SQLMesh/sqlmesh#5669 · 1 条评论 ·
维护者通常 4 天内回复
相似的 Issue
-
accepted
难度 2/5 1-3 小时 新手友好度 67/100
future-agi/future-agi#5274 ·
维护者通常 1 天内回复
-
难度 2/5 1-3 小时 新手友好度 78/100
GenericMappingTools/pygmt#5014 · 1 条评论 ·
维护者通常 1 天内回复
-
bug
难度 2/5 1-3 小时 新手友好度 66/100
w3c/feedvalidator#204 ·
-
[BUG][Low–Medium] founder_numbers.py silently reads a SAFE discount or pool target given as a fraction (0.2) as 0.2%可能已有人在做 @sumitkhond111-del 今天认领。 未关闭bug
难度 2/5 1-3 小时 新手友好度 72/100
bmad-code-org/BMAD-METHOD#3358 · 1 条评论 ·
维护者通常 1 天内回复
-
难度 2/5 1-3 小时 新手友好度 86/100
open-mmlab/mmcv#3352 ·