Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: missed condition in adding vars args #1543

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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
2 changes: 2 additions & 0 deletions cosmos/dbt/graph.py
Original file line number Diff line number Diff line change
Expand Up @@ -378,6 +378,8 @@ def _add_vars_arg(self, cmd_args: list[str]) -> None:
"""
if self.project.dbt_vars:
cmd_args.extend(["--vars", json.dumps(self.project.dbt_vars, sort_keys=True)])
elif self.dbt_vars:
cmd_args.extend(["--vars", json.dumps(self.dbt_vars, sort_keys=True)])
Comment on lines 379 to +382
Copy link
Contributor

@pankajkoti pankajkoti Feb 19, 2025

Choose a reason for hiding this comment

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

Suggested change
if self.project.dbt_vars:
cmd_args.extend(["--vars", json.dumps(self.project.dbt_vars, sort_keys=True)])
elif self.dbt_vars:
cmd_args.extend(["--vars", json.dumps(self.dbt_vars, sort_keys=True)])
if self.dbt_vars:
cmd_args.extend(["--vars", json.dumps(self.dbt_vars, sort_keys=True)])

This should yield the same result, right?

@tatiana Was there a specific reason we checked for dbt_vars only in self.project instead of directly using self.dbt_vars while working on #1114?

@AlexandrKhabarov While Tatiana(she is Out of Office this week) provides insights on this, logically speaking, wouldn’t it make more sense to use the variables set at the project level during parsing/graph building rather than the ones set for the operator(I would incline to think that operator args would be used for each of the operator tasks during execution)? What do you think?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

So, I saw this part of the code and decided that project level variables have higher priority than operator vars and here can be two different approaches to pass variables to dbt (we are passing dbt variables via operator_args)

And this PR was intended to follow to the same logic in listing resource of dbt

Also, project level variables can be the same as variables of operator, right? Listing resource of dbt will lead to duplicated logic on client side (I will need to pass the same variable in two places)


@cached_property
def dbt_ls_args(self) -> list[str]:
Expand Down
35 changes: 35 additions & 0 deletions tests/dbt/test_graph.py
Original file line number Diff line number Diff line change
Expand Up @@ -1532,6 +1532,41 @@ def test_load_via_dbt_ls_project_config_dbt_vars(
assert ls_command[ls_command.index("--vars") + 1] == '{"my_var1": "my_value1", "my_var2": "my_value2"}'


@patch("cosmos.dbt.graph.DbtGraph.should_use_dbt_ls_cache", return_value=False)
@patch("cosmos.dbt.graph.Popen")
@patch("cosmos.dbt.graph.DbtGraph.update_node_dependency")
@patch("cosmos.config.RenderConfig.validate_dbt_command")
@patch.dict(sys.modules, {"dbt.cli.main": None})
def test_load_via_dbt_ls_dbt_graph_dbt_vars(
mock_validate, mock_update_nodes, mock_popen, mock_use_case, tmp_dbt_project_dir
):
"""Tests that the dbt ls command in the subprocess has "--vars" with the DbtGraph dbt_vars."""
mock_popen().communicate.return_value = ("", "")
mock_popen().returncode = 0
dbt_vars = {"my_var3": "my_value3"}
render_config = RenderConfig(
dbt_project_path=tmp_dbt_project_dir / DBT_PROJECT_NAME,
source_rendering_behavior=SOURCE_RENDERING_BEHAVIOR,
)
profile_config = ProfileConfig(
profile_name="test",
target_name="test",
profiles_yml_filepath=DBT_PROJECTS_ROOT_DIR / DBT_PROJECT_NAME / "profiles.yml",
)
execution_config = ExecutionConfig(dbt_project_path=tmp_dbt_project_dir / DBT_PROJECT_NAME)
dbt_graph = DbtGraph(
project=ProjectConfig(),
render_config=render_config,
execution_config=execution_config,
profile_config=profile_config,
dbt_vars=dbt_vars,
)
dbt_graph.load_via_dbt_ls()
ls_command = mock_popen.call_args.args[0]
assert "--vars" in ls_command
assert ls_command[ls_command.index("--vars") + 1] == json.dumps(dbt_vars, sort_keys=True)


@patch("cosmos.dbt.graph.DbtGraph.should_use_dbt_ls_cache", return_value=False)
@patch("cosmos.dbt.graph.Popen")
@patch("cosmos.dbt.graph.DbtGraph.update_node_dependency")
Expand Down