Skip to content

Bug: using recursive CTE to solve sudoku returns error #18237

Open
@SkyFan2002

Description

@SkyFan2002

Summary

root@localhost:8000/default> WITH RECURSIVE input(sud) AS (
    VALUES('53..7....6..195....98....6.8...6...34..8.3..17...2...6.6....28....419..5....8..79')
),
digits(z, lp) AS (
    VALUES('1', 1)
    UNION ALL SELECT CAST(lp+1 AS TEXT), lp+1 FROM digits WHERE lp<9
),
x(s, ind) AS (
    SELECT sud, strpos(sud, '.') FROM input
    UNION ALL
    SELECT
        substr(s, 1, ind-1) || z || substr(s, ind+1),
        strpos(substr(s, 1, ind-1) || z || substr(s, ind+1), '.')
    FROM x, digits AS z
    WHERE ind>0
    AND NOT EXISTS (
        SELECT 1 FROM digits AS lp
        WHERE z.z = substr(s, ((ind-1)/9)::int*9 + lp, 1)
        OR z.z = substr(s, ((ind-1)%9) + (lp-1)*9 + 1, 1)
        OR z.z = substr(s, 
            (((ind-1)/27)::int * 27) +
            ((((ind-1)%9)/3)::int * 3) +
            ((lp-1)/3)::int * 9 +
            ((lp-1)%3) + 1, 1)
    )
)
SELECT s FROM x WHERE ind=0;

error: APIError: QueryFailed: [1104]index out of bounds: the len is 0 but the index is 0

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions