@@ -3367,6 +3367,56 @@ def execute(context: ExecutionContext, **kwargs: t.Any) -> pd.DataFrame:
33673367 _check_successful_or_raise (test_default_vars .run ())
33683368
33693369
3370+ def test_python_model_sorting (tmp_path : Path ) -> None :
3371+ py_model = tmp_path / "models" / "test_sort_model.py"
3372+ py_model .parent .mkdir (parents = True , exist_ok = True )
3373+ py_model .write_text (
3374+ """
3375+ import pandas as pd # noqa: TID253
3376+ from sqlmesh import model, ExecutionContext
3377+ import typing as t
3378+
3379+ @model(
3380+ name="test_sort_model",
3381+ columns={"id": "int", "value": "varchar"},
3382+ )
3383+ def execute(context: ExecutionContext, **kwargs: t.Any) -> pd.DataFrame:
3384+ # Return rows in a potentially non-deterministic order
3385+ # (simulating a model that doesn't guarantee order)
3386+ return pd.DataFrame([
3387+ {"id": 3, "value": "c"},
3388+ {"id": 1, "value": "a"},
3389+ {"id": 2, "value": "b"},
3390+ ])"""
3391+ )
3392+
3393+ config = Config (model_defaults = ModelDefaultsConfig (dialect = "duckdb" ))
3394+ context = Context (config = config , paths = tmp_path )
3395+
3396+ python_model = context .models ['"test_sort_model"' ]
3397+
3398+ _check_successful_or_raise (
3399+ _create_test (
3400+ body = load_yaml ("""
3401+ test_without_sort:
3402+ model: test_sort_model
3403+ outputs:
3404+ query:
3405+ rows:
3406+ - id: 1
3407+ value: "a"
3408+ - id: 2
3409+ value: "b"
3410+ - id: 3
3411+ value: "c"
3412+ """ ),
3413+ test_name = "test_without_sort" ,
3414+ model = python_model ,
3415+ context = context ,
3416+ ).run ()
3417+ )
3418+
3419+
33703420@use_terminal_console
33713421def test_cte_failure (tmp_path : Path ) -> None :
33723422 models_dir = tmp_path / "models"
0 commit comments