Skip to content

<=> comparison operator throws IndexError when applying rule ST09 with Databricks or Sparksql dialect #6560

@NodeJSmith

Description

@NodeJSmith

Search before asking

  • I searched the issues and found no similar issues.

What Happened

When applying rule ST09 on a line that contains the <=> comparison operator an IndexError is thrown because comparison_operator.get_children("raw_comparison_operator") returns an empty list and the fix block expects there to always be at least one item in that list.

Expected Behaviour

ST09 is applied correct and the join condition is flipped

Observed Behaviour

❯ sqlfluff fix reproduce.sql --ignore-local-config --show-lint-violations --dialect databricks --verbose
==== sqlfluff ====
sqlfluff:                3.3.0 python:                3.11.10
implementation:        cpython verbosity:                   1
dialect:            databricks templater:               jinja
rules:                                all
==== finding fixable violations ====
CRITICAL   [ST09] Applying rule ST09 to 'reproduce.sql' threw an Exception: list index out of range                                                                                                                                
Traceback (most recent call last):
  File "/home/jessica/source/rhyme/dbx-dbt-legacy/.venv/lib/python3.11/site-packages/sqlfluff/core/rules/base.py", line 524, in crawl
    res = self._eval(context=context)
          ^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/jessica/source/rhyme/dbx-dbt-legacy/.venv/lib/python3.11/site-packages/sqlfluff/rules/structure/ST09.py", line 237, in _eval
    if raw_comparison_operators[0].raw
       ~~~~~~~~~~~~~~~~~~~~~~~~^^^
IndexError: list index out of range
== [reproduce.sql] FAIL                                                                                                                                                                                                            
L:   1 | P:  21 | ST09 | 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: ST09'
                       | at the end
of line 1

                       | [structure.join_condition_order]
==== no fixable linting violations found ====
All Finished 📜 🎉!
  [1 unfixable linting violations found]
==== lint for unfixable violations ====
== [reproduce.sql] FAIL
L:   1 | P:  21 | ST09 | 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: ST09' at the
                       | end
of line 1
 [structure.join_condition_order]

How to reproduce

SELECT bt.test
FROM base_table AS bt
INNER JOIN second_table AS st
    ON st.test <=> bt.test

Dialect

Databricks and Sparksql both exhibit the issue, but I imagine it's any dialect that supports the <=> operator

Version

Tested on sqlfluff version 3.3.0 and version 3.2.5. Both using the jinja templater.

at 12:27:23 ❯ python --version && sqlfluff --version                                       
Python 3.11.10
sqlfluff, version 3.3.0

Configuration

Ran with --ignore-local-config, so skipping this unless there's some reason it is still necessary.

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't workingdatabrickssparksqlRelated to the SparkSQL (aka Spark3) dialect

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions