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

graph object not available in dbt-power-user #1470

Open
1 task
Cquad opened this issue Oct 17, 2024 · 4 comments
Open
1 task

graph object not available in dbt-power-user #1470

Cquad opened this issue Oct 17, 2024 · 4 comments
Labels
bug Something isn't working

Comments

@Cquad
Copy link

Cquad commented Oct 17, 2024

Expected behavior

Be able to call macro which use graph.nodes.values() method.

Actual behavior

When I use a macro which use graph context and ask to compile to dbt-power-user I have this error :

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/home/dbt/.vscode-server/extensions/innoverio.vscode-dbt-power-user-0.47.2/dist/node_python_bridge.py", line 104, in <module>
    value = eval(_compile(data['code'], '<input>', 'eval'), _locals)
            ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "<input>", line 1, in <module>
  File "/home/dbt/.vscode-server/extensions/innoverio.vscode-dbt-power-user-0.47.2/dist/dbt_core_integration.py", line 698, in compile_sql
    raise Exception(str(e))
Exception: Compilation Error in sql_operation t_3d436d9853c44498bc47853630920867 (from remote system.sql)
  'dict object' has no attribute 'nodes'
  
  > in macro doc_star (macros/doc_star.sql)
  > called by sql_operation t_3d436d9853c44498bc47853630920867 (from remote system.sql)

When running dbt compile command on the same model, it works fine.

Steps To Reproduce

My goal was to generate a column list based on documentation and not on adapter.get_relation.

Add these macros to jaffle_shop project :

{%- macro doc_star(from, except=none, alias=none) -%}
    {%- if execute %} {%- set node = get_node(from) %} {{- generate_star_expression(node, except or [], alias) }}

    {%- else -%} {{ "*" if alias is none else alias ~ "." ~ "*" }}
    {%- endif -%}
{%- endmacro %}

{%- macro get_node(model_name) -%}

    {#- Filter the graph to find the node for the specified model -#}
    {%- set node = (
        graph.nodes.values()
        | selectattr("resource_type", "equalto", "model")
        | selectattr("name", "equalto", model_name.identifier)
    ) | first %}

    {#- If node is not found in model, trying in sources -#}
    {%- if node is not defined %}
        {%- set node = (
            graph.sources.values()
            | selectattr("resource_type", "equalto", "source")
            | selectattr("name", "equalto", model_name.identifier)
        ) | first %}
        {%- if node is not defined %}
            {{- exceptions.raise_compiler_error('Model or Source"' ~ model_name.identifier ~ '" is not found.') }}
        {%- endif %}
    {%- endif %}

    {{- return(node) }}

{%- endmacro -%}

{%- macro generate_star_expression(node, except, alias) -%}
    {%- set needed_columns = [] %}
    {%- for column in node.columns %}

        {#- Nested columns (with a '.') must be remove also #}
        {%- if column not in except and '.' not in column %} {%- do needed_columns.append(column) %} {%- endif %}

    {%- endfor %}

    {%- if needed_columns | length == 0 %}
        {{- exceptions.raise_compiler_error("Node " ~ node.unique_id ~ " has 0 column to select.") }}
    {%- endif %}

    {%- for column in needed_columns %}
        {{- "" if alias is none else alias ~ "." }}{{- column -}}{{- ",\n" if not loop.last else "" }}
    {%- endfor %}

{%- endmacro %}

Compile with dbt-power-user a new model with this code :

select {{ doc_star(ref("customers")) }} from {{ ref("customers") }}

It raises an error.

Log output/Screenshots

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
File "/home/dbt/.vscode-server/extensions/innoverio.vscode-dbt-power-user-0.47.2/dist/node_python_bridge.py", line 104, in
value = eval(_compile(data['code'], '', 'eval'), _locals)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "", line 1, in
File "/home/dbt/.vscode-server/extensions/innoverio.vscode-dbt-power-user-0.47.2/dist/dbt_core_integration.py", line 698, in compile_sql
raise Exception(str(e))
Exception: Compilation Error in sql_operation t_3d436d9853c44498bc47853630920867 (from remote system.sql)
'dict object' has no attribute 'nodes'

in macro doc_star (macros/doc_star.sql)
called by sql_operation t_3d436d9853c44498bc47853630920867 (from remote system.sql)

Operating System

Debian GNU/Linux

dbt version

1.8.6

dbt Adapter

bigquery

dbt Power User version

0.47.2

Are you willing to submit PR?

  • Yes I am willing to submit a PR!
@Cquad Cquad added the bug Something isn't working label Oct 17, 2024
@alexandre-bernard
Copy link

Hello,
I'm facing the same problem.
Any idea to solve this ?

@alexandre-bernard
Copy link

Bump :)

@cedric-orange
Copy link

I'm facing the same issue, is there a trick to avoid this ?

@cedric-orange
Copy link

Updated traceback with latest vscode-dbt-power-user version:

Traceback (most recent call last):
  File "/home/vscode/.vscode-server/extensions/innoverio.vscode-dbt-power-user-0.51.2/dist/dbt_core_integration.py", line 751, in _compile_node
    compiled_node = self.sql_compiler.compile(self.dbt)
  File "/usr/local/lib/python3.9/site-packages/dbt/task/sql.py", line 44, in compile
    return self.compiler.compile_node(self.node, manifest, {}, write=False)
  File "/usr/local/lib/python3.9/site-packages/dbt/compilation.py", line 533, in compile_node
    node = self._compile_code(node, manifest, extra_context)
  File "/usr/local/lib/python3.9/site-packages/dbt/compilation.py", line 421, in _compile_code
    node.compiled_code = jinja.get_rendered(
  File "/usr/local/lib/python3.9/site-packages/dbt/clients/jinja.py", line 146, in get_rendered
    rendered = render_template(template, ctx, node)
  File "/usr/local/lib/python3.9/site-packages/dbt_common/clients/jinja.py", line 572, in render_template
    return template.render(ctx)
  File "/usr/local/lib/python3.9/site-packages/dbt_common/clients/jinja.py", line 163, in render
    return self.environment.handle_exception()
  File "/usr/local/lib/python3.9/site-packages/jinja2/environment.py", line 936, in handle_exception
    raise rewrite_traceback_stack(source=source)
  File "<template>", line 5, in top-level template code
  File "/usr/local/lib/python3.9/site-packages/jinja2/sandbox.py", line 393, in call
    return __context.call(__obj, *args, **kwargs)
  File "/usr/local/lib/python3.9/site-packages/dbt/clients/jinja.py", line 84, in __call__
    return self.call_macro(*args, **kwargs)
  File "/usr/local/lib/python3.9/site-packages/dbt_common/clients/jinja.py", line 322, in call_macro
    return macro(*args, **kwargs)
  File "/usr/local/lib/python3.9/site-packages/jinja2/runtime.py", line 777, in _invoke
    rv = self._func(*arguments)
  File "<template>", line 25, in template
  File "/usr/local/lib/python3.9/site-packages/jinja2/sandbox.py", line 393, in call
    return __context.call(__obj, *args, **kwargs)
  File "/usr/local/lib/python3.9/site-packages/dbt/clients/jinja.py", line 84, in __call__
    return self.call_macro(*args, **kwargs)
  File "/usr/local/lib/python3.9/site-packages/dbt_common/clients/jinja.py", line 324, in call_macro
    return e.value
  File "/usr/local/lib/python3.9/contextlib.py", line 137, in __exit__
    self.gen.throw(typ, value, traceback)
  File "/usr/local/lib/python3.9/site-packages/dbt_common/clients/jinja.py", line 347, in exception_handler
    raise CaughtMacroErrorWithNodeError(exc=e, node=self.macro)
dbt_common.exceptions.macros.CaughtMacroErrorWithNodeError: Compilation Error in sql_operation t_ec6dc5fd2f3e4e67aebfb02bbbba2c20 (from remote system.sql)
  'dict object' has no attribute 'nodes'
  
  > in macro star (macros/star.sql)
  > called by sql_operation t_ec6dc5fd2f3e4e67aebfb02bbbba2c20 (from remote system.sql)

anandgupta42 added a commit that referenced this issue Jan 27, 2025
- Replace graph.nodes with ref() for model reference
- Use adapter.get_relation() and adapter.get_columns_in_relation()
- Simplify source handling
- Fix #1470
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
3 participants