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

[AM07] Unexpected exception: list index out of range; #5991

Closed
3 tasks done
Timvde opened this issue Jun 29, 2024 · 0 comments · Fixed by #6005
Closed
3 tasks done

[AM07] Unexpected exception: list index out of range; #5991

Timvde opened this issue Jun 29, 2024 · 0 comments · Fixed by #6005
Labels
bug Something isn't working

Comments

@Timvde
Copy link

Timvde commented Jun 29, 2024

Search before asking

  • I searched the issues and found no similar issues.

What Happened

The AM07 linting rule seems to have a hard crash when unions and parentheses are involved.

The issue may be similar to #5401, but that one specifically mentions dbt_utils.union_relations, while I can reproduce seemingly the same error with a simple SQL union.

Expected Behaviour

No internal errors

Observed Behaviour

CRITICAL   [AM07] Applying rule AM07 to 'models/200_WRK/w_test_union.sql' threw an Exception: list index out of range 
Traceback (most recent call last):
  File "/home/tim/.cache/pypoetry/virtualenvs/byta-OvIvHU96-py3.11/lib/python3.11/site-packages/sqlfluff/core/rules/base.py", line 507, in crawl
    res = self._eval(context=context)
          ^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/tim/.cache/pypoetry/virtualenvs/byta-OvIvHU96-py3.11/lib/python3.11/site-packages/sqlfluff/rules/ambiguous/AM07.py", line 217, in _eval
    set_segment_select_sizes, resolve_wildcard = self._get_select_target_counts(
                                                 ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/tim/.cache/pypoetry/virtualenvs/byta-OvIvHU96-py3.11/lib/python3.11/site-packages/sqlfluff/rules/ambiguous/AM07.py", line 196, in _get_select_target_counts
    cnt, res = self.__resolve_selectable(selectable, query)
               ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/tim/.cache/pypoetry/virtualenvs/byta-OvIvHU96-py3.11/lib/python3.11/site-packages/sqlfluff/rules/ambiguous/AM07.py", line 167, in __resolve_selectable
    _cols, _resolved = self.__resolve_selectable_wildcard(
                       ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/tim/.cache/pypoetry/virtualenvs/byta-OvIvHU96-py3.11/lib/python3.11/site-packages/sqlfluff/rules/ambiguous/AM07.py", line 135, in __resolve_selectable_wildcard
    _cols, _resolved = self.__resolve_wild_query(cte)
                       ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/tim/.cache/pypoetry/virtualenvs/byta-OvIvHU96-py3.11/lib/python3.11/site-packages/sqlfluff/rules/ambiguous/AM07.py", line 88, in __resolve_wild_query
    return self.__resolve_selectable(query.selectables[0], query)
                                     ~~~~~~~~~~~~~~~~~^^^
IndexError: list index out of range
== [models/200_WRK/w_test_union.sql] FAIL                                                                            
L:   3 | P:   9 | AM07 | Unexpected exception: list index out of range;
Could you
                       | open an issue at
                       | https://github.com/sqlfluff/sqlfluff/issues ?
You can
                       | ignore this exception for now, by adding '-- noqa: AM07'
                       | at the end
of line 3
 [ambiguous.set_columns]
All Finished 📜 🎉!

How to reproduce

I narrowed it down to this tiny test case:

with
    foo as (
        (select 1)

        union all

        (select 2)
    )

select *
from foo

Note that the following do not crash:

with
    foo as (
        select 1

        union

        select 2
    )

select *
from foo
with
    foo as (
        (select 1 as a)

        union

        (select 2 as a)
    )

select a
from foo

Dialect

postgres

Version

  • sqlfluff 3.0.7
  • Python 3.11.9
  • dbt-core 1.8.2
  • dbt-postgres 1.8.1
  • sqlfluff-templater-dbt 3.0.7

Configuration

[sqlfluff]
dialect = postgres
templater = dbt
exclude_rules = layout.indent, layout.cte_bracket, layout.select_targets, layout.spacing
max_line_length = 88

[sqlfluff:rules]
capitalisation_policy = lower
extended_capitalisation_policy = lower

[sqlfluff:rules:convention.terminator]
multiline_newline = True

(Basically just the sqlfmt recommendations as stated here)

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

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
Development

Successfully merging a pull request may close this issue.

1 participant