8000 Postgres `ON` preventing formatting after occurrence. · Issue #6659 · sqlfluff/sqlfluff · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

Postgres ON preventing formatting after occurrence. #6659

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

Closed
3 tasks done
WillMatthews opened this issue Feb 17, 2025 · 3 comments · Fixed by #6660
Closed
3 tasks done

Postgres ON preventing formatting after occurrence. #6659

WillMatthews opened this issue Feb 17, 2025 · 3 comments · Fixed by #6660
Labels
bug Something isn't working postgres Issues relating to the Postgres dialect

Comments

@WillMatthews
Copy link
Contributor

Search before asking

  • I searched the issues and found no similar issues.

What Happened

I note that any query that I have is not formatted correctly (in terms of indentation) after a on conflict (..) do statement.
Within the query, anything that is before the on is formatted absolutely perfectly, but after it fails to indent.

Example of SQL to format:

insert into foo (
id,
bar
)
values (
$1,
$2
) on conflict (id) do update
set
bar = $2;

Expected Behaviour

(I think I should expect this - at least, just any indentation after the do...)

insert into foo (
    id,
    bar
)
values (
    $1,
    $2
)
on conflict (id) do update
    set
        bar = $2;

If I have just a update statement (without a do) it indents as follows just fine.

update foo
set
bar = $2;

is converted to

update foo
set
    bar = $2;

Observed Behaviour

Any query that I have is indented absolutely fine until a on conflict ... do update is reached.
Running fix on the above yields:

insert into foo (
    id,
    bar
)
values (
    $1,
    $2
)
on conflict (id) do update
set
bar = $2;

How to reproduce

Here is some example SQL that will fail to indent after on conflict .. do update

insert into foo (
    id,
    bar
)
values (
    $1,
    $2
)
on conflict (id) do update
set
bar = $2;

Dialect

Postgres

Version

sqlfluff, version 3.3.1

Configuration

.sqlfluff

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

@WillMatthews WillMatthews added the bug Something isn't working label Feb 17, 2025
@github-actions github-actions bot added the postgres Issues relating to the Postgres dialect label Feb 17, 2025
@willavos
Copy link

I suspect it's a case of adding Indent and Dedent at appropriate points in here... I'd be happy to submit a PR if I'm on the right track :)

class ConflictActionSegment(BaseSegment):
    """A Conflict Action Statement used within an INSERT statement.

    As specified in https://www.postgresql.org/docs/14/sql-insert.html
    """

    type = "conflict_action"

    match_grammar = Sequence(
        "DO",
        OneOf(
            "NOTHING",
            Sequence(
                "UPDATE",
                "SET",
                Delimited(
                    OneOf(
                        Sequence(
                            Ref("ColumnReferenceSegment"),
                            Ref("EqualsSegment"),
                            OneOf(Ref("ExpressionSegment"), "DEFAULT"),
                        ),
                        Sequence(
                            Bracketed(Delimited(Ref("ColumnReferenceSegment"))),
                            Ref("EqualsSegment"),
                            Ref.keyword("ROW", optional=True),
                            Bracketed(
                                Delimited(OneOf(Ref("ExpressionSegment"), "DEFAULT"))
                            ),
                        ),
                        Sequence(
                            Bracketed(Delimited(Ref("ColumnReferenceSegment"))),
                            Ref("EqualsSegment"),
                            Bracketed(Ref("SelectableGrammar")),
                        ),
                    )
                ),
                Sequence("WHERE", Ref("ExpressionSegment"), optional=True),
            ),
        ),
    )

8000

@willavos
Copy link

DB triggers fail to indent as well. (happy to look at this and attempt to fix in the existing PR).
(Sorry for the multiple accounts!)

create trigger trigger_abc
after insert or update on foo
for each row
when (new.abc = true)
execute function abc();

@willavos
Copy link

I think I'm out of my depth. test/fixtures/dialects/postgres/create_trigger.sql has test fixtures that indents as it should do. I'll keep playing but I don't really know.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working postgres Issues relating to the Postgres dialect
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants
0