Maintainability · rule sql-high-cte-count
SQL files with too many CTEs
A Metacenta review checks this under the rule SQL files stay readable. Everything below applies whether or not you ever commission one.
What this rule checks
For SQL projects without dbt, this rule flags files that write a relation and hold more than eight CTEs; eight passes. We count CTE definitions across every statement in the file. Assertion files are not counted.
Why it matters
Logic this dense is hard to test in pieces, and a wrong step is hard to find. A reviewer has to hold every CTE in mind to know what the output means.
How to fix it
Split the densest SQL files. Split the intermediate steps into their own relations, each written by its own file. Start with the steps other files could read too.
When it is fine to leave
A file of several short statements, each with a few CTEs, is counted as one total and may exceed the threshold while staying readable. So may a file of many one-line import CTEs. Leave either as it is.
What we need to check it
The .sql files, read from your repository with read-only access or uploaded. We tokenise the SQL rather than parse it, so the finding is medium confidence. Files over 500,000 characters are skipped.