sqlfluff: dbt.exceptions.InternalException: Asked to compile node, but it has no compiled form

Search before asking

  • I searched the issues and found no similar issues.

What Happened

While linting any dbt model the error is raised part-way through linting (apparently after all the fail lines have been produced and displayed).

I’ve tried sqlfluff versions

0.10.1 - no error 0.11.0, 0.13.2, 1.0.0, 1.1.0 - error thrown

Expected Behaviour

sqlfluff reports linting failures.

Observed Behaviour

SQL model materialising as a view. This is really the WEB_USERS model from the fivetran segment dbt package, just renamed to use our naming standard.

with

SOURCE as (
  select
    *
  from
    {{ ref('SG__WEB_USERS__UNION') }}
),

RENAMED as (
  select
    _DBT_SOURCE_RELATION,
    EMAIL,

    {{ sv_utils.get_email_domain('EMAIL') }} as EMAIL_DOMAIN,

    {{ sv_utils.is_valid_email('EMAIL') }} as IS_EMAIL_VALID,

    ID as USER_ID,
    PERSON_ID,
    USERNAME,
    EMAIL_ADDRESS,
    {{ sv_utils.get_email_domain('EMAIL_ADDRESS') }} as EMAIL_ADDRESS_DOMAIN,
    {{ sv_utils.is_valid_email('EMAIL_ADDRESS') }} as IS_EMAIL_ADDRESS_VALID,
    "NAME" as FULL_NAME,
    SHORT_NAME,
    FIRST_NAME,
    INITIALS,
    SURNAME,
    LAST_NAME,
    USER_TYPE,

    CONTEXT_IP as IP,
    -- CONTEXT_LIBRARY_NAME, -- not useful
    SUBSCRIPTIONS,
    CONTEXT_PAGE_SEARCH as PAGE_URL_QUERY,
    CONTEXT_PAGE_URL as PAGE_URL,
    {{ dbt_utils.get_url_host('CONTEXT_PAGE_URL') }} as PAGE_URL_HOST,
    -- CONTEXT_LIBRARY_VERSION, -- not useful
    CONTEXT_PAGE_REFERRER as PAGE_REFERRER,

    replace(
      {{ dbt_utils.get_url_host('CONTEXT_PAGE_REFERRER') }},
      'www.',
      ''
    ) as PAGE_REFERRER_HOST,

    CONTEXT_PAGE_TITLE as PAGE_TITLE,
    CONTEXT_LOCALE as LOCALE,
    CONTEXT_PAGE_PATH as PAGE_PATH,
    CONTEXT_CAMPAIGN_SOURCE as UTM_SOURCE,
    CONTEXT_CAMPAIGN_MEDIUM as UTM_MEDIUM,
    CONTEXT_CAMPAIGN_NAME as UTM_CAMPAIGN,
    BISLR_ID,
    CONTEXT_USER_AGENT as USER_AGENT,
    case
      when lower(context_user_agent) like '%android%' then 'Android'
      else replace(
        {{ dbt_utils.split_part(
          dbt_utils.split_part('CONTEXT_USER_AGENT',"'('",2),"' '",1) 
        }},
        ';','')
    end as device,
    UUID_TS,
    RECEIVED_AT as RECEIVED_AT_DATE_TIME,
    "ROLE" as COMPANY_ROLE,
    AGENCY_ID,
    ORGANISATION_ID,
    COMPANY_ID,
    COMPANY,
    AGENCY_NAME,
    ORGANISATION_NAME,
    COMPANY_NAME,
    ORGANISATION_TRADING_STYLES,
    COMPANY_FINANCIAL_FORCE_ID,
    COMPANY_CURRENCIES,
    COMPANY_RISK_PROFILES,

    AGENCIES,
    HAS_LTD_TERMS,
    AGENCY_ACCOUNT_STATUS,
    PROPOSAL_STATE,
    HAS_LABS_ACCESS,
    HAS_PAYE_TERMS,
    AGENCY_PAYROLL_PROVIDER,
    AML_STATUS,
    AGENCY_SIGN_UP_STATE,
    TIME_SHEETS_AVAILABLE,
    PICTURE_URL,
    AGENCY_HAS_FULL_ACCESS,
    ONLINE_TIMESHEET_PROVIDER,

    ROLES,
    ROLES_IS_SUPER_USER,
    ROLES_IS_CLIENT_AUTHORISER,
    ROLES_IS_SONOVATE_ADMIN,
    ROLES_IS_CANDIDATE,
    PERMISSIONS as USERS_PERMISSIONS,
    PERMISSIONS_IS_CLIENT_AUTHORISER,
    PERMISSIONS_IS_SONOVATE_ADMIN,
    PERMISSIONS_IS_CANDIDATE,

    "PROFILE" as USERS_PROFILE,
    "SESSION_ID" as USERS_SESSION_ID,
    AUTHENTICATED,
    IMAGE_URL,
    AUTHENTICATION_TOKEN,
    TOKEN,
    PERSON_TOKEN,

    LANGUAGE_CODE,
    SETTINGS_DATE_FORMAT,
    SETTINGS_SUBSCRIPTIONS,
    SETTINGS_TIME_ZONE,
    SETTINGS_LANGUAGE,
    SETTINGS_TIME_FORMAT,
    SETTINGS_COUNTRY_CODE,
    SETTINGS_WEEK_START

  from
    SOURCE
),

FINAL as (
  select 
    *,
    case
      when DEVICE = 'iPhone' then 'iPhone'
      when DEVICE = 'Android' then 'Android'
      when DEVICE in ('iPad', 'iPod') then 'Tablet'
      when DEVICE in ('Windows', 'Macintosh', 'X11')
        then 'Desktop'
      else 'Uncategorized' end as DEVICE_CATEGORY
  from
    RENAMED
)

select
  *
from
  FINAL

Example verbose output

fluff.txt

How to reproduce

Just a basic sqlfluff lint operation

sqlfluff lint .\models\base\segment\SG__WEB_USERS.sql

Dialect

snowflake

Version

0.11.0 0.13.2 1.0.1 1.1.0

Configuration

.sqlfluff

# dbt templating does not keep trailing new lines (L009)

[sqlfluff]
verbose = 3
templater = dbt
dialect = snowflake
exclude_rules = L009,L031,L034
ignore_templated_areas = True
sql_file_exts = .sql,.sql.j2,.dml,.ddl,.SQL

[sqlfluff:rules]
tab_space_size = 2

[sqlfluff:rules:L010]
capitalisation_policy = lower

[sqlfluff:rules:L014]
extended_capitalisation_policy = upper

[sqlfluff:rules:L016]
max_line_length = 180

[sqlfluff:rules:L019]
comma_style = leading

[sqlfluff:rules:L030]
extended_capitalisation_policy = lower

[sqlfluff:rules:L040]
capitalisation_policy = lower

[sqlfluff:templater:dbt]
apply_dbt_builtins = True
load_macros_from_path=./dbt_modules/dbt_utils/macros,./macros

[sqlfluff:templater:dbt:macros]
# Macros provided as builtins for dbt projects
dbt_ref = {% macro ref(model_ref) %}{{model_ref}}{% endmacro %}
dbt_source = {% macro source(source_name, table) %}{{source_name}}_{{table}}{% endmacro %}
dbt_config = {% macro config() %}{% for k in kwargs %}{% endfor %}{% endmacro %}
dbt_var = {% macro var(variable) %}item{% endmacro %}
dbt_is_incremental = {% macro is_incremental() %}True{% endmacro %}

[sqlfluff:templater:jinja:context]
num_things=456
tbl_name=my_table
my_list=['a', 'b', 'c']
MY_LIST=("d", "e", "f")

[sqlfluff:templater:jinja]
apply_dbt_builtins = True
load_macros_from_path=./dbt_modules/dbt_utils/macros

[sqlfluff:templater:jinja:macros]
# Macros provided as builtins for dbt projects
dbt_ref = {% macro ref(model_ref) %}{{model_ref}}{% endmacro %}
dbt_source = {% macro source(source_name, table) %}{{source_name}}_{{table}}{% endmacro %}
dbt_config = {% macro config() %}{% for k in kwargs %}{% endfor %}{% endmacro %}
dbt_var = {% macro var(variable) %}item{% endmacro %}
dbt_is_incremental = {% macro is_incremental() %}True{% endmacro %}

.sqlfluffignore

target/
dbt_modules/
macros/

Are you willing to work on and submit a PR to address the issue?

  • Yes I am willing to submit a PR!

Code of Conduct

About this issue

  • Original URL
  • State: closed
  • Created 2 years ago
  • Reactions: 1
  • Comments: 50 (28 by maintainers)

Most upvoted comments

Ok - unfortunately I don’t think git supports installing direct from subdirectories of git repositories (you’ll need a new version of sqlfluff-templater-dbt not the root sqlfluff project).

Can you download the code for that branch and install from the local version.

You can install the templater locally by running:

pip install <path to wherever you extracted the zip file>/plugins/sqlfluff-templater-dbt

What you should get instead of getting dbt.exceptions.InternalException directly is that dbt will skip the file and carry on - but will also give you a warning with the details of exactly which file (or files) are causing the issue. This should at least enable you to use the tool - but also know which files are causing issues.

I downloaded and installed PR3629. I’m getting a stack dump running that.

@reubster - that’s a very surprising issue. It looks like it’s an issue with importing mashumaro before any work is done at all in dbt or in sqlfluff. Given the changes made in that branch, where there are no changes to any of the imports, it seems strange that we would have triggered an import error.

One thing I do know is that support for dbt-snowflake in python 310 isn’t live yet (see supported versions on pypi), and part of that is due to dependencies, so while it’s a bit of a shot in the dark, would you be able to try the same thing but in python 3.7, 3.8 or 3.9? I can see that your testing env is a python 3.10 env. I know 3.10 is coming for that package but it’s still in pre-release.

@barrywhart I might need your help here, especially on option 1.

I think we have three possible options here:

  1. Don’t recompile the file if node is already a CompiledModelNode. This feels like the easiest, but I’m not sure how the tracing monkey patch would function properly in that case.
  2. Cache the results of _unsafe_process(), so we never re-process a file. That dodges the problem here, but potentially leaves us open to issues if we ever get a CompiledModelNode on the first attempt.
  3. Don’t try and fix anything here and instead, focus on why we’re trying to process the same file twice.

I’m going to make a PR to better understand option 3.

hi @alanmcruickshank

That worked a treat. I’ve downloaded main and installed. I need to install both sqlfluff and the templater. Re-running the lint I now get.

... <snipped>...
==== sqlfluff ====
sqlfluff:                1.1.0 python:                 3.9.12
implementation:        cpython verbosity:                   1
dialect:             snowflake templater:                 dbt
dbt:                    =1.1.1
rules:                                all
==== readout ====

=== [ path: .\models\base\segment\SG__WEB_USERS.sql ] ===

=== [dbt templater] Sorting Nodes...
17:59:24  [WARNING]: Deprecated functionality
The `source-paths` config has been renamed to `model-paths`. Please update your
`dbt_project.yml` configuration to reflect this change.
17:59:24  [WARNING]: Deprecated functionality
The `data-paths` config has been renamed to `seed-paths`. Please update your
`dbt_project.yml` configuration to reflect this change.
=== [dbt templater] Compiling dbt project...
=== [dbt templater] Project Compiled.
== [models\base\segment\SG__WEB_USERS.sql] FAIL
L:   4 | P:   3 | L036 | Select targets should be on a new line unless there is
                       | only one select target.
L:   8 | P:   2 | L019 | Found trailing comma. Expected only leading.
L:  12 | P:  25 | L019 | Found trailing comma. Expected only leading.
L:  13 | P:  10 | L019 | Found trailing comma. Expected only leading.
L:  15 | P:  61 | L019 | Found trailing comma. Expected only leading.
L:  17 | P:  61 | L019 | Found trailing comma. Expected only leading.
L:  19 | P:  18 | L019 | Found trailing comma. Expected only leading.
L:  20 | P:  14 | L019 | Found trailing comma. Expected only leading.
L:  21 | P:  13 | L019 | Found trailing comma. Expected only leading.
L:  22 | P:  18 | L019 | Found trailing comma. Expected only leading.
L:  23 | P:  77 | L019 | Found trailing comma. Expected only leading.
L:  24 | P:  77 | L019 | Found trailing comma. Expected only leading.
L:  25 | P:  24 | L019 | Found trailing comma. Expected only leading.
L:  26 | P:  15 | L019 | Found trailing comma. Expected only leading.
L:  27 | P:  15 | L019 | Found trailing comma. Expected only leading.
L:  28 | P:  13 | L019 | Found trailing comma. Expected only leading.
L:  29 | P:  12 | L019 | Found trailing comma. Expected only leading.
L:  30 | P:  14 | L019 | Found trailing comma. Expected only leading.
L:  31 | P:  14 | L019 | Found trailing comma. Expected only leading.
L:  33 | P:  21 | L019 | Found trailing comma. Expected only leading.
L:  35 | P:  18 | L019 | Found trailing comma. Expected only leading.
L:  36 | P:  42 | L019 | Found trailing comma. Expected only leading.
L:  37 | P:  33 | L019 | Found trailing comma. Expected only leading.
L:  38 | P:  70 | L019 | Found trailing comma. Expected only leading.
L:  40 | P:  43 | L019 | Found trailing comma. Expected only leading.
L:  43 | P:  60 | L019 | Found trailing comma. Expected only leading.
L:  44 | P:  13 | L019 | Found trailing comma. Expected only leading.
L:  46 | P:  28 | L019 | Found trailing comma. Expected only leading.
L:  48 | P:  37 | L019 | Found trailing comma. Expected only leading.
L:  49 | P:  29 | L019 | Found trailing comma. Expected only leading.
L:  50 | P:  35 | L019 | Found trailing comma. Expected only leading.
L:  51 | P:  42 | L019 | Found trailing comma. Expected only leading.
L:  52 | P:  42 | L019 | Found trailing comma. Expected only leading.
L:  53 | P:  42 | L019 | Found trailing comma. Expected only leading.
L:  54 | P:  13 | L019 | Found trailing comma. Expected only leading.
L:  55 | P:  37 | L019 | Found trailing comma. Expected only leading.
L:  57 | P:  18 | L014 | Unquoted identifiers must be consistently upper case.
L:  61 | P:  11 | L019 | Found trailing comma. Expected only leading.
L:  62 | P:  13 | L008 | Commas should be followed by a single whitespace unless
                       | followed by a comment.
L:  63 | P:  12 | L014 | Unquoted identifiers must be consistently upper case.
L:  63 | P:  18 | L019 | Found trailing comma. Expected only leading.
L:  64 | P:  12 | L019 | Found trailing comma. Expected only leading.
L:  65 | P:  41 | L019 | Found trailing comma. Expected only leading.
L:  66 | P:  27 | L019 | Found trailing comma. Expected only leading.
L:  67 | P:  14 | L019 | Found trailing comma. Expected only leading.
L:  68 | P:  20 | L019 | Found trailing comma. Expected only leading.
L:  69 | P:  15 | L019 | Found trailing comma. Expected only leading.
L:  70 | P:  12 | L019 | Found trailing comma. Expected only leading.
L:  71 | P:  16 | L019 | Found trailing comma. Expected only leading.
L:  72 | P:  22 | L019 | Found trailing comma. Expected only leading.
L:  73 | P:  17 | L019 | Found trailing comma. Expected only leading.
L:  74 | P:  32 | L019 | Found trailing comma. Expected only leading.
L:  75 | P:  31 | L019 | Found trailing comma. Expected only leading.
L:  76 | P:  23 | L019 | Found trailing comma. Expected only leading.
L:  77 | P:  26 | L019 | Found trailing comma. Expected only leading.
L:  79 | P:  13 | L019 | Found trailing comma. Expected only leading.
L:  80 | P:  18 | L019 | Found trailing comma. Expected only leading.
L:  81 | P:  26 | L019 | Found trailing comma. Expected only leading.
L:  82 | P:  19 | L019 | Found trailing comma. Expected only leading.
L:  83 | P:  20 | L019 | Found trailing comma. Expected only leading.
L:  84 | P:  19 | L019 | Found trailing comma. Expected only leading.
L:  85 | P:  28 | L019 | Found trailing comma. Expected only leading.
L:  86 | P:  15 | L019 | Found trailing comma. Expected only leading.
L:  87 | P:  25 | L019 | Found trailing comma. Expected only leading.
L:  88 | P:  26 | L019 | Found trailing comma. Expected only leading.
L:  89 | P:  16 | L019 | Found trailing comma. Expected only leading.
L:  90 | P:  27 | L019 | Found trailing comma. Expected only leading.
L:  91 | P:  30 | L019 | Found trailing comma. Expected only leading.
L:  93 | P:  10 | L019 | Found trailing comma. Expected only leading.
L:  94 | P:  24 | L019 | Found trailing comma. Expected only leading.
L:  95 | P:  31 | L019 | Found trailing comma. Expected only leading.
L:  96 | P:  28 | L019 | Found trailing comma. Expected only leading.
L:  97 | P:  23 | L019 | Found trailing comma. Expected only leading.
L:  98 | P:  37 | L019 | Found trailing comma. Expected only leading.
L:  99 | P:  37 | L019 | Found trailing comma. Expected only leading.
L: 100 | P:  34 | L019 | Found trailing comma. Expected only leading.
L: 101 | P:  29 | L019 | Found trailing comma. Expected only leading.
L: 103 | P:  31 | L019 | Found trailing comma. Expected only leading.
L: 104 | P:  37 | L019 | Found trailing comma. Expected only leading.
L: 105 | P:  18 | L019 | Found trailing comma. Expected only leading.
L: 106 | P:  14 | L019 | Found trailing comma. Expected only leading.
L: 107 | P:  25 | L019 | Found trailing comma. Expected only leading.
L: 108 | P:  10 | L019 | Found trailing comma. Expected only leading.
L: 109 | P:  17 | L019 | Found trailing comma. Expected only leading.
L: 111 | P:  18 | L019 | Found trailing comma. Expected only leading.
L: 112 | P:  25 | L019 | Found trailing comma. Expected only leading.
L: 113 | P:  27 | L019 | Found trailing comma. Expected only leading.
L: 114 | P:  23 | L019 | Found trailing comma. Expected only leading.
L: 115 | P:  22 | L019 | Found trailing comma. Expected only leading.
L: 116 | P:  25 | L019 | Found trailing comma. Expected only leading.
L: 117 | P:  26 | L019 | Found trailing comma. Expected only leading.
L: 122 | P:   2 | L019 | Found trailing comma. Expected only leading.
L: 125 | P:   9 | L001 | Unnecessary trailing whitespace.
L: 126 | P:   6 | L019 | Found trailing comma. Expected only leading.
L: 138 | P:   1 | L036 | Select targets should be on a new line unless there is
                       | only one select target.
ERROR      Fatal dbt compilation error on C:\source\analytics-dbt-models\src\sv.analytics\models\base\segment\SG__WEB_USERS.sql. This occurs most often during incorrect sorting of ephemeral models before linting. Please report this error on github at https://github.com/sqlfluff/sqlfluff/issues, including both the raw and compiled sql for the model affected. 
Traceback (most recent call last):
  File "C:\source\analytics-dbt-models\.venv\lib\site-packages\sqlfluff_templater_dbt\templater.py", line 464, in _unsafe_process
    node = self.dbt_compiler.compile_node(
  File "C:\source\analytics-dbt-models\.venv\lib\site-packages\dbt\compilation.py", line 513, in compile_node
    node = self._compile_node(node, manifest, extra_context)
  File "C:\source\analytics-dbt-models\.venv\lib\site-packages\dbt\compilation.py", line 370, in _compile_node
    compiled_node = _compiled_type_for(node).from_dict(data)
  File "C:\source\analytics-dbt-models\.venv\lib\site-packages\dbt\compilation.py", line 41, in _compiled_type_for
    raise InternalException(
dbt.exceptions.InternalException: Asked to compile <class 'dbt.contracts.graph.compiled.CompiledModelNode'> node, but it has no compiled form
WARNING    Skipped file C:\source\analytics-dbt-models\src\sv.analytics\models\base\segment\SG__WEB_USERS.sql because dbt raised a fatal exception during compilation: Asked to compile <class 'dbt.contracts.graph.compiled.CompiledModelNode'> node, but it has no compiled form 
== [models\base\segment\SG__WEB_USERS.sql] PASS
==== summary ====
violations:       95 status:         FAIL
All Finished!

The changes I’ve suggested on that branch above are actually now merged into the main branch (we decided they’re actually broadly useful and applicable beyond just debugging). I’ll see if we can cut a new release soon which we can then use for testing without needing to install from source.

Barry, from my point of view… I don’t get the error in 0.10.1, and I do get the error in 0.11.0 - which is the release that includes the ephemeral fix.

Only thing my staging models are using is dbt source, Could it be that the linting tool is looking for a compiled form of source ? I am just relating it to error message - Asked to compile <class ‘dbt.contracts.graph.compiled.CompiledModelNode’> node, but it has no compiled form I do not think sources are compiled as such.

Mine too.

I have now;

  • re-upgraded to 1.1.0
  • changed to materialisation table
  • replaced the dbt source with an explicit object

I get the same error. I get this error for any model.

Things I note from the trace;

  • the actual linting works, and the stack trace is written afterwards. I guess the exception might have occurred earlier and only been written to the console after the linting output?
  • the actual linting does show errors for the WHOLE file, it’s not hitting a problematic line and then exiting. the last linting fail line - line 126 is near the end of the file
  • the output shows “file SG__WEB_USERS.sql: 50%|” immediately before the stack trace… 50% of what?

Stuck figuring out what to try next. Is it viable for me to download the source and run it in a debug mode?