[X] I searched the issues and found no similar issues.
What Happened
I'm trying to set up sqlfluff in my dbt project and used dbt's recommended .sqlfluff file. I ran sqlfluff fix on one of my files to test and it ran successfully but produced a very strange result that will not run in Snowflake. When using the jinja templater, no changes are made.
Expected Behaviour
Below is a simplified version of my SQL file.
{{ config(
materialized = "incremental",
incremental_strategy="merge",
unique_key = "surrogate_key",
alias="entity_performance_by_day",
enabled=true,
tags=["daily"]
) }}
with
eligible_entities as (
select * from {{ ref('stg_source__eligible_entity') }}
where date_local < convert_timezone('UTC', 'America/New_York', current_timestamp)::date
{% if is_incremental() %}
and date_local > coalesce(
(select max(date_local) from {{ this }}),
dateadd('day', -1, {{ var('min_date') }})::date
)
{% endif %}
),
eligible_entities_rollup as (
select
date_local,
entity_id,
location_id,
has_attribute,
{{ dbt_utils.generate_surrogate_key(['date_local', 'entity_id', 'location_id', 'has_attribute']) }} as surrogate_key,
count(distinct request_id) as requests,
avg(score_metric_1) as avg_score_metric_1,
avg(distance_metric) as avg_distance_metric,
avg(rank_metric_1) as avg_rank_metric_1,
avg(rank_metric_2) as avg_rank_metric_2,
avg(rank_metric_3) as avg_rank_metric_3,
{{ dbt.current_timestamp() }} as dbt_processed_timestamp_utc
from eligible_entities
group by all
)
select * from eligible_entities_rollup
When I run sqlfluff fix --dialect snowflake I'd expect it to make some minor changes with whitespace, column ordering. These are the violations it picked up on.
{{ dbt_utils.generate_surrogate_key(['date_local', 'entity_id', 'location_id', 'has_attribute']) }}
as surrogate_key,
count(distinct request_id) as requests,
avg(score_metric_1) as avg_score_metric_1,
avg(distance_metric) as avg_distance_metric,
avg(rank_metric_1) as avg_rank_metric_1,
avg(rank_metric_2) as avg_rank_metric_2,
avg(rank_metric_3) as avg_rank_metric_3,
{{ dbt.current_timestamp() }} as dbt_processed_timestamp_utc
from eligible_entities
group by all
)
select * from eligible_entities_rollup
### How to reproduce
The command I ran was
```shell
sqlfluff fix --dialect snowflake direct/path/to/my/file.sql
Dialect
Snowflake
Version
I'm on Python 3.12.5 and have these relevant packages installed:
Search before asking
What Happened
I'm trying to set up sqlfluff in my dbt project and used dbt's recommended
.sqlfluff
file. I ransqlfluff fix
on one of my files to test and it ran successfully but produced a very strange result that will not run in Snowflake. When using thejinja
templater, no changes are made.Expected Behaviour
Below is a simplified version of my SQL file.
When I run
sqlfluff fix --dialect snowflake
I'd expect it to make some minor changes with whitespace, column ordering. These are the violations it picked up on.Observed Behaviour
However, after finishing, my file then looks like this. If I try to execute this code, it obviously fails with syntax errors. Note how it:
eligible_entities_rollup
CTEdbt_utils.generate_surrogate_key
line and then left aligned one of them while indenting the other even fartherwith
eligible_entities as ( select * from {{ ref('stg_source__eligible_entity') }} where date_local < convert_timezone('UTC', 'America/New_York', current_timestamp)::date {% if is_incremental() %} and date_local > coalesce( (select max(date_local) from {{ this }}), dateadd('day', -1, {{ var('min_date') }})::date ) {% endif %} ),
eligible_entities_rollup as ( select date_local, entity_id, location_id, has_attribute,
{{ dbt_utils.generate_surrogate_key(['date_local', 'entity_id', 'location_id', 'has_attribute']) }} as surrogate_key, count(distinct request_id) as requests, avg(score_metric_1) as avg_score_metric_1, avg(distance_metric) as avg_distance_metric, avg(rank_metric_1) as avg_rank_metric_1, avg(rank_metric_2) as avg_rank_metric_2, avg(rank_metric_3) as avg_rank_metric_3, {{ dbt.current_timestamp() }} as dbt_processed_timestamp_utc from eligible_entities group by all )
select * from eligible_entities_rollup
Dialect
Snowflake
Version
I'm on Python 3.12.5 and have these relevant packages installed:
Configuration
To reproduce, this is the
.sqlfluff
file I'm using. Basically dbt's with one extra rule exclusion.I also have this
.sqlfluff
ignore fileAre you willing to work on and submit a PR to address the issue?
Code of Conduct