sqlfluff / sqlfluff

A modular SQL linter and auto-formatter with support for multiple dialects and templated code.
https://www.sqlfluff.com
MIT License
7.31k stars 648 forks source link

Linting doesn't work over plpgsql blocks #5864

Open galhar opened 3 weeks ago

galhar commented 3 weeks ago

Search before asking

What Happened

I really want to use sqlfluff in my system contains multiple, big sql files running over greenplum. The problem is that many of my sql files contain plpgsql blocks, and sqlfluff seems to not work when being run over such files.

Expected Behaviour

Running lint should yield errors, running fix should fix the file. When running over the following code of plpgsql block:

do
$$
declare 
  v1 INTEGER;
begin
  create temp table bla3 as
  with bla1 as (select generate_series(0,100,1) as x1
  ),
  bla2 as (
        select x1, x1*2 as x2
                 from bla1
  )
  select 
    bla1.x1, bla2.x2,
         bla2.x2 * bla1.x1 as x3
  from bla2
inner join bla1
    on true;
  analyze bla3;

  raise info 'max is %', (select max(x3) from bla3);  

end;
$$
language plpgsql;

We expect sqlfluff to work the same way it would work over the same code out of the plpgsql block:

create temp table bla3 as
  with bla1 as (select generate_series(0,100,1) as x1
  ),
  bla2 as (
        select x1, x1*2 as x2
                 from bla1
  )
  select 
    bla1.x1, bla2.x2,
         bla2.x2 * bla1.x1 as x3
  from bla2
inner join bla1
    on true;
  analyze bla3;

Observed Behaviour

But I get the following results when fixing: The plpgsql remains exactly the same:

do
$$
declare 
  v1 INTEGER;
begin
  create temp table bla3 as
  with bla1 as (select generate_series(0,100,1) as x1
  ),
  bla2 as (
        select x1, x1*2 as x2
                 from bla1
  )
  select 
    bla1.x1, bla2.x2,
         bla2.x2 * bla1.x1 as x3
  from bla2
inner join bla1
    on true;
  analyze bla3;

  raise info 'max is %', (select max(x3) from bla3);  

end;
$$
language plpgsql;

whereas the same code not within such a block get fixed as desired:

create temp table bla3 as
with bla1 as (select generate_series(0, 100, 1) as x1
),

bla2 as (
    select
        x1,
        x1 * 2 as x2
    from bla1
)

select
    bla1.x1,
    bla2.x2,
    bla2.x2 * bla1.x1 as x3
from bla2
inner join bla1
    on true;
analyze bla3;

How to reproduce

Just run sqlfluff fix example.sql --dialect greenplum, when the example.sql file contains either one of the code blocks wrote above.

Dialect

greenplum

Version

3.0.6

Configuration

default, no configuration file needed

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

Code of Conduct

galhar commented 3 weeks ago

I might be wrongly labelling it as "bug", if it isn't supported by purpose. But it seems like Sqlfluff should support postgresql and greenplum.