#8608·sqlfluff

BigQuery - Missing support for WITH Expressions

Author: MartinLecatCreated Sep 29, 2026Updated Sep 29, 2026
Labelsenhancementbigquery

Search before asking

  • I searched the issues and found no similar issues.

Description

In BigQuery, the WITH keyword isn't just for CTEs, it can also be used as a way to create temporary variables when creating a column for example.

It is documented here.

Here is the configuration used for the following tests:

ini
[sqlfluff]
dialect = bigquery
templater = raw

And here the examples provided by the documentation with the parsing error message from SQLFluff:

sql
SELECT WITH(a AS '123',               -- a is '123'
            b AS CONCAT(a, '456'),    -- b is '123456'
            c AS '789',               -- c is '789'
            CONCAT(b, c)) AS result;  -- b + c is '123456789'
==== parsing violations ====
L:   1 | P:   8 |  PRS | Line 1, Position 8: Found unparsable section: "WITH(a AS '123',   
                       | -- a is '..."
WARNING: Parsing errors found and dialect is set to 'bigquery'. Have you configured your dialect correctly?

sql
SELECT WITH(a AS RAND(), a - a);
==== parsing violations ====
L:   1 | P:   8 |  PRS | Line 1, Position 8: Found unparsable section: 'WITH(a AS RAND(), a
                       | - a)'
WARNING: Parsing errors found and dialect is set to 'bigquery'. Have you configured your dialect correctly?

Use case

There's many thing we can imagine that would be nice to add in SQLFluff:

  • A rule for forbidding WITH expressions
  • A rule for encouraging WITH expressions when an expressions is getting lengthy or has repetitive bits
  • A rule for defining variable case (may not be pertinent, as we could follow column casing but just letting it here)
  • A rule for forbidding/forcing the use of "final variables":
sql
SELECT
    WITH(
        a AS 1,
        b AS 2,
        -- Here we create a "final variable" that will return the result
        result AS a + b,
        result
    ) AS foo,
    WITH(
        a AS 1,
        b AS 2,
        -- Here we skip the "final variable" as it's not mandatory
        a + b
    ) AS bar

Dialect

  • BigQuery
  • Maybe others ? I couldn't find anything but searching is "hard" as you always end up on CTEs pages...

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