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

[Feature] Return agate_table in dbt run-operation result #10957

Open
wants to merge 4 commits into
base: main
Choose a base branch
from

Conversation

acjh
Copy link

@acjh acjh commented Oct 31, 2024

Resolves #10956

Problem

dbtRunner does not propagate the return value of dbt run-operation.

Solution

Propagate the return value of dbt run-operation in the result as agate_table:

  • In the run method of RunOperationTask, capture the return value of _run_unsafe() in a variable execute_macro_result and pass it when instantiating RunResult with agate_table=execute_macro_result.
  • Define agate_table field in RunResultOutput, and propagate agate_result from RunResult to RunResultOutput in the class method RunResultsArtifact.from_execution_results.

Sample usage:

>>> from dbt.cli.main import dbtRunner
>>> dbt = dbtRunner()
>>> res = dbt.invoke(['run-operation', 'select_something_with_return', '--args', 'name: world'])
>>> res.result.results[0].agate_table.print_json()
[{"name": "hello, world"}]

Also:

  • Added tests/functional/adapter/dbt_run_operations/test_dbt_run_operations.py.
  • Delegated process_run_result to a class method of RunResultsArtifact to allow overrides if desired.

Checklist

  • I have read the contributing guide and understand what's expected of me.
  • I have run this code in development, and it appears to resolve the stated issue.
  • This PR includes tests, or tests are not required or relevant for this PR.
  • This PR has no interface changes (e.g., macros, CLI, logs, JSON artifacts, config files, adapter interface, etc.) or this PR has already received feedback and approval from Product or DX.
  • This PR includes type annotations for new and modified functions.

@acjh acjh requested a review from a team as a code owner October 31, 2024 12:12
Copy link

cla-bot bot commented Oct 31, 2024

Thanks for your pull request, and welcome to our community! We require contributors to sign our Contributor License Agreement and we don't seem to have your signature on file. Check out this article for more information on why we have a CLA.

In order for us to review and merge your code, please submit the Individual Contributor License Agreement form attached above above. If you have questions about the CLA, or if you believe you've received this message in error, please reach out through a comment on this PR.

CLA has not been signed by users: @acjh

@github-actions github-actions bot added the community This PR is from a community member label Oct 31, 2024
@acjh
Copy link
Author

acjh commented Oct 31, 2024

@cla-bot check

@cla-bot cla-bot bot added the cla:yes label Oct 31, 2024
Copy link

cla-bot bot commented Oct 31, 2024

The cla-bot has been summoned, and re-checked this pull request!

@@ -49,6 +49,9 @@ def _run_unsafe(self, package_name, macro_name) -> "agate.Table":
macro_name, project=package_name, kwargs=macro_kwargs, macro_resolver=self.manifest
)

if isinstance(res, str):
return None
Copy link
Author

Choose a reason for hiding this comment

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

Note: When there is no explicit return function call in the macro — like for the select_something test — res is a str of newline characters and spaces, at least as observed with the Postgres and SQLite adapters.

Copy link
Author

Choose a reason for hiding this comment

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

A more robust check is not isinstance(res, agate.Table) but there appears to be deliberate avoidance of import agate. Let me know if that is preferred.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
cla:yes community This PR is from a community member
Projects
None yet
Development

Successfully merging this pull request may close these issues.

[Feature] Return agate_table in dbt run-operation result
1 participant