fix(domain): hoist descendants filter preds into named CTEs (dolt 2.1.6 analyzer workaround)

Inlining the id IN (SELECT id FROM issues WHERE ...) filter into three or
more recursive-CTE branches trips a dolt 2.1.6 analyzer bug on the
recursive LIKE-join branch: 'unable to find field with index N in row of
M columns' (works on 2.1.0; minimal trigger is the branch-4 predicate
plus any two others). Hoisting each filter into a named non-recursive
CTE (issue_matches / wisp_matches) keeps the per-level filter semantics
and dodges the analyzer on both versions. Also de-duplicates the filter
args, which previously rode along once per branch.

Found by TestProxiedServerList/tree_parent against local dolt 2.1.6
while restoring the proxied CI lane (bd-6dnrw.44).

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
This commit is contained in:
Steve Yegge 2026-06-12 19:50:56 -04:00
commit 341c7a5a47

View file

@ -10,18 +10,28 @@ import (
)
type predBundle struct {
snippet string
args []any
// matchesCTE is a named non-recursive CTE ("<name> AS (SELECT id FROM
// <table> WHERE ...)") that the walk branches reference via snippet.
// Hoisting the filter subquery out of the recursive branches is load-
// bearing: inlining `id IN (SELECT id FROM issues WHERE ...)` into three
// or more branches trips a dolt 2.1.6 analyzer bug ("unable to find
// field with index N in row of M columns") on the recursive LIKE-join
// branch. The named CTE keeps the semantics (same per-level filter) and
// dodges the analyzer.
matchesCTE string
snippet string
args []any
}
func buildDescendantsPred(table, alias string, clauses []string, args []any) predBundle {
func buildDescendantsPred(table, alias, cteName string, clauses []string, args []any) predBundle {
if len(clauses) == 0 {
return predBundle{}
}
return predBundle{
snippet: fmt.Sprintf(" AND %s.id IN (SELECT id FROM %s WHERE %s)",
alias, table, strings.Join(clauses, " AND ")),
args: args,
matchesCTE: fmt.Sprintf("%s AS (SELECT id FROM %s WHERE %s)",
cteName, table, strings.Join(clauses, " AND ")),
snippet: fmt.Sprintf(" AND %s.id IN (SELECT id FROM %s)", alias, cteName),
args: args,
}
}
@ -58,10 +68,10 @@ func (r *issueSQLRepositoryImpl) GetDescendants(ctx context.Context, rootID stri
}
}
issuePred := buildDescendantsPred("issues", "i", issueWhereClauses, issueArgs)
issuePred := buildDescendantsPred("issues", "i", "issue_matches", issueWhereClauses, issueArgs)
var wispPred predBundle
if walkWisps {
wispPred = buildDescendantsPred("wisps", "w", wispWhereClauses, wispArgs)
wispPred = buildDescendantsPred("wisps", "w", "wisp_matches", wispWhereClauses, wispArgs)
}
cte, allArgs := buildDescendantsCTE(rootID, walkWisps, issuePred, wispPred)
@ -98,10 +108,23 @@ func (r *issueSQLRepositoryImpl) GetDescendants(ctx context.Context, rootID stri
// recursion only fires from 'e' rows — a dotted node's own dotted
// descendants share its prefix and were already matched by whichever LIKE
// found it, so re-expanding them would only multiply duplicate rows.
// Filter predicates are hoisted into named matches CTEs (see predBundle) to
// dodge a dolt 2.1.6 analyzer bug.
func buildDescendantsCTE(rootID string, walkWisps bool, issuePred, wispPred predBundle) (string, []any) {
var b strings.Builder
var args []any
b.WriteString("WITH RECURSIVE descendants AS (\n")
b.WriteString("WITH RECURSIVE ")
if issuePred.matchesCTE != "" {
b.WriteString(issuePred.matchesCTE)
b.WriteString(",\n")
args = append(args, issuePred.args...)
}
if walkWisps && wispPred.matchesCTE != "" {
b.WriteString(wispPred.matchesCTE)
b.WriteString(",\n")
args = append(args, wispPred.args...)
}
b.WriteString("descendants AS (\n")
fmt.Fprintf(&b, ` SELECT i.id, 'i' AS src, 'e' AS via
FROM issues i
@ -110,7 +133,6 @@ func buildDescendantsCTE(rootID string, walkWisps bool, issuePred, wispPred pred
AND COALESCE(d.depends_on_issue_id, d.depends_on_wisp_id) = ?
%s`, issuePred.snippet)
args = append(args, rootID)
args = append(args, issuePred.args...)
b.WriteString("\n UNION ALL\n")
fmt.Fprintf(&b, ` SELECT i.id, 'i' AS src, 'd' AS via
@ -119,7 +141,6 @@ func buildDescendantsCTE(rootID string, walkWisps bool, issuePred, wispPred pred
AND i.id NOT IN (SELECT issue_id FROM dependencies WHERE type = 'parent-child')
%s`, issuePred.snippet)
args = append(args, rootID)
args = append(args, issuePred.args...)
if walkWisps {
b.WriteString("\n UNION ALL\n")
@ -130,7 +151,6 @@ func buildDescendantsCTE(rootID string, walkWisps bool, issuePred, wispPred pred
AND COALESCE(wd.depends_on_issue_id, wd.depends_on_wisp_id) = ?
%s`, wispPred.snippet)
args = append(args, rootID)
args = append(args, wispPred.args...)
b.WriteString("\n UNION ALL\n")
fmt.Fprintf(&b, ` SELECT w.id, 'w' AS src, 'd' AS via
@ -139,7 +159,6 @@ func buildDescendantsCTE(rootID string, walkWisps bool, issuePred, wispPred pred
AND w.id NOT IN (SELECT issue_id FROM wisp_dependencies WHERE type = 'parent-child')
%s`, wispPred.snippet)
args = append(args, rootID)
args = append(args, wispPred.args...)
}
b.WriteString("\n UNION ALL\n")
@ -150,7 +169,6 @@ func buildDescendantsCTE(rootID string, walkWisps bool, issuePred, wispPred pred
JOIN descendants p ON COALESCE(d.depends_on_issue_id, d.depends_on_wisp_id) = p.id
WHERE d.type = 'parent-child'
%s`, issuePred.snippet)
args = append(args, issuePred.args...)
b.WriteString("\n UNION ALL\n")
fmt.Fprintf(&b, ` SELECT i.id, 'i' AS src, 'd' AS via
@ -159,7 +177,6 @@ func buildDescendantsCTE(rootID string, walkWisps bool, issuePred, wispPred pred
WHERE p.via = 'e'
AND i.id NOT IN (SELECT issue_id FROM dependencies WHERE type = 'parent-child')
%s`, issuePred.snippet)
args = append(args, issuePred.args...)
if walkWisps {
b.WriteString("\n UNION ALL\n")
@ -169,7 +186,6 @@ func buildDescendantsCTE(rootID string, walkWisps bool, issuePred, wispPred pred
JOIN descendants p ON COALESCE(wd.depends_on_issue_id, wd.depends_on_wisp_id) = p.id
WHERE wd.type = 'parent-child'
%s`, wispPred.snippet)
args = append(args, wispPred.args...)
b.WriteString("\n UNION ALL\n")
fmt.Fprintf(&b, ` SELECT w.id, 'w' AS src, 'd' AS via
@ -178,7 +194,6 @@ func buildDescendantsCTE(rootID string, walkWisps bool, issuePred, wispPred pred
WHERE p.via = 'e'
AND w.id NOT IN (SELECT issue_id FROM wisp_dependencies WHERE type = 'parent-child')
%s`, wispPred.snippet)
args = append(args, wispPred.args...)
}
b.WriteString("\n)\nSELECT id, src FROM descendants\n")