Model and column descriptions that don't parse to a single string literal are silently stored as empty strings
Les mainteneurs répondent en général sous 4 jours
Personne n'a encore pris cette issue.
Évaluation
- Difficulté
- 2/5
- Temps estimé
- 1-3 heures
- Accessibilité débutants
- 86/100
Piste de recherche
Commence par lire les description validators dans sqlmesh/core/node.py et sqlmesh/core/model/meta.py, puis reproduis le comportement avec l’exemple fourni de chargement de modèle BigQuery. Ajoute une couverture pour les expressions de description composées dans les métadonnées du modèle et des colonnes, et vérifie que les littéraux valides sont toujours chargés sans modification. Done doit signifier que les expressions non prises en charge déclenchent une erreur affichant l’expression fautive au lieu de devenir silencieusement des chaînes vides.
Rédigé par le modèle d'indexation à partir du texte de l'issue.
Description
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!
- Langage dominant
- Python
- Étoiles
- 3.3k
- Forks
- 471
- Merge moyen
- 6 j 7 h
- PR mergées (30 j)
- 35
Préparer son environnement
Lance le conteneur de développement du projet dans votre navigateur, avec votre propre compte GitHub.
- Aucun Dockerfile ni fichier Docker Compose
- Propose un modèle de pull request
- Lire le guide de contribution
Par où commencer
- Lisez l'issue en entier, puis le guide de contribution du projet.
- Signalez en commentaire que vous la prenez — cela évite que deux personnes fassent le même travail.
- Forkez le dépôt et travaillez sur une branche.
- Ouvrez une pull request qui référence le numéro de l'issue.
Autres issues de SQLMesh/sqlmesh
-
Difficulté 2/5 1-3 heures Accessibilité débutants 68/100
Les mainteneurs répondent en général sous 4 jours
-
Difficulté 2/5 1-3 heures Accessibilité débutants 67/100
SQLMesh/sqlmesh#6008 · 1 commentaire ·
Les mainteneurs répondent en général sous 4 jours
-
VSCode Extension: SQLMesh LSP crashes on startup: ImportError: cannot import name 'WorkspaceDiagnosticRefreshRequest' with lsprotocol 2025.xPeut-être pris @boomesh11 l’a pris il y a 15 jours. OuverteBug
Difficulté 2/5 1-3 heures Accessibilité débutants 66/100
Les mainteneurs répondent en général sous 4 jours
-
Improvement
Difficulté 1/5 Moins d'une heure Accessibilité débutants 85/100
SQLMesh/sqlmesh#5696 · 6 commentaires ·
Les mainteneurs répondent en général sous 4 jours
-
Support pg_lakeOuverteFeature Improvement
Difficulté 2/5 1-3 heures Accessibilité débutants 70/100
SQLMesh/sqlmesh#5669 · 1 commentaire ·
Les mainteneurs répondent en général sous 4 jours
Toutes les issues de SQLMesh/sqlmesh
Issues similaires
-
accepted
Difficulté 2/5 1-3 heures Accessibilité débutants 67/100
future-agi/future-agi#5274 ·
Les mainteneurs répondent en général sous 1 jour
-
Difficulté 2/5 1-3 heures Accessibilité débutants 78/100
GenericMappingTools/pygmt#5014 · 1 commentaire ·
Les mainteneurs répondent en général sous 1 jour
-
Feed validator crashes with "Memory error" when validating https://www.tidyverse.org/index.xmlOuvertebug
Difficulté 2/5 1-3 heures Accessibilité débutants 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%Peut-être pris @sumitkhond111-del l’a pris aujourd’hui. Ouvertebug
Difficulté 2/5 1-3 heures Accessibilité débutants 72/100
bmad-code-org/BMAD-METHOD#3358 · 1 commentaire ·
Les mainteneurs répondent en général sous 1 jour
-
Difficulté 2/5 1-3 heures Accessibilité débutants 86/100
open-mmlab/mmcv#3352 ·