Skip to content

Parsing error on oracle with PIVOT when aggregate function is aliased #6711

@nickbayley

Description

@nickbayley

Search before asking

  • I searched the issues and found no similar issues.

What Happened

A parsing error is thrown when linting a statement that uses a PIVOT clause when the aggregate function is aliased. If no alias is used, the file parses successfully.

Expected Behaviour

The file should parse correctly.

Observed Behaviour

Parsing error is thrown instead:

== [broken_pivot.sql] FAIL                                                                                                              
L:   7 | P:   1 |  PRS | Line 7, Position 1: Found unparsable section:                                                                  
                       | 'pivot\n(\n    count(state_code) as states\n...'
WARNING: Parsing errors found and dialect is set to 'oracle'. Have you configured your dialect correctly? 

How to reproduce

-- broken_pivot.sql
select * from (
    select
        times_purchased,
        state_code
    from customers
)
pivot
(
    count(state_code) as states
    for state_code in ('NY' as new_york, 'CT', 'NJ', 'FL', 'MO')
);
-- working_pivot.sql
select * from (
    select
        times_purchased,
        state_code
    from customers
)
pivot
(
    count(state_code)
    for state_code in ('NY' as new_york, 'CT', 'NJ', 'FL', 'MO')
);

The only difference between the two files is the count(state_code) line. One is aliased the other not.

$ sqlfluff lint working_pivot.sql broken_pivot.sql

I think the problem lies here:

Delimited(
Ref("FunctionSegment"), Ref("AliasExpressionSegment", optional=True)
),

Maybe a missing Sequence?

Dialect

oracle

Version

3.3.1

Configuration

[sqlfluff]
dialect = oracle

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 workingoracle

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions