Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 15 additions & 1 deletion sqlmesh/utils/jinja.py
Original file line number Diff line number Diff line change
Expand Up @@ -206,6 +206,20 @@ def extract_macro_references_and_variables(
return macro_references, variables


def sort_dict_recursive(
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I couldnt find an existing util function that would recurse into values and sort those too, let me know if it exists and i'm just blind

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@eakmanrq did we end up removing the utility you implemented for python_env sorting?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

do you mean _dict_sort or sort_python_env? i'm not sure either of these would achieve the same, unless there is another one

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah sorry I'm bad with Github notification. I originally added a recrusive dict sort but rolled that back in this PR and just sort the top level: #4984

item: t.Dict[str, t.Any],
) -> t.Dict[str, t.Any]:
sorted_dict: t.Dict[str, t.Any] = {}
for k, v in sorted(item.items()):
if isinstance(v, list):
sorted_dict[k] = sorted(v)
elif isinstance(v, dict):
sorted_dict[k] = sort_dict_recursive(v)
else:
sorted_dict[k] = v
return sorted_dict


JinjaGlobalAttribute = t.Union[str, int, float, bool, AttributeDict]


Expand Down Expand Up @@ -440,7 +454,7 @@ def to_expressions(self) -> t.List[Expression]:
d.PythonCode(
expressions=[
f"{k} = '{v}'" if isinstance(v, str) else f"{k} = {v}"
for k, v in sorted(filtered_objs.items())
for k, v in sort_dict_recursive(filtered_objs).items()
]
)
)
Expand Down
27 changes: 27 additions & 0 deletions tests/utils/test_jinja.py
Original file line number Diff line number Diff line change
Expand Up @@ -302,3 +302,30 @@ def test_dbt_adapter_macro_scope():

rendered = registry.build_environment().from_string("{{ spark__macro_a() }}").render()
assert rendered.strip() == "macro_a"


def test_macro_registry_to_expressions_sorted():
refs = AttributeDict(
{
"payments": {
"database": "jaffle_shop",
"schema": "main",
"nested": {"foo": "bar", "baz": "bing"},
},
"orders": {"schema": "main", "database": "jaffle_shop", "nested_list": ["b", "a", "c"]},
}
)

registry = JinjaMacroRegistry()
registry.add_globals({"sources": {}, "refs": refs})

# Ensure that the AttributeDict string representation is sorted
# in order to prevent an unexpected *visual* diff in ModelDiff
# (note that the actual diff is based on the data hashes, so this is purely visual)
expressions = registry.to_expressions()
assert len(expressions) == 1
assert (
expressions[0].sql(dialect="duckdb")
== "refs = {'orders': {'database': 'jaffle_shop', 'nested_list': ['a', 'b', 'c'], 'schema': 'main'}, 'payments': {'database': 'jaffle_shop', 'nested': {'baz': 'bing', 'foo': 'bar'}, 'schema': 'main'}}\n"
"sources = {}"
)