pythonpostgresqlsqlfluff

SQLfluff parsing violation when aliasing column from generate_series()


On using sqlfluff to lint postgreSQL query, ended up in a parsing violation while attempting to set alias for column generated via generate_series(). Query works fine though.

Not sure of how to set alias for columns generated via generate_series(),

CREATE MATERIALIZED VIEW dummy AS
WITH base_times AS (
    SELECT
        (CURRENT_TIMESTAMP AT TIME ZONE 'america/los_angeles' + INTERVAL '2 hours')::date AS base_start,
        (CURRENT_TIMESTAMP AT TIME ZONE 'america/los_angeles' + INTERVAL '2 hours' - INTERVAL '12 weeks' + INTERVAL '1 day')::date AS base_end,
        (CURRENT_TIMESTAMP AT TIME ZONE 'america/los_angeles' + INTERVAL '2 hours' - INTERVAL '1 day')::date AS base_next_start
),
bucket AS (
    SELECT (generate_series(
        time_bucket('7 days', base_end, base_start)::date,
        time_bucket('7 days', base_next_start, base_start)::date,
        INTERVAL '7 days'
    ) + INTERVAL '6 days')::date AS end
    FROM base_times
) select * from bucket;

Error:

[L: 14, P:  1]      |                                                            whitespace:  '    '
[L: 14, P:  5]      |                                                            [META] dedent:
[L: 14, P:  5]      |                                                            end_bracket:  ')'
[L: 14, P:  6]      |                                                    whitespace:  ' '
[L: 14, P:  7]      |                                                    binary_operator:  '+'
[L: 14, P:  8]      |                                                    whitespace:  ' '
[L: 14, P:  9]      |                                                    datetime_literal:
[L: 14, P:  9]      |                                                        datetime_type_identifier:
[L: 14, P:  9]      |                                                            keyword:  'INTERVAL'
[L: 14, P: 17]      |                                                        whitespace:  ' '
[L: 14, P: 18]      |                                                        quoted_literal:  "'6 days'"
[L: 14, P: 26]      |                                                [META] dedent:
[L: 14, P: 26]      |                                                end_bracket:  ')'
[L: 14, P: 27]      |                                            casting_operator:  '::'
[L: 14, P: 29]      |                                            data_type:
[L: 14, P: 29]      |                                                datetime_type_identifier:
[L: 14, P: 29]      |                                                    keyword:  'date'
[L: 14, P: 33]      |                                [META] dedent:
[L: 14, P: 33]      |                                whitespace:                   ' '
[L: 14, P: 34]      |                                unparsable:                   !! Expected: 'Nothing here.'
[L: 14, P: 34]      |                                    word:                     'AS'
[L: 14, P: 36]      |                                    whitespace:               ' '
[L: 14, P: 37]      |                                    word:                     'end'
[L: 14, P: 40]      |                            newline:                          '\n'

==== parsing violations ====
L:  14 | P:  34 |  PRS | Line 14, Position 34: Found unparsable section: 'AS end'

Solution

  • The list of PostgreSQL Key Words defines end as reserved.

    SQL distinguishes between reserved and non-reserved key words. According to the standard, reserved key words are the only real key words; they are never allowed as identifiers

    So I suspect the real cause of the problem is the illegal column name end and you should quote it or change.

    But you are right e.g. this query works perfect in 14.10

    select 1 as end