Skip to content

Commit d1ad515

Browse files
EliSauderCopilot
andcommitted
fix(core): scope recursive CTE nullability by dialect
Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
1 parent bbc976e commit d1ad515

9 files changed

Lines changed: 71 additions & 4 deletions

File tree

‎internal/core/analyzer/analyzer.go‎

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -340,12 +340,14 @@ func (a *analyzer) bindCTEs(with *ast.WithClause) error {
340340
}
341341
rel := derivedRel(*cte.Ctename, cols)
342342
renameColumns(&rel, cte.Aliascolnames)
343-
// What a recursive CTE yields is the union of every round of it, so
344-
// no column stands for a single table column and any may be NULL.
343+
// A recursive CTE's output no longer has one source column. SQL
344+
// Server also reports all of its columns as nullable.
345345
if recursive {
346346
for i := range rel.cols {
347347
rel.cols[i].AttOID = 0
348-
rel.cols[i].NotNull = false
348+
if a.cat.RecursiveCTEsNullable() {
349+
rel.cols[i].NotNull = false
350+
}
349351
}
350352
}
351353
a.ctes[*cte.Ctename] = rel

‎internal/core/dialect.go‎

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,10 @@ const FlagQualifyDuplicateColumns = "columns.qualify_duplicates"
8282
// fill them with the type's default instead.
8383
const FlagOuterJoinNullable = "joins.outer_nullable"
8484

85+
// FlagRecursiveCTENullable marks a dialect whose recursive CTE columns are
86+
// nullable regardless of the nullability of their anchor and recursive terms.
87+
const FlagRecursiveCTENullable = "ctes.recursive_nullable"
88+
8589
// FlagReturningAliases holds, comma separated, the names a RETURNING list
8690
// may use for the statement's target.
8791
const FlagReturningAliases = "returning.aliases"
@@ -215,6 +219,16 @@ func (c *Catalog) OuterJoinsNullable() bool {
215219
return v == "true"
216220
}
217221

222+
// RecursiveCTEsNullable reports whether the dialect treats every column of a
223+
// recursive CTE as nullable, regardless of its terms' nullability.
224+
func (c *Catalog) RecursiveCTEsNullable() bool {
225+
if c.dialectOID == 0 {
226+
return false
227+
}
228+
v, _ := c.DialectFlag(c.dialectOID, FlagRecursiveCTENullable)
229+
return v == "true"
230+
}
231+
218232
// ReturningAliases lists the names a RETURNING list may qualify a column of
219233
// the statement's target with, as SQL Server's OUTPUT clause reads
220234
// INSERTED.id and DELETED.id.

‎internal/core/seed/seed.go‎

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,10 @@ type Settings struct {
8888
// nullable, as SQL Server fills an unmatched row with NULL.
8989
OuterJoinNullable bool `json:"outer_join_nullable,omitempty"`
9090

91+
// RecursiveCTENullable makes all recursive CTE columns nullable, as
92+
// SQL Server reports them regardless of their terms' nullability.
93+
RecursiveCTENullable bool `json:"recursive_cte_nullable,omitempty"`
94+
9195
// ReturningAliases are the names a RETURNING list may qualify a column
9296
// of the statement's target with, as SQL Server's OUTPUT clause has
9397
// INSERTED and DELETED.
@@ -674,6 +678,11 @@ func (b *builder) consts() error {
674678
return err
675679
}
676680
}
681+
if b.settings.RecursiveCTENullable {
682+
if err := b.cat.SetDialectFlag(b.dialectOID, core.FlagRecursiveCTENullable, "true"); err != nil {
683+
return err
684+
}
685+
}
677686
if len(b.settings.ReturningAliases) > 0 {
678687
if err := b.cat.SetDialectFlag(b.dialectOID, core.FlagReturningAliases, strings.Join(b.settings.ReturningAliases, ",")); err != nil {
679688
return err
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
{
2+
"command": "analyze",
3+
"args": ["--dialect", "postgresql", "--schema", "schema.sql", "query.sql"],
4+
"contexts": ["core"]
5+
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
-- name: NestedCTEScope :one
2+
WITH items AS (
3+
SELECT 1::integer AS value
4+
),
5+
derived AS (
6+
SELECT value FROM (
7+
WITH items AS (
8+
SELECT 'inner'::text AS value
9+
)
10+
SELECT value FROM items
11+
) AS nested
12+
)
13+
SELECT items.value AS outer_value, derived.value AS nested_value
14+
FROM items CROSS JOIN derived;
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
-- This query uses only CTEs and does not require table definitions.
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
[
2+
{
3+
"name": "NestedCTEScope",
4+
"cmd": ":one",
5+
"columns": [
6+
{
7+
"name": "outer_value",
8+
"type": {
9+
"name": "integer"
10+
}
11+
},
12+
{
13+
"name": "nested_value",
14+
"type": {
15+
"name": "text"
16+
}
17+
}
18+
],
19+
"params": []
20+
}
21+
]
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
{
22
"command": "analyze",
33
"args": ["--dialect", "mssql", "--schema", "schema.sql", "query.sql"],
4-
"contexts": ["base"]
4+
"contexts": ["base", "core"]
55
}

‎internal/engine/mssql/dialect/dialect.json‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
"bool": "bit",
1111
"limit": "bigint",
1212
"outer_join_nullable": true,
13+
"recursive_cte_nullable": true,
1314
"returning_aliases": ["inserted", "deleted"],
1415
"comparison": ["=", "<>", "!=", "<", "<=", ">", ">="],
1516
"comparison_categories": "NBSD",

0 commit comments

Comments
 (0)